Commit Graph

634 Commits

Author SHA1 Message Date
Steven B.
e7f921299a
Fix smartcamera childdevice module (#1206)
Unlike most `smartcamera` queries, the child info query request and response have
different section names, i.e. `controlChild` and `child_device_list` respectively.
2024-10-29 07:11:31 +00:00
Steven B.
440b2d153b
Fix SslAesTransport default login and add tests (#1202)
Co-authored-by: Teemu R. <tpr@iki.fi>
2024-10-28 16:36:34 +00:00
Teemu R.
0287606235
Add TC65 fixture (#1200) 2024-10-28 13:47:24 +01:00
Fulch36
c051e75d1d
Add P304M(UK) test fixture (#1185)
P304M supports energy monitoring on child SMART devices.
2024-10-27 12:15:13 +00:00
Steven B.
5161115621
Update SMART test framework to use fake child protocols (#1199) 2024-10-27 12:08:02 +00:00
Steven B.
88b7951fee
Allow passing an aiohttp client session during discover try_connect_all (#1198) 2024-10-25 19:43:37 +01:00
Steven B.
7eb8d45b6e
Try default logon credentials in SslAesTransport (#1195)
Also ensure `AuthenticationErrors` are raised during handshake1.
2024-10-25 19:27:40 +01:00
Fulch36
8b95b7d557
Fallback to get_current_power if get_energy_usage does not provide current_power (#1186) 2024-10-25 19:24:43 +01:00
Steven B.
1e0ca799bc
Add stream_rtsp_url to camera module (#1197) 2024-10-25 18:30:21 +01:00
Steven B.
91e219f467
Fix device_config serialisation of https value (#1196) 2024-10-25 18:04:43 +01:00
Steven B.
e3610cf37e
Add Time module to SmartCamera devices (#1182) 2024-10-24 19:11:21 +01:00
Steven B.
28361c1727
Add core device, child and camera modules to smartcamera (#1193)
Co-authored-by: Teemu R. <tpr@iki.fi>
2024-10-24 17:22:45 +01:00
Steven B.
8ee8c17bdc
Update smartcamera to support single get/set/do requests (#1187)
Not supported by H200 hub
2024-10-24 13:11:28 +01:00
Steven B.
c839aaa1dd
Add test framework for smartcamera (#1192) 2024-10-24 09:36:18 +01:00
Steven B.
51958d8078
Allow deriving from SmartModule without being registered (#1189) 2024-10-23 21:42:01 +01:00
Steven B.
a88b677776
Combine smartcamera error codes into SmartErrorCode (#1190)
Having these in a seperate place complicates the code unnecessarily.
2024-10-23 20:07:32 +01:00
Steven B.
a0f3f016a2
Rename experimental fixtures folder to smartcamera (#1191) 2024-10-23 19:26:11 +01:00
Steven B.
cd0a74ca96
Improve supported module checks for hub children (#1188)
No devices in `fixtures/smart/child` support the `get_device_time` or
`get_device_usage` methods so this PR tests for whether the device is a
hub child and marks those modules/methods as not supported. This
prevents features being erroneously created on child devices.

It also moves the logic for getting the time from the parent module
behind getting it from the child module which was masking the creation
of these unsupported modules.
2024-10-23 17:17:27 +02:00
Steven B.
048c84d72c
Add https parameter to device class factory (#1184)
`SMART.TAPOHUB` resolves to different device classes based on the https value
2024-10-22 18:09:35 +01:00
Steven B.
3c865b5fb6
Add try_connect_all to allow initialisation without udp broadcast (#1171)
- Try all valid combinations of protocol/transport/device class and attempt to connect. 
- Add cli command `discover config` to return the connection options after connecting via `try_connect_all`.
- The cli command does not return the actual device for processing as this is not a recommended way to regularly connect to devices.
2024-10-22 14:33:46 +01:00
Steven B.
852116795c
Add discovery list command to cli (#1183)
Report discovered devices in a concise table format.
2024-10-22 12:15:08 +01:00
Steven B.
53fafc3994
Add T110(US), T310(US) and T315(US) sensor fixtures (#1179)
Many thanks to @SirWaddles for the fixtures!
2024-10-18 16:05:53 +02:00
Steven B.
d5450d89ff
Add H200 experimental fixture (#1180) 2024-10-18 14:02:08 +01:00
Teemu R.
6ba7c4ac05
Convert fixtures to use unix newlines (#1177)
Also, add a .gitattributes entry to let git handle this automatically
for json files
2024-10-18 14:00:23 +02:00
Teemu R.
8a17752ae2
Add waterleak alert timestamp (#1162)
The T300 reports the timestamp of the last alarm, this exposes it to
consumers.
2024-10-18 13:18:12 +02:00
Steven B.
acd0202cab
Update dump_devinfo for smart camera protocol (#1169)
Introduces the child camera protocol wrapper, required to get the child device info with the new protocol.
2024-10-18 12:06:22 +01:00
Teemu R.
486984fff8
Add motion sensor to known categories (#1176)
Also, improve device type warning on unknown devices
2024-10-18 12:31:52 +02:00
Steven B.
2dd621675a
Drop urllib3 dependency and create ssl context in executor thread (#1175) 2024-10-18 11:40:17 +02:00
Steven B.
c6f2d89d44
Expose smart child device map as a class constant (#1173)
To facilitate distinguishing between smart and smart camera child devices.
2024-10-18 09:55:07 +01:00
Steven B.
dcc36e1dfe
Initial TapoCamera support (#1165)
Adds experimental support for the Tapo Camera protocol also used by the H200 hub.
Creates a new SslAesTransport and a derived SmartCamera and SmartCameraProtocol.
2024-10-16 16:53:52 +01:00
Steven B.
380fbb93c3
Enable newer encrypted discovery protocol (#1168) 2024-10-16 15:28:27 +01:00
Steven B.
7fd8c14c1f
Create common Time module and add time set cli command (#1157) 2024-10-15 08:59:25 +01:00
Steven B.
bd5a24b0ed
Use tzinfo in time constructor instead of astime for iot devices (#1158)
Fixes using `astime` on a non tzinfo aware object which causes issues with daylight saving.
2024-10-08 12:33:19 +01:00
Steven B.
7c1686d3ae
Cache zoneinfo for smart devices (#1156) 2024-10-08 12:21:01 +01:00
Steven B.
9641edcbc0
Make iot time timezone aware (#1147)
Also makes on_since for iot devices use device time.
Changes the return value for device.timezone to be tzinfo instead of a dict.
2024-10-08 08:16:51 +01:00
Steven B.
8bb2cca7cf
Remove async magic patch from tests (#1146)
Not required since AsyncMock available in python 3.8 and probably better
to keep magic to a minimum.
2024-10-02 17:12:10 +02:00
Steven B.
1026e890a1
Correctly define SmartModule.call as an async function (#1148) 2024-10-02 16:00:27 +01:00
Steven B.
1fcf3e44c2
Stabilise on_since value for smart devices (#1144)
Caches the `on_since` value to prevent jitter caused by the device calculations.
2024-10-02 15:04:16 +01:00
Steven B.
81e2685605
Send empty dictionary instead of null for iot queries (#1145) 2024-10-01 12:47:36 +01:00
Teemu R.
d897503b58
Move feature initialization from __init__ to _initialize_features (#1140) 2024-09-28 20:14:31 +02:00
Teemu R.
1ce5af2494
Add factory_reset() to iotdevice (#1125)
Also extend the base device class API to make factory_reset() part of the common API.
2024-09-27 17:42:22 +01:00
Steven B.
d1b43f5408
Fix cli command for device off (#1121)
Was previously missed when using the full `kasa device off` command as
opposed to the shortcut.
2024-09-27 17:36:45 +01:00
Steven B.
b4aba36b73
Use pytest-socket to ensure no tests are performing io (#1133) 2024-09-27 17:20:25 +01:00
Steven B.
936e45cad7
Enable ruff lint pycodestyle warnings (#1132)
Addresses repeated SyntaxWarnings when running linters:
```
kasa/tests/test_bulb.py:254: SyntaxWarning: invalid escape sequence '\d'
  ValueError, match="Temperature should be between \d+ and \d+, was 1000"
kasa/tests/test_bulb.py:258: SyntaxWarning: invalid escape sequence '\d'
  ValueError, match="Temperature should be between \d+ and \d+, was 10000"
kasa/tests/test_common_modules.py:216: SyntaxWarning: invalid escape sequence '\d'
  with pytest.raises(ValueError, match="Temperature should be between \d+ and \d+"):
kasa/tests/test_common_modules.py:219: SyntaxWarning: invalid escape sequence '\d'
  with pytest.raises(ValueError, match="Temperature should be between \d+ and \d+"):
```
2024-09-27 17:36:41 +02:00
Steven B.
db686e191a
Add autouse fixture to patch asyncio.sleep (#1131) 2024-09-27 10:57:23 +01:00
Steven B.
038b6993ca
Speed up and simplify github workflows (#1128)
- Enable parallel tests in the CI with pytest-xdist
- Migrate to the official `astral-sh/setup-uv` github action
- Call `pre-commit` run as a single job in CI instead of relisting each
check
- Use `uv` version 0.4.16
- Fix bug with pre-commit cache
- Update `publish.yml` to use  `astral-sh/setup-uv`
2024-09-27 10:27:53 +01:00
Teemu R.
1ab08f454f
Add fixture for T110 fw 1.9.0 (#1129) 2024-09-27 09:35:17 +01:00
Teemu R.
8321fd08aa
Mock asyncio.sleep for klapprotocol tests (#1130)
Speeds up tests in `test_klapprotocol.py` from 26s to 2s when there's no
sleep between the retries.
2024-09-27 09:34:30 +01:00
Ethan G
69c270055d
Add KS200M(US) fw 1.0.12 fixture (#1127) 2024-09-22 22:29:42 +02:00
Teemu R.
89d611d2cd
Add fixture for KL135(US) fw 1.0.15 (#1122)
By courtesy of @jhemak:
https://github.com/home-assistant/core/issues/126300#issuecomment-2364640319
2024-09-21 20:18:55 +02:00
Teemu R.
73b6d16074
Extend KL135 ct range up to 9000K (#1123) 2024-09-21 16:29:25 +01:00
Teemu R.
b7fa0d2040
Add factory-reset command to cli (#1108)
Allow reseting devices to factory settings using the cli: `kasa device factory-reset`.
2024-09-21 16:52:52 +02:00
Teemu R.
f07341a5a6
Add reboot() to the device interface (#1124)
Both device families have already had a method following this signature,
but defining the interface in the base class will make the contract
clear.
2024-09-21 16:37:38 +02:00
Steven B.
fcf8f07232
Do not regenerate aes key pair (#1114)
And read it from `device_config` if provided.

This is required as key generation can eat up cpu when a device is not fully available and the library is retrying.
2024-09-10 17:24:38 +01:00
Teemu R.
2a89e58ae0
Add missing type hints to alarm module (#1111) 2024-09-10 17:20:00 +01:00
Steven B.
1773f98aad
Fix tests due to yarl URL str output change (#1112)
Latest versions of yarl>=1.9.5 omit the port 80 when calling str(url) which broke tests.
2024-09-06 15:27:23 +01:00
Teemu R.
b0d0c4b703
Add KH100 EU fixtures (#1109) 2024-09-06 14:46:44 +02:00
Steven B.
520b9d7a38
Disable automatic updating of latest firmware (#1103)
To resolve issues with the calls to the tplink cloud to get the latest firmware.
Disables the automatic calling of `get_latest_fw` and requires firmware update checks to be triggered manually.
2024-08-30 18:01:54 +01:00
Teemu R.
6a86ffbbba
Add flake8-pytest-style (PT) for ruff (#1105)
This will catch common issues with pytest code.

* Use `match` when using `pytest.raises()` for base exception types like
`TypeError` or `ValueError`
* Use tuples for `parametrize()`
* Enforces `pytest.raises()` to contain simple statements, using `noqa`
to skip this on some cases for now.
* Fixes incorrect exception type (valueerror instead of typeerror) for
iotdimmer.
* Adds check valid types for `iotbulb.set_hsv` and `color` smart module.
* Consolidate exception messages for common interface modules.
2024-08-30 17:30:07 +02:00
Teemu R.
3e43781bb2
Add flake8-logging (LOG) and flake8-logging-format (G) for ruff (#1104)
Enables rules LOG (flake8-logging) and G (flake8-logging-format) for
ruff. This will catch eager log message formatting, among other similar
issues.
2024-08-30 16:13:14 +02:00
J. Nick Koston
b6339be9ec
Fix logging in iotdevice when a module is module not supported (#1100)
Debug logger was generating the `repr()` of each module and throwing it away because it had a `%` instead of a `,`
2024-08-23 10:56:33 +01:00
Steven B.
ae1ee388f6
Remove top level await xdoctest fixture (#1098)
This is now natively supported since [xdoctest
#158](https://github.com/Erotemic/xdoctest/pull/158) has been released
so no need for the monkey patching fixture anymore.
2024-08-22 17:14:47 +02:00
J. Nick Koston
4669e08605
Improve performance of dict merge code (#1097)
Co-authored-by: Teemu R. <tpr@iki.fi>
2024-08-14 16:33:54 -05:00
Steven B.
31ec27c1c8
Fix iot light effect brightness (#1092)
Fixes issue where the brightness of the `iot` light effect is set properly
on the light effect but read back incorrectly from the light.
2024-07-31 15:58:48 +01:00
Steven B.
cb0077f634
Do not send light_on value to iot bulb set_state (#1090)
Passing this extra value caused the `ignore_default` check in the `IotBulb._set_light_state`
method to fail which causes the device to come back on to the default state.
2024-07-31 15:56:07 +01:00
Steven B.
cb7e904d30
Enable setting brightness with color temp for smart devices (#1091) 2024-07-31 15:52:27 +01:00
Steven B.
7bba9926ed
Allow erroring modules to recover (#1080)
Re-query failed modules after some delay instead of immediately disabling them.
Changes to features so they can still be created when modules are erroring.
2024-07-30 19:23:07 +01:00
Steven B.
7416e855f1
Fix mypy pre-commit hook on windows (#1081) 2024-07-25 09:11:48 +01:00
Steven B.
1c83675e57
Fix intermittently failing decryption error test (#1082) 2024-07-24 18:58:37 +01:00
Teemu R.
055bbcc0c9
Add support for T100 motion sensor (#1079)
Add support for T100 motion sensor.
Thanks to @DarthSonic for the fixture file!
2024-07-24 15:48:33 +02:00
Teemu R.
dc0aedad20
Expose reboot action (#1073)
Expose reboot through the feature interface.
This can be useful in situations where one wants to reboot the device,
e.g., in recent cases where frequent update calls will render the device
unresponsive after a specific amount of time.
2024-07-24 15:47:38 +02:00
Steven B.
ed033679e5
Split out main cli module into lazily loaded submodules (#1039) 2024-07-23 19:13:52 +01:00
Steven B.
58afeb28a1
Update smart request parameter handling (#1061)
Changes to the smart request handling:
- Do not send params if null
- Drop the requestId parameter
- get_preset_rules doesn't send parameters for preset component version less than 3
- get_led_info no longer sends the wrong parameters
- get_on_off_gradually_info no longer sends an empty {} parameter
2024-07-23 19:02:20 +01:00
Steven B.
06ff598d9c
Raise KasaException on decryption errors (#1078)
Currently if the library encounters an invalid decryption error it
raises a value error. This PR wraps it in a KasaException so consumers
such as HA can catch an expected library exception.
2024-07-22 20:33:31 +02:00
Steven B.
c4f015a2fb
Redact sensitive info from debug logs (#1069)
Redacts sensitive data when debug logging device responses such as mac,
location and usernames
2024-07-17 19:57:09 +02:00
Steven B.
e17ca21a83
Only refresh smart LightEffect module daily (#1064)
Fixes an issue with L530 bulbs on HW version 1.0 whereby the light effect
query causes the device to crash with JSON_ENCODE_FAIL_ERROR after
approximately 60 calls.
2024-07-17 08:28:11 +01:00
Steven B.
b220beb811
Use monotonic time for query timing (#1070)
To fix intermittent issues with [windows
CI](https://github.com/python-kasa/python-kasa/actions/runs/9952477932/job/27493918272?pr=1068).
Probably better to use monotonic here anyway.

```
FAILED kasa/tests/test_smartdevice.py::test_update_module_update_delays[L530E(EU)_3.0_1.1.6.json-SMART] - ValueError: Clock moved backwards. Refusing to generate ID.
```
2024-07-16 14:25:32 +02:00
daleye
a2b7daa069
Add fixture file for KP405 fw 1.0.6 (#1063) 2024-07-14 16:31:31 +02:00
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
03f72b8be0
Disable multi-request on unknown errors (#1027)
Another P100 fix
2024-07-01 14:33:28 +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
J. Nick Koston
368590cd36
Cache SmartErrorCode creation (#1022)
Uses the python 3.9 cache feature to improve performance of error code creation
2024-06-30 10:49:59 +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
f7557daa32
Disable lighttransition module on child devices (#1013)
Module is not working properly for updates on KS240 so temporarily
disable until fixed
2024-06-25 20:52:25 +02:00
Teemu R
b80e3c916a
Remove frost_protection feature (#1009)
This provides the same functionality as the state in
`TemperatureControl`, so we should not expose this separately.
2024-06-25 20:00:39 +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
5846bbdbbb
Fix iot strip so the children do not have led and cloud modules (#1010)
Also adds led and cloud connection on the strip parent
2024-06-25 17:24:05 +01:00
Teemu R
1b619effe5
Demote device_time back to debug (#1001)
Reverts unintentional change of feature category for device_time.
2024-06-23 07:39:34 +01: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
cee8b0fadc
Improve autooff name and unit (#997) 2024-06-21 20:25:55 +02:00
Steven B
e083449049
Update mode, time, rssi and report_interval feature names/units (#995) 2024-06-21 18:42:43 +02:00
Teemu R
ac1e81dc17
Add unit_getter for feature (#993)
Allow defining getter for unit, necessary to set the correct unit based
on device responses.
2024-06-21 14:51:56 +02: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
Steven B
6cdbbefb90
Add timezone to on_since attributes (#978)
This allows them to displayed in HA without errors.
2024-06-14 23:04:20 +02:00
Steven B
4cf395483f
Add type hints to feature set_value (#974)
To prevent untyped call mypy errors in consumers
2024-06-12 21:58:21 +02:00
Steven B
7f24408c32
Handle unknown light effect names and only calculate effect list once (#973)
Fixes issue with unpaired devices reporting light effect as `softAP`
reported in https://github.com/python-kasa/python-kasa/pull/972. I don't
think we need to handle that effect properly so just reports as off.
2024-06-11 20:23:06 +02: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
Steven B
f0be672cf5
Add supported check to light transition module (#971)
Adds an implementation of `_check_supported` to the light transition module so it is
not added to a parent device that reports it but doesn't support it, i.e. ks240.
2024-06-11 15:46:36 +01: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
927fe648ac
Better checking of child modules not supported by parent device (#966)
Replaces the logic to skip adding child modules to parent devices based
on whether a device is a wall switch and instead relies on the
`_check_supported` method. Is more future proof and will fix issue with
the P300 with child `auto_off` modules
https://github.com/python-kasa/python-kasa/pull/915 not supported on the
parent.
2024-06-10 15:13:46 +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
Teemu R
9b66ac8765
Require update in cli for wifi commands (#956)
Executing wifi join requires passing the current time information for smart devices which we read from the device.
This PR removes wifi from the block list to make it work.
2024-06-07 12:47:51 +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
5befe51c42
Ensure http delay logic works during default login attempt (#959)
Ensures retryable exceptions are raised on failure to login with default login credentials.
2024-06-06 17:01:58 +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
Teemu R
b217811096
Do not show a zero error code when cli exits from showing help (#935)
asyncclick raises a custom runtime exception when exiting help. This suppresses reporting it.
2024-05-23 19:35:41 +01:00
Teemu R
23c5ee089a
Add state feature for iot devices (#924)
This is allows a generic implementation for the switch platform in the
homeassistant integration.

Also elevates set_state(bool) to be part of the standard API.
2024-05-22 16:52:00 +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
Steven B
9989d0f6ec
Add post update hook to module and use in smart LightEffect (#921)
Adds a post update hook to modules so they can calculate values and collections once rather than on each property access
2024-05-19 10:18:17 +01:00
Steven B
3490a1ef84
Add tutorial doctest module and enable top level await (#919)
Add a tutorial module with examples that can be tested with `doctest`.

In order to simplify the examples they can be run with doctest allowing
top level await statements by adding a fixture to patch the builtins
that xdoctest uses to test code.

---------

Co-authored-by: Teemu R. <tpr@iki.fi>
2024-05-16 18:13:44 +02:00
Steven B
a2e8d2c4e8
Deprecate device level light, effect and led attributes (#916)
Deprecates the attributes at device level for light, light effects, and led. i.e. device.led, device.is_color. Will continue to support consumers using these attributes and emit a warning.
2024-05-15 18:49:08 +01:00
Steven B
133a839f22
Add LightEffect module for smart light strips (#918)
Implements the `light_strip_lighting_effect` components for
`smart` devices. Uses a new list of effects captured from a L900 which
are similar to the `iot` effects but include some additional properties
and a few extra effects.

Assumes that a device only implements `light_strip_lighting_effect` or
`light_effect` but not both.
2024-05-15 06:16:57 +01:00