mirror of
https://github.com/home-assistant/core.git
synced 2025-07-25 06:07:17 +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 import AuthenticationError, Credentials, Device, KasaException
|
||||||
from kasa.iot import IotStrip
|
from kasa.iot import IotStrip
|
||||||
|
|
||||||
|
from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.exceptions import ConfigEntryAuthFailed
|
from homeassistant.exceptions import ConfigEntryAuthFailed
|
||||||
@ -46,11 +47,9 @@ class TPLinkDataUpdateCoordinator(DataUpdateCoordinator[None]):
|
|||||||
device: Device,
|
device: Device,
|
||||||
update_interval: timedelta,
|
update_interval: timedelta,
|
||||||
config_entry: TPLinkConfigEntry,
|
config_entry: TPLinkConfigEntry,
|
||||||
parent_coordinator: TPLinkDataUpdateCoordinator | None = None,
|
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize DataUpdateCoordinator to gather data for specific SmartPlug."""
|
"""Initialize DataUpdateCoordinator to gather data for specific SmartPlug."""
|
||||||
self.device = device
|
self.device = device
|
||||||
self.parent_coordinator = parent_coordinator
|
|
||||||
|
|
||||||
# The iot HS300 allows a limited number of concurrent requests and
|
# The iot HS300 allows a limited number of concurrent requests and
|
||||||
# fetching the emeter information requires separate ones, so child
|
# fetching the emeter information requires separate ones, so child
|
||||||
@ -97,12 +96,6 @@ class TPLinkDataUpdateCoordinator(DataUpdateCoordinator[None]):
|
|||||||
) from ex
|
) from ex
|
||||||
|
|
||||||
await self._process_child_devices()
|
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:
|
async def _process_child_devices(self) -> None:
|
||||||
"""Process child devices and remove stale devices."""
|
"""Process child devices and remove stale devices."""
|
||||||
@ -131,20 +124,19 @@ class TPLinkDataUpdateCoordinator(DataUpdateCoordinator[None]):
|
|||||||
def get_child_coordinator(
|
def get_child_coordinator(
|
||||||
self,
|
self,
|
||||||
child: Device,
|
child: Device,
|
||||||
|
platform_domain: str,
|
||||||
) -> TPLinkDataUpdateCoordinator:
|
) -> TPLinkDataUpdateCoordinator:
|
||||||
"""Get separate child coordinator for a device or self if not needed."""
|
"""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
|
# The iot HS300 allows a limited number of concurrent requests and fetching the
|
||||||
# emeter information requires separate ones so create child coordinators here.
|
# 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)):
|
if not (child_coordinator := self._child_coordinators.get(child.device_id)):
|
||||||
# The child coordinators only update energy data so we can
|
# The child coordinators only update energy data so we can
|
||||||
# set a longer update interval to avoid flooding the device
|
# set a longer update interval to avoid flooding the device
|
||||||
child_coordinator = TPLinkDataUpdateCoordinator(
|
child_coordinator = TPLinkDataUpdateCoordinator(
|
||||||
self.hass,
|
self.hass, child, timedelta(seconds=60), self.config_entry
|
||||||
child,
|
|
||||||
timedelta(seconds=60),
|
|
||||||
self.config_entry,
|
|
||||||
parent_coordinator=self,
|
|
||||||
)
|
)
|
||||||
self._child_coordinators[child.device_id] = child_coordinator
|
self._child_coordinators[child.device_id] = child_coordinator
|
||||||
return child_coordinator
|
return child_coordinator
|
||||||
|
@ -151,13 +151,7 @@ def async_refresh_after[_T: CoordinatedTPLinkEntity, **_P](
|
|||||||
"exc": str(ex),
|
"exc": str(ex),
|
||||||
},
|
},
|
||||||
) from ex
|
) from ex
|
||||||
coordinator = self.coordinator
|
await self.coordinator.async_request_refresh()
|
||||||
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()
|
|
||||||
|
|
||||||
return _async_wrap
|
return _async_wrap
|
||||||
|
|
||||||
@ -514,7 +508,9 @@ class CoordinatedTPLinkFeatureEntity(CoordinatedTPLinkEntity, ABC):
|
|||||||
)
|
)
|
||||||
|
|
||||||
for child in children:
|
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(
|
child_entities = cls._entities_for_device(
|
||||||
hass,
|
hass,
|
||||||
@ -657,7 +653,9 @@ class CoordinatedTPLinkModuleEntity(CoordinatedTPLinkEntity, ABC):
|
|||||||
device.host,
|
device.host,
|
||||||
)
|
)
|
||||||
for child in children:
|
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(
|
child_entities: list[_E] = cls._entities_for_device(
|
||||||
hass,
|
hass,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user