Allow calling update directly on child devices and skipping updates on the parent

This commit is contained in:
sdb9696
2024-06-28 19:25:39 +01:00
parent 2a62849987
commit ec1082a228
5 changed files with 124 additions and 13 deletions

View File

@@ -147,8 +147,14 @@ class SmartDevice(Device):
if "child_device" in self._components and not self.children:
await self._initialize_children()
async def update(self, update_children: bool = False):
async def update(self, update_children: bool = True, update_parent: bool = True):
"""Update the device."""
await self._update(update_children)
async def _update(
self, update_children: bool = True, called_from_child: SmartDevice | None = None
):
"""If called from a child device will only update that child."""
if self.credentials is None and self.credentials_hash is None:
raise AuthenticationError("Tapo plug requires authentication.")
@@ -167,11 +173,13 @@ class SmartDevice(Device):
self._info = self._try_get_response(resp, "get_device_info")
# Call child update which will only update module calls, info is updated
# from get_child_device_list. update_children only affects hub devices, other
# devices will always update children to prevent errors on module access.
if update_children or self.device_type != DeviceType.Hub:
# from get_child_device_list. If this method is being called by a child
# it will only call update on that child
if called_from_child:
await called_from_child._update()
elif update_children:
for child in self._children.values():
await child.update()
await child._update()
if child_info := self._try_get_response(resp, "get_child_device_list", {}):
for info in child_info["child_device_list"]:
self._children[info["device_id"]]._update_internal_state(info)