mirror of
https://github.com/python-kasa/python-kasa.git
synced 2024-12-23 19:53:34 +00:00
9cffbe9e48
Adds a new decorator that adds child options to a command and gets the child device if the options are set. - Single definition of options and error handling - Adds options automatically to command - Backwards compatible with `--index` and `--name` - `--child` allows for id and alias for ease of use - Omitting a value for `--child` gives an interactive prompt Implements private `_update` to allow the CLI to patch a child `update` method to call the parent device `update`. Example help output: ``` $ kasa brightness --help Usage: kasa brightness [OPTIONS] [BRIGHTNESS] Get or set brightness. Options: --transition INTEGER --child, --name TEXT Child ID or alias for controlling sub- devices. If no value provided will show an interactive prompt allowing you to select a child. --child-index, --index INTEGER Child index controlling sub-devices --help Show this message and exit. ``` Fixes #769
85 lines
2.9 KiB
Python
85 lines
2.9 KiB
Python
"""Child device implementation."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
from typing import Any
|
|
|
|
from ..device_type import DeviceType
|
|
from ..deviceconfig import DeviceConfig
|
|
from ..smartprotocol import SmartProtocol, _ChildProtocolWrapper
|
|
from .smartdevice import SmartDevice
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
class SmartChildDevice(SmartDevice):
|
|
"""Presentation of a child device.
|
|
|
|
This wraps the protocol communications and sets internal data for the child.
|
|
"""
|
|
|
|
def __init__(
|
|
self,
|
|
parent: SmartDevice,
|
|
info,
|
|
component_info,
|
|
config: DeviceConfig | None = None,
|
|
protocol: SmartProtocol | None = None,
|
|
) -> None:
|
|
super().__init__(parent.host, config=parent.config, protocol=parent.protocol)
|
|
self._parent = parent
|
|
self._update_internal_state(info)
|
|
self._components = component_info
|
|
self._id = info["device_id"]
|
|
self.protocol = _ChildProtocolWrapper(self._id, parent.protocol)
|
|
|
|
async def update(self, update_children: bool = True):
|
|
"""Update child module info.
|
|
|
|
The parent updates our internal info so just update modules with
|
|
their own queries.
|
|
"""
|
|
await self._update(update_children)
|
|
|
|
async def _update(self, update_children: bool = True):
|
|
"""Update child module info.
|
|
|
|
Internal implementation to allow patching of public update in the cli
|
|
or test framework.
|
|
"""
|
|
req: dict[str, Any] = {}
|
|
for module in self.modules.values():
|
|
if mod_query := module.query():
|
|
req.update(mod_query)
|
|
if req:
|
|
self._last_update = await self.protocol.query(req)
|
|
|
|
@classmethod
|
|
async def create(cls, parent: SmartDevice, child_info, child_components):
|
|
"""Create a child device based on device info and component listing."""
|
|
child: SmartChildDevice = cls(parent, child_info, child_components)
|
|
await child._initialize_modules()
|
|
return child
|
|
|
|
@property
|
|
def device_type(self) -> DeviceType:
|
|
"""Return child device type."""
|
|
child_device_map = {
|
|
"plug.powerstrip.sub-plug": DeviceType.Plug,
|
|
"subg.trigger.contact-sensor": DeviceType.Sensor,
|
|
"subg.trigger.temp-hmdt-sensor": DeviceType.Sensor,
|
|
"subg.trigger.water-leak-sensor": DeviceType.Sensor,
|
|
"kasa.switch.outlet.sub-fan": DeviceType.Fan,
|
|
"kasa.switch.outlet.sub-dimmer": DeviceType.Dimmer,
|
|
"subg.trv": DeviceType.Thermostat,
|
|
}
|
|
dev_type = child_device_map.get(self.sys_info["category"])
|
|
if dev_type is None:
|
|
_LOGGER.warning("Unknown child device type, please open issue ")
|
|
dev_type = DeviceType.Unknown
|
|
return dev_type
|
|
|
|
def __repr__(self):
|
|
return f"<{self.device_type} {self.alias} ({self.model}) of {self._parent}>"
|