From d9108cc0035e05ae0560114382c753862ef317da Mon Sep 17 00:00:00 2001 From: "Steven B." <51370195+sdb9696@users.noreply.github.com> Date: Wed, 12 Feb 2025 17:46:11 +0000 Subject: [PATCH] Fix tplink iot strip sensor refresh (#138375) --- .../components/tplink/coordinator.py | 20 ++++++------------- homeassistant/components/tplink/entity.py | 16 +++++++-------- 2 files changed, 13 insertions(+), 23 deletions(-) diff --git a/homeassistant/components/tplink/coordinator.py b/homeassistant/components/tplink/coordinator.py index fcd1335a77a..1a7b40457f0 100644 --- a/homeassistant/components/tplink/coordinator.py +++ b/homeassistant/components/tplink/coordinator.py @@ -9,6 +9,7 @@ import logging from kasa import AuthenticationError, Credentials, Device, KasaException from kasa.iot import IotStrip +from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant from homeassistant.exceptions import ConfigEntryAuthFailed @@ -46,11 +47,9 @@ class TPLinkDataUpdateCoordinator(DataUpdateCoordinator[None]): device: Device, update_interval: timedelta, config_entry: TPLinkConfigEntry, - parent_coordinator: TPLinkDataUpdateCoordinator | None = None, ) -> None: """Initialize DataUpdateCoordinator to gather data for specific SmartPlug.""" self.device = device - self.parent_coordinator = parent_coordinator # The iot HS300 allows a limited number of concurrent requests and # fetching the emeter information requires separate ones, so child @@ -97,12 +96,6 @@ class TPLinkDataUpdateCoordinator(DataUpdateCoordinator[None]): ) from ex await self._process_child_devices() - if not self._update_children: - # If the children are not being updated, it means this is an - # IotStrip, and we need to tell the children to write state - # since the power state is provided by the parent. - for child_coordinator in self._child_coordinators.values(): - child_coordinator.async_set_updated_data(None) async def _process_child_devices(self) -> None: """Process child devices and remove stale devices.""" @@ -131,20 +124,19 @@ class TPLinkDataUpdateCoordinator(DataUpdateCoordinator[None]): def get_child_coordinator( self, child: Device, + platform_domain: str, ) -> TPLinkDataUpdateCoordinator: """Get separate child coordinator for a device or self if not needed.""" # The iot HS300 allows a limited number of concurrent requests and fetching the # emeter information requires separate ones so create child coordinators here. - if isinstance(self.device, IotStrip): + # This does not happen for switches as the state is available on the + # parent device info. + if isinstance(self.device, IotStrip) and platform_domain != SWITCH_DOMAIN: if not (child_coordinator := self._child_coordinators.get(child.device_id)): # The child coordinators only update energy data so we can # set a longer update interval to avoid flooding the device child_coordinator = TPLinkDataUpdateCoordinator( - self.hass, - child, - timedelta(seconds=60), - self.config_entry, - parent_coordinator=self, + self.hass, child, timedelta(seconds=60), self.config_entry ) self._child_coordinators[child.device_id] = child_coordinator return child_coordinator diff --git a/homeassistant/components/tplink/entity.py b/homeassistant/components/tplink/entity.py index 7a0d811b30d..7c1e9e72b85 100644 --- a/homeassistant/components/tplink/entity.py +++ b/homeassistant/components/tplink/entity.py @@ -151,13 +151,7 @@ def async_refresh_after[_T: CoordinatedTPLinkEntity, **_P]( "exc": str(ex), }, ) from ex - coordinator = self.coordinator - if coordinator.parent_coordinator: - # If there is a parent coordinator we need to refresh - # the parent as its what provides the power state data - # for the child entities. - coordinator = coordinator.parent_coordinator - await coordinator.async_request_refresh() + await self.coordinator.async_request_refresh() return _async_wrap @@ -514,7 +508,9 @@ class CoordinatedTPLinkFeatureEntity(CoordinatedTPLinkEntity, ABC): ) for child in children: - child_coordinator = coordinator.get_child_coordinator(child) + child_coordinator = coordinator.get_child_coordinator( + child, platform_domain + ) child_entities = cls._entities_for_device( hass, @@ -657,7 +653,9 @@ class CoordinatedTPLinkModuleEntity(CoordinatedTPLinkEntity, ABC): device.host, ) for child in children: - child_coordinator = coordinator.get_child_coordinator(child) + child_coordinator = coordinator.get_child_coordinator( + child, platform_domain + ) child_entities: list[_E] = cls._entities_for_device( hass,