refactor: fix inner asyncio import and expose entry_id publicly on coordinator

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Sebastien Lavoie
2026-03-29 08:49:00 -04:00
parent 60f7e1e980
commit 8c1c25a1a9
2 changed files with 7 additions and 7 deletions
@@ -43,6 +43,7 @@ class DknCoordinator(DataUpdateCoordinator[dict[str, dict[str, Any]]]):
) )
self.client = client self.client = client
self._entry = entry self._entry = entry
self.entry_id = entry.entry_id
async def _async_update_data(self) -> dict[str, dict[str, Any]]: async def _async_update_data(self) -> dict[str, dict[str, Any]]:
"""Fetch all installations and flatten into {mac: device_dict}.""" """Fetch all installations and flatten into {mac: device_dict}."""
+6 -7
View File
@@ -53,7 +53,7 @@ class DknEntity(CoordinatorEntity[DknCoordinator]):
def _get_device_lock(self) -> asyncio.Lock: def _get_device_lock(self) -> asyncio.Lock:
"""Return (creating if needed) the asyncio.Lock for this device.""" """Return (creating if needed) the asyncio.Lock for this device."""
bucket = self.hass.data.setdefault(DOMAIN, {}).setdefault( bucket = self.hass.data.setdefault(DOMAIN, {}).setdefault(
self.coordinator._entry.entry_id, {} self.coordinator.entry_id, {}
) )
locks: dict[str, asyncio.Lock] = bucket.setdefault("device_locks", {}) locks: dict[str, asyncio.Lock] = bucket.setdefault("device_locks", {})
if self._mac not in locks: if self._mac not in locks:
@@ -67,7 +67,7 @@ class DknEntity(CoordinatorEntity[DknCoordinator]):
def _optimistic_set(self, key: str, value: Any) -> None: def _optimistic_set(self, key: str, value: Any) -> None:
"""Store a locally-set value with a TTL timestamp.""" """Store a locally-set value with a TTL timestamp."""
bucket = self.hass.data.setdefault(DOMAIN, {}).setdefault( bucket = self.hass.data.setdefault(DOMAIN, {}).setdefault(
self.coordinator._entry.entry_id, {} self.coordinator.entry_id, {}
) )
overlays: dict[str, dict[str, Any]] = bucket.setdefault("optimistic", {}) overlays: dict[str, dict[str, Any]] = bucket.setdefault("optimistic", {})
device_overlays = overlays.setdefault(self._mac, {}) device_overlays = overlays.setdefault(self._mac, {})
@@ -76,7 +76,7 @@ class DknEntity(CoordinatorEntity[DknCoordinator]):
def _optimistic_get(self, key: str, fallback: Any) -> Any: def _optimistic_get(self, key: str, fallback: Any) -> Any:
"""Return the optimistic value if still fresh, else fallback.""" """Return the optimistic value if still fresh, else fallback."""
bucket = self.hass.data.get(DOMAIN, {}).get( bucket = self.hass.data.get(DOMAIN, {}).get(
self.coordinator._entry.entry_id, {} self.coordinator.entry_id, {}
) )
overlays = bucket.get("optimistic", {}).get(self._mac, {}) overlays = bucket.get("optimistic", {}).get(self._mac, {})
entry = overlays.get(key) entry = overlays.get(key)
@@ -87,7 +87,7 @@ class DknEntity(CoordinatorEntity[DknCoordinator]):
def _optimistic_clear(self, key: str) -> None: def _optimistic_clear(self, key: str) -> None:
"""Expire an optimistic overlay immediately.""" """Expire an optimistic overlay immediately."""
bucket = self.hass.data.get(DOMAIN, {}).get( bucket = self.hass.data.get(DOMAIN, {}).get(
self.coordinator._entry.entry_id, {} self.coordinator.entry_id, {}
) )
overlays = bucket.get("optimistic", {}).get(self._mac, {}) overlays = bucket.get("optimistic", {}).get(self._mac, {})
overlays.pop(key, None) overlays.pop(key, None)
@@ -102,15 +102,14 @@ class DknEntity(CoordinatorEntity[DknCoordinator]):
Multiple calls within the window collapse into a single refresh. Multiple calls within the window collapse into a single refresh.
""" """
bucket = self.hass.data.setdefault(DOMAIN, {}).setdefault( bucket = self.hass.data.setdefault(DOMAIN, {}).setdefault(
self.coordinator._entry.entry_id, {} self.coordinator.entry_id, {}
) )
existing: asyncio.Task | None = bucket.get("pending_refresh") existing: asyncio.Task | None = bucket.get("pending_refresh")
if existing and not existing.done(): if existing and not existing.done():
return return
async def _do_refresh() -> None: async def _do_refresh() -> None:
import asyncio as _asyncio await asyncio.sleep(POST_WRITE_REFRESH_DELAY_SEC)
await _asyncio.sleep(POST_WRITE_REFRESH_DELAY_SEC)
await self.coordinator.async_request_refresh() await self.coordinator.async_request_refresh()
bucket["pending_refresh"] = self.hass.async_create_task(_do_refresh()) bucket["pending_refresh"] = self.hass.async_create_task(_do_refresh())