mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 05:07:41 +00:00
Fix tplink iot strip sensor refresh (#138375)
This commit is contained in:
parent
ff5ddce7b0
commit
d9108cc003
@ -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
|
||||
|
@ -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,
|
||||
|
Loading…
x
Reference in New Issue
Block a user