mirror of
https://github.com/python-kasa/python-kasa.git
synced 2025-10-13 02:48:07 +00:00
Remove sync interface, add asyncio discovery (#14)
* do not update inside __repr__ * Convert discovery to asyncio * Use asyncio.DatagramProtocol * Cleanup parameters, no more positional arguments Closes #7 * Remove sync interface * This requires #13 to be merged. Closes #12. * Converts cli to use asyncio.run() where needed. * The children from smartstrips is being initialized during the first update call. * Convert on and off commands to use asyncio.run * conftest: do the initial update automatically for the device, cleans up tests a bit * return subdevices alias for strip plugs, remove sync from docstrings * Make tests pass using pytest-asyncio * Simplify tests and use pytest-asyncio. * Removed the emeter tests for child devices, as this information do not seem to exist (based on the dummy sysinfo data). Can be added again if needed. * Remove sync from docstrings. * Fix incorrect type hint * Add type hints and some docstrings to discovery
This commit is contained in:
@@ -22,22 +22,21 @@ class SmartStrip(SmartPlug):
|
||||
p = SmartStrip("192.168.1.105")
|
||||
|
||||
# query the state of the strip
|
||||
await p.update()
|
||||
print(p.is_on)
|
||||
|
||||
# change state of all outlets
|
||||
p.sync.turn_on()
|
||||
p.sync.turn_off()
|
||||
await p.turn_on()
|
||||
await p.turn_off()
|
||||
|
||||
# individual outlets are accessible through plugs variable
|
||||
for plug in p.plugs:
|
||||
print(f"{p}: {p.is_on}")
|
||||
|
||||
# change state of a single outlet
|
||||
p.plugs[0].sync.turn_on()
|
||||
await p.plugs[0].turn_on()
|
||||
```
|
||||
|
||||
Omit the `sync` attribute to get coroutines.
|
||||
|
||||
Errors reported by the device are raised as SmartDeviceExceptions,
|
||||
and should be handled by the user of the library.
|
||||
"""
|
||||
@@ -53,18 +52,6 @@ class SmartStrip(SmartPlug):
|
||||
self.emeter_type = "emeter"
|
||||
self._device_type = DeviceType.Strip
|
||||
self.plugs: List[SmartPlug] = []
|
||||
children = self.sync.get_sys_info()["children"]
|
||||
self.num_children = len(children)
|
||||
for child in children:
|
||||
self.plugs.append(
|
||||
SmartPlug(
|
||||
host,
|
||||
protocol,
|
||||
context=child["id"],
|
||||
cache_ttl=cache_ttl,
|
||||
ioloop=ioloop,
|
||||
)
|
||||
)
|
||||
|
||||
@property # type: ignore
|
||||
@requires_update
|
||||
@@ -82,6 +69,22 @@ class SmartStrip(SmartPlug):
|
||||
Needed for methods that are decorated with `requires_update`.
|
||||
"""
|
||||
await super().update()
|
||||
|
||||
# Initialize the child devices during the first update.
|
||||
if not self.plugs:
|
||||
children = self.sys_info["children"]
|
||||
self.num_children = len(children)
|
||||
for child in children:
|
||||
self.plugs.append(
|
||||
SmartPlug(
|
||||
self.host,
|
||||
self.protocol,
|
||||
context=child["id"],
|
||||
cache_ttl=self.cache_ttl.total_seconds(),
|
||||
ioloop=self.ioloop,
|
||||
)
|
||||
)
|
||||
|
||||
for plug in self.plugs:
|
||||
await plug.update()
|
||||
|
||||
|
Reference in New Issue
Block a user