From d4a361dd3e3aad1aa92fd4758e23ff7af23f1122 Mon Sep 17 00:00:00 2001 From: dlee1j1 <53192782+dlee1j1@users.noreply.github.com> Date: Sat, 6 Feb 2021 07:14:36 -0800 Subject: [PATCH] Leverage data from UDP discovery to initialize device structure (#132) * avoid talking to devices after UDP discovery * formatting fix * more formatting * more formatting changes * undo gitignore changes * fixing git ignore for black Co-authored-by: dlee1j1 --- kasa/cli.py | 3 +-- kasa/discover.py | 2 +- kasa/smartdevice.py | 5 +++++ 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/kasa/cli.py b/kasa/cli.py index 167179e3..5efd6be6 100755 --- a/kasa/cli.py +++ b/kasa/cli.py @@ -174,13 +174,12 @@ async def dump_discover(ctx, scrub): async def discover(ctx, timeout, discover_only, dump_raw): """Discover devices in the network.""" target = ctx.parent.params["target"] - click.echo(f"Discovering devices for {timeout} seconds") + click.echo(f"Discovering devices on {target} for {timeout} seconds") found_devs = await Discover.discover( target=target, timeout=timeout, return_raw=dump_raw ) if not discover_only: for ip, dev in found_devs.items(): - await dev.update() if dump_raw: click.echo(dev) continue diff --git a/kasa/discover.py b/kasa/discover.py index e4091512..3e7f7020 100755 --- a/kasa/discover.py +++ b/kasa/discover.py @@ -77,7 +77,7 @@ class _DiscoverProtocol(asyncio.DatagramProtocol): device_class = Discover._get_device_class(info) device = device_class(ip) - asyncio.ensure_future(device.update()) + device.update_from_discover_info(info) self.discovered_devices[ip] = device self.discovered_devices_raw[ip] = info diff --git a/kasa/smartdevice.py b/kasa/smartdevice.py index 19589bba..da5c0f65 100755 --- a/kasa/smartdevice.py +++ b/kasa/smartdevice.py @@ -304,6 +304,11 @@ class SmartDevice: # TODO: keep accessible for tests self._sys_info = self._last_update["system"]["get_sysinfo"] + def update_from_discover_info(self, info): + """Update state from info from the discover call.""" + self._last_update = info + self._sys_info = info["system"]["get_sysinfo"] + @property # type: ignore @requires_update def sys_info(self) -> Dict[str, Any]: