2024-07-23 18:13:52 +00:00
|
|
|
"""Main module for 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
|
2022-04-06 00:25:47 +00:00
|
|
|
import sys
|
2024-07-23 18:13:52 +00:00
|
|
|
from contextlib import asynccontextmanager
|
|
|
|
from typing import TYPE_CHECKING, Any
|
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-06-17 09:04:46 +00:00
|
|
|
|
2024-07-23 18:13:52 +00:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from kasa import Device
|
2024-06-17 09:04:46 +00:00
|
|
|
|
2024-07-23 18:13:52 +00:00
|
|
|
from kasa.deviceconfig import DeviceEncryptionType
|
2024-07-02 13:11:19 +00:00
|
|
|
|
2024-07-23 18:13:52 +00:00
|
|
|
from .common import (
|
|
|
|
SKIP_UPDATE_COMMANDS,
|
|
|
|
CatchAllExceptions,
|
|
|
|
echo,
|
|
|
|
error,
|
|
|
|
json_formatter_cb,
|
|
|
|
pass_dev_or_child,
|
|
|
|
)
|
|
|
|
from .lazygroup import LazyGroup
|
|
|
|
|
|
|
|
TYPES = [
|
|
|
|
"plug",
|
|
|
|
"switch",
|
|
|
|
"bulb",
|
|
|
|
"dimmer",
|
|
|
|
"strip",
|
|
|
|
"lightstrip",
|
|
|
|
"smart",
|
|
|
|
]
|
2023-12-29 19:17:15 +00:00
|
|
|
|
2024-06-03 18:06:54 +00:00
|
|
|
ENCRYPT_TYPES = [encrypt_type.value for encrypt_type in DeviceEncryptionType]
|
2023-12-29 19:17:15 +00:00
|
|
|
|
2024-02-20 11:21:04 +00:00
|
|
|
|
2024-07-23 18:13:52 +00:00
|
|
|
def _legacy_type_to_class(_type):
|
|
|
|
from kasa.iot import (
|
|
|
|
IotBulb,
|
|
|
|
IotDimmer,
|
|
|
|
IotLightStrip,
|
|
|
|
IotPlug,
|
|
|
|
IotStrip,
|
|
|
|
IotWallSwitch,
|
2024-07-02 13:11:19 +00:00
|
|
|
)
|
|
|
|
|
2024-07-23 18:13:52 +00:00
|
|
|
TYPE_TO_CLASS = {
|
|
|
|
"plug": IotPlug,
|
|
|
|
"switch": IotWallSwitch,
|
|
|
|
"bulb": IotBulb,
|
|
|
|
"dimmer": IotDimmer,
|
|
|
|
"strip": IotStrip,
|
|
|
|
"lightstrip": IotLightStrip,
|
|
|
|
}
|
|
|
|
return TYPE_TO_CLASS[_type]
|
2024-07-02 13:11:19 +00:00
|
|
|
|
|
|
|
|
2023-02-18 20:41:08 +00:00
|
|
|
@click.group(
|
|
|
|
invoke_without_command=True,
|
2024-07-23 18:13:52 +00:00
|
|
|
cls=CatchAllExceptions(LazyGroup),
|
|
|
|
lazy_subcommands={
|
|
|
|
"discover": None,
|
|
|
|
"device": None,
|
|
|
|
"feature": None,
|
|
|
|
"light": None,
|
|
|
|
"wifi": None,
|
|
|
|
"time": None,
|
|
|
|
"schedule": None,
|
|
|
|
"usage": None,
|
|
|
|
# device commands runnnable at top level
|
|
|
|
"state": "device",
|
|
|
|
"on": "device",
|
|
|
|
"off": "device",
|
|
|
|
"toggle": "device",
|
|
|
|
"led": "device",
|
|
|
|
"alias": "device",
|
|
|
|
"reboot": "device",
|
|
|
|
"update_credentials": "device",
|
|
|
|
"sysinfo": "device",
|
|
|
|
# light commands runnnable at top level
|
|
|
|
"presets": "light",
|
|
|
|
"brightness": "light",
|
|
|
|
"hsv": "light",
|
|
|
|
"temperature": "light",
|
|
|
|
"effect": "light",
|
|
|
|
},
|
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,
|
2024-07-23 18:13:52 +00:00
|
|
|
type=click.Choice(TYPES, case_sensitive=False),
|
|
|
|
help="The device type in order to bypass discovery. Use `smart` for newer devices",
|
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(
|
2024-07-02 13:11:19 +00:00
|
|
|
"-e",
|
2023-12-29 19:17:15 +00:00
|
|
|
"--encrypt-type",
|
|
|
|
envvar="KASA_ENCRYPT_TYPE",
|
|
|
|
default=None,
|
|
|
|
type=click.Choice(ENCRYPT_TYPES, case_sensitive=False),
|
|
|
|
)
|
|
|
|
@click.option(
|
|
|
|
"--device-family",
|
|
|
|
envvar="KASA_DEVICE_FAMILY",
|
2024-07-02 13:11:19 +00:00
|
|
|
default="SMART.TAPOPLUG",
|
2024-07-23 18:13:52 +00:00
|
|
|
help="Device family type, e.g. `SMART.KASASWITCH`. Deprecated use `--type smart`",
|
2023-12-29 19:17:15 +00:00
|
|
|
)
|
2024-01-03 21:46:08 +00:00
|
|
|
@click.option(
|
2024-07-02 13:11:19 +00:00
|
|
|
"-lv",
|
2024-01-03 21:46:08 +00:00
|
|
|
"--login-version",
|
|
|
|
envvar="KASA_LOGIN_VERSION",
|
2024-07-02 13:11:19 +00:00
|
|
|
default=2,
|
2024-01-03 21:46:08 +00:00
|
|
|
type=int,
|
2024-07-23 18:13:52 +00:00
|
|
|
help="The login version for device authentication. Defaults to 2",
|
2024-01-03 21:46:08 +00:00
|
|
|
)
|
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
|
|
|
|
|
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}")
|
2024-07-23 18:13:52 +00:00
|
|
|
|
|
|
|
from .discover import find_host_from_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:
|
2024-07-23 18:13:52 +00:00
|
|
|
from kasa.credentials import Credentials
|
|
|
|
|
2024-01-03 21:46:08 +00:00
|
|
|
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:
|
2024-06-17 09:04:46 +00:00
|
|
|
if ctx.invoked_subcommand and ctx.invoked_subcommand != "discover":
|
|
|
|
error("Only discover is available without --host or --alias")
|
|
|
|
|
2023-02-18 16:31:06 +00:00
|
|
|
echo("No host name given, trying discovery..")
|
2024-07-23 18:13:52 +00:00
|
|
|
from .discover import discover
|
|
|
|
|
2023-12-19 12:50:33 +00:00
|
|
|
return await ctx.invoke(discover)
|
2021-12-13 19:17:54 +00:00
|
|
|
|
2024-06-19 10:01:35 +00:00
|
|
|
device_updated = False
|
2024-07-23 18:13:52 +00:00
|
|
|
if type is not None and type != "smart":
|
|
|
|
from kasa.deviceconfig import DeviceConfig
|
|
|
|
|
2024-07-02 13:11:19 +00:00
|
|
|
config = DeviceConfig(host=host, port_override=port, timeout=timeout)
|
2024-07-23 18:13:52 +00:00
|
|
|
dev = _legacy_type_to_class(type)(host, config=config)
|
|
|
|
elif type == "smart" or (device_family and encrypt_type):
|
|
|
|
from kasa.device import Device
|
|
|
|
from kasa.deviceconfig import (
|
|
|
|
DeviceConfig,
|
|
|
|
DeviceConnectionParameters,
|
|
|
|
DeviceEncryptionType,
|
|
|
|
DeviceFamily,
|
|
|
|
)
|
|
|
|
|
|
|
|
if not encrypt_type:
|
|
|
|
encrypt_type = "KLAP"
|
2024-06-03 18:06:54 +00:00
|
|
|
ctype = DeviceConnectionParameters(
|
|
|
|
DeviceFamily(device_family),
|
|
|
|
DeviceEncryptionType(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)
|
2024-06-19 10:01:35 +00:00
|
|
|
device_updated = True
|
2021-12-13 19:17:54 +00:00
|
|
|
else:
|
2024-07-23 18:13:52 +00:00
|
|
|
from kasa.discover import Discover
|
|
|
|
|
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.
|
2024-06-19 10:01:35 +00:00
|
|
|
if ctx.invoked_subcommand not in SKIP_UPDATE_COMMANDS and not device_updated:
|
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:
|
2024-07-23 18:13:52 +00:00
|
|
|
from .device import state
|
2024-02-05 17:53:09 +00:00
|
|
|
|
2024-07-23 18:13:52 +00:00
|
|
|
return await ctx.invoke(state)
|
2023-02-18 20:41:08 +00:00
|
|
|
|
2017-03-20 18:03:19 +00:00
|
|
|
|
2018-08-13 18:37:43 +00:00
|
|
|
@cli.command()
|
2024-07-02 13:11:19 +00:00
|
|
|
@pass_dev_or_child
|
2024-07-23 18:13:52 +00:00
|
|
|
async def shell(dev: Device):
|
|
|
|
"""Open interactive shell."""
|
|
|
|
echo("Opening shell for %s" % dev)
|
|
|
|
from ptpython.repl import embed
|
2018-08-13 18:37:43 +00:00
|
|
|
|
2024-07-23 18:13:52 +00:00
|
|
|
logging.getLogger("parso").setLevel(logging.WARNING) # prompt parsing
|
|
|
|
logging.getLogger("asyncio").setLevel(logging.WARNING)
|
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
try:
|
|
|
|
await embed( # type: ignore[func-returns-value]
|
|
|
|
globals=globals(),
|
|
|
|
locals=locals(),
|
|
|
|
return_asyncio_coroutine=True,
|
|
|
|
patch_stdout=True,
|
|
|
|
)
|
|
|
|
except EOFError:
|
|
|
|
loop.stop()
|
2023-02-18 20:41:08 +00:00
|
|
|
|
2018-08-13 18:37:43 +00:00
|
|
|
|
2017-03-20 18:03:19 +00:00
|
|
|
@cli.command()
|
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-07-02 13:11:19 +00:00
|
|
|
async def raw_command(ctx, 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")
|
|
|
|
@click.option("--module", required=False, help="Module for IOT protocol.")
|
|
|
|
@click.argument("command")
|
|
|
|
@click.argument("parameters", default=None, required=False)
|
2024-07-02 13:11:19 +00:00
|
|
|
@pass_dev_or_child
|
|
|
|
async def cmd_command(dev: Device, module, 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-07-23 18:13:52 +00:00
|
|
|
from kasa import KasaException
|
|
|
|
from kasa.iot import IotDevice
|
|
|
|
from kasa.smart import SmartDevice
|
|
|
|
|
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
|