Skip to content

Commit fefac73

Browse files
committed
simplification
1 parent 69329f3 commit fefac73

9 files changed

Lines changed: 424 additions & 261 deletions

File tree

custom_components/august_access_codes/__init__.py

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""The August Access integration."""
22

3+
from asyncio import gather
4+
from collections.abc import Mapping
35
from dataclasses import asdict
46
import logging
57
from typing import cast
@@ -26,16 +28,17 @@
2628
import homeassistant.helpers.entity_registry as er
2729
from homeassistant.helpers.typing import ConfigType
2830

29-
from .api import SeamAPI
31+
from .api import SeamAPI, SeamDeviceID
3032
from .const import (
3133
CREATE_SERVICE_SCHEMA,
3234
DELETE_SERVICE_SCHEMA,
3335
DOMAIN,
3436
MODIFY_SERVICE_SCHEMA,
3537
AugustEntityFeature,
3638
)
39+
from .coordinator import AccessCodeCoordinator
3740

38-
type AugustAccessConfigEntry = ConfigEntry[SeamAPI]
41+
type AugustAccessConfigEntry = ConfigEntry[Mapping[SeamDeviceID, AccessCodeCoordinator]]
3942

4043
_LOGGER = logging.getLogger(__name__)
4144

@@ -75,7 +78,7 @@ def _get_api(call: ServiceCall) -> SeamAPI:
7578
raise ServiceValidationError("entry_not_found")
7679
if entry.state is not ConfigEntryState.LOADED:
7780
raise ServiceValidationError("entry_not_loaded")
78-
return cast(AugustAccessConfigEntry, entry).runtime_data
81+
return list(cast(AugustAccessConfigEntry, entry).runtime_data.values())[0].api
7982

8083
async def _create_access_code(call: ServiceCall) -> ServiceResponse:
8184
"""Create access code service."""
@@ -168,9 +171,22 @@ async def async_setup_entry(
168171

169172
seam_api: SeamAPI = await SeamAPI.auth(hass=hass, entry=entry)
170173

171-
entry.runtime_data = seam_api
174+
coordinators: dict[SeamDeviceID, AccessCodeCoordinator] = {
175+
seam_device.device_id: AccessCodeCoordinator(hass, seam_api, seam_device)
176+
for seam_device in await seam_api.async_get_devices()
177+
}
172178

173-
await hass.config_entries.async_forward_entry_setups(entry, [Platform.SENSOR])
179+
await gather(
180+
*[
181+
coordinator.async_config_entry_first_refresh()
182+
for coordinator in coordinators.values()
183+
]
184+
)
185+
entry.runtime_data = coordinators
186+
187+
await hass.config_entries.async_forward_entry_setups(
188+
entry, [Platform.BINARY_SENSOR, Platform.SENSOR]
189+
)
174190

175191
return True
176192

@@ -179,7 +195,13 @@ async def async_unload_entry(
179195
hass: HomeAssistant, entry: AugustAccessConfigEntry
180196
) -> bool:
181197
"""Unload a config entry."""
182-
return await entry.runtime_data.unload()
198+
# Close api
199+
await entry.runtime_data.values()[0].api.async_close()
200+
# Shutdown coordinators to remove listeners
201+
gather(
202+
*[coordinator.async_shutdown() for coordinator in entry.runtime_data.values()]
203+
)
204+
return True
183205

184206

185207
__all__ = ["AugustEntityFeature"]

0 commit comments

Comments
 (0)