mirror of
https://github.com/python-kasa/python-kasa.git
synced 2025-10-13 10:58:02 +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,18 +22,16 @@ class SmartPlug(SmartDevice):
|
||||
p = SmartPlug("192.168.1.105")
|
||||
|
||||
# print the devices alias
|
||||
print(p.sync.alias)
|
||||
print(p.alias)
|
||||
|
||||
# change state of plug
|
||||
p.sync.turn_on()
|
||||
p.sync.turn_off()
|
||||
await p.turn_on()
|
||||
await p.turn_off()
|
||||
|
||||
# query and print current state of plug
|
||||
print(p.sync.state_information)
|
||||
print(p.state_information)
|
||||
```
|
||||
|
||||
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.
|
||||
"""
|
||||
@@ -92,6 +90,26 @@ class SmartPlug(SmartDevice):
|
||||
else:
|
||||
raise ValueError("Brightness value %s is not valid." % value)
|
||||
|
||||
def _get_child_info(self):
|
||||
for plug in self.sys_info["children"]:
|
||||
if plug["id"] == self.context:
|
||||
return plug
|
||||
raise SmartDeviceException("Unable to find children %s")
|
||||
|
||||
@property # type: ignore
|
||||
@requires_update
|
||||
def alias(self) -> str:
|
||||
"""Return device name (alias).
|
||||
|
||||
:return: Device name aka alias.
|
||||
:rtype: str
|
||||
"""
|
||||
if self.context:
|
||||
info = self._get_child_info()
|
||||
return info["alias"]
|
||||
else:
|
||||
return super().alias
|
||||
|
||||
@property # type: ignore
|
||||
@requires_update
|
||||
def is_dimmable(self):
|
||||
@@ -122,6 +140,10 @@ class SmartPlug(SmartDevice):
|
||||
|
||||
:return: True if device is on, False otherwise
|
||||
"""
|
||||
if self.context:
|
||||
info = self._get_child_info()
|
||||
return info["state"]
|
||||
|
||||
sys_info = self.sys_info
|
||||
return bool(sys_info["relay_state"])
|
||||
|
||||
@@ -171,10 +193,8 @@ class SmartPlug(SmartDevice):
|
||||
"""
|
||||
sys_info = self.sys_info
|
||||
if self.context:
|
||||
for plug in sys_info["children"]:
|
||||
if plug["id"] == self.context:
|
||||
on_time = plug["on_time"]
|
||||
break
|
||||
info = self._get_child_info()
|
||||
on_time = info["on_time"]
|
||||
else:
|
||||
on_time = sys_info["on_time"]
|
||||
|
||||
|
Reference in New Issue
Block a user