Address the inconsistent naming of smart modules by removing all "Module" suffixes and aligning filenames with class names. Removes the casting of modules to the correct module type now that is is redundant. Update the adding of iot modules to use the ModuleName class rather than a free string.
* Expose current and latest firmware as features
* Provide API to get information about available firmware updates (e.g., changelog, release date etc.)
* Implement updating the firmware
Passing in a string still works and returns either `IotModule` or
`SmartModule` type when called on `IotDevice` or `SmartDevice`
respectively. When calling on `Device` will return `Module` type.
Passing in a module type is then typed to that module, i.e.:
```py
smartdev.get_module(FanModule) # type is FanModule
smartdev.get_module("FanModule") # type is SmartModule
```
Only thing this doesn't do is check that you can't pass an `IotModule`
to a `SmartDevice.get_module()`. However there is a runtime check which
will return null if the passed `ModuleType` is not a subclass of
`SmartModule`.
Many thanks to @cdce8p for helping with this.
Support the `light_effect` module which allows setting the effect to Off
or Party or Relax. Uses the new `Feature.Type.Choice`. Does not
currently allow editing of effects.
This adds a test to check that all feature.set_value() calls will cause
a query, i.e., that there are no self.call()s that are not awaited, and
fixes existing code in this context.
This also fixes an issue where it was not possible to print out the
feature if the value threw an exception.
This improves the temperature control features to allow implementing
climate platform support for homeassistant.
Also adds frostprotection module, which is also used to turn
the thermostat on and off.
Brings consistency to the api across Smart and Iot so the interfaces can be used for their specialist methods as well as the device methods (e.g. turn_on/off).
The fixture now includes the queries returned directly from the child devices which is stored under child_devices along with valid device ids.
Also fixes a bug in the test_cli.py::test_wifi_scan which fails with more than 9 networks.
Enables the Fan interface for devices supporting that component.
Currently the only device with a fan is the ks240 which implements it as
a child device. This PR adds a method `get_module` to search the child
device for modules if it is a WallSwitch device type.
Pass a dummy object as context object as it will not be used by --help
anyway.
Also, allow defining --help anywhere in the argv, not just in the last
place.
This can be used to hint how the sensor value should be rounded when
displaying it to users.
The values are adapted from the values used by homeassistant.
Exposes IOT emeter information using features, bases on #843 to allow
defining the units.
---------
Co-authored-by: Steven B <51370195+sdb9696@users.noreply.github.com>
When devices have lists greater than 10 for child devices only the first
10 are returned. This retrieves the rest of the items (currently with
single requests rather than multiple requests)
In order to support the ks240 which has children for the fan and light
components, this PR adds those modules at the parent level and hides the
children so it looks like a single device to consumers. It also decides
which modules not to take from the child because the child does not
support them even though it say it does. It does this for now via a
fixed list, e.g. `Time`, `Firmware` etc.
Also adds fixtures from two versions and corresponding tests.
Moves `FeatureType` into `Feature` to make it easier to use the API.
This also enforces that no invalid types are accepted (i.e.,
`Category.Config` cannot be a `Sensor`)
If `--verbose` is used with the cli tool, some extra information is
displayed for features when in the state command.
Adds `FeatureType.Action` making it possible to expose features like
"reboot", "test alarm", "pair" etc.
The `attribute_getter` is no longer mandatory, but it will raise an
exception if not defined for other types than actions.
Trying to read returns a static string `<Action>`.
This overloads the `set_value` to call the given callable on any value.
This also fixes the `play` and `stop` coroutines of the alarm module to
await the call.
Initial implementation for feature categories to help downstreams and
our cli tool to categorize the data for more user-friendly manner. As
more and more information is being exposed through the generic features
interface, it is necessary to give some hints to downstreams about how
might want to present the information to users.
This is not a 1:1 mapping to the homeassistant's mental model, and it
will be necessary to fine-tune homeassistant-specific parameters by
other means to polish the presentation.
Devices not connected to the internet will either error when querying
firmware queries (e.g. P300) or return misleading information (e.g.
P100). This PR adds the cloud connect query to the initial queries and
bypasses the firmware module if not connected.
Add SMART.KASAHUB to the map of supported devices.
This also adds fixture files for KH100, KE100, and T310, and adapts affected modules and their tests accordingly.
---------
Co-authored-by: Steven B <51370195+sdb9696@users.noreply.github.com>
When smart devices encounter an error during a multipleRequest they
return the previous successes and the current error and stop processing
subsequent requests. This checks the responses returned and
re-queries individually for any missing responses so that individual
errors do not break other components.
Also updates CI pypy versions to be 3.9 and 3.10 which are the currently
[supported
versions](https://www.pypy.org/posts/2024/01/pypy-v7315-release.html).
Otherwise latest cryptography doesn't ship with pypy3.8 wheels and is
unable to build on windows.
Also updates the `codecov-action` to v4 which fixed some intermittent
uploading errors.
This changes `state_information` to return the names and values of
all defined features.
It was originally a "temporary" hack to show some extra, device-specific
information in the cli tool, but now that we have device-defined
features we can leverage them.
Allow controlling the color temperature via features interface:
```
$ kasa --host 192.168.xx.xx feature color_temperature
Color temperature (color_temperature): 0
$ kasa --host 192.168.xx.xx feature color_temperature 2000
Setting color_temperature to 2000
Raised error: Temperature should be between 2500 and 6500, was 2000
Run with --debug enabled to see stacktrace
$ kasa --host 192.168.xx.xx feature color_temperature 3000
Setting color_temperature to 3000
$ kasa --host 192.168.xx.xx feature color_temperature
Color temperature (color_temperature): 3000
```
This improves the initial update cycle to fetch the information as early
as possible and avoid requesting unnecessary information (like the child
component listing) in every subsequent call of `update()`.
The initial update performs the following steps:
1. `component_nego` (for components) and `get_device_info` (for common
device info) are requested as first, and their results are stored in the
internal state to allow individual modules (like colortemp) to access
the data during the initialization later on.
2. If `child_device` component is available, the child device list and
their components is requested separately to initialize the children.
3. The modules are initialized based on component lists, making the
queries available for the regular `update()`.
4. Finally, a query requesting all module-defined queries is executed,
including also those that we already did above, like the device info.
All subsequent updates will only involve queries that are defined by the
supported modules. This also means that we do not currently support
adding & removing child devices on the fly.
The internal state contains now only the responses for the most recent
update (i.e., no component information is directly available anymore,
but needs to be accessed separately if needed). If component information
is wanted from homeassistant users via diagnostics reports, the
diagnostic platform needs to be adapted to acquire this separately.
This should prevent child fixtures from hubs breaking tests due to
missing discovery info. To get these devices in `filter_fixtures`
include protocol string of `SMART.CHILD`.
This is in preparation for tests based on supporting features amongst
other tweaks:
- Consolidates the filtering logic that was split across `filter_model`
and `filter_fixture`
- Allows filtering `dev` fixture by `component`
- Consolidates fixtures missing method warnings into one warning
- Does not raise exceptions from `FakeSmartTransport` for missing
methods (required for KS240)
Previously, only v1 of on_off_gradually is supported, and the newer versions are not backwards compatible.
This PR adds support for the newer versions of the component, and implements `number` type for `Feature` to expose the transition time selection.
This also adds a new `supported_version` property to the main module API.
Adds initial support for H100 and its alarmmodule.
Also implements the following modules for T315:
* reportmodule (reporting interval)
* battery
* humidity
* temperature
* Initialize children's modules (and features) using the child component negotiation results
* Set device_type based on the device response
* Print out child features in cli 'state'
* Add --child option to cli 'command' to allow targeting child devices
* Guard "generic" features like rssi, ssid, etc. only to devices which have this information
Note, we do not currently perform queries on child modules so some data may not be available. At the moment, a stop-gap solution to use parent's data is used but this is not always correct; even if the device shares the same clock and cloud connectivity, it may have its own firmware updates.
If a handshake session becomes invalid the device returns 403 on send and an `AuthenticationError` is raised which prevents a retry, however a retry would be successful. In HA this causes devices to go into reauth flow which is not necessary.
With the move towards autodetecting available features, there is no reason to keep SmartPlug around.
kasa.smart.SmartPlug is removed in favor of kasa.smart.SmartDevice which offers the same functionality.
Information about auto_off can be accessed using Features of the AutoOffModule on supported devices.
Co-authored-by: Steven B. <51370195+sdb9696@users.noreply.github.com>
Adds auto-off implementation. The feature stays enabled after the timer runs out, and it will start the countdown if the device is turned on again without explicitly disabling it.
New features:
* Switch to select if enabled: `Auto off enabled (auto_off_enabled): False`
* Setting to change the delay: `Auto off minutes (auto_off_minutes): 222`
* If timer is active, datetime object when the device gets turned off: `Auto off at (auto_off_at): None`
The initial steps to modularize the smartdevice. Modules are initialized based on the component negotiation, and each module can indicate which features it supports and which queries should be run during the update cycle.
This adds a generic interface for all device classes to introspect available device features,
that is necessary to make it easier to support a wide variety of supported devices with different set of features.
This will allow constructing generic interfaces (e.g., in homeassistant) that fetch and change these features without hard-coding the API calls.
`Device.features()` now returns a mapping of `<identifier, Feature>` where the `Feature` contains all necessary information (like the name, the icon, a way to get and change the setting) to present and change the defined feature through its interface.
* Fix for P100 on fw 1.1.3 login_version none
* Fix coverage
* Add delay before trying default login
* Move devtools and fixture out
* Change logging string
Co-authored-by: Teemu R. <tpr@iki.fi>
* Fix test
---------
Co-authored-by: Teemu R. <tpr@iki.fi>
* Fix discovery cli to print devices not printed during discovery
* Fix tests
* Fix print exceptions not being propagated
* Fix tests
* Reduce test discover_send time
* Simplify wait logic
* Add tests
* Remove sleep loop and make auth failed a list
* Separate fake protocols for iot and smart
* Move control_child impl into its own method
* Organize schemas into correct places
* Add test_childdevice
* Add missing return for _handle_control_child
* Add ChildDevice and ChildProtocolWrapper
* Initialize & update children
* Fix circular imports
* Add dummy_protocol fixture and tests for unwrapping responseData
* Use dummy_protocol for existing smartprotocol tests
* Move _ChildProtocolWrapper to smartprotocol.py
* Use dummy_protocol for test multiple requests
* Use device_id instead of position for selecting the child
* Fix wrapping for regular requests
* Remove unused imports
* tweak
* rename child_device to childdevice
* Fix import
* Enable batching of multiple requests
* Test for debug enabled outside of loop
* tweaks
* tweaks
* tweaks
* Update kasa/smartprotocol.py
Co-authored-by: Teemu R. <tpr@iki.fi>
* revert
* Update pyproject.toml
* Add batch test and make batch_size configurable
---------
Co-authored-by: J. Nick Koston <nick@koston.org>
Co-authored-by: Teemu R. <tpr@iki.fi>
* Renew the KLAP handshake session 20 minutes before we think it will expire
Currently we assumed the clocks were perfectly aligned and the handshake
session lasted 20 hours. We now add a 20 minute buffer
* use timeout cookie when available
* Add new cli command 'command' to execute arbitrary commands
This deprecates 'raw-command', which requires positional argument for module,
in favor of new 'command' that accepts '--module' option for IOT devices.
* Pull block list to the module level
* Allow raw-command and wifi without update
* Call update always but on wifi&raw-command
* Add tests
* Skip update also if device_family was defined, as device factory performs an update
* dump_devinfo improvements
* Scrub only the last three bytes for mac addresses
* Add --target to allow creating fixtures based on discovery
* Save fixtures directly to correct location, add --basedir to allow defining the location of repository root
* Add --autosave to disable prompting for saving
* Update fixtures for devices I have
* Add fixture for HS110 hw 4.0 fw 1.0.4
* Improve help strings
* Fix tests
* Update devtools README
* Default to discovery if no host/target given
* Fix connection indeterminate state on cancellation
If the task the query is running in it cancelled, we do
know the state of the connection so we must close. Previously
we would not close on BaseException which could result
in reading the previous response if the previous query was
cancelled after the request had been sent
* add test for cancellation
* Avoid linear search for emeter realtime and emeter_today
Most of the time the data we want is at the end of the
list so we now search backwards to avoid having to
scale all the data and throw most of it away
* more tweaks
* coverage
* coverage
* preen
* coverage
* branch cover
* Pull has_emeter property up to tapodevice base class
This will also use the existence of energy_monitoring in the component_nego query to decide if the device has the service.
* Move emeter related functions to tapodevice
* Remove supported_modules override for now
This should be done in a separate PR, if we want to expose the available components to cli and downstreams
* Dedent extra reqs
* Move extra_reqs initialization
* Fix tests
* Do login entirely within AesTransport
* Remove login and handshake attributes from BaseTransport
* Add AesTransport tests
* Synchronise transport and protocol __init__ signatures and rename internal variables
* Update after review
- Show discovery results for unsupported devices and devices that fail to authenticate.
- Rename `--show-unsupported` to `--verbose`.
- Remove separate `--timeout` parameter from cli discovery so it's not confused with `--timeout` now added to cli command.
- Add tests.
This fixes changing the color for L530:
* If color temp is set on the device, it overrides any hue/sat settings. We override it to zero which seems to work.
* L530 does not allow None/null for brightness, so we avoid passing it on to the device.
* Add KP125M fixture. Enable tapo auth in pytest.
* authentication is not just for tapo
* Use "##MASKEDNAME##" base64 for nickname and ssid.
---------
Co-authored-by: Teemu R. <tpr@iki.fi>
* Add support for tapo light bulbs
* Use TapoDevice for on/off
* Add tapobulbs to discovery
* Add partial support for effects
Activating the effect does not work as I thought it would,
but this implements rest of the interface from SmartLightStrip.
* Add missing __init__ for tapo package
* Make mypy happy
* Add docstrings to make ruff happy
* Implement state_information and has_emeter
* Import tapoplug from kasa.tapo package
* Add tapo L530 fixture
* Enable tests for L530 fixture
* Make ruff happy
* Update fixture filename
* Raise exceptions on invalid parameters
* Return results in a wrapped dict
* Implement set_*
* Reorganize bulbs to iot&smart, fix tests for smarts
* Fix linting
* Fix BULBS_LIGHT_STRIP back to LIGHT_STRIPS
* Add support for TAPO/SMART KLAP and seperate transports from protocols
* Add tests and some review changes
* Update following review
* Updates following review
* Fix dump_devinfo and add discovery_result to json
* Update following review. Do not serialize aliases.
* Delete kasa/tests/fixtures/HS100(UK)_1.0_1.2.6.json
This refactors `Discover.connect_single` by moving device instance construction into a separate device factory module.
New `SmartDevice.connect(host, *, port, timeout, credentials, device_type)` class method replaces the functionality of `connect_single`,
and also now allows constructing device instances without relying on UDP discovery for type discovery if `device_type` parameter is set.
---------
Co-authored-by: Teemu R. <tpr@iki.fi>
* Add support for the new encryption protocol
This adds support for the new TP-Link discovery and encryption
protocols. It is currently incomplete - only devices without
username and password are current supported, and single device
discovery is not implemented.
Discovery should find both old and new devices. When accessing
a device by IP the --klap option can be specified on the command
line to active the new connection protocol.
sdb9696 - This commit also contains 16 later commits from Simon Wilkinson
squashed into the original
* Update klap changes 2023 to fix encryption, deal with kasa credential switching and work with new discovery changes
* Move from aiohttp to httpx
* Changes following review comments
---------
Co-authored-by: Simon Wilkinson <simon@sxw.org.uk>
Display an error if both --alias and --host are defined to avoid ambiguous target device:
```
❯ kasa --host 123 --alias 123 state
Usage: kasa [OPTIONS] COMMAND [ARGS]...
Try 'kasa --help' for help.
Error: Use either --alias or --host, not both.
```
Also, use `click.BadOptionUsage` consistently for other errors, like when only `--username` or `--password` is given.
Several KASA devices seem to have pretty strict buffer size limitations on incoming/outgoing data transfers.
Testing on KL125-US and HL103 has shown that sending a request size larger than about ~768 bytes will immediately crash the device. Additionally, a query that generates a response larger than ~4096 bytes will crash the KL125-US. I was unable to generate such a large response to test the HL103.
The KL125-US will only return such large queries when its monthly usage stats have been populated. This means that a new bulb would work fine, but after a month of data collection the bulb would break the 4K limit and start to crash.
To work around this issue, an estimated worst-case response size is calculated before sending a request by summing up all modules estimated response size. If the estimated size is greater than the device's max_response_payload_size then the query will be split into multiple queries.
This PR implements splitting queries expected to have large responses and also removes the module 'skip list' which was a previous workaround to the crash (which worked by simply reducing the number of modules queried, which prevented the overflow) since it is no longer necessary.
This PR does not attempt to address the "input buffer size limit." Thus far this limit has not been an issue.
* Add plumbing for passing credentials as far as discovery
* Pass credentials to Smart devices
* Rename authentication exception
* Fix tests failure due to test_json_output leaving echo as nop
* Fix test_credentials test
* Do not print credentials, fix echo function bug and improve get type parameter
* Add device class constructor test
* Add comment for echo handling and move assignment
asyncio.wait_for has some underlying problems that are only fixed in cpython 3.12.
Use async_timeout instead until the minimum supported version is 3.11+ and it can be replaced with asyncio.timeout
See https://github.com/python/cpython/pull/98518
* commented out modules that break
* added exclusion logic to smartdevice.py
* cleaning up a name
* removing test fixture that isn't related to this PR
* incorporating PR feedback
* fixed an if statement
* reduced exclusion list to just 'cloud'
* Tidy up the issue comment
Co-authored-by: Teemu R. <tpr@iki.fi>
* this seems to be what the linter whats
---------
Co-authored-by: Teemu R. <tpr@iki.fi>
* Drop python 3.7 support
* CI: drop 3.7 and add 3.11
* Remove skipifs that were required for <3.8
* Use pypy-3.8 for CI, re-enable pypy for windows to see if it works now
* Bump readthedocs to use py3.8
* Remove py3.7 failure comment
* Mark mocked fixtures as such
* Use consistent filenames including the swver
* Remove executable bit
* Remove duplicate KL130(US)
* Remove unnecessary mocks where we have real ones available
* Fix filenames in tests
* Make hue, saturation and color_temp optional for smartbulbpresets
* Adjust bulb preset attributes for effect mode
* Don't send None values on save_preset
* Add tests for save_preset payloads
This will make --json to output relevant contents even when no command is defined (i.e., when calling 'kasa --host <host> --json' or 'kasa --target <bcast> --json'.
* Basic fix for issue: https://github.com/python-kasa/python-kasa/issues/373
Change usage module get_daystat and get_monthat to return dictionaries of date index: time values as spec'd instead of raw usage data. Output matches emeter module get_daystat and get_monthstat
* Fixed some formatting and lint warnings to comply with black/flake8
Use the new _convert function in emeter for all conversions rather than the one in smartdevice.py
Removed unused function _emeter_convert_emeter_data from smartdevice.py
* Added a first pass test module for testing the new usage conversion function
* Changes based on PR feedback
Tidied up some doc string comments
Added a check for explicit values from conversion function
* Rebase on top of current master, fix docstrings
---------
Co-authored-by: Teemu Rytilahti <tpr@iki.fi>
* adding cli option to delete rule
* resolving black linting issue
* simplifying command name
Co-authored-by: Teemu R. <tpr@iki.fi>
* updating rule filter
Co-authored-by: Teemu R. <tpr@iki.fi>
* Fix type hinting issue with call to click.Choice which takes a Sequence not dictionary. Convert TYPE_TO_CLASS keys to a list to pass in.
* Update kasa/cli.py
Co-authored-by: Teemu R. <tpr@iki.fi>
Co-authored-by: Jules Davis <jules@focalpointvr.com>
Co-authored-by: Teemu R. <tpr@iki.fi>
* Add KP125 test fixture and support note.
Signed-off-by: James Alseth <james@jalseth.me>
* mark KP125 having an emeter
Co-authored-by: Teemu R <tpr@iki.fi>
* Avoid retrying open_connection on unrecoverable errors
- We can retry so hard that we block the event loop
Fixes
```
2022-04-16 22:18:51 WARNING (MainThread) [asyncio] Executing <Task finished name=Task-3576 coro=<open_connection() done, defined at /opt/homebrew/Cellar/python@3.9/3.9.12/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/streams.py:25> exception=ConnectionRefusedError(61, "Connect call failed (192.168.107.200, 9999)") created at /opt/homebrew/Cellar/python@3.9/3.9.12/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/tasks.py:460> took 1.001 seconds
```
* comment
* Add module support & modularize existing query
This creates a base to expose more features on the supported devices.
At the moment, the most visible change is that each update cycle gets information from all available modules:
* Basic system info
* Cloud (new)
* Countdown (new)
* Antitheft (new)
* Schedule (new)
* Time (existing, implements the time/timezone handling)
* Emeter (existing, partially separated from smartdevice)
* Fix imports
* Fix linting
* Use device host instead of alias in module repr
* Add property to list available modules, print them in cli state report
* usage: fix the get_realtime query
* separate usage from schedule to avoid multi-inheritance
* Fix module querying
* Add is_supported property to modules
* Ensure state is restored when turning back on
Fixes https://github.com/home-assistant/core/issues/69039
* Update kasa/tests/test_bulb.py
Co-authored-by: Teemu R. <tpr@iki.fi>
Co-authored-by: Teemu R. <tpr@iki.fi>
* Fix unsafe __del__ in TPLinkSmartHomeProtocol
Fixes
```
Exception ignored in: <function TPLinkSmartHomeProtocol.__del__ at 0x1096d0670>
Traceback (most recent call last):
File "/Users/bdraco/home-assistant/venv/lib/python3.9/site-packages/kasa/protocol.py", line 159, in __del__
self.writer.close()
File "/opt/homebrew/Cellar/python@3.9/3.9.6/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/streams.py", line 353, in close
return self._transport.close()
File "/opt/homebrew/Cellar/python@3.9/3.9.6/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/selector_events.py", line 700, in close
self._loop.call_soon(self._call_connection_lost, None)
File "/opt/homebrew/Cellar/python@3.9/3.9.6/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/base_events.py", line 748, in call_soon
self._check_thread()
File "/opt/homebrew/Cellar/python@3.9/3.9.6/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/base_events.py", line 785, in _check_thread
raise RuntimeError(
RuntimeError: Non-thread-safe operation invoked on an event loop other than the current one
```
* comment
* comment
* comment
* Added motion and light sensor for KS220M
* Added fixture file for ks220m
* Remove dump_devinfo and add the extra queries to devtools/dump_devinfo
* Test KS220M as a dimmer
* Add empty modules to baseproto to make the tests pass
Co-authored-by: mrbetta <bettale@gmail.com>
Co-authored-by: Teemu Rytilahti <tpr@iki.fi>