2019-12-18 08:11:18 +00:00
|
|
|
"""python-kasa cli tool."""
|
2024-04-16 18:21:20 +00:00
|
|
|
|
2024-04-17 13:39:24 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2024-01-24 08:10:55 +00:00
|
|
|
import ast
|
2022-02-02 18:30:48 +00:00
|
|
|
import asyncio
|
2023-02-18 16:31:06 +00:00
|
|
|
import json
|
2017-03-20 18:03:19 +00:00
|
|
|
import logging
|
2023-02-18 16:31:06 +00:00
|
|
|
import re
|
2022-04-06 00:25:47 +00:00
|
|
|
import sys
|
2024-02-14 17:03:50 +00:00
|
|
|
from contextlib import asynccontextmanager
|
2023-02-18 20:41:08 +00:00
|
|
|
from functools import singledispatch, wraps
|
2020-03-17 23:40:06 +00:00
|
|
|
from pprint import pformat as pf
|
2024-04-17 13:39:24 +00:00
|
|
|
from typing import Any, cast
|
2017-03-20 18:03:19 +00:00
|
|
|
|
async++, small powerstrip improvements (#46)
* async++, small powerstrip improvements
* use asyncclick instead of click, allows defining the commands with async def to avoid manual eventloop/asyncio.run handling
* improve powerstrip support:
* new powerstrip api: turn_{on,off}_by_{name,index} methods
* cli: fix on/off for powerstrip using the new apis
* add missing update()s for cli's hsv, led, temperature (fixes #43)
* prettyprint the received payloads when debug mode in use
* cli: debug mode can be activated now with '-d'
* update requirements_test.txt
* remove outdated click-datetime, replace click with asyncclick
* debug is a flag
* make smartstripplug to inherit the sysinfo from its parent, allows for simple access of general plug properties
* proper bound checking for index accesses, allow controlling the plug at index 0
* remove the mess of turn_{on,off}_by_{name,index}, get_plug_by_{name,index} are enough.
* adapt cli to use that
* allow changing the alias per index
* use f-strings consistently everywhere in the cli
* add tests for get_plug_by_{index,name}
2020-04-21 18:46:13 +00:00
|
|
|
import asyncclick as click
|
2024-05-01 13:59:35 +00:00
|
|
|
from pydantic.v1 import ValidationError
|
2020-05-12 10:11:47 +00:00
|
|
|
|
2023-09-13 13:46:38 +00:00
|
|
|
from kasa import (
|
2024-02-21 15:52:55 +00:00
|
|
|
AuthenticationError,
|
2023-12-29 19:17:15 +00:00
|
|
|
ConnectionType,
|
2023-09-13 13:46:38 +00:00
|
|
|
Credentials,
|
2024-02-04 15:20:08 +00:00
|
|
|
Device,
|
2023-12-29 19:17:15 +00:00
|
|
|
DeviceConfig,
|
|
|
|
DeviceFamilyType,
|
2023-09-13 13:46:38 +00:00
|
|
|
Discover,
|
2023-12-29 19:17:15 +00:00
|
|
|
EncryptType,
|
2024-04-23 17:20:12 +00:00
|
|
|
Feature,
|
2024-02-21 15:52:55 +00:00
|
|
|
KasaException,
|
2024-05-14 07:38:21 +00:00
|
|
|
Module,
|
2024-02-21 15:52:55 +00:00
|
|
|
UnsupportedDeviceError,
|
2023-09-13 13:46:38 +00:00
|
|
|
)
|
2023-12-19 12:50:33 +00:00
|
|
|
from kasa.discover import DiscoveryResult
|
2024-03-01 18:32:45 +00:00
|
|
|
from kasa.iot import (
|
|
|
|
IotBulb,
|
|
|
|
IotDevice,
|
|
|
|
IotDimmer,
|
|
|
|
IotLightStrip,
|
|
|
|
IotPlug,
|
|
|
|
IotStrip,
|
|
|
|
IotWallSwitch,
|
|
|
|
)
|
2024-04-29 16:34:20 +00:00
|
|
|
from kasa.iot.modules import Usage
|
2024-04-29 17:19:44 +00:00
|
|
|
from kasa.smart import SmartDevice
|
2023-12-19 12:50:33 +00:00
|
|
|
|
2023-02-18 16:31:06 +00:00
|
|
|
try:
|
2023-09-13 13:46:38 +00:00
|
|
|
from rich import print as _do_echo
|
2023-02-18 16:31:06 +00:00
|
|
|
except ImportError:
|
2024-01-25 07:49:26 +00:00
|
|
|
# Strip out rich formatting if rich is not installed
|
|
|
|
# but only lower case tags to avoid stripping out
|
|
|
|
# raw data from the device that is printed from
|
|
|
|
# the device state.
|
|
|
|
rich_formatting = re.compile(r"\[/?[a-z]+]")
|
2023-02-18 16:31:06 +00:00
|
|
|
|
|
|
|
def _strip_rich_formatting(echo_func):
|
|
|
|
"""Strip rich formatting from messages."""
|
|
|
|
|
|
|
|
@wraps(echo_func)
|
|
|
|
def wrapper(message=None, *args, **kwargs):
|
|
|
|
if message is not None:
|
2024-01-25 07:49:26 +00:00
|
|
|
message = rich_formatting.sub("", message)
|
2023-02-18 16:31:06 +00:00
|
|
|
echo_func(message, *args, **kwargs)
|
|
|
|
|
|
|
|
return wrapper
|
|
|
|
|
2023-09-13 13:46:38 +00:00
|
|
|
_do_echo = _strip_rich_formatting(click.echo)
|
2023-02-18 16:31:06 +00:00
|
|
|
|
2023-09-13 13:46:38 +00:00
|
|
|
# echo is set to _do_echo so that it can be reset to _do_echo later after
|
|
|
|
# --json has set it to _nop_echo
|
|
|
|
echo = _do_echo
|
2019-11-11 18:39:43 +00:00
|
|
|
|
2023-12-29 19:17:15 +00:00
|
|
|
|
|
|
|
TYPE_TO_CLASS = {
|
2024-02-04 15:20:08 +00:00
|
|
|
"plug": IotPlug,
|
2024-03-01 18:32:45 +00:00
|
|
|
"switch": IotWallSwitch,
|
2024-02-04 15:20:08 +00:00
|
|
|
"bulb": IotBulb,
|
|
|
|
"dimmer": IotDimmer,
|
|
|
|
"strip": IotStrip,
|
|
|
|
"lightstrip": IotLightStrip,
|
|
|
|
"iot.plug": IotPlug,
|
2024-03-01 18:32:45 +00:00
|
|
|
"iot.switch": IotWallSwitch,
|
2024-02-04 15:20:08 +00:00
|
|
|
"iot.bulb": IotBulb,
|
|
|
|
"iot.dimmer": IotDimmer,
|
|
|
|
"iot.strip": IotStrip,
|
|
|
|
"iot.lightstrip": IotLightStrip,
|
2024-02-22 13:34:55 +00:00
|
|
|
"smart.plug": SmartDevice,
|
2024-04-29 17:19:44 +00:00
|
|
|
"smart.bulb": SmartDevice,
|
2023-12-29 19:17:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ENCRYPT_TYPES = [encrypt_type.value for encrypt_type in EncryptType]
|
|
|
|
|
|
|
|
DEVICE_FAMILY_TYPES = [
|
|
|
|
device_family_type.value for device_family_type in DeviceFamilyType
|
2023-11-21 22:48:53 +00:00
|
|
|
]
|
2021-12-13 19:17:54 +00:00
|
|
|
|
2024-01-24 08:10:55 +00:00
|
|
|
# Block list of commands which require no update
|
|
|
|
SKIP_UPDATE_COMMANDS = ["wifi", "raw-command", "command"]
|
|
|
|
|
async++, small powerstrip improvements (#46)
* async++, small powerstrip improvements
* use asyncclick instead of click, allows defining the commands with async def to avoid manual eventloop/asyncio.run handling
* improve powerstrip support:
* new powerstrip api: turn_{on,off}_by_{name,index} methods
* cli: fix on/off for powerstrip using the new apis
* add missing update()s for cli's hsv, led, temperature (fixes #43)
* prettyprint the received payloads when debug mode in use
* cli: debug mode can be activated now with '-d'
* update requirements_test.txt
* remove outdated click-datetime, replace click with asyncclick
* debug is a flag
* make smartstripplug to inherit the sysinfo from its parent, allows for simple access of general plug properties
* proper bound checking for index accesses, allow controlling the plug at index 0
* remove the mess of turn_{on,off}_by_{name,index}, get_plug_by_{name,index} are enough.
* adapt cli to use that
* allow changing the alias per index
* use f-strings consistently everywhere in the cli
* add tests for get_plug_by_{index,name}
2020-04-21 18:46:13 +00:00
|
|
|
click.anyio_backend = "asyncio"
|
2019-11-15 15:28:02 +00:00
|
|
|
|
2024-02-04 15:20:08 +00:00
|
|
|
pass_dev = click.make_pass_decorator(Device)
|
2017-03-20 18:03:19 +00:00
|
|
|
|
|
|
|
|
2024-02-20 11:21:04 +00:00
|
|
|
def CatchAllExceptions(cls):
|
|
|
|
"""Capture all exceptions and prints them nicely.
|
2023-02-18 16:32:03 +00:00
|
|
|
|
2024-02-20 11:21:04 +00:00
|
|
|
Idea from https://stackoverflow.com/a/44347763 and
|
|
|
|
https://stackoverflow.com/questions/52213375
|
2023-02-18 16:32:03 +00:00
|
|
|
"""
|
|
|
|
|
2024-02-20 11:21:04 +00:00
|
|
|
def _handle_exception(debug, exc):
|
|
|
|
if isinstance(exc, click.ClickException):
|
2024-02-15 15:25:08 +00:00
|
|
|
raise
|
2024-05-23 18:35:41 +00:00
|
|
|
# Handle exit request from click.
|
|
|
|
if isinstance(exc, click.exceptions.Exit):
|
|
|
|
sys.exit(exc.exit_code)
|
|
|
|
|
2024-02-20 11:21:04 +00:00
|
|
|
echo(f"Raised error: {exc}")
|
|
|
|
if debug:
|
|
|
|
raise
|
|
|
|
echo("Run with --debug enabled to see stacktrace")
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
class _CommandCls(cls):
|
|
|
|
_debug = False
|
|
|
|
|
|
|
|
async def make_context(self, info_name, args, parent=None, **extra):
|
|
|
|
self._debug = any(
|
|
|
|
[arg for arg in args if arg in ["--debug", "-d", "--verbose", "-v"]]
|
|
|
|
)
|
|
|
|
try:
|
|
|
|
return await super().make_context(
|
|
|
|
info_name, args, parent=parent, **extra
|
|
|
|
)
|
|
|
|
except Exception as exc:
|
|
|
|
_handle_exception(self._debug, exc)
|
|
|
|
|
|
|
|
async def invoke(self, ctx):
|
|
|
|
try:
|
|
|
|
return await super().invoke(ctx)
|
|
|
|
except Exception as exc:
|
|
|
|
_handle_exception(self._debug, exc)
|
|
|
|
|
|
|
|
return _CommandCls
|
2023-02-18 16:32:03 +00:00
|
|
|
|
|
|
|
|
2023-02-18 20:41:08 +00:00
|
|
|
def json_formatter_cb(result, **kwargs):
|
|
|
|
"""Format and output the result as JSON, if requested."""
|
|
|
|
if not kwargs.get("json"):
|
|
|
|
return
|
|
|
|
|
|
|
|
@singledispatch
|
|
|
|
def to_serializable(val):
|
|
|
|
"""Regular obj-to-string for json serialization.
|
|
|
|
|
|
|
|
The singledispatch trick is from hynek: https://hynek.me/articles/serialization/
|
|
|
|
"""
|
|
|
|
return str(val)
|
|
|
|
|
2024-02-04 15:20:08 +00:00
|
|
|
@to_serializable.register(Device)
|
|
|
|
def _device_to_serializable(val: Device):
|
2023-02-18 20:41:08 +00:00
|
|
|
"""Serialize smart device data, just using the last update raw payload."""
|
|
|
|
return val.internal_state
|
|
|
|
|
|
|
|
json_content = json.dumps(result, indent=4, default=to_serializable)
|
|
|
|
print(json_content)
|
|
|
|
|
|
|
|
|
|
|
|
@click.group(
|
|
|
|
invoke_without_command=True,
|
2024-02-20 11:21:04 +00:00
|
|
|
cls=CatchAllExceptions(click.Group),
|
2023-02-18 20:41:08 +00:00
|
|
|
result_callback=json_formatter_cb,
|
|
|
|
)
|
API and tests cleanup (#151)
* Add new cli commands: raw_command and dump_discover
- raw_command can be used to execute raw commands with given parameters
* Useful for testing new calls before implementing them properly
- dump_discover can be used to dump the device discovery information (into a file)
* The discovery is extended to request more modules and methods from devices
* smartlife.iot.dimmer get_dimmer_parameters
* smartlife.iot.common.emeter get_realtime
* smartlife.iot.smartbulb.lightingservice get_light_state
* This is used to dump more information for proper tests, and will also allow better discovery in the future
This commit contains also some documentation updates and dropping click_datetime in favor of click's built-in datetime
* Docstring fixes
* Major API cleanup
Properties shall no more change the state of the device, this work in still in progress, the main goal being making the API more user-friendly and to make implementing new features simpler.
The newly deprecated functionality will remain working and will simply warn the user about deprecation.
Previously deprecated 'features' property and 'identify' method are now finally removed.
Deprecate and replace the following property setters:
* state with turn_on() and turn_off()
* hsv with set_hsv()
* color_temp with set_color_temp()
* brightness with set_brightness()
* led with set_led()
* alias with set_alias()
* mac with set_mac()
And getters:
* state with is_on and is_off
The {BULB,PLUG}_STATE_{ON,OFF} is simplified to STATE_ON and STATE_OFF, UNKNOWN state is removed.
These are now deprecated and will be removed in the future.
* is_on and is_off can be used to check for the state
* turn_on() and turn_off() for changing the device state.
Trying to use functionality not supported by the device will cause SmartDeviceExceptions instead of failing silently and/or returning None.
This includes, e.g., trying to set a color temperature on non-supported bulb.
ValueErrors are raised instead of SmartDeviceExceptions where appropriate (e.g. when trying to set an invalid hsv or brightness).
New enum type DeviceType is added to allow detecting device types without resorting to isinstance() calling. SmartDevice class' device_type property can be used to query the type. is_plug and is_bulb helpers are added.
* Cleanup tests and improve test coverage
* Make writing tests easier by sharing code for common implementations
* Instead of storing test data inside python files, dump-discover based information is used
* This will simplify adding new tests and remove code duplication
* fixtures are based on https://github.com/plasticrake/tplink-smarthome-simulator
* run black on newfakes
* Add HS300 tests and update SmartStrip API according to earlier changes, still WIP
* run black and avoid wildcard imports
* Black on conftest
* bump minimum required version to 3.5
* Rename fixture_tests to test_fixtures for autocollect
* fix typoed type to _type, black
* run black on several files with -79 to fix hound issues
* Fix broken merge on hue
* Fix tests (hue update, pass context to smartdevice), add is_strip property, disable emeter tests for HS300 until a solution for API is found.
* Fix old tests
* Run black on changed files
* Add real HS220 discovery, thanks to @poiyo
* add is_dimmable and is_variable_color_temp to smartdevice class, simplifies interfacing with homeassistant
* add KL120(US) fixture
* Add a simple query cache
This commit adds a simple query cache to speed up the process for users
requesting lots of different properties from the device, as done by the
cli tool as well as homeassistant.
The logic for caching is very simple:
1. A timestamp for last fetch for each module+command is stored alongside the response.
2. If the issued command starts with `get_` and the TTL has not expired, the cache result is returned.
3. Otherwise the cache for the whole corresponding module gets invalidated, the device will be queried and the result will be stored in the cache.
* add deprecation to tox.ini
* make tests pass again
* remove old tests, add flake8 to tox reqs
* run black against pyhs100 module, add it to precommit hooks, fix flake8 configuration to conform to black standards (https://ljvmiranda921.github.io/notebook/2018/06/21/precommits-using-black-and-flake8/)
* fix syntax
* cleanup conftest
2019-06-16 21:05:00 +00:00
|
|
|
@click.option(
|
|
|
|
"--host",
|
2019-12-18 08:11:18 +00:00
|
|
|
envvar="KASA_HOST",
|
API and tests cleanup (#151)
* Add new cli commands: raw_command and dump_discover
- raw_command can be used to execute raw commands with given parameters
* Useful for testing new calls before implementing them properly
- dump_discover can be used to dump the device discovery information (into a file)
* The discovery is extended to request more modules and methods from devices
* smartlife.iot.dimmer get_dimmer_parameters
* smartlife.iot.common.emeter get_realtime
* smartlife.iot.smartbulb.lightingservice get_light_state
* This is used to dump more information for proper tests, and will also allow better discovery in the future
This commit contains also some documentation updates and dropping click_datetime in favor of click's built-in datetime
* Docstring fixes
* Major API cleanup
Properties shall no more change the state of the device, this work in still in progress, the main goal being making the API more user-friendly and to make implementing new features simpler.
The newly deprecated functionality will remain working and will simply warn the user about deprecation.
Previously deprecated 'features' property and 'identify' method are now finally removed.
Deprecate and replace the following property setters:
* state with turn_on() and turn_off()
* hsv with set_hsv()
* color_temp with set_color_temp()
* brightness with set_brightness()
* led with set_led()
* alias with set_alias()
* mac with set_mac()
And getters:
* state with is_on and is_off
The {BULB,PLUG}_STATE_{ON,OFF} is simplified to STATE_ON and STATE_OFF, UNKNOWN state is removed.
These are now deprecated and will be removed in the future.
* is_on and is_off can be used to check for the state
* turn_on() and turn_off() for changing the device state.
Trying to use functionality not supported by the device will cause SmartDeviceExceptions instead of failing silently and/or returning None.
This includes, e.g., trying to set a color temperature on non-supported bulb.
ValueErrors are raised instead of SmartDeviceExceptions where appropriate (e.g. when trying to set an invalid hsv or brightness).
New enum type DeviceType is added to allow detecting device types without resorting to isinstance() calling. SmartDevice class' device_type property can be used to query the type. is_plug and is_bulb helpers are added.
* Cleanup tests and improve test coverage
* Make writing tests easier by sharing code for common implementations
* Instead of storing test data inside python files, dump-discover based information is used
* This will simplify adding new tests and remove code duplication
* fixtures are based on https://github.com/plasticrake/tplink-smarthome-simulator
* run black on newfakes
* Add HS300 tests and update SmartStrip API according to earlier changes, still WIP
* run black and avoid wildcard imports
* Black on conftest
* bump minimum required version to 3.5
* Rename fixture_tests to test_fixtures for autocollect
* fix typoed type to _type, black
* run black on several files with -79 to fix hound issues
* Fix broken merge on hue
* Fix tests (hue update, pass context to smartdevice), add is_strip property, disable emeter tests for HS300 until a solution for API is found.
* Fix old tests
* Run black on changed files
* Add real HS220 discovery, thanks to @poiyo
* add is_dimmable and is_variable_color_temp to smartdevice class, simplifies interfacing with homeassistant
* add KL120(US) fixture
* Add a simple query cache
This commit adds a simple query cache to speed up the process for users
requesting lots of different properties from the device, as done by the
cli tool as well as homeassistant.
The logic for caching is very simple:
1. A timestamp for last fetch for each module+command is stored alongside the response.
2. If the issued command starts with `get_` and the TTL has not expired, the cache result is returned.
3. Otherwise the cache for the whole corresponding module gets invalidated, the device will be queried and the result will be stored in the cache.
* add deprecation to tox.ini
* make tests pass again
* remove old tests, add flake8 to tox reqs
* run black against pyhs100 module, add it to precommit hooks, fix flake8 configuration to conform to black standards (https://ljvmiranda921.github.io/notebook/2018/06/21/precommits-using-black-and-flake8/)
* fix syntax
* cleanup conftest
2019-06-16 21:05:00 +00:00
|
|
|
required=False,
|
|
|
|
help="The host name or IP address of the device to connect to.",
|
|
|
|
)
|
2023-07-09 23:55:27 +00:00
|
|
|
@click.option(
|
|
|
|
"--port",
|
|
|
|
envvar="KASA_PORT",
|
|
|
|
required=False,
|
2023-08-29 13:04:28 +00:00
|
|
|
type=int,
|
2023-07-09 23:55:27 +00:00
|
|
|
help="The port of the device to connect to.",
|
|
|
|
)
|
API and tests cleanup (#151)
* Add new cli commands: raw_command and dump_discover
- raw_command can be used to execute raw commands with given parameters
* Useful for testing new calls before implementing them properly
- dump_discover can be used to dump the device discovery information (into a file)
* The discovery is extended to request more modules and methods from devices
* smartlife.iot.dimmer get_dimmer_parameters
* smartlife.iot.common.emeter get_realtime
* smartlife.iot.smartbulb.lightingservice get_light_state
* This is used to dump more information for proper tests, and will also allow better discovery in the future
This commit contains also some documentation updates and dropping click_datetime in favor of click's built-in datetime
* Docstring fixes
* Major API cleanup
Properties shall no more change the state of the device, this work in still in progress, the main goal being making the API more user-friendly and to make implementing new features simpler.
The newly deprecated functionality will remain working and will simply warn the user about deprecation.
Previously deprecated 'features' property and 'identify' method are now finally removed.
Deprecate and replace the following property setters:
* state with turn_on() and turn_off()
* hsv with set_hsv()
* color_temp with set_color_temp()
* brightness with set_brightness()
* led with set_led()
* alias with set_alias()
* mac with set_mac()
And getters:
* state with is_on and is_off
The {BULB,PLUG}_STATE_{ON,OFF} is simplified to STATE_ON and STATE_OFF, UNKNOWN state is removed.
These are now deprecated and will be removed in the future.
* is_on and is_off can be used to check for the state
* turn_on() and turn_off() for changing the device state.
Trying to use functionality not supported by the device will cause SmartDeviceExceptions instead of failing silently and/or returning None.
This includes, e.g., trying to set a color temperature on non-supported bulb.
ValueErrors are raised instead of SmartDeviceExceptions where appropriate (e.g. when trying to set an invalid hsv or brightness).
New enum type DeviceType is added to allow detecting device types without resorting to isinstance() calling. SmartDevice class' device_type property can be used to query the type. is_plug and is_bulb helpers are added.
* Cleanup tests and improve test coverage
* Make writing tests easier by sharing code for common implementations
* Instead of storing test data inside python files, dump-discover based information is used
* This will simplify adding new tests and remove code duplication
* fixtures are based on https://github.com/plasticrake/tplink-smarthome-simulator
* run black on newfakes
* Add HS300 tests and update SmartStrip API according to earlier changes, still WIP
* run black and avoid wildcard imports
* Black on conftest
* bump minimum required version to 3.5
* Rename fixture_tests to test_fixtures for autocollect
* fix typoed type to _type, black
* run black on several files with -79 to fix hound issues
* Fix broken merge on hue
* Fix tests (hue update, pass context to smartdevice), add is_strip property, disable emeter tests for HS300 until a solution for API is found.
* Fix old tests
* Run black on changed files
* Add real HS220 discovery, thanks to @poiyo
* add is_dimmable and is_variable_color_temp to smartdevice class, simplifies interfacing with homeassistant
* add KL120(US) fixture
* Add a simple query cache
This commit adds a simple query cache to speed up the process for users
requesting lots of different properties from the device, as done by the
cli tool as well as homeassistant.
The logic for caching is very simple:
1. A timestamp for last fetch for each module+command is stored alongside the response.
2. If the issued command starts with `get_` and the TTL has not expired, the cache result is returned.
3. Otherwise the cache for the whole corresponding module gets invalidated, the device will be queried and the result will be stored in the cache.
* add deprecation to tox.ini
* make tests pass again
* remove old tests, add flake8 to tox reqs
* run black against pyhs100 module, add it to precommit hooks, fix flake8 configuration to conform to black standards (https://ljvmiranda921.github.io/notebook/2018/06/21/precommits-using-black-and-flake8/)
* fix syntax
* cleanup conftest
2019-06-16 21:05:00 +00:00
|
|
|
@click.option(
|
|
|
|
"--alias",
|
2019-12-18 08:11:18 +00:00
|
|
|
envvar="KASA_NAME",
|
API and tests cleanup (#151)
* Add new cli commands: raw_command and dump_discover
- raw_command can be used to execute raw commands with given parameters
* Useful for testing new calls before implementing them properly
- dump_discover can be used to dump the device discovery information (into a file)
* The discovery is extended to request more modules and methods from devices
* smartlife.iot.dimmer get_dimmer_parameters
* smartlife.iot.common.emeter get_realtime
* smartlife.iot.smartbulb.lightingservice get_light_state
* This is used to dump more information for proper tests, and will also allow better discovery in the future
This commit contains also some documentation updates and dropping click_datetime in favor of click's built-in datetime
* Docstring fixes
* Major API cleanup
Properties shall no more change the state of the device, this work in still in progress, the main goal being making the API more user-friendly and to make implementing new features simpler.
The newly deprecated functionality will remain working and will simply warn the user about deprecation.
Previously deprecated 'features' property and 'identify' method are now finally removed.
Deprecate and replace the following property setters:
* state with turn_on() and turn_off()
* hsv with set_hsv()
* color_temp with set_color_temp()
* brightness with set_brightness()
* led with set_led()
* alias with set_alias()
* mac with set_mac()
And getters:
* state with is_on and is_off
The {BULB,PLUG}_STATE_{ON,OFF} is simplified to STATE_ON and STATE_OFF, UNKNOWN state is removed.
These are now deprecated and will be removed in the future.
* is_on and is_off can be used to check for the state
* turn_on() and turn_off() for changing the device state.
Trying to use functionality not supported by the device will cause SmartDeviceExceptions instead of failing silently and/or returning None.
This includes, e.g., trying to set a color temperature on non-supported bulb.
ValueErrors are raised instead of SmartDeviceExceptions where appropriate (e.g. when trying to set an invalid hsv or brightness).
New enum type DeviceType is added to allow detecting device types without resorting to isinstance() calling. SmartDevice class' device_type property can be used to query the type. is_plug and is_bulb helpers are added.
* Cleanup tests and improve test coverage
* Make writing tests easier by sharing code for common implementations
* Instead of storing test data inside python files, dump-discover based information is used
* This will simplify adding new tests and remove code duplication
* fixtures are based on https://github.com/plasticrake/tplink-smarthome-simulator
* run black on newfakes
* Add HS300 tests and update SmartStrip API according to earlier changes, still WIP
* run black and avoid wildcard imports
* Black on conftest
* bump minimum required version to 3.5
* Rename fixture_tests to test_fixtures for autocollect
* fix typoed type to _type, black
* run black on several files with -79 to fix hound issues
* Fix broken merge on hue
* Fix tests (hue update, pass context to smartdevice), add is_strip property, disable emeter tests for HS300 until a solution for API is found.
* Fix old tests
* Run black on changed files
* Add real HS220 discovery, thanks to @poiyo
* add is_dimmable and is_variable_color_temp to smartdevice class, simplifies interfacing with homeassistant
* add KL120(US) fixture
* Add a simple query cache
This commit adds a simple query cache to speed up the process for users
requesting lots of different properties from the device, as done by the
cli tool as well as homeassistant.
The logic for caching is very simple:
1. A timestamp for last fetch for each module+command is stored alongside the response.
2. If the issued command starts with `get_` and the TTL has not expired, the cache result is returned.
3. Otherwise the cache for the whole corresponding module gets invalidated, the device will be queried and the result will be stored in the cache.
* add deprecation to tox.ini
* make tests pass again
* remove old tests, add flake8 to tox reqs
* run black against pyhs100 module, add it to precommit hooks, fix flake8 configuration to conform to black standards (https://ljvmiranda921.github.io/notebook/2018/06/21/precommits-using-black-and-flake8/)
* fix syntax
* cleanup conftest
2019-06-16 21:05:00 +00:00
|
|
|
required=False,
|
|
|
|
help="The device name, or alias, of the device to connect to.",
|
|
|
|
)
|
2019-07-25 04:21:24 +00:00
|
|
|
@click.option(
|
|
|
|
"--target",
|
2022-02-15 15:59:36 +00:00
|
|
|
envvar="KASA_TARGET",
|
2019-10-26 12:21:08 +00:00
|
|
|
default="255.255.255.255",
|
|
|
|
required=False,
|
2022-02-15 15:59:36 +00:00
|
|
|
show_default=True,
|
2019-07-25 04:21:24 +00:00
|
|
|
help="The broadcast address to be used for discovery.",
|
|
|
|
)
|
2023-12-29 15:04:41 +00:00
|
|
|
@click.option(
|
|
|
|
"-v",
|
|
|
|
"--verbose",
|
|
|
|
envvar="KASA_VERBOSE",
|
|
|
|
required=False,
|
|
|
|
default=False,
|
|
|
|
is_flag=True,
|
|
|
|
help="Be more verbose on output",
|
|
|
|
)
|
|
|
|
@click.option(
|
|
|
|
"-d",
|
|
|
|
"--debug",
|
|
|
|
envvar="KASA_DEBUG",
|
|
|
|
default=False,
|
|
|
|
is_flag=True,
|
|
|
|
help="Print debug output",
|
|
|
|
)
|
2021-12-13 19:17:54 +00:00
|
|
|
@click.option(
|
2022-02-15 15:59:36 +00:00
|
|
|
"--type",
|
|
|
|
envvar="KASA_TYPE",
|
|
|
|
default=None,
|
2023-12-29 19:17:15 +00:00
|
|
|
type=click.Choice(list(TYPE_TO_CLASS), case_sensitive=False),
|
2021-12-13 19:17:54 +00:00
|
|
|
)
|
2023-02-18 20:41:08 +00:00
|
|
|
@click.option(
|
2023-12-05 22:20:29 +00:00
|
|
|
"--json/--no-json",
|
|
|
|
envvar="KASA_JSON",
|
|
|
|
default=False,
|
|
|
|
is_flag=True,
|
|
|
|
help="Output raw device response as JSON.",
|
2023-02-18 20:41:08 +00:00
|
|
|
)
|
2023-12-29 19:17:15 +00:00
|
|
|
@click.option(
|
|
|
|
"--encrypt-type",
|
|
|
|
envvar="KASA_ENCRYPT_TYPE",
|
|
|
|
default=None,
|
|
|
|
type=click.Choice(ENCRYPT_TYPES, case_sensitive=False),
|
|
|
|
)
|
|
|
|
@click.option(
|
|
|
|
"--device-family",
|
|
|
|
envvar="KASA_DEVICE_FAMILY",
|
|
|
|
default=None,
|
|
|
|
type=click.Choice(DEVICE_FAMILY_TYPES, case_sensitive=False),
|
|
|
|
)
|
2024-01-03 21:46:08 +00:00
|
|
|
@click.option(
|
|
|
|
"--login-version",
|
|
|
|
envvar="KASA_LOGIN_VERSION",
|
|
|
|
default=None,
|
|
|
|
type=int,
|
|
|
|
)
|
2023-12-04 15:44:27 +00:00
|
|
|
@click.option(
|
|
|
|
"--timeout",
|
|
|
|
envvar="KASA_TIMEOUT",
|
|
|
|
default=5,
|
|
|
|
required=False,
|
2023-12-29 15:04:41 +00:00
|
|
|
show_default=True,
|
2023-12-04 15:44:27 +00:00
|
|
|
help="Timeout for device communications.",
|
|
|
|
)
|
2023-08-03 12:24:46 +00:00
|
|
|
@click.option(
|
|
|
|
"--discovery-timeout",
|
|
|
|
envvar="KASA_DISCOVERY_TIMEOUT",
|
2024-02-08 19:03:06 +00:00
|
|
|
default=5,
|
2023-08-03 12:24:46 +00:00
|
|
|
required=False,
|
2023-12-29 15:04:41 +00:00
|
|
|
show_default=True,
|
2023-08-03 12:24:46 +00:00
|
|
|
help="Timeout for discovery.",
|
|
|
|
)
|
2023-09-13 13:46:38 +00:00
|
|
|
@click.option(
|
|
|
|
"--username",
|
|
|
|
default=None,
|
|
|
|
required=False,
|
2023-12-05 22:20:29 +00:00
|
|
|
envvar="KASA_USERNAME",
|
2023-09-13 13:46:38 +00:00
|
|
|
help="Username/email address to authenticate to device.",
|
|
|
|
)
|
|
|
|
@click.option(
|
|
|
|
"--password",
|
|
|
|
default=None,
|
|
|
|
required=False,
|
2023-12-05 22:20:29 +00:00
|
|
|
envvar="KASA_PASSWORD",
|
2023-09-13 13:46:38 +00:00
|
|
|
help="Password to use to authenticate to device.",
|
|
|
|
)
|
2024-01-03 21:46:08 +00:00
|
|
|
@click.option(
|
|
|
|
"--credentials-hash",
|
|
|
|
default=None,
|
|
|
|
required=False,
|
|
|
|
envvar="KASA_CREDENTIALS_HASH",
|
|
|
|
help="Hashed credentials used to authenticate to the device.",
|
|
|
|
)
|
2022-01-14 15:32:32 +00:00
|
|
|
@click.version_option(package_name="python-kasa")
|
2017-03-20 18:03:19 +00:00
|
|
|
@click.pass_context
|
2023-08-29 13:04:28 +00:00
|
|
|
async def cli(
|
|
|
|
ctx,
|
|
|
|
host,
|
|
|
|
port,
|
|
|
|
alias,
|
|
|
|
target,
|
2023-12-29 15:04:41 +00:00
|
|
|
verbose,
|
2023-08-29 13:04:28 +00:00
|
|
|
debug,
|
|
|
|
type,
|
2023-12-29 19:17:15 +00:00
|
|
|
encrypt_type,
|
|
|
|
device_family,
|
2024-01-03 21:46:08 +00:00
|
|
|
login_version,
|
2023-08-29 13:04:28 +00:00
|
|
|
json,
|
2023-12-04 15:44:27 +00:00
|
|
|
timeout,
|
2023-08-29 13:04:28 +00:00
|
|
|
discovery_timeout,
|
2023-09-13 13:46:38 +00:00
|
|
|
username,
|
|
|
|
password,
|
2024-01-03 21:46:08 +00:00
|
|
|
credentials_hash,
|
2023-08-29 13:04:28 +00:00
|
|
|
):
|
2020-05-27 14:55:18 +00:00
|
|
|
"""A tool for controlling TP-Link smart home devices.""" # noqa
|
2022-04-06 00:25:47 +00:00
|
|
|
# no need to perform any checks if we are just displaying the help
|
2024-04-30 16:30:03 +00:00
|
|
|
if "--help" in sys.argv:
|
2022-04-06 00:25:47 +00:00
|
|
|
# Context object is required to avoid crashing on sub-groups
|
2024-04-30 16:30:03 +00:00
|
|
|
ctx.obj = object()
|
2022-04-06 00:25:47 +00:00
|
|
|
return
|
|
|
|
|
2023-02-18 20:41:08 +00:00
|
|
|
# If JSON output is requested, disable echo
|
2023-09-13 13:46:38 +00:00
|
|
|
global echo
|
2023-02-18 20:41:08 +00:00
|
|
|
if json:
|
|
|
|
|
|
|
|
def _nop_echo(*args, **kwargs):
|
|
|
|
pass
|
|
|
|
|
|
|
|
echo = _nop_echo
|
2023-09-13 13:46:38 +00:00
|
|
|
else:
|
|
|
|
# Set back to default is required if running tests with CliRunner
|
|
|
|
global _do_echo
|
|
|
|
echo = _do_echo
|
2023-02-18 20:41:08 +00:00
|
|
|
|
2024-04-17 13:39:24 +00:00
|
|
|
logging_config: dict[str, Any] = {
|
2023-02-18 16:31:06 +00:00
|
|
|
"level": logging.DEBUG if debug > 0 else logging.INFO
|
|
|
|
}
|
|
|
|
try:
|
|
|
|
from rich.logging import RichHandler
|
|
|
|
|
|
|
|
rich_config = {
|
|
|
|
"show_time": False,
|
|
|
|
}
|
|
|
|
logging_config["handlers"] = [RichHandler(**rich_config)]
|
|
|
|
logging_config["format"] = "%(message)s"
|
|
|
|
except ImportError:
|
|
|
|
pass
|
|
|
|
|
2023-10-29 22:15:42 +00:00
|
|
|
# The configuration should be converted to use dictConfig,
|
|
|
|
# but this keeps mypy happy for now
|
2023-02-18 16:31:06 +00:00
|
|
|
logging.basicConfig(**logging_config) # type: ignore
|
2017-03-20 18:03:19 +00:00
|
|
|
|
|
|
|
if ctx.invoked_subcommand == "discover":
|
|
|
|
return
|
|
|
|
|
2023-10-04 21:35:26 +00:00
|
|
|
if alias is not None and host is not None:
|
|
|
|
raise click.BadOptionUsage("alias", "Use either --alias or --host, not both.")
|
|
|
|
|
2018-08-30 20:22:23 +00:00
|
|
|
if alias is not None and host is None:
|
2023-02-18 16:31:06 +00:00
|
|
|
echo(f"Alias is given, using discovery to find host {alias}")
|
async++, small powerstrip improvements (#46)
* async++, small powerstrip improvements
* use asyncclick instead of click, allows defining the commands with async def to avoid manual eventloop/asyncio.run handling
* improve powerstrip support:
* new powerstrip api: turn_{on,off}_by_{name,index} methods
* cli: fix on/off for powerstrip using the new apis
* add missing update()s for cli's hsv, led, temperature (fixes #43)
* prettyprint the received payloads when debug mode in use
* cli: debug mode can be activated now with '-d'
* update requirements_test.txt
* remove outdated click-datetime, replace click with asyncclick
* debug is a flag
* make smartstripplug to inherit the sysinfo from its parent, allows for simple access of general plug properties
* proper bound checking for index accesses, allow controlling the plug at index 0
* remove the mess of turn_{on,off}_by_{name,index}, get_plug_by_{name,index} are enough.
* adapt cli to use that
* allow changing the alias per index
* use f-strings consistently everywhere in the cli
* add tests for get_plug_by_{index,name}
2020-04-21 18:46:13 +00:00
|
|
|
host = await find_host_from_alias(alias=alias, target=target)
|
2018-08-30 20:22:23 +00:00
|
|
|
if host:
|
2023-02-18 16:31:06 +00:00
|
|
|
echo(f"Found hostname is {host}")
|
2018-08-30 20:22:23 +00:00
|
|
|
else:
|
2023-02-18 16:31:06 +00:00
|
|
|
echo(f"No device with name {alias} found")
|
2018-08-30 20:22:23 +00:00
|
|
|
return
|
|
|
|
|
2023-09-13 13:46:38 +00:00
|
|
|
if bool(password) != bool(username):
|
2023-10-04 21:35:26 +00:00
|
|
|
raise click.BadOptionUsage(
|
|
|
|
"username", "Using authentication requires both --username and --password"
|
|
|
|
)
|
2023-09-13 13:46:38 +00:00
|
|
|
|
2024-01-03 21:46:08 +00:00
|
|
|
if username:
|
|
|
|
credentials = Credentials(username=username, password=password)
|
|
|
|
else:
|
|
|
|
credentials = None
|
2023-09-13 13:46:38 +00:00
|
|
|
|
2018-01-17 21:03:19 +00:00
|
|
|
if host is None:
|
2023-02-18 16:31:06 +00:00
|
|
|
echo("No host name given, trying discovery..")
|
2023-12-19 12:50:33 +00:00
|
|
|
return await ctx.invoke(discover)
|
2021-12-13 19:17:54 +00:00
|
|
|
|
|
|
|
if type is not None:
|
2023-12-29 19:17:15 +00:00
|
|
|
dev = TYPE_TO_CLASS[type](host)
|
2024-01-03 21:46:08 +00:00
|
|
|
elif device_family and encrypt_type:
|
2023-12-29 19:17:15 +00:00
|
|
|
ctype = ConnectionType(
|
|
|
|
DeviceFamilyType(device_family),
|
|
|
|
EncryptType(encrypt_type),
|
2024-01-03 21:46:08 +00:00
|
|
|
login_version,
|
2023-11-21 22:48:53 +00:00
|
|
|
)
|
2023-12-29 19:17:15 +00:00
|
|
|
config = DeviceConfig(
|
2024-01-03 21:46:08 +00:00
|
|
|
host=host,
|
2024-02-03 14:28:20 +00:00
|
|
|
port_override=port,
|
2024-01-03 21:46:08 +00:00
|
|
|
credentials=credentials,
|
|
|
|
credentials_hash=credentials_hash,
|
|
|
|
timeout=timeout,
|
|
|
|
connection_type=ctype,
|
2023-12-29 19:17:15 +00:00
|
|
|
)
|
2024-02-04 15:20:08 +00:00
|
|
|
dev = await Device.connect(config=config)
|
2021-12-13 19:17:54 +00:00
|
|
|
else:
|
2024-02-08 19:03:06 +00:00
|
|
|
echo(
|
|
|
|
"No --type or --device-family and --encrypt-type defined, "
|
|
|
|
+ f"discovering for {discovery_timeout} seconds.."
|
|
|
|
)
|
2023-09-13 13:46:38 +00:00
|
|
|
dev = await Discover.discover_single(
|
|
|
|
host,
|
|
|
|
port=port,
|
|
|
|
credentials=credentials,
|
2024-02-08 19:03:06 +00:00
|
|
|
timeout=timeout,
|
|
|
|
discovery_timeout=discovery_timeout,
|
2023-09-13 13:46:38 +00:00
|
|
|
)
|
2024-01-23 21:58:57 +00:00
|
|
|
|
2024-01-24 08:10:55 +00:00
|
|
|
# Skip update on specific commands, or if device factory,
|
|
|
|
# that performs an update was used for the device.
|
|
|
|
if ctx.invoked_subcommand not in SKIP_UPDATE_COMMANDS and not device_family:
|
2023-11-21 22:48:53 +00:00
|
|
|
await dev.update()
|
2021-10-09 14:36:36 +00:00
|
|
|
|
2024-02-14 17:03:50 +00:00
|
|
|
@asynccontextmanager
|
|
|
|
async def async_wrapped_device(device: Device):
|
|
|
|
try:
|
|
|
|
yield device
|
|
|
|
finally:
|
|
|
|
await device.disconnect()
|
|
|
|
|
|
|
|
ctx.obj = await ctx.with_async_resource(async_wrapped_device(dev))
|
2017-03-20 18:03:19 +00:00
|
|
|
|
|
|
|
if ctx.invoked_subcommand is None:
|
2023-03-29 23:53:38 +00:00
|
|
|
return await ctx.invoke(state)
|
2017-03-20 18:03:19 +00:00
|
|
|
|
|
|
|
|
2020-04-20 16:57:33 +00:00
|
|
|
@cli.group()
|
|
|
|
@pass_dev
|
|
|
|
def wifi(dev):
|
|
|
|
"""Commands to control wifi settings."""
|
|
|
|
|
|
|
|
|
|
|
|
@wifi.command()
|
|
|
|
@pass_dev
|
async++, small powerstrip improvements (#46)
* async++, small powerstrip improvements
* use asyncclick instead of click, allows defining the commands with async def to avoid manual eventloop/asyncio.run handling
* improve powerstrip support:
* new powerstrip api: turn_{on,off}_by_{name,index} methods
* cli: fix on/off for powerstrip using the new apis
* add missing update()s for cli's hsv, led, temperature (fixes #43)
* prettyprint the received payloads when debug mode in use
* cli: debug mode can be activated now with '-d'
* update requirements_test.txt
* remove outdated click-datetime, replace click with asyncclick
* debug is a flag
* make smartstripplug to inherit the sysinfo from its parent, allows for simple access of general plug properties
* proper bound checking for index accesses, allow controlling the plug at index 0
* remove the mess of turn_{on,off}_by_{name,index}, get_plug_by_{name,index} are enough.
* adapt cli to use that
* allow changing the alias per index
* use f-strings consistently everywhere in the cli
* add tests for get_plug_by_{index,name}
2020-04-21 18:46:13 +00:00
|
|
|
async def scan(dev):
|
2020-04-20 16:57:33 +00:00
|
|
|
"""Scan for available wifi networks."""
|
2023-02-18 16:31:06 +00:00
|
|
|
echo("Scanning for wifi networks, wait a second..")
|
async++, small powerstrip improvements (#46)
* async++, small powerstrip improvements
* use asyncclick instead of click, allows defining the commands with async def to avoid manual eventloop/asyncio.run handling
* improve powerstrip support:
* new powerstrip api: turn_{on,off}_by_{name,index} methods
* cli: fix on/off for powerstrip using the new apis
* add missing update()s for cli's hsv, led, temperature (fixes #43)
* prettyprint the received payloads when debug mode in use
* cli: debug mode can be activated now with '-d'
* update requirements_test.txt
* remove outdated click-datetime, replace click with asyncclick
* debug is a flag
* make smartstripplug to inherit the sysinfo from its parent, allows for simple access of general plug properties
* proper bound checking for index accesses, allow controlling the plug at index 0
* remove the mess of turn_{on,off}_by_{name,index}, get_plug_by_{name,index} are enough.
* adapt cli to use that
* allow changing the alias per index
* use f-strings consistently everywhere in the cli
* add tests for get_plug_by_{index,name}
2020-04-21 18:46:13 +00:00
|
|
|
devs = await dev.wifi_scan()
|
2023-02-18 16:31:06 +00:00
|
|
|
echo(f"Found {len(devs)} wifi networks!")
|
2020-04-20 16:57:33 +00:00
|
|
|
for dev in devs:
|
2023-02-18 16:31:06 +00:00
|
|
|
echo(f"\t {dev}")
|
2020-04-20 16:57:33 +00:00
|
|
|
|
2021-10-09 14:36:36 +00:00
|
|
|
return devs
|
|
|
|
|
2020-04-20 16:57:33 +00:00
|
|
|
|
|
|
|
@wifi.command()
|
|
|
|
@click.argument("ssid")
|
2024-01-03 21:45:16 +00:00
|
|
|
@click.option("--keytype", prompt=True)
|
2020-04-20 16:57:33 +00:00
|
|
|
@click.option("--password", prompt=True, hide_input=True)
|
|
|
|
@pass_dev
|
2024-02-04 15:20:08 +00:00
|
|
|
async def join(dev: Device, ssid: str, password: str, keytype: str):
|
2020-04-20 16:57:33 +00:00
|
|
|
"""Join the given wifi network."""
|
2023-02-18 16:31:06 +00:00
|
|
|
echo(f"Asking the device to connect to {ssid}..")
|
async++, small powerstrip improvements (#46)
* async++, small powerstrip improvements
* use asyncclick instead of click, allows defining the commands with async def to avoid manual eventloop/asyncio.run handling
* improve powerstrip support:
* new powerstrip api: turn_{on,off}_by_{name,index} methods
* cli: fix on/off for powerstrip using the new apis
* add missing update()s for cli's hsv, led, temperature (fixes #43)
* prettyprint the received payloads when debug mode in use
* cli: debug mode can be activated now with '-d'
* update requirements_test.txt
* remove outdated click-datetime, replace click with asyncclick
* debug is a flag
* make smartstripplug to inherit the sysinfo from its parent, allows for simple access of general plug properties
* proper bound checking for index accesses, allow controlling the plug at index 0
* remove the mess of turn_{on,off}_by_{name,index}, get_plug_by_{name,index} are enough.
* adapt cli to use that
* allow changing the alias per index
* use f-strings consistently everywhere in the cli
* add tests for get_plug_by_{index,name}
2020-04-21 18:46:13 +00:00
|
|
|
res = await dev.wifi_join(ssid, password, keytype=keytype)
|
2023-02-18 16:31:06 +00:00
|
|
|
echo(
|
2023-10-29 22:15:42 +00:00
|
|
|
f"Response: {res} - if the device is not able to join the network, "
|
|
|
|
f"it will revert back to its previous state."
|
2020-04-20 16:57:33 +00:00
|
|
|
)
|
|
|
|
|
2021-10-09 14:36:36 +00:00
|
|
|
return res
|
API and tests cleanup (#151)
* Add new cli commands: raw_command and dump_discover
- raw_command can be used to execute raw commands with given parameters
* Useful for testing new calls before implementing them properly
- dump_discover can be used to dump the device discovery information (into a file)
* The discovery is extended to request more modules and methods from devices
* smartlife.iot.dimmer get_dimmer_parameters
* smartlife.iot.common.emeter get_realtime
* smartlife.iot.smartbulb.lightingservice get_light_state
* This is used to dump more information for proper tests, and will also allow better discovery in the future
This commit contains also some documentation updates and dropping click_datetime in favor of click's built-in datetime
* Docstring fixes
* Major API cleanup
Properties shall no more change the state of the device, this work in still in progress, the main goal being making the API more user-friendly and to make implementing new features simpler.
The newly deprecated functionality will remain working and will simply warn the user about deprecation.
Previously deprecated 'features' property and 'identify' method are now finally removed.
Deprecate and replace the following property setters:
* state with turn_on() and turn_off()
* hsv with set_hsv()
* color_temp with set_color_temp()
* brightness with set_brightness()
* led with set_led()
* alias with set_alias()
* mac with set_mac()
And getters:
* state with is_on and is_off
The {BULB,PLUG}_STATE_{ON,OFF} is simplified to STATE_ON and STATE_OFF, UNKNOWN state is removed.
These are now deprecated and will be removed in the future.
* is_on and is_off can be used to check for the state
* turn_on() and turn_off() for changing the device state.
Trying to use functionality not supported by the device will cause SmartDeviceExceptions instead of failing silently and/or returning None.
This includes, e.g., trying to set a color temperature on non-supported bulb.
ValueErrors are raised instead of SmartDeviceExceptions where appropriate (e.g. when trying to set an invalid hsv or brightness).
New enum type DeviceType is added to allow detecting device types without resorting to isinstance() calling. SmartDevice class' device_type property can be used to query the type. is_plug and is_bulb helpers are added.
* Cleanup tests and improve test coverage
* Make writing tests easier by sharing code for common implementations
* Instead of storing test data inside python files, dump-discover based information is used
* This will simplify adding new tests and remove code duplication
* fixtures are based on https://github.com/plasticrake/tplink-smarthome-simulator
* run black on newfakes
* Add HS300 tests and update SmartStrip API according to earlier changes, still WIP
* run black and avoid wildcard imports
* Black on conftest
* bump minimum required version to 3.5
* Rename fixture_tests to test_fixtures for autocollect
* fix typoed type to _type, black
* run black on several files with -79 to fix hound issues
* Fix broken merge on hue
* Fix tests (hue update, pass context to smartdevice), add is_strip property, disable emeter tests for HS300 until a solution for API is found.
* Fix old tests
* Run black on changed files
* Add real HS220 discovery, thanks to @poiyo
* add is_dimmable and is_variable_color_temp to smartdevice class, simplifies interfacing with homeassistant
* add KL120(US) fixture
* Add a simple query cache
This commit adds a simple query cache to speed up the process for users
requesting lots of different properties from the device, as done by the
cli tool as well as homeassistant.
The logic for caching is very simple:
1. A timestamp for last fetch for each module+command is stored alongside the response.
2. If the issued command starts with `get_` and the TTL has not expired, the cache result is returned.
3. Otherwise the cache for the whole corresponding module gets invalidated, the device will be queried and the result will be stored in the cache.
* add deprecation to tox.ini
* make tests pass again
* remove old tests, add flake8 to tox reqs
* run black against pyhs100 module, add it to precommit hooks, fix flake8 configuration to conform to black standards (https://ljvmiranda921.github.io/notebook/2018/06/21/precommits-using-black-and-flake8/)
* fix syntax
* cleanup conftest
2019-06-16 21:05:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
@cli.command()
|
2017-08-05 13:49:56 +00:00
|
|
|
@click.pass_context
|
2023-12-29 15:04:41 +00:00
|
|
|
async def discover(ctx):
|
2017-03-20 18:03:19 +00:00
|
|
|
"""Discover devices in the network."""
|
2019-10-26 12:21:08 +00:00
|
|
|
target = ctx.parent.params["target"]
|
2023-09-13 13:46:38 +00:00
|
|
|
username = ctx.parent.params["username"]
|
|
|
|
password = ctx.parent.params["password"]
|
2023-12-29 19:17:15 +00:00
|
|
|
discovery_timeout = ctx.parent.params["discovery_timeout"]
|
|
|
|
timeout = ctx.parent.params["timeout"]
|
|
|
|
port = ctx.parent.params["port"]
|
2023-09-13 13:46:38 +00:00
|
|
|
|
2024-01-04 18:17:48 +00:00
|
|
|
credentials = Credentials(username, password) if username and password else None
|
2023-09-13 13:46:38 +00:00
|
|
|
|
2022-02-02 18:30:48 +00:00
|
|
|
sem = asyncio.Semaphore()
|
2023-03-29 23:53:38 +00:00
|
|
|
discovered = dict()
|
2023-08-29 13:04:28 +00:00
|
|
|
unsupported = []
|
2023-11-20 13:17:10 +00:00
|
|
|
auth_failed = []
|
2023-08-29 13:04:28 +00:00
|
|
|
|
2024-02-21 15:52:55 +00:00
|
|
|
async def print_unsupported(unsupported_exception: UnsupportedDeviceError):
|
2023-12-19 12:50:33 +00:00
|
|
|
unsupported.append(unsupported_exception)
|
|
|
|
async with sem:
|
|
|
|
if unsupported_exception.discovery_result:
|
|
|
|
echo("== Unsupported device ==")
|
|
|
|
_echo_discovery_info(unsupported_exception.discovery_result)
|
|
|
|
echo()
|
|
|
|
else:
|
|
|
|
echo("== Unsupported device ==")
|
|
|
|
echo(f"\t{unsupported_exception}")
|
|
|
|
echo()
|
2023-08-29 13:04:28 +00:00
|
|
|
|
2023-12-29 19:17:15 +00:00
|
|
|
echo(f"Discovering devices on {target} for {discovery_timeout} seconds")
|
2022-02-02 18:30:48 +00:00
|
|
|
|
2024-02-04 15:20:08 +00:00
|
|
|
async def print_discovered(dev: Device):
|
2023-12-19 12:50:33 +00:00
|
|
|
async with sem:
|
|
|
|
try:
|
|
|
|
await dev.update()
|
2024-02-21 15:52:55 +00:00
|
|
|
except AuthenticationError:
|
2023-12-19 12:50:33 +00:00
|
|
|
auth_failed.append(dev._discovery_info)
|
|
|
|
echo("== Authentication failed for device ==")
|
|
|
|
_echo_discovery_info(dev._discovery_info)
|
|
|
|
echo()
|
|
|
|
else:
|
2024-01-10 19:37:43 +00:00
|
|
|
ctx.parent.obj = dev
|
|
|
|
await ctx.parent.invoke(state)
|
2024-02-05 17:53:09 +00:00
|
|
|
discovered[dev.host] = dev.internal_state
|
2023-12-19 12:50:33 +00:00
|
|
|
echo()
|
2017-08-05 13:49:56 +00:00
|
|
|
|
2024-02-05 17:53:09 +00:00
|
|
|
discovered_devices = await Discover.discover(
|
2023-08-29 13:04:28 +00:00
|
|
|
target=target,
|
2023-12-29 19:17:15 +00:00
|
|
|
discovery_timeout=discovery_timeout,
|
2023-08-29 13:04:28 +00:00
|
|
|
on_discovered=print_discovered,
|
|
|
|
on_unsupported=print_unsupported,
|
2023-12-29 19:17:15 +00:00
|
|
|
port=port,
|
|
|
|
timeout=timeout,
|
2023-09-13 13:46:38 +00:00
|
|
|
credentials=credentials,
|
2022-02-02 18:30:48 +00:00
|
|
|
)
|
2017-03-20 18:03:19 +00:00
|
|
|
|
2024-02-05 17:53:09 +00:00
|
|
|
for device in discovered_devices.values():
|
|
|
|
await device.protocol.close()
|
|
|
|
|
2023-08-29 13:04:28 +00:00
|
|
|
echo(f"Found {len(discovered)} devices")
|
|
|
|
if unsupported:
|
2023-12-19 12:50:33 +00:00
|
|
|
echo(f"Found {len(unsupported)} unsupported devices")
|
2023-11-20 13:17:10 +00:00
|
|
|
if auth_failed:
|
|
|
|
echo(f"Found {len(auth_failed)} devices that failed to authenticate")
|
2023-08-29 13:04:28 +00:00
|
|
|
|
2023-03-29 23:53:38 +00:00
|
|
|
return discovered
|
|
|
|
|
2017-03-20 18:03:19 +00:00
|
|
|
|
2023-12-19 12:50:33 +00:00
|
|
|
def _echo_dictionary(discovery_info: dict):
|
|
|
|
echo("\t[bold]== Discovery information ==[/bold]")
|
|
|
|
for key, value in discovery_info.items():
|
|
|
|
key_name = " ".join(x.capitalize() or "_" for x in key.split("_"))
|
|
|
|
key_name_and_spaces = "{:<15}".format(key_name + ":")
|
|
|
|
echo(f"\t{key_name_and_spaces}{value}")
|
|
|
|
|
|
|
|
|
|
|
|
def _echo_discovery_info(discovery_info):
|
2024-02-03 14:28:51 +00:00
|
|
|
# We don't have discovery info when all connection params are passed manually
|
|
|
|
if discovery_info is None:
|
|
|
|
return
|
|
|
|
|
2023-12-19 12:50:33 +00:00
|
|
|
if "system" in discovery_info and "get_sysinfo" in discovery_info["system"]:
|
|
|
|
_echo_dictionary(discovery_info["system"]["get_sysinfo"])
|
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
|
|
|
dr = DiscoveryResult(**discovery_info)
|
|
|
|
except ValidationError:
|
|
|
|
_echo_dictionary(discovery_info)
|
|
|
|
return
|
|
|
|
|
|
|
|
echo("\t[bold]== Discovery Result ==[/bold]")
|
2024-01-23 12:24:17 +00:00
|
|
|
echo(f"\tDevice Type: {dr.device_type}")
|
|
|
|
echo(f"\tDevice Model: {dr.device_model}")
|
|
|
|
echo(f"\tIP: {dr.ip}")
|
|
|
|
echo(f"\tMAC: {dr.mac}")
|
|
|
|
echo(f"\tDevice Id (hash): {dr.device_id}")
|
|
|
|
echo(f"\tOwner (hash): {dr.owner}")
|
|
|
|
echo(f"\tHW Ver: {dr.hw_ver}")
|
|
|
|
echo(f"\tSupports IOT Cloud: {dr.is_support_iot_cloud}")
|
|
|
|
echo(f"\tOBD Src: {dr.obd_src}")
|
|
|
|
echo(f"\tFactory Default: {dr.factory_default}")
|
|
|
|
echo(f"\tEncrypt Type: {dr.mgt_encrypt_schm.encrypt_type}")
|
|
|
|
echo(f"\tSupports HTTPS: {dr.mgt_encrypt_schm.is_support_https}")
|
|
|
|
echo(f"\tHTTP Port: {dr.mgt_encrypt_schm.http_port}")
|
|
|
|
echo(f"\tLV (Login Level): {dr.mgt_encrypt_schm.lv}")
|
2023-12-19 12:50:33 +00:00
|
|
|
|
|
|
|
|
async++, small powerstrip improvements (#46)
* async++, small powerstrip improvements
* use asyncclick instead of click, allows defining the commands with async def to avoid manual eventloop/asyncio.run handling
* improve powerstrip support:
* new powerstrip api: turn_{on,off}_by_{name,index} methods
* cli: fix on/off for powerstrip using the new apis
* add missing update()s for cli's hsv, led, temperature (fixes #43)
* prettyprint the received payloads when debug mode in use
* cli: debug mode can be activated now with '-d'
* update requirements_test.txt
* remove outdated click-datetime, replace click with asyncclick
* debug is a flag
* make smartstripplug to inherit the sysinfo from its parent, allows for simple access of general plug properties
* proper bound checking for index accesses, allow controlling the plug at index 0
* remove the mess of turn_{on,off}_by_{name,index}, get_plug_by_{name,index} are enough.
* adapt cli to use that
* allow changing the alias per index
* use f-strings consistently everywhere in the cli
* add tests for get_plug_by_{index,name}
2020-04-21 18:46:13 +00:00
|
|
|
async def find_host_from_alias(alias, target="255.255.255.255", timeout=1, attempts=3):
|
API and tests cleanup (#151)
* Add new cli commands: raw_command and dump_discover
- raw_command can be used to execute raw commands with given parameters
* Useful for testing new calls before implementing them properly
- dump_discover can be used to dump the device discovery information (into a file)
* The discovery is extended to request more modules and methods from devices
* smartlife.iot.dimmer get_dimmer_parameters
* smartlife.iot.common.emeter get_realtime
* smartlife.iot.smartbulb.lightingservice get_light_state
* This is used to dump more information for proper tests, and will also allow better discovery in the future
This commit contains also some documentation updates and dropping click_datetime in favor of click's built-in datetime
* Docstring fixes
* Major API cleanup
Properties shall no more change the state of the device, this work in still in progress, the main goal being making the API more user-friendly and to make implementing new features simpler.
The newly deprecated functionality will remain working and will simply warn the user about deprecation.
Previously deprecated 'features' property and 'identify' method are now finally removed.
Deprecate and replace the following property setters:
* state with turn_on() and turn_off()
* hsv with set_hsv()
* color_temp with set_color_temp()
* brightness with set_brightness()
* led with set_led()
* alias with set_alias()
* mac with set_mac()
And getters:
* state with is_on and is_off
The {BULB,PLUG}_STATE_{ON,OFF} is simplified to STATE_ON and STATE_OFF, UNKNOWN state is removed.
These are now deprecated and will be removed in the future.
* is_on and is_off can be used to check for the state
* turn_on() and turn_off() for changing the device state.
Trying to use functionality not supported by the device will cause SmartDeviceExceptions instead of failing silently and/or returning None.
This includes, e.g., trying to set a color temperature on non-supported bulb.
ValueErrors are raised instead of SmartDeviceExceptions where appropriate (e.g. when trying to set an invalid hsv or brightness).
New enum type DeviceType is added to allow detecting device types without resorting to isinstance() calling. SmartDevice class' device_type property can be used to query the type. is_plug and is_bulb helpers are added.
* Cleanup tests and improve test coverage
* Make writing tests easier by sharing code for common implementations
* Instead of storing test data inside python files, dump-discover based information is used
* This will simplify adding new tests and remove code duplication
* fixtures are based on https://github.com/plasticrake/tplink-smarthome-simulator
* run black on newfakes
* Add HS300 tests and update SmartStrip API according to earlier changes, still WIP
* run black and avoid wildcard imports
* Black on conftest
* bump minimum required version to 3.5
* Rename fixture_tests to test_fixtures for autocollect
* fix typoed type to _type, black
* run black on several files with -79 to fix hound issues
* Fix broken merge on hue
* Fix tests (hue update, pass context to smartdevice), add is_strip property, disable emeter tests for HS300 until a solution for API is found.
* Fix old tests
* Run black on changed files
* Add real HS220 discovery, thanks to @poiyo
* add is_dimmable and is_variable_color_temp to smartdevice class, simplifies interfacing with homeassistant
* add KL120(US) fixture
* Add a simple query cache
This commit adds a simple query cache to speed up the process for users
requesting lots of different properties from the device, as done by the
cli tool as well as homeassistant.
The logic for caching is very simple:
1. A timestamp for last fetch for each module+command is stored alongside the response.
2. If the issued command starts with `get_` and the TTL has not expired, the cache result is returned.
3. Otherwise the cache for the whole corresponding module gets invalidated, the device will be queried and the result will be stored in the cache.
* add deprecation to tox.ini
* make tests pass again
* remove old tests, add flake8 to tox reqs
* run black against pyhs100 module, add it to precommit hooks, fix flake8 configuration to conform to black standards (https://ljvmiranda921.github.io/notebook/2018/06/21/precommits-using-black-and-flake8/)
* fix syntax
* cleanup conftest
2019-06-16 21:05:00 +00:00
|
|
|
"""Discover a device identified by its alias."""
|
2023-10-29 22:15:42 +00:00
|
|
|
for _attempt in range(1, attempts):
|
async++, small powerstrip improvements (#46)
* async++, small powerstrip improvements
* use asyncclick instead of click, allows defining the commands with async def to avoid manual eventloop/asyncio.run handling
* improve powerstrip support:
* new powerstrip api: turn_{on,off}_by_{name,index} methods
* cli: fix on/off for powerstrip using the new apis
* add missing update()s for cli's hsv, led, temperature (fixes #43)
* prettyprint the received payloads when debug mode in use
* cli: debug mode can be activated now with '-d'
* update requirements_test.txt
* remove outdated click-datetime, replace click with asyncclick
* debug is a flag
* make smartstripplug to inherit the sysinfo from its parent, allows for simple access of general plug properties
* proper bound checking for index accesses, allow controlling the plug at index 0
* remove the mess of turn_{on,off}_by_{name,index}, get_plug_by_{name,index} are enough.
* adapt cli to use that
* allow changing the alias per index
* use f-strings consistently everywhere in the cli
* add tests for get_plug_by_{index,name}
2020-04-21 18:46:13 +00:00
|
|
|
found_devs = await Discover.discover(target=target, timeout=timeout)
|
2023-10-29 22:15:42 +00:00
|
|
|
for _ip, dev in found_devs.items():
|
2019-11-15 16:48:36 +00:00
|
|
|
if dev.alias.lower() == alias.lower():
|
2018-08-30 20:22:23 +00:00
|
|
|
host = dev.host
|
|
|
|
return host
|
2021-10-09 14:36:36 +00:00
|
|
|
|
2018-08-30 20:22:23 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
|
2017-03-20 18:03:19 +00:00
|
|
|
@cli.command()
|
|
|
|
@pass_dev
|
async++, small powerstrip improvements (#46)
* async++, small powerstrip improvements
* use asyncclick instead of click, allows defining the commands with async def to avoid manual eventloop/asyncio.run handling
* improve powerstrip support:
* new powerstrip api: turn_{on,off}_by_{name,index} methods
* cli: fix on/off for powerstrip using the new apis
* add missing update()s for cli's hsv, led, temperature (fixes #43)
* prettyprint the received payloads when debug mode in use
* cli: debug mode can be activated now with '-d'
* update requirements_test.txt
* remove outdated click-datetime, replace click with asyncclick
* debug is a flag
* make smartstripplug to inherit the sysinfo from its parent, allows for simple access of general plug properties
* proper bound checking for index accesses, allow controlling the plug at index 0
* remove the mess of turn_{on,off}_by_{name,index}, get_plug_by_{name,index} are enough.
* adapt cli to use that
* allow changing the alias per index
* use f-strings consistently everywhere in the cli
* add tests for get_plug_by_{index,name}
2020-04-21 18:46:13 +00:00
|
|
|
async def sysinfo(dev):
|
2017-03-20 18:03:19 +00:00
|
|
|
"""Print out full system information."""
|
2023-02-18 16:31:06 +00:00
|
|
|
echo("== System info ==")
|
|
|
|
echo(pf(dev.sys_info))
|
2021-10-09 14:36:36 +00:00
|
|
|
return dev.sys_info
|
2017-03-20 18:03:19 +00:00
|
|
|
|
|
|
|
|
2024-04-23 17:20:12 +00:00
|
|
|
def _echo_features(
|
2024-04-24 16:38:52 +00:00
|
|
|
features: dict[str, Feature],
|
|
|
|
title: str,
|
|
|
|
category: Feature.Category | None = None,
|
|
|
|
verbose: bool = False,
|
2024-05-02 14:31:12 +00:00
|
|
|
indent: str = "\t",
|
2024-04-23 17:20:12 +00:00
|
|
|
):
|
|
|
|
"""Print out a listing of features and their values."""
|
|
|
|
if category is not None:
|
|
|
|
features = {
|
|
|
|
id_: feat for id_, feat in features.items() if feat.category == category
|
|
|
|
}
|
|
|
|
|
|
|
|
if not features:
|
|
|
|
return
|
|
|
|
echo(f"[bold]{title}[/bold]")
|
|
|
|
for _, feat in features.items():
|
|
|
|
try:
|
2024-05-02 14:31:12 +00:00
|
|
|
echo(f"{indent}{feat}")
|
2024-04-24 16:38:52 +00:00
|
|
|
if verbose:
|
2024-05-02 14:31:12 +00:00
|
|
|
echo(f"{indent}\tType: {feat.type}")
|
|
|
|
echo(f"{indent}\tCategory: {feat.category}")
|
|
|
|
echo(f"{indent}\tIcon: {feat.icon}")
|
2024-04-23 17:20:12 +00:00
|
|
|
except Exception as ex:
|
2024-05-02 14:31:12 +00:00
|
|
|
echo(f"{indent}{feat.name} ({feat.id}): [red]got exception ({ex})[/red]")
|
2024-04-23 17:20:12 +00:00
|
|
|
|
|
|
|
|
2024-04-24 16:38:52 +00:00
|
|
|
def _echo_all_features(features, *, verbose=False, title_prefix=None):
|
2024-04-23 17:20:12 +00:00
|
|
|
"""Print out all features by category."""
|
|
|
|
if title_prefix is not None:
|
|
|
|
echo(f"[bold]\n\t == {title_prefix} ==[/bold]")
|
|
|
|
_echo_features(
|
2024-04-24 16:38:52 +00:00
|
|
|
features,
|
|
|
|
title="\n\t== Primary features ==",
|
|
|
|
category=Feature.Category.Primary,
|
|
|
|
verbose=verbose,
|
2024-04-23 17:20:12 +00:00
|
|
|
)
|
|
|
|
_echo_features(
|
2024-04-24 16:38:52 +00:00
|
|
|
features,
|
|
|
|
title="\n\t== Information ==",
|
|
|
|
category=Feature.Category.Info,
|
|
|
|
verbose=verbose,
|
2024-04-23 17:20:12 +00:00
|
|
|
)
|
|
|
|
_echo_features(
|
2024-04-24 16:38:52 +00:00
|
|
|
features,
|
|
|
|
title="\n\t== Configuration ==",
|
|
|
|
category=Feature.Category.Config,
|
|
|
|
verbose=verbose,
|
|
|
|
)
|
|
|
|
_echo_features(
|
|
|
|
features,
|
|
|
|
title="\n\t== Debug ==",
|
|
|
|
category=Feature.Category.Debug,
|
|
|
|
verbose=verbose,
|
2024-04-23 17:20:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2017-03-20 18:03:19 +00:00
|
|
|
@cli.command()
|
|
|
|
@pass_dev
|
2024-01-10 19:37:43 +00:00
|
|
|
@click.pass_context
|
2024-02-04 15:20:08 +00:00
|
|
|
async def state(ctx, dev: Device):
|
2017-03-20 18:03:19 +00:00
|
|
|
"""Print out device state and versions."""
|
2024-01-10 19:37:43 +00:00
|
|
|
verbose = ctx.parent.params.get("verbose", False) if ctx.parent else False
|
|
|
|
|
2023-02-18 16:31:06 +00:00
|
|
|
echo(f"[bold]== {dev.alias} - {dev.model} ==[/bold]")
|
|
|
|
echo(f"\tHost: {dev.host}")
|
2023-07-09 23:55:27 +00:00
|
|
|
echo(f"\tPort: {dev.port}")
|
2023-02-18 16:31:06 +00:00
|
|
|
echo(f"\tDevice state: {dev.is_on}")
|
2024-02-22 22:09:38 +00:00
|
|
|
if dev.children:
|
2024-04-23 17:20:12 +00:00
|
|
|
echo("\t== Children ==")
|
2024-02-22 19:46:19 +00:00
|
|
|
for child in dev.children:
|
2024-04-23 17:20:12 +00:00
|
|
|
_echo_all_features(
|
|
|
|
child.features,
|
|
|
|
title_prefix=f"{child.alias} ({child.model}, {child.device_type})",
|
2024-04-24 16:38:52 +00:00
|
|
|
verbose=verbose,
|
2024-04-23 17:20:12 +00:00
|
|
|
)
|
|
|
|
|
2023-02-18 16:31:06 +00:00
|
|
|
echo()
|
2020-05-27 14:55:18 +00:00
|
|
|
|
2023-02-18 16:31:06 +00:00
|
|
|
echo("\t[bold]== Generic information ==[/bold]")
|
|
|
|
echo(f"\tTime: {dev.time} (tz: {dev.timezone}")
|
|
|
|
echo(f"\tHardware: {dev.hw_info['hw_ver']}")
|
|
|
|
echo(f"\tSoftware: {dev.hw_info['sw_ver']}")
|
|
|
|
echo(f"\tMAC (rssi): {dev.mac} ({dev.rssi})")
|
|
|
|
echo(f"\tLocation: {dev.location}")
|
API and tests cleanup (#151)
* Add new cli commands: raw_command and dump_discover
- raw_command can be used to execute raw commands with given parameters
* Useful for testing new calls before implementing them properly
- dump_discover can be used to dump the device discovery information (into a file)
* The discovery is extended to request more modules and methods from devices
* smartlife.iot.dimmer get_dimmer_parameters
* smartlife.iot.common.emeter get_realtime
* smartlife.iot.smartbulb.lightingservice get_light_state
* This is used to dump more information for proper tests, and will also allow better discovery in the future
This commit contains also some documentation updates and dropping click_datetime in favor of click's built-in datetime
* Docstring fixes
* Major API cleanup
Properties shall no more change the state of the device, this work in still in progress, the main goal being making the API more user-friendly and to make implementing new features simpler.
The newly deprecated functionality will remain working and will simply warn the user about deprecation.
Previously deprecated 'features' property and 'identify' method are now finally removed.
Deprecate and replace the following property setters:
* state with turn_on() and turn_off()
* hsv with set_hsv()
* color_temp with set_color_temp()
* brightness with set_brightness()
* led with set_led()
* alias with set_alias()
* mac with set_mac()
And getters:
* state with is_on and is_off
The {BULB,PLUG}_STATE_{ON,OFF} is simplified to STATE_ON and STATE_OFF, UNKNOWN state is removed.
These are now deprecated and will be removed in the future.
* is_on and is_off can be used to check for the state
* turn_on() and turn_off() for changing the device state.
Trying to use functionality not supported by the device will cause SmartDeviceExceptions instead of failing silently and/or returning None.
This includes, e.g., trying to set a color temperature on non-supported bulb.
ValueErrors are raised instead of SmartDeviceExceptions where appropriate (e.g. when trying to set an invalid hsv or brightness).
New enum type DeviceType is added to allow detecting device types without resorting to isinstance() calling. SmartDevice class' device_type property can be used to query the type. is_plug and is_bulb helpers are added.
* Cleanup tests and improve test coverage
* Make writing tests easier by sharing code for common implementations
* Instead of storing test data inside python files, dump-discover based information is used
* This will simplify adding new tests and remove code duplication
* fixtures are based on https://github.com/plasticrake/tplink-smarthome-simulator
* run black on newfakes
* Add HS300 tests and update SmartStrip API according to earlier changes, still WIP
* run black and avoid wildcard imports
* Black on conftest
* bump minimum required version to 3.5
* Rename fixture_tests to test_fixtures for autocollect
* fix typoed type to _type, black
* run black on several files with -79 to fix hound issues
* Fix broken merge on hue
* Fix tests (hue update, pass context to smartdevice), add is_strip property, disable emeter tests for HS300 until a solution for API is found.
* Fix old tests
* Run black on changed files
* Add real HS220 discovery, thanks to @poiyo
* add is_dimmable and is_variable_color_temp to smartdevice class, simplifies interfacing with homeassistant
* add KL120(US) fixture
* Add a simple query cache
This commit adds a simple query cache to speed up the process for users
requesting lots of different properties from the device, as done by the
cli tool as well as homeassistant.
The logic for caching is very simple:
1. A timestamp for last fetch for each module+command is stored alongside the response.
2. If the issued command starts with `get_` and the TTL has not expired, the cache result is returned.
3. Otherwise the cache for the whole corresponding module gets invalidated, the device will be queried and the result will be stored in the cache.
* add deprecation to tox.ini
* make tests pass again
* remove old tests, add flake8 to tox reqs
* run black against pyhs100 module, add it to precommit hooks, fix flake8 configuration to conform to black standards (https://ljvmiranda921.github.io/notebook/2018/06/21/precommits-using-black-and-flake8/)
* fix syntax
* cleanup conftest
2019-06-16 21:05:00 +00:00
|
|
|
|
2024-04-24 16:38:52 +00:00
|
|
|
_echo_all_features(dev.features, verbose=verbose)
|
2017-03-20 18:03:19 +00:00
|
|
|
|
2023-02-18 16:31:06 +00:00
|
|
|
echo("\n\t[bold]== Modules ==[/bold]")
|
2021-11-07 01:41:12 +00:00
|
|
|
for module in dev.modules.values():
|
2024-02-19 17:01:31 +00:00
|
|
|
echo(f"\t[green]+ {module}[/green]")
|
2021-11-07 01:41:12 +00:00
|
|
|
|
2024-01-10 19:37:43 +00:00
|
|
|
if verbose:
|
2024-04-23 17:20:12 +00:00
|
|
|
echo("\n\t[bold]== Protocol information ==[/bold]")
|
2024-01-10 19:37:43 +00:00
|
|
|
echo(f"\tCredentials hash: {dev.credentials_hash}")
|
2024-01-23 12:24:17 +00:00
|
|
|
echo()
|
|
|
|
_echo_discovery_info(dev._discovery_info)
|
2023-02-18 20:41:08 +00:00
|
|
|
return dev.internal_state
|
|
|
|
|
2017-03-20 18:03:19 +00:00
|
|
|
|
2018-08-13 18:37:43 +00:00
|
|
|
@cli.command()
|
|
|
|
@pass_dev
|
API and tests cleanup (#151)
* Add new cli commands: raw_command and dump_discover
- raw_command can be used to execute raw commands with given parameters
* Useful for testing new calls before implementing them properly
- dump_discover can be used to dump the device discovery information (into a file)
* The discovery is extended to request more modules and methods from devices
* smartlife.iot.dimmer get_dimmer_parameters
* smartlife.iot.common.emeter get_realtime
* smartlife.iot.smartbulb.lightingservice get_light_state
* This is used to dump more information for proper tests, and will also allow better discovery in the future
This commit contains also some documentation updates and dropping click_datetime in favor of click's built-in datetime
* Docstring fixes
* Major API cleanup
Properties shall no more change the state of the device, this work in still in progress, the main goal being making the API more user-friendly and to make implementing new features simpler.
The newly deprecated functionality will remain working and will simply warn the user about deprecation.
Previously deprecated 'features' property and 'identify' method are now finally removed.
Deprecate and replace the following property setters:
* state with turn_on() and turn_off()
* hsv with set_hsv()
* color_temp with set_color_temp()
* brightness with set_brightness()
* led with set_led()
* alias with set_alias()
* mac with set_mac()
And getters:
* state with is_on and is_off
The {BULB,PLUG}_STATE_{ON,OFF} is simplified to STATE_ON and STATE_OFF, UNKNOWN state is removed.
These are now deprecated and will be removed in the future.
* is_on and is_off can be used to check for the state
* turn_on() and turn_off() for changing the device state.
Trying to use functionality not supported by the device will cause SmartDeviceExceptions instead of failing silently and/or returning None.
This includes, e.g., trying to set a color temperature on non-supported bulb.
ValueErrors are raised instead of SmartDeviceExceptions where appropriate (e.g. when trying to set an invalid hsv or brightness).
New enum type DeviceType is added to allow detecting device types without resorting to isinstance() calling. SmartDevice class' device_type property can be used to query the type. is_plug and is_bulb helpers are added.
* Cleanup tests and improve test coverage
* Make writing tests easier by sharing code for common implementations
* Instead of storing test data inside python files, dump-discover based information is used
* This will simplify adding new tests and remove code duplication
* fixtures are based on https://github.com/plasticrake/tplink-smarthome-simulator
* run black on newfakes
* Add HS300 tests and update SmartStrip API according to earlier changes, still WIP
* run black and avoid wildcard imports
* Black on conftest
* bump minimum required version to 3.5
* Rename fixture_tests to test_fixtures for autocollect
* fix typoed type to _type, black
* run black on several files with -79 to fix hound issues
* Fix broken merge on hue
* Fix tests (hue update, pass context to smartdevice), add is_strip property, disable emeter tests for HS300 until a solution for API is found.
* Fix old tests
* Run black on changed files
* Add real HS220 discovery, thanks to @poiyo
* add is_dimmable and is_variable_color_temp to smartdevice class, simplifies interfacing with homeassistant
* add KL120(US) fixture
* Add a simple query cache
This commit adds a simple query cache to speed up the process for users
requesting lots of different properties from the device, as done by the
cli tool as well as homeassistant.
The logic for caching is very simple:
1. A timestamp for last fetch for each module+command is stored alongside the response.
2. If the issued command starts with `get_` and the TTL has not expired, the cache result is returned.
3. Otherwise the cache for the whole corresponding module gets invalidated, the device will be queried and the result will be stored in the cache.
* add deprecation to tox.ini
* make tests pass again
* remove old tests, add flake8 to tox reqs
* run black against pyhs100 module, add it to precommit hooks, fix flake8 configuration to conform to black standards (https://ljvmiranda921.github.io/notebook/2018/06/21/precommits-using-black-and-flake8/)
* fix syntax
* cleanup conftest
2019-06-16 21:05:00 +00:00
|
|
|
@click.argument("new_alias", required=False, default=None)
|
async++, small powerstrip improvements (#46)
* async++, small powerstrip improvements
* use asyncclick instead of click, allows defining the commands with async def to avoid manual eventloop/asyncio.run handling
* improve powerstrip support:
* new powerstrip api: turn_{on,off}_by_{name,index} methods
* cli: fix on/off for powerstrip using the new apis
* add missing update()s for cli's hsv, led, temperature (fixes #43)
* prettyprint the received payloads when debug mode in use
* cli: debug mode can be activated now with '-d'
* update requirements_test.txt
* remove outdated click-datetime, replace click with asyncclick
* debug is a flag
* make smartstripplug to inherit the sysinfo from its parent, allows for simple access of general plug properties
* proper bound checking for index accesses, allow controlling the plug at index 0
* remove the mess of turn_{on,off}_by_{name,index}, get_plug_by_{name,index} are enough.
* adapt cli to use that
* allow changing the alias per index
* use f-strings consistently everywhere in the cli
* add tests for get_plug_by_{index,name}
2020-04-21 18:46:13 +00:00
|
|
|
@click.option("--index", type=int)
|
|
|
|
async def alias(dev, new_alias, index):
|
|
|
|
"""Get or set the device (or plug) alias."""
|
|
|
|
if index is not None:
|
|
|
|
if not dev.is_strip:
|
2023-02-18 16:31:06 +00:00
|
|
|
echo("Index can only used for power strips!")
|
async++, small powerstrip improvements (#46)
* async++, small powerstrip improvements
* use asyncclick instead of click, allows defining the commands with async def to avoid manual eventloop/asyncio.run handling
* improve powerstrip support:
* new powerstrip api: turn_{on,off}_by_{name,index} methods
* cli: fix on/off for powerstrip using the new apis
* add missing update()s for cli's hsv, led, temperature (fixes #43)
* prettyprint the received payloads when debug mode in use
* cli: debug mode can be activated now with '-d'
* update requirements_test.txt
* remove outdated click-datetime, replace click with asyncclick
* debug is a flag
* make smartstripplug to inherit the sysinfo from its parent, allows for simple access of general plug properties
* proper bound checking for index accesses, allow controlling the plug at index 0
* remove the mess of turn_{on,off}_by_{name,index}, get_plug_by_{name,index} are enough.
* adapt cli to use that
* allow changing the alias per index
* use f-strings consistently everywhere in the cli
* add tests for get_plug_by_{index,name}
2020-04-21 18:46:13 +00:00
|
|
|
return
|
|
|
|
dev = dev.get_plug_by_index(index)
|
|
|
|
|
2018-08-13 18:37:43 +00:00
|
|
|
if new_alias is not None:
|
2023-02-18 16:31:06 +00:00
|
|
|
echo(f"Setting alias to {new_alias}")
|
2021-10-09 14:36:36 +00:00
|
|
|
res = await dev.set_alias(new_alias)
|
|
|
|
return res
|
2018-08-13 18:37:43 +00:00
|
|
|
|
2023-02-18 16:31:06 +00:00
|
|
|
echo(f"Alias: {dev.alias}")
|
async++, small powerstrip improvements (#46)
* async++, small powerstrip improvements
* use asyncclick instead of click, allows defining the commands with async def to avoid manual eventloop/asyncio.run handling
* improve powerstrip support:
* new powerstrip api: turn_{on,off}_by_{name,index} methods
* cli: fix on/off for powerstrip using the new apis
* add missing update()s for cli's hsv, led, temperature (fixes #43)
* prettyprint the received payloads when debug mode in use
* cli: debug mode can be activated now with '-d'
* update requirements_test.txt
* remove outdated click-datetime, replace click with asyncclick
* debug is a flag
* make smartstripplug to inherit the sysinfo from its parent, allows for simple access of general plug properties
* proper bound checking for index accesses, allow controlling the plug at index 0
* remove the mess of turn_{on,off}_by_{name,index}, get_plug_by_{name,index} are enough.
* adapt cli to use that
* allow changing the alias per index
* use f-strings consistently everywhere in the cli
* add tests for get_plug_by_{index,name}
2020-04-21 18:46:13 +00:00
|
|
|
if dev.is_strip:
|
2020-05-27 14:55:18 +00:00
|
|
|
for plug in dev.children:
|
2023-02-18 16:31:06 +00:00
|
|
|
echo(f" * {plug.alias}")
|
2018-08-13 18:37:43 +00:00
|
|
|
|
2023-02-18 20:41:08 +00:00
|
|
|
return dev.alias
|
|
|
|
|
2018-08-13 18:37:43 +00:00
|
|
|
|
2017-03-20 18:03:19 +00:00
|
|
|
@cli.command()
|
|
|
|
@pass_dev
|
2024-01-24 08:10:55 +00:00
|
|
|
@click.pass_context
|
API and tests cleanup (#151)
* Add new cli commands: raw_command and dump_discover
- raw_command can be used to execute raw commands with given parameters
* Useful for testing new calls before implementing them properly
- dump_discover can be used to dump the device discovery information (into a file)
* The discovery is extended to request more modules and methods from devices
* smartlife.iot.dimmer get_dimmer_parameters
* smartlife.iot.common.emeter get_realtime
* smartlife.iot.smartbulb.lightingservice get_light_state
* This is used to dump more information for proper tests, and will also allow better discovery in the future
This commit contains also some documentation updates and dropping click_datetime in favor of click's built-in datetime
* Docstring fixes
* Major API cleanup
Properties shall no more change the state of the device, this work in still in progress, the main goal being making the API more user-friendly and to make implementing new features simpler.
The newly deprecated functionality will remain working and will simply warn the user about deprecation.
Previously deprecated 'features' property and 'identify' method are now finally removed.
Deprecate and replace the following property setters:
* state with turn_on() and turn_off()
* hsv with set_hsv()
* color_temp with set_color_temp()
* brightness with set_brightness()
* led with set_led()
* alias with set_alias()
* mac with set_mac()
And getters:
* state with is_on and is_off
The {BULB,PLUG}_STATE_{ON,OFF} is simplified to STATE_ON and STATE_OFF, UNKNOWN state is removed.
These are now deprecated and will be removed in the future.
* is_on and is_off can be used to check for the state
* turn_on() and turn_off() for changing the device state.
Trying to use functionality not supported by the device will cause SmartDeviceExceptions instead of failing silently and/or returning None.
This includes, e.g., trying to set a color temperature on non-supported bulb.
ValueErrors are raised instead of SmartDeviceExceptions where appropriate (e.g. when trying to set an invalid hsv or brightness).
New enum type DeviceType is added to allow detecting device types without resorting to isinstance() calling. SmartDevice class' device_type property can be used to query the type. is_plug and is_bulb helpers are added.
* Cleanup tests and improve test coverage
* Make writing tests easier by sharing code for common implementations
* Instead of storing test data inside python files, dump-discover based information is used
* This will simplify adding new tests and remove code duplication
* fixtures are based on https://github.com/plasticrake/tplink-smarthome-simulator
* run black on newfakes
* Add HS300 tests and update SmartStrip API according to earlier changes, still WIP
* run black and avoid wildcard imports
* Black on conftest
* bump minimum required version to 3.5
* Rename fixture_tests to test_fixtures for autocollect
* fix typoed type to _type, black
* run black on several files with -79 to fix hound issues
* Fix broken merge on hue
* Fix tests (hue update, pass context to smartdevice), add is_strip property, disable emeter tests for HS300 until a solution for API is found.
* Fix old tests
* Run black on changed files
* Add real HS220 discovery, thanks to @poiyo
* add is_dimmable and is_variable_color_temp to smartdevice class, simplifies interfacing with homeassistant
* add KL120(US) fixture
* Add a simple query cache
This commit adds a simple query cache to speed up the process for users
requesting lots of different properties from the device, as done by the
cli tool as well as homeassistant.
The logic for caching is very simple:
1. A timestamp for last fetch for each module+command is stored alongside the response.
2. If the issued command starts with `get_` and the TTL has not expired, the cache result is returned.
3. Otherwise the cache for the whole corresponding module gets invalidated, the device will be queried and the result will be stored in the cache.
* add deprecation to tox.ini
* make tests pass again
* remove old tests, add flake8 to tox reqs
* run black against pyhs100 module, add it to precommit hooks, fix flake8 configuration to conform to black standards (https://ljvmiranda921.github.io/notebook/2018/06/21/precommits-using-black-and-flake8/)
* fix syntax
* cleanup conftest
2019-06-16 21:05:00 +00:00
|
|
|
@click.argument("module")
|
|
|
|
@click.argument("command")
|
|
|
|
@click.argument("parameters", default=None, required=False)
|
2024-02-04 15:20:08 +00:00
|
|
|
async def raw_command(ctx, dev: Device, module, command, parameters):
|
API and tests cleanup (#151)
* Add new cli commands: raw_command and dump_discover
- raw_command can be used to execute raw commands with given parameters
* Useful for testing new calls before implementing them properly
- dump_discover can be used to dump the device discovery information (into a file)
* The discovery is extended to request more modules and methods from devices
* smartlife.iot.dimmer get_dimmer_parameters
* smartlife.iot.common.emeter get_realtime
* smartlife.iot.smartbulb.lightingservice get_light_state
* This is used to dump more information for proper tests, and will also allow better discovery in the future
This commit contains also some documentation updates and dropping click_datetime in favor of click's built-in datetime
* Docstring fixes
* Major API cleanup
Properties shall no more change the state of the device, this work in still in progress, the main goal being making the API more user-friendly and to make implementing new features simpler.
The newly deprecated functionality will remain working and will simply warn the user about deprecation.
Previously deprecated 'features' property and 'identify' method are now finally removed.
Deprecate and replace the following property setters:
* state with turn_on() and turn_off()
* hsv with set_hsv()
* color_temp with set_color_temp()
* brightness with set_brightness()
* led with set_led()
* alias with set_alias()
* mac with set_mac()
And getters:
* state with is_on and is_off
The {BULB,PLUG}_STATE_{ON,OFF} is simplified to STATE_ON and STATE_OFF, UNKNOWN state is removed.
These are now deprecated and will be removed in the future.
* is_on and is_off can be used to check for the state
* turn_on() and turn_off() for changing the device state.
Trying to use functionality not supported by the device will cause SmartDeviceExceptions instead of failing silently and/or returning None.
This includes, e.g., trying to set a color temperature on non-supported bulb.
ValueErrors are raised instead of SmartDeviceExceptions where appropriate (e.g. when trying to set an invalid hsv or brightness).
New enum type DeviceType is added to allow detecting device types without resorting to isinstance() calling. SmartDevice class' device_type property can be used to query the type. is_plug and is_bulb helpers are added.
* Cleanup tests and improve test coverage
* Make writing tests easier by sharing code for common implementations
* Instead of storing test data inside python files, dump-discover based information is used
* This will simplify adding new tests and remove code duplication
* fixtures are based on https://github.com/plasticrake/tplink-smarthome-simulator
* run black on newfakes
* Add HS300 tests and update SmartStrip API according to earlier changes, still WIP
* run black and avoid wildcard imports
* Black on conftest
* bump minimum required version to 3.5
* Rename fixture_tests to test_fixtures for autocollect
* fix typoed type to _type, black
* run black on several files with -79 to fix hound issues
* Fix broken merge on hue
* Fix tests (hue update, pass context to smartdevice), add is_strip property, disable emeter tests for HS300 until a solution for API is found.
* Fix old tests
* Run black on changed files
* Add real HS220 discovery, thanks to @poiyo
* add is_dimmable and is_variable_color_temp to smartdevice class, simplifies interfacing with homeassistant
* add KL120(US) fixture
* Add a simple query cache
This commit adds a simple query cache to speed up the process for users
requesting lots of different properties from the device, as done by the
cli tool as well as homeassistant.
The logic for caching is very simple:
1. A timestamp for last fetch for each module+command is stored alongside the response.
2. If the issued command starts with `get_` and the TTL has not expired, the cache result is returned.
3. Otherwise the cache for the whole corresponding module gets invalidated, the device will be queried and the result will be stored in the cache.
* add deprecation to tox.ini
* make tests pass again
* remove old tests, add flake8 to tox reqs
* run black against pyhs100 module, add it to precommit hooks, fix flake8 configuration to conform to black standards (https://ljvmiranda921.github.io/notebook/2018/06/21/precommits-using-black-and-flake8/)
* fix syntax
* cleanup conftest
2019-06-16 21:05:00 +00:00
|
|
|
"""Run a raw command on the device."""
|
2024-01-24 08:10:55 +00:00
|
|
|
logging.warning("Deprecated, use 'kasa command --module %s %s'", module, command)
|
|
|
|
return await ctx.forward(cmd_command)
|
API and tests cleanup (#151)
* Add new cli commands: raw_command and dump_discover
- raw_command can be used to execute raw commands with given parameters
* Useful for testing new calls before implementing them properly
- dump_discover can be used to dump the device discovery information (into a file)
* The discovery is extended to request more modules and methods from devices
* smartlife.iot.dimmer get_dimmer_parameters
* smartlife.iot.common.emeter get_realtime
* smartlife.iot.smartbulb.lightingservice get_light_state
* This is used to dump more information for proper tests, and will also allow better discovery in the future
This commit contains also some documentation updates and dropping click_datetime in favor of click's built-in datetime
* Docstring fixes
* Major API cleanup
Properties shall no more change the state of the device, this work in still in progress, the main goal being making the API more user-friendly and to make implementing new features simpler.
The newly deprecated functionality will remain working and will simply warn the user about deprecation.
Previously deprecated 'features' property and 'identify' method are now finally removed.
Deprecate and replace the following property setters:
* state with turn_on() and turn_off()
* hsv with set_hsv()
* color_temp with set_color_temp()
* brightness with set_brightness()
* led with set_led()
* alias with set_alias()
* mac with set_mac()
And getters:
* state with is_on and is_off
The {BULB,PLUG}_STATE_{ON,OFF} is simplified to STATE_ON and STATE_OFF, UNKNOWN state is removed.
These are now deprecated and will be removed in the future.
* is_on and is_off can be used to check for the state
* turn_on() and turn_off() for changing the device state.
Trying to use functionality not supported by the device will cause SmartDeviceExceptions instead of failing silently and/or returning None.
This includes, e.g., trying to set a color temperature on non-supported bulb.
ValueErrors are raised instead of SmartDeviceExceptions where appropriate (e.g. when trying to set an invalid hsv or brightness).
New enum type DeviceType is added to allow detecting device types without resorting to isinstance() calling. SmartDevice class' device_type property can be used to query the type. is_plug and is_bulb helpers are added.
* Cleanup tests and improve test coverage
* Make writing tests easier by sharing code for common implementations
* Instead of storing test data inside python files, dump-discover based information is used
* This will simplify adding new tests and remove code duplication
* fixtures are based on https://github.com/plasticrake/tplink-smarthome-simulator
* run black on newfakes
* Add HS300 tests and update SmartStrip API according to earlier changes, still WIP
* run black and avoid wildcard imports
* Black on conftest
* bump minimum required version to 3.5
* Rename fixture_tests to test_fixtures for autocollect
* fix typoed type to _type, black
* run black on several files with -79 to fix hound issues
* Fix broken merge on hue
* Fix tests (hue update, pass context to smartdevice), add is_strip property, disable emeter tests for HS300 until a solution for API is found.
* Fix old tests
* Run black on changed files
* Add real HS220 discovery, thanks to @poiyo
* add is_dimmable and is_variable_color_temp to smartdevice class, simplifies interfacing with homeassistant
* add KL120(US) fixture
* Add a simple query cache
This commit adds a simple query cache to speed up the process for users
requesting lots of different properties from the device, as done by the
cli tool as well as homeassistant.
The logic for caching is very simple:
1. A timestamp for last fetch for each module+command is stored alongside the response.
2. If the issued command starts with `get_` and the TTL has not expired, the cache result is returned.
3. Otherwise the cache for the whole corresponding module gets invalidated, the device will be queried and the result will be stored in the cache.
* add deprecation to tox.ini
* make tests pass again
* remove old tests, add flake8 to tox reqs
* run black against pyhs100 module, add it to precommit hooks, fix flake8 configuration to conform to black standards (https://ljvmiranda921.github.io/notebook/2018/06/21/precommits-using-black-and-flake8/)
* fix syntax
* cleanup conftest
2019-06-16 21:05:00 +00:00
|
|
|
|
2024-01-24 08:10:55 +00:00
|
|
|
|
|
|
|
@cli.command(name="command")
|
|
|
|
@pass_dev
|
|
|
|
@click.option("--module", required=False, help="Module for IOT protocol.")
|
2024-02-22 19:46:19 +00:00
|
|
|
@click.option("--child", required=False, help="Child ID for controlling sub-devices")
|
2024-01-24 08:10:55 +00:00
|
|
|
@click.argument("command")
|
|
|
|
@click.argument("parameters", default=None, required=False)
|
2024-02-22 19:46:19 +00:00
|
|
|
async def cmd_command(dev: Device, module, child, command, parameters):
|
2024-01-24 08:10:55 +00:00
|
|
|
"""Run a raw command on the device."""
|
API and tests cleanup (#151)
* Add new cli commands: raw_command and dump_discover
- raw_command can be used to execute raw commands with given parameters
* Useful for testing new calls before implementing them properly
- dump_discover can be used to dump the device discovery information (into a file)
* The discovery is extended to request more modules and methods from devices
* smartlife.iot.dimmer get_dimmer_parameters
* smartlife.iot.common.emeter get_realtime
* smartlife.iot.smartbulb.lightingservice get_light_state
* This is used to dump more information for proper tests, and will also allow better discovery in the future
This commit contains also some documentation updates and dropping click_datetime in favor of click's built-in datetime
* Docstring fixes
* Major API cleanup
Properties shall no more change the state of the device, this work in still in progress, the main goal being making the API more user-friendly and to make implementing new features simpler.
The newly deprecated functionality will remain working and will simply warn the user about deprecation.
Previously deprecated 'features' property and 'identify' method are now finally removed.
Deprecate and replace the following property setters:
* state with turn_on() and turn_off()
* hsv with set_hsv()
* color_temp with set_color_temp()
* brightness with set_brightness()
* led with set_led()
* alias with set_alias()
* mac with set_mac()
And getters:
* state with is_on and is_off
The {BULB,PLUG}_STATE_{ON,OFF} is simplified to STATE_ON and STATE_OFF, UNKNOWN state is removed.
These are now deprecated and will be removed in the future.
* is_on and is_off can be used to check for the state
* turn_on() and turn_off() for changing the device state.
Trying to use functionality not supported by the device will cause SmartDeviceExceptions instead of failing silently and/or returning None.
This includes, e.g., trying to set a color temperature on non-supported bulb.
ValueErrors are raised instead of SmartDeviceExceptions where appropriate (e.g. when trying to set an invalid hsv or brightness).
New enum type DeviceType is added to allow detecting device types without resorting to isinstance() calling. SmartDevice class' device_type property can be used to query the type. is_plug and is_bulb helpers are added.
* Cleanup tests and improve test coverage
* Make writing tests easier by sharing code for common implementations
* Instead of storing test data inside python files, dump-discover based information is used
* This will simplify adding new tests and remove code duplication
* fixtures are based on https://github.com/plasticrake/tplink-smarthome-simulator
* run black on newfakes
* Add HS300 tests and update SmartStrip API according to earlier changes, still WIP
* run black and avoid wildcard imports
* Black on conftest
* bump minimum required version to 3.5
* Rename fixture_tests to test_fixtures for autocollect
* fix typoed type to _type, black
* run black on several files with -79 to fix hound issues
* Fix broken merge on hue
* Fix tests (hue update, pass context to smartdevice), add is_strip property, disable emeter tests for HS300 until a solution for API is found.
* Fix old tests
* Run black on changed files
* Add real HS220 discovery, thanks to @poiyo
* add is_dimmable and is_variable_color_temp to smartdevice class, simplifies interfacing with homeassistant
* add KL120(US) fixture
* Add a simple query cache
This commit adds a simple query cache to speed up the process for users
requesting lots of different properties from the device, as done by the
cli tool as well as homeassistant.
The logic for caching is very simple:
1. A timestamp for last fetch for each module+command is stored alongside the response.
2. If the issued command starts with `get_` and the TTL has not expired, the cache result is returned.
3. Otherwise the cache for the whole corresponding module gets invalidated, the device will be queried and the result will be stored in the cache.
* add deprecation to tox.ini
* make tests pass again
* remove old tests, add flake8 to tox reqs
* run black against pyhs100 module, add it to precommit hooks, fix flake8 configuration to conform to black standards (https://ljvmiranda921.github.io/notebook/2018/06/21/precommits-using-black-and-flake8/)
* fix syntax
* cleanup conftest
2019-06-16 21:05:00 +00:00
|
|
|
if parameters is not None:
|
|
|
|
parameters = ast.literal_eval(parameters)
|
2021-10-09 14:36:36 +00:00
|
|
|
|
2024-02-22 19:46:19 +00:00
|
|
|
if child:
|
|
|
|
# The way child devices are accessed requires a ChildDevice to
|
|
|
|
# wrap the communications. Doing this properly would require creating
|
|
|
|
# a common interfaces for both IOT and SMART child devices.
|
|
|
|
# As a stop-gap solution, we perform an update instead.
|
|
|
|
await dev.update()
|
|
|
|
dev = dev.get_child_device(child)
|
|
|
|
|
2024-02-04 15:20:08 +00:00
|
|
|
if isinstance(dev, IotDevice):
|
|
|
|
res = await dev._query_helper(module, command, parameters)
|
|
|
|
elif isinstance(dev, SmartDevice):
|
|
|
|
res = await dev._query_helper(command, parameters)
|
|
|
|
else:
|
2024-02-21 15:52:55 +00:00
|
|
|
raise KasaException("Unexpected device type %s.", dev)
|
2023-02-18 16:31:06 +00:00
|
|
|
echo(json.dumps(res))
|
2021-10-09 14:36:36 +00:00
|
|
|
return res
|
API and tests cleanup (#151)
* Add new cli commands: raw_command and dump_discover
- raw_command can be used to execute raw commands with given parameters
* Useful for testing new calls before implementing them properly
- dump_discover can be used to dump the device discovery information (into a file)
* The discovery is extended to request more modules and methods from devices
* smartlife.iot.dimmer get_dimmer_parameters
* smartlife.iot.common.emeter get_realtime
* smartlife.iot.smartbulb.lightingservice get_light_state
* This is used to dump more information for proper tests, and will also allow better discovery in the future
This commit contains also some documentation updates and dropping click_datetime in favor of click's built-in datetime
* Docstring fixes
* Major API cleanup
Properties shall no more change the state of the device, this work in still in progress, the main goal being making the API more user-friendly and to make implementing new features simpler.
The newly deprecated functionality will remain working and will simply warn the user about deprecation.
Previously deprecated 'features' property and 'identify' method are now finally removed.
Deprecate and replace the following property setters:
* state with turn_on() and turn_off()
* hsv with set_hsv()
* color_temp with set_color_temp()
* brightness with set_brightness()
* led with set_led()
* alias with set_alias()
* mac with set_mac()
And getters:
* state with is_on and is_off
The {BULB,PLUG}_STATE_{ON,OFF} is simplified to STATE_ON and STATE_OFF, UNKNOWN state is removed.
These are now deprecated and will be removed in the future.
* is_on and is_off can be used to check for the state
* turn_on() and turn_off() for changing the device state.
Trying to use functionality not supported by the device will cause SmartDeviceExceptions instead of failing silently and/or returning None.
This includes, e.g., trying to set a color temperature on non-supported bulb.
ValueErrors are raised instead of SmartDeviceExceptions where appropriate (e.g. when trying to set an invalid hsv or brightness).
New enum type DeviceType is added to allow detecting device types without resorting to isinstance() calling. SmartDevice class' device_type property can be used to query the type. is_plug and is_bulb helpers are added.
* Cleanup tests and improve test coverage
* Make writing tests easier by sharing code for common implementations
* Instead of storing test data inside python files, dump-discover based information is used
* This will simplify adding new tests and remove code duplication
* fixtures are based on https://github.com/plasticrake/tplink-smarthome-simulator
* run black on newfakes
* Add HS300 tests and update SmartStrip API according to earlier changes, still WIP
* run black and avoid wildcard imports
* Black on conftest
* bump minimum required version to 3.5
* Rename fixture_tests to test_fixtures for autocollect
* fix typoed type to _type, black
* run black on several files with -79 to fix hound issues
* Fix broken merge on hue
* Fix tests (hue update, pass context to smartdevice), add is_strip property, disable emeter tests for HS300 until a solution for API is found.
* Fix old tests
* Run black on changed files
* Add real HS220 discovery, thanks to @poiyo
* add is_dimmable and is_variable_color_temp to smartdevice class, simplifies interfacing with homeassistant
* add KL120(US) fixture
* Add a simple query cache
This commit adds a simple query cache to speed up the process for users
requesting lots of different properties from the device, as done by the
cli tool as well as homeassistant.
The logic for caching is very simple:
1. A timestamp for last fetch for each module+command is stored alongside the response.
2. If the issued command starts with `get_` and the TTL has not expired, the cache result is returned.
3. Otherwise the cache for the whole corresponding module gets invalidated, the device will be queried and the result will be stored in the cache.
* add deprecation to tox.ini
* make tests pass again
* remove old tests, add flake8 to tox reqs
* run black against pyhs100 module, add it to precommit hooks, fix flake8 configuration to conform to black standards (https://ljvmiranda921.github.io/notebook/2018/06/21/precommits-using-black-and-flake8/)
* fix syntax
* cleanup conftest
2019-06-16 21:05:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
@cli.command()
|
|
|
|
@pass_dev
|
2024-01-05 01:25:24 +00:00
|
|
|
@click.option("--index", type=int, required=False)
|
|
|
|
@click.option("--name", type=str, required=False)
|
API and tests cleanup (#151)
* Add new cli commands: raw_command and dump_discover
- raw_command can be used to execute raw commands with given parameters
* Useful for testing new calls before implementing them properly
- dump_discover can be used to dump the device discovery information (into a file)
* The discovery is extended to request more modules and methods from devices
* smartlife.iot.dimmer get_dimmer_parameters
* smartlife.iot.common.emeter get_realtime
* smartlife.iot.smartbulb.lightingservice get_light_state
* This is used to dump more information for proper tests, and will also allow better discovery in the future
This commit contains also some documentation updates and dropping click_datetime in favor of click's built-in datetime
* Docstring fixes
* Major API cleanup
Properties shall no more change the state of the device, this work in still in progress, the main goal being making the API more user-friendly and to make implementing new features simpler.
The newly deprecated functionality will remain working and will simply warn the user about deprecation.
Previously deprecated 'features' property and 'identify' method are now finally removed.
Deprecate and replace the following property setters:
* state with turn_on() and turn_off()
* hsv with set_hsv()
* color_temp with set_color_temp()
* brightness with set_brightness()
* led with set_led()
* alias with set_alias()
* mac with set_mac()
And getters:
* state with is_on and is_off
The {BULB,PLUG}_STATE_{ON,OFF} is simplified to STATE_ON and STATE_OFF, UNKNOWN state is removed.
These are now deprecated and will be removed in the future.
* is_on and is_off can be used to check for the state
* turn_on() and turn_off() for changing the device state.
Trying to use functionality not supported by the device will cause SmartDeviceExceptions instead of failing silently and/or returning None.
This includes, e.g., trying to set a color temperature on non-supported bulb.
ValueErrors are raised instead of SmartDeviceExceptions where appropriate (e.g. when trying to set an invalid hsv or brightness).
New enum type DeviceType is added to allow detecting device types without resorting to isinstance() calling. SmartDevice class' device_type property can be used to query the type. is_plug and is_bulb helpers are added.
* Cleanup tests and improve test coverage
* Make writing tests easier by sharing code for common implementations
* Instead of storing test data inside python files, dump-discover based information is used
* This will simplify adding new tests and remove code duplication
* fixtures are based on https://github.com/plasticrake/tplink-smarthome-simulator
* run black on newfakes
* Add HS300 tests and update SmartStrip API according to earlier changes, still WIP
* run black and avoid wildcard imports
* Black on conftest
* bump minimum required version to 3.5
* Rename fixture_tests to test_fixtures for autocollect
* fix typoed type to _type, black
* run black on several files with -79 to fix hound issues
* Fix broken merge on hue
* Fix tests (hue update, pass context to smartdevice), add is_strip property, disable emeter tests for HS300 until a solution for API is found.
* Fix old tests
* Run black on changed files
* Add real HS220 discovery, thanks to @poiyo
* add is_dimmable and is_variable_color_temp to smartdevice class, simplifies interfacing with homeassistant
* add KL120(US) fixture
* Add a simple query cache
This commit adds a simple query cache to speed up the process for users
requesting lots of different properties from the device, as done by the
cli tool as well as homeassistant.
The logic for caching is very simple:
1. A timestamp for last fetch for each module+command is stored alongside the response.
2. If the issued command starts with `get_` and the TTL has not expired, the cache result is returned.
3. Otherwise the cache for the whole corresponding module gets invalidated, the device will be queried and the result will be stored in the cache.
* add deprecation to tox.ini
* make tests pass again
* remove old tests, add flake8 to tox reqs
* run black against pyhs100 module, add it to precommit hooks, fix flake8 configuration to conform to black standards (https://ljvmiranda921.github.io/notebook/2018/06/21/precommits-using-black-and-flake8/)
* fix syntax
* cleanup conftest
2019-06-16 21:05:00 +00:00
|
|
|
@click.option("--year", type=click.DateTime(["%Y"]), default=None, required=False)
|
|
|
|
@click.option("--month", type=click.DateTime(["%Y-%m"]), default=None, required=False)
|
|
|
|
@click.option("--erase", is_flag=True)
|
2024-02-04 15:20:08 +00:00
|
|
|
async def emeter(dev: Device, index: int, name: str, year, month, erase):
|
2020-10-03 18:32:38 +00:00
|
|
|
"""Query emeter for historical consumption.
|
|
|
|
|
|
|
|
Daily and monthly data provided in CSV format.
|
|
|
|
"""
|
2024-01-05 01:25:24 +00:00
|
|
|
if index is not None or name is not None:
|
|
|
|
if not dev.is_strip:
|
|
|
|
echo("Index and name are only for power strips!")
|
|
|
|
return
|
|
|
|
|
|
|
|
if index is not None:
|
|
|
|
dev = dev.get_plug_by_index(index)
|
|
|
|
elif name:
|
|
|
|
dev = dev.get_plug_by_name(name)
|
|
|
|
|
2023-02-18 16:31:06 +00:00
|
|
|
echo("[bold]== Emeter ==[/bold]")
|
2019-11-15 16:48:36 +00:00
|
|
|
if not dev.has_emeter:
|
2023-02-18 16:31:06 +00:00
|
|
|
echo("Device has no emeter")
|
2017-03-20 18:03:19 +00:00
|
|
|
return
|
|
|
|
|
2024-02-04 15:20:08 +00:00
|
|
|
if (year or month or erase) and not isinstance(dev, IotDevice):
|
|
|
|
echo("Device has no historical statistics")
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
dev = cast(IotDevice, dev)
|
|
|
|
|
2017-03-20 18:03:19 +00:00
|
|
|
if erase:
|
2023-02-18 16:31:06 +00:00
|
|
|
echo("Erasing emeter statistics..")
|
2023-02-18 20:41:08 +00:00
|
|
|
return await dev.erase_emeter_stats()
|
2017-03-20 18:03:19 +00:00
|
|
|
|
|
|
|
if year:
|
2023-02-18 16:31:06 +00:00
|
|
|
echo(f"== For year {year.year} ==")
|
|
|
|
echo("Month, usage (kWh)")
|
2022-10-03 18:28:05 +00:00
|
|
|
usage_data = await dev.get_emeter_monthly(year=year.year)
|
2017-03-20 18:03:19 +00:00
|
|
|
elif month:
|
2023-02-18 16:31:06 +00:00
|
|
|
echo(f"== For month {month.month} of {month.year} ==")
|
|
|
|
echo("Day, usage (kWh)")
|
2020-10-03 18:32:38 +00:00
|
|
|
usage_data = await dev.get_emeter_daily(year=month.year, month=month.month)
|
2019-01-08 19:13:25 +00:00
|
|
|
else:
|
2020-10-03 18:32:38 +00:00
|
|
|
# Call with no argument outputs summary data and returns
|
2024-01-05 01:25:24 +00:00
|
|
|
if index is not None or name is not None:
|
|
|
|
emeter_status = await dev.get_emeter_realtime()
|
|
|
|
else:
|
|
|
|
emeter_status = dev.emeter_realtime
|
2019-01-08 19:13:25 +00:00
|
|
|
|
2023-02-18 16:31:06 +00:00
|
|
|
echo("Current: %s A" % emeter_status["current"])
|
|
|
|
echo("Voltage: %s V" % emeter_status["voltage"])
|
|
|
|
echo("Power: %s W" % emeter_status["power"])
|
|
|
|
echo("Total consumption: %s kWh" % emeter_status["total"])
|
2020-05-24 15:57:54 +00:00
|
|
|
|
2023-02-18 16:31:06 +00:00
|
|
|
echo("Today: %s kWh" % dev.emeter_today)
|
|
|
|
echo("This month: %s kWh" % dev.emeter_this_month)
|
2017-04-24 17:28:22 +00:00
|
|
|
|
2023-02-18 20:41:08 +00:00
|
|
|
return emeter_status
|
2020-10-03 18:32:38 +00:00
|
|
|
|
|
|
|
# output any detailed usage data
|
|
|
|
for index, usage in usage_data.items():
|
2023-02-18 16:31:06 +00:00
|
|
|
echo(f"{index}, {usage}")
|
2020-10-03 18:32:38 +00:00
|
|
|
|
2023-02-18 20:41:08 +00:00
|
|
|
return usage_data
|
|
|
|
|
2017-04-24 17:28:22 +00:00
|
|
|
|
2021-11-19 15:41:49 +00:00
|
|
|
@cli.command()
|
|
|
|
@pass_dev
|
|
|
|
@click.option("--year", type=click.DateTime(["%Y"]), default=None, required=False)
|
|
|
|
@click.option("--month", type=click.DateTime(["%Y-%m"]), default=None, required=False)
|
|
|
|
@click.option("--erase", is_flag=True)
|
2024-02-04 15:20:08 +00:00
|
|
|
async def usage(dev: Device, year, month, erase):
|
2021-11-19 15:41:49 +00:00
|
|
|
"""Query usage for historical consumption.
|
|
|
|
|
|
|
|
Daily and monthly data provided in CSV format.
|
|
|
|
"""
|
2023-02-18 16:31:06 +00:00
|
|
|
echo("[bold]== Usage ==[/bold]")
|
2024-04-29 16:34:20 +00:00
|
|
|
usage = cast(Usage, dev.modules["usage"])
|
2021-11-19 15:41:49 +00:00
|
|
|
|
|
|
|
if erase:
|
2023-02-18 16:31:06 +00:00
|
|
|
echo("Erasing usage statistics..")
|
2023-02-18 20:41:08 +00:00
|
|
|
return await usage.erase_stats()
|
2021-11-19 15:41:49 +00:00
|
|
|
|
|
|
|
if year:
|
2023-02-18 16:31:06 +00:00
|
|
|
echo(f"== For year {year.year} ==")
|
|
|
|
echo("Month, usage (minutes)")
|
|
|
|
usage_data = await usage.get_monthstat(year=year.year)
|
2021-11-19 15:41:49 +00:00
|
|
|
elif month:
|
2023-02-18 16:31:06 +00:00
|
|
|
echo(f"== For month {month.month} of {month.year} ==")
|
|
|
|
echo("Day, usage (minutes)")
|
2021-11-19 15:41:49 +00:00
|
|
|
usage_data = await usage.get_daystat(year=month.year, month=month.month)
|
|
|
|
else:
|
|
|
|
# Call with no argument outputs summary data and returns
|
2023-02-18 16:31:06 +00:00
|
|
|
echo("Today: %s minutes" % usage.usage_today)
|
|
|
|
echo("This month: %s minutes" % usage.usage_this_month)
|
2021-11-19 15:41:49 +00:00
|
|
|
|
2023-02-18 20:41:08 +00:00
|
|
|
return usage
|
2021-11-19 15:41:49 +00:00
|
|
|
|
|
|
|
# output any detailed usage data
|
|
|
|
for index, usage in usage_data.items():
|
2023-02-18 16:31:06 +00:00
|
|
|
echo(f"{index}, {usage}")
|
2021-11-19 15:41:49 +00:00
|
|
|
|
2023-02-18 20:41:08 +00:00
|
|
|
return usage_data
|
|
|
|
|
2021-11-19 15:41:49 +00:00
|
|
|
|
2017-04-24 17:28:22 +00:00
|
|
|
@cli.command()
|
API and tests cleanup (#151)
* Add new cli commands: raw_command and dump_discover
- raw_command can be used to execute raw commands with given parameters
* Useful for testing new calls before implementing them properly
- dump_discover can be used to dump the device discovery information (into a file)
* The discovery is extended to request more modules and methods from devices
* smartlife.iot.dimmer get_dimmer_parameters
* smartlife.iot.common.emeter get_realtime
* smartlife.iot.smartbulb.lightingservice get_light_state
* This is used to dump more information for proper tests, and will also allow better discovery in the future
This commit contains also some documentation updates and dropping click_datetime in favor of click's built-in datetime
* Docstring fixes
* Major API cleanup
Properties shall no more change the state of the device, this work in still in progress, the main goal being making the API more user-friendly and to make implementing new features simpler.
The newly deprecated functionality will remain working and will simply warn the user about deprecation.
Previously deprecated 'features' property and 'identify' method are now finally removed.
Deprecate and replace the following property setters:
* state with turn_on() and turn_off()
* hsv with set_hsv()
* color_temp with set_color_temp()
* brightness with set_brightness()
* led with set_led()
* alias with set_alias()
* mac with set_mac()
And getters:
* state with is_on and is_off
The {BULB,PLUG}_STATE_{ON,OFF} is simplified to STATE_ON and STATE_OFF, UNKNOWN state is removed.
These are now deprecated and will be removed in the future.
* is_on and is_off can be used to check for the state
* turn_on() and turn_off() for changing the device state.
Trying to use functionality not supported by the device will cause SmartDeviceExceptions instead of failing silently and/or returning None.
This includes, e.g., trying to set a color temperature on non-supported bulb.
ValueErrors are raised instead of SmartDeviceExceptions where appropriate (e.g. when trying to set an invalid hsv or brightness).
New enum type DeviceType is added to allow detecting device types without resorting to isinstance() calling. SmartDevice class' device_type property can be used to query the type. is_plug and is_bulb helpers are added.
* Cleanup tests and improve test coverage
* Make writing tests easier by sharing code for common implementations
* Instead of storing test data inside python files, dump-discover based information is used
* This will simplify adding new tests and remove code duplication
* fixtures are based on https://github.com/plasticrake/tplink-smarthome-simulator
* run black on newfakes
* Add HS300 tests and update SmartStrip API according to earlier changes, still WIP
* run black and avoid wildcard imports
* Black on conftest
* bump minimum required version to 3.5
* Rename fixture_tests to test_fixtures for autocollect
* fix typoed type to _type, black
* run black on several files with -79 to fix hound issues
* Fix broken merge on hue
* Fix tests (hue update, pass context to smartdevice), add is_strip property, disable emeter tests for HS300 until a solution for API is found.
* Fix old tests
* Run black on changed files
* Add real HS220 discovery, thanks to @poiyo
* add is_dimmable and is_variable_color_temp to smartdevice class, simplifies interfacing with homeassistant
* add KL120(US) fixture
* Add a simple query cache
This commit adds a simple query cache to speed up the process for users
requesting lots of different properties from the device, as done by the
cli tool as well as homeassistant.
The logic for caching is very simple:
1. A timestamp for last fetch for each module+command is stored alongside the response.
2. If the issued command starts with `get_` and the TTL has not expired, the cache result is returned.
3. Otherwise the cache for the whole corresponding module gets invalidated, the device will be queried and the result will be stored in the cache.
* add deprecation to tox.ini
* make tests pass again
* remove old tests, add flake8 to tox reqs
* run black against pyhs100 module, add it to precommit hooks, fix flake8 configuration to conform to black standards (https://ljvmiranda921.github.io/notebook/2018/06/21/precommits-using-black-and-flake8/)
* fix syntax
* cleanup conftest
2019-06-16 21:05:00 +00:00
|
|
|
@click.argument("brightness", type=click.IntRange(0, 100), default=None, required=False)
|
2020-07-06 14:10:28 +00:00
|
|
|
@click.option("--transition", type=int, required=False)
|
2017-04-24 17:28:22 +00:00
|
|
|
@pass_dev
|
2024-05-14 07:38:21 +00:00
|
|
|
async def brightness(dev: Device, brightness: int, transition: int):
|
2018-10-18 17:57:44 +00:00
|
|
|
"""Get or set brightness."""
|
2024-05-14 07:38:21 +00:00
|
|
|
if not (light := dev.modules.get(Module.Light)) or not light.is_dimmable:
|
2023-02-18 16:31:06 +00:00
|
|
|
echo("This device does not support brightness.")
|
2018-10-18 17:57:44 +00:00
|
|
|
return
|
2021-10-09 14:36:36 +00:00
|
|
|
|
2017-09-15 12:23:06 +00:00
|
|
|
if brightness is None:
|
2024-05-14 07:38:21 +00:00
|
|
|
echo(f"Brightness: {light.brightness}")
|
|
|
|
return light.brightness
|
2017-04-24 17:28:22 +00:00
|
|
|
else:
|
2023-02-18 16:31:06 +00:00
|
|
|
echo(f"Setting brightness to {brightness}")
|
2024-05-14 07:38:21 +00:00
|
|
|
return await light.set_brightness(brightness, transition=transition)
|
2017-04-24 17:28:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
@cli.command()
|
API and tests cleanup (#151)
* Add new cli commands: raw_command and dump_discover
- raw_command can be used to execute raw commands with given parameters
* Useful for testing new calls before implementing them properly
- dump_discover can be used to dump the device discovery information (into a file)
* The discovery is extended to request more modules and methods from devices
* smartlife.iot.dimmer get_dimmer_parameters
* smartlife.iot.common.emeter get_realtime
* smartlife.iot.smartbulb.lightingservice get_light_state
* This is used to dump more information for proper tests, and will also allow better discovery in the future
This commit contains also some documentation updates and dropping click_datetime in favor of click's built-in datetime
* Docstring fixes
* Major API cleanup
Properties shall no more change the state of the device, this work in still in progress, the main goal being making the API more user-friendly and to make implementing new features simpler.
The newly deprecated functionality will remain working and will simply warn the user about deprecation.
Previously deprecated 'features' property and 'identify' method are now finally removed.
Deprecate and replace the following property setters:
* state with turn_on() and turn_off()
* hsv with set_hsv()
* color_temp with set_color_temp()
* brightness with set_brightness()
* led with set_led()
* alias with set_alias()
* mac with set_mac()
And getters:
* state with is_on and is_off
The {BULB,PLUG}_STATE_{ON,OFF} is simplified to STATE_ON and STATE_OFF, UNKNOWN state is removed.
These are now deprecated and will be removed in the future.
* is_on and is_off can be used to check for the state
* turn_on() and turn_off() for changing the device state.
Trying to use functionality not supported by the device will cause SmartDeviceExceptions instead of failing silently and/or returning None.
This includes, e.g., trying to set a color temperature on non-supported bulb.
ValueErrors are raised instead of SmartDeviceExceptions where appropriate (e.g. when trying to set an invalid hsv or brightness).
New enum type DeviceType is added to allow detecting device types without resorting to isinstance() calling. SmartDevice class' device_type property can be used to query the type. is_plug and is_bulb helpers are added.
* Cleanup tests and improve test coverage
* Make writing tests easier by sharing code for common implementations
* Instead of storing test data inside python files, dump-discover based information is used
* This will simplify adding new tests and remove code duplication
* fixtures are based on https://github.com/plasticrake/tplink-smarthome-simulator
* run black on newfakes
* Add HS300 tests and update SmartStrip API according to earlier changes, still WIP
* run black and avoid wildcard imports
* Black on conftest
* bump minimum required version to 3.5
* Rename fixture_tests to test_fixtures for autocollect
* fix typoed type to _type, black
* run black on several files with -79 to fix hound issues
* Fix broken merge on hue
* Fix tests (hue update, pass context to smartdevice), add is_strip property, disable emeter tests for HS300 until a solution for API is found.
* Fix old tests
* Run black on changed files
* Add real HS220 discovery, thanks to @poiyo
* add is_dimmable and is_variable_color_temp to smartdevice class, simplifies interfacing with homeassistant
* add KL120(US) fixture
* Add a simple query cache
This commit adds a simple query cache to speed up the process for users
requesting lots of different properties from the device, as done by the
cli tool as well as homeassistant.
The logic for caching is very simple:
1. A timestamp for last fetch for each module+command is stored alongside the response.
2. If the issued command starts with `get_` and the TTL has not expired, the cache result is returned.
3. Otherwise the cache for the whole corresponding module gets invalidated, the device will be queried and the result will be stored in the cache.
* add deprecation to tox.ini
* make tests pass again
* remove old tests, add flake8 to tox reqs
* run black against pyhs100 module, add it to precommit hooks, fix flake8 configuration to conform to black standards (https://ljvmiranda921.github.io/notebook/2018/06/21/precommits-using-black-and-flake8/)
* fix syntax
* cleanup conftest
2019-06-16 21:05:00 +00:00
|
|
|
@click.argument(
|
|
|
|
"temperature", type=click.IntRange(2500, 9000), default=None, required=False
|
|
|
|
)
|
2020-07-06 14:10:28 +00:00
|
|
|
@click.option("--transition", type=int, required=False)
|
2017-04-24 17:28:22 +00:00
|
|
|
@pass_dev
|
2024-05-14 07:38:21 +00:00
|
|
|
async def temperature(dev: Device, temperature: int, transition: int):
|
API and tests cleanup (#151)
* Add new cli commands: raw_command and dump_discover
- raw_command can be used to execute raw commands with given parameters
* Useful for testing new calls before implementing them properly
- dump_discover can be used to dump the device discovery information (into a file)
* The discovery is extended to request more modules and methods from devices
* smartlife.iot.dimmer get_dimmer_parameters
* smartlife.iot.common.emeter get_realtime
* smartlife.iot.smartbulb.lightingservice get_light_state
* This is used to dump more information for proper tests, and will also allow better discovery in the future
This commit contains also some documentation updates and dropping click_datetime in favor of click's built-in datetime
* Docstring fixes
* Major API cleanup
Properties shall no more change the state of the device, this work in still in progress, the main goal being making the API more user-friendly and to make implementing new features simpler.
The newly deprecated functionality will remain working and will simply warn the user about deprecation.
Previously deprecated 'features' property and 'identify' method are now finally removed.
Deprecate and replace the following property setters:
* state with turn_on() and turn_off()
* hsv with set_hsv()
* color_temp with set_color_temp()
* brightness with set_brightness()
* led with set_led()
* alias with set_alias()
* mac with set_mac()
And getters:
* state with is_on and is_off
The {BULB,PLUG}_STATE_{ON,OFF} is simplified to STATE_ON and STATE_OFF, UNKNOWN state is removed.
These are now deprecated and will be removed in the future.
* is_on and is_off can be used to check for the state
* turn_on() and turn_off() for changing the device state.
Trying to use functionality not supported by the device will cause SmartDeviceExceptions instead of failing silently and/or returning None.
This includes, e.g., trying to set a color temperature on non-supported bulb.
ValueErrors are raised instead of SmartDeviceExceptions where appropriate (e.g. when trying to set an invalid hsv or brightness).
New enum type DeviceType is added to allow detecting device types without resorting to isinstance() calling. SmartDevice class' device_type property can be used to query the type. is_plug and is_bulb helpers are added.
* Cleanup tests and improve test coverage
* Make writing tests easier by sharing code for common implementations
* Instead of storing test data inside python files, dump-discover based information is used
* This will simplify adding new tests and remove code duplication
* fixtures are based on https://github.com/plasticrake/tplink-smarthome-simulator
* run black on newfakes
* Add HS300 tests and update SmartStrip API according to earlier changes, still WIP
* run black and avoid wildcard imports
* Black on conftest
* bump minimum required version to 3.5
* Rename fixture_tests to test_fixtures for autocollect
* fix typoed type to _type, black
* run black on several files with -79 to fix hound issues
* Fix broken merge on hue
* Fix tests (hue update, pass context to smartdevice), add is_strip property, disable emeter tests for HS300 until a solution for API is found.
* Fix old tests
* Run black on changed files
* Add real HS220 discovery, thanks to @poiyo
* add is_dimmable and is_variable_color_temp to smartdevice class, simplifies interfacing with homeassistant
* add KL120(US) fixture
* Add a simple query cache
This commit adds a simple query cache to speed up the process for users
requesting lots of different properties from the device, as done by the
cli tool as well as homeassistant.
The logic for caching is very simple:
1. A timestamp for last fetch for each module+command is stored alongside the response.
2. If the issued command starts with `get_` and the TTL has not expired, the cache result is returned.
3. Otherwise the cache for the whole corresponding module gets invalidated, the device will be queried and the result will be stored in the cache.
* add deprecation to tox.ini
* make tests pass again
* remove old tests, add flake8 to tox reqs
* run black against pyhs100 module, add it to precommit hooks, fix flake8 configuration to conform to black standards (https://ljvmiranda921.github.io/notebook/2018/06/21/precommits-using-black-and-flake8/)
* fix syntax
* cleanup conftest
2019-06-16 21:05:00 +00:00
|
|
|
"""Get or set color temperature."""
|
2024-05-14 07:38:21 +00:00
|
|
|
if not (light := dev.modules.get(Module.Light)) or not light.is_variable_color_temp:
|
2023-02-18 16:31:06 +00:00
|
|
|
echo("Device does not support color temperature")
|
2021-09-04 00:18:21 +00:00
|
|
|
return
|
2021-10-09 14:36:36 +00:00
|
|
|
|
2017-09-15 12:23:06 +00:00
|
|
|
if temperature is None:
|
2024-05-14 07:38:21 +00:00
|
|
|
echo(f"Color temperature: {light.color_temp}")
|
|
|
|
valid_temperature_range = light.valid_temperature_range
|
2019-11-11 18:53:17 +00:00
|
|
|
if valid_temperature_range != (0, 0):
|
2023-02-18 16:31:06 +00:00
|
|
|
echo("(min: {}, max: {})".format(*valid_temperature_range))
|
2019-03-16 20:32:25 +00:00
|
|
|
else:
|
2023-02-18 16:31:06 +00:00
|
|
|
echo(
|
API and tests cleanup (#151)
* Add new cli commands: raw_command and dump_discover
- raw_command can be used to execute raw commands with given parameters
* Useful for testing new calls before implementing them properly
- dump_discover can be used to dump the device discovery information (into a file)
* The discovery is extended to request more modules and methods from devices
* smartlife.iot.dimmer get_dimmer_parameters
* smartlife.iot.common.emeter get_realtime
* smartlife.iot.smartbulb.lightingservice get_light_state
* This is used to dump more information for proper tests, and will also allow better discovery in the future
This commit contains also some documentation updates and dropping click_datetime in favor of click's built-in datetime
* Docstring fixes
* Major API cleanup
Properties shall no more change the state of the device, this work in still in progress, the main goal being making the API more user-friendly and to make implementing new features simpler.
The newly deprecated functionality will remain working and will simply warn the user about deprecation.
Previously deprecated 'features' property and 'identify' method are now finally removed.
Deprecate and replace the following property setters:
* state with turn_on() and turn_off()
* hsv with set_hsv()
* color_temp with set_color_temp()
* brightness with set_brightness()
* led with set_led()
* alias with set_alias()
* mac with set_mac()
And getters:
* state with is_on and is_off
The {BULB,PLUG}_STATE_{ON,OFF} is simplified to STATE_ON and STATE_OFF, UNKNOWN state is removed.
These are now deprecated and will be removed in the future.
* is_on and is_off can be used to check for the state
* turn_on() and turn_off() for changing the device state.
Trying to use functionality not supported by the device will cause SmartDeviceExceptions instead of failing silently and/or returning None.
This includes, e.g., trying to set a color temperature on non-supported bulb.
ValueErrors are raised instead of SmartDeviceExceptions where appropriate (e.g. when trying to set an invalid hsv or brightness).
New enum type DeviceType is added to allow detecting device types without resorting to isinstance() calling. SmartDevice class' device_type property can be used to query the type. is_plug and is_bulb helpers are added.
* Cleanup tests and improve test coverage
* Make writing tests easier by sharing code for common implementations
* Instead of storing test data inside python files, dump-discover based information is used
* This will simplify adding new tests and remove code duplication
* fixtures are based on https://github.com/plasticrake/tplink-smarthome-simulator
* run black on newfakes
* Add HS300 tests and update SmartStrip API according to earlier changes, still WIP
* run black and avoid wildcard imports
* Black on conftest
* bump minimum required version to 3.5
* Rename fixture_tests to test_fixtures for autocollect
* fix typoed type to _type, black
* run black on several files with -79 to fix hound issues
* Fix broken merge on hue
* Fix tests (hue update, pass context to smartdevice), add is_strip property, disable emeter tests for HS300 until a solution for API is found.
* Fix old tests
* Run black on changed files
* Add real HS220 discovery, thanks to @poiyo
* add is_dimmable and is_variable_color_temp to smartdevice class, simplifies interfacing with homeassistant
* add KL120(US) fixture
* Add a simple query cache
This commit adds a simple query cache to speed up the process for users
requesting lots of different properties from the device, as done by the
cli tool as well as homeassistant.
The logic for caching is very simple:
1. A timestamp for last fetch for each module+command is stored alongside the response.
2. If the issued command starts with `get_` and the TTL has not expired, the cache result is returned.
3. Otherwise the cache for the whole corresponding module gets invalidated, the device will be queried and the result will be stored in the cache.
* add deprecation to tox.ini
* make tests pass again
* remove old tests, add flake8 to tox reqs
* run black against pyhs100 module, add it to precommit hooks, fix flake8 configuration to conform to black standards (https://ljvmiranda921.github.io/notebook/2018/06/21/precommits-using-black-and-flake8/)
* fix syntax
* cleanup conftest
2019-06-16 21:05:00 +00:00
|
|
|
"Temperature range unknown, please open a github issue"
|
2019-11-15 16:48:36 +00:00
|
|
|
f" or a pull request for model '{dev.model}'"
|
API and tests cleanup (#151)
* Add new cli commands: raw_command and dump_discover
- raw_command can be used to execute raw commands with given parameters
* Useful for testing new calls before implementing them properly
- dump_discover can be used to dump the device discovery information (into a file)
* The discovery is extended to request more modules and methods from devices
* smartlife.iot.dimmer get_dimmer_parameters
* smartlife.iot.common.emeter get_realtime
* smartlife.iot.smartbulb.lightingservice get_light_state
* This is used to dump more information for proper tests, and will also allow better discovery in the future
This commit contains also some documentation updates and dropping click_datetime in favor of click's built-in datetime
* Docstring fixes
* Major API cleanup
Properties shall no more change the state of the device, this work in still in progress, the main goal being making the API more user-friendly and to make implementing new features simpler.
The newly deprecated functionality will remain working and will simply warn the user about deprecation.
Previously deprecated 'features' property and 'identify' method are now finally removed.
Deprecate and replace the following property setters:
* state with turn_on() and turn_off()
* hsv with set_hsv()
* color_temp with set_color_temp()
* brightness with set_brightness()
* led with set_led()
* alias with set_alias()
* mac with set_mac()
And getters:
* state with is_on and is_off
The {BULB,PLUG}_STATE_{ON,OFF} is simplified to STATE_ON and STATE_OFF, UNKNOWN state is removed.
These are now deprecated and will be removed in the future.
* is_on and is_off can be used to check for the state
* turn_on() and turn_off() for changing the device state.
Trying to use functionality not supported by the device will cause SmartDeviceExceptions instead of failing silently and/or returning None.
This includes, e.g., trying to set a color temperature on non-supported bulb.
ValueErrors are raised instead of SmartDeviceExceptions where appropriate (e.g. when trying to set an invalid hsv or brightness).
New enum type DeviceType is added to allow detecting device types without resorting to isinstance() calling. SmartDevice class' device_type property can be used to query the type. is_plug and is_bulb helpers are added.
* Cleanup tests and improve test coverage
* Make writing tests easier by sharing code for common implementations
* Instead of storing test data inside python files, dump-discover based information is used
* This will simplify adding new tests and remove code duplication
* fixtures are based on https://github.com/plasticrake/tplink-smarthome-simulator
* run black on newfakes
* Add HS300 tests and update SmartStrip API according to earlier changes, still WIP
* run black and avoid wildcard imports
* Black on conftest
* bump minimum required version to 3.5
* Rename fixture_tests to test_fixtures for autocollect
* fix typoed type to _type, black
* run black on several files with -79 to fix hound issues
* Fix broken merge on hue
* Fix tests (hue update, pass context to smartdevice), add is_strip property, disable emeter tests for HS300 until a solution for API is found.
* Fix old tests
* Run black on changed files
* Add real HS220 discovery, thanks to @poiyo
* add is_dimmable and is_variable_color_temp to smartdevice class, simplifies interfacing with homeassistant
* add KL120(US) fixture
* Add a simple query cache
This commit adds a simple query cache to speed up the process for users
requesting lots of different properties from the device, as done by the
cli tool as well as homeassistant.
The logic for caching is very simple:
1. A timestamp for last fetch for each module+command is stored alongside the response.
2. If the issued command starts with `get_` and the TTL has not expired, the cache result is returned.
3. Otherwise the cache for the whole corresponding module gets invalidated, the device will be queried and the result will be stored in the cache.
* add deprecation to tox.ini
* make tests pass again
* remove old tests, add flake8 to tox reqs
* run black against pyhs100 module, add it to precommit hooks, fix flake8 configuration to conform to black standards (https://ljvmiranda921.github.io/notebook/2018/06/21/precommits-using-black-and-flake8/)
* fix syntax
* cleanup conftest
2019-06-16 21:05:00 +00:00
|
|
|
)
|
2024-05-14 07:38:21 +00:00
|
|
|
return light.valid_temperature_range
|
2017-04-24 17:28:22 +00:00
|
|
|
else:
|
2023-02-18 16:31:06 +00:00
|
|
|
echo(f"Setting color temperature to {temperature}")
|
2024-05-14 07:38:21 +00:00
|
|
|
return await light.set_color_temp(temperature, transition=transition)
|
2017-04-24 17:28:22 +00:00
|
|
|
|
|
|
|
|
2022-03-21 21:10:12 +00:00
|
|
|
@cli.command()
|
|
|
|
@click.argument("effect", type=click.STRING, default=None, required=False)
|
|
|
|
@click.pass_context
|
|
|
|
@pass_dev
|
2024-05-14 07:38:21 +00:00
|
|
|
async def effect(dev: Device, ctx, effect):
|
2022-03-21 21:10:12 +00:00
|
|
|
"""Set an effect."""
|
2024-05-14 07:38:21 +00:00
|
|
|
if not (light_effect := dev.modules.get(Module.LightEffect)):
|
2023-02-18 16:31:06 +00:00
|
|
|
echo("Device does not support effects")
|
2022-03-21 21:10:12 +00:00
|
|
|
return
|
|
|
|
if effect is None:
|
2024-05-15 05:16:57 +00:00
|
|
|
echo(
|
|
|
|
f"Light effect: {light_effect.effect}\n"
|
|
|
|
+ f"Available Effects: {light_effect.effect_list}"
|
2022-03-21 21:10:12 +00:00
|
|
|
)
|
2024-05-15 05:16:57 +00:00
|
|
|
return light_effect.effect
|
|
|
|
|
2024-05-14 07:38:21 +00:00
|
|
|
if effect not in light_effect.effect_list:
|
|
|
|
raise click.BadArgumentUsage(
|
|
|
|
f"Effect must be one of: {light_effect.effect_list}", ctx
|
|
|
|
)
|
2022-03-21 21:10:12 +00:00
|
|
|
|
2023-02-18 16:31:06 +00:00
|
|
|
echo(f"Setting Effect: {effect}")
|
2024-05-14 07:38:21 +00:00
|
|
|
return await light_effect.set_effect(effect)
|
2022-03-21 21:10:12 +00:00
|
|
|
|
|
|
|
|
2017-04-24 17:28:22 +00:00
|
|
|
@cli.command()
|
2018-11-23 21:39:58 +00:00
|
|
|
@click.argument("h", type=click.IntRange(0, 360), default=None, required=False)
|
|
|
|
@click.argument("s", type=click.IntRange(0, 100), default=None, required=False)
|
|
|
|
@click.argument("v", type=click.IntRange(0, 100), default=None, required=False)
|
2020-07-06 14:10:28 +00:00
|
|
|
@click.option("--transition", type=int, required=False)
|
2018-11-23 21:39:58 +00:00
|
|
|
@click.pass_context
|
2017-08-03 20:13:56 +00:00
|
|
|
@pass_dev
|
2024-05-14 07:38:21 +00:00
|
|
|
async def hsv(dev: Device, ctx, h, s, v, transition):
|
2021-10-09 14:36:36 +00:00
|
|
|
"""Get or set color in HSV."""
|
2024-05-14 07:38:21 +00:00
|
|
|
if not (light := dev.modules.get(Module.Light)) or not light.is_color:
|
2023-02-18 16:31:06 +00:00
|
|
|
echo("Device does not support colors")
|
2021-10-09 14:36:36 +00:00
|
|
|
return
|
|
|
|
|
2024-05-14 07:38:21 +00:00
|
|
|
if h is None and s is None and v is None:
|
|
|
|
echo(f"Current HSV: {light.hsv}")
|
|
|
|
return light.hsv
|
2018-11-23 21:39:58 +00:00
|
|
|
elif s is None or v is None:
|
|
|
|
raise click.BadArgumentUsage("Setting a color requires 3 values.", ctx)
|
2017-04-24 17:28:22 +00:00
|
|
|
else:
|
2023-02-18 16:31:06 +00:00
|
|
|
echo(f"Setting HSV: {h} {s} {v}")
|
2024-05-14 07:38:21 +00:00
|
|
|
return await light.set_hsv(h, s, v, transition=transition)
|
2017-03-20 18:03:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
@cli.command()
|
API and tests cleanup (#151)
* Add new cli commands: raw_command and dump_discover
- raw_command can be used to execute raw commands with given parameters
* Useful for testing new calls before implementing them properly
- dump_discover can be used to dump the device discovery information (into a file)
* The discovery is extended to request more modules and methods from devices
* smartlife.iot.dimmer get_dimmer_parameters
* smartlife.iot.common.emeter get_realtime
* smartlife.iot.smartbulb.lightingservice get_light_state
* This is used to dump more information for proper tests, and will also allow better discovery in the future
This commit contains also some documentation updates and dropping click_datetime in favor of click's built-in datetime
* Docstring fixes
* Major API cleanup
Properties shall no more change the state of the device, this work in still in progress, the main goal being making the API more user-friendly and to make implementing new features simpler.
The newly deprecated functionality will remain working and will simply warn the user about deprecation.
Previously deprecated 'features' property and 'identify' method are now finally removed.
Deprecate and replace the following property setters:
* state with turn_on() and turn_off()
* hsv with set_hsv()
* color_temp with set_color_temp()
* brightness with set_brightness()
* led with set_led()
* alias with set_alias()
* mac with set_mac()
And getters:
* state with is_on and is_off
The {BULB,PLUG}_STATE_{ON,OFF} is simplified to STATE_ON and STATE_OFF, UNKNOWN state is removed.
These are now deprecated and will be removed in the future.
* is_on and is_off can be used to check for the state
* turn_on() and turn_off() for changing the device state.
Trying to use functionality not supported by the device will cause SmartDeviceExceptions instead of failing silently and/or returning None.
This includes, e.g., trying to set a color temperature on non-supported bulb.
ValueErrors are raised instead of SmartDeviceExceptions where appropriate (e.g. when trying to set an invalid hsv or brightness).
New enum type DeviceType is added to allow detecting device types without resorting to isinstance() calling. SmartDevice class' device_type property can be used to query the type. is_plug and is_bulb helpers are added.
* Cleanup tests and improve test coverage
* Make writing tests easier by sharing code for common implementations
* Instead of storing test data inside python files, dump-discover based information is used
* This will simplify adding new tests and remove code duplication
* fixtures are based on https://github.com/plasticrake/tplink-smarthome-simulator
* run black on newfakes
* Add HS300 tests and update SmartStrip API according to earlier changes, still WIP
* run black and avoid wildcard imports
* Black on conftest
* bump minimum required version to 3.5
* Rename fixture_tests to test_fixtures for autocollect
* fix typoed type to _type, black
* run black on several files with -79 to fix hound issues
* Fix broken merge on hue
* Fix tests (hue update, pass context to smartdevice), add is_strip property, disable emeter tests for HS300 until a solution for API is found.
* Fix old tests
* Run black on changed files
* Add real HS220 discovery, thanks to @poiyo
* add is_dimmable and is_variable_color_temp to smartdevice class, simplifies interfacing with homeassistant
* add KL120(US) fixture
* Add a simple query cache
This commit adds a simple query cache to speed up the process for users
requesting lots of different properties from the device, as done by the
cli tool as well as homeassistant.
The logic for caching is very simple:
1. A timestamp for last fetch for each module+command is stored alongside the response.
2. If the issued command starts with `get_` and the TTL has not expired, the cache result is returned.
3. Otherwise the cache for the whole corresponding module gets invalidated, the device will be queried and the result will be stored in the cache.
* add deprecation to tox.ini
* make tests pass again
* remove old tests, add flake8 to tox reqs
* run black against pyhs100 module, add it to precommit hooks, fix flake8 configuration to conform to black standards (https://ljvmiranda921.github.io/notebook/2018/06/21/precommits-using-black-and-flake8/)
* fix syntax
* cleanup conftest
2019-06-16 21:05:00 +00:00
|
|
|
@click.argument("state", type=bool, required=False)
|
2017-03-20 18:03:19 +00:00
|
|
|
@pass_dev
|
2024-05-14 07:38:21 +00:00
|
|
|
async def led(dev: Device, state):
|
API and tests cleanup (#151)
* Add new cli commands: raw_command and dump_discover
- raw_command can be used to execute raw commands with given parameters
* Useful for testing new calls before implementing them properly
- dump_discover can be used to dump the device discovery information (into a file)
* The discovery is extended to request more modules and methods from devices
* smartlife.iot.dimmer get_dimmer_parameters
* smartlife.iot.common.emeter get_realtime
* smartlife.iot.smartbulb.lightingservice get_light_state
* This is used to dump more information for proper tests, and will also allow better discovery in the future
This commit contains also some documentation updates and dropping click_datetime in favor of click's built-in datetime
* Docstring fixes
* Major API cleanup
Properties shall no more change the state of the device, this work in still in progress, the main goal being making the API more user-friendly and to make implementing new features simpler.
The newly deprecated functionality will remain working and will simply warn the user about deprecation.
Previously deprecated 'features' property and 'identify' method are now finally removed.
Deprecate and replace the following property setters:
* state with turn_on() and turn_off()
* hsv with set_hsv()
* color_temp with set_color_temp()
* brightness with set_brightness()
* led with set_led()
* alias with set_alias()
* mac with set_mac()
And getters:
* state with is_on and is_off
The {BULB,PLUG}_STATE_{ON,OFF} is simplified to STATE_ON and STATE_OFF, UNKNOWN state is removed.
These are now deprecated and will be removed in the future.
* is_on and is_off can be used to check for the state
* turn_on() and turn_off() for changing the device state.
Trying to use functionality not supported by the device will cause SmartDeviceExceptions instead of failing silently and/or returning None.
This includes, e.g., trying to set a color temperature on non-supported bulb.
ValueErrors are raised instead of SmartDeviceExceptions where appropriate (e.g. when trying to set an invalid hsv or brightness).
New enum type DeviceType is added to allow detecting device types without resorting to isinstance() calling. SmartDevice class' device_type property can be used to query the type. is_plug and is_bulb helpers are added.
* Cleanup tests and improve test coverage
* Make writing tests easier by sharing code for common implementations
* Instead of storing test data inside python files, dump-discover based information is used
* This will simplify adding new tests and remove code duplication
* fixtures are based on https://github.com/plasticrake/tplink-smarthome-simulator
* run black on newfakes
* Add HS300 tests and update SmartStrip API according to earlier changes, still WIP
* run black and avoid wildcard imports
* Black on conftest
* bump minimum required version to 3.5
* Rename fixture_tests to test_fixtures for autocollect
* fix typoed type to _type, black
* run black on several files with -79 to fix hound issues
* Fix broken merge on hue
* Fix tests (hue update, pass context to smartdevice), add is_strip property, disable emeter tests for HS300 until a solution for API is found.
* Fix old tests
* Run black on changed files
* Add real HS220 discovery, thanks to @poiyo
* add is_dimmable and is_variable_color_temp to smartdevice class, simplifies interfacing with homeassistant
* add KL120(US) fixture
* Add a simple query cache
This commit adds a simple query cache to speed up the process for users
requesting lots of different properties from the device, as done by the
cli tool as well as homeassistant.
The logic for caching is very simple:
1. A timestamp for last fetch for each module+command is stored alongside the response.
2. If the issued command starts with `get_` and the TTL has not expired, the cache result is returned.
3. Otherwise the cache for the whole corresponding module gets invalidated, the device will be queried and the result will be stored in the cache.
* add deprecation to tox.ini
* make tests pass again
* remove old tests, add flake8 to tox reqs
* run black against pyhs100 module, add it to precommit hooks, fix flake8 configuration to conform to black standards (https://ljvmiranda921.github.io/notebook/2018/06/21/precommits-using-black-and-flake8/)
* fix syntax
* cleanup conftest
2019-06-16 21:05:00 +00:00
|
|
|
"""Get or set (Plug's) led state."""
|
2024-05-14 07:38:21 +00:00
|
|
|
if not (led := dev.modules.get(Module.Led)):
|
|
|
|
echo("Device does not support led.")
|
|
|
|
return
|
2017-03-20 18:03:19 +00:00
|
|
|
if state is not None:
|
2023-02-18 16:31:06 +00:00
|
|
|
echo(f"Turning led to {state}")
|
2024-05-14 07:38:21 +00:00
|
|
|
return await led.set_led(state)
|
2017-03-20 18:03:19 +00:00
|
|
|
else:
|
2024-05-14 07:38:21 +00:00
|
|
|
echo(f"LED state: {led.led}")
|
|
|
|
return led.led
|
2017-03-20 18:03:19 +00:00
|
|
|
|
|
|
|
|
2017-10-16 22:53:18 +00:00
|
|
|
@cli.command()
|
|
|
|
@pass_dev
|
async++, small powerstrip improvements (#46)
* async++, small powerstrip improvements
* use asyncclick instead of click, allows defining the commands with async def to avoid manual eventloop/asyncio.run handling
* improve powerstrip support:
* new powerstrip api: turn_{on,off}_by_{name,index} methods
* cli: fix on/off for powerstrip using the new apis
* add missing update()s for cli's hsv, led, temperature (fixes #43)
* prettyprint the received payloads when debug mode in use
* cli: debug mode can be activated now with '-d'
* update requirements_test.txt
* remove outdated click-datetime, replace click with asyncclick
* debug is a flag
* make smartstripplug to inherit the sysinfo from its parent, allows for simple access of general plug properties
* proper bound checking for index accesses, allow controlling the plug at index 0
* remove the mess of turn_{on,off}_by_{name,index}, get_plug_by_{name,index} are enough.
* adapt cli to use that
* allow changing the alias per index
* use f-strings consistently everywhere in the cli
* add tests for get_plug_by_{index,name}
2020-04-21 18:46:13 +00:00
|
|
|
async def time(dev):
|
2017-10-16 22:53:18 +00:00
|
|
|
"""Get the device time."""
|
2021-11-07 01:41:12 +00:00
|
|
|
res = dev.time
|
2023-02-18 16:31:06 +00:00
|
|
|
echo(f"Current time: {res}")
|
2021-10-09 14:36:36 +00:00
|
|
|
return res
|
2017-10-16 22:53:18 +00:00
|
|
|
|
|
|
|
|
2017-03-20 18:03:19 +00:00
|
|
|
@cli.command()
|
async++, small powerstrip improvements (#46)
* async++, small powerstrip improvements
* use asyncclick instead of click, allows defining the commands with async def to avoid manual eventloop/asyncio.run handling
* improve powerstrip support:
* new powerstrip api: turn_{on,off}_by_{name,index} methods
* cli: fix on/off for powerstrip using the new apis
* add missing update()s for cli's hsv, led, temperature (fixes #43)
* prettyprint the received payloads when debug mode in use
* cli: debug mode can be activated now with '-d'
* update requirements_test.txt
* remove outdated click-datetime, replace click with asyncclick
* debug is a flag
* make smartstripplug to inherit the sysinfo from its parent, allows for simple access of general plug properties
* proper bound checking for index accesses, allow controlling the plug at index 0
* remove the mess of turn_{on,off}_by_{name,index}, get_plug_by_{name,index} are enough.
* adapt cli to use that
* allow changing the alias per index
* use f-strings consistently everywhere in the cli
* add tests for get_plug_by_{index,name}
2020-04-21 18:46:13 +00:00
|
|
|
@click.option("--index", type=int, required=False)
|
|
|
|
@click.option("--name", type=str, required=False)
|
2020-07-06 14:10:28 +00:00
|
|
|
@click.option("--transition", type=int, required=False)
|
2017-03-20 18:03:19 +00:00
|
|
|
@pass_dev
|
2024-02-04 15:20:08 +00:00
|
|
|
async def on(dev: Device, index: int, name: str, transition: int):
|
2017-03-20 18:03:19 +00:00
|
|
|
"""Turn the device on."""
|
async++, small powerstrip improvements (#46)
* async++, small powerstrip improvements
* use asyncclick instead of click, allows defining the commands with async def to avoid manual eventloop/asyncio.run handling
* improve powerstrip support:
* new powerstrip api: turn_{on,off}_by_{name,index} methods
* cli: fix on/off for powerstrip using the new apis
* add missing update()s for cli's hsv, led, temperature (fixes #43)
* prettyprint the received payloads when debug mode in use
* cli: debug mode can be activated now with '-d'
* update requirements_test.txt
* remove outdated click-datetime, replace click with asyncclick
* debug is a flag
* make smartstripplug to inherit the sysinfo from its parent, allows for simple access of general plug properties
* proper bound checking for index accesses, allow controlling the plug at index 0
* remove the mess of turn_{on,off}_by_{name,index}, get_plug_by_{name,index} are enough.
* adapt cli to use that
* allow changing the alias per index
* use f-strings consistently everywhere in the cli
* add tests for get_plug_by_{index,name}
2020-04-21 18:46:13 +00:00
|
|
|
if index is not None or name is not None:
|
2024-05-14 07:38:21 +00:00
|
|
|
if not dev.children:
|
|
|
|
echo("Index and name are only for devices with children.")
|
async++, small powerstrip improvements (#46)
* async++, small powerstrip improvements
* use asyncclick instead of click, allows defining the commands with async def to avoid manual eventloop/asyncio.run handling
* improve powerstrip support:
* new powerstrip api: turn_{on,off}_by_{name,index} methods
* cli: fix on/off for powerstrip using the new apis
* add missing update()s for cli's hsv, led, temperature (fixes #43)
* prettyprint the received payloads when debug mode in use
* cli: debug mode can be activated now with '-d'
* update requirements_test.txt
* remove outdated click-datetime, replace click with asyncclick
* debug is a flag
* make smartstripplug to inherit the sysinfo from its parent, allows for simple access of general plug properties
* proper bound checking for index accesses, allow controlling the plug at index 0
* remove the mess of turn_{on,off}_by_{name,index}, get_plug_by_{name,index} are enough.
* adapt cli to use that
* allow changing the alias per index
* use f-strings consistently everywhere in the cli
* add tests for get_plug_by_{index,name}
2020-04-21 18:46:13 +00:00
|
|
|
return
|
2021-10-09 14:36:36 +00:00
|
|
|
|
async++, small powerstrip improvements (#46)
* async++, small powerstrip improvements
* use asyncclick instead of click, allows defining the commands with async def to avoid manual eventloop/asyncio.run handling
* improve powerstrip support:
* new powerstrip api: turn_{on,off}_by_{name,index} methods
* cli: fix on/off for powerstrip using the new apis
* add missing update()s for cli's hsv, led, temperature (fixes #43)
* prettyprint the received payloads when debug mode in use
* cli: debug mode can be activated now with '-d'
* update requirements_test.txt
* remove outdated click-datetime, replace click with asyncclick
* debug is a flag
* make smartstripplug to inherit the sysinfo from its parent, allows for simple access of general plug properties
* proper bound checking for index accesses, allow controlling the plug at index 0
* remove the mess of turn_{on,off}_by_{name,index}, get_plug_by_{name,index} are enough.
* adapt cli to use that
* allow changing the alias per index
* use f-strings consistently everywhere in the cli
* add tests for get_plug_by_{index,name}
2020-04-21 18:46:13 +00:00
|
|
|
if index is not None:
|
|
|
|
dev = dev.get_plug_by_index(index)
|
|
|
|
elif name:
|
|
|
|
dev = dev.get_plug_by_name(name)
|
|
|
|
|
2023-02-18 16:31:06 +00:00
|
|
|
echo(f"Turning on {dev.alias}")
|
2021-10-09 14:36:36 +00:00
|
|
|
return await dev.turn_on(transition=transition)
|
2017-03-20 18:03:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
@cli.command()
|
async++, small powerstrip improvements (#46)
* async++, small powerstrip improvements
* use asyncclick instead of click, allows defining the commands with async def to avoid manual eventloop/asyncio.run handling
* improve powerstrip support:
* new powerstrip api: turn_{on,off}_by_{name,index} methods
* cli: fix on/off for powerstrip using the new apis
* add missing update()s for cli's hsv, led, temperature (fixes #43)
* prettyprint the received payloads when debug mode in use
* cli: debug mode can be activated now with '-d'
* update requirements_test.txt
* remove outdated click-datetime, replace click with asyncclick
* debug is a flag
* make smartstripplug to inherit the sysinfo from its parent, allows for simple access of general plug properties
* proper bound checking for index accesses, allow controlling the plug at index 0
* remove the mess of turn_{on,off}_by_{name,index}, get_plug_by_{name,index} are enough.
* adapt cli to use that
* allow changing the alias per index
* use f-strings consistently everywhere in the cli
* add tests for get_plug_by_{index,name}
2020-04-21 18:46:13 +00:00
|
|
|
@click.option("--index", type=int, required=False)
|
|
|
|
@click.option("--name", type=str, required=False)
|
2020-07-06 14:10:28 +00:00
|
|
|
@click.option("--transition", type=int, required=False)
|
2017-03-20 18:03:19 +00:00
|
|
|
@pass_dev
|
2024-02-04 15:20:08 +00:00
|
|
|
async def off(dev: Device, index: int, name: str, transition: int):
|
2017-03-20 18:03:19 +00:00
|
|
|
"""Turn the device off."""
|
async++, small powerstrip improvements (#46)
* async++, small powerstrip improvements
* use asyncclick instead of click, allows defining the commands with async def to avoid manual eventloop/asyncio.run handling
* improve powerstrip support:
* new powerstrip api: turn_{on,off}_by_{name,index} methods
* cli: fix on/off for powerstrip using the new apis
* add missing update()s for cli's hsv, led, temperature (fixes #43)
* prettyprint the received payloads when debug mode in use
* cli: debug mode can be activated now with '-d'
* update requirements_test.txt
* remove outdated click-datetime, replace click with asyncclick
* debug is a flag
* make smartstripplug to inherit the sysinfo from its parent, allows for simple access of general plug properties
* proper bound checking for index accesses, allow controlling the plug at index 0
* remove the mess of turn_{on,off}_by_{name,index}, get_plug_by_{name,index} are enough.
* adapt cli to use that
* allow changing the alias per index
* use f-strings consistently everywhere in the cli
* add tests for get_plug_by_{index,name}
2020-04-21 18:46:13 +00:00
|
|
|
if index is not None or name is not None:
|
2024-05-14 07:38:21 +00:00
|
|
|
if not dev.children:
|
|
|
|
echo("Index and name are only for devices with children.")
|
async++, small powerstrip improvements (#46)
* async++, small powerstrip improvements
* use asyncclick instead of click, allows defining the commands with async def to avoid manual eventloop/asyncio.run handling
* improve powerstrip support:
* new powerstrip api: turn_{on,off}_by_{name,index} methods
* cli: fix on/off for powerstrip using the new apis
* add missing update()s for cli's hsv, led, temperature (fixes #43)
* prettyprint the received payloads when debug mode in use
* cli: debug mode can be activated now with '-d'
* update requirements_test.txt
* remove outdated click-datetime, replace click with asyncclick
* debug is a flag
* make smartstripplug to inherit the sysinfo from its parent, allows for simple access of general plug properties
* proper bound checking for index accesses, allow controlling the plug at index 0
* remove the mess of turn_{on,off}_by_{name,index}, get_plug_by_{name,index} are enough.
* adapt cli to use that
* allow changing the alias per index
* use f-strings consistently everywhere in the cli
* add tests for get_plug_by_{index,name}
2020-04-21 18:46:13 +00:00
|
|
|
return
|
2021-10-09 14:36:36 +00:00
|
|
|
|
async++, small powerstrip improvements (#46)
* async++, small powerstrip improvements
* use asyncclick instead of click, allows defining the commands with async def to avoid manual eventloop/asyncio.run handling
* improve powerstrip support:
* new powerstrip api: turn_{on,off}_by_{name,index} methods
* cli: fix on/off for powerstrip using the new apis
* add missing update()s for cli's hsv, led, temperature (fixes #43)
* prettyprint the received payloads when debug mode in use
* cli: debug mode can be activated now with '-d'
* update requirements_test.txt
* remove outdated click-datetime, replace click with asyncclick
* debug is a flag
* make smartstripplug to inherit the sysinfo from its parent, allows for simple access of general plug properties
* proper bound checking for index accesses, allow controlling the plug at index 0
* remove the mess of turn_{on,off}_by_{name,index}, get_plug_by_{name,index} are enough.
* adapt cli to use that
* allow changing the alias per index
* use f-strings consistently everywhere in the cli
* add tests for get_plug_by_{index,name}
2020-04-21 18:46:13 +00:00
|
|
|
if index is not None:
|
|
|
|
dev = dev.get_plug_by_index(index)
|
|
|
|
elif name:
|
|
|
|
dev = dev.get_plug_by_name(name)
|
|
|
|
|
2023-02-18 16:31:06 +00:00
|
|
|
echo(f"Turning off {dev.alias}")
|
2021-10-09 14:36:36 +00:00
|
|
|
return await dev.turn_off(transition=transition)
|
2017-03-20 18:03:19 +00:00
|
|
|
|
|
|
|
|
2023-08-26 12:21:38 +00:00
|
|
|
@cli.command()
|
|
|
|
@click.option("--index", type=int, required=False)
|
|
|
|
@click.option("--name", type=str, required=False)
|
|
|
|
@click.option("--transition", type=int, required=False)
|
|
|
|
@pass_dev
|
2024-02-04 15:20:08 +00:00
|
|
|
async def toggle(dev: Device, index: int, name: str, transition: int):
|
2023-08-26 12:21:38 +00:00
|
|
|
"""Toggle the device on/off."""
|
|
|
|
if index is not None or name is not None:
|
2024-05-14 07:38:21 +00:00
|
|
|
if not dev.children:
|
|
|
|
echo("Index and name are only for devices with children.")
|
2023-08-26 12:21:38 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
if index is not None:
|
|
|
|
dev = dev.get_plug_by_index(index)
|
|
|
|
elif name:
|
|
|
|
dev = dev.get_plug_by_name(name)
|
|
|
|
|
|
|
|
if dev.is_on:
|
|
|
|
echo(f"Turning off {dev.alias}")
|
|
|
|
return await dev.turn_off(transition=transition)
|
|
|
|
|
|
|
|
echo(f"Turning on {dev.alias}")
|
|
|
|
return await dev.turn_on(transition=transition)
|
|
|
|
|
|
|
|
|
2018-09-08 14:11:58 +00:00
|
|
|
@cli.command()
|
|
|
|
@click.option("--delay", default=1)
|
|
|
|
@pass_dev
|
async++, small powerstrip improvements (#46)
* async++, small powerstrip improvements
* use asyncclick instead of click, allows defining the commands with async def to avoid manual eventloop/asyncio.run handling
* improve powerstrip support:
* new powerstrip api: turn_{on,off}_by_{name,index} methods
* cli: fix on/off for powerstrip using the new apis
* add missing update()s for cli's hsv, led, temperature (fixes #43)
* prettyprint the received payloads when debug mode in use
* cli: debug mode can be activated now with '-d'
* update requirements_test.txt
* remove outdated click-datetime, replace click with asyncclick
* debug is a flag
* make smartstripplug to inherit the sysinfo from its parent, allows for simple access of general plug properties
* proper bound checking for index accesses, allow controlling the plug at index 0
* remove the mess of turn_{on,off}_by_{name,index}, get_plug_by_{name,index} are enough.
* adapt cli to use that
* allow changing the alias per index
* use f-strings consistently everywhere in the cli
* add tests for get_plug_by_{index,name}
2020-04-21 18:46:13 +00:00
|
|
|
async def reboot(plug, delay):
|
2018-09-08 14:11:58 +00:00
|
|
|
"""Reboot the device."""
|
2023-02-18 16:31:06 +00:00
|
|
|
echo("Rebooting the device..")
|
2021-10-09 14:36:36 +00:00
|
|
|
return await plug.reboot(delay)
|
2018-09-08 14:11:58 +00:00
|
|
|
|
2018-10-18 17:57:44 +00:00
|
|
|
|
2021-11-07 01:41:12 +00:00
|
|
|
@cli.group()
|
|
|
|
@pass_dev
|
|
|
|
async def schedule(dev):
|
|
|
|
"""Scheduling commands."""
|
|
|
|
|
|
|
|
|
|
|
|
@schedule.command(name="list")
|
|
|
|
@pass_dev
|
|
|
|
@click.argument("type", default="schedule")
|
|
|
|
def _schedule_list(dev, type):
|
|
|
|
"""Return the list of schedule actions for the given type."""
|
|
|
|
sched = dev.modules[type]
|
|
|
|
for rule in sched.rules:
|
|
|
|
print(rule)
|
|
|
|
else:
|
2023-02-18 16:31:06 +00:00
|
|
|
echo(f"No rules of type {type}")
|
2021-11-07 01:41:12 +00:00
|
|
|
|
2023-02-18 20:41:08 +00:00
|
|
|
return sched.rules
|
|
|
|
|
2021-11-07 01:41:12 +00:00
|
|
|
|
2022-11-03 01:11:24 +00:00
|
|
|
@schedule.command(name="delete")
|
|
|
|
@pass_dev
|
|
|
|
@click.option("--id", type=str, required=True)
|
|
|
|
async def delete_rule(dev, id):
|
|
|
|
"""Delete rule from device."""
|
|
|
|
schedule = dev.modules["schedule"]
|
|
|
|
rule_to_delete = next(filter(lambda rule: (rule.id == id), schedule.rules), None)
|
|
|
|
if rule_to_delete:
|
2023-02-18 16:31:06 +00:00
|
|
|
echo(f"Deleting rule id {id}")
|
2023-02-18 20:41:08 +00:00
|
|
|
return await schedule.delete_rule(rule_to_delete)
|
2022-11-03 01:11:24 +00:00
|
|
|
else:
|
2023-02-18 16:31:06 +00:00
|
|
|
echo(f"No rule with id {id} was found")
|
2022-11-03 01:11:24 +00:00
|
|
|
|
|
|
|
|
2022-10-22 22:15:47 +00:00
|
|
|
@cli.group(invoke_without_command=True)
|
|
|
|
@click.pass_context
|
|
|
|
async def presets(ctx):
|
|
|
|
"""List and modify bulb setting presets."""
|
|
|
|
if ctx.invoked_subcommand is None:
|
|
|
|
return await ctx.invoke(presets_list)
|
|
|
|
|
|
|
|
|
|
|
|
@presets.command(name="list")
|
|
|
|
@pass_dev
|
2024-02-04 15:20:08 +00:00
|
|
|
def presets_list(dev: IotBulb):
|
2022-10-22 22:15:47 +00:00
|
|
|
"""List presets."""
|
2024-02-04 15:20:08 +00:00
|
|
|
if not dev.is_bulb or not isinstance(dev, IotBulb):
|
|
|
|
echo("Presets only supported on iot bulbs")
|
2022-10-22 22:15:47 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
for preset in dev.presets:
|
2023-02-18 20:41:08 +00:00
|
|
|
echo(preset)
|
|
|
|
|
|
|
|
return dev.presets
|
2022-10-22 22:15:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
@presets.command(name="modify")
|
|
|
|
@click.argument("index", type=int)
|
|
|
|
@click.option("--brightness", type=int)
|
|
|
|
@click.option("--hue", type=int)
|
|
|
|
@click.option("--saturation", type=int)
|
|
|
|
@click.option("--temperature", type=int)
|
|
|
|
@pass_dev
|
2024-02-04 15:20:08 +00:00
|
|
|
async def presets_modify(dev: IotBulb, index, brightness, hue, saturation, temperature):
|
2022-10-22 22:15:47 +00:00
|
|
|
"""Modify a preset."""
|
|
|
|
for preset in dev.presets:
|
|
|
|
if preset.index == index:
|
|
|
|
break
|
|
|
|
else:
|
2023-02-18 16:31:06 +00:00
|
|
|
echo(f"No preset found for index {index}")
|
2022-10-22 22:15:47 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
if brightness is not None:
|
|
|
|
preset.brightness = brightness
|
|
|
|
if hue is not None:
|
|
|
|
preset.hue = hue
|
|
|
|
if saturation is not None:
|
|
|
|
preset.saturation = saturation
|
|
|
|
if temperature is not None:
|
|
|
|
preset.color_temp = temperature
|
|
|
|
|
2023-02-18 16:31:06 +00:00
|
|
|
echo(f"Going to save preset: {preset}")
|
2022-10-22 22:15:47 +00:00
|
|
|
|
2023-02-18 20:41:08 +00:00
|
|
|
return await dev.save_preset(preset)
|
2022-10-22 22:15:47 +00:00
|
|
|
|
|
|
|
|
2022-10-27 15:40:54 +00:00
|
|
|
@cli.command()
|
|
|
|
@pass_dev
|
|
|
|
@click.option("--type", type=click.Choice(["soft", "hard"], case_sensitive=False))
|
|
|
|
@click.option("--last", is_flag=True)
|
|
|
|
@click.option("--preset", type=int)
|
2024-02-04 15:20:08 +00:00
|
|
|
async def turn_on_behavior(dev: IotBulb, type, last, preset):
|
2022-10-27 15:40:54 +00:00
|
|
|
"""Modify bulb turn-on behavior."""
|
2024-02-04 15:20:08 +00:00
|
|
|
if not dev.is_bulb or not isinstance(dev, IotBulb):
|
|
|
|
echo("Presets only supported on iot bulbs")
|
|
|
|
return
|
2022-10-27 15:40:54 +00:00
|
|
|
settings = await dev.get_turn_on_behavior()
|
2023-02-18 16:31:06 +00:00
|
|
|
echo(f"Current turn on behavior: {settings}")
|
2022-10-27 15:40:54 +00:00
|
|
|
|
|
|
|
# Return if we are not setting the value
|
|
|
|
if not type and not last and not preset:
|
2023-02-18 20:41:08 +00:00
|
|
|
return settings
|
2022-10-27 15:40:54 +00:00
|
|
|
|
|
|
|
# If we are setting the value, the type has to be specified
|
|
|
|
if (last or preset) and type is None:
|
2023-02-18 16:31:06 +00:00
|
|
|
echo("To set the behavior, you need to define --type")
|
2022-10-27 15:40:54 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
behavior = getattr(settings, type)
|
|
|
|
|
|
|
|
if last:
|
2023-02-18 16:31:06 +00:00
|
|
|
echo(f"Going to set {type} to last")
|
2022-10-27 15:40:54 +00:00
|
|
|
behavior.preset = None
|
|
|
|
elif preset is not None:
|
2023-02-18 16:31:06 +00:00
|
|
|
echo(f"Going to set {type} to preset {preset}")
|
2022-10-27 15:40:54 +00:00
|
|
|
behavior.preset = preset
|
|
|
|
|
2023-02-18 20:41:08 +00:00
|
|
|
return await dev.set_turn_on_behavior(settings)
|
2022-10-27 15:40:54 +00:00
|
|
|
|
|
|
|
|
2024-01-05 01:25:15 +00:00
|
|
|
@cli.command()
|
|
|
|
@pass_dev
|
|
|
|
@click.option(
|
|
|
|
"--username", required=True, prompt=True, help="New username to set on the device"
|
|
|
|
)
|
|
|
|
@click.option(
|
|
|
|
"--password", required=True, prompt=True, help="New password to set on the device"
|
|
|
|
)
|
|
|
|
async def update_credentials(dev, username, password):
|
|
|
|
"""Update device credentials for authenticated devices."""
|
2024-02-04 15:20:08 +00:00
|
|
|
if not isinstance(dev, SmartDevice):
|
2024-01-05 01:25:15 +00:00
|
|
|
raise NotImplementedError(
|
|
|
|
"Credentials can only be updated on authenticated devices."
|
|
|
|
)
|
|
|
|
|
|
|
|
click.confirm("Do you really want to replace the existing credentials?", abort=True)
|
|
|
|
|
|
|
|
return await dev.update_credentials(username, password)
|
|
|
|
|
|
|
|
|
2024-02-06 13:48:19 +00:00
|
|
|
@cli.command()
|
|
|
|
@pass_dev
|
|
|
|
async def shell(dev: Device):
|
|
|
|
"""Open interactive shell."""
|
|
|
|
echo("Opening shell for %s" % dev)
|
|
|
|
from ptpython.repl import embed
|
|
|
|
|
|
|
|
logging.getLogger("parso").setLevel(logging.WARNING) # prompt parsing
|
|
|
|
logging.getLogger("asyncio").setLevel(logging.WARNING)
|
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
try:
|
|
|
|
await embed(
|
|
|
|
globals=globals(),
|
|
|
|
locals=locals(),
|
|
|
|
return_asyncio_coroutine=True,
|
|
|
|
patch_stdout=True,
|
|
|
|
)
|
|
|
|
except EOFError:
|
|
|
|
loop.stop()
|
|
|
|
|
|
|
|
|
2024-02-15 15:25:08 +00:00
|
|
|
@cli.command(name="feature")
|
|
|
|
@click.argument("name", required=False)
|
|
|
|
@click.argument("value", required=False)
|
2024-02-23 22:32:17 +00:00
|
|
|
@click.option("--child", required=False)
|
2024-02-15 15:25:08 +00:00
|
|
|
@pass_dev
|
2024-02-23 22:32:17 +00:00
|
|
|
async def feature(dev: Device, child: str, name: str, value):
|
2024-02-15 15:25:08 +00:00
|
|
|
"""Access and modify features.
|
|
|
|
|
|
|
|
If no *name* is given, lists available features and their values.
|
|
|
|
If only *name* is given, the value of named feature is returned.
|
|
|
|
If both *name* and *value* are set, the described setting is changed.
|
|
|
|
"""
|
2024-02-23 22:32:17 +00:00
|
|
|
if child is not None:
|
|
|
|
echo(f"Targeting child device {child}")
|
|
|
|
dev = dev.get_child_device(child)
|
2024-02-15 15:25:08 +00:00
|
|
|
if not name:
|
2024-05-02 14:31:12 +00:00
|
|
|
_echo_features(dev.features, "\n[bold]== Features ==[/bold]\n", indent="")
|
2024-02-23 22:32:17 +00:00
|
|
|
|
|
|
|
if dev.children:
|
|
|
|
for child_dev in dev.children:
|
2024-05-02 14:31:12 +00:00
|
|
|
_echo_features(
|
|
|
|
child_dev.features,
|
|
|
|
f"\n[bold]== Child {child_dev.alias} ==\n",
|
|
|
|
indent="",
|
|
|
|
)
|
2024-02-23 22:32:17 +00:00
|
|
|
|
2024-02-15 15:25:08 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
if name not in dev.features:
|
2024-02-23 22:32:17 +00:00
|
|
|
echo(f"No feature by name '{name}'")
|
2024-02-15 15:25:08 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
feat = dev.features[name]
|
|
|
|
|
|
|
|
if value is None:
|
2024-04-22 09:25:30 +00:00
|
|
|
unit = f" {feat.unit}" if feat.unit else ""
|
|
|
|
echo(f"{feat.name} ({name}): {feat.value}{unit}")
|
2024-02-15 15:25:08 +00:00
|
|
|
return feat.value
|
|
|
|
|
|
|
|
value = ast.literal_eval(value)
|
2024-05-02 14:31:12 +00:00
|
|
|
echo(f"Changing {name} from {feat.value} to {value}")
|
|
|
|
response = await dev.features[name].set_value(value)
|
|
|
|
await dev.update()
|
|
|
|
echo(f"New state: {feat.value}")
|
|
|
|
|
|
|
|
return response
|
2024-02-15 15:25:08 +00:00
|
|
|
|
|
|
|
|
2017-03-20 18:03:19 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
cli()
|