From 8c1c25a1a9ba4ec89d0b0d3bec414f61e50b17ed Mon Sep 17 00:00:00 2001 From: Sebastien Lavoie Date: Sun, 29 Mar 2026 08:49:00 -0400 Subject: [PATCH] refactor: fix inner asyncio import and expose entry_id publicly on coordinator Co-Authored-By: Claude Sonnet 4.6 --- custom_components/dkncloudna/coordinator.py | 1 + custom_components/dkncloudna/entity.py | 13 ++++++------- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/custom_components/dkncloudna/coordinator.py b/custom_components/dkncloudna/coordinator.py index 78a2e7c..2565f64 100644 --- a/custom_components/dkncloudna/coordinator.py +++ b/custom_components/dkncloudna/coordinator.py @@ -43,6 +43,7 @@ class DknCoordinator(DataUpdateCoordinator[dict[str, dict[str, Any]]]): ) self.client = client self._entry = entry + self.entry_id = entry.entry_id async def _async_update_data(self) -> dict[str, dict[str, Any]]: """Fetch all installations and flatten into {mac: device_dict}.""" diff --git a/custom_components/dkncloudna/entity.py b/custom_components/dkncloudna/entity.py index cba7ec4..ca7fd92 100644 --- a/custom_components/dkncloudna/entity.py +++ b/custom_components/dkncloudna/entity.py @@ -53,7 +53,7 @@ class DknEntity(CoordinatorEntity[DknCoordinator]): def _get_device_lock(self) -> asyncio.Lock: """Return (creating if needed) the asyncio.Lock for this device.""" 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", {}) if self._mac not in locks: @@ -67,7 +67,7 @@ class DknEntity(CoordinatorEntity[DknCoordinator]): def _optimistic_set(self, key: str, value: Any) -> None: """Store a locally-set value with a TTL timestamp.""" 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", {}) device_overlays = overlays.setdefault(self._mac, {}) @@ -76,7 +76,7 @@ class DknEntity(CoordinatorEntity[DknCoordinator]): def _optimistic_get(self, key: str, fallback: Any) -> Any: """Return the optimistic value if still fresh, else fallback.""" bucket = self.hass.data.get(DOMAIN, {}).get( - self.coordinator._entry.entry_id, {} + self.coordinator.entry_id, {} ) overlays = bucket.get("optimistic", {}).get(self._mac, {}) entry = overlays.get(key) @@ -87,7 +87,7 @@ class DknEntity(CoordinatorEntity[DknCoordinator]): def _optimistic_clear(self, key: str) -> None: """Expire an optimistic overlay immediately.""" bucket = self.hass.data.get(DOMAIN, {}).get( - self.coordinator._entry.entry_id, {} + self.coordinator.entry_id, {} ) overlays = bucket.get("optimistic", {}).get(self._mac, {}) overlays.pop(key, None) @@ -102,15 +102,14 @@ class DknEntity(CoordinatorEntity[DknCoordinator]): Multiple calls within the window collapse into a single refresh. """ bucket = self.hass.data.setdefault(DOMAIN, {}).setdefault( - self.coordinator._entry.entry_id, {} + self.coordinator.entry_id, {} ) existing: asyncio.Task | None = bucket.get("pending_refresh") if existing and not existing.done(): return 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() bucket["pending_refresh"] = self.hass.async_create_task(_do_refresh())