Commit Graph

314 Commits

Author SHA1 Message Date
Steven B
7fd5c213e6
Defer module updates for less volatile modules (#1052)
Addresses stability issues on older hw device versions

 - Handles module timeout errors better by querying modules individually on errors and disabling problematic modules like Firmware that go out to the internet to get updates.
- Addresses an issue with the Led module on P100 hardware version 1.0 which appears to have a memory leak and will cause the device to crash after approximately 500 calls.
- Delays updates of modules that do not have regular changes like LightPreset and LightEffect and enables them to be updated on the next update cycle only if required values have changed.
2024-07-11 16:21:59 +01:00
Teemu R
a044063526
Use first known thermostat state as main state (#1054)
Instead of trying to use the first state when multiple are reported,
iterate over the known states and pick the first matching.
This will fix an issue where the device reports extra states (like
`low_battery`) while having a known mode active.

Related to https://github.com/home-assistant/core/issues/121335
2024-07-11 15:11:50 +02:00
gimpy88
abb5d0d412
Add KP400(US) v1.0.4 fixture (#1051) 2024-07-07 14:23:24 +02:00
Steven B
4b77db31d0
Add new HS220 kasa aes fixture (#1050)
Many thanks to @pjarbit for making the device available for a fixture!
2024-07-07 14:22:43 +02:00
gimpy88
6e0bbd8720
Add KS205(US) v1.1.0 fixture (#1049) 2024-07-07 09:16:07 +01:00
Steven B
7888f4904a
Fix light preset module when list contains lighting effects (#1048)
Fixes the residual issues with the light preset module not handling
unexpected `lighting_effect` items in the presets list.

Completes the fixes started with PR
https://github.com/python-kasa/python-kasa/pull/1043 to fix
https://github.com/python-kasa/python-kasa/issues/1040, [HA
#121115](https://github.com/home-assistant/core/issues/121115) and [HA
#121119](https://github.com/home-assistant/core/issues/121119)

With this PR affected devices will no longer have the light preset
functionality disabled. As this is a new feature this does not warrant a
hotfix so will go into the next release.

Updated fixture for testing thanks to @szssamuel, many thanks!
2024-07-04 17:22:47 +02:00
Steven B
983aacbc24
Structure cli into a package (#1038)
PR with just the initial structural changes for the cli to be a package.
Subsequent PR will break out `main.py` into modules. Doing it in two
stages ensure that the commit history will be continuous for `cli.py` >
`cli/main.py`
2024-07-04 14:52:01 +02:00
gimpy88
7427a88570
Add KP400 v1.0.3 fixture (#1037) 2024-07-04 12:21:03 +01:00
Steven B
88df7f9ba6
Add KS200M(US) v1.0.11 fixture (#1047) 2024-07-04 12:02:47 +01:00
Steven B
fd4d084839
Add KS225(US) v1.1.0 fixture (#1046) 2024-07-04 11:48:18 +01:00
Steven B
905a14895d
Handle module errors more robustly and add query params to light preset and transition (#1036)
Ensures that all modules try to access their data in `_post_update_hook` in a safe manner and disable themselves if there's an error.
Also adds parameters to get_preset_rules and get_on_off_gradually_info to fix issues with recent firmware updates.
[#1033](https://github.com/python-kasa/python-kasa/issues/1033)
2024-07-04 08:02:50 +01:00
Steven B
9cffbe9e48
Support child devices in all applicable cli commands (#1020)
Adds a new decorator that adds child options to a command and gets the
child device if the options are set.

- Single definition of options and error handling
- Adds options automatically to command
- Backwards compatible with `--index` and `--name`
- `--child` allows for id and alias for ease of use
- Omitting a value for `--child` gives an interactive prompt

Implements private `_update` to allow the CLI to patch a child `update`
method to call the parent device `update`.

Example help output:
```
$ kasa brightness --help
Usage: kasa brightness [OPTIONS] [BRIGHTNESS]

  Get or set brightness.

Options:
  --transition INTEGER
  --child, --name TEXT            Child ID or alias for controlling sub-
                                  devices. If no value provided will show an
                                  interactive prompt allowing you to select a
                                  child.
  --child-index, --index INTEGER  Child index controlling sub-devices
  --help                          Show this message and exit.
```

Fixes #769
2024-07-02 15:11:19 +02:00
Steven B
b8a87f1c57
Fix credential hash to return None on empty credentials (#1029)
If discovery is triggered without credentials and discovers devices requiring authentication,
blank credentials are used to initialise the protocols and no connection is actually made.
In this instance we should not return the credentials_hash for blank credentials as it will be invalid.
2024-07-02 13:43:37 +01:00
Teemu R
e5b959e4a9
Add L920(EU) v1.1.3 fixture (#1031) 2024-07-02 13:36:57 +01:00
Steven B
8d1a4a4229
Disable multi requests on json decode error during multi-request (#1025)
Issue affecting some P100 devices
2024-07-01 13:57:13 +01:00
Teemu R
b31a2ede7f
Fix changing brightness when effect is active (#1019)
This PR changes the behavior of `brightness` module if an effect is
active.
Currently, changing the brightness disables the effect when the
brightness is changed, this fixes that.
This will also improve the `set_effect` interface to use the current
brightness when an effect is activated.

* light_strip_effect: passing `bAdjusted` with the changed properties
changes the brightness.
* light_effect: the brightness is stored only in the rule, so we modify
it when adjusting the brightness. This is also done during the initial
effect activation.

---------

Co-authored-by: Steven B <51370195+sdb9696@users.noreply.github.com>
2024-07-01 13:59:24 +02:00
Steven B
2687c71c4b
Make parent attribute on device consistent across iot and smart (#1023)
Both device types now have an internal `_parent` and a public property getter
2024-07-01 11:51:06 +01:00
Steven B
2a62849987
Update light transition module to work with child devices (#1017)
Fixes module to work with child devices, i.e. ks240
Interrogates the data to see whether maximums are available.
Fixes a bug whereby setting a duration while the feature is not
enabled does not actually enable it.
2024-06-27 18:52:54 +01:00
Teemu R
cf24a94526
Handle unknown error codes gracefully (#1016)
Makes unknown error codes to be reported through KasaException which may
be recoverable in some cases (i.e., a single command failing in the
multi request).

Related to https://github.com/home-assistant/core/issues/118446
2024-06-27 16:58:45 +02:00
Steven B
07fa0d7a7b
Fix post update hook for iot child devices (#1011)
`_post_update_hook` not being called on child `iot` devices, causing
missing emeter features for children
2024-06-25 17:58:07 +00:00
Teemu R
0f5bafaa43
Require explicit feature type (#1006)
Explicit > implicit. Having this previously would have avoided using a
wrong type for water_alert in the first place.
2024-06-25 18:30:36 +02:00
Steven B
f041f9d7e9
Fix smart led status to report rule status (#1002)
Change the reporting of the led state for smart devices to match the setter which sets the rule to always or never.
2024-06-23 07:22:29 +01:00
Teemu R
9f14854747
Cleanup cli output (#1000)
Avoid unnecessary indentation of elements, now only the child device information is indented
Use _echo_all_features consistently for both state and feature
Avoid discovery log message which brings no extra value
Hide location by default
2024-06-23 07:09:13 +01:00
Teemu R
472008e818
Drop python3.8 support (#992)
Drop support for soon-to-be eol'd python 3.8.
This will allow some minor cleanups & makes it easier to add support for
timezones.

Related to
https://github.com/python-kasa/python-kasa/issues/980#issuecomment-2170889543
2024-06-19 20:24:12 +02:00
Steven B
416d3118bf
Configure mypy to run in virtual environment and fix resulting issues (#989)
For some time I've noticed that my IDE is reporting mypy errors that the
pre-commit hook is not picking up. This is because [mypy
mirror](https://github.com/pre-commit/mirrors-mypy) runs in an isolated
pre-commit environment which does not have dependencies installed and it
enables `--ignore-missing-imports` to avoid errors.

This is [advised against by
mypy](https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-library-stubs-or-py-typed-marker)
for obvious reasons:

> We recommend avoiding --ignore-missing-imports if possible: it’s
equivalent to adding a # type: ignore to all unresolved imports in your
codebase.

This PR configures the mypy pre-commit hook to run in the virtual
environment and addresses the additional errors identified as a result.
It also introduces a minimal mypy config into the `pyproject.toml`

[mypy errors identified without the fixes in this
PR](https://github.com/user-attachments/files/15896693/mypyerrors.txt)
2024-06-19 15:07:59 +02:00
Steven B
f3fe1bc3f4
Fix to call update when only --device-family passed to cli (#987) 2024-06-19 11:01:35 +01:00
Steven B
0d84d8785e
Update docs with more howto examples (#968)
Co-authored-by: Teemu R. <tpr@iki.fi>
2024-06-19 09:53:40 +01:00
Steven B
b4a6df2b5c
Add common energy module and deprecate device emeter attributes (#976)
Consolidates logic for energy monitoring across smart and iot devices.
Deprecates emeter attributes in favour of common names.
2024-06-17 11:22:05 +01:00
Teemu R
51a972542f
Disallow non-targeted device commands (#982)
Prevent the cli from allowing sub commands unless host or alias is specified.
It is unwise to allow commands to be run on an arbitrary set of discovered
devices so this PR shows an error if attempted.
Also consolidates other invalid cli operations to use a single error function
to display the error to the user.
2024-06-17 10:04:46 +01:00
Teemu R
867b7b8830
Add time sync command (#951)
Allows setting the device time (on SMART devices) to the current time.
Fixes also setting the time which was previously broken.
2024-06-17 09:37:08 +01:00
Teemu R
5d5c353422
Add fixture for L920-5(EU) 1.0.7 (#972)
When not paired, the effect is softAP: `Light effect (light_effect):
invalid value 'softAP' not in ['Off', 'Aurora', ...]`
2024-06-11 20:22:32 +02:00
Teemu R
447d829abe
Add fixture for p300 1.0.15 (#915)
This version adds auto-off for individual strip sockets.
2024-06-10 17:00:31 +02:00
Steven B
db6276d3fd
Support smart child modules queries (#967)
Required for the P300 firmware update with `auto_off` module on child
devices. Will query child modules for parent devices that are not hubs.

Coverage will be fixed when the P300 fixture is added
https://github.com/python-kasa/python-kasa/pull/915
2024-06-10 16:47:00 +02:00
Steven B
9e74e1bd40
Do not add parent only modules to strip sockets (#963)
Excludes modules that child devices report as supported but do not make sense
on a child device like firmware, cloud, time etc.
2024-06-10 06:21:21 +01:00
Steven B
fe0bbf1b98
Do not expose child modules on parent devices (#964)
Removes the logic to expose child modules on parent devices,
which could cause complications with downstream consumers unknowingly duplicating things.
2024-06-10 05:59:37 +01:00
Steven B
b8c1b39cf0
Fix switching off light effects for iot lights strips (#961)
Fixes the newly implemented method to turn off active effects on iot devices
2024-06-07 11:29:26 +01:00
Steven B
e1e2a396b8
Add state features to iot strip sockets (#960)
Fixes iot strip sockets not creating their own state and on_since features.
2024-06-07 10:52:11 +01:00
Steven B
40e40522f9
Fix fan speed level when off and derive smart fan module from common fan interface (#957)
Picked this up while updating the [Fan platform
PR](https://github.com/home-assistant/core/pull/116605) for HA. The
smart fan module was not correctly deriving from the common interface
and the speed_level is reported as >0 when off.
2024-06-06 15:18:34 +02:00
Steven B
39fc21a124
Use freezegun for testing aes http client delays (#954) 2024-06-05 18:13:10 +01:00
Steven B
91de5e20ba
Fix P100 errors on multi-requests (#930)
Fixes an issue reported by @bdraco with the P100 not working in the
latest branch:

`[Errno None] Can not write request body for HOST_REDACTED,
ClientOSError(None, 'Can not write request body for
URL_REDACTED'))`

Issue caused by the number of multi requests going above the default
batch of 5 and the P100 not being able to handle the second multi
request happening immediately as it closes the connection after each
query (See https://github.com/python-kasa/python-kasa/pull/690 for
similar issue). This introduces a small wait time on concurrent requests
once the device has raised a ClientOSError.
2024-06-04 19:49:01 +02:00
Teemu R
40f2263770
Add some device fixtures (#948)
Adds some device fixtures by courtesy of @jimboca, thanks!
This is a slightly patched and rebased version of #441.

---------

Co-authored-by: JimBo <jimboca3@gmail.com>
Co-authored-by: sdb9696 <steven.beth@gmail.com>
2024-06-04 19:24:53 +02:00
Teemu R
f890fcedc7
Add P115 fixture (#950) 2024-06-04 19:18:23 +02:00
Teemu R
22347381bc
Do not raise on multi-request errors on child devices (#949)
This will avoid crashing when some commands return an error on
multi-requests on child devices.

Idea from
https://github.com/python-kasa/python-kasa/pull/900/files#r1624803457
2024-06-03 20:41:55 +02:00
Steven B
be5202ccb7
Make device initialisation easier by reducing required imports (#936)
Adds username and password arguments to discovery to remove the need to import Credentials.
Creates TypeAliases in Device for connection configuration classes and DeviceType.
Using the API with these changes will only require importing either Discover or Device
depending on whether using Discover.discover() or Device.connect() to 
initialize and interact with the API.
2024-06-03 19:06:54 +01:00
Teemu R
bfba7a347f
Add fixture for S505D (#947)
By courtesy of @steveredden:
https://github.com/python-kasa/python-kasa/issues/888#issuecomment-2145193072
2024-06-03 18:52:15 +02:00
Steven B
6616d68d42
Update documentation structure and start migrating to markdown (#934)
Starts structuring the documentation library usage into Tutorials, Guides, Explanations and Reference.
Continues migrating new docs from rst to markdown.
Extends the test framework discovery mocks to allow easy writing and testing of code examples.
2024-06-03 10:14:10 +01:00
Teemu R
767156421b
Initialize autooff features only when data is available (#933)
For power strips, the autooff data needs to be requested from the
children.
Until we do that, we should not create these features to avoid crashing
during switch platform initialization.

This also ports the module to use `_initialize_features` and add tests.
2024-05-24 19:39:10 +02:00
Steven B
db6e335346
Fix set_state for common light modules (#929)
PR contains a number of fixes from testing with HA devices:

- Fixes a bug with turning the light on and off via `set_state`
- Aligns `set_brightness` behaviour across `smart` and `iot` devices
such that a value of 0 is off.
- Aligns `set_brightness` behaviour for `IotDimmer` such that setting
the brightness turns on the device with a transition of 1ms. ([HA
comment](https://github.com/home-assistant/core/pull/117839#discussion_r1608720006))
- Fixes a typing issue in `LightState`.
- Adds `ColorTempRange` and `HSV` to `__init__.py`
- Adds a `state` property to the interface returning `LightState` for
validating `set_state` changes.
- Adds tests for `set_state`
2024-05-22 15:33:55 +02:00
Steven B
273c541fcc
Add light presets common module to devices. (#907)
Adds light preset common module for switching to presets and saving presets.
Deprecates the `presets` attribute and `save_preset` method from the `bulb` 
interface in favour of the modular approach.  Allows setting preset for `iot` 
which was not previously supported.
2024-05-19 11:20:18 +01:00
Steven B
1ba5c73279
Fix potential infinite loop if incomplete lists returned (#920)
Fixes the test framework to handle fixtures with incomplete lists better by checking for completeness and overriding the sum. Also adds a pytest-timeout dev dependency with timeout set to 10 seconds. Finally fixes smartprotocol to prevent an infinite loop if incomplete lists ever happens in the real world.

Co-authored-by: Teemu R. <tpr@iki.fi>
2024-05-19 10:34:52 +01:00