mirror of
https://github.com/python-kasa/python-kasa.git
synced 2025-08-09 20:24:02 +00:00
Allow calling update directly on child devices and skipping updates on the parent
This commit is contained in:
@@ -19,6 +19,8 @@ class SmartChildDevice(SmartDevice):
|
||||
This wraps the protocol communications and sets internal data for the child.
|
||||
"""
|
||||
|
||||
_parent: SmartDevice
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
parent: SmartDevice,
|
||||
@@ -34,12 +36,26 @@ class SmartChildDevice(SmartDevice):
|
||||
self._id = info["device_id"]
|
||||
self.protocol = _ChildProtocolWrapper(self._id, parent.protocol)
|
||||
|
||||
async def update(self, update_children: bool = True):
|
||||
async def update(self, update_children: bool = True, update_parent: bool = True):
|
||||
"""Update the device.
|
||||
|
||||
Calling update directly on a child device will update the parent
|
||||
and only this child.
|
||||
"""
|
||||
if update_parent:
|
||||
await self._parent._update(update_children=False, called_from_child=self)
|
||||
else:
|
||||
await self._update()
|
||||
|
||||
async def _update(self):
|
||||
"""Update child module info.
|
||||
|
||||
The parent updates our internal info so just update modules with
|
||||
their own queries.
|
||||
"""
|
||||
# Hubs attached devices only update via the parent hub
|
||||
if self._parent.device_type == DeviceType.Hub:
|
||||
return
|
||||
req: dict[str, Any] = {}
|
||||
for module in self.modules.values():
|
||||
if mod_query := module.query():
|
||||
|
@@ -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)
|
||||
|
Reference in New Issue
Block a user