mirror of
https://github.com/python-kasa/python-kasa.git
synced 2025-12-01 05:38:33 +00:00
Support child devices in all applicable cli commands (#1020)
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
This commit is contained in:
@@ -338,9 +338,15 @@ class Device(ABC):
|
||||
"""Returns the child devices."""
|
||||
return list(self._children.values())
|
||||
|
||||
def get_child_device(self, id_: str) -> Device:
|
||||
"""Return child device by its ID."""
|
||||
return self._children[id_]
|
||||
def get_child_device(self, name_or_id: str) -> Device | None:
|
||||
"""Return child device by its device_id or alias."""
|
||||
if name_or_id in self._children:
|
||||
return self._children[name_or_id]
|
||||
name_lower = name_or_id.lower()
|
||||
for child in self.children:
|
||||
if child.alias and child.alias.lower() == name_lower:
|
||||
return child
|
||||
return None
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
|
||||
Reference in New Issue
Block a user