mirror of
https://github.com/python-kasa/python-kasa.git
synced 2025-04-25 08:06:25 +00:00
Compare commits
69 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
579fd5aa2a | ||
![]() |
8501390c61 | ||
![]() |
f0abc2800d | ||
![]() |
f488492c7d | ||
![]() |
29195fa639 | ||
![]() |
8b138698b8 | ||
![]() |
ad8a0eebec | ||
![]() |
668e32d3a5 | ||
![]() |
d5187dc6f1 | ||
![]() |
cbab40a59e | ||
![]() |
bff5409d22 | ||
![]() |
8259d28b12 | ||
![]() |
44c561b04d | ||
![]() |
ebd370da74 | ||
![]() |
82fbe1226e | ||
![]() |
09e73faca3 | ||
![]() |
781d07f6a2 | ||
![]() |
1df05af208 | ||
![]() |
656c88771a | ||
![]() |
d857cc68bb | ||
![]() |
62c1dd87dc | ||
![]() |
ba6d6560f4 | ||
![]() |
7f2a1be392 | ||
![]() |
0aa1242a00 | ||
![]() |
5b9b89769a | ||
![]() |
9b7bf367ae | ||
![]() |
09fce3f426 | ||
![]() |
b701441215 | ||
![]() |
b6a584971a | ||
![]() |
988eb96bd1 | ||
![]() |
5e57f8bd6c | ||
![]() |
bd43e0f7d2 | ||
![]() |
57c4ffa8a3 | ||
![]() |
54bb53899e | ||
![]() |
acc0e9a80a | ||
![]() |
307173487a | ||
![]() |
7b1b14d1e6 | ||
![]() |
fa0f7157c6 | ||
![]() |
a03a4b1d63 | ||
![]() |
05085462d3 | ||
![]() |
bca5576425 | ||
![]() |
2d26f91981 | ||
![]() |
fd6067e5a0 | ||
![]() |
980f6a38ca | ||
![]() |
773801cad5 | ||
![]() |
d27697c50f | ||
![]() |
b23019e748 | ||
![]() |
17356c10f1 | ||
![]() |
bc97c0794a | ||
![]() |
0f185f1905 | ||
![]() |
2ab42f59b3 | ||
![]() |
1355e85f8e | ||
![]() |
4e7e18cef1 | ||
![]() |
2542516009 | ||
![]() |
3c98efb015 | ||
![]() |
68f50aa763 | ||
![]() |
d03f535568 | ||
![]() |
1be87674bf | ||
![]() |
be34dbd387 | ||
![]() |
57f6c4138a | ||
![]() |
589d15091a | ||
![]() |
a211cc0af5 | ||
![]() |
333a36bf42 | ||
![]() |
6420d76351 | ||
![]() |
660b9f81de | ||
![]() |
2e3b1bc376 | ||
![]() |
debcff9f9b | ||
![]() |
3c038fc13b | ||
![]() |
7b3dde9aa0 |
15
.github/workflows/ci.yml
vendored
15
.github/workflows/ci.yml
vendored
@ -2,11 +2,22 @@ name: CI
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: ["master", "patch"]
|
||||
branches:
|
||||
- master
|
||||
- patch
|
||||
pull_request:
|
||||
branches: ["master", "patch"]
|
||||
branches:
|
||||
- master
|
||||
- patch
|
||||
- 'feat/**'
|
||||
- 'fix/**'
|
||||
- 'janitor/**'
|
||||
workflow_dispatch: # to allow manual re-runs
|
||||
|
||||
concurrency:
|
||||
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
|
||||
cancel-in-progress: true
|
||||
|
||||
env:
|
||||
UV_VERSION: 0.4.16
|
||||
|
||||
|
15
.github/workflows/codeql-analysis.yml
vendored
15
.github/workflows/codeql-analysis.yml
vendored
@ -2,12 +2,23 @@ name: "CodeQL checks"
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ "master", "patch" ]
|
||||
branches:
|
||||
- master
|
||||
- patch
|
||||
pull_request:
|
||||
branches: [ master, "patch" ]
|
||||
branches:
|
||||
- master
|
||||
- patch
|
||||
- 'feat/**'
|
||||
- 'fix/**'
|
||||
- 'janitor/**'
|
||||
schedule:
|
||||
- cron: '44 17 * * 3'
|
||||
|
||||
concurrency:
|
||||
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
|
||||
cancel-in-progress: true
|
||||
|
||||
jobs:
|
||||
analyze:
|
||||
name: Analyze
|
||||
|
@ -2,13 +2,13 @@ repos:
|
||||
|
||||
- repo: https://github.com/astral-sh/uv-pre-commit
|
||||
# uv version.
|
||||
rev: 0.4.16
|
||||
rev: 0.5.30
|
||||
hooks:
|
||||
# Update the uv lockfile
|
||||
- id: uv-lock
|
||||
|
||||
- repo: https://github.com/pre-commit/pre-commit-hooks
|
||||
rev: v4.6.0
|
||||
rev: v5.0.0
|
||||
hooks:
|
||||
- id: trailing-whitespace
|
||||
- id: end-of-file-fixer
|
||||
@ -16,16 +16,20 @@ repos:
|
||||
- id: check-yaml
|
||||
- id: debug-statements
|
||||
- id: check-ast
|
||||
- id: pretty-format-json
|
||||
args:
|
||||
- "--autofix"
|
||||
- "--indent=4"
|
||||
|
||||
- repo: https://github.com/astral-sh/ruff-pre-commit
|
||||
rev: v0.7.4
|
||||
rev: v0.9.6
|
||||
hooks:
|
||||
- id: ruff
|
||||
args: [--fix, --exit-non-zero-on-fix]
|
||||
- id: ruff-format
|
||||
|
||||
- repo: https://github.com/PyCQA/doc8
|
||||
rev: 'v1.1.1'
|
||||
rev: 'v1.1.2'
|
||||
hooks:
|
||||
- id: doc8
|
||||
additional_dependencies: [tomli]
|
||||
|
@ -2,6 +2,10 @@ version: 2
|
||||
|
||||
formats: all
|
||||
|
||||
sphinx:
|
||||
configuration: docs/source/conf.py
|
||||
|
||||
|
||||
build:
|
||||
os: ubuntu-22.04
|
||||
tools:
|
||||
|
142
CHANGELOG.md
142
CHANGELOG.md
@ -1,5 +1,143 @@
|
||||
# Changelog
|
||||
|
||||
## [0.10.2](https://github.com/python-kasa/python-kasa/tree/0.10.2) (2025-02-12)
|
||||
|
||||
[Full Changelog](https://github.com/python-kasa/python-kasa/compare/0.10.1...0.10.2)
|
||||
|
||||
**Release summary:**
|
||||
|
||||
- Bugfix for [#1499](https://github.com/python-kasa/python-kasa/issues/1499).
|
||||
- Support for L530B and C110 devices.
|
||||
|
||||
**Fixed bugs:**
|
||||
|
||||
- H100 - Raised error: not enough values to unpack \(expected 2, got 1\) [\#1499](https://github.com/python-kasa/python-kasa/issues/1499)
|
||||
- Do not crash on missing build number in fw version [\#1500](https://github.com/python-kasa/python-kasa/pull/1500) (@rytilahti)
|
||||
|
||||
**Added support for devices:**
|
||||
|
||||
- Add C110\(EU\) 2.0 1.4.3 fixture [\#1503](https://github.com/python-kasa/python-kasa/pull/1503) (@sdb9696)
|
||||
- Add L530B\(EU\) 3.0 1.1.9 fixture [\#1502](https://github.com/python-kasa/python-kasa/pull/1502) (@sdb9696)
|
||||
|
||||
**Project maintenance:**
|
||||
|
||||
- Add fixtures for new versions of H100, P110, and T100 devices [\#1501](https://github.com/python-kasa/python-kasa/pull/1501) (@LXGaming)
|
||||
- Add L530E\(TW\) 2.0 1.1.1 fixture [\#1497](https://github.com/python-kasa/python-kasa/pull/1497) (@bluehomewu)
|
||||
|
||||
## [0.10.1](https://github.com/python-kasa/python-kasa/tree/0.10.1) (2025-02-02)
|
||||
|
||||
[Full Changelog](https://github.com/python-kasa/python-kasa/compare/0.10.0...0.10.1)
|
||||
|
||||
**Release summary:**
|
||||
|
||||
Small patch release for bugfixes
|
||||
|
||||
**Implemented enhancements:**
|
||||
|
||||
- dustbin\_mode: add 'off' mode for cleaner downstream impl [\#1488](https://github.com/python-kasa/python-kasa/pull/1488) (@rytilahti)
|
||||
- Add Dimmer Configuration Support [\#1484](https://github.com/python-kasa/python-kasa/pull/1484) (@ryenitcher)
|
||||
|
||||
**Fixed bugs:**
|
||||
|
||||
- Do not return empty string for custom light effect name [\#1491](https://github.com/python-kasa/python-kasa/pull/1491) (@sdb9696)
|
||||
- Add FeatureAttributes to smartcam Alarm [\#1489](https://github.com/python-kasa/python-kasa/pull/1489) (@sdb9696)
|
||||
|
||||
**Project maintenance:**
|
||||
|
||||
- Add module.device to the public api [\#1478](https://github.com/python-kasa/python-kasa/pull/1478) (@sdb9696)
|
||||
|
||||
## [0.10.0](https://github.com/python-kasa/python-kasa/tree/0.10.0) (2025-01-26)
|
||||
|
||||
[Full Changelog](https://github.com/python-kasa/python-kasa/compare/0.9.1...0.10.0)
|
||||
|
||||
**Release summary:**
|
||||
|
||||
This release brings support for many new devices, including completely new device types:
|
||||
|
||||
- Support for Tapo robot vacuums. Special thanks to @steveredden, @MAXIGAMESSUPPER, and veep60 for helping to get this implemented!
|
||||
- Support for hub attached cameras and doorbells (H200)
|
||||
- Improved support for hubs (including pairing & better chime controls)
|
||||
- Support for many new camera and doorbell device models, including C220, C720, D100C, D130, and D230
|
||||
|
||||
Many thanks to testers and new contributors - @steveredden, @DawidPietrykowski, @Obbay2, @andrewome, @ryenitcher and @etmmvdp!
|
||||
|
||||
**Breaking changes:**
|
||||
|
||||
- `uses_http` is now a readonly property of device config. Consumers that relied on `uses_http` to be persisted with `DeviceConfig.to_dict()` will need to store the value separately.
|
||||
- `is_color`, `is_dimmable`, `is_variable_color_temp`, `valid_temperate_range`, and `has_effects` attributes from the `Light` module are deprecated, consumers should use `has_feature("hsv")`, `has_feature("brightness")`, `has_feature("color_temp")`, `get_feature("color_temp").range`, and `Module.LightEffect in dev.modules` respectively. Calling the deprecated attributes will emit a `DeprecationWarning` and type checkers will fail them.
|
||||
- `alarm_volume` on the `smart.Alarm` module is changed from `str` to `int`
|
||||
|
||||
**Breaking changes:**
|
||||
|
||||
- Make uses\_http a readonly property of device config [\#1449](https://github.com/python-kasa/python-kasa/pull/1449) (@sdb9696)
|
||||
- Allow passing alarm parameter overrides [\#1340](https://github.com/python-kasa/python-kasa/pull/1340) (@rytilahti)
|
||||
- Deprecate legacy light module is\_capability checks [\#1297](https://github.com/python-kasa/python-kasa/pull/1297) (@sdb9696)
|
||||
|
||||
**Implemented enhancements:**
|
||||
|
||||
- Expose more battery sensors for D230 [\#1451](https://github.com/python-kasa/python-kasa/issues/1451)
|
||||
- dumping HTTP POST Body for Tapo Vacuum \(RV30 Plus\) [\#937](https://github.com/python-kasa/python-kasa/issues/937)
|
||||
- Add smartcam pet detection toggle module [\#1465](https://github.com/python-kasa/python-kasa/pull/1465) (@DawidPietrykowski)
|
||||
- Add childlock module for vacuums [\#1461](https://github.com/python-kasa/python-kasa/pull/1461) (@rytilahti)
|
||||
- Add ultra mode \(fanspeed = 5\) for vacuums [\#1459](https://github.com/python-kasa/python-kasa/pull/1459) (@rytilahti)
|
||||
- Add setting to change carpet clean mode [\#1458](https://github.com/python-kasa/python-kasa/pull/1458) (@rytilahti)
|
||||
- Add setting to change clean count [\#1457](https://github.com/python-kasa/python-kasa/pull/1457) (@rytilahti)
|
||||
- Add mop module [\#1456](https://github.com/python-kasa/python-kasa/pull/1456) (@rytilahti)
|
||||
- Enable dynamic hub child creation and deletion on update [\#1454](https://github.com/python-kasa/python-kasa/pull/1454) (@sdb9696)
|
||||
- Expose current cleaning information [\#1453](https://github.com/python-kasa/python-kasa/pull/1453) (@rytilahti)
|
||||
- Add battery module to smartcam devices [\#1452](https://github.com/python-kasa/python-kasa/pull/1452) (@sdb9696)
|
||||
- Allow update of camera modules after setting values [\#1450](https://github.com/python-kasa/python-kasa/pull/1450) (@sdb9696)
|
||||
- Update hub children on first update and delay subsequent updates [\#1438](https://github.com/python-kasa/python-kasa/pull/1438) (@sdb9696)
|
||||
- Implement vacuum dustbin module \(dust\_bucket\) [\#1423](https://github.com/python-kasa/python-kasa/pull/1423) (@rytilahti)
|
||||
- Add smartcam child device support for smartcam hubs [\#1413](https://github.com/python-kasa/python-kasa/pull/1413) (@sdb9696)
|
||||
- Add vacuum speaker controls [\#1332](https://github.com/python-kasa/python-kasa/pull/1332) (@rytilahti)
|
||||
- Add consumables module for vacuums [\#1327](https://github.com/python-kasa/python-kasa/pull/1327) (@rytilahti)
|
||||
- Add ADC Value to PIR Enabled Switches [\#1263](https://github.com/python-kasa/python-kasa/pull/1263) (@ryenitcher)
|
||||
- Add support for cleaning records [\#945](https://github.com/python-kasa/python-kasa/pull/945) (@rytilahti)
|
||||
- Initial support for vacuums \(clean module\) [\#944](https://github.com/python-kasa/python-kasa/pull/944) (@rytilahti)
|
||||
- Add support for pairing devices with hubs [\#859](https://github.com/python-kasa/python-kasa/pull/859) (@rytilahti)
|
||||
- Add common alarm interface [\#1479](https://github.com/python-kasa/python-kasa/pull/1479) (@sdb9696)
|
||||
- Add common childsetup interface [\#1470](https://github.com/python-kasa/python-kasa/pull/1470) (@sdb9696)
|
||||
- Add childsetup module to smartcam hubs [\#1469](https://github.com/python-kasa/python-kasa/pull/1469) (@sdb9696)
|
||||
- Only log one warning per unknown clean error code and status [\#1462](https://github.com/python-kasa/python-kasa/pull/1462) (@rytilahti)
|
||||
- Add support for doorbells and chimes [\#1435](https://github.com/python-kasa/python-kasa/pull/1435) (@steveredden)
|
||||
- Allow https for klaptransport [\#1415](https://github.com/python-kasa/python-kasa/pull/1415) (@rytilahti)
|
||||
- Add powerprotection module [\#1337](https://github.com/python-kasa/python-kasa/pull/1337) (@rytilahti)
|
||||
|
||||
**Fixed bugs:**
|
||||
|
||||
- TP-Link HS300 Wi-Fi Power-Strip - "Parent On/Off" not functioning. [\#637](https://github.com/python-kasa/python-kasa/issues/637)
|
||||
- Report 0 for instead of None for zero current and voltage [\#1483](https://github.com/python-kasa/python-kasa/pull/1483) (@ryenitcher)
|
||||
- ssltransport: use debug logger for sending requests [\#1443](https://github.com/python-kasa/python-kasa/pull/1443) (@rytilahti)
|
||||
- Fix discover cli command with host [\#1437](https://github.com/python-kasa/python-kasa/pull/1437) (@sdb9696)
|
||||
- Fallback to is\_low for batterysensor's battery\_low [\#1420](https://github.com/python-kasa/python-kasa/pull/1420) (@rytilahti)
|
||||
- Convert carpet\_clean\_mode to carpet\_boost switch [\#1486](https://github.com/python-kasa/python-kasa/pull/1486) (@rytilahti)
|
||||
- Change category for empty dustbin feature from Primary to Config [\#1485](https://github.com/python-kasa/python-kasa/pull/1485) (@rytilahti)
|
||||
- Disable iot camera creation until more complete [\#1480](https://github.com/python-kasa/python-kasa/pull/1480) (@sdb9696)
|
||||
- Add error code 7 for clean module [\#1474](https://github.com/python-kasa/python-kasa/pull/1474) (@rytilahti)
|
||||
- Fix iot strip turn on and off from parent [\#639](https://github.com/python-kasa/python-kasa/pull/639) (@Obbay2)
|
||||
|
||||
**Added support for devices:**
|
||||
|
||||
- Add C220\(EU\) 1.0 1.2.2 camera fixture [\#1466](https://github.com/python-kasa/python-kasa/pull/1466) (@DawidPietrykowski)
|
||||
- Add D230\(EU\) 1.20 1.1.19 fixture [\#1448](https://github.com/python-kasa/python-kasa/pull/1448) (@sdb9696)
|
||||
- Add fixture for C720 camera [\#1433](https://github.com/python-kasa/python-kasa/pull/1433) (@steveredden)
|
||||
- Add D130\(US\) 1.0 1.1.9 fixture [\#1476](https://github.com/python-kasa/python-kasa/pull/1476) (@sdb9696)
|
||||
- Add D100C\(US\) 1.0 1.1.3 fixture [\#1475](https://github.com/python-kasa/python-kasa/pull/1475) (@sdb9696)
|
||||
|
||||
**Project maintenance:**
|
||||
|
||||
- Enable CI workflow on PRs to feat/ fix/ and janitor/ [\#1471](https://github.com/python-kasa/python-kasa/pull/1471) (@sdb9696)
|
||||
- Add commit-hook to prettify JSON files [\#1455](https://github.com/python-kasa/python-kasa/pull/1455) (@rytilahti)
|
||||
- Add required sphinx.configuration [\#1446](https://github.com/python-kasa/python-kasa/pull/1446) (@rytilahti)
|
||||
- Add more redactors for smartcams [\#1439](https://github.com/python-kasa/python-kasa/pull/1439) (@sdb9696)
|
||||
- Add KS230\(US\) 2.0 1.0.11 IOT Fixture [\#1430](https://github.com/python-kasa/python-kasa/pull/1430) (@ZeliardM)
|
||||
- Update ruff to 0.9 [\#1482](https://github.com/python-kasa/python-kasa/pull/1482) (@sdb9696)
|
||||
- Cancel in progress CI workflows after new pushes [\#1481](https://github.com/python-kasa/python-kasa/pull/1481) (@sdb9696)
|
||||
- Update test framework to support smartcam device discovery. [\#1477](https://github.com/python-kasa/python-kasa/pull/1477) (@sdb9696)
|
||||
- Add tests for dump\_devinfo parent/child smartcam fixture generation [\#1428](https://github.com/python-kasa/python-kasa/pull/1428) (@sdb9696)
|
||||
- Raise errors on single smartcam child requests [\#1427](https://github.com/python-kasa/python-kasa/pull/1427) (@sdb9696)
|
||||
|
||||
## [0.9.1](https://github.com/python-kasa/python-kasa/tree/0.9.1) (2025-01-06)
|
||||
|
||||
[Full Changelog](https://github.com/python-kasa/python-kasa/compare/0.9.0...0.9.1)
|
||||
@ -19,8 +157,8 @@
|
||||
**Fixed bugs:**
|
||||
|
||||
- T310 not detected with H200 Hub [\#1409](https://github.com/python-kasa/python-kasa/issues/1409)
|
||||
- Backoff after xor timeout and improve error reporting [\#1424](https://github.com/python-kasa/python-kasa/pull/1424) (@bdraco)
|
||||
- Fix incorrect obd src echo [\#1412](https://github.com/python-kasa/python-kasa/pull/1412) (@rytilahti)
|
||||
- Backoff after xor timeout and improve error reporting [\#1424](https://github.com/python-kasa/python-kasa/pull/1424) (@bdraco)
|
||||
- Handle smartcam partial list responses [\#1411](https://github.com/python-kasa/python-kasa/pull/1411) (@sdb9696)
|
||||
|
||||
**Added support for devices:**
|
||||
@ -34,8 +172,8 @@
|
||||
|
||||
**Project maintenance:**
|
||||
|
||||
- Add C210 2.0 1.3.11 fixture [\#1406](https://github.com/python-kasa/python-kasa/pull/1406) (@sdb9696)
|
||||
- Add HS210\(US\) 3.0 1.0.10 IOT Fixture [\#1405](https://github.com/python-kasa/python-kasa/pull/1405) (@ZeliardM)
|
||||
- Add C210 2.0 1.3.11 fixture [\#1406](https://github.com/python-kasa/python-kasa/pull/1406) (@sdb9696)
|
||||
- Change smartcam detection features to category config [\#1402](https://github.com/python-kasa/python-kasa/pull/1402) (@sdb9696)
|
||||
|
||||
## [0.9.0](https://github.com/python-kasa/python-kasa/tree/0.9.0) (2024-12-21)
|
||||
|
@ -189,7 +189,7 @@ The following devices have been tested and confirmed as working. If your device
|
||||
- **Plugs**: EP10, EP25[^1], HS100[^2], HS103, HS105, HS110, KP100, KP105, KP115, KP125, KP125M[^1], KP401
|
||||
- **Power Strips**: EP40, EP40M[^1], HS107, HS300, KP200, KP303, KP400
|
||||
- **Wall Switches**: ES20M, HS200[^2], HS210, HS220[^2], KP405, KS200, KS200M, KS205[^1], KS220, KS220M, KS225[^1], KS230, KS240[^1]
|
||||
- **Bulbs**: KL110, KL120, KL125, KL130, KL135, KL50, KL60, LB110
|
||||
- **Bulbs**: KL110, KL120, KL125, KL130, KL135, KL50, KL60, LB100, LB110
|
||||
- **Light Strips**: KL400L5, KL420L5, KL430
|
||||
- **Hubs**: KH100[^1]
|
||||
- **Hub-Connected Devices[^3]**: KE100[^1]
|
||||
@ -199,9 +199,11 @@ The following devices have been tested and confirmed as working. If your device
|
||||
- **Plugs**: P100, P110, P110M, P115, P125M, P135, TP15
|
||||
- **Power Strips**: P210M, P300, P304M, P306, TP25
|
||||
- **Wall Switches**: S210, S220, S500D, S505, S505D
|
||||
- **Bulbs**: L510B, L510E, L530E, L630
|
||||
- **Bulbs**: L510B, L510E, L530B, L530E, L630
|
||||
- **Light Strips**: L900-10, L900-5, L920-5, L930-5
|
||||
- **Cameras**: C100, C210, C225, C325WB, C520WS, TC65, TC70
|
||||
- **Cameras**: C100, C110, C210, C220, C225, C325WB, C520WS, C720, TC65, TC70
|
||||
- **Doorbells and chimes**: D100C, D130, D230
|
||||
- **Vacuums**: RV20 Max Plus, RV30 Max
|
||||
- **Hubs**: H100, H200
|
||||
- **Hub-Connected Devices[^3]**: S200B, S200D, T100, T110, T300, T310, T315
|
||||
|
||||
|
@ -44,9 +44,10 @@ uv lock --upgrade
|
||||
uv sync --all-extras
|
||||
```
|
||||
|
||||
### Run pre-commit and tests
|
||||
### Update and run pre-commit and tests
|
||||
|
||||
```bash
|
||||
pre-commit autoupdate
|
||||
uv run pre-commit run --all-files
|
||||
uv run pytest -n auto
|
||||
```
|
||||
@ -124,6 +125,12 @@ git push upstream release/$NEW_RELEASE -u
|
||||
gh pr create --title "Prepare $NEW_RELEASE" --body "$RELEASE_NOTES" --label release-prep --base master
|
||||
```
|
||||
|
||||
To update the PR after refreshing the changelog:
|
||||
|
||||
```
|
||||
gh pr edit --body "$RELEASE_NOTES"
|
||||
```
|
||||
|
||||
#### Merge the PR once the CI passes
|
||||
|
||||
Create a squash commit and add the markdown from the PR description to the commit description.
|
||||
|
34
SUPPORTED.md
34
SUPPORTED.md
@ -118,8 +118,10 @@ Some newer Kasa devices require authentication. These are marked with [^1] in th
|
||||
- **KS225**
|
||||
- Hardware: 1.0 (US) / Firmware: 1.0.2[^1]
|
||||
- Hardware: 1.0 (US) / Firmware: 1.1.0[^1]
|
||||
- Hardware: 1.0 (US) / Firmware: 1.1.1[^1]
|
||||
- **KS230**
|
||||
- Hardware: 1.0 (US) / Firmware: 1.0.14
|
||||
- Hardware: 2.0 (US) / Firmware: 1.0.11
|
||||
- **KS240**
|
||||
- Hardware: 1.0 (US) / Firmware: 1.0.4[^1]
|
||||
- Hardware: 1.0 (US) / Firmware: 1.0.5[^1]
|
||||
@ -147,6 +149,8 @@ Some newer Kasa devices require authentication. These are marked with [^1] in th
|
||||
- **KL60**
|
||||
- Hardware: 1.0 (UN) / Firmware: 1.1.4
|
||||
- Hardware: 1.0 (US) / Firmware: 1.1.13
|
||||
- **LB100**
|
||||
- Hardware: 1.0 (US) / Firmware: 1.8.11
|
||||
- **LB110**
|
||||
- Hardware: 1.0 (US) / Firmware: 1.8.11
|
||||
|
||||
@ -190,6 +194,7 @@ All Tapo devices require authentication.<br>Hub-Connected Devices may work acros
|
||||
- Hardware: 1.0.0 (US) / Firmware: 1.3.7
|
||||
- Hardware: 1.0.0 (US) / Firmware: 1.4.0
|
||||
- **P110**
|
||||
- Hardware: 1.0 (AU) / Firmware: 1.3.1
|
||||
- Hardware: 1.0 (EU) / Firmware: 1.0.7
|
||||
- Hardware: 1.0 (EU) / Firmware: 1.2.3
|
||||
- Hardware: 1.0 (UK) / Firmware: 1.3.0
|
||||
@ -242,10 +247,13 @@ All Tapo devices require authentication.<br>Hub-Connected Devices may work acros
|
||||
- **L510E**
|
||||
- Hardware: 3.0 (US) / Firmware: 1.0.5
|
||||
- Hardware: 3.0 (US) / Firmware: 1.1.2
|
||||
- **L530B**
|
||||
- Hardware: 3.0 (EU) / Firmware: 1.1.9
|
||||
- **L530E**
|
||||
- Hardware: 3.0 (EU) / Firmware: 1.0.6
|
||||
- Hardware: 3.0 (EU) / Firmware: 1.1.0
|
||||
- Hardware: 3.0 (EU) / Firmware: 1.1.6
|
||||
- Hardware: 2.0 (TW) / Firmware: 1.1.1
|
||||
- Hardware: 2.0 (US) / Firmware: 1.1.0
|
||||
- **L630**
|
||||
- Hardware: 1.0 (EU) / Firmware: 1.1.2
|
||||
@ -264,35 +272,60 @@ All Tapo devices require authentication.<br>Hub-Connected Devices may work acros
|
||||
- Hardware: 1.0 (US) / Firmware: 1.1.0
|
||||
- Hardware: 1.0 (US) / Firmware: 1.1.3
|
||||
- **L930-5**
|
||||
- Hardware: 1.0 (EU) / Firmware: 1.2.5
|
||||
- Hardware: 1.0 (US) / Firmware: 1.1.2
|
||||
|
||||
### Cameras
|
||||
|
||||
- **C100**
|
||||
- Hardware: 4.0 / Firmware: 1.3.14
|
||||
- **C110**
|
||||
- Hardware: 2.0 (EU) / Firmware: 1.4.3
|
||||
- **C210**
|
||||
- Hardware: 2.0 / Firmware: 1.3.11
|
||||
- Hardware: 2.0 (EU) / Firmware: 1.4.2
|
||||
- Hardware: 2.0 (EU) / Firmware: 1.4.3
|
||||
- **C220**
|
||||
- Hardware: 1.0 (EU) / Firmware: 1.2.2
|
||||
- **C225**
|
||||
- Hardware: 2.0 (US) / Firmware: 1.0.11
|
||||
- **C325WB**
|
||||
- Hardware: 1.0 (EU) / Firmware: 1.1.17
|
||||
- **C520WS**
|
||||
- Hardware: 1.0 (US) / Firmware: 1.2.8
|
||||
- **C720**
|
||||
- Hardware: 1.0 (US) / Firmware: 1.2.3
|
||||
- **TC65**
|
||||
- Hardware: 1.0 / Firmware: 1.3.9
|
||||
- **TC70**
|
||||
- Hardware: 3.0 / Firmware: 1.3.11
|
||||
|
||||
### Doorbells and chimes
|
||||
|
||||
- **D100C**
|
||||
- Hardware: 1.0 (US) / Firmware: 1.1.3
|
||||
- **D130**
|
||||
- Hardware: 1.0 (US) / Firmware: 1.1.9
|
||||
- **D230**
|
||||
- Hardware: 1.20 (EU) / Firmware: 1.1.19
|
||||
|
||||
### Vacuums
|
||||
|
||||
- **RV20 Max Plus**
|
||||
- Hardware: 1.0 (EU) / Firmware: 1.0.7
|
||||
- **RV30 Max**
|
||||
- Hardware: 1.0 (US) / Firmware: 1.2.0
|
||||
|
||||
### Hubs
|
||||
|
||||
- **H100**
|
||||
- Hardware: 1.0 (AU) / Firmware: 1.5.23
|
||||
- Hardware: 1.0 (EU) / Firmware: 1.2.3
|
||||
- Hardware: 1.0 (EU) / Firmware: 1.5.10
|
||||
- Hardware: 1.0 (EU) / Firmware: 1.5.5
|
||||
- **H200**
|
||||
- Hardware: 1.0 (EU) / Firmware: 1.3.2
|
||||
- Hardware: 1.0 (EU) / Firmware: 1.3.6
|
||||
- Hardware: 1.0 (US) / Firmware: 1.3.6
|
||||
|
||||
### Hub-Connected Devices
|
||||
@ -305,6 +338,7 @@ All Tapo devices require authentication.<br>Hub-Connected Devices may work acros
|
||||
- Hardware: 1.0 (EU) / Firmware: 1.12.0
|
||||
- **T100**
|
||||
- Hardware: 1.0 (EU) / Firmware: 1.12.0
|
||||
- Hardware: 1.0 (US) / Firmware: 1.12.0
|
||||
- **T110**
|
||||
- Hardware: 1.0 (EU) / Firmware: 1.8.0
|
||||
- Hardware: 1.0 (EU) / Firmware: 1.9.0
|
||||
|
@ -54,7 +54,8 @@ from kasa.protocols.smartcamprotocol import (
|
||||
from kasa.protocols.smartprotocol import REDACTORS as SMART_REDACTORS
|
||||
from kasa.protocols.smartprotocol import SmartProtocol, _ChildProtocolWrapper
|
||||
from kasa.smart import SmartChildDevice, SmartDevice
|
||||
from kasa.smartcam import SmartCamDevice
|
||||
from kasa.smartcam import SmartCamChild, SmartCamDevice
|
||||
from kasa.smartcam.smartcamchild import CHILD_INFO_FROM_PARENT
|
||||
|
||||
Call = namedtuple("Call", "module method")
|
||||
FixtureResult = namedtuple("FixtureResult", "filename, folder, data, protocol_suffix")
|
||||
@ -62,11 +63,13 @@ FixtureResult = namedtuple("FixtureResult", "filename, folder, data, protocol_su
|
||||
SMART_FOLDER = "tests/fixtures/smart/"
|
||||
SMARTCAM_FOLDER = "tests/fixtures/smartcam/"
|
||||
SMART_CHILD_FOLDER = "tests/fixtures/smart/child/"
|
||||
SMARTCAM_CHILD_FOLDER = "tests/fixtures/smartcam/child/"
|
||||
IOT_FOLDER = "tests/fixtures/iot/"
|
||||
|
||||
SMART_PROTOCOL_SUFFIX = "SMART"
|
||||
SMARTCAM_SUFFIX = "SMARTCAM"
|
||||
SMART_CHILD_SUFFIX = "SMART.CHILD"
|
||||
SMARTCAM_CHILD_SUFFIX = "SMARTCAM.CHILD"
|
||||
IOT_SUFFIX = "IOT"
|
||||
|
||||
NO_GIT_FIXTURE_FOLDER = "kasa-fixtures"
|
||||
@ -297,7 +300,9 @@ async def cli(
|
||||
connection_type = DeviceConnectionParameters.from_values(
|
||||
dr.device_type,
|
||||
dr.mgt_encrypt_schm.encrypt_type,
|
||||
dr.mgt_encrypt_schm.lv,
|
||||
login_version=dr.mgt_encrypt_schm.lv,
|
||||
https=dr.mgt_encrypt_schm.is_support_https,
|
||||
http_port=dr.mgt_encrypt_schm.http_port,
|
||||
)
|
||||
dc = DeviceConfig(
|
||||
host=host,
|
||||
@ -720,15 +725,6 @@ async def get_smart_test_calls(protocol: SmartProtocol):
|
||||
successes = []
|
||||
child_device_components = {}
|
||||
|
||||
extra_test_calls = [
|
||||
SmartCall(
|
||||
module="temp_humidity_records",
|
||||
request=SmartRequest.get_raw_request("get_temp_humidity_records").to_dict(),
|
||||
should_succeed=False,
|
||||
child_device_id="",
|
||||
),
|
||||
]
|
||||
|
||||
click.echo("Testing component_nego call ..", nl=False)
|
||||
responses = await _make_requests_or_exit(
|
||||
protocol,
|
||||
@ -807,8 +803,6 @@ async def get_smart_test_calls(protocol: SmartProtocol):
|
||||
click.echo(f"Skipping {component_id}..", nl=False)
|
||||
click.echo(click.style("UNSUPPORTED", fg="yellow"))
|
||||
|
||||
test_calls.extend(extra_test_calls)
|
||||
|
||||
# Child component calls
|
||||
for child_device_id, child_components in child_device_components.items():
|
||||
test_calls.append(
|
||||
@ -834,19 +828,12 @@ async def get_smart_test_calls(protocol: SmartProtocol):
|
||||
else:
|
||||
click.echo(f"Skipping {component_id}..", nl=False)
|
||||
click.echo(click.style("UNSUPPORTED", fg="yellow"))
|
||||
# Add the extra calls for each child
|
||||
for extra_call in extra_test_calls:
|
||||
extra_child_call = dataclasses.replace(
|
||||
extra_call, child_device_id=child_device_id
|
||||
)
|
||||
test_calls.append(extra_child_call)
|
||||
|
||||
return test_calls, successes
|
||||
|
||||
|
||||
def get_smart_child_fixture(response):
|
||||
def get_smart_child_fixture(response, model_info, folder, suffix):
|
||||
"""Get a seperate fixture for the child device."""
|
||||
model_info = SmartDevice._get_device_info(response, None)
|
||||
hw_version = model_info.hardware_version
|
||||
fw_version = model_info.firmware_version
|
||||
model = model_info.long_name
|
||||
@ -855,12 +842,68 @@ def get_smart_child_fixture(response):
|
||||
save_filename = f"{model}_{hw_version}_{fw_version}"
|
||||
return FixtureResult(
|
||||
filename=save_filename,
|
||||
folder=SMART_CHILD_FOLDER,
|
||||
folder=folder,
|
||||
data=response,
|
||||
protocol_suffix=SMART_CHILD_SUFFIX,
|
||||
protocol_suffix=suffix,
|
||||
)
|
||||
|
||||
|
||||
def scrub_child_device_ids(
|
||||
main_response: dict, child_responses: dict
|
||||
) -> dict[str, str]:
|
||||
"""Scrub all the child device ids in the responses."""
|
||||
# Make the scrubbed id map
|
||||
scrubbed_child_id_map = {
|
||||
device_id: f"SCRUBBED_CHILD_DEVICE_ID_{index + 1}"
|
||||
for index, device_id in enumerate(child_responses.keys())
|
||||
if device_id != ""
|
||||
}
|
||||
|
||||
for child_id, response in child_responses.items():
|
||||
scrubbed_child_id = scrubbed_child_id_map[child_id]
|
||||
# scrub the device id in the child's get info response
|
||||
# The checks for the device_id will ensure we can get a fixture
|
||||
# even if the data is unexpectedly not available although it should
|
||||
# always be there
|
||||
if "get_device_info" in response and "device_id" in response["get_device_info"]:
|
||||
response["get_device_info"]["device_id"] = scrubbed_child_id
|
||||
elif (
|
||||
basic_info := response.get("getDeviceInfo", {})
|
||||
.get("device_info", {})
|
||||
.get("basic_info")
|
||||
) and "dev_id" in basic_info:
|
||||
basic_info["dev_id"] = scrubbed_child_id
|
||||
else:
|
||||
_LOGGER.error(
|
||||
"Cannot find device id in child get device info: %s", child_id
|
||||
)
|
||||
|
||||
# Scrub the device ids in the parent for smart protocol
|
||||
if gc := main_response.get("get_child_device_component_list"):
|
||||
for child in gc["child_component_list"]:
|
||||
device_id = child["device_id"]
|
||||
child["device_id"] = scrubbed_child_id_map[device_id]
|
||||
for child in main_response["get_child_device_list"]["child_device_list"]:
|
||||
device_id = child["device_id"]
|
||||
child["device_id"] = scrubbed_child_id_map[device_id]
|
||||
|
||||
# Scrub the device ids in the parent for the smart camera protocol
|
||||
if gc := main_response.get("getChildDeviceComponentList"):
|
||||
for child in gc["child_component_list"]:
|
||||
device_id = child["device_id"]
|
||||
child["device_id"] = scrubbed_child_id_map[device_id]
|
||||
for child in main_response["getChildDeviceList"]["child_device_list"]:
|
||||
if device_id := child.get("device_id"):
|
||||
child["device_id"] = scrubbed_child_id_map[device_id]
|
||||
continue
|
||||
elif dev_id := child.get("dev_id"):
|
||||
child["dev_id"] = scrubbed_child_id_map[dev_id]
|
||||
continue
|
||||
_LOGGER.error("Could not find a device id for the child device: %s", child)
|
||||
|
||||
return scrubbed_child_id_map
|
||||
|
||||
|
||||
async def get_smart_fixtures(
|
||||
protocol: SmartProtocol,
|
||||
*,
|
||||
@ -917,21 +960,19 @@ async def get_smart_fixtures(
|
||||
finally:
|
||||
await protocol.close()
|
||||
|
||||
# Put all the successes into a dict[child_device_id or "", successes[]]
|
||||
device_requests: dict[str, list[SmartCall]] = {}
|
||||
for success in successes:
|
||||
device_request = device_requests.setdefault(success.child_device_id, [])
|
||||
device_request.append(success)
|
||||
|
||||
scrubbed_device_ids = {
|
||||
device_id: f"SCRUBBED_CHILD_DEVICE_ID_{index}"
|
||||
for index, device_id in enumerate(device_requests.keys())
|
||||
if device_id != ""
|
||||
}
|
||||
|
||||
final = await _make_final_calls(
|
||||
protocol, device_requests[""], "All successes", batch_size, child_device_id=""
|
||||
)
|
||||
fixture_results = []
|
||||
|
||||
# Make the final child calls
|
||||
child_responses = {}
|
||||
for child_device_id, requests in device_requests.items():
|
||||
if child_device_id == "":
|
||||
continue
|
||||
@ -942,55 +983,82 @@ async def get_smart_fixtures(
|
||||
batch_size,
|
||||
child_device_id=child_device_id,
|
||||
)
|
||||
child_responses[child_device_id] = response
|
||||
|
||||
scrubbed = scrubbed_device_ids[child_device_id]
|
||||
if "get_device_info" in response and "device_id" in response["get_device_info"]:
|
||||
response["get_device_info"]["device_id"] = scrubbed
|
||||
# If the child is a different model to the parent create a seperate fixture
|
||||
if "get_device_info" in final:
|
||||
parent_model = final["get_device_info"]["model"]
|
||||
elif "getDeviceInfo" in final:
|
||||
parent_model = final["getDeviceInfo"]["device_info"]["basic_info"][
|
||||
"device_model"
|
||||
]
|
||||
# scrub the child ids
|
||||
scrubbed_child_id_map = scrub_child_device_ids(final, child_responses)
|
||||
|
||||
# Redact data from the main device response. _wrap_redactors ensure we do
|
||||
# not redact the scrubbed child device ids and replaces REDACTED_partial_id
|
||||
# with zeros
|
||||
final = redact_data(final, _wrap_redactors(SMART_REDACTORS))
|
||||
|
||||
# smart cam child devices provide more information in getChildDeviceList on the
|
||||
# parent than they return when queried directly for getDeviceInfo so we will store
|
||||
# it in the child fixture.
|
||||
if smart_cam_child_list := final.get("getChildDeviceList"):
|
||||
child_infos_on_parent = {
|
||||
info["device_id"]: info
|
||||
for info in smart_cam_child_list["child_device_list"]
|
||||
}
|
||||
|
||||
for child_id, response in child_responses.items():
|
||||
scrubbed_child_id = scrubbed_child_id_map[child_id]
|
||||
|
||||
# Get the parent model for checking whether to create a seperate child fixture
|
||||
if model := final.get("get_device_info", {}).get("model"):
|
||||
parent_model = model
|
||||
elif (
|
||||
device_model := final.get("getDeviceInfo", {})
|
||||
.get("device_info", {})
|
||||
.get("basic_info", {})
|
||||
.get("device_model")
|
||||
):
|
||||
parent_model = device_model
|
||||
else:
|
||||
raise KasaException("Cannot determine parent device model.")
|
||||
parent_model = None
|
||||
_LOGGER.error("Cannot determine parent device model.")
|
||||
|
||||
# different model smart child device
|
||||
if (
|
||||
"component_nego" in response
|
||||
and "get_device_info" in response
|
||||
and (child_model := response["get_device_info"].get("model"))
|
||||
(child_model := response.get("get_device_info", {}).get("model"))
|
||||
and parent_model
|
||||
and child_model != parent_model
|
||||
):
|
||||
response = redact_data(response, _wrap_redactors(SMART_REDACTORS))
|
||||
fixture_results.append(get_smart_child_fixture(response))
|
||||
model_info = SmartDevice._get_device_info(response, None)
|
||||
fixture_results.append(
|
||||
get_smart_child_fixture(
|
||||
response, model_info, SMART_CHILD_FOLDER, SMART_CHILD_SUFFIX
|
||||
)
|
||||
)
|
||||
# different model smartcam child device
|
||||
elif (
|
||||
(
|
||||
child_model := response.get("getDeviceInfo", {})
|
||||
.get("device_info", {})
|
||||
.get("basic_info", {})
|
||||
.get("device_model")
|
||||
)
|
||||
and parent_model
|
||||
and child_model != parent_model
|
||||
):
|
||||
response = redact_data(response, _wrap_redactors(SMART_REDACTORS))
|
||||
# There is more info in the childDeviceList on the parent
|
||||
# particularly the region is needed here.
|
||||
child_info_from_parent = child_infos_on_parent[scrubbed_child_id]
|
||||
response[CHILD_INFO_FROM_PARENT] = child_info_from_parent
|
||||
model_info = SmartCamChild._get_device_info(response, None)
|
||||
fixture_results.append(
|
||||
get_smart_child_fixture(
|
||||
response, model_info, SMARTCAM_CHILD_FOLDER, SMARTCAM_CHILD_SUFFIX
|
||||
)
|
||||
)
|
||||
# same model child device
|
||||
else:
|
||||
cd = final.setdefault("child_devices", {})
|
||||
cd[scrubbed] = response
|
||||
cd[scrubbed_child_id] = response
|
||||
|
||||
# Scrub the device ids in the parent for smart protocol
|
||||
if gc := final.get("get_child_device_component_list"):
|
||||
for child in gc["child_component_list"]:
|
||||
device_id = child["device_id"]
|
||||
child["device_id"] = scrubbed_device_ids[device_id]
|
||||
for child in final["get_child_device_list"]["child_device_list"]:
|
||||
device_id = child["device_id"]
|
||||
child["device_id"] = scrubbed_device_ids[device_id]
|
||||
|
||||
# Scrub the device ids in the parent for the smart camera protocol
|
||||
if gc := final.get("getChildDeviceComponentList"):
|
||||
for child in gc["child_component_list"]:
|
||||
device_id = child["device_id"]
|
||||
child["device_id"] = scrubbed_device_ids[device_id]
|
||||
for child in final["getChildDeviceList"]["child_device_list"]:
|
||||
if device_id := child.get("device_id"):
|
||||
child["device_id"] = scrubbed_device_ids[device_id]
|
||||
continue
|
||||
elif dev_id := child.get("dev_id"):
|
||||
child["dev_id"] = scrubbed_device_ids[dev_id]
|
||||
continue
|
||||
_LOGGER.error("Could not find a device for the child device: %s", child)
|
||||
|
||||
final = redact_data(final, _wrap_redactors(SMART_REDACTORS))
|
||||
discovery_result = None
|
||||
if discovery_info:
|
||||
final["discovery_result"] = redact_data(
|
||||
|
@ -13,7 +13,7 @@ from typing import Any, NamedTuple
|
||||
from kasa.device_type import DeviceType
|
||||
from kasa.iot import IotDevice
|
||||
from kasa.smart import SmartDevice
|
||||
from kasa.smartcam import SmartCamDevice
|
||||
from kasa.smartcam import SmartCamChild, SmartCamDevice
|
||||
|
||||
|
||||
class SupportedVersion(NamedTuple):
|
||||
@ -36,6 +36,9 @@ DEVICE_TYPE_TO_PRODUCT_GROUP = {
|
||||
DeviceType.Bulb: "Bulbs",
|
||||
DeviceType.LightStrip: "Light Strips",
|
||||
DeviceType.Camera: "Cameras",
|
||||
DeviceType.Doorbell: "Doorbells and chimes",
|
||||
DeviceType.Chime: "Doorbells and chimes",
|
||||
DeviceType.Vacuum: "Vacuums",
|
||||
DeviceType.Hub: "Hubs",
|
||||
DeviceType.Sensor: "Hub-Connected Devices",
|
||||
DeviceType.Thermostat: "Hub-Connected Devices",
|
||||
@ -49,6 +52,7 @@ IOT_FOLDER = "tests/fixtures/iot/"
|
||||
SMART_FOLDER = "tests/fixtures/smart/"
|
||||
SMART_CHILD_FOLDER = "tests/fixtures/smart/child"
|
||||
SMARTCAM_FOLDER = "tests/fixtures/smartcam/"
|
||||
SMARTCAM_CHILD_FOLDER = "tests/fixtures/smartcam/child"
|
||||
|
||||
|
||||
def generate_supported(args):
|
||||
@ -66,6 +70,7 @@ def generate_supported(args):
|
||||
_get_supported_devices(supported, SMART_FOLDER, SmartDevice)
|
||||
_get_supported_devices(supported, SMART_CHILD_FOLDER, SmartDevice)
|
||||
_get_supported_devices(supported, SMARTCAM_FOLDER, SmartCamDevice)
|
||||
_get_supported_devices(supported, SMARTCAM_CHILD_FOLDER, SmartCamChild)
|
||||
|
||||
readme_updated = _update_supported_file(
|
||||
README_FILENAME, _supported_summary(supported), print_diffs
|
||||
|
@ -118,6 +118,16 @@ class SmartRequest:
|
||||
enable: bool
|
||||
id: str | None = None
|
||||
|
||||
@dataclass
|
||||
class GetCleanAttrParams(SmartRequestParams):
|
||||
"""CleanAttr params.
|
||||
|
||||
Decides which cleaning settings are requested
|
||||
"""
|
||||
|
||||
#: type can be global or pose
|
||||
type: str = "global"
|
||||
|
||||
@staticmethod
|
||||
def get_raw_request(
|
||||
method: str, params: SmartRequestParams | None = None
|
||||
@ -415,6 +425,7 @@ COMPONENT_REQUESTS = {
|
||||
"get_trigger_logs", SmartRequest.GetTriggerLogsParams()
|
||||
)
|
||||
],
|
||||
"temp_humidity_record": [SmartRequest.get_raw_request("get_temp_humidity_records")],
|
||||
"double_click": [SmartRequest.get_raw_request("get_double_click_info")],
|
||||
"child_device": [
|
||||
SmartRequest.get_raw_request("get_child_device_list"),
|
||||
@ -427,23 +438,32 @@ COMPONENT_REQUESTS = {
|
||||
"overheat_protection": [],
|
||||
# Vacuum components
|
||||
"clean": [
|
||||
SmartRequest.get_raw_request("getCarpetClean"),
|
||||
SmartRequest.get_raw_request("getCleanRecords"),
|
||||
SmartRequest.get_raw_request("getVacStatus"),
|
||||
SmartRequest.get_raw_request("getAreaUnit"),
|
||||
SmartRequest.get_raw_request("getCleanInfo"),
|
||||
SmartRequest.get_raw_request("getCleanStatus"),
|
||||
SmartRequest("getCleanAttr", SmartRequest.GetCleanAttrParams()),
|
||||
],
|
||||
"battery": [SmartRequest.get_raw_request("getBatteryInfo")],
|
||||
"consumables": [SmartRequest.get_raw_request("getConsumablesInfo")],
|
||||
"direction_control": [],
|
||||
"button_and_led": [],
|
||||
"button_and_led": [SmartRequest.get_raw_request("getChildLockInfo")],
|
||||
"speaker": [
|
||||
SmartRequest.get_raw_request("getSupportVoiceLanguage"),
|
||||
SmartRequest.get_raw_request("getCurrentVoiceLanguage"),
|
||||
SmartRequest.get_raw_request("getVolume"),
|
||||
],
|
||||
"map": [
|
||||
SmartRequest.get_raw_request("getMapInfo"),
|
||||
SmartRequest.get_raw_request("getMapData"),
|
||||
],
|
||||
"auto_change_map": [SmartRequest.get_raw_request("getAutoChangeMap")],
|
||||
"dust_bucket": [SmartRequest.get_raw_request("getAutoDustCollection")],
|
||||
"dust_bucket": [
|
||||
SmartRequest.get_raw_request("getAutoDustCollection"),
|
||||
SmartRequest.get_raw_request("getDustCollectionInfo"),
|
||||
],
|
||||
"mop": [SmartRequest.get_raw_request("getMopState")],
|
||||
"do_not_disturb": [SmartRequest.get_raw_request("getDoNotDisturb")],
|
||||
"charge_pose_clean": [],
|
||||
|
@ -286,8 +286,7 @@ def main(
|
||||
operator.local_seed = message
|
||||
response = None
|
||||
print(
|
||||
f"got handshake1 in {packet_number}, "
|
||||
f"looking for the response"
|
||||
f"got handshake1 in {packet_number}, looking for the response"
|
||||
)
|
||||
while (
|
||||
True
|
||||
|
@ -1,6 +1,10 @@
|
||||
|
||||
# Get Energy Consumption and Usage Statistics
|
||||
|
||||
:::{note}
|
||||
The documentation on this page applies only to KASA-branded devices.
|
||||
:::
|
||||
|
||||
:::{note}
|
||||
In order to use the helper methods to calculate the statistics correctly, your devices need to have correct time set.
|
||||
The devices use NTP (123/UDP) and public servers from [NTP Pool Project](https://www.ntppool.org/) to synchronize their time.
|
||||
|
@ -8,3 +8,10 @@
|
||||
.. automodule:: kasa.smart.modules.childdevice
|
||||
:noindex:
|
||||
```
|
||||
|
||||
## Pairing and unpairing
|
||||
|
||||
```{eval-rst}
|
||||
.. automodule:: kasa.interfaces.childsetup
|
||||
:noindex:
|
||||
```
|
||||
|
@ -13,6 +13,7 @@
|
||||
127.0.0.3
|
||||
127.0.0.4
|
||||
127.0.0.5
|
||||
127.0.0.6
|
||||
|
||||
:meth:`~kasa.Discover.discover_single` returns a single device by hostname:
|
||||
|
||||
@ -91,5 +92,5 @@ False
|
||||
True
|
||||
>>> for feat in dev.features.values():
|
||||
>>> print(f"{feat.name}: {feat.value}")
|
||||
Device ID: 0000000000000000000000000000000000000000\nState: True\nSignal Level: 2\nRSSI: -52\nSSID: #MASKED_SSID#\nReboot: <Action>\nBrightness: 50\nCloud connection: True\nHSV: HSV(hue=0, saturation=100, value=50)\nColor temperature: 2700\nAuto update enabled: True\nUpdate available: None\nCurrent firmware version: 1.1.6 Build 240130 Rel.173828\nAvailable firmware version: None\nCheck latest firmware: <Action>\nLight effect: Party\nLight preset: Light preset 1\nSmooth transition on: 2\nSmooth transition off: 2\nOverheated: False\nDevice time: 2024-02-23 02:40:15+01:00
|
||||
Device ID: 0000000000000000000000000000000000000000\nState: True\nSignal Level: 2\nRSSI: -52\nSSID: #MASKED_SSID#\nReboot: <Action>\nDevice time: 2024-02-23 02:40:15+01:00\nBrightness: 50\nCloud connection: True\nHSV: HSV(hue=0, saturation=100, value=50)\nColor temperature: 2700\nAuto update enabled: True\nUpdate available: None\nCurrent firmware version: 1.1.6 Build 240130 Rel.173828\nAvailable firmware version: None\nCheck latest firmware: <Action>\nLight effect: Party\nLight preset: Light preset 1\nSmooth transition on: 2\nSmooth transition off: 2\nOverheated: False
|
||||
"""
|
||||
|
@ -10,7 +10,7 @@ from collections.abc import Callable
|
||||
from contextlib import contextmanager
|
||||
from functools import singledispatch, update_wrapper, wraps
|
||||
from gettext import gettext
|
||||
from typing import TYPE_CHECKING, Any, Final
|
||||
from typing import TYPE_CHECKING, Any, Final, NoReturn
|
||||
|
||||
import asyncclick as click
|
||||
|
||||
@ -57,7 +57,7 @@ def echo(*args, **kwargs) -> None:
|
||||
_echo(*args, **kwargs)
|
||||
|
||||
|
||||
def error(msg: str) -> None:
|
||||
def error(msg: str) -> NoReturn:
|
||||
"""Print an error and exit."""
|
||||
echo(f"[bold red]{msg}[/bold red]")
|
||||
sys.exit(1)
|
||||
@ -68,6 +68,16 @@ def json_formatter_cb(result: Any, **kwargs) -> None:
|
||||
if not kwargs.get("json"):
|
||||
return
|
||||
|
||||
# Calling the discover command directly always returns a DeviceDict so if host
|
||||
# was specified just format the device json
|
||||
if (
|
||||
(host := kwargs.get("host"))
|
||||
and isinstance(result, dict)
|
||||
and (dev := result.get(host))
|
||||
and isinstance(dev, Device)
|
||||
):
|
||||
result = dev
|
||||
|
||||
@singledispatch
|
||||
def to_serializable(val):
|
||||
"""Regular obj-to-string for json serialization.
|
||||
@ -85,6 +95,25 @@ def json_formatter_cb(result: Any, **kwargs) -> None:
|
||||
print(json_content)
|
||||
|
||||
|
||||
async def invoke_subcommand(
|
||||
command: click.BaseCommand,
|
||||
ctx: click.Context,
|
||||
args: list[str] | None = None,
|
||||
**extra: Any,
|
||||
) -> Any:
|
||||
"""Invoke a click subcommand.
|
||||
|
||||
Calling ctx.Invoke() treats the command like a simple callback and doesn't
|
||||
process any result_callbacks so we use this pattern from the click docs
|
||||
https://click.palletsprojects.com/en/stable/exceptions/#what-if-i-don-t-want-that.
|
||||
"""
|
||||
if args is None:
|
||||
args = []
|
||||
sub_ctx = await command.make_context(command.name, args, parent=ctx, **extra)
|
||||
async with sub_ctx:
|
||||
return await command.invoke(sub_ctx)
|
||||
|
||||
|
||||
def pass_dev_or_child(wrapped_function: Callable) -> Callable:
|
||||
"""Pass the device or child to the click command based on the child options."""
|
||||
child_help = (
|
||||
|
@ -3,6 +3,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from pprint import pformat as pf
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
import asyncclick as click
|
||||
|
||||
@ -47,7 +48,7 @@ async def state(ctx, dev: Device):
|
||||
)
|
||||
echo(
|
||||
f"Firmware: {dev.device_info.firmware_version}"
|
||||
f" {dev.device_info.firmware_build}"
|
||||
f"{' ' + build if (build := dev.device_info.firmware_build) else ''}"
|
||||
)
|
||||
echo(f"MAC (rssi): {dev.mac} ({dev.rssi})")
|
||||
if verbose:
|
||||
@ -82,6 +83,8 @@ async def state(ctx, dev: Device):
|
||||
echo()
|
||||
from .discover import _echo_discovery_info
|
||||
|
||||
if TYPE_CHECKING:
|
||||
assert dev._discovery_info
|
||||
_echo_discovery_info(dev._discovery_info)
|
||||
|
||||
return dev.internal_state
|
||||
|
@ -4,6 +4,7 @@ from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
from pprint import pformat as pf
|
||||
from typing import TYPE_CHECKING, cast
|
||||
|
||||
import asyncclick as click
|
||||
|
||||
@ -17,8 +18,12 @@ from kasa import (
|
||||
from kasa.discover import (
|
||||
NEW_DISCOVERY_REDACTORS,
|
||||
ConnectAttempt,
|
||||
DeviceDict,
|
||||
DiscoveredRaw,
|
||||
DiscoveryResult,
|
||||
OnDiscoveredCallable,
|
||||
OnDiscoveredRawCallable,
|
||||
OnUnsupportedCallable,
|
||||
)
|
||||
from kasa.iot.iotdevice import _extract_sys_info
|
||||
from kasa.protocols.iotprotocol import REDACTORS as IOT_REDACTORS
|
||||
@ -30,15 +35,33 @@ from .common import echo, error
|
||||
|
||||
@click.group(invoke_without_command=True)
|
||||
@click.pass_context
|
||||
async def discover(ctx):
|
||||
async def discover(ctx: click.Context):
|
||||
"""Discover devices in the network."""
|
||||
if ctx.invoked_subcommand is None:
|
||||
return await ctx.invoke(detail)
|
||||
|
||||
|
||||
@discover.result_callback()
|
||||
@click.pass_context
|
||||
async def _close_protocols(ctx: click.Context, discovered: DeviceDict):
|
||||
"""Close all the device protocols if discover was invoked directly by the user."""
|
||||
if _discover_is_root_cmd(ctx):
|
||||
for dev in discovered.values():
|
||||
await dev.disconnect()
|
||||
return discovered
|
||||
|
||||
|
||||
def _discover_is_root_cmd(ctx: click.Context) -> bool:
|
||||
"""Will return true if discover was invoked directly by the user."""
|
||||
root_ctx = ctx.find_root()
|
||||
return (
|
||||
root_ctx.invoked_subcommand is None or root_ctx.invoked_subcommand == "discover"
|
||||
)
|
||||
|
||||
|
||||
@discover.command()
|
||||
@click.pass_context
|
||||
async def detail(ctx):
|
||||
async def detail(ctx: click.Context) -> DeviceDict:
|
||||
"""Discover devices in the network using udp broadcasts."""
|
||||
unsupported = []
|
||||
auth_failed = []
|
||||
@ -59,10 +82,14 @@ async def detail(ctx):
|
||||
from .device import state
|
||||
|
||||
async def print_discovered(dev: Device) -> None:
|
||||
if TYPE_CHECKING:
|
||||
assert ctx.parent
|
||||
async with sem:
|
||||
try:
|
||||
await dev.update()
|
||||
except AuthenticationError:
|
||||
if TYPE_CHECKING:
|
||||
assert dev._discovery_info
|
||||
auth_failed.append(dev._discovery_info)
|
||||
echo("== Authentication failed for device ==")
|
||||
_echo_discovery_info(dev._discovery_info)
|
||||
@ -73,9 +100,11 @@ async def detail(ctx):
|
||||
echo()
|
||||
|
||||
discovered = await _discover(
|
||||
ctx, print_discovered=print_discovered, print_unsupported=print_unsupported
|
||||
ctx,
|
||||
print_discovered=print_discovered if _discover_is_root_cmd(ctx) else None,
|
||||
print_unsupported=print_unsupported,
|
||||
)
|
||||
if ctx.parent.parent.params["host"]:
|
||||
if ctx.find_root().params["host"]:
|
||||
return discovered
|
||||
|
||||
echo(f"Found {len(discovered)} devices")
|
||||
@ -96,7 +125,7 @@ async def detail(ctx):
|
||||
help="Set flag to redact sensitive data from raw output.",
|
||||
)
|
||||
@click.pass_context
|
||||
async def raw(ctx, redact: bool):
|
||||
async def raw(ctx: click.Context, redact: bool) -> DeviceDict:
|
||||
"""Return raw discovery data returned from devices."""
|
||||
|
||||
def print_raw(discovered: DiscoveredRaw):
|
||||
@ -116,7 +145,7 @@ async def raw(ctx, redact: bool):
|
||||
|
||||
@discover.command()
|
||||
@click.pass_context
|
||||
async def list(ctx):
|
||||
async def list(ctx: click.Context) -> DeviceDict:
|
||||
"""List devices in the network in a table using udp broadcasts."""
|
||||
sem = asyncio.Semaphore()
|
||||
|
||||
@ -147,18 +176,24 @@ async def list(ctx):
|
||||
f"{'HOST':<15} {'MODEL':<9} {'DEVICE FAMILY':<20} {'ENCRYPT':<7} "
|
||||
f"{'HTTPS':<5} {'LV':<3} {'ALIAS'}"
|
||||
)
|
||||
return await _discover(
|
||||
discovered = await _discover(
|
||||
ctx,
|
||||
print_discovered=print_discovered,
|
||||
print_unsupported=print_unsupported,
|
||||
do_echo=False,
|
||||
)
|
||||
return discovered
|
||||
|
||||
|
||||
async def _discover(
|
||||
ctx, *, print_discovered=None, print_unsupported=None, print_raw=None, do_echo=True
|
||||
):
|
||||
params = ctx.parent.parent.params
|
||||
ctx: click.Context,
|
||||
*,
|
||||
print_discovered: OnDiscoveredCallable | None = None,
|
||||
print_unsupported: OnUnsupportedCallable | None = None,
|
||||
print_raw: OnDiscoveredRawCallable | None = None,
|
||||
do_echo=True,
|
||||
) -> DeviceDict:
|
||||
params = ctx.find_root().params
|
||||
target = params["target"]
|
||||
username = params["username"]
|
||||
password = params["password"]
|
||||
@ -170,8 +205,9 @@ async def _discover(
|
||||
credentials = Credentials(username, password) if username and password else None
|
||||
|
||||
if host:
|
||||
host = cast(str, host)
|
||||
echo(f"Discovering device {host} for {discovery_timeout} seconds")
|
||||
return await Discover.discover_single(
|
||||
dev = await Discover.discover_single(
|
||||
host,
|
||||
port=port,
|
||||
credentials=credentials,
|
||||
@ -180,6 +216,12 @@ async def _discover(
|
||||
on_unsupported=print_unsupported,
|
||||
on_discovered_raw=print_raw,
|
||||
)
|
||||
if dev:
|
||||
if print_discovered:
|
||||
await print_discovered(dev)
|
||||
return {host: dev}
|
||||
else:
|
||||
return {}
|
||||
if do_echo:
|
||||
echo(f"Discovering devices on {target} for {discovery_timeout} seconds")
|
||||
discovered_devices = await Discover.discover(
|
||||
@ -193,21 +235,18 @@ async def _discover(
|
||||
on_discovered_raw=print_raw,
|
||||
)
|
||||
|
||||
for device in discovered_devices.values():
|
||||
await device.protocol.close()
|
||||
|
||||
return discovered_devices
|
||||
|
||||
|
||||
@discover.command()
|
||||
@click.pass_context
|
||||
async def config(ctx):
|
||||
async def config(ctx: click.Context) -> DeviceDict:
|
||||
"""Bypass udp discovery and try to show connection config for a device.
|
||||
|
||||
Bypasses udp discovery and shows the parameters required to connect
|
||||
directly to the device.
|
||||
"""
|
||||
params = ctx.parent.parent.params
|
||||
params = ctx.find_root().params
|
||||
username = params["username"]
|
||||
password = params["password"]
|
||||
timeout = params["timeout"]
|
||||
@ -222,8 +261,11 @@ async def config(ctx):
|
||||
host_port = host + (f":{port}" if port else "")
|
||||
|
||||
def on_attempt(connect_attempt: ConnectAttempt, success: bool) -> None:
|
||||
prot, tran, dev = connect_attempt
|
||||
key_str = f"{prot.__name__} + {tran.__name__} + {dev.__name__}"
|
||||
prot, tran, dev, https = connect_attempt
|
||||
key_str = (
|
||||
f"{prot.__name__} + {tran.__name__} + {dev.__name__}"
|
||||
f" + {'https' if https else 'http'}"
|
||||
)
|
||||
result = "succeeded" if success else "failed"
|
||||
msg = f"Attempt to connect to {host_port} with {key_str} {result}"
|
||||
echo(msg)
|
||||
@ -239,6 +281,7 @@ async def config(ctx):
|
||||
f"--encrypt-type {cparams.encryption_type.value} "
|
||||
f"{'--https' if cparams.https else '--no-https'}"
|
||||
)
|
||||
return {host: dev}
|
||||
else:
|
||||
error(f"Unable to connect to {host}")
|
||||
|
||||
@ -251,7 +294,7 @@ def _echo_dictionary(discovery_info: dict) -> None:
|
||||
echo(f"\t{key_name_and_spaces}{value}")
|
||||
|
||||
|
||||
def _echo_discovery_info(discovery_info) -> None:
|
||||
def _echo_discovery_info(discovery_info: dict) -> None:
|
||||
# We don't have discovery info when all connection params are passed manually
|
||||
if discovery_info is None:
|
||||
return
|
||||
|
@ -6,10 +6,7 @@ import ast
|
||||
|
||||
import asyncclick as click
|
||||
|
||||
from kasa import (
|
||||
Device,
|
||||
Feature,
|
||||
)
|
||||
from kasa import Device, Feature
|
||||
|
||||
from .common import (
|
||||
echo,
|
||||
@ -133,7 +130,22 @@ async def feature(
|
||||
echo(f"{feat.name} ({name}): {feat.value}{unit}")
|
||||
return feat.value
|
||||
|
||||
value = ast.literal_eval(value)
|
||||
try:
|
||||
# Attempt to parse as python literal.
|
||||
value = ast.literal_eval(value)
|
||||
except ValueError:
|
||||
# The value is probably an unquoted string, so we'll raise an error,
|
||||
# and tell the user to quote the string.
|
||||
raise click.exceptions.BadParameter(
|
||||
f'{repr(value)} for {name} (Perhaps you forgot to "quote" the value?)'
|
||||
) from SyntaxError
|
||||
except SyntaxError:
|
||||
# There are likely miss-matched quotes or odd characters in the input,
|
||||
# so abort and complain to the user.
|
||||
raise click.exceptions.BadParameter(
|
||||
f"{repr(value)} for {name}"
|
||||
) from SyntaxError
|
||||
|
||||
echo(f"Changing {name} from {feat.value} to {value}")
|
||||
response = await dev.features[name].set_value(value)
|
||||
await dev.update()
|
||||
|
95
kasa/cli/hub.py
Normal file
95
kasa/cli/hub.py
Normal file
@ -0,0 +1,95 @@
|
||||
"""Hub-specific commands."""
|
||||
|
||||
import asyncio
|
||||
|
||||
import asyncclick as click
|
||||
|
||||
from kasa import DeviceType, Module, SmartDevice
|
||||
from kasa.smart import SmartChildDevice
|
||||
|
||||
from .common import (
|
||||
echo,
|
||||
error,
|
||||
pass_dev,
|
||||
)
|
||||
|
||||
|
||||
def pretty_category(cat: str):
|
||||
"""Return pretty category for paired devices."""
|
||||
return SmartChildDevice.CHILD_DEVICE_TYPE_MAP.get(cat)
|
||||
|
||||
|
||||
@click.group()
|
||||
@pass_dev
|
||||
async def hub(dev: SmartDevice):
|
||||
"""Commands controlling hub child device pairing."""
|
||||
if dev.device_type is not DeviceType.Hub:
|
||||
error(f"{dev} is not a hub.")
|
||||
|
||||
if dev.modules.get(Module.ChildSetup) is None:
|
||||
error(f"{dev} does not have child setup module.")
|
||||
|
||||
|
||||
@hub.command(name="list")
|
||||
@pass_dev
|
||||
async def hub_list(dev: SmartDevice):
|
||||
"""List hub paired child devices."""
|
||||
for c in dev.children:
|
||||
echo(f"{c.device_id}: {c}")
|
||||
|
||||
|
||||
@hub.command(name="supported")
|
||||
@pass_dev
|
||||
async def hub_supported(dev: SmartDevice):
|
||||
"""List supported hub child device categories."""
|
||||
cs = dev.modules[Module.ChildSetup]
|
||||
|
||||
for cat in cs.supported_categories:
|
||||
echo(f"Supports: {cat}")
|
||||
|
||||
|
||||
@hub.command(name="pair")
|
||||
@click.option("--timeout", default=10)
|
||||
@pass_dev
|
||||
async def hub_pair(dev: SmartDevice, timeout: int):
|
||||
"""Pair all pairable device.
|
||||
|
||||
This will pair any child devices currently in pairing mode.
|
||||
"""
|
||||
cs = dev.modules[Module.ChildSetup]
|
||||
|
||||
echo(f"Finding new devices for {timeout} seconds...")
|
||||
|
||||
pair_res = await cs.pair(timeout=timeout)
|
||||
if not pair_res:
|
||||
echo("No devices found.")
|
||||
|
||||
for child in pair_res:
|
||||
echo(
|
||||
f"Paired {child['name']} ({child['device_model']}, "
|
||||
f"{pretty_category(child['category'])}) with id {child['device_id']}"
|
||||
)
|
||||
|
||||
|
||||
@hub.command(name="unpair")
|
||||
@click.argument("device_id")
|
||||
@pass_dev
|
||||
async def hub_unpair(dev, device_id: str):
|
||||
"""Unpair given device."""
|
||||
cs = dev.modules[Module.ChildSetup]
|
||||
|
||||
# Accessing private here, as the property exposes only values
|
||||
if device_id not in dev._children:
|
||||
error(f"{dev} does not have children with identifier {device_id}")
|
||||
|
||||
res = await cs.unpair(device_id=device_id)
|
||||
# Give the device some time to update its internal state, just in case.
|
||||
await asyncio.sleep(1)
|
||||
await dev.update()
|
||||
|
||||
if device_id not in dev._children:
|
||||
echo(f"Unpaired {device_id}")
|
||||
else:
|
||||
error(f"Failed to unpair {device_id}")
|
||||
|
||||
return res
|
@ -66,7 +66,6 @@ class LazyGroup(click.Group):
|
||||
# check the result to make debugging easier
|
||||
if not isinstance(cmd_object, click.BaseCommand):
|
||||
raise ValueError(
|
||||
f"Lazy loading of {cmd_name} failed by returning "
|
||||
"a non-command object"
|
||||
f"Lazy loading of {cmd_name} failed by returning a non-command object"
|
||||
)
|
||||
return cmd_object
|
||||
|
@ -22,6 +22,7 @@ from .common import (
|
||||
CatchAllExceptions,
|
||||
echo,
|
||||
error,
|
||||
invoke_subcommand,
|
||||
json_formatter_cb,
|
||||
pass_dev_or_child,
|
||||
)
|
||||
@ -92,6 +93,8 @@ def _legacy_type_to_class(_type: str) -> Any:
|
||||
"hsv": "light",
|
||||
"temperature": "light",
|
||||
"effect": "light",
|
||||
"vacuum": "vacuum",
|
||||
"hub": "hub",
|
||||
},
|
||||
result_callback=json_formatter_cb,
|
||||
)
|
||||
@ -295,9 +298,10 @@ async def cli(
|
||||
echo("No host name given, trying discovery..")
|
||||
from .discover import discover
|
||||
|
||||
return await ctx.invoke(discover)
|
||||
return await invoke_subcommand(discover, ctx)
|
||||
|
||||
device_updated = False
|
||||
device_discovered = False
|
||||
|
||||
if type is not None and type not in {"smart", "camera"}:
|
||||
from kasa.deviceconfig import DeviceConfig
|
||||
@ -351,12 +355,14 @@ async def cli(
|
||||
return
|
||||
echo(f"Found hostname by alias: {dev.host}")
|
||||
device_updated = True
|
||||
else:
|
||||
else: # host will be set
|
||||
from .discover import discover
|
||||
|
||||
dev = await ctx.invoke(discover)
|
||||
if not dev:
|
||||
discovered = await invoke_subcommand(discover, ctx)
|
||||
if not discovered:
|
||||
error(f"Unable to create device for {host}")
|
||||
dev = discovered[host]
|
||||
device_discovered = True
|
||||
|
||||
# Skip update on specific commands, or if device factory,
|
||||
# that performs an update was used for the device.
|
||||
@ -372,11 +378,14 @@ async def cli(
|
||||
|
||||
ctx.obj = await ctx.with_async_resource(async_wrapped_device(dev))
|
||||
|
||||
if ctx.invoked_subcommand is None:
|
||||
# discover command has already invoked state
|
||||
if ctx.invoked_subcommand is None and not device_discovered:
|
||||
from .device import state
|
||||
|
||||
return await ctx.invoke(state)
|
||||
|
||||
return dev
|
||||
|
||||
|
||||
@cli.command()
|
||||
@pass_dev_or_child
|
||||
|
84
kasa/cli/vacuum.py
Normal file
84
kasa/cli/vacuum.py
Normal file
@ -0,0 +1,84 @@
|
||||
"""Module for cli vacuum commands.."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncclick as click
|
||||
|
||||
from kasa import (
|
||||
Device,
|
||||
Module,
|
||||
)
|
||||
|
||||
from .common import (
|
||||
error,
|
||||
pass_dev_or_child,
|
||||
)
|
||||
|
||||
|
||||
@click.group(invoke_without_command=False)
|
||||
@click.pass_context
|
||||
async def vacuum(ctx: click.Context) -> None:
|
||||
"""Vacuum commands."""
|
||||
|
||||
|
||||
@vacuum.group(invoke_without_command=True, name="records")
|
||||
@pass_dev_or_child
|
||||
async def records_group(dev: Device) -> None:
|
||||
"""Access cleaning records."""
|
||||
if not (rec := dev.modules.get(Module.CleanRecords)):
|
||||
error("This device does not support records.")
|
||||
|
||||
data = rec.parsed_data
|
||||
latest = data.last_clean
|
||||
click.echo(
|
||||
f"Totals: {rec.total_clean_area} {rec.area_unit} in {rec.total_clean_time} "
|
||||
f"(cleaned {rec.total_clean_count} times)"
|
||||
)
|
||||
click.echo(f"Last clean: {latest.clean_area} {rec.area_unit} @ {latest.clean_time}")
|
||||
click.echo("Execute `kasa vacuum records list` to list all records.")
|
||||
|
||||
|
||||
@records_group.command(name="list")
|
||||
@pass_dev_or_child
|
||||
async def records_list(dev: Device) -> None:
|
||||
"""List all cleaning records."""
|
||||
if not (rec := dev.modules.get(Module.CleanRecords)):
|
||||
error("This device does not support records.")
|
||||
|
||||
data = rec.parsed_data
|
||||
for record in data.records:
|
||||
click.echo(
|
||||
f"* {record.timestamp}: cleaned {record.clean_area} {rec.area_unit}"
|
||||
f" in {record.clean_time}"
|
||||
)
|
||||
|
||||
|
||||
@vacuum.group(invoke_without_command=True, name="consumables")
|
||||
@pass_dev_or_child
|
||||
@click.pass_context
|
||||
async def consumables(ctx: click.Context, dev: Device) -> None:
|
||||
"""List device consumables."""
|
||||
if not (cons := dev.modules.get(Module.Consumables)):
|
||||
error("This device does not support consumables.")
|
||||
|
||||
if not ctx.invoked_subcommand:
|
||||
for c in cons.consumables.values():
|
||||
click.echo(f"{c.name} ({c.id}): {c.used} used, {c.remaining} remaining")
|
||||
|
||||
|
||||
@consumables.command(name="reset")
|
||||
@click.argument("consumable_id", required=True)
|
||||
@pass_dev_or_child
|
||||
async def reset_consumable(dev: Device, consumable_id: str) -> None:
|
||||
"""Reset the consumable used/remaining time."""
|
||||
cons = dev.modules[Module.Consumables]
|
||||
|
||||
if consumable_id not in cons.consumables:
|
||||
error(
|
||||
f"Consumable {consumable_id} not found in "
|
||||
f"device consumables: {', '.join(cons.consumables.keys())}."
|
||||
)
|
||||
|
||||
await cons.reset_consumable(consumable_id)
|
||||
|
||||
click.echo(f"Consumable {consumable_id} reset")
|
@ -107,7 +107,7 @@ from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from abc import ABC, abstractmethod
|
||||
from collections.abc import Mapping, Sequence
|
||||
from collections.abc import Callable, Mapping, Sequence
|
||||
from dataclasses import dataclass
|
||||
from datetime import datetime, tzinfo
|
||||
from typing import TYPE_CHECKING, Any, TypeAlias
|
||||
@ -161,7 +161,7 @@ class DeviceInfo:
|
||||
device_type: DeviceType
|
||||
hardware_version: str
|
||||
firmware_version: str
|
||||
firmware_build: str
|
||||
firmware_build: str | None
|
||||
requires_auth: bool
|
||||
region: str | None
|
||||
|
||||
@ -537,19 +537,52 @@ class Device(ABC):
|
||||
|
||||
return None
|
||||
|
||||
def _get_deprecated_callable_attribute(self, name: str) -> Any | None:
|
||||
vals: dict[str, tuple[ModuleName, Callable[[Any], Any], str]] = {
|
||||
"is_dimmable": (
|
||||
Module.Light,
|
||||
lambda c: c.has_feature("brightness"),
|
||||
'light_module.has_feature("brightness")',
|
||||
),
|
||||
"is_color": (
|
||||
Module.Light,
|
||||
lambda c: c.has_feature("hsv"),
|
||||
'light_module.has_feature("hsv")',
|
||||
),
|
||||
"is_variable_color_temp": (
|
||||
Module.Light,
|
||||
lambda c: c.has_feature("color_temp"),
|
||||
'light_module.has_feature("color_temp")',
|
||||
),
|
||||
"valid_temperature_range": (
|
||||
Module.Light,
|
||||
lambda c: c._deprecated_valid_temperature_range(),
|
||||
'minimum and maximum value of get_feature("color_temp")',
|
||||
),
|
||||
"has_effects": (
|
||||
Module.Light,
|
||||
lambda c: Module.LightEffect in c._device.modules,
|
||||
"Module.LightEffect in device.modules",
|
||||
),
|
||||
}
|
||||
if mod_call_msg := vals.get(name):
|
||||
mod, call, msg = mod_call_msg
|
||||
msg = f"{name} is deprecated, use: {msg} instead"
|
||||
warn(msg, DeprecationWarning, stacklevel=2)
|
||||
if (module := self.modules.get(mod)) is None:
|
||||
raise AttributeError(f"Device has no attribute {name!r}")
|
||||
return call(module)
|
||||
|
||||
return None
|
||||
|
||||
_deprecated_other_attributes = {
|
||||
# light attributes
|
||||
"is_color": (Module.Light, ["is_color"]),
|
||||
"is_dimmable": (Module.Light, ["is_dimmable"]),
|
||||
"is_variable_color_temp": (Module.Light, ["is_variable_color_temp"]),
|
||||
"brightness": (Module.Light, ["brightness"]),
|
||||
"set_brightness": (Module.Light, ["set_brightness"]),
|
||||
"hsv": (Module.Light, ["hsv"]),
|
||||
"set_hsv": (Module.Light, ["set_hsv"]),
|
||||
"color_temp": (Module.Light, ["color_temp"]),
|
||||
"set_color_temp": (Module.Light, ["set_color_temp"]),
|
||||
"valid_temperature_range": (Module.Light, ["valid_temperature_range"]),
|
||||
"has_effects": (Module.Light, ["has_effects"]),
|
||||
"_deprecated_set_light_state": (Module.Light, ["has_effects"]),
|
||||
# led attributes
|
||||
"led": (Module.Led, ["led"]),
|
||||
@ -588,6 +621,9 @@ class Device(ABC):
|
||||
msg = f"{name} is deprecated, use device_type property instead"
|
||||
warn(msg, DeprecationWarning, stacklevel=2)
|
||||
return self.device_type == dep_device_type_attr[1]
|
||||
# callable
|
||||
if (result := self._get_deprecated_callable_attribute(name)) is not None:
|
||||
return result
|
||||
# Other deprecated attributes
|
||||
if (dep_attr := self._deprecated_other_attributes.get(name)) and (
|
||||
(replacing_attr := self._get_replacing_attr(dep_attr[0], *dep_attr[1]))
|
||||
|
@ -8,11 +8,10 @@ from typing import Any
|
||||
|
||||
from .device import Device
|
||||
from .device_type import DeviceType
|
||||
from .deviceconfig import DeviceConfig, DeviceFamily
|
||||
from .deviceconfig import DeviceConfig, DeviceEncryptionType, DeviceFamily
|
||||
from .exceptions import KasaException, UnsupportedDeviceError
|
||||
from .iot import (
|
||||
IotBulb,
|
||||
IotCamera,
|
||||
IotDevice,
|
||||
IotDimmer,
|
||||
IotLightStrip,
|
||||
@ -140,7 +139,8 @@ def get_device_class_from_sys_info(sysinfo: dict[str, Any]) -> type[IotDevice]:
|
||||
DeviceType.Strip: IotStrip,
|
||||
DeviceType.WallSwitch: IotWallSwitch,
|
||||
DeviceType.LightStrip: IotLightStrip,
|
||||
DeviceType.Camera: IotCamera,
|
||||
# Disabled until properly implemented
|
||||
# DeviceType.Camera: IotCamera,
|
||||
}
|
||||
return TYPE_TO_CLASS[IotDevice._get_device_type_from_sys_info(sysinfo)]
|
||||
|
||||
@ -159,10 +159,12 @@ def get_device_class_from_family(
|
||||
"SMART.KASAHUB": SmartDevice,
|
||||
"SMART.KASASWITCH": SmartDevice,
|
||||
"SMART.IPCAMERA.HTTPS": SmartCamDevice,
|
||||
"SMART.TAPOROBOVAC": SmartDevice,
|
||||
"SMART.TAPODOORBELL.HTTPS": SmartCamDevice,
|
||||
"SMART.TAPOROBOVAC.HTTPS": SmartDevice,
|
||||
"IOT.SMARTPLUGSWITCH": IotPlug,
|
||||
"IOT.SMARTBULB": IotBulb,
|
||||
"IOT.IPCAMERA": IotCamera,
|
||||
# Disabled until properly implemented
|
||||
# "IOT.IPCAMERA": IotCamera,
|
||||
}
|
||||
lookup_key = f"{device_type}{'.HTTPS' if https else ''}"
|
||||
if (
|
||||
@ -173,28 +175,45 @@ def get_device_class_from_family(
|
||||
_LOGGER.debug("Unknown SMART device with %s, using SmartDevice", device_type)
|
||||
cls = SmartDevice
|
||||
|
||||
if cls is not None:
|
||||
_LOGGER.debug("Using %s for %s", cls.__name__, device_type)
|
||||
|
||||
return cls
|
||||
|
||||
|
||||
def get_protocol(
|
||||
config: DeviceConfig,
|
||||
) -> BaseProtocol | None:
|
||||
"""Return the protocol from the connection name.
|
||||
def get_protocol(config: DeviceConfig, *, strict: bool = False) -> BaseProtocol | None:
|
||||
"""Return the protocol from the device config.
|
||||
|
||||
For cameras and vacuums the device family is a simple mapping to
|
||||
the protocol/transport. For other device types the transport varies
|
||||
based on the discovery information.
|
||||
|
||||
:param config: Device config to derive protocol
|
||||
:param strict: Require exact match on encrypt type
|
||||
"""
|
||||
_LOGGER.debug("Finding protocol for %s", config.host)
|
||||
ctype = config.connection_type
|
||||
protocol_name = ctype.device_family.value.split(".")[0]
|
||||
_LOGGER.debug("Finding protocol for %s", ctype.device_family)
|
||||
|
||||
if ctype.device_family is DeviceFamily.SmartIpCamera:
|
||||
if ctype.device_family in {
|
||||
DeviceFamily.SmartIpCamera,
|
||||
DeviceFamily.SmartTapoDoorbell,
|
||||
}:
|
||||
if strict and ctype.encryption_type is not DeviceEncryptionType.Aes:
|
||||
return None
|
||||
return SmartCamProtocol(transport=SslAesTransport(config=config))
|
||||
|
||||
if ctype.device_family is DeviceFamily.IotIpCamera:
|
||||
if strict and ctype.encryption_type is not DeviceEncryptionType.Xor:
|
||||
return None
|
||||
return IotProtocol(transport=LinkieTransportV2(config=config))
|
||||
|
||||
if ctype.device_family is DeviceFamily.SmartTapoRobovac:
|
||||
# Older FW used a different transport
|
||||
if (
|
||||
ctype.device_family is DeviceFamily.SmartTapoRobovac
|
||||
and ctype.encryption_type is DeviceEncryptionType.Aes
|
||||
):
|
||||
return SmartProtocol(transport=SslTransport(config=config))
|
||||
|
||||
protocol_transport_key = (
|
||||
@ -212,6 +231,7 @@ def get_protocol(
|
||||
"IOT.KLAP": (IotProtocol, KlapTransport),
|
||||
"SMART.AES": (SmartProtocol, AesTransport),
|
||||
"SMART.KLAP": (SmartProtocol, KlapTransportV2),
|
||||
"SMART.KLAP.HTTPS": (SmartProtocol, KlapTransportV2),
|
||||
# H200 is device family SMART.TAPOHUB and uses SmartCamProtocol so use
|
||||
# https to distuingish from SmartProtocol devices
|
||||
"SMART.AES.HTTPS": (SmartCamProtocol, SslAesTransport),
|
||||
|
@ -22,6 +22,8 @@ class DeviceType(Enum):
|
||||
Fan = "fan"
|
||||
Thermostat = "thermostat"
|
||||
Vacuum = "vacuum"
|
||||
Chime = "chime"
|
||||
Doorbell = "doorbell"
|
||||
Unknown = "unknown"
|
||||
|
||||
@staticmethod
|
||||
|
@ -20,7 +20,7 @@ None
|
||||
{'host': '127.0.0.3', 'timeout': 5, 'credentials': {'username': 'user@example.com', \
|
||||
'password': 'great_password'}, 'connection_type'\
|
||||
: {'device_family': 'SMART.TAPOBULB', 'encryption_type': 'KLAP', 'login_version': 2, \
|
||||
'https': False}, 'uses_http': True}
|
||||
'https': False, 'http_port': 80}}
|
||||
|
||||
>>> later_device = await Device.connect(config=Device.Config.from_dict(config_dict))
|
||||
>>> print(later_device.alias) # Alias is available as connect() calls update()
|
||||
@ -79,6 +79,8 @@ class DeviceFamily(Enum):
|
||||
SmartKasaHub = "SMART.KASAHUB"
|
||||
SmartIpCamera = "SMART.IPCAMERA"
|
||||
SmartTapoRobovac = "SMART.TAPOROBOVAC"
|
||||
SmartTapoChime = "SMART.TAPOCHIME"
|
||||
SmartTapoDoorbell = "SMART.TAPODOORBELL"
|
||||
|
||||
|
||||
class _DeviceConfigBaseMixin(DataClassJSONMixin):
|
||||
@ -98,13 +100,16 @@ class DeviceConnectionParameters(_DeviceConfigBaseMixin):
|
||||
encryption_type: DeviceEncryptionType
|
||||
login_version: int | None = None
|
||||
https: bool = False
|
||||
http_port: int | None = None
|
||||
|
||||
@staticmethod
|
||||
def from_values(
|
||||
device_family: str,
|
||||
encryption_type: str,
|
||||
*,
|
||||
login_version: int | None = None,
|
||||
https: bool | None = None,
|
||||
http_port: int | None = None,
|
||||
) -> DeviceConnectionParameters:
|
||||
"""Return connection parameters from string values."""
|
||||
try:
|
||||
@ -115,6 +120,7 @@ class DeviceConnectionParameters(_DeviceConfigBaseMixin):
|
||||
DeviceEncryptionType(encryption_type),
|
||||
login_version,
|
||||
https,
|
||||
http_port=http_port,
|
||||
)
|
||||
except (ValueError, TypeError) as ex:
|
||||
raise KasaException(
|
||||
@ -148,9 +154,12 @@ class DeviceConfig(_DeviceConfigBaseMixin):
|
||||
DeviceFamily.IotSmartPlugSwitch, DeviceEncryptionType.Xor
|
||||
)
|
||||
)
|
||||
#: True if the device uses http. Consumers should retrieve rather than set this
|
||||
#: in order to determine whether they should pass a custom http client if desired.
|
||||
uses_http: bool = False
|
||||
|
||||
@property
|
||||
def uses_http(self) -> bool:
|
||||
"""True if the device uses http."""
|
||||
ctype = self.connection_type
|
||||
return ctype.encryption_type is not DeviceEncryptionType.Xor or ctype.https
|
||||
|
||||
#: Set a custom http_client for the device to use.
|
||||
http_client: ClientSession | None = field(
|
||||
|
115
kasa/discover.py
115
kasa/discover.py
@ -22,7 +22,7 @@ Discovery returns a dict of {ip: discovered devices}:
|
||||
>>>
|
||||
>>> found_devices = await Discover.discover()
|
||||
>>> [dev.model for dev in found_devices.values()]
|
||||
['KP303', 'HS110', 'L530E', 'KL430', 'HS220']
|
||||
['KP303', 'HS110', 'L530E', 'KL430', 'HS220', 'H200']
|
||||
|
||||
You can pass username and password for devices requiring authentication
|
||||
|
||||
@ -31,21 +31,21 @@ You can pass username and password for devices requiring authentication
|
||||
>>> password="great_password",
|
||||
>>> )
|
||||
>>> print(len(devices))
|
||||
5
|
||||
6
|
||||
|
||||
You can also pass a :class:`kasa.Credentials`
|
||||
|
||||
>>> creds = Credentials("user@example.com", "great_password")
|
||||
>>> devices = await Discover.discover(credentials=creds)
|
||||
>>> print(len(devices))
|
||||
5
|
||||
6
|
||||
|
||||
Discovery can also be targeted to a specific broadcast address instead of
|
||||
the default 255.255.255.255:
|
||||
|
||||
>>> found_devices = await Discover.discover(target="127.0.0.255", credentials=creds)
|
||||
>>> print(len(found_devices))
|
||||
5
|
||||
6
|
||||
|
||||
Basic information is available on the device from the discovery broadcast response
|
||||
but it is important to call device.update() after discovery if you want to access
|
||||
@ -70,6 +70,7 @@ Discovered Bedroom Lamp Plug (model: HS110)
|
||||
Discovered Living Room Bulb (model: L530)
|
||||
Discovered Bedroom Lightstrip (model: KL430)
|
||||
Discovered Living Room Dimmer Switch (model: HS220)
|
||||
Discovered Tapo Hub (model: H200)
|
||||
|
||||
Discovering a single device returns a kasa.Device object.
|
||||
|
||||
@ -146,6 +147,7 @@ class ConnectAttempt(NamedTuple):
|
||||
protocol: type
|
||||
transport: type
|
||||
device: type
|
||||
https: bool
|
||||
|
||||
|
||||
class DiscoveredMeta(TypedDict):
|
||||
@ -360,7 +362,6 @@ class _DiscoverProtocol(asyncio.DatagramProtocol):
|
||||
json_func = Discover._get_discovery_json_legacy
|
||||
device_func = Discover._get_device_instance_legacy
|
||||
elif port == Discover.DISCOVERY_PORT_2:
|
||||
config.uses_http = True
|
||||
json_func = Discover._get_discovery_json
|
||||
device_func = Discover._get_device_instance
|
||||
else:
|
||||
@ -634,12 +635,14 @@ class Discover:
|
||||
Device.Family.SmartTapoPlug,
|
||||
Device.Family.IotSmartPlugSwitch,
|
||||
Device.Family.SmartIpCamera,
|
||||
Device.Family.SmartTapoRobovac,
|
||||
Device.Family.IotIpCamera,
|
||||
}
|
||||
candidates: dict[
|
||||
tuple[type[BaseProtocol], type[BaseTransport], type[Device]],
|
||||
tuple[type[BaseProtocol], type[BaseTransport], type[Device], bool],
|
||||
tuple[BaseProtocol, DeviceConfig],
|
||||
] = {
|
||||
(type(protocol), type(protocol._transport), device_class): (
|
||||
(type(protocol), type(protocol._transport), device_class, https): (
|
||||
protocol,
|
||||
config,
|
||||
)
|
||||
@ -663,10 +666,9 @@ class Discover:
|
||||
port_override=port,
|
||||
credentials=credentials,
|
||||
http_client=http_client,
|
||||
uses_http=encrypt is not Device.EncryptionType.Xor,
|
||||
)
|
||||
)
|
||||
and (protocol := get_protocol(config))
|
||||
and (protocol := get_protocol(config, strict=True))
|
||||
and (
|
||||
device_class := get_device_class_from_family(
|
||||
device_family.value, https=https, require_exact=True
|
||||
@ -676,9 +678,14 @@ class Discover:
|
||||
for key, val in candidates.items():
|
||||
try:
|
||||
prot, config = val
|
||||
_LOGGER.debug("Trying to connect with %s", prot.__class__.__name__)
|
||||
dev = await _connect(config, prot)
|
||||
except Exception:
|
||||
_LOGGER.debug("Unable to connect with %s", prot)
|
||||
except Exception as ex:
|
||||
_LOGGER.debug(
|
||||
"Unable to connect with %s: %s",
|
||||
prot.__class__.__name__,
|
||||
ex,
|
||||
)
|
||||
if on_attempt:
|
||||
ca = tuple.__new__(ConnectAttempt, key)
|
||||
on_attempt(ca, False)
|
||||
@ -686,6 +693,7 @@ class Discover:
|
||||
if on_attempt:
|
||||
ca = tuple.__new__(ConnectAttempt, key)
|
||||
on_attempt(ca, True)
|
||||
_LOGGER.debug("Found working protocol %s", prot.__class__.__name__)
|
||||
return dev
|
||||
finally:
|
||||
await prot.close()
|
||||
@ -792,6 +800,47 @@ class Discover:
|
||||
) from ex
|
||||
return info
|
||||
|
||||
@staticmethod
|
||||
def _get_connection_parameters(
|
||||
discovery_result: DiscoveryResult,
|
||||
) -> DeviceConnectionParameters:
|
||||
"""Get connection parameters from the discovery result."""
|
||||
type_ = discovery_result.device_type
|
||||
if (encrypt_schm := discovery_result.mgt_encrypt_schm) is None:
|
||||
raise UnsupportedDeviceError(
|
||||
f"Unsupported device {discovery_result.ip} of type {type_} "
|
||||
"with no mgt_encrypt_schm",
|
||||
discovery_result=discovery_result.to_dict(),
|
||||
host=discovery_result.ip,
|
||||
)
|
||||
|
||||
if not (encrypt_type := encrypt_schm.encrypt_type) and (
|
||||
encrypt_info := discovery_result.encrypt_info
|
||||
):
|
||||
encrypt_type = encrypt_info.sym_schm
|
||||
|
||||
if not (login_version := encrypt_schm.lv) and (
|
||||
et := discovery_result.encrypt_type
|
||||
):
|
||||
# Known encrypt types are ["1","2"] and ["3"]
|
||||
# Reuse the login_version attribute to pass the max to transport
|
||||
login_version = max([int(i) for i in et])
|
||||
|
||||
if not encrypt_type:
|
||||
raise UnsupportedDeviceError(
|
||||
f"Unsupported device {discovery_result.ip} of type {type_} "
|
||||
+ "with no encryption type",
|
||||
discovery_result=discovery_result.to_dict(),
|
||||
host=discovery_result.ip,
|
||||
)
|
||||
return DeviceConnectionParameters.from_values(
|
||||
type_,
|
||||
encrypt_type,
|
||||
login_version=login_version,
|
||||
https=encrypt_schm.is_support_https,
|
||||
http_port=encrypt_schm.http_port,
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def _get_device_instance(
|
||||
info: dict,
|
||||
@ -831,54 +880,22 @@ class Discover:
|
||||
config.host,
|
||||
redact_data(info, NEW_DISCOVERY_REDACTORS),
|
||||
)
|
||||
|
||||
type_ = discovery_result.device_type
|
||||
if (encrypt_schm := discovery_result.mgt_encrypt_schm) is None:
|
||||
raise UnsupportedDeviceError(
|
||||
f"Unsupported device {config.host} of type {type_} "
|
||||
"with no mgt_encrypt_schm",
|
||||
discovery_result=discovery_result.to_dict(),
|
||||
host=config.host,
|
||||
)
|
||||
|
||||
try:
|
||||
if not (encrypt_type := encrypt_schm.encrypt_type) and (
|
||||
encrypt_info := discovery_result.encrypt_info
|
||||
):
|
||||
encrypt_type = encrypt_info.sym_schm
|
||||
|
||||
if not (login_version := encrypt_schm.lv) and (
|
||||
et := discovery_result.encrypt_type
|
||||
):
|
||||
# Known encrypt types are ["1","2"] and ["3"]
|
||||
# Reuse the login_version attribute to pass the max to transport
|
||||
login_version = max([int(i) for i in et])
|
||||
|
||||
if not encrypt_type:
|
||||
raise UnsupportedDeviceError(
|
||||
f"Unsupported device {config.host} of type {type_} "
|
||||
+ "with no encryption type",
|
||||
discovery_result=discovery_result.to_dict(),
|
||||
host=config.host,
|
||||
)
|
||||
config.connection_type = DeviceConnectionParameters.from_values(
|
||||
type_,
|
||||
encrypt_type,
|
||||
login_version,
|
||||
encrypt_schm.is_support_https,
|
||||
)
|
||||
conn_params = Discover._get_connection_parameters(discovery_result)
|
||||
config.connection_type = conn_params
|
||||
except KasaException as ex:
|
||||
if isinstance(ex, UnsupportedDeviceError):
|
||||
raise
|
||||
raise UnsupportedDeviceError(
|
||||
f"Unsupported device {config.host} of type {type_} "
|
||||
+ f"with encrypt_type {encrypt_schm.encrypt_type}",
|
||||
+ f"with encrypt_scheme {discovery_result.mgt_encrypt_schm}",
|
||||
discovery_result=discovery_result.to_dict(),
|
||||
host=config.host,
|
||||
) from ex
|
||||
|
||||
if (
|
||||
device_class := get_device_class_from_family(
|
||||
type_, https=encrypt_schm.is_support_https
|
||||
)
|
||||
device_class := get_device_class_from_family(type_, https=conn_params.https)
|
||||
) is None:
|
||||
_LOGGER.debug("Got unsupported device type: %s", type_)
|
||||
raise UnsupportedDeviceError(
|
||||
|
@ -127,6 +127,8 @@ class SmartErrorCode(IntEnum):
|
||||
DST_ERROR = -2301
|
||||
DST_SAVE_ERROR = -2302
|
||||
|
||||
VACUUM_BATTERY_LOW = -3001
|
||||
|
||||
SYSTEM_ERROR = -40101
|
||||
INVALID_ARGUMENTS = -40209
|
||||
|
||||
|
@ -25,6 +25,7 @@ Signal Level (signal_level): 2
|
||||
RSSI (rssi): -52
|
||||
SSID (ssid): #MASKED_SSID#
|
||||
Reboot (reboot): <Action>
|
||||
Device time (device_time): 2024-02-23 02:40:15+01:00
|
||||
Brightness (brightness): 100
|
||||
Cloud connection (cloud_connection): True
|
||||
HSV (hsv): HSV(hue=0, saturation=100, value=100)
|
||||
@ -39,7 +40,6 @@ Light preset (light_preset): Not set
|
||||
Smooth transition on (smooth_transition_on): 2
|
||||
Smooth transition off (smooth_transition_off): 2
|
||||
Overheated (overheated): False
|
||||
Device time (device_time): 2024-02-23 02:40:15+01:00
|
||||
|
||||
To see whether a device supports a feature, check for the existence of it:
|
||||
|
||||
@ -76,6 +76,7 @@ from typing import TYPE_CHECKING, Any
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .device import Device
|
||||
from .module import Module
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@ -142,7 +143,7 @@ class Feature:
|
||||
#: Callable coroutine or name of the method that allows changing the value
|
||||
attribute_setter: str | Callable[..., Coroutine[Any, Any, Any]] | None = None
|
||||
#: Container storing the data, this overrides 'device' for getters
|
||||
container: Any = None
|
||||
container: Device | Module | None = None
|
||||
#: Icon suggestion
|
||||
icon: str | None = None
|
||||
#: Attribute containing the name of the unit getter property.
|
||||
@ -255,7 +256,7 @@ class Feature:
|
||||
elif self.type == Feature.Type.Choice: # noqa: SIM102
|
||||
if not self.choices or value not in self.choices:
|
||||
raise ValueError(
|
||||
f"Unexpected value for {self.name}: {value}"
|
||||
f"Unexpected value for {self.name}: '{value}'"
|
||||
f" - allowed: {self.choices}"
|
||||
)
|
||||
|
||||
@ -278,7 +279,18 @@ class Feature:
|
||||
return f"Unable to read value ({self.id}): {ex}"
|
||||
|
||||
if self.type == Feature.Type.Choice:
|
||||
if not isinstance(choices, list) or value not in choices:
|
||||
if not isinstance(choices, list):
|
||||
_LOGGER.error(
|
||||
"Choices are not properly defined for %s (%s). Type: <%s> Value: %s", # noqa: E501
|
||||
self.name,
|
||||
self.id,
|
||||
type(choices),
|
||||
choices,
|
||||
)
|
||||
return f"{self.name} ({self.id}): improperly defined choice set."
|
||||
if (value not in choices) and (
|
||||
isinstance(value, Enum) and value.name not in choices
|
||||
):
|
||||
_LOGGER.warning(
|
||||
"Invalid value for for choice %s (%s): %s not in %s",
|
||||
self.name,
|
||||
@ -290,7 +302,13 @@ class Feature:
|
||||
f"{self.name} ({self.id}): invalid value '{value}' not in {choices}"
|
||||
)
|
||||
value = " ".join(
|
||||
[f"*{choice}*" if choice == value else choice for choice in choices]
|
||||
[
|
||||
f"*{choice}*"
|
||||
if choice == value
|
||||
or (isinstance(value, Enum) and choice == value.name)
|
||||
else f"{choice}"
|
||||
for choice in choices
|
||||
]
|
||||
)
|
||||
if self.precision_hint is not None and isinstance(value, float):
|
||||
value = round(value, self.precision_hint)
|
||||
@ -298,8 +316,10 @@ class Feature:
|
||||
if isinstance(value, Enum):
|
||||
value = repr(value)
|
||||
s = f"{self.name} ({self.id}): {value}"
|
||||
if self.unit is not None:
|
||||
s += f" {self.unit}"
|
||||
if (unit := self.unit) is not None:
|
||||
if isinstance(unit, Enum):
|
||||
unit = repr(unit)
|
||||
s += f" {unit}"
|
||||
|
||||
if self.type == Feature.Type.Number:
|
||||
s += f" (range: {self.minimum_value}-{self.maximum_value})"
|
||||
|
@ -1,5 +1,7 @@
|
||||
"""Package for interfaces."""
|
||||
|
||||
from .alarm import Alarm
|
||||
from .childsetup import ChildSetup
|
||||
from .energy import Energy
|
||||
from .fan import Fan
|
||||
from .led import Led
|
||||
@ -10,6 +12,8 @@ from .thermostat import Thermostat, ThermostatState
|
||||
from .time import Time
|
||||
|
||||
__all__ = [
|
||||
"Alarm",
|
||||
"ChildSetup",
|
||||
"Fan",
|
||||
"Energy",
|
||||
"Led",
|
||||
|
75
kasa/interfaces/alarm.py
Normal file
75
kasa/interfaces/alarm.py
Normal file
@ -0,0 +1,75 @@
|
||||
"""Module for base alarm module."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import Annotated
|
||||
|
||||
from ..module import FeatureAttribute, Module
|
||||
|
||||
|
||||
class Alarm(Module, ABC):
|
||||
"""Base interface to represent an alarm module."""
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
def alarm_sound(self) -> Annotated[str, FeatureAttribute()]:
|
||||
"""Return current alarm sound."""
|
||||
|
||||
@abstractmethod
|
||||
async def set_alarm_sound(self, sound: str) -> Annotated[dict, FeatureAttribute()]:
|
||||
"""Set alarm sound.
|
||||
|
||||
See *alarm_sounds* for list of available sounds.
|
||||
"""
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
def alarm_sounds(self) -> list[str]:
|
||||
"""Return list of available alarm sounds."""
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
def alarm_volume(self) -> Annotated[int, FeatureAttribute()]:
|
||||
"""Return alarm volume."""
|
||||
|
||||
@abstractmethod
|
||||
async def set_alarm_volume(
|
||||
self, volume: int
|
||||
) -> Annotated[dict, FeatureAttribute()]:
|
||||
"""Set alarm volume."""
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
def alarm_duration(self) -> Annotated[int, FeatureAttribute()]:
|
||||
"""Return alarm duration."""
|
||||
|
||||
@abstractmethod
|
||||
async def set_alarm_duration(
|
||||
self, duration: int
|
||||
) -> Annotated[dict, FeatureAttribute()]:
|
||||
"""Set alarm duration."""
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
def active(self) -> bool:
|
||||
"""Return true if alarm is active."""
|
||||
|
||||
@abstractmethod
|
||||
async def play(
|
||||
self,
|
||||
*,
|
||||
duration: int | None = None,
|
||||
volume: int | None = None,
|
||||
sound: str | None = None,
|
||||
) -> dict:
|
||||
"""Play alarm.
|
||||
|
||||
The optional *duration*, *volume*, and *sound* to override the device settings.
|
||||
*duration* is in seconds.
|
||||
See *alarm_sounds* for the list of sounds available for the device.
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
async def stop(self) -> dict:
|
||||
"""Stop alarm."""
|
70
kasa/interfaces/childsetup.py
Normal file
70
kasa/interfaces/childsetup.py
Normal file
@ -0,0 +1,70 @@
|
||||
"""Module for childsetup interface.
|
||||
|
||||
The childsetup module allows pairing and unpairing of supported child device types to
|
||||
hubs.
|
||||
|
||||
>>> from kasa import Discover, Module, LightState
|
||||
>>>
|
||||
>>> dev = await Discover.discover_single(
|
||||
>>> "127.0.0.6",
|
||||
>>> username="user@example.com",
|
||||
>>> password="great_password"
|
||||
>>> )
|
||||
>>> await dev.update()
|
||||
>>> print(dev.alias)
|
||||
Tapo Hub
|
||||
|
||||
>>> childsetup = dev.modules[Module.ChildSetup]
|
||||
>>> childsetup.supported_categories
|
||||
['camera', 'subg.trv', 'subg.trigger', 'subg.plugswitch']
|
||||
|
||||
Put child devices in pairing mode.
|
||||
The hub will pair with all supported devices in pairing mode:
|
||||
|
||||
>>> added = await childsetup.pair()
|
||||
>>> added
|
||||
[{'device_id': 'SCRUBBED_CHILD_DEVICE_ID_5', 'category': 'subg.trigger.button', \
|
||||
'device_model': 'S200B', 'name': 'I01BU0tFRF9OQU1FIw===='}]
|
||||
|
||||
>>> for child in dev.children:
|
||||
>>> print(f"{child.device_id} - {child.model}")
|
||||
SCRUBBED_CHILD_DEVICE_ID_1 - T310
|
||||
SCRUBBED_CHILD_DEVICE_ID_2 - T315
|
||||
SCRUBBED_CHILD_DEVICE_ID_3 - T110
|
||||
SCRUBBED_CHILD_DEVICE_ID_4 - S200B
|
||||
SCRUBBED_CHILD_DEVICE_ID_5 - S200B
|
||||
|
||||
Unpair with the child `device_id`:
|
||||
|
||||
>>> await childsetup.unpair("SCRUBBED_CHILD_DEVICE_ID_4")
|
||||
>>> for child in dev.children:
|
||||
>>> print(f"{child.device_id} - {child.model}")
|
||||
SCRUBBED_CHILD_DEVICE_ID_1 - T310
|
||||
SCRUBBED_CHILD_DEVICE_ID_2 - T315
|
||||
SCRUBBED_CHILD_DEVICE_ID_3 - T110
|
||||
SCRUBBED_CHILD_DEVICE_ID_5 - S200B
|
||||
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from abc import ABC, abstractmethod
|
||||
|
||||
from ..module import Module
|
||||
|
||||
|
||||
class ChildSetup(Module, ABC):
|
||||
"""Interface for child setup on hubs."""
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
def supported_categories(self) -> list[str]:
|
||||
"""Supported child device categories."""
|
||||
|
||||
@abstractmethod
|
||||
async def pair(self, *, timeout: int = 10) -> list[dict]:
|
||||
"""Scan for new devices and pair them."""
|
||||
|
||||
@abstractmethod
|
||||
async def unpair(self, device_id: str) -> dict:
|
||||
"""Remove device from the hub."""
|
@ -28,7 +28,7 @@ class Energy(Module, ABC):
|
||||
|
||||
_supported: ModuleFeature = ModuleFeature(0)
|
||||
|
||||
def supports(self, module_feature: ModuleFeature) -> bool:
|
||||
def supports(self, module_feature: Energy.ModuleFeature) -> bool:
|
||||
"""Return True if module supports the feature."""
|
||||
return module_feature in self._supported
|
||||
|
||||
|
@ -65,8 +65,10 @@ from __future__ import annotations
|
||||
|
||||
from abc import ABC, abstractmethod
|
||||
from dataclasses import dataclass
|
||||
from typing import Annotated, NamedTuple
|
||||
from typing import TYPE_CHECKING, Annotated, Any, NamedTuple
|
||||
from warnings import warn
|
||||
|
||||
from ..exceptions import KasaException
|
||||
from ..module import FeatureAttribute, Module
|
||||
|
||||
|
||||
@ -100,34 +102,6 @@ class HSV(NamedTuple):
|
||||
class Light(Module, ABC):
|
||||
"""Base class for TP-Link Light."""
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
def is_dimmable(self) -> bool:
|
||||
"""Whether the light supports brightness changes."""
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
def is_color(self) -> bool:
|
||||
"""Whether the bulb supports color changes."""
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
def is_variable_color_temp(self) -> bool:
|
||||
"""Whether the bulb supports color temperature changes."""
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
def valid_temperature_range(self) -> ColorTempRange:
|
||||
"""Return the device-specific white temperature range (in Kelvin).
|
||||
|
||||
:return: White temperature range in Kelvin (minimum, maximum)
|
||||
"""
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
def has_effects(self) -> bool:
|
||||
"""Return True if the device supports effects."""
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
def hsv(self) -> Annotated[HSV, FeatureAttribute()]:
|
||||
@ -197,3 +171,44 @@ class Light(Module, ABC):
|
||||
@abstractmethod
|
||||
async def set_state(self, state: LightState) -> dict:
|
||||
"""Set the light state."""
|
||||
|
||||
def _deprecated_valid_temperature_range(self) -> ColorTempRange:
|
||||
if not (temp := self.get_feature("color_temp")):
|
||||
raise KasaException("Color temperature not supported")
|
||||
return ColorTempRange(temp.minimum_value, temp.maximum_value)
|
||||
|
||||
def _deprecated_attributes(self, dep_name: str) -> str | None:
|
||||
map: dict[str, str] = {
|
||||
"is_color": "hsv",
|
||||
"is_dimmable": "brightness",
|
||||
"is_variable_color_temp": "color_temp",
|
||||
}
|
||||
return map.get(dep_name)
|
||||
|
||||
if not TYPE_CHECKING:
|
||||
|
||||
def __getattr__(self, name: str) -> Any:
|
||||
if name == "valid_temperature_range":
|
||||
msg = (
|
||||
"valid_temperature_range is deprecated, use "
|
||||
'get_feature("color_temp") minimum_value '
|
||||
" and maximum_value instead"
|
||||
)
|
||||
warn(msg, DeprecationWarning, stacklevel=2)
|
||||
res = self._deprecated_valid_temperature_range()
|
||||
return res
|
||||
|
||||
if name == "has_effects":
|
||||
msg = (
|
||||
"has_effects is deprecated, check `Module.LightEffect "
|
||||
"in device.modules` instead"
|
||||
)
|
||||
warn(msg, DeprecationWarning, stacklevel=2)
|
||||
return Module.LightEffect in self._device.modules
|
||||
|
||||
if attr := self._deprecated_attributes(name):
|
||||
msg = f'{name} is deprecated, use has_feature("{attr}") instead'
|
||||
warn(msg, DeprecationWarning, stacklevel=2)
|
||||
return self.has_feature(attr)
|
||||
|
||||
raise AttributeError(f"Energy module has no attribute {name!r}")
|
||||
|
@ -51,6 +51,7 @@ class LightEffect(Module, ABC):
|
||||
"""Interface to represent a light effect module."""
|
||||
|
||||
LIGHT_EFFECTS_OFF = "Off"
|
||||
LIGHT_EFFECTS_UNNAMED_CUSTOM = "Custom"
|
||||
|
||||
def _initialize_features(self) -> None:
|
||||
"""Initialize features."""
|
||||
@ -77,7 +78,7 @@ class LightEffect(Module, ABC):
|
||||
@property
|
||||
@abstractmethod
|
||||
def effect(self) -> str:
|
||||
"""Return effect state or name."""
|
||||
"""Return effect name."""
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
|
@ -760,7 +760,10 @@ class IotDevice(Device):
|
||||
device_family = sys_info.get("type", sys_info.get("mic_type"))
|
||||
device_type = IotDevice._get_device_type_from_sys_info(info)
|
||||
fw_version_full = sys_info["sw_ver"]
|
||||
firmware_version, firmware_build = fw_version_full.split(" ", maxsplit=1)
|
||||
if " " in fw_version_full:
|
||||
firmware_version, firmware_build = fw_version_full.split(" ", maxsplit=1)
|
||||
else:
|
||||
firmware_version, firmware_build = fw_version_full, None
|
||||
auth = bool(discovery_info and ("mgt_encrypt_schm" in discovery_info))
|
||||
|
||||
return DeviceInfo(
|
||||
|
@ -11,7 +11,7 @@ from ..module import Module
|
||||
from ..protocols import BaseProtocol
|
||||
from .iotdevice import KasaException, requires_update
|
||||
from .iotplug import IotPlug
|
||||
from .modules import AmbientLight, Light, Motion
|
||||
from .modules import AmbientLight, Dimmer, Light, Motion
|
||||
|
||||
|
||||
class ButtonAction(Enum):
|
||||
@ -87,6 +87,7 @@ class IotDimmer(IotPlug):
|
||||
# TODO: need to be figured out what's the best approach to detect support
|
||||
self.add_module(Module.IotMotion, Motion(self, "smartlife.iot.PIR"))
|
||||
self.add_module(Module.IotAmbientLight, AmbientLight(self, "smartlife.iot.LAS"))
|
||||
self.add_module(Module.IotDimmer, Dimmer(self, "smartlife.iot.dimmer"))
|
||||
self.add_module(Module.Light, Light(self, "light"))
|
||||
|
||||
@property # type: ignore
|
||||
@ -115,9 +116,7 @@ class IotDimmer(IotPlug):
|
||||
raise KasaException("Device is not dimmable.")
|
||||
|
||||
if not isinstance(brightness, int):
|
||||
raise ValueError(
|
||||
"Brightness must be integer, " "not of %s.", type(brightness)
|
||||
)
|
||||
raise ValueError("Brightness must be integer, not of %s.", type(brightness))
|
||||
|
||||
if not 0 <= brightness <= 100:
|
||||
raise ValueError(
|
||||
|
@ -161,11 +161,17 @@ class IotStrip(IotDevice):
|
||||
|
||||
async def turn_on(self, **kwargs) -> dict:
|
||||
"""Turn the strip on."""
|
||||
return await self._query_helper("system", "set_relay_state", {"state": 1})
|
||||
for plug in self.children:
|
||||
if plug.is_off:
|
||||
await plug.turn_on()
|
||||
return {}
|
||||
|
||||
async def turn_off(self, **kwargs) -> dict:
|
||||
"""Turn the strip off."""
|
||||
return await self._query_helper("system", "set_relay_state", {"state": 0})
|
||||
for plug in self.children:
|
||||
if plug.is_on:
|
||||
await plug.turn_off()
|
||||
return {}
|
||||
|
||||
@property # type: ignore
|
||||
@requires_update
|
||||
|
@ -4,6 +4,7 @@ from .ambientlight import AmbientLight
|
||||
from .antitheft import Antitheft
|
||||
from .cloud import Cloud
|
||||
from .countdown import Countdown
|
||||
from .dimmer import Dimmer
|
||||
from .emeter import Emeter
|
||||
from .led import Led
|
||||
from .light import Light
|
||||
@ -20,6 +21,7 @@ __all__ = [
|
||||
"Antitheft",
|
||||
"Cloud",
|
||||
"Countdown",
|
||||
"Dimmer",
|
||||
"Emeter",
|
||||
"Led",
|
||||
"Light",
|
||||
|
270
kasa/iot/modules/dimmer.py
Normal file
270
kasa/iot/modules/dimmer.py
Normal file
@ -0,0 +1,270 @@
|
||||
"""Implementation of the dimmer config module found in dimmers."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from datetime import timedelta
|
||||
from typing import Any, Final, cast
|
||||
|
||||
from ...exceptions import KasaException
|
||||
from ...feature import Feature
|
||||
from ..iotmodule import IotModule, merge
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def _td_to_ms(td: timedelta) -> int:
|
||||
"""
|
||||
Convert timedelta to integer milliseconds.
|
||||
|
||||
Uses default float to integer rounding.
|
||||
"""
|
||||
return int(td / timedelta(milliseconds=1))
|
||||
|
||||
|
||||
class Dimmer(IotModule):
|
||||
"""Implements the dimmer config module."""
|
||||
|
||||
THRESHOLD_ABS_MIN: Final[int] = 0
|
||||
# Strange value, but verified against hardware (KS220).
|
||||
THRESHOLD_ABS_MAX: Final[int] = 51
|
||||
FADE_TIME_ABS_MIN: Final[timedelta] = timedelta(seconds=0)
|
||||
# Arbitrary, but set low intending GENTLE FADE for longer fades.
|
||||
FADE_TIME_ABS_MAX: Final[timedelta] = timedelta(seconds=10)
|
||||
GENTLE_TIME_ABS_MIN: Final[timedelta] = timedelta(seconds=0)
|
||||
# Arbitrary, but reasonable default.
|
||||
GENTLE_TIME_ABS_MAX: Final[timedelta] = timedelta(seconds=120)
|
||||
# Verified against KS220.
|
||||
RAMP_RATE_ABS_MIN: Final[int] = 10
|
||||
# Verified against KS220.
|
||||
RAMP_RATE_ABS_MAX: Final[int] = 50
|
||||
|
||||
def _initialize_features(self) -> None:
|
||||
"""Initialize features after the initial update."""
|
||||
self._add_feature(
|
||||
Feature(
|
||||
device=self._device,
|
||||
container=self,
|
||||
id="dimmer_threshold_min",
|
||||
name="Minimum dimming level",
|
||||
icon="mdi:lightbulb-on-20",
|
||||
attribute_getter="threshold_min",
|
||||
attribute_setter="set_threshold_min",
|
||||
range_getter=lambda: (self.THRESHOLD_ABS_MIN, self.THRESHOLD_ABS_MAX),
|
||||
type=Feature.Type.Number,
|
||||
category=Feature.Category.Config,
|
||||
)
|
||||
)
|
||||
|
||||
self._add_feature(
|
||||
Feature(
|
||||
device=self._device,
|
||||
container=self,
|
||||
id="dimmer_fade_off_time",
|
||||
name="Dimmer fade off time",
|
||||
icon="mdi:clock-in",
|
||||
attribute_getter="fade_off_time",
|
||||
attribute_setter="set_fade_off_time",
|
||||
range_getter=lambda: (
|
||||
_td_to_ms(self.FADE_TIME_ABS_MIN),
|
||||
_td_to_ms(self.FADE_TIME_ABS_MAX),
|
||||
),
|
||||
type=Feature.Type.Number,
|
||||
category=Feature.Category.Config,
|
||||
)
|
||||
)
|
||||
|
||||
self._add_feature(
|
||||
Feature(
|
||||
device=self._device,
|
||||
container=self,
|
||||
id="dimmer_fade_on_time",
|
||||
name="Dimmer fade on time",
|
||||
icon="mdi:clock-out",
|
||||
attribute_getter="fade_on_time",
|
||||
attribute_setter="set_fade_on_time",
|
||||
range_getter=lambda: (
|
||||
_td_to_ms(self.FADE_TIME_ABS_MIN),
|
||||
_td_to_ms(self.FADE_TIME_ABS_MAX),
|
||||
),
|
||||
type=Feature.Type.Number,
|
||||
category=Feature.Category.Config,
|
||||
)
|
||||
)
|
||||
|
||||
self._add_feature(
|
||||
Feature(
|
||||
device=self._device,
|
||||
container=self,
|
||||
id="dimmer_gentle_off_time",
|
||||
name="Dimmer gentle off time",
|
||||
icon="mdi:clock-in",
|
||||
attribute_getter="gentle_off_time",
|
||||
attribute_setter="set_gentle_off_time",
|
||||
range_getter=lambda: (
|
||||
_td_to_ms(self.GENTLE_TIME_ABS_MIN),
|
||||
_td_to_ms(self.GENTLE_TIME_ABS_MAX),
|
||||
),
|
||||
type=Feature.Type.Number,
|
||||
category=Feature.Category.Config,
|
||||
)
|
||||
)
|
||||
|
||||
self._add_feature(
|
||||
Feature(
|
||||
device=self._device,
|
||||
container=self,
|
||||
id="dimmer_gentle_on_time",
|
||||
name="Dimmer gentle on time",
|
||||
icon="mdi:clock-out",
|
||||
attribute_getter="gentle_on_time",
|
||||
attribute_setter="set_gentle_on_time",
|
||||
range_getter=lambda: (
|
||||
_td_to_ms(self.GENTLE_TIME_ABS_MIN),
|
||||
_td_to_ms(self.GENTLE_TIME_ABS_MAX),
|
||||
),
|
||||
type=Feature.Type.Number,
|
||||
category=Feature.Category.Config,
|
||||
)
|
||||
)
|
||||
|
||||
self._add_feature(
|
||||
Feature(
|
||||
device=self._device,
|
||||
container=self,
|
||||
id="dimmer_ramp_rate",
|
||||
name="Dimmer ramp rate",
|
||||
icon="mdi:clock-fast",
|
||||
attribute_getter="ramp_rate",
|
||||
attribute_setter="set_ramp_rate",
|
||||
range_getter=lambda: (self.RAMP_RATE_ABS_MIN, self.RAMP_RATE_ABS_MAX),
|
||||
type=Feature.Type.Number,
|
||||
category=Feature.Category.Config,
|
||||
)
|
||||
)
|
||||
|
||||
def query(self) -> dict:
|
||||
"""Request Dimming configuration."""
|
||||
req = merge(
|
||||
self.query_for_command("get_dimmer_parameters"),
|
||||
self.query_for_command("get_default_behavior"),
|
||||
)
|
||||
|
||||
return req
|
||||
|
||||
@property
|
||||
def config(self) -> dict[str, Any]:
|
||||
"""Return current configuration."""
|
||||
return self.data["get_dimmer_parameters"]
|
||||
|
||||
@property
|
||||
def threshold_min(self) -> int:
|
||||
"""Return the minimum dimming level for this dimmer."""
|
||||
return self.config["minThreshold"]
|
||||
|
||||
async def set_threshold_min(self, min: int) -> dict:
|
||||
"""Set the minimum dimming level for this dimmer.
|
||||
|
||||
The value will depend on the luminaries connected to the dimmer.
|
||||
|
||||
:param min: The minimum dimming level, in the range 0-51.
|
||||
"""
|
||||
if min < self.THRESHOLD_ABS_MIN or min > self.THRESHOLD_ABS_MAX:
|
||||
raise KasaException(
|
||||
"Minimum dimming threshold is outside the supported range: "
|
||||
f"{self.THRESHOLD_ABS_MIN}-{self.THRESHOLD_ABS_MAX}"
|
||||
)
|
||||
return await self.call("calibrate_brightness", {"minThreshold": min})
|
||||
|
||||
@property
|
||||
def fade_off_time(self) -> timedelta:
|
||||
"""Return the fade off animation duration."""
|
||||
return timedelta(milliseconds=cast(int, self.config["fadeOffTime"]))
|
||||
|
||||
async def set_fade_off_time(self, time: int | timedelta) -> dict:
|
||||
"""Set the duration of the fade off animation.
|
||||
|
||||
:param time: The animation duration, in ms.
|
||||
"""
|
||||
if isinstance(time, int):
|
||||
time = timedelta(milliseconds=time)
|
||||
if time < self.FADE_TIME_ABS_MIN or time > self.FADE_TIME_ABS_MAX:
|
||||
raise KasaException(
|
||||
"Fade time is outside the bounds of the supported range:"
|
||||
f"{self.FADE_TIME_ABS_MIN}-{self.FADE_TIME_ABS_MAX}"
|
||||
)
|
||||
return await self.call("set_fade_off_time", {"fadeTime": _td_to_ms(time)})
|
||||
|
||||
@property
|
||||
def fade_on_time(self) -> timedelta:
|
||||
"""Return the fade on animation duration."""
|
||||
return timedelta(milliseconds=cast(int, self.config["fadeOnTime"]))
|
||||
|
||||
async def set_fade_on_time(self, time: int | timedelta) -> dict:
|
||||
"""Set the duration of the fade on animation.
|
||||
|
||||
:param time: The animation duration, in ms.
|
||||
"""
|
||||
if isinstance(time, int):
|
||||
time = timedelta(milliseconds=time)
|
||||
if time < self.FADE_TIME_ABS_MIN or time > self.FADE_TIME_ABS_MAX:
|
||||
raise KasaException(
|
||||
"Fade time is outside the bounds of the supported range:"
|
||||
f"{self.FADE_TIME_ABS_MIN}-{self.FADE_TIME_ABS_MAX}"
|
||||
)
|
||||
return await self.call("set_fade_on_time", {"fadeTime": _td_to_ms(time)})
|
||||
|
||||
@property
|
||||
def gentle_off_time(self) -> timedelta:
|
||||
"""Return the gentle fade off animation duration."""
|
||||
return timedelta(milliseconds=cast(int, self.config["gentleOffTime"]))
|
||||
|
||||
async def set_gentle_off_time(self, time: int | timedelta) -> dict:
|
||||
"""Set the duration of the gentle fade off animation.
|
||||
|
||||
:param time: The animation duration, in ms.
|
||||
"""
|
||||
if isinstance(time, int):
|
||||
time = timedelta(milliseconds=time)
|
||||
if time < self.GENTLE_TIME_ABS_MIN or time > self.GENTLE_TIME_ABS_MAX:
|
||||
raise KasaException(
|
||||
"Gentle off time is outside the bounds of the supported range: "
|
||||
f"{self.GENTLE_TIME_ABS_MIN}-{self.GENTLE_TIME_ABS_MAX}."
|
||||
)
|
||||
return await self.call("set_gentle_off_time", {"duration": _td_to_ms(time)})
|
||||
|
||||
@property
|
||||
def gentle_on_time(self) -> timedelta:
|
||||
"""Return the gentle fade on animation duration."""
|
||||
return timedelta(milliseconds=cast(int, self.config["gentleOnTime"]))
|
||||
|
||||
async def set_gentle_on_time(self, time: int | timedelta) -> dict:
|
||||
"""Set the duration of the gentle fade on animation.
|
||||
|
||||
:param time: The animation duration, in ms.
|
||||
"""
|
||||
if isinstance(time, int):
|
||||
time = timedelta(milliseconds=time)
|
||||
if time < self.GENTLE_TIME_ABS_MIN or time > self.GENTLE_TIME_ABS_MAX:
|
||||
raise KasaException(
|
||||
"Gentle off time is outside the bounds of the supported range: "
|
||||
f"{self.GENTLE_TIME_ABS_MIN}-{self.GENTLE_TIME_ABS_MAX}."
|
||||
)
|
||||
return await self.call("set_gentle_on_time", {"duration": _td_to_ms(time)})
|
||||
|
||||
@property
|
||||
def ramp_rate(self) -> int:
|
||||
"""Return the rate that the dimmer buttons increment the dimmer level."""
|
||||
return self.config["rampRate"]
|
||||
|
||||
async def set_ramp_rate(self, rate: int) -> dict:
|
||||
"""Set how quickly to ramp the dimming level when using the dimmer buttons.
|
||||
|
||||
:param rate: The rate to increment the dimming level with each press.
|
||||
"""
|
||||
if rate < self.RAMP_RATE_ABS_MIN or rate > self.RAMP_RATE_ABS_MAX:
|
||||
raise KasaException(
|
||||
"Gentle off time is outside the bounds of the supported range:"
|
||||
f"{self.RAMP_RATE_ABS_MIN}-{self.RAMP_RATE_ABS_MAX}"
|
||||
)
|
||||
return await self.call("set_button_ramp_rate", {"rampRate": rate})
|
@ -8,7 +8,7 @@ from typing import TYPE_CHECKING, Annotated, cast
|
||||
from ...device_type import DeviceType
|
||||
from ...exceptions import KasaException
|
||||
from ...feature import Feature
|
||||
from ...interfaces.light import HSV, ColorTempRange, LightState
|
||||
from ...interfaces.light import HSV, LightState
|
||||
from ...interfaces.light import Light as LightInterface
|
||||
from ...module import FeatureAttribute
|
||||
from ..iotmodule import IotModule
|
||||
@ -48,6 +48,8 @@ class Light(IotModule, LightInterface):
|
||||
)
|
||||
)
|
||||
if device._is_variable_color_temp:
|
||||
if TYPE_CHECKING:
|
||||
assert isinstance(device, IotBulb)
|
||||
self._add_feature(
|
||||
Feature(
|
||||
device=device,
|
||||
@ -56,7 +58,7 @@ class Light(IotModule, LightInterface):
|
||||
container=self,
|
||||
attribute_getter="color_temp",
|
||||
attribute_setter="set_color_temp",
|
||||
range_getter="valid_temperature_range",
|
||||
range_getter=lambda: device._valid_temperature_range,
|
||||
category=Feature.Category.Primary,
|
||||
type=Feature.Type.Number,
|
||||
)
|
||||
@ -90,11 +92,6 @@ class Light(IotModule, LightInterface):
|
||||
return cast("IotBulb", self._device)
|
||||
return None
|
||||
|
||||
@property # type: ignore
|
||||
def is_dimmable(self) -> int:
|
||||
"""Whether the bulb supports brightness changes."""
|
||||
return self._device._is_dimmable
|
||||
|
||||
@property # type: ignore
|
||||
def brightness(self) -> Annotated[int, FeatureAttribute()]:
|
||||
"""Return the current brightness in percentage."""
|
||||
@ -112,27 +109,6 @@ class Light(IotModule, LightInterface):
|
||||
LightState(brightness=brightness, transition=transition)
|
||||
)
|
||||
|
||||
@property
|
||||
def is_color(self) -> bool:
|
||||
"""Whether the light supports color changes."""
|
||||
if (bulb := self._get_bulb_device()) is None:
|
||||
return False
|
||||
return bulb._is_color
|
||||
|
||||
@property
|
||||
def is_variable_color_temp(self) -> bool:
|
||||
"""Whether the bulb supports color temperature changes."""
|
||||
if (bulb := self._get_bulb_device()) is None:
|
||||
return False
|
||||
return bulb._is_variable_color_temp
|
||||
|
||||
@property
|
||||
def has_effects(self) -> bool:
|
||||
"""Return True if the device supports effects."""
|
||||
if (bulb := self._get_bulb_device()) is None:
|
||||
return False
|
||||
return bulb._has_effects
|
||||
|
||||
@property
|
||||
def hsv(self) -> Annotated[HSV, FeatureAttribute()]:
|
||||
"""Return the current HSV state of the bulb.
|
||||
@ -164,18 +140,6 @@ class Light(IotModule, LightInterface):
|
||||
raise KasaException("Light does not support color.")
|
||||
return await bulb._set_hsv(hue, saturation, value, transition=transition)
|
||||
|
||||
@property
|
||||
def valid_temperature_range(self) -> ColorTempRange:
|
||||
"""Return the device-specific white temperature range (in Kelvin).
|
||||
|
||||
:return: White temperature range in Kelvin (minimum, maximum)
|
||||
"""
|
||||
if (
|
||||
bulb := self._get_bulb_device()
|
||||
) is None or not bulb._is_variable_color_temp:
|
||||
raise KasaException("Light does not support colortemp.")
|
||||
return bulb._valid_temperature_range
|
||||
|
||||
@property
|
||||
def color_temp(self) -> Annotated[int, FeatureAttribute()]:
|
||||
"""Whether the bulb supports color temperature changes."""
|
||||
|
@ -12,20 +12,11 @@ class LightEffect(IotModule, LightEffectInterface):
|
||||
|
||||
@property
|
||||
def effect(self) -> str:
|
||||
"""Return effect state.
|
||||
|
||||
Example:
|
||||
{'brightness': 50,
|
||||
'custom': 0,
|
||||
'enable': 0,
|
||||
'id': '',
|
||||
'name': ''}
|
||||
"""
|
||||
"""Return effect name."""
|
||||
eff = self.data["lighting_effect_state"]
|
||||
name = eff["name"]
|
||||
if eff["enable"]:
|
||||
return name
|
||||
|
||||
return name or self.LIGHT_EFFECTS_UNNAMED_CUSTOM
|
||||
return self.LIGHT_EFFECTS_OFF
|
||||
|
||||
@property
|
||||
|
@ -54,7 +54,7 @@ class LightPreset(IotModule, LightPresetInterface):
|
||||
async def _post_update_hook(self) -> None:
|
||||
"""Update the internal presets."""
|
||||
self._presets = {
|
||||
f"Light preset {index+1}": IotLightPreset.from_dict(vals)
|
||||
f"Light preset {index + 1}": IotLightPreset.from_dict(vals)
|
||||
for index, vals in enumerate(self.data["preferred_state"])
|
||||
# Devices may list some light effects along with normal presets but these
|
||||
# are handled by the LightEffect module so exclude preferred states with id
|
||||
|
@ -3,11 +3,13 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import math
|
||||
from dataclasses import dataclass
|
||||
from enum import Enum
|
||||
|
||||
from ...exceptions import KasaException
|
||||
from ...feature import Feature
|
||||
from ..iotmodule import IotModule
|
||||
from ..iotmodule import IotModule, merge
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@ -20,6 +22,71 @@ class Range(Enum):
|
||||
Near = 2
|
||||
Custom = 3
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.name
|
||||
|
||||
|
||||
@dataclass
|
||||
class PIRConfig:
|
||||
"""Dataclass representing a PIR sensor configuration."""
|
||||
|
||||
enabled: bool
|
||||
adc_min: int
|
||||
adc_max: int
|
||||
range: Range
|
||||
threshold: int
|
||||
|
||||
@property
|
||||
def adc_mid(self) -> int:
|
||||
"""Compute the ADC midpoint from the configured ADC Max and Min values."""
|
||||
return math.floor(abs(self.adc_max - self.adc_min) / 2)
|
||||
|
||||
|
||||
@dataclass
|
||||
class PIRStatus:
|
||||
"""Dataclass representing the current trigger state of an ADC PIR sensor."""
|
||||
|
||||
pir_config: PIRConfig
|
||||
adc_value: int
|
||||
|
||||
@property
|
||||
def pir_value(self) -> int:
|
||||
"""
|
||||
Get the PIR status value in integer form.
|
||||
|
||||
Computes the PIR status value that this object represents,
|
||||
using the given PIR configuration.
|
||||
"""
|
||||
return self.pir_config.adc_mid - self.adc_value
|
||||
|
||||
@property
|
||||
def pir_percent(self) -> float:
|
||||
"""
|
||||
Get the PIR status value in percentile form.
|
||||
|
||||
Computes the PIR status percentage that this object represents,
|
||||
using the given PIR configuration.
|
||||
"""
|
||||
value = self.pir_value
|
||||
divisor = (
|
||||
(self.pir_config.adc_mid - self.pir_config.adc_min)
|
||||
if (value < 0)
|
||||
else (self.pir_config.adc_max - self.pir_config.adc_mid)
|
||||
)
|
||||
return (float(value) / divisor) * 100
|
||||
|
||||
@property
|
||||
def pir_triggered(self) -> bool:
|
||||
"""
|
||||
Get the PIR status trigger state.
|
||||
|
||||
Compute the PIR trigger state this object represents,
|
||||
using the given PIR configuration.
|
||||
"""
|
||||
return (self.pir_config.enabled) and (
|
||||
abs(self.pir_percent) > (100 - self.pir_config.threshold)
|
||||
)
|
||||
|
||||
|
||||
class Motion(IotModule):
|
||||
"""Implements the motion detection (PIR) module."""
|
||||
@ -30,6 +97,11 @@ class Motion(IotModule):
|
||||
if "get_config" not in self.data:
|
||||
return
|
||||
|
||||
# Require that ADC value is also present.
|
||||
if "get_adc_value" not in self.data:
|
||||
_LOGGER.warning("%r initialized, but no get_adc_value in response")
|
||||
return
|
||||
|
||||
if "enable" not in self.config:
|
||||
_LOGGER.warning("%r initialized, but no enable in response")
|
||||
return
|
||||
@ -48,9 +120,143 @@ class Motion(IotModule):
|
||||
)
|
||||
)
|
||||
|
||||
self._add_feature(
|
||||
Feature(
|
||||
device=self._device,
|
||||
container=self,
|
||||
id="pir_range",
|
||||
name="Motion Sensor Range",
|
||||
icon="mdi:motion-sensor",
|
||||
attribute_getter="range",
|
||||
attribute_setter="_set_range_from_str",
|
||||
type=Feature.Type.Choice,
|
||||
choices_getter="ranges",
|
||||
category=Feature.Category.Config,
|
||||
)
|
||||
)
|
||||
|
||||
self._add_feature(
|
||||
Feature(
|
||||
device=self._device,
|
||||
container=self,
|
||||
id="pir_threshold",
|
||||
name="Motion Sensor Threshold",
|
||||
icon="mdi:motion-sensor",
|
||||
attribute_getter="threshold",
|
||||
attribute_setter="set_threshold",
|
||||
type=Feature.Type.Number,
|
||||
category=Feature.Category.Config,
|
||||
range_getter=lambda: (0, 100),
|
||||
)
|
||||
)
|
||||
|
||||
self._add_feature(
|
||||
Feature(
|
||||
device=self._device,
|
||||
container=self,
|
||||
id="pir_triggered",
|
||||
name="PIR Triggered",
|
||||
icon="mdi:motion-sensor",
|
||||
attribute_getter="pir_triggered",
|
||||
attribute_setter=None,
|
||||
type=Feature.Type.Sensor,
|
||||
category=Feature.Category.Primary,
|
||||
)
|
||||
)
|
||||
|
||||
self._add_feature(
|
||||
Feature(
|
||||
device=self._device,
|
||||
container=self,
|
||||
id="pir_value",
|
||||
name="PIR Value",
|
||||
icon="mdi:motion-sensor",
|
||||
attribute_getter="pir_value",
|
||||
attribute_setter=None,
|
||||
type=Feature.Type.Sensor,
|
||||
category=Feature.Category.Info,
|
||||
)
|
||||
)
|
||||
|
||||
self._add_feature(
|
||||
Feature(
|
||||
device=self._device,
|
||||
container=self,
|
||||
id="pir_adc_value",
|
||||
name="PIR ADC Value",
|
||||
icon="mdi:motion-sensor",
|
||||
attribute_getter="adc_value",
|
||||
attribute_setter=None,
|
||||
type=Feature.Type.Sensor,
|
||||
category=Feature.Category.Debug,
|
||||
)
|
||||
)
|
||||
|
||||
self._add_feature(
|
||||
Feature(
|
||||
device=self._device,
|
||||
container=self,
|
||||
id="pir_adc_min",
|
||||
name="PIR ADC Min",
|
||||
icon="mdi:motion-sensor",
|
||||
attribute_getter="adc_min",
|
||||
attribute_setter=None,
|
||||
type=Feature.Type.Sensor,
|
||||
category=Feature.Category.Debug,
|
||||
)
|
||||
)
|
||||
|
||||
self._add_feature(
|
||||
Feature(
|
||||
device=self._device,
|
||||
container=self,
|
||||
id="pir_adc_mid",
|
||||
name="PIR ADC Mid",
|
||||
icon="mdi:motion-sensor",
|
||||
attribute_getter="adc_mid",
|
||||
attribute_setter=None,
|
||||
type=Feature.Type.Sensor,
|
||||
category=Feature.Category.Debug,
|
||||
)
|
||||
)
|
||||
|
||||
self._add_feature(
|
||||
Feature(
|
||||
device=self._device,
|
||||
container=self,
|
||||
id="pir_adc_max",
|
||||
name="PIR ADC Max",
|
||||
icon="mdi:motion-sensor",
|
||||
attribute_getter="adc_max",
|
||||
attribute_setter=None,
|
||||
type=Feature.Type.Sensor,
|
||||
category=Feature.Category.Debug,
|
||||
)
|
||||
)
|
||||
|
||||
self._add_feature(
|
||||
Feature(
|
||||
device=self._device,
|
||||
container=self,
|
||||
id="pir_percent",
|
||||
name="PIR Percentile",
|
||||
icon="mdi:motion-sensor",
|
||||
attribute_getter="pir_percent",
|
||||
attribute_setter=None,
|
||||
type=Feature.Type.Sensor,
|
||||
category=Feature.Category.Debug,
|
||||
unit_getter=lambda: "%",
|
||||
)
|
||||
)
|
||||
|
||||
def query(self) -> dict:
|
||||
"""Request PIR configuration."""
|
||||
return self.query_for_command("get_config")
|
||||
req = merge(
|
||||
self.query_for_command("get_config"),
|
||||
self.query_for_command("get_adc_value"),
|
||||
)
|
||||
|
||||
return req
|
||||
|
||||
@property
|
||||
def config(self) -> dict:
|
||||
@ -58,34 +264,103 @@ class Motion(IotModule):
|
||||
return self.data["get_config"]
|
||||
|
||||
@property
|
||||
def range(self) -> Range:
|
||||
"""Return motion detection range."""
|
||||
return Range(self.config["trigger_index"])
|
||||
def pir_config(self) -> PIRConfig:
|
||||
"""Return PIR sensor configuration."""
|
||||
pir_range = Range(self.config["trigger_index"])
|
||||
return PIRConfig(
|
||||
enabled=bool(self.config["enable"]),
|
||||
adc_min=int(self.config["min_adc"]),
|
||||
adc_max=int(self.config["max_adc"]),
|
||||
range=pir_range,
|
||||
threshold=self.get_range_threshold(pir_range),
|
||||
)
|
||||
|
||||
@property
|
||||
def enabled(self) -> bool:
|
||||
"""Return True if module is enabled."""
|
||||
return bool(self.config["enable"])
|
||||
return self.pir_config.enabled
|
||||
|
||||
@property
|
||||
def adc_min(self) -> int:
|
||||
"""Return minimum ADC sensor value."""
|
||||
return self.pir_config.adc_min
|
||||
|
||||
@property
|
||||
def adc_max(self) -> int:
|
||||
"""Return maximum ADC sensor value."""
|
||||
return self.pir_config.adc_max
|
||||
|
||||
@property
|
||||
def adc_mid(self) -> int:
|
||||
"""
|
||||
Return the midpoint for the ADC.
|
||||
|
||||
The midpoint represents the zero point for the PIR sensor waveform.
|
||||
|
||||
Currently this is estimated by:
|
||||
math.floor(abs(adc_max - adc_min) / 2)
|
||||
"""
|
||||
return self.pir_config.adc_mid
|
||||
|
||||
async def set_enabled(self, state: bool) -> dict:
|
||||
"""Enable/disable PIR."""
|
||||
return await self.call("set_enable", {"enable": int(state)})
|
||||
|
||||
async def set_range(
|
||||
self, *, range: Range | None = None, custom_range: int | None = None
|
||||
) -> dict:
|
||||
"""Set the range for the sensor.
|
||||
@property
|
||||
def ranges(self) -> list[str]:
|
||||
"""Return set of supported range classes."""
|
||||
range_min = 0
|
||||
range_max = len(self.config["array"])
|
||||
valid_ranges = list()
|
||||
for r in Range:
|
||||
if (r.value >= range_min) and (r.value < range_max):
|
||||
valid_ranges.append(r.name)
|
||||
return valid_ranges
|
||||
|
||||
:param range: for using standard ranges
|
||||
:param custom_range: range in decimeters, overrides the range parameter
|
||||
@property
|
||||
def range(self) -> Range:
|
||||
"""Return motion detection Range."""
|
||||
return self.pir_config.range
|
||||
|
||||
async def set_range(self, range: Range) -> dict:
|
||||
"""Set the Range for the sensor.
|
||||
|
||||
:param Range: the range class to use.
|
||||
"""
|
||||
if custom_range is not None:
|
||||
payload = {"index": Range.Custom.value, "value": custom_range}
|
||||
elif range is not None:
|
||||
payload = {"index": range.value}
|
||||
else:
|
||||
raise KasaException("Either range or custom_range need to be defined")
|
||||
payload = {"index": range.value}
|
||||
return await self.call("set_trigger_sens", payload)
|
||||
|
||||
def _parse_range_value(self, value: str) -> Range:
|
||||
"""Attempt to parse a range value from the given string."""
|
||||
value = value.strip().capitalize()
|
||||
try:
|
||||
return Range[value]
|
||||
except KeyError:
|
||||
raise KasaException(
|
||||
f"Invalid range value: '{value}'."
|
||||
f" Valid options are: {Range._member_names_}"
|
||||
) from KeyError
|
||||
|
||||
async def _set_range_from_str(self, input: str) -> dict:
|
||||
value = self._parse_range_value(input)
|
||||
return await self.set_range(range=value)
|
||||
|
||||
def get_range_threshold(self, range_type: Range) -> int:
|
||||
"""Get the distance threshold at which the PIR sensor is will trigger."""
|
||||
if range_type.value < 0 or range_type.value >= len(self.config["array"]):
|
||||
raise KasaException(
|
||||
"Range type is outside the bounds of the configured device ranges."
|
||||
)
|
||||
return int(self.config["array"][range_type.value])
|
||||
|
||||
@property
|
||||
def threshold(self) -> int:
|
||||
"""Return motion detection Range."""
|
||||
return self.pir_config.threshold
|
||||
|
||||
async def set_threshold(self, value: int) -> dict:
|
||||
"""Set the distance threshold at which the PIR sensor is will trigger."""
|
||||
payload = {"index": Range.Custom.value, "value": value}
|
||||
return await self.call("set_trigger_sens", payload)
|
||||
|
||||
@property
|
||||
@ -100,3 +375,34 @@ class Motion(IotModule):
|
||||
to avoid reverting this back to 60 seconds after a period of time.
|
||||
"""
|
||||
return await self.call("set_cold_time", {"cold_time": timeout})
|
||||
|
||||
@property
|
||||
def pir_state(self) -> PIRStatus:
|
||||
"""Return cached PIR status."""
|
||||
return PIRStatus(self.pir_config, self.data["get_adc_value"]["value"])
|
||||
|
||||
async def get_pir_state(self) -> PIRStatus:
|
||||
"""Return real-time PIR status."""
|
||||
latest = await self.call("get_adc_value")
|
||||
self.data["get_adc_value"] = latest
|
||||
return PIRStatus(self.pir_config, latest["value"])
|
||||
|
||||
@property
|
||||
def adc_value(self) -> int:
|
||||
"""Return motion adc value."""
|
||||
return self.pir_state.adc_value
|
||||
|
||||
@property
|
||||
def pir_value(self) -> int:
|
||||
"""Return the computed PIR sensor value."""
|
||||
return self.pir_state.pir_value
|
||||
|
||||
@property
|
||||
def pir_percent(self) -> float:
|
||||
"""Return the computed PIR sensor value, in percentile form."""
|
||||
return self.pir_state.pir_percent
|
||||
|
||||
@property
|
||||
def pir_triggered(self) -> bool:
|
||||
"""Return if the motion sensor has been triggered."""
|
||||
return self.pir_state.pir_triggered
|
||||
|
@ -81,6 +81,9 @@ ModuleT = TypeVar("ModuleT", bound="Module")
|
||||
class FeatureAttribute:
|
||||
"""Class for annotating attributes bound to feature."""
|
||||
|
||||
def __init__(self, feature_name: str | None = None) -> None:
|
||||
self.feature_name = feature_name
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return "FeatureAttribute"
|
||||
|
||||
@ -93,6 +96,8 @@ class Module(ABC):
|
||||
"""
|
||||
|
||||
# Common Modules
|
||||
Alarm: Final[ModuleName[interfaces.Alarm]] = ModuleName("Alarm")
|
||||
ChildSetup: Final[ModuleName[interfaces.ChildSetup]] = ModuleName("ChildSetup")
|
||||
Energy: Final[ModuleName[interfaces.Energy]] = ModuleName("Energy")
|
||||
Fan: Final[ModuleName[interfaces.Fan]] = ModuleName("Fan")
|
||||
LightEffect: Final[ModuleName[interfaces.LightEffect]] = ModuleName("LightEffect")
|
||||
@ -106,13 +111,13 @@ class Module(ABC):
|
||||
IotAmbientLight: Final[ModuleName[iot.AmbientLight]] = ModuleName("ambient")
|
||||
IotAntitheft: Final[ModuleName[iot.Antitheft]] = ModuleName("anti_theft")
|
||||
IotCountdown: Final[ModuleName[iot.Countdown]] = ModuleName("countdown")
|
||||
IotDimmer: Final[ModuleName[iot.Dimmer]] = ModuleName("dimmer")
|
||||
IotMotion: Final[ModuleName[iot.Motion]] = ModuleName("motion")
|
||||
IotSchedule: Final[ModuleName[iot.Schedule]] = ModuleName("schedule")
|
||||
IotUsage: Final[ModuleName[iot.Usage]] = ModuleName("usage")
|
||||
IotCloud: Final[ModuleName[iot.Cloud]] = ModuleName("cloud")
|
||||
|
||||
# SMART only Modules
|
||||
Alarm: Final[ModuleName[smart.Alarm]] = ModuleName("Alarm")
|
||||
AutoOff: Final[ModuleName[smart.AutoOff]] = ModuleName("AutoOff")
|
||||
BatterySensor: Final[ModuleName[smart.BatterySensor]] = ModuleName("BatterySensor")
|
||||
Brightness: Final[ModuleName[smart.Brightness]] = ModuleName("Brightness")
|
||||
@ -152,7 +157,11 @@ class Module(ABC):
|
||||
ChildProtection: Final[ModuleName[smart.ChildProtection]] = ModuleName(
|
||||
"ChildProtection"
|
||||
)
|
||||
ChildLock: Final[ModuleName[smart.ChildLock]] = ModuleName("ChildLock")
|
||||
TriggerLogs: Final[ModuleName[smart.TriggerLogs]] = ModuleName("TriggerLogs")
|
||||
PowerProtection: Final[ModuleName[smart.PowerProtection]] = ModuleName(
|
||||
"PowerProtection"
|
||||
)
|
||||
|
||||
HomeKit: Final[ModuleName[smart.HomeKit]] = ModuleName("HomeKit")
|
||||
Matter: Final[ModuleName[smart.Matter]] = ModuleName("Matter")
|
||||
@ -161,11 +170,24 @@ class Module(ABC):
|
||||
Camera: Final[ModuleName[smartcam.Camera]] = ModuleName("Camera")
|
||||
LensMask: Final[ModuleName[smartcam.LensMask]] = ModuleName("LensMask")
|
||||
|
||||
# Vacuum modules
|
||||
Clean: Final[ModuleName[smart.Clean]] = ModuleName("Clean")
|
||||
Consumables: Final[ModuleName[smart.Consumables]] = ModuleName("Consumables")
|
||||
Dustbin: Final[ModuleName[smart.Dustbin]] = ModuleName("Dustbin")
|
||||
Speaker: Final[ModuleName[smart.Speaker]] = ModuleName("Speaker")
|
||||
Mop: Final[ModuleName[smart.Mop]] = ModuleName("Mop")
|
||||
CleanRecords: Final[ModuleName[smart.CleanRecords]] = ModuleName("CleanRecords")
|
||||
|
||||
def __init__(self, device: Device, module: str) -> None:
|
||||
self._device = device
|
||||
self._module = module
|
||||
self._module_features: dict[str, Feature] = {}
|
||||
|
||||
@property
|
||||
def device(self) -> Device:
|
||||
"""Return the device exposing the module."""
|
||||
return self._device
|
||||
|
||||
@property
|
||||
def _all_features(self) -> dict[str, Feature]:
|
||||
"""Get the features for this module and any sub modules."""
|
||||
@ -224,7 +246,7 @@ class Module(ABC):
|
||||
)
|
||||
|
||||
|
||||
def _is_bound_feature(attribute: property | Callable) -> bool:
|
||||
def _get_feature_attribute(attribute: property | Callable) -> FeatureAttribute | None:
|
||||
"""Check if an attribute is bound to a feature with FeatureAttribute."""
|
||||
if isinstance(attribute, property):
|
||||
hints = get_type_hints(attribute.fget, include_extras=True)
|
||||
@ -235,9 +257,9 @@ def _is_bound_feature(attribute: property | Callable) -> bool:
|
||||
metadata = hints["return"].__metadata__
|
||||
for meta in metadata:
|
||||
if isinstance(meta, FeatureAttribute):
|
||||
return True
|
||||
return meta
|
||||
|
||||
return False
|
||||
return None
|
||||
|
||||
|
||||
@cache
|
||||
@ -264,12 +286,17 @@ def _get_bound_feature(
|
||||
f"module {module.__class__.__name__}"
|
||||
)
|
||||
|
||||
if not _is_bound_feature(attribute_callable):
|
||||
if not (fa := _get_feature_attribute(attribute_callable)):
|
||||
raise KasaException(
|
||||
f"Attribute {attribute_name} of module {module.__class__.__name__}"
|
||||
" is not bound to a feature"
|
||||
)
|
||||
|
||||
# If a feature_name was passed to the FeatureAttribute use that to check
|
||||
# for the feature. Otherwise check the getters and setters in the features
|
||||
if fa.feature_name:
|
||||
return module._all_features.get(fa.feature_name)
|
||||
|
||||
check = {attribute_name, attribute_callable}
|
||||
for feature in module._all_features.values():
|
||||
if (getter := feature.attribute_getter) and getter in check:
|
||||
|
@ -30,7 +30,7 @@ def _mask_children(children: list[dict[str, Any]]) -> list[dict[str, Any]]:
|
||||
def mask_child(child: dict[str, Any], index: int) -> dict[str, Any]:
|
||||
result = {
|
||||
**child,
|
||||
"id": f"SCRUBBED_CHILD_DEVICE_ID_{index+1}",
|
||||
"id": f"SCRUBBED_CHILD_DEVICE_ID_{index + 1}",
|
||||
}
|
||||
# Will leave empty aliases as blank
|
||||
if child.get("alias"):
|
||||
|
@ -244,11 +244,15 @@ class _ChildCameraProtocolWrapper(SmartProtocol):
|
||||
|
||||
responses = response["multipleRequest"]["responses"]
|
||||
response_dict = {}
|
||||
|
||||
# Raise errors for single calls
|
||||
raise_on_error = len(requests) == 1
|
||||
|
||||
for index_id, response in enumerate(responses):
|
||||
response_data = response["result"]["response_data"]
|
||||
method = methods[index_id]
|
||||
self._handle_response_error_code(
|
||||
response_data, method, raise_on_error=False
|
||||
response_data, method, raise_on_error=raise_on_error
|
||||
)
|
||||
response_dict[method] = response_data.get("result")
|
||||
|
||||
|
@ -36,6 +36,18 @@ if TYPE_CHECKING:
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def _mask_area_list(area_list: list[dict[str, Any]]) -> list[dict[str, Any]]:
|
||||
def mask_area(area: dict[str, Any]) -> dict[str, Any]:
|
||||
result = {**area}
|
||||
# Will leave empty names as blank
|
||||
if area.get("name"):
|
||||
result["name"] = "I01BU0tFRF9OQU1FIw==" # #MASKED_NAME#
|
||||
return result
|
||||
|
||||
return [mask_area(area) for area in area_list]
|
||||
|
||||
|
||||
REDACTORS: dict[str, Callable[[Any], Any] | None] = {
|
||||
"latitude": lambda x: 0,
|
||||
"longitude": lambda x: 0,
|
||||
@ -61,14 +73,20 @@ REDACTORS: dict[str, Callable[[Any], Any] | None] = {
|
||||
"ip": lambda x: x, # don't redact but keep listed here for dump_devinfo
|
||||
# smartcam
|
||||
"dev_id": lambda x: "REDACTED_" + x[9::],
|
||||
"ext_addr": lambda x: "REDACTED_" + x[9::],
|
||||
"device_name": lambda x: "#MASKED_NAME#" if x else "",
|
||||
"device_alias": lambda x: "#MASKED_NAME#" if x else "",
|
||||
"alias": lambda x: "#MASKED_NAME#" if x else "", # child info on parent uses alias
|
||||
"local_ip": lambda x: x, # don't redact but keep listed here for dump_devinfo
|
||||
# robovac
|
||||
"board_sn": lambda _: "000000000000",
|
||||
"custom_sn": lambda _: "000000000000",
|
||||
"location": lambda x: "#MASKED_NAME#" if x else "",
|
||||
"map_data": lambda x: "#SCRUBBED_MAPDATA#" if x else "",
|
||||
"map_name": lambda x: "I01BU0tFRF9OQU1FIw==", # #MASKED_NAME#
|
||||
"area_list": _mask_area_list,
|
||||
# unknown robovac binary blob in get_device_info
|
||||
"cd": lambda x: "I01BU0tFRF9CSU5BUlkj", # #MASKED_BINARY#
|
||||
}
|
||||
|
||||
# Queries that are known not to work properly when sent as a
|
||||
@ -218,7 +236,7 @@ class SmartProtocol(BaseProtocol):
|
||||
|
||||
smart_params = {"requests": requests_step}
|
||||
smart_request = self.get_smart_request(smart_method, smart_params)
|
||||
batch_name = f"multi-request-batch-{batch_num+1}-of-{int(end/step)+1}"
|
||||
batch_name = f"multi-request-batch-{batch_num + 1}-of-{int(end / step) + 1}"
|
||||
if debug_enabled:
|
||||
_LOGGER.debug(
|
||||
"%s %s >> %s",
|
||||
|
@ -6,12 +6,18 @@ from .autooff import AutoOff
|
||||
from .batterysensor import BatterySensor
|
||||
from .brightness import Brightness
|
||||
from .childdevice import ChildDevice
|
||||
from .childlock import ChildLock
|
||||
from .childprotection import ChildProtection
|
||||
from .childsetup import ChildSetup
|
||||
from .clean import Clean
|
||||
from .cleanrecords import CleanRecords
|
||||
from .cloud import Cloud
|
||||
from .color import Color
|
||||
from .colortemperature import ColorTemperature
|
||||
from .consumables import Consumables
|
||||
from .contactsensor import ContactSensor
|
||||
from .devicemodule import DeviceModule
|
||||
from .dustbin import Dustbin
|
||||
from .energy import Energy
|
||||
from .fan import Fan
|
||||
from .firmware import Firmware
|
||||
@ -25,9 +31,12 @@ from .lightpreset import LightPreset
|
||||
from .lightstripeffect import LightStripEffect
|
||||
from .lighttransition import LightTransition
|
||||
from .matter import Matter
|
||||
from .mop import Mop
|
||||
from .motionsensor import MotionSensor
|
||||
from .overheatprotection import OverheatProtection
|
||||
from .powerprotection import PowerProtection
|
||||
from .reportmode import ReportMode
|
||||
from .speaker import Speaker
|
||||
from .temperaturecontrol import TemperatureControl
|
||||
from .temperaturesensor import TemperatureSensor
|
||||
from .thermostat import Thermostat
|
||||
@ -41,6 +50,8 @@ __all__ = [
|
||||
"Energy",
|
||||
"DeviceModule",
|
||||
"ChildDevice",
|
||||
"ChildLock",
|
||||
"ChildSetup",
|
||||
"BatterySensor",
|
||||
"HumiditySensor",
|
||||
"TemperatureSensor",
|
||||
@ -66,8 +77,15 @@ __all__ = [
|
||||
"TriggerLogs",
|
||||
"FrostProtection",
|
||||
"Thermostat",
|
||||
"Clean",
|
||||
"Consumables",
|
||||
"CleanRecords",
|
||||
"SmartLightEffect",
|
||||
"PowerProtection",
|
||||
"OverheatProtection",
|
||||
"Speaker",
|
||||
"HomeKit",
|
||||
"Matter",
|
||||
"Dustbin",
|
||||
"Mop",
|
||||
]
|
||||
|
@ -2,13 +2,30 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Literal
|
||||
from typing import TYPE_CHECKING, Annotated, Literal, TypeAlias
|
||||
|
||||
from ...feature import Feature
|
||||
from ...interfaces import Alarm as AlarmInterface
|
||||
from ...module import FeatureAttribute
|
||||
from ..smartmodule import SmartModule
|
||||
|
||||
DURATION_MAX = 10 * 60
|
||||
|
||||
class Alarm(SmartModule):
|
||||
VOLUME_INT_TO_STR = {
|
||||
0: "mute",
|
||||
1: "low",
|
||||
2: "normal",
|
||||
3: "high",
|
||||
}
|
||||
|
||||
VOLUME_STR_LIST = [v for v in VOLUME_INT_TO_STR.values()]
|
||||
VOLUME_INT_RANGE = (min(VOLUME_INT_TO_STR.keys()), max(VOLUME_INT_TO_STR.keys()))
|
||||
VOLUME_STR_TO_INT = {v: k for k, v in VOLUME_INT_TO_STR.items()}
|
||||
|
||||
AlarmVolume: TypeAlias = Literal["mute", "low", "normal", "high"]
|
||||
|
||||
|
||||
class Alarm(SmartModule, AlarmInterface):
|
||||
"""Implementation of alarm module."""
|
||||
|
||||
REQUIRED_COMPONENT = "alarm"
|
||||
@ -21,10 +38,7 @@ class Alarm(SmartModule):
|
||||
}
|
||||
|
||||
def _initialize_features(self) -> None:
|
||||
"""Initialize features.
|
||||
|
||||
This is implemented as some features depend on device responses.
|
||||
"""
|
||||
"""Initialize features."""
|
||||
device = self._device
|
||||
self._add_feature(
|
||||
Feature(
|
||||
@ -67,11 +81,37 @@ class Alarm(SmartModule):
|
||||
id="alarm_volume",
|
||||
name="Alarm volume",
|
||||
container=self,
|
||||
attribute_getter="alarm_volume",
|
||||
attribute_getter="_alarm_volume_str",
|
||||
attribute_setter="set_alarm_volume",
|
||||
category=Feature.Category.Config,
|
||||
type=Feature.Type.Choice,
|
||||
choices_getter=lambda: ["low", "normal", "high"],
|
||||
choices_getter=lambda: VOLUME_STR_LIST,
|
||||
)
|
||||
)
|
||||
self._add_feature(
|
||||
Feature(
|
||||
device,
|
||||
id="alarm_volume_level",
|
||||
name="Alarm volume",
|
||||
container=self,
|
||||
attribute_getter="alarm_volume",
|
||||
attribute_setter="set_alarm_volume",
|
||||
category=Feature.Category.Config,
|
||||
type=Feature.Type.Number,
|
||||
range_getter=lambda: VOLUME_INT_RANGE,
|
||||
)
|
||||
)
|
||||
self._add_feature(
|
||||
Feature(
|
||||
device,
|
||||
id="alarm_duration",
|
||||
name="Alarm duration",
|
||||
container=self,
|
||||
attribute_getter="alarm_duration",
|
||||
attribute_setter="set_alarm_duration",
|
||||
category=Feature.Category.Config,
|
||||
type=Feature.Type.Number,
|
||||
range_getter=lambda: (1, DURATION_MAX),
|
||||
)
|
||||
)
|
||||
self._add_feature(
|
||||
@ -96,15 +136,16 @@ class Alarm(SmartModule):
|
||||
)
|
||||
|
||||
@property
|
||||
def alarm_sound(self) -> str:
|
||||
def alarm_sound(self) -> Annotated[str, FeatureAttribute()]:
|
||||
"""Return current alarm sound."""
|
||||
return self.data["get_alarm_configure"]["type"]
|
||||
|
||||
async def set_alarm_sound(self, sound: str) -> dict:
|
||||
async def set_alarm_sound(self, sound: str) -> Annotated[dict, FeatureAttribute()]:
|
||||
"""Set alarm sound.
|
||||
|
||||
See *alarm_sounds* for list of available sounds.
|
||||
"""
|
||||
self._check_sound(sound)
|
||||
payload = self.data["get_alarm_configure"].copy()
|
||||
payload["type"] = sound
|
||||
return await self.call("set_alarm_configure", payload)
|
||||
@ -115,16 +156,40 @@ class Alarm(SmartModule):
|
||||
return self.data["get_support_alarm_type_list"]["alarm_type_list"]
|
||||
|
||||
@property
|
||||
def alarm_volume(self) -> Literal["low", "normal", "high"]:
|
||||
def alarm_volume(self) -> Annotated[int, FeatureAttribute("alarm_volume_level")]:
|
||||
"""Return alarm volume."""
|
||||
return VOLUME_STR_TO_INT[self._alarm_volume_str]
|
||||
|
||||
@property
|
||||
def _alarm_volume_str(
|
||||
self,
|
||||
) -> Annotated[AlarmVolume, FeatureAttribute("alarm_volume")]:
|
||||
"""Return alarm volume."""
|
||||
return self.data["get_alarm_configure"]["volume"]
|
||||
|
||||
async def set_alarm_volume(self, volume: Literal["low", "normal", "high"]) -> dict:
|
||||
async def set_alarm_volume(
|
||||
self, volume: AlarmVolume | int
|
||||
) -> Annotated[dict, FeatureAttribute()]:
|
||||
"""Set alarm volume."""
|
||||
self._check_and_convert_volume(volume)
|
||||
payload = self.data["get_alarm_configure"].copy()
|
||||
payload["volume"] = volume
|
||||
return await self.call("set_alarm_configure", payload)
|
||||
|
||||
@property
|
||||
def alarm_duration(self) -> Annotated[int, FeatureAttribute()]:
|
||||
"""Return alarm duration."""
|
||||
return self.data["get_alarm_configure"]["duration"]
|
||||
|
||||
async def set_alarm_duration(
|
||||
self, duration: int
|
||||
) -> Annotated[dict, FeatureAttribute()]:
|
||||
"""Set alarm duration."""
|
||||
self._check_duration(duration)
|
||||
payload = self.data["get_alarm_configure"].copy()
|
||||
payload["duration"] = duration
|
||||
return await self.call("set_alarm_configure", payload)
|
||||
|
||||
@property
|
||||
def active(self) -> bool:
|
||||
"""Return true if alarm is active."""
|
||||
@ -136,10 +201,62 @@ class Alarm(SmartModule):
|
||||
src = self._device.sys_info["in_alarm_source"]
|
||||
return src if src else None
|
||||
|
||||
async def play(self) -> dict:
|
||||
"""Play alarm."""
|
||||
return await self.call("play_alarm")
|
||||
async def play(
|
||||
self,
|
||||
*,
|
||||
duration: int | None = None,
|
||||
volume: int | AlarmVolume | None = None,
|
||||
sound: str | None = None,
|
||||
) -> dict:
|
||||
"""Play alarm.
|
||||
|
||||
The optional *duration*, *volume*, and *sound* to override the device settings.
|
||||
*volume* can be set to 'mute', 'low', 'normal', or 'high'.
|
||||
*duration* is in seconds.
|
||||
See *alarm_sounds* for the list of sounds available for the device.
|
||||
"""
|
||||
params: dict[str, str | int] = {}
|
||||
|
||||
if duration is not None:
|
||||
self._check_duration(duration)
|
||||
params["alarm_duration"] = duration
|
||||
|
||||
if volume is not None:
|
||||
target_volume = self._check_and_convert_volume(volume)
|
||||
params["alarm_volume"] = target_volume
|
||||
|
||||
if sound is not None:
|
||||
self._check_sound(sound)
|
||||
params["alarm_type"] = sound
|
||||
|
||||
return await self.call("play_alarm", params)
|
||||
|
||||
async def stop(self) -> dict:
|
||||
"""Stop alarm."""
|
||||
return await self.call("stop_alarm")
|
||||
|
||||
def _check_and_convert_volume(self, volume: str | int) -> str:
|
||||
"""Raise an exception on invalid volume."""
|
||||
if isinstance(volume, int):
|
||||
volume = VOLUME_INT_TO_STR.get(volume, "invalid")
|
||||
|
||||
if TYPE_CHECKING:
|
||||
assert isinstance(volume, str)
|
||||
|
||||
if volume not in VOLUME_INT_TO_STR.values():
|
||||
raise ValueError(
|
||||
f"Invalid volume {volume} "
|
||||
f"available: {VOLUME_INT_TO_STR.keys()}, {VOLUME_INT_TO_STR.values()}"
|
||||
)
|
||||
|
||||
return volume
|
||||
|
||||
def _check_duration(self, duration: int) -> None:
|
||||
"""Raise an exception on invalid duration."""
|
||||
if duration < 1 or duration > DURATION_MAX:
|
||||
raise ValueError(f"Invalid duration {duration} available: 1-600")
|
||||
|
||||
def _check_sound(self, sound: str) -> None:
|
||||
"""Raise an exception on invalid sound."""
|
||||
if sound not in self.alarm_sounds:
|
||||
raise ValueError(f"Invalid sound {sound} available: {self.alarm_sounds}")
|
||||
|
@ -2,7 +2,11 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Annotated
|
||||
|
||||
from ...exceptions import KasaException
|
||||
from ...feature import Feature
|
||||
from ...module import FeatureAttribute
|
||||
from ..smartmodule import SmartModule
|
||||
|
||||
|
||||
@ -14,18 +18,22 @@ class BatterySensor(SmartModule):
|
||||
|
||||
def _initialize_features(self) -> None:
|
||||
"""Initialize features."""
|
||||
self._add_feature(
|
||||
Feature(
|
||||
self._device,
|
||||
"battery_low",
|
||||
"Battery low",
|
||||
container=self,
|
||||
attribute_getter="battery_low",
|
||||
icon="mdi:alert",
|
||||
type=Feature.Type.BinarySensor,
|
||||
category=Feature.Category.Debug,
|
||||
if (
|
||||
"at_low_battery" in self._device.sys_info
|
||||
or "is_low" in self._device.sys_info
|
||||
):
|
||||
self._add_feature(
|
||||
Feature(
|
||||
self._device,
|
||||
"battery_low",
|
||||
"Battery low",
|
||||
container=self,
|
||||
attribute_getter="battery_low",
|
||||
icon="mdi:alert",
|
||||
type=Feature.Type.BinarySensor,
|
||||
category=Feature.Category.Debug,
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
# Some devices, like T110 contact sensor do not report the battery percentage
|
||||
if "battery_percentage" in self._device.sys_info:
|
||||
@ -48,11 +56,17 @@ class BatterySensor(SmartModule):
|
||||
return {}
|
||||
|
||||
@property
|
||||
def battery(self) -> int:
|
||||
def battery(self) -> Annotated[int, FeatureAttribute()]:
|
||||
"""Return battery level."""
|
||||
return self._device.sys_info["battery_percentage"]
|
||||
|
||||
@property
|
||||
def battery_low(self) -> bool:
|
||||
def battery_low(self) -> Annotated[bool, FeatureAttribute()]:
|
||||
"""Return True if battery is low."""
|
||||
return self._device.sys_info["at_low_battery"]
|
||||
is_low = self._device.sys_info.get(
|
||||
"at_low_battery", self._device.sys_info.get("is_low")
|
||||
)
|
||||
if is_low is None:
|
||||
raise KasaException("Device does not report battery low status")
|
||||
|
||||
return is_low
|
||||
|
@ -38,6 +38,7 @@ Plug 3: False
|
||||
True
|
||||
"""
|
||||
|
||||
from ...device_type import DeviceType
|
||||
from ..smartmodule import SmartModule
|
||||
|
||||
|
||||
@ -46,3 +47,10 @@ class ChildDevice(SmartModule):
|
||||
|
||||
REQUIRED_COMPONENT = "child_device"
|
||||
QUERY_GETTER_NAME = "get_child_device_list"
|
||||
|
||||
def query(self) -> dict:
|
||||
"""Query to execute during the update cycle."""
|
||||
q = super().query()
|
||||
if self._device.device_type is DeviceType.Hub:
|
||||
q["get_child_device_component_list"] = None
|
||||
return q
|
||||
|
37
kasa/smart/modules/childlock.py
Normal file
37
kasa/smart/modules/childlock.py
Normal file
@ -0,0 +1,37 @@
|
||||
"""Child lock module."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from ...feature import Feature
|
||||
from ..smartmodule import SmartModule
|
||||
|
||||
|
||||
class ChildLock(SmartModule):
|
||||
"""Implementation for child lock."""
|
||||
|
||||
REQUIRED_COMPONENT = "button_and_led"
|
||||
QUERY_GETTER_NAME = "getChildLockInfo"
|
||||
|
||||
def _initialize_features(self) -> None:
|
||||
"""Initialize features after the initial update."""
|
||||
self._add_feature(
|
||||
Feature(
|
||||
device=self._device,
|
||||
id="child_lock",
|
||||
name="Child lock",
|
||||
container=self,
|
||||
attribute_getter="enabled",
|
||||
attribute_setter="set_enabled",
|
||||
type=Feature.Type.Switch,
|
||||
category=Feature.Category.Config,
|
||||
)
|
||||
)
|
||||
|
||||
@property
|
||||
def enabled(self) -> bool:
|
||||
"""Return True if child lock is enabled."""
|
||||
return self.data["child_lock_status"]
|
||||
|
||||
async def set_enabled(self, enabled: bool) -> dict:
|
||||
"""Set child lock."""
|
||||
return await self.call("setChildLockInfo", {"child_lock_status": enabled})
|
112
kasa/smart/modules/childsetup.py
Normal file
112
kasa/smart/modules/childsetup.py
Normal file
@ -0,0 +1,112 @@
|
||||
"""Implementation for child device setup.
|
||||
|
||||
This module allows pairing and disconnecting child devices.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
|
||||
from ...feature import Feature
|
||||
from ...interfaces.childsetup import ChildSetup as ChildSetupInterface
|
||||
from ..smartmodule import SmartModule
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class ChildSetup(SmartModule, ChildSetupInterface):
|
||||
"""Implementation for child device setup."""
|
||||
|
||||
REQUIRED_COMPONENT = "child_quick_setup"
|
||||
QUERY_GETTER_NAME = "get_support_child_device_category"
|
||||
_categories: list[str] = []
|
||||
|
||||
# Supported child device categories will hardly ever change
|
||||
MINIMUM_UPDATE_INTERVAL_SECS = 60 * 60 * 24
|
||||
|
||||
def _initialize_features(self) -> None:
|
||||
"""Initialize features."""
|
||||
self._add_feature(
|
||||
Feature(
|
||||
self._device,
|
||||
id="pair",
|
||||
name="Pair",
|
||||
container=self,
|
||||
attribute_setter="pair",
|
||||
category=Feature.Category.Config,
|
||||
type=Feature.Type.Action,
|
||||
)
|
||||
)
|
||||
|
||||
async def _post_update_hook(self) -> None:
|
||||
self._categories = [
|
||||
cat["category"] for cat in self.data["device_category_list"]
|
||||
]
|
||||
|
||||
@property
|
||||
def supported_categories(self) -> list[str]:
|
||||
"""Supported child device categories."""
|
||||
return self._categories
|
||||
|
||||
async def pair(self, *, timeout: int = 10) -> list[dict]:
|
||||
"""Scan for new devices and pair them."""
|
||||
await self.call("begin_scanning_child_device")
|
||||
|
||||
_LOGGER.info("Waiting %s seconds for discovering new devices", timeout)
|
||||
await asyncio.sleep(timeout)
|
||||
detected = await self._get_detected_devices()
|
||||
|
||||
if not detected["child_device_list"]:
|
||||
_LOGGER.warning(
|
||||
"No devices found, make sure to activate pairing "
|
||||
"mode on the devices to be added."
|
||||
)
|
||||
return []
|
||||
|
||||
_LOGGER.info(
|
||||
"Discovery done, found %s devices: %s",
|
||||
len(detected["child_device_list"]),
|
||||
detected,
|
||||
)
|
||||
|
||||
return await self._add_devices(detected)
|
||||
|
||||
async def unpair(self, device_id: str) -> dict:
|
||||
"""Remove device from the hub."""
|
||||
_LOGGER.info("Going to unpair %s from %s", device_id, self)
|
||||
|
||||
payload = {"child_device_list": [{"device_id": device_id}]}
|
||||
res = await self.call("remove_child_device_list", payload)
|
||||
await self._device.update()
|
||||
return res
|
||||
|
||||
async def _add_devices(self, devices: dict) -> list[dict]:
|
||||
"""Add devices based on get_detected_device response.
|
||||
|
||||
Pass the output from :ref:_get_detected_devices: as a parameter.
|
||||
"""
|
||||
await self.call("add_child_device_list", devices)
|
||||
|
||||
await self._device.update()
|
||||
|
||||
successes = []
|
||||
for detected in devices["child_device_list"]:
|
||||
device_id = detected["device_id"]
|
||||
|
||||
result = "not added"
|
||||
if device_id in self._device._children:
|
||||
result = "added"
|
||||
successes.append(detected)
|
||||
|
||||
msg = f"{detected['device_model']} - {device_id} - {result}"
|
||||
_LOGGER.info("Added child to %s: %s", self._device.host, msg)
|
||||
|
||||
return successes
|
||||
|
||||
async def _get_detected_devices(self) -> dict:
|
||||
"""Return list of devices detected during scanning."""
|
||||
param = {"scan_list": self.data["device_category_list"]}
|
||||
res = await self.call("get_scan_child_device_list", param)
|
||||
_LOGGER.debug("Scan status: %s", res)
|
||||
return res["get_scan_child_device_list"]
|
411
kasa/smart/modules/clean.py
Normal file
411
kasa/smart/modules/clean.py
Normal file
@ -0,0 +1,411 @@
|
||||
"""Implementation of vacuum clean module."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from datetime import timedelta
|
||||
from enum import IntEnum
|
||||
from typing import Annotated, Literal
|
||||
|
||||
from ...feature import Feature
|
||||
from ...module import FeatureAttribute
|
||||
from ..smartmodule import SmartModule
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class Status(IntEnum):
|
||||
"""Status of vacuum."""
|
||||
|
||||
Idle = 0
|
||||
Cleaning = 1
|
||||
Mapping = 2
|
||||
GoingHome = 4
|
||||
Charging = 5
|
||||
Charged = 6
|
||||
Paused = 7
|
||||
Undocked = 8
|
||||
Error = 100
|
||||
|
||||
UnknownInternal = -1000
|
||||
|
||||
|
||||
class ErrorCode(IntEnum):
|
||||
"""Error codes for vacuum."""
|
||||
|
||||
Ok = 0
|
||||
SideBrushStuck = 2
|
||||
MainBrushStuck = 3
|
||||
WheelBlocked = 4
|
||||
Trapped = 6
|
||||
TrappedCliff = 7
|
||||
DustBinRemoved = 14
|
||||
UnableToMove = 15
|
||||
LidarBlocked = 16
|
||||
UnableToFindDock = 21
|
||||
BatteryLow = 22
|
||||
|
||||
UnknownInternal = -1000
|
||||
|
||||
|
||||
class FanSpeed(IntEnum):
|
||||
"""Fan speed level."""
|
||||
|
||||
Quiet = 1
|
||||
Standard = 2
|
||||
Turbo = 3
|
||||
Max = 4
|
||||
Ultra = 5
|
||||
|
||||
|
||||
class AreaUnit(IntEnum):
|
||||
"""Area unit."""
|
||||
|
||||
#: Square meter
|
||||
Sqm = 0
|
||||
#: Square feet
|
||||
Sqft = 1
|
||||
#: Taiwanese unit: https://en.wikipedia.org/wiki/Taiwanese_units_of_measurement#Area
|
||||
Ping = 2
|
||||
|
||||
|
||||
class Clean(SmartModule):
|
||||
"""Implementation of vacuum clean module."""
|
||||
|
||||
REQUIRED_COMPONENT = "clean"
|
||||
_error_code = ErrorCode.Ok
|
||||
_logged_error_code_warnings: set | None = None
|
||||
_logged_status_code_warnings: set
|
||||
|
||||
def _initialize_features(self) -> None:
|
||||
"""Initialize features."""
|
||||
self._add_feature(
|
||||
Feature(
|
||||
self._device,
|
||||
id="vacuum_return_home",
|
||||
name="Return home",
|
||||
container=self,
|
||||
attribute_setter="return_home",
|
||||
category=Feature.Category.Primary,
|
||||
type=Feature.Action,
|
||||
)
|
||||
)
|
||||
self._add_feature(
|
||||
Feature(
|
||||
self._device,
|
||||
id="vacuum_start",
|
||||
name="Start cleaning",
|
||||
container=self,
|
||||
attribute_setter="start",
|
||||
category=Feature.Category.Primary,
|
||||
type=Feature.Action,
|
||||
)
|
||||
)
|
||||
self._add_feature(
|
||||
Feature(
|
||||
self._device,
|
||||
id="vacuum_pause",
|
||||
name="Pause",
|
||||
container=self,
|
||||
attribute_setter="pause",
|
||||
category=Feature.Category.Primary,
|
||||
type=Feature.Action,
|
||||
)
|
||||
)
|
||||
self._add_feature(
|
||||
Feature(
|
||||
self._device,
|
||||
id="vacuum_status",
|
||||
name="Vacuum status",
|
||||
container=self,
|
||||
attribute_getter="status",
|
||||
category=Feature.Category.Primary,
|
||||
type=Feature.Type.Sensor,
|
||||
)
|
||||
)
|
||||
self._add_feature(
|
||||
Feature(
|
||||
self._device,
|
||||
id="vacuum_error",
|
||||
name="Error",
|
||||
container=self,
|
||||
attribute_getter="error",
|
||||
category=Feature.Category.Info,
|
||||
type=Feature.Type.Sensor,
|
||||
)
|
||||
)
|
||||
self._add_feature(
|
||||
Feature(
|
||||
self._device,
|
||||
id="battery_level",
|
||||
name="Battery level",
|
||||
container=self,
|
||||
attribute_getter="battery",
|
||||
icon="mdi:battery",
|
||||
unit_getter=lambda: "%",
|
||||
category=Feature.Category.Info,
|
||||
type=Feature.Type.Sensor,
|
||||
)
|
||||
)
|
||||
self._add_feature(
|
||||
Feature(
|
||||
self._device,
|
||||
id="vacuum_fan_speed",
|
||||
name="Fan speed",
|
||||
container=self,
|
||||
attribute_getter="fan_speed_preset",
|
||||
attribute_setter="set_fan_speed_preset",
|
||||
icon="mdi:fan",
|
||||
choices_getter=lambda: list(FanSpeed.__members__),
|
||||
category=Feature.Category.Primary,
|
||||
type=Feature.Type.Choice,
|
||||
)
|
||||
)
|
||||
self._add_feature(
|
||||
Feature(
|
||||
self._device,
|
||||
id="clean_count",
|
||||
name="Clean count",
|
||||
container=self,
|
||||
attribute_getter="clean_count",
|
||||
attribute_setter="set_clean_count",
|
||||
range_getter=lambda: (1, 3),
|
||||
category=Feature.Category.Config,
|
||||
type=Feature.Type.Number,
|
||||
)
|
||||
)
|
||||
self._add_feature(
|
||||
Feature(
|
||||
self._device,
|
||||
id="carpet_boost",
|
||||
name="Carpet boost",
|
||||
container=self,
|
||||
attribute_getter="carpet_boost",
|
||||
attribute_setter="set_carpet_boost",
|
||||
icon="mdi:rug",
|
||||
category=Feature.Category.Config,
|
||||
type=Feature.Type.Switch,
|
||||
)
|
||||
)
|
||||
self._add_feature(
|
||||
Feature(
|
||||
self._device,
|
||||
id="clean_area",
|
||||
name="Cleaning area",
|
||||
container=self,
|
||||
attribute_getter="clean_area",
|
||||
unit_getter="area_unit",
|
||||
category=Feature.Category.Info,
|
||||
type=Feature.Type.Sensor,
|
||||
)
|
||||
)
|
||||
self._add_feature(
|
||||
Feature(
|
||||
self._device,
|
||||
id="clean_time",
|
||||
name="Cleaning time",
|
||||
container=self,
|
||||
attribute_getter="clean_time",
|
||||
category=Feature.Category.Info,
|
||||
type=Feature.Type.Sensor,
|
||||
)
|
||||
)
|
||||
self._add_feature(
|
||||
Feature(
|
||||
self._device,
|
||||
id="clean_progress",
|
||||
name="Cleaning progress",
|
||||
container=self,
|
||||
attribute_getter="clean_progress",
|
||||
unit_getter=lambda: "%",
|
||||
category=Feature.Category.Info,
|
||||
type=Feature.Type.Sensor,
|
||||
)
|
||||
)
|
||||
|
||||
async def _post_update_hook(self) -> None:
|
||||
"""Set error code after update."""
|
||||
if self._logged_error_code_warnings is None:
|
||||
self._logged_error_code_warnings = set()
|
||||
self._logged_status_code_warnings = set()
|
||||
|
||||
errors = self._vac_status.get("err_status")
|
||||
if errors is None or not errors:
|
||||
self._error_code = ErrorCode.Ok
|
||||
return
|
||||
|
||||
if len(errors) > 1 and "multiple" not in self._logged_error_code_warnings:
|
||||
self._logged_error_code_warnings.add("multiple")
|
||||
_LOGGER.warning(
|
||||
"Multiple error codes, using the first one only: %s", errors
|
||||
)
|
||||
|
||||
error = errors.pop(0)
|
||||
try:
|
||||
self._error_code = ErrorCode(error)
|
||||
except ValueError:
|
||||
if error not in self._logged_error_code_warnings:
|
||||
self._logged_error_code_warnings.add(error)
|
||||
_LOGGER.warning(
|
||||
"Unknown error code, please create an issue "
|
||||
"describing the error: %s",
|
||||
error,
|
||||
)
|
||||
self._error_code = ErrorCode.UnknownInternal
|
||||
|
||||
def query(self) -> dict:
|
||||
"""Query to execute during the update cycle."""
|
||||
return {
|
||||
"getVacStatus": {},
|
||||
"getCleanInfo": {},
|
||||
"getCarpetClean": {},
|
||||
"getAreaUnit": {},
|
||||
"getBatteryInfo": {},
|
||||
"getCleanStatus": {},
|
||||
"getCleanAttr": {"type": "global"},
|
||||
}
|
||||
|
||||
async def start(self) -> dict:
|
||||
"""Start cleaning."""
|
||||
# If we are paused, do not restart cleaning
|
||||
|
||||
if self.status is Status.Paused:
|
||||
return await self.resume()
|
||||
|
||||
return await self.call(
|
||||
"setSwitchClean",
|
||||
{
|
||||
"clean_mode": 0,
|
||||
"clean_on": True,
|
||||
"clean_order": True,
|
||||
"force_clean": False,
|
||||
},
|
||||
)
|
||||
|
||||
async def pause(self) -> dict:
|
||||
"""Pause cleaning."""
|
||||
if self.status is Status.GoingHome:
|
||||
return await self.set_return_home(False)
|
||||
|
||||
return await self.set_pause(True)
|
||||
|
||||
async def resume(self) -> dict:
|
||||
"""Resume cleaning."""
|
||||
return await self.set_pause(False)
|
||||
|
||||
async def set_pause(self, enabled: bool) -> dict:
|
||||
"""Pause or resume cleaning."""
|
||||
return await self.call("setRobotPause", {"pause": enabled})
|
||||
|
||||
async def return_home(self) -> dict:
|
||||
"""Return home."""
|
||||
return await self.set_return_home(True)
|
||||
|
||||
async def set_return_home(self, enabled: bool) -> dict:
|
||||
"""Return home / pause returning."""
|
||||
return await self.call("setSwitchCharge", {"switch_charge": enabled})
|
||||
|
||||
@property
|
||||
def error(self) -> ErrorCode:
|
||||
"""Return error."""
|
||||
return self._error_code
|
||||
|
||||
@property
|
||||
def fan_speed_preset(self) -> Annotated[str, FeatureAttribute()]:
|
||||
"""Return fan speed preset."""
|
||||
return FanSpeed(self._settings["suction"]).name
|
||||
|
||||
async def set_fan_speed_preset(
|
||||
self, speed: str
|
||||
) -> Annotated[dict, FeatureAttribute]:
|
||||
"""Set fan speed preset."""
|
||||
name_to_value = {x.name: x.value for x in FanSpeed}
|
||||
if speed not in name_to_value:
|
||||
raise ValueError("Invalid fan speed %s, available %s", speed, name_to_value)
|
||||
return await self._change_setting("suction", name_to_value[speed])
|
||||
|
||||
async def _change_setting(
|
||||
self, name: str, value: int, *, scope: Literal["global", "pose"] = "global"
|
||||
) -> dict:
|
||||
"""Change device setting."""
|
||||
params = {
|
||||
name: value,
|
||||
"type": scope,
|
||||
}
|
||||
return await self.call("setCleanAttr", params)
|
||||
|
||||
@property
|
||||
def battery(self) -> int:
|
||||
"""Return battery level."""
|
||||
return self.data["getBatteryInfo"]["battery_percentage"]
|
||||
|
||||
@property
|
||||
def _vac_status(self) -> dict:
|
||||
"""Return vac status container."""
|
||||
return self.data["getVacStatus"]
|
||||
|
||||
@property
|
||||
def _info(self) -> dict:
|
||||
"""Return current cleaning info."""
|
||||
return self.data["getCleanInfo"]
|
||||
|
||||
@property
|
||||
def _settings(self) -> dict:
|
||||
"""Return cleaning settings."""
|
||||
return self.data["getCleanAttr"]
|
||||
|
||||
@property
|
||||
def status(self) -> Status:
|
||||
"""Return current status."""
|
||||
if self._error_code is not ErrorCode.Ok:
|
||||
return Status.Error
|
||||
|
||||
status_code = self._vac_status["status"]
|
||||
try:
|
||||
return Status(status_code)
|
||||
except ValueError:
|
||||
if status_code not in self._logged_status_code_warnings:
|
||||
self._logged_status_code_warnings.add(status_code)
|
||||
_LOGGER.warning(
|
||||
"Got unknown status code: %s (%s)", status_code, self.data
|
||||
)
|
||||
return Status.UnknownInternal
|
||||
|
||||
@property
|
||||
def carpet_boost(self) -> bool:
|
||||
"""Return carpet boost mode."""
|
||||
return self.data["getCarpetClean"]["carpet_clean_prefer"] == "boost"
|
||||
|
||||
async def set_carpet_boost(self, on: bool) -> dict:
|
||||
"""Set carpet clean mode."""
|
||||
mode = "boost" if on else "normal"
|
||||
return await self.call("setCarpetClean", {"carpet_clean_prefer": mode})
|
||||
|
||||
@property
|
||||
def area_unit(self) -> AreaUnit:
|
||||
"""Return area unit."""
|
||||
return AreaUnit(self.data["getAreaUnit"]["area_unit"])
|
||||
|
||||
@property
|
||||
def clean_area(self) -> Annotated[int, FeatureAttribute()]:
|
||||
"""Return currently cleaned area."""
|
||||
return self._info["clean_area"]
|
||||
|
||||
@property
|
||||
def clean_time(self) -> timedelta:
|
||||
"""Return current cleaning time."""
|
||||
return timedelta(minutes=self._info["clean_time"])
|
||||
|
||||
@property
|
||||
def clean_progress(self) -> int:
|
||||
"""Return amount of currently cleaned area."""
|
||||
return self._info["clean_percent"]
|
||||
|
||||
@property
|
||||
def clean_count(self) -> Annotated[int, FeatureAttribute()]:
|
||||
"""Return number of times to clean."""
|
||||
return self._settings["clean_number"]
|
||||
|
||||
async def set_clean_count(self, count: int) -> Annotated[dict, FeatureAttribute()]:
|
||||
"""Set number of times to clean."""
|
||||
return await self._change_setting("clean_number", count)
|
205
kasa/smart/modules/cleanrecords.py
Normal file
205
kasa/smart/modules/cleanrecords.py
Normal file
@ -0,0 +1,205 @@
|
||||
"""Implementation of vacuum cleaning records."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from dataclasses import dataclass, field
|
||||
from datetime import datetime, timedelta, tzinfo
|
||||
from typing import Annotated, cast
|
||||
|
||||
from mashumaro import DataClassDictMixin, field_options
|
||||
from mashumaro.config import ADD_DIALECT_SUPPORT
|
||||
from mashumaro.dialect import Dialect
|
||||
from mashumaro.types import SerializationStrategy
|
||||
|
||||
from ...feature import Feature
|
||||
from ...module import FeatureAttribute
|
||||
from ..smartmodule import Module, SmartModule
|
||||
from .clean import AreaUnit, Clean
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@dataclass
|
||||
class Record(DataClassDictMixin):
|
||||
"""Historical cleanup result."""
|
||||
|
||||
class Config:
|
||||
"""Configuration class."""
|
||||
|
||||
code_generation_options = [ADD_DIALECT_SUPPORT]
|
||||
|
||||
#: Total time cleaned (in minutes)
|
||||
clean_time: timedelta = field(
|
||||
metadata=field_options(deserialize=lambda x: timedelta(minutes=x))
|
||||
)
|
||||
#: Total area cleaned
|
||||
clean_area: int
|
||||
dust_collection: bool
|
||||
timestamp: datetime
|
||||
|
||||
info_num: int | None = None
|
||||
message: int | None = None
|
||||
map_id: int | None = None
|
||||
start_type: int | None = None
|
||||
task_type: int | None = None
|
||||
record_index: int | None = None
|
||||
|
||||
#: Error code from cleaning
|
||||
error: int = field(default=0)
|
||||
|
||||
|
||||
class _DateTimeSerializationStrategy(SerializationStrategy):
|
||||
def __init__(self, tz: tzinfo) -> None:
|
||||
self.tz = tz
|
||||
|
||||
def deserialize(self, value: float) -> datetime:
|
||||
return datetime.fromtimestamp(value, self.tz)
|
||||
|
||||
|
||||
def _get_tz_strategy(tz: tzinfo) -> type[Dialect]:
|
||||
"""Return a timezone aware de-serialization strategy."""
|
||||
|
||||
class TimezoneDialect(Dialect):
|
||||
serialization_strategy = {datetime: _DateTimeSerializationStrategy(tz)}
|
||||
|
||||
return TimezoneDialect
|
||||
|
||||
|
||||
@dataclass
|
||||
class Records(DataClassDictMixin):
|
||||
"""Response payload for getCleanRecords."""
|
||||
|
||||
class Config:
|
||||
"""Configuration class."""
|
||||
|
||||
code_generation_options = [ADD_DIALECT_SUPPORT]
|
||||
|
||||
total_time: timedelta = field(
|
||||
metadata=field_options(deserialize=lambda x: timedelta(minutes=x))
|
||||
)
|
||||
total_area: int
|
||||
total_count: int = field(metadata=field_options(alias="total_number"))
|
||||
|
||||
records: list[Record] = field(metadata=field_options(alias="record_list"))
|
||||
last_clean: Record = field(metadata=field_options(alias="lastest_day_record"))
|
||||
|
||||
@classmethod
|
||||
def __pre_deserialize__(cls, d: dict) -> dict:
|
||||
if ldr := d.get("lastest_day_record"):
|
||||
d["lastest_day_record"] = {
|
||||
"timestamp": ldr[0],
|
||||
"clean_time": ldr[1],
|
||||
"clean_area": ldr[2],
|
||||
"dust_collection": ldr[3],
|
||||
}
|
||||
return d
|
||||
|
||||
|
||||
class CleanRecords(SmartModule):
|
||||
"""Implementation of vacuum cleaning records."""
|
||||
|
||||
REQUIRED_COMPONENT = "clean_percent"
|
||||
_parsed_data: Records
|
||||
|
||||
async def _post_update_hook(self) -> None:
|
||||
"""Cache parsed data after an update."""
|
||||
self._parsed_data = Records.from_dict(
|
||||
self.data, dialect=_get_tz_strategy(self._device.timezone)
|
||||
)
|
||||
|
||||
def _initialize_features(self) -> None:
|
||||
"""Initialize features."""
|
||||
for type_ in ["total", "last"]:
|
||||
self._add_feature(
|
||||
Feature(
|
||||
self._device,
|
||||
id=f"{type_}_clean_area",
|
||||
name=f"{type_.capitalize()} area cleaned",
|
||||
container=self,
|
||||
attribute_getter=f"{type_}_clean_area",
|
||||
unit_getter="area_unit",
|
||||
category=Feature.Category.Debug,
|
||||
type=Feature.Type.Sensor,
|
||||
)
|
||||
)
|
||||
self._add_feature(
|
||||
Feature(
|
||||
self._device,
|
||||
id=f"{type_}_clean_time",
|
||||
name=f"{type_.capitalize()} time cleaned",
|
||||
container=self,
|
||||
attribute_getter=f"{type_}_clean_time",
|
||||
category=Feature.Category.Debug,
|
||||
type=Feature.Type.Sensor,
|
||||
)
|
||||
)
|
||||
self._add_feature(
|
||||
Feature(
|
||||
self._device,
|
||||
id="total_clean_count",
|
||||
name="Total clean count",
|
||||
container=self,
|
||||
attribute_getter="total_clean_count",
|
||||
category=Feature.Category.Debug,
|
||||
type=Feature.Type.Sensor,
|
||||
)
|
||||
)
|
||||
self._add_feature(
|
||||
Feature(
|
||||
self._device,
|
||||
id="last_clean_timestamp",
|
||||
name="Last clean timestamp",
|
||||
container=self,
|
||||
attribute_getter="last_clean_timestamp",
|
||||
category=Feature.Category.Debug,
|
||||
type=Feature.Type.Sensor,
|
||||
)
|
||||
)
|
||||
|
||||
def query(self) -> dict:
|
||||
"""Query to execute during the update cycle."""
|
||||
return {
|
||||
"getCleanRecords": {},
|
||||
}
|
||||
|
||||
@property
|
||||
def total_clean_area(self) -> Annotated[int, FeatureAttribute()]:
|
||||
"""Return total cleaning area."""
|
||||
return self._parsed_data.total_area
|
||||
|
||||
@property
|
||||
def total_clean_time(self) -> timedelta:
|
||||
"""Return total cleaning time."""
|
||||
return self._parsed_data.total_time
|
||||
|
||||
@property
|
||||
def total_clean_count(self) -> int:
|
||||
"""Return total clean count."""
|
||||
return self._parsed_data.total_count
|
||||
|
||||
@property
|
||||
def last_clean_area(self) -> Annotated[int, FeatureAttribute()]:
|
||||
"""Return latest cleaning area."""
|
||||
return self._parsed_data.last_clean.clean_area
|
||||
|
||||
@property
|
||||
def last_clean_time(self) -> timedelta:
|
||||
"""Return total cleaning time."""
|
||||
return self._parsed_data.last_clean.clean_time
|
||||
|
||||
@property
|
||||
def last_clean_timestamp(self) -> datetime:
|
||||
"""Return latest cleaning timestamp."""
|
||||
return self._parsed_data.last_clean.timestamp
|
||||
|
||||
@property
|
||||
def area_unit(self) -> AreaUnit:
|
||||
"""Return area unit."""
|
||||
clean = cast(Clean, self._device.modules[Module.Clean])
|
||||
return clean.area_unit
|
||||
|
||||
@property
|
||||
def parsed_data(self) -> Records:
|
||||
"""Return parsed records data."""
|
||||
return self._parsed_data
|
170
kasa/smart/modules/consumables.py
Normal file
170
kasa/smart/modules/consumables.py
Normal file
@ -0,0 +1,170 @@
|
||||
"""Implementation of vacuum consumables."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from collections.abc import Mapping
|
||||
from dataclasses import dataclass
|
||||
from datetime import timedelta
|
||||
|
||||
from ...feature import Feature
|
||||
from ..smartmodule import SmartModule
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@dataclass
|
||||
class _ConsumableMeta:
|
||||
"""Consumable meta container."""
|
||||
|
||||
#: Name of the consumable.
|
||||
name: str
|
||||
#: Internal id of the consumable
|
||||
id: str
|
||||
#: Data key in the device reported data
|
||||
data_key: str
|
||||
#: Lifetime
|
||||
lifetime: timedelta
|
||||
|
||||
|
||||
@dataclass
|
||||
class Consumable:
|
||||
"""Consumable container."""
|
||||
|
||||
#: Name of the consumable.
|
||||
name: str
|
||||
#: Id of the consumable
|
||||
id: str
|
||||
#: Lifetime
|
||||
lifetime: timedelta
|
||||
#: Used
|
||||
used: timedelta
|
||||
#: Remaining
|
||||
remaining: timedelta
|
||||
#: Device data key
|
||||
_data_key: str
|
||||
|
||||
|
||||
CONSUMABLE_METAS = [
|
||||
_ConsumableMeta(
|
||||
"Main brush",
|
||||
id="main_brush",
|
||||
data_key="roll_brush_time",
|
||||
lifetime=timedelta(hours=400),
|
||||
),
|
||||
_ConsumableMeta(
|
||||
"Side brush",
|
||||
id="side_brush",
|
||||
data_key="edge_brush_time",
|
||||
lifetime=timedelta(hours=200),
|
||||
),
|
||||
_ConsumableMeta(
|
||||
"Filter",
|
||||
id="filter",
|
||||
data_key="filter_time",
|
||||
lifetime=timedelta(hours=200),
|
||||
),
|
||||
_ConsumableMeta(
|
||||
"Sensor",
|
||||
id="sensor",
|
||||
data_key="sensor_time",
|
||||
lifetime=timedelta(hours=30),
|
||||
),
|
||||
_ConsumableMeta(
|
||||
"Charging contacts",
|
||||
id="charging_contacts",
|
||||
data_key="charge_contact_time",
|
||||
lifetime=timedelta(hours=30),
|
||||
),
|
||||
# Unknown keys: main_brush_lid_time, rag_time
|
||||
]
|
||||
|
||||
|
||||
class Consumables(SmartModule):
|
||||
"""Implementation of vacuum consumables."""
|
||||
|
||||
REQUIRED_COMPONENT = "consumables"
|
||||
QUERY_GETTER_NAME = "getConsumablesInfo"
|
||||
|
||||
_consumables: dict[str, Consumable] = {}
|
||||
|
||||
def _initialize_features(self) -> None:
|
||||
"""Initialize features."""
|
||||
for c_meta in CONSUMABLE_METAS:
|
||||
if c_meta.data_key not in self.data:
|
||||
continue
|
||||
|
||||
self._add_feature(
|
||||
Feature(
|
||||
self._device,
|
||||
id=f"{c_meta.id}_used",
|
||||
name=f"{c_meta.name} used",
|
||||
container=self,
|
||||
attribute_getter=lambda _, c_id=c_meta.id: self._consumables[
|
||||
c_id
|
||||
].used,
|
||||
category=Feature.Category.Debug,
|
||||
type=Feature.Type.Sensor,
|
||||
)
|
||||
)
|
||||
|
||||
self._add_feature(
|
||||
Feature(
|
||||
self._device,
|
||||
id=f"{c_meta.id}_remaining",
|
||||
name=f"{c_meta.name} remaining",
|
||||
container=self,
|
||||
attribute_getter=lambda _, c_id=c_meta.id: self._consumables[
|
||||
c_id
|
||||
].remaining,
|
||||
category=Feature.Category.Info,
|
||||
type=Feature.Type.Sensor,
|
||||
)
|
||||
)
|
||||
|
||||
self._add_feature(
|
||||
Feature(
|
||||
self._device,
|
||||
id=f"{c_meta.id}_reset",
|
||||
name=f"Reset {c_meta.name.lower()} consumable",
|
||||
container=self,
|
||||
attribute_setter=lambda c_id=c_meta.id: self.reset_consumable(c_id),
|
||||
category=Feature.Category.Debug,
|
||||
type=Feature.Type.Action,
|
||||
)
|
||||
)
|
||||
|
||||
async def _post_update_hook(self) -> None:
|
||||
"""Update the consumables."""
|
||||
if not self._consumables:
|
||||
for consumable_meta in CONSUMABLE_METAS:
|
||||
if consumable_meta.data_key not in self.data:
|
||||
continue
|
||||
used = timedelta(minutes=self.data[consumable_meta.data_key])
|
||||
consumable = Consumable(
|
||||
id=consumable_meta.id,
|
||||
name=consumable_meta.name,
|
||||
lifetime=consumable_meta.lifetime,
|
||||
used=used,
|
||||
remaining=consumable_meta.lifetime - used,
|
||||
_data_key=consumable_meta.data_key,
|
||||
)
|
||||
self._consumables[consumable_meta.id] = consumable
|
||||
else:
|
||||
for consumable in self._consumables.values():
|
||||
consumable.used = timedelta(minutes=self.data[consumable._data_key])
|
||||
consumable.remaining = consumable.lifetime - consumable.used
|
||||
|
||||
async def reset_consumable(self, consumable_id: str) -> dict:
|
||||
"""Reset consumable stats."""
|
||||
consumable_name = self._consumables[consumable_id]._data_key.removesuffix(
|
||||
"_time"
|
||||
)
|
||||
return await self.call(
|
||||
"resetConsumablesTime", {"reset_list": [consumable_name]}
|
||||
)
|
||||
|
||||
@property
|
||||
def consumables(self) -> Mapping[str, Consumable]:
|
||||
"""Get list of consumables on the device."""
|
||||
return self._consumables
|
@ -19,12 +19,15 @@ class DeviceModule(SmartModule):
|
||||
|
||||
def query(self) -> dict:
|
||||
"""Query to execute during the update cycle."""
|
||||
if self._device._is_hub_child:
|
||||
# Child devices get their device info updated by the parent device.
|
||||
return {}
|
||||
query = {
|
||||
"get_device_info": None,
|
||||
}
|
||||
# Device usage is not available on older firmware versions
|
||||
# or child devices of hubs
|
||||
if self.supported_version >= 2 and not self._device._is_hub_child:
|
||||
if self.supported_version >= 2:
|
||||
query["get_device_usage"] = None
|
||||
|
||||
return query
|
||||
|
127
kasa/smart/modules/dustbin.py
Normal file
127
kasa/smart/modules/dustbin.py
Normal file
@ -0,0 +1,127 @@
|
||||
"""Implementation of vacuum dustbin."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from enum import IntEnum
|
||||
|
||||
from ...feature import Feature
|
||||
from ..smartmodule import SmartModule
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class Mode(IntEnum):
|
||||
"""Dust collection modes."""
|
||||
|
||||
Smart = 0
|
||||
Light = 1
|
||||
Balanced = 2
|
||||
Max = 3
|
||||
|
||||
Off = -1_000
|
||||
|
||||
|
||||
class Dustbin(SmartModule):
|
||||
"""Implementation of vacuum dustbin."""
|
||||
|
||||
REQUIRED_COMPONENT = "dust_bucket"
|
||||
|
||||
def _initialize_features(self) -> None:
|
||||
"""Initialize features."""
|
||||
self._add_feature(
|
||||
Feature(
|
||||
self._device,
|
||||
id="dustbin_empty",
|
||||
name="Empty dustbin",
|
||||
container=self,
|
||||
attribute_setter="start_emptying",
|
||||
category=Feature.Category.Config,
|
||||
type=Feature.Action,
|
||||
)
|
||||
)
|
||||
|
||||
self._add_feature(
|
||||
Feature(
|
||||
self._device,
|
||||
id="dustbin_autocollection_enabled",
|
||||
name="Automatic emptying enabled",
|
||||
container=self,
|
||||
attribute_getter="auto_collection",
|
||||
attribute_setter="set_auto_collection",
|
||||
category=Feature.Category.Config,
|
||||
type=Feature.Switch,
|
||||
)
|
||||
)
|
||||
|
||||
self._add_feature(
|
||||
Feature(
|
||||
self._device,
|
||||
id="dustbin_mode",
|
||||
name="Automatic emptying mode",
|
||||
container=self,
|
||||
attribute_getter="mode",
|
||||
attribute_setter="set_mode",
|
||||
icon="mdi:fan",
|
||||
choices_getter=lambda: list(Mode.__members__),
|
||||
category=Feature.Category.Config,
|
||||
type=Feature.Type.Choice,
|
||||
)
|
||||
)
|
||||
|
||||
def query(self) -> dict:
|
||||
"""Query to execute during the update cycle."""
|
||||
return {
|
||||
"getAutoDustCollection": {},
|
||||
"getDustCollectionInfo": {},
|
||||
}
|
||||
|
||||
async def start_emptying(self) -> dict:
|
||||
"""Start emptying the bin."""
|
||||
return await self.call(
|
||||
"setSwitchDustCollection",
|
||||
{
|
||||
"switch_dust_collection": True,
|
||||
},
|
||||
)
|
||||
|
||||
@property
|
||||
def _settings(self) -> dict:
|
||||
"""Return auto-empty settings."""
|
||||
return self.data["getDustCollectionInfo"]
|
||||
|
||||
@property
|
||||
def mode(self) -> str:
|
||||
"""Return auto-emptying mode."""
|
||||
if self.auto_collection is False:
|
||||
return Mode.Off.name
|
||||
return Mode(self._settings["dust_collection_mode"]).name
|
||||
|
||||
async def set_mode(self, mode: str) -> dict:
|
||||
"""Set auto-emptying mode."""
|
||||
name_to_value = {x.name: x.value for x in Mode}
|
||||
if mode not in name_to_value:
|
||||
raise ValueError(
|
||||
"Invalid auto/emptying mode speed %s, available %s", mode, name_to_value
|
||||
)
|
||||
|
||||
if mode == Mode.Off.name:
|
||||
return await self.set_auto_collection(False)
|
||||
|
||||
# Make a copy just in case, even when we are overriding both settings
|
||||
settings = self._settings.copy()
|
||||
settings["auto_dust_collection"] = True
|
||||
settings["dust_collection_mode"] = name_to_value[mode]
|
||||
|
||||
return await self.call("setDustCollectionInfo", settings)
|
||||
|
||||
@property
|
||||
def auto_collection(self) -> dict:
|
||||
"""Return auto-emptying config."""
|
||||
return self._settings["auto_dust_collection"]
|
||||
|
||||
async def set_auto_collection(self, on: bool) -> dict:
|
||||
"""Toggle auto-emptying."""
|
||||
settings = self._settings.copy()
|
||||
settings["auto_dust_collection"] = on
|
||||
return await self.call("setDustCollectionInfo", settings)
|
@ -126,15 +126,17 @@ class Energy(SmartModule, EnergyInterface):
|
||||
@raise_if_update_error
|
||||
def current(self) -> float | None:
|
||||
"""Return the current in A."""
|
||||
ma = self.data.get("get_emeter_data", {}).get("current_ma")
|
||||
return ma / 1000 if ma else None
|
||||
if (ma := self.data.get("get_emeter_data", {}).get("current_ma")) is not None:
|
||||
return ma / 1_000
|
||||
return None
|
||||
|
||||
@property
|
||||
@raise_if_update_error
|
||||
def voltage(self) -> float | None:
|
||||
"""Get the current voltage in V."""
|
||||
mv = self.data.get("get_emeter_data", {}).get("voltage_mv")
|
||||
return mv / 1000 if mv else None
|
||||
if (mv := self.data.get("get_emeter_data", {}).get("voltage_mv")) is not None:
|
||||
return mv / 1_000
|
||||
return None
|
||||
|
||||
async def _deprecated_get_realtime(self) -> EmeterStatus:
|
||||
"""Retrieve current energy readings."""
|
||||
|
@ -7,7 +7,7 @@ from typing import Annotated
|
||||
|
||||
from ...exceptions import KasaException
|
||||
from ...feature import Feature
|
||||
from ...interfaces.light import HSV, ColorTempRange, LightState
|
||||
from ...interfaces.light import HSV, LightState
|
||||
from ...interfaces.light import Light as LightInterface
|
||||
from ...module import FeatureAttribute, Module
|
||||
from ..smartmodule import SmartModule
|
||||
@ -34,32 +34,6 @@ class Light(SmartModule, LightInterface):
|
||||
"""Query to execute during the update cycle."""
|
||||
return {}
|
||||
|
||||
@property
|
||||
def is_color(self) -> bool:
|
||||
"""Whether the bulb supports color changes."""
|
||||
return Module.Color in self._device.modules
|
||||
|
||||
@property
|
||||
def is_dimmable(self) -> bool:
|
||||
"""Whether the bulb supports brightness changes."""
|
||||
return Module.Brightness in self._device.modules
|
||||
|
||||
@property
|
||||
def is_variable_color_temp(self) -> bool:
|
||||
"""Whether the bulb supports color temperature changes."""
|
||||
return Module.ColorTemperature in self._device.modules
|
||||
|
||||
@property
|
||||
def valid_temperature_range(self) -> ColorTempRange:
|
||||
"""Return the device-specific white temperature range (in Kelvin).
|
||||
|
||||
:return: White temperature range in Kelvin (minimum, maximum)
|
||||
"""
|
||||
if Module.ColorTemperature not in self._device.modules:
|
||||
raise KasaException("Color temperature not supported")
|
||||
|
||||
return self._device.modules[Module.ColorTemperature].valid_temperature_range
|
||||
|
||||
@property
|
||||
def hsv(self) -> Annotated[HSV, FeatureAttribute()]:
|
||||
"""Return the current HSV state of the bulb.
|
||||
@ -82,7 +56,7 @@ class Light(SmartModule, LightInterface):
|
||||
@property
|
||||
def brightness(self) -> Annotated[int, FeatureAttribute()]:
|
||||
"""Return the current brightness in percentage."""
|
||||
if Module.Brightness not in self._device.modules:
|
||||
if Module.Brightness not in self._device.modules: # pragma: no cover
|
||||
raise KasaException("Bulb is not dimmable.")
|
||||
|
||||
return self._device.modules[Module.Brightness].brightness
|
||||
@ -135,16 +109,11 @@ class Light(SmartModule, LightInterface):
|
||||
:param int brightness: brightness in percent
|
||||
:param int transition: transition in milliseconds.
|
||||
"""
|
||||
if Module.Brightness not in self._device.modules:
|
||||
if Module.Brightness not in self._device.modules: # pragma: no cover
|
||||
raise KasaException("Bulb is not dimmable.")
|
||||
|
||||
return await self._device.modules[Module.Brightness].set_brightness(brightness)
|
||||
|
||||
@property
|
||||
def has_effects(self) -> bool:
|
||||
"""Return True if the device supports effects."""
|
||||
return Module.LightEffect in self._device.modules
|
||||
|
||||
async def set_state(self, state: LightState) -> dict:
|
||||
"""Set the light state."""
|
||||
state_dict = asdict(state)
|
||||
|
@ -37,20 +37,14 @@ class LightStripEffect(SmartModule, SmartLightEffect):
|
||||
|
||||
@property
|
||||
def effect(self) -> str:
|
||||
"""Return effect state.
|
||||
|
||||
Example:
|
||||
{'brightness': 50,
|
||||
'custom': 0,
|
||||
'enable': 0,
|
||||
'id': '',
|
||||
'name': ''}
|
||||
"""
|
||||
"""Return effect name."""
|
||||
eff = self.data["lighting_effect"]
|
||||
name = eff["name"]
|
||||
# When devices are unpaired effect name is softAP which is not in our list
|
||||
if eff["enable"] and name in self._effect_list:
|
||||
return name
|
||||
if eff["enable"] and eff["custom"]:
|
||||
return name or self.LIGHT_EFFECTS_UNNAMED_CUSTOM
|
||||
return self.LIGHT_EFFECTS_OFF
|
||||
|
||||
@property
|
||||
|
90
kasa/smart/modules/mop.py
Normal file
90
kasa/smart/modules/mop.py
Normal file
@ -0,0 +1,90 @@
|
||||
"""Implementation of vacuum mop."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from enum import IntEnum
|
||||
from typing import Annotated
|
||||
|
||||
from ...feature import Feature
|
||||
from ...module import FeatureAttribute
|
||||
from ..smartmodule import SmartModule
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class Waterlevel(IntEnum):
|
||||
"""Water level for mopping."""
|
||||
|
||||
Disable = 0
|
||||
Low = 1
|
||||
Medium = 2
|
||||
High = 3
|
||||
|
||||
|
||||
class Mop(SmartModule):
|
||||
"""Implementation of vacuum mop."""
|
||||
|
||||
REQUIRED_COMPONENT = "mop"
|
||||
|
||||
def _initialize_features(self) -> None:
|
||||
"""Initialize features."""
|
||||
self._add_feature(
|
||||
Feature(
|
||||
self._device,
|
||||
id="mop_attached",
|
||||
name="Mop attached",
|
||||
container=self,
|
||||
icon="mdi:square-rounded",
|
||||
attribute_getter="mop_attached",
|
||||
category=Feature.Category.Info,
|
||||
type=Feature.BinarySensor,
|
||||
)
|
||||
)
|
||||
|
||||
self._add_feature(
|
||||
Feature(
|
||||
self._device,
|
||||
id="mop_waterlevel",
|
||||
name="Mop water level",
|
||||
container=self,
|
||||
attribute_getter="waterlevel",
|
||||
attribute_setter="set_waterlevel",
|
||||
icon="mdi:water",
|
||||
choices_getter=lambda: list(Waterlevel.__members__),
|
||||
category=Feature.Category.Config,
|
||||
type=Feature.Type.Choice,
|
||||
)
|
||||
)
|
||||
|
||||
def query(self) -> dict:
|
||||
"""Query to execute during the update cycle."""
|
||||
return {
|
||||
"getMopState": {},
|
||||
"getCleanAttr": {"type": "global"},
|
||||
}
|
||||
|
||||
@property
|
||||
def mop_attached(self) -> bool:
|
||||
"""Return True if mop is attached."""
|
||||
return self.data["getMopState"]["mop_state"]
|
||||
|
||||
@property
|
||||
def _settings(self) -> dict:
|
||||
"""Return settings settings."""
|
||||
return self.data["getCleanAttr"]
|
||||
|
||||
@property
|
||||
def waterlevel(self) -> Annotated[str, FeatureAttribute()]:
|
||||
"""Return water level."""
|
||||
return Waterlevel(int(self._settings["cistern"])).name
|
||||
|
||||
async def set_waterlevel(self, mode: str) -> Annotated[dict, FeatureAttribute()]:
|
||||
"""Set waterlevel mode."""
|
||||
name_to_value = {x.name: x.value for x in Waterlevel}
|
||||
if mode not in name_to_value:
|
||||
raise ValueError("Invalid waterlevel %s, available %s", mode, name_to_value)
|
||||
|
||||
settings = self._settings.copy()
|
||||
settings["cistern"] = name_to_value[mode]
|
||||
return await self.call("setCleanAttr", settings)
|
124
kasa/smart/modules/powerprotection.py
Normal file
124
kasa/smart/modules/powerprotection.py
Normal file
@ -0,0 +1,124 @@
|
||||
"""Power protection module."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Annotated
|
||||
|
||||
from ...feature import Feature
|
||||
from ...module import FeatureAttribute
|
||||
from ..smartmodule import SmartModule
|
||||
|
||||
|
||||
class PowerProtection(SmartModule):
|
||||
"""Implementation for power_protection."""
|
||||
|
||||
REQUIRED_COMPONENT = "power_protection"
|
||||
|
||||
def _initialize_features(self) -> None:
|
||||
"""Initialize features after the initial update."""
|
||||
self._add_feature(
|
||||
Feature(
|
||||
device=self._device,
|
||||
id="overloaded",
|
||||
name="Overloaded",
|
||||
container=self,
|
||||
attribute_getter="overloaded",
|
||||
type=Feature.Type.BinarySensor,
|
||||
category=Feature.Category.Info,
|
||||
)
|
||||
)
|
||||
self._add_feature(
|
||||
Feature(
|
||||
device=self._device,
|
||||
id="power_protection_threshold",
|
||||
name="Power protection threshold",
|
||||
container=self,
|
||||
attribute_getter="_threshold_or_zero",
|
||||
attribute_setter="_set_threshold_auto_enable",
|
||||
unit_getter=lambda: "W",
|
||||
type=Feature.Type.Number,
|
||||
range_getter=lambda: (0, self._max_power),
|
||||
category=Feature.Category.Config,
|
||||
)
|
||||
)
|
||||
|
||||
def query(self) -> dict:
|
||||
"""Query to execute during the update cycle."""
|
||||
return {"get_protection_power": {}, "get_max_power": {}}
|
||||
|
||||
@property
|
||||
def overloaded(self) -> bool:
|
||||
"""Return True is power protection has been triggered.
|
||||
|
||||
This value remains True until the device is turned on again.
|
||||
"""
|
||||
return self._device.sys_info["power_protection_status"] == "overloaded"
|
||||
|
||||
@property
|
||||
def enabled(self) -> bool:
|
||||
"""Return True if child protection is enabled."""
|
||||
return self.data["get_protection_power"]["enabled"]
|
||||
|
||||
async def set_enabled(self, enabled: bool, *, threshold: int | None = None) -> dict:
|
||||
"""Set power protection enabled.
|
||||
|
||||
If power protection has never been enabled before the threshold will
|
||||
be 0 so if threshold is not provided it will be set to half the max.
|
||||
"""
|
||||
if threshold is None and enabled and self.protection_threshold == 0:
|
||||
threshold = int(self._max_power / 2)
|
||||
|
||||
if threshold and (threshold < 0 or threshold > self._max_power):
|
||||
raise ValueError(
|
||||
"Threshold out of range: %s (%s)", threshold, self.protection_threshold
|
||||
)
|
||||
|
||||
params = {**self.data["get_protection_power"], "enabled": enabled}
|
||||
if threshold is not None:
|
||||
params["protection_power"] = threshold
|
||||
return await self.call("set_protection_power", params)
|
||||
|
||||
async def _set_threshold_auto_enable(self, threshold: int) -> dict:
|
||||
"""Set power protection and enable."""
|
||||
if threshold == 0:
|
||||
return await self.set_enabled(False)
|
||||
else:
|
||||
return await self.set_enabled(True, threshold=threshold)
|
||||
|
||||
@property
|
||||
def _threshold_or_zero(self) -> int:
|
||||
"""Get power protection threshold. 0 if not enabled."""
|
||||
return self.protection_threshold if self.enabled else 0
|
||||
|
||||
@property
|
||||
def _max_power(self) -> int:
|
||||
"""Return max power."""
|
||||
return self.data["get_max_power"]["max_power"]
|
||||
|
||||
@property
|
||||
def protection_threshold(
|
||||
self,
|
||||
) -> Annotated[int, FeatureAttribute("power_protection_threshold")]:
|
||||
"""Return protection threshold in watts."""
|
||||
# If never configured, there is no value set.
|
||||
return self.data["get_protection_power"].get("protection_power", 0)
|
||||
|
||||
async def set_protection_threshold(self, threshold: int) -> dict:
|
||||
"""Set protection threshold."""
|
||||
if threshold < 0 or threshold > self._max_power:
|
||||
raise ValueError(
|
||||
"Threshold out of range: %s (%s)", threshold, self.protection_threshold
|
||||
)
|
||||
|
||||
params = {
|
||||
**self.data["get_protection_power"],
|
||||
"protection_power": threshold,
|
||||
}
|
||||
return await self.call("set_protection_power", params)
|
||||
|
||||
async def _check_supported(self) -> bool:
|
||||
"""Return True if module is supported.
|
||||
|
||||
This is needed, as strips like P304M report the status only for children.
|
||||
"""
|
||||
return "power_protection_status" in self._device.sys_info
|
67
kasa/smart/modules/speaker.py
Normal file
67
kasa/smart/modules/speaker.py
Normal file
@ -0,0 +1,67 @@
|
||||
"""Implementation of vacuum speaker."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from typing import Annotated
|
||||
|
||||
from ...feature import Feature
|
||||
from ...module import FeatureAttribute
|
||||
from ..smartmodule import SmartModule
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class Speaker(SmartModule):
|
||||
"""Implementation of vacuum speaker."""
|
||||
|
||||
REQUIRED_COMPONENT = "speaker"
|
||||
|
||||
def _initialize_features(self) -> None:
|
||||
"""Initialize features."""
|
||||
self._add_feature(
|
||||
Feature(
|
||||
self._device,
|
||||
id="locate",
|
||||
name="Locate device",
|
||||
container=self,
|
||||
attribute_setter="locate",
|
||||
category=Feature.Category.Primary,
|
||||
type=Feature.Action,
|
||||
)
|
||||
)
|
||||
self._add_feature(
|
||||
Feature(
|
||||
self._device,
|
||||
id="volume",
|
||||
name="Volume",
|
||||
container=self,
|
||||
attribute_getter="volume",
|
||||
attribute_setter="set_volume",
|
||||
range_getter=lambda: (0, 100),
|
||||
category=Feature.Category.Config,
|
||||
type=Feature.Type.Number,
|
||||
)
|
||||
)
|
||||
|
||||
def query(self) -> dict:
|
||||
"""Query to execute during the update cycle."""
|
||||
return {
|
||||
"getVolume": None,
|
||||
}
|
||||
|
||||
@property
|
||||
def volume(self) -> Annotated[str, FeatureAttribute()]:
|
||||
"""Return volume."""
|
||||
return self.data["volume"]
|
||||
|
||||
async def set_volume(self, volume: int) -> Annotated[dict, FeatureAttribute()]:
|
||||
"""Set volume."""
|
||||
if volume < 0 or volume > 100:
|
||||
raise ValueError("Volume must be between 0 and 100")
|
||||
|
||||
return await self.call("setVolume", {"volume": volume})
|
||||
|
||||
async def locate(self) -> dict:
|
||||
"""Play sound to locate the device."""
|
||||
return await self.call("playSelectAudio", {"audio_type": "seek_me"})
|
@ -86,11 +86,22 @@ class SmartChildDevice(SmartDevice):
|
||||
module_queries: list[SmartModule] = []
|
||||
req: dict[str, Any] = {}
|
||||
for module in self.modules.values():
|
||||
if module.disabled is False and (mod_query := module.query()):
|
||||
if (
|
||||
module.disabled is False
|
||||
and (mod_query := module.query())
|
||||
and module._should_update(now)
|
||||
):
|
||||
module_queries.append(module)
|
||||
req.update(mod_query)
|
||||
if req:
|
||||
self._last_update = await self.protocol.query(req)
|
||||
first_update = self._last_update != {}
|
||||
try:
|
||||
resp = await self.protocol.query(req)
|
||||
except Exception as ex:
|
||||
resp = await self._handle_modular_update_error(
|
||||
ex, first_update, ", ".join(mod.name for mod in module_queries), req
|
||||
)
|
||||
self._last_update = resp
|
||||
|
||||
for module in self.modules.values():
|
||||
await self._handle_module_post_update(
|
||||
@ -98,6 +109,11 @@ class SmartChildDevice(SmartDevice):
|
||||
)
|
||||
self._last_update_time = now
|
||||
|
||||
# We can first initialize the features after the first update.
|
||||
# We make here an assumption that every device has at least a single feature.
|
||||
if not self._features:
|
||||
await self._initialize_features()
|
||||
|
||||
@classmethod
|
||||
async def create(
|
||||
cls,
|
||||
|
@ -5,7 +5,8 @@ from __future__ import annotations
|
||||
import base64
|
||||
import logging
|
||||
import time
|
||||
from collections.abc import Mapping, Sequence
|
||||
from collections import OrderedDict
|
||||
from collections.abc import Sequence
|
||||
from datetime import UTC, datetime, timedelta, tzinfo
|
||||
from typing import TYPE_CHECKING, Any, TypeAlias, cast
|
||||
|
||||
@ -66,12 +67,15 @@ class SmartDevice(Device):
|
||||
self._components_raw: ComponentsRaw | None = None
|
||||
self._components: dict[str, int] = {}
|
||||
self._state_information: dict[str, Any] = {}
|
||||
self._modules: dict[str | ModuleName[Module], SmartModule] = {}
|
||||
self._modules: OrderedDict[str | ModuleName[Module], SmartModule] = (
|
||||
OrderedDict()
|
||||
)
|
||||
self._parent: SmartDevice | None = None
|
||||
self._children: Mapping[str, SmartDevice] = {}
|
||||
self._children: dict[str, SmartDevice] = {}
|
||||
self._last_update_time: float | None = None
|
||||
self._on_since: datetime | None = None
|
||||
self._info: dict[str, Any] = {}
|
||||
self._logged_missing_child_ids: set[str] = set()
|
||||
|
||||
async def _initialize_children(self) -> None:
|
||||
"""Initialize children for power strips."""
|
||||
@ -82,23 +86,86 @@ class SmartDevice(Device):
|
||||
resp = await self.protocol.query(child_info_query)
|
||||
self.internal_state.update(resp)
|
||||
|
||||
children = self.internal_state["get_child_device_list"]["child_device_list"]
|
||||
children_components_raw = {
|
||||
child["device_id"]: child
|
||||
for child in self.internal_state["get_child_device_component_list"][
|
||||
"child_component_list"
|
||||
]
|
||||
}
|
||||
async def _try_create_child(
|
||||
self, info: dict, child_components: dict
|
||||
) -> SmartDevice | None:
|
||||
from .smartchilddevice import SmartChildDevice
|
||||
|
||||
self._children = {
|
||||
child_info["device_id"]: await SmartChildDevice.create(
|
||||
parent=self,
|
||||
child_info=child_info,
|
||||
child_components_raw=children_components_raw[child_info["device_id"]],
|
||||
)
|
||||
for child_info in children
|
||||
return await SmartChildDevice.create(
|
||||
parent=self,
|
||||
child_info=info,
|
||||
child_components_raw=child_components,
|
||||
)
|
||||
|
||||
async def _create_delete_children(
|
||||
self,
|
||||
child_device_resp: dict[str, list],
|
||||
child_device_components_resp: dict[str, list],
|
||||
) -> bool:
|
||||
"""Create and delete children. Return True if children changed.
|
||||
|
||||
Adds newly found children and deletes children that are no longer
|
||||
reported by the device. It will only log once per child_id that
|
||||
can't be created to avoid spamming the logs on every update.
|
||||
"""
|
||||
changed = False
|
||||
smart_children_components = {
|
||||
child["device_id"]: child
|
||||
for child in child_device_components_resp["child_component_list"]
|
||||
}
|
||||
children = self._children
|
||||
child_ids: set[str] = set()
|
||||
existing_child_ids = set(self._children.keys())
|
||||
|
||||
for info in child_device_resp["child_device_list"]:
|
||||
if (child_id := info.get("device_id")) and (
|
||||
child_components := smart_children_components.get(child_id)
|
||||
):
|
||||
child_ids.add(child_id)
|
||||
|
||||
if child_id in existing_child_ids:
|
||||
continue
|
||||
|
||||
child = await self._try_create_child(info, child_components)
|
||||
if child:
|
||||
_LOGGER.debug("Created child device %s for %s", child, self.host)
|
||||
changed = True
|
||||
children[child_id] = child
|
||||
continue
|
||||
|
||||
if child_id not in self._logged_missing_child_ids:
|
||||
self._logged_missing_child_ids.add(child_id)
|
||||
_LOGGER.debug("Child device type not supported: %s", info)
|
||||
continue
|
||||
|
||||
if child_id:
|
||||
if child_id not in self._logged_missing_child_ids:
|
||||
self._logged_missing_child_ids.add(child_id)
|
||||
_LOGGER.debug(
|
||||
"Could not find child components for device %s, "
|
||||
"child_id %s, components: %s: ",
|
||||
self.host,
|
||||
child_id,
|
||||
smart_children_components,
|
||||
)
|
||||
continue
|
||||
|
||||
# If we couldn't get a child device id we still only want to
|
||||
# log once to avoid spamming the logs on every update cycle
|
||||
# so store it under an empty string
|
||||
if "" not in self._logged_missing_child_ids:
|
||||
self._logged_missing_child_ids.add("")
|
||||
_LOGGER.debug(
|
||||
"Could not find child id for device %s, info: %s", self.host, info
|
||||
)
|
||||
|
||||
removed_ids = existing_child_ids - child_ids
|
||||
for removed_id in removed_ids:
|
||||
changed = True
|
||||
removed = children.pop(removed_id)
|
||||
_LOGGER.debug("Removed child device %s from %s", removed, self.host)
|
||||
|
||||
return changed
|
||||
|
||||
@property
|
||||
def children(self) -> Sequence[SmartDevice]:
|
||||
@ -164,26 +231,34 @@ class SmartDevice(Device):
|
||||
if "child_device" in self._components and not self.children:
|
||||
await self._initialize_children()
|
||||
|
||||
def _update_children_info(self) -> None:
|
||||
"""Update the internal child device info from the parent info."""
|
||||
async def _update_children_info(self) -> bool:
|
||||
"""Update the internal child device info from the parent info.
|
||||
|
||||
Return true if children added or deleted.
|
||||
"""
|
||||
changed = False
|
||||
if child_info := self._try_get_response(
|
||||
self._last_update, "get_child_device_list", {}
|
||||
):
|
||||
changed = await self._create_delete_children(
|
||||
child_info, self._last_update["get_child_device_component_list"]
|
||||
)
|
||||
|
||||
for info in child_info["child_device_list"]:
|
||||
child_id = info["device_id"]
|
||||
child_id = info.get("device_id")
|
||||
if child_id not in self._children:
|
||||
_LOGGER.debug(
|
||||
"Skipping child update for %s, probably unsupported device",
|
||||
child_id,
|
||||
)
|
||||
# _create_delete_children has already logged a message
|
||||
continue
|
||||
|
||||
self._children[child_id]._update_internal_state(info)
|
||||
|
||||
return changed
|
||||
|
||||
def _update_internal_info(self, info_resp: dict) -> None:
|
||||
"""Update the internal device info."""
|
||||
self._info = self._try_get_response(info_resp, "get_device_info")
|
||||
|
||||
async def update(self, update_children: bool = False) -> None:
|
||||
async def update(self, update_children: bool = True) -> None:
|
||||
"""Update the device."""
|
||||
if self.credentials is None and self.credentials_hash is None:
|
||||
raise AuthenticationError("Tapo plug requires authentication.")
|
||||
@ -201,13 +276,13 @@ class SmartDevice(Device):
|
||||
|
||||
resp = await self._modular_update(first_update, now)
|
||||
|
||||
self._update_children_info()
|
||||
children_changed = await self._update_children_info()
|
||||
# Call child update which will only update module calls, info is updated
|
||||
# from get_child_device_list. update_children only affects hub devices, other
|
||||
# devices will always update children to prevent errors on module access.
|
||||
# This needs to go after updating the internal state of the children so that
|
||||
# child modules have access to their sysinfo.
|
||||
if update_children or self.device_type != DeviceType.Hub:
|
||||
if children_changed or update_children or self.device_type != DeviceType.Hub:
|
||||
for child in self._children.values():
|
||||
if TYPE_CHECKING:
|
||||
assert isinstance(child, SmartChildDevice)
|
||||
@ -260,11 +335,7 @@ class SmartDevice(Device):
|
||||
if first_update and module.__class__ in self.FIRST_UPDATE_MODULES:
|
||||
module._last_update_time = update_time
|
||||
continue
|
||||
if (
|
||||
not module.update_interval
|
||||
or not module._last_update_time
|
||||
or (update_time - module._last_update_time) >= module.update_interval
|
||||
):
|
||||
if module._should_update(update_time):
|
||||
module_queries.append(module)
|
||||
req.update(query)
|
||||
|
||||
@ -377,6 +448,11 @@ class SmartDevice(Device):
|
||||
):
|
||||
self._modules[Thermostat.__name__] = Thermostat(self, "thermostat")
|
||||
|
||||
# We move time to the beginning so other modules can access the
|
||||
# time and timezone after update if required. e.g. cleanrecords
|
||||
if Time.__name__ in self._modules:
|
||||
self._modules.move_to_end(Time.__name__, last=False)
|
||||
|
||||
async def _initialize_features(self) -> None:
|
||||
"""Initialize device features."""
|
||||
self._add_feature(
|
||||
@ -469,12 +545,25 @@ class SmartDevice(Device):
|
||||
)
|
||||
)
|
||||
|
||||
if self.parent is not None and (
|
||||
cs := self.parent.modules.get(Module.ChildSetup)
|
||||
):
|
||||
self._add_feature(
|
||||
Feature(
|
||||
device=self,
|
||||
id="unpair",
|
||||
name="Unpair device",
|
||||
container=cs,
|
||||
attribute_setter=lambda: cs.unpair(self.device_id),
|
||||
category=Feature.Category.Debug,
|
||||
type=Feature.Type.Action,
|
||||
)
|
||||
)
|
||||
|
||||
for module in self.modules.values():
|
||||
module._initialize_features()
|
||||
for feat in module._module_features.values():
|
||||
self._add_feature(feat)
|
||||
for child in self._children.values():
|
||||
await child._initialize_features()
|
||||
|
||||
@property
|
||||
def _is_hub_child(self) -> bool:
|
||||
@ -602,12 +691,8 @@ class SmartDevice(Device):
|
||||
"""
|
||||
self._info = info
|
||||
|
||||
async def _query_helper(
|
||||
self, method: str, params: dict | None = None, child_ids: None = None
|
||||
) -> dict:
|
||||
res = await self.protocol.query({method: params})
|
||||
|
||||
return res
|
||||
async def _query_helper(self, method: str, params: dict | None = None) -> dict:
|
||||
return await self.protocol.query({method: params})
|
||||
|
||||
@property
|
||||
def ssid(self) -> str:
|
||||
@ -796,6 +881,8 @@ class SmartDevice(Device):
|
||||
return DeviceType.Thermostat
|
||||
if "ROBOVAC" in device_type:
|
||||
return DeviceType.Vacuum
|
||||
if "TAPOCHIME" in device_type:
|
||||
return DeviceType.Chime
|
||||
_LOGGER.warning("Unknown device type, falling back to plug")
|
||||
return DeviceType.Plug
|
||||
|
||||
@ -826,7 +913,10 @@ class SmartDevice(Device):
|
||||
components, device_family
|
||||
)
|
||||
fw_version_full = di["fw_ver"]
|
||||
firmware_version, firmware_build = fw_version_full.split(" ", maxsplit=1)
|
||||
if " " in fw_version_full:
|
||||
firmware_version, firmware_build = fw_version_full.split(" ", maxsplit=1)
|
||||
else:
|
||||
firmware_version, firmware_build = fw_version_full, None
|
||||
_protocol, devicetype = device_family.split(".")
|
||||
# Brand inferred from SMART.KASAPLUG/SMART.TAPOPLUG etc.
|
||||
brand = devicetype[:4].lower()
|
||||
|
@ -3,7 +3,8 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from collections.abc import Awaitable, Callable, Coroutine
|
||||
from collections.abc import Callable, Coroutine
|
||||
from functools import wraps
|
||||
from typing import TYPE_CHECKING, Any, Concatenate, ParamSpec, TypeVar
|
||||
|
||||
from ..exceptions import DeviceError, KasaException, SmartErrorCode
|
||||
@ -20,15 +21,16 @@ _R = TypeVar("_R")
|
||||
|
||||
|
||||
def allow_update_after(
|
||||
func: Callable[Concatenate[_T, _P], Awaitable[dict]],
|
||||
) -> Callable[Concatenate[_T, _P], Coroutine[Any, Any, dict]]:
|
||||
func: Callable[Concatenate[_T, _P], Coroutine[Any, Any, _R]],
|
||||
) -> Callable[Concatenate[_T, _P], Coroutine[Any, Any, _R]]:
|
||||
"""Define a wrapper to set _last_update_time to None.
|
||||
|
||||
This will ensure that a module is updated in the next update cycle after
|
||||
a value has been changed.
|
||||
"""
|
||||
|
||||
async def _async_wrap(self: _T, *args: _P.args, **kwargs: _P.kwargs) -> dict:
|
||||
@wraps(func)
|
||||
async def _async_wrap(self: _T, *args: _P.args, **kwargs: _P.kwargs) -> _R:
|
||||
try:
|
||||
return await func(self, *args, **kwargs)
|
||||
finally:
|
||||
@ -40,6 +42,7 @@ def allow_update_after(
|
||||
def raise_if_update_error(func: Callable[[_T], _R]) -> Callable[[_T], _R]:
|
||||
"""Define a wrapper to raise an error if the last module update was an error."""
|
||||
|
||||
@wraps(func)
|
||||
def _wrap(self: _T) -> _R:
|
||||
if err := self._last_update_error:
|
||||
raise err
|
||||
@ -62,6 +65,8 @@ class SmartModule(Module):
|
||||
REGISTERED_MODULES: dict[str, type[SmartModule]] = {}
|
||||
|
||||
MINIMUM_UPDATE_INTERVAL_SECS = 0
|
||||
MINIMUM_HUB_CHILD_UPDATE_INTERVAL_SECS = 60 * 60 * 24
|
||||
|
||||
UPDATE_INTERVAL_AFTER_ERROR_SECS = 30
|
||||
|
||||
DISABLE_AFTER_ERROR_COUNT = 10
|
||||
@ -107,16 +112,27 @@ class SmartModule(Module):
|
||||
@property
|
||||
def update_interval(self) -> int:
|
||||
"""Time to wait between updates."""
|
||||
if self._last_update_error is None:
|
||||
return self.MINIMUM_UPDATE_INTERVAL_SECS
|
||||
if self._last_update_error:
|
||||
return self.UPDATE_INTERVAL_AFTER_ERROR_SECS * self._error_count
|
||||
|
||||
return self.UPDATE_INTERVAL_AFTER_ERROR_SECS * self._error_count
|
||||
if self._device._is_hub_child:
|
||||
return self.MINIMUM_HUB_CHILD_UPDATE_INTERVAL_SECS
|
||||
|
||||
return self.MINIMUM_UPDATE_INTERVAL_SECS
|
||||
|
||||
@property
|
||||
def disabled(self) -> bool:
|
||||
"""Return true if the module is disabled due to errors."""
|
||||
return self._error_count >= self.DISABLE_AFTER_ERROR_COUNT
|
||||
|
||||
def _should_update(self, update_time: float) -> bool:
|
||||
"""Return true if module should update based on delay parameters."""
|
||||
return (
|
||||
not self.update_interval
|
||||
or not self._last_update_time
|
||||
or (update_time - self._last_update_time) >= self.update_interval
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def _module_name(cls) -> str:
|
||||
return getattr(cls, "NAME", cls.__name__)
|
||||
|
@ -1,5 +1,6 @@
|
||||
"""Package for supporting tapo-branded cameras."""
|
||||
|
||||
from .smartcamchild import SmartCamChild
|
||||
from .smartcamdevice import SmartCamDevice
|
||||
|
||||
__all__ = ["SmartCamDevice"]
|
||||
__all__ = ["SmartCamDevice", "SmartCamChild"]
|
||||
|
@ -2,8 +2,10 @@
|
||||
|
||||
from .alarm import Alarm
|
||||
from .babycrydetection import BabyCryDetection
|
||||
from .battery import Battery
|
||||
from .camera import Camera
|
||||
from .childdevice import ChildDevice
|
||||
from .childsetup import ChildSetup
|
||||
from .device import DeviceModule
|
||||
from .homekit import HomeKit
|
||||
from .led import Led
|
||||
@ -12,18 +14,22 @@ from .matter import Matter
|
||||
from .motiondetection import MotionDetection
|
||||
from .pantilt import PanTilt
|
||||
from .persondetection import PersonDetection
|
||||
from .petdetection import PetDetection
|
||||
from .tamperdetection import TamperDetection
|
||||
from .time import Time
|
||||
|
||||
__all__ = [
|
||||
"Alarm",
|
||||
"BabyCryDetection",
|
||||
"Battery",
|
||||
"Camera",
|
||||
"ChildDevice",
|
||||
"ChildSetup",
|
||||
"DeviceModule",
|
||||
"Led",
|
||||
"PanTilt",
|
||||
"PersonDetection",
|
||||
"PetDetection",
|
||||
"Time",
|
||||
"HomeKit",
|
||||
"Matter",
|
||||
|
@ -2,7 +2,12 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Annotated
|
||||
|
||||
from ...feature import Feature
|
||||
from ...interfaces import Alarm as AlarmInterface
|
||||
from ...module import FeatureAttribute
|
||||
from ...smart.smartmodule import allow_update_after
|
||||
from ..smartcammodule import SmartCamModule
|
||||
|
||||
DURATION_MIN = 0
|
||||
@ -12,12 +17,9 @@ VOLUME_MIN = 0
|
||||
VOLUME_MAX = 10
|
||||
|
||||
|
||||
class Alarm(SmartCamModule):
|
||||
class Alarm(SmartCamModule, AlarmInterface):
|
||||
"""Implementation of alarm module."""
|
||||
|
||||
# Needs a different name to avoid clashing with SmartAlarm
|
||||
NAME = "SmartCamAlarm"
|
||||
|
||||
REQUIRED_COMPONENT = "siren"
|
||||
QUERY_GETTER_NAME = "getSirenStatus"
|
||||
QUERY_MODULE_NAME = "siren"
|
||||
@ -106,20 +108,18 @@ class Alarm(SmartCamModule):
|
||||
)
|
||||
|
||||
@property
|
||||
def alarm_sound(self) -> str:
|
||||
def alarm_sound(self) -> Annotated[str, FeatureAttribute()]:
|
||||
"""Return current alarm sound."""
|
||||
return self.data["getSirenConfig"]["siren_type"]
|
||||
|
||||
async def set_alarm_sound(self, sound: str) -> dict:
|
||||
@allow_update_after
|
||||
async def set_alarm_sound(self, sound: str) -> Annotated[dict, FeatureAttribute()]:
|
||||
"""Set alarm sound.
|
||||
|
||||
See *alarm_sounds* for list of available sounds.
|
||||
"""
|
||||
if sound not in self.alarm_sounds:
|
||||
raise ValueError(
|
||||
f"sound must be one of {', '.join(self.alarm_sounds)}: {sound}"
|
||||
)
|
||||
return await self.call("setSirenConfig", {"siren": {"siren_type": sound}})
|
||||
config = self._validate_and_get_config(sound=sound)
|
||||
return await self.call("setSirenConfig", {"siren": config})
|
||||
|
||||
@property
|
||||
def alarm_sounds(self) -> list[str]:
|
||||
@ -127,40 +127,90 @@ class Alarm(SmartCamModule):
|
||||
return self.data["getSirenTypeList"]["siren_type_list"]
|
||||
|
||||
@property
|
||||
def alarm_volume(self) -> int:
|
||||
def alarm_volume(self) -> Annotated[int, FeatureAttribute()]:
|
||||
"""Return alarm volume.
|
||||
|
||||
Unlike duration the device expects/returns a string for volume.
|
||||
"""
|
||||
return int(self.data["getSirenConfig"]["volume"])
|
||||
|
||||
async def set_alarm_volume(self, volume: int) -> dict:
|
||||
@allow_update_after
|
||||
async def set_alarm_volume(
|
||||
self, volume: int
|
||||
) -> Annotated[dict, FeatureAttribute()]:
|
||||
"""Set alarm volume."""
|
||||
if volume < VOLUME_MIN or volume > VOLUME_MAX:
|
||||
raise ValueError(f"volume must be between {VOLUME_MIN} and {VOLUME_MAX}")
|
||||
return await self.call("setSirenConfig", {"siren": {"volume": str(volume)}})
|
||||
config = self._validate_and_get_config(volume=volume)
|
||||
return await self.call("setSirenConfig", {"siren": config})
|
||||
|
||||
@property
|
||||
def alarm_duration(self) -> int:
|
||||
def alarm_duration(self) -> Annotated[int, FeatureAttribute()]:
|
||||
"""Return alarm duration."""
|
||||
return self.data["getSirenConfig"]["duration"]
|
||||
|
||||
async def set_alarm_duration(self, duration: int) -> dict:
|
||||
@allow_update_after
|
||||
async def set_alarm_duration(
|
||||
self, duration: int
|
||||
) -> Annotated[dict, FeatureAttribute()]:
|
||||
"""Set alarm volume."""
|
||||
if duration < DURATION_MIN or duration > DURATION_MAX:
|
||||
msg = f"duration must be between {DURATION_MIN} and {DURATION_MAX}"
|
||||
raise ValueError(msg)
|
||||
return await self.call("setSirenConfig", {"siren": {"duration": duration}})
|
||||
config = self._validate_and_get_config(duration=duration)
|
||||
return await self.call("setSirenConfig", {"siren": config})
|
||||
|
||||
@property
|
||||
def active(self) -> bool:
|
||||
"""Return true if alarm is active."""
|
||||
return self.data["getSirenStatus"]["status"] != "off"
|
||||
|
||||
async def play(self) -> dict:
|
||||
"""Play alarm."""
|
||||
async def play(
|
||||
self,
|
||||
*,
|
||||
duration: int | None = None,
|
||||
volume: int | None = None,
|
||||
sound: str | None = None,
|
||||
) -> dict:
|
||||
"""Play alarm.
|
||||
|
||||
The optional *duration*, *volume*, and *sound* to override the device settings.
|
||||
*duration* is in seconds.
|
||||
See *alarm_sounds* for the list of sounds available for the device.
|
||||
"""
|
||||
if config := self._validate_and_get_config(
|
||||
duration=duration, volume=volume, sound=sound
|
||||
):
|
||||
await self.call("setSirenConfig", {"siren": config})
|
||||
|
||||
return await self.call("setSirenStatus", {"siren": {"status": "on"}})
|
||||
|
||||
async def stop(self) -> dict:
|
||||
"""Stop alarm."""
|
||||
return await self.call("setSirenStatus", {"siren": {"status": "off"}})
|
||||
|
||||
def _validate_and_get_config(
|
||||
self,
|
||||
*,
|
||||
duration: int | None = None,
|
||||
volume: int | None = None,
|
||||
sound: str | None = None,
|
||||
) -> dict:
|
||||
if sound and sound not in self.alarm_sounds:
|
||||
raise ValueError(
|
||||
f"sound must be one of {', '.join(self.alarm_sounds)}: {sound}"
|
||||
)
|
||||
|
||||
if duration is not None and (
|
||||
duration < DURATION_MIN or duration > DURATION_MAX
|
||||
):
|
||||
msg = f"duration must be between {DURATION_MIN} and {DURATION_MAX}"
|
||||
raise ValueError(msg)
|
||||
|
||||
if volume is not None and (volume < VOLUME_MIN or volume > VOLUME_MAX):
|
||||
raise ValueError(f"volume must be between {VOLUME_MIN} and {VOLUME_MAX}")
|
||||
|
||||
config: dict[str, str | int] = {}
|
||||
if sound:
|
||||
config["siren_type"] = sound
|
||||
if duration is not None:
|
||||
config["duration"] = duration
|
||||
if volume is not None:
|
||||
config["volume"] = str(volume)
|
||||
|
||||
return config
|
||||
|
@ -5,6 +5,7 @@ from __future__ import annotations
|
||||
import logging
|
||||
|
||||
from ...feature import Feature
|
||||
from ...smart.smartmodule import allow_update_after
|
||||
from ..smartcammodule import SmartCamModule
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
@ -39,6 +40,7 @@ class BabyCryDetection(SmartCamModule):
|
||||
"""Return the baby cry detection enabled state."""
|
||||
return self.data["bcd"]["enabled"] == "on"
|
||||
|
||||
@allow_update_after
|
||||
async def set_enabled(self, enable: bool) -> dict:
|
||||
"""Set the baby cry detection enabled state."""
|
||||
params = {"enabled": "on" if enable else "off"}
|
||||
|
113
kasa/smartcam/modules/battery.py
Normal file
113
kasa/smartcam/modules/battery.py
Normal file
@ -0,0 +1,113 @@
|
||||
"""Implementation of baby cry detection module."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
|
||||
from ...feature import Feature
|
||||
from ..smartcammodule import SmartCamModule
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class Battery(SmartCamModule):
|
||||
"""Implementation of a battery module."""
|
||||
|
||||
REQUIRED_COMPONENT = "battery"
|
||||
|
||||
def _initialize_features(self) -> None:
|
||||
"""Initialize features."""
|
||||
self._add_feature(
|
||||
Feature(
|
||||
self._device,
|
||||
"battery_low",
|
||||
"Battery low",
|
||||
container=self,
|
||||
attribute_getter="battery_low",
|
||||
icon="mdi:alert",
|
||||
type=Feature.Type.BinarySensor,
|
||||
category=Feature.Category.Debug,
|
||||
)
|
||||
)
|
||||
|
||||
self._add_feature(
|
||||
Feature(
|
||||
self._device,
|
||||
"battery_level",
|
||||
"Battery level",
|
||||
container=self,
|
||||
attribute_getter="battery_percent",
|
||||
icon="mdi:battery",
|
||||
unit_getter=lambda: "%",
|
||||
category=Feature.Category.Info,
|
||||
type=Feature.Type.Sensor,
|
||||
)
|
||||
)
|
||||
|
||||
self._add_feature(
|
||||
Feature(
|
||||
self._device,
|
||||
"battery_temperature",
|
||||
"Battery temperature",
|
||||
container=self,
|
||||
attribute_getter="battery_temperature",
|
||||
icon="mdi:battery",
|
||||
unit_getter=lambda: "celsius",
|
||||
category=Feature.Category.Debug,
|
||||
type=Feature.Type.Sensor,
|
||||
)
|
||||
)
|
||||
self._add_feature(
|
||||
Feature(
|
||||
self._device,
|
||||
"battery_voltage",
|
||||
"Battery voltage",
|
||||
container=self,
|
||||
attribute_getter="battery_voltage",
|
||||
icon="mdi:battery",
|
||||
unit_getter=lambda: "V",
|
||||
category=Feature.Category.Debug,
|
||||
type=Feature.Type.Sensor,
|
||||
)
|
||||
)
|
||||
self._add_feature(
|
||||
Feature(
|
||||
self._device,
|
||||
"battery_charging",
|
||||
"Battery charging",
|
||||
container=self,
|
||||
attribute_getter="battery_charging",
|
||||
icon="mdi:alert",
|
||||
type=Feature.Type.BinarySensor,
|
||||
category=Feature.Category.Debug,
|
||||
)
|
||||
)
|
||||
|
||||
def query(self) -> dict:
|
||||
"""Query to execute during the update cycle."""
|
||||
return {}
|
||||
|
||||
@property
|
||||
def battery_percent(self) -> int:
|
||||
"""Return battery level."""
|
||||
return self._device.sys_info["battery_percent"]
|
||||
|
||||
@property
|
||||
def battery_low(self) -> bool:
|
||||
"""Return True if battery is low."""
|
||||
return self._device.sys_info["low_battery"]
|
||||
|
||||
@property
|
||||
def battery_temperature(self) -> bool:
|
||||
"""Return battery voltage in C."""
|
||||
return self._device.sys_info["battery_temperature"]
|
||||
|
||||
@property
|
||||
def battery_voltage(self) -> bool:
|
||||
"""Return battery voltage in V."""
|
||||
return self._device.sys_info["battery_voltage"] / 1_000
|
||||
|
||||
@property
|
||||
def battery_charging(self) -> bool:
|
||||
"""Return True if battery is charging."""
|
||||
return self._device.sys_info["battery_voltage"] != "NO"
|
@ -9,7 +9,6 @@ from typing import Annotated
|
||||
from urllib.parse import quote_plus
|
||||
|
||||
from ...credentials import Credentials
|
||||
from ...device_type import DeviceType
|
||||
from ...feature import Feature
|
||||
from ...json import loads as json_loads
|
||||
from ...module import FeatureAttribute, Module
|
||||
@ -31,6 +30,8 @@ class StreamResolution(StrEnum):
|
||||
class Camera(SmartCamModule):
|
||||
"""Implementation of device module."""
|
||||
|
||||
REQUIRED_COMPONENT = "video"
|
||||
|
||||
def _initialize_features(self) -> None:
|
||||
"""Initialize features after the initial update."""
|
||||
if Module.LensMask in self._device.modules:
|
||||
@ -99,6 +100,9 @@ class Camera(SmartCamModule):
|
||||
:return: rtsp url with escaped credentials or None if no credentials or
|
||||
camera is off.
|
||||
"""
|
||||
if self._device._is_hub_child:
|
||||
return None
|
||||
|
||||
streams = {
|
||||
StreamResolution.HD: "stream1",
|
||||
StreamResolution.SD: "stream2",
|
||||
@ -119,8 +123,7 @@ class Camera(SmartCamModule):
|
||||
|
||||
def onvif_url(self) -> str | None:
|
||||
"""Return the onvif url."""
|
||||
return f"http://{self._device.host}:{ONVIF_PORT}/onvif/device_service"
|
||||
if self._device._is_hub_child:
|
||||
return None
|
||||
|
||||
async def _check_supported(self) -> bool:
|
||||
"""Additional check to see if the module is supported by the device."""
|
||||
return self._device.device_type is DeviceType.Camera
|
||||
return f"http://{self._device.host}:{ONVIF_PORT}/onvif/device_service"
|
||||
|
@ -19,7 +19,10 @@ class ChildDevice(SmartCamModule):
|
||||
|
||||
Default implementation uses the raw query getter w/o parameters.
|
||||
"""
|
||||
return {self.QUERY_GETTER_NAME: {"childControl": {"start_index": 0}}}
|
||||
q = {self.QUERY_GETTER_NAME: {"childControl": {"start_index": 0}}}
|
||||
if self._device.device_type is DeviceType.Hub:
|
||||
q["getChildDeviceComponentList"] = {"childControl": {"start_index": 0}}
|
||||
return q
|
||||
|
||||
async def _check_supported(self) -> bool:
|
||||
"""Additional check to see if the module is supported by the device."""
|
||||
|
112
kasa/smartcam/modules/childsetup.py
Normal file
112
kasa/smartcam/modules/childsetup.py
Normal file
@ -0,0 +1,112 @@
|
||||
"""Implementation for child device setup.
|
||||
|
||||
This module allows pairing and disconnecting child devices.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
|
||||
from ...feature import Feature
|
||||
from ...interfaces.childsetup import ChildSetup as ChildSetupInterface
|
||||
from ..smartcammodule import SmartCamModule
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class ChildSetup(SmartCamModule, ChildSetupInterface):
|
||||
"""Implementation for child device setup."""
|
||||
|
||||
REQUIRED_COMPONENT = "childQuickSetup"
|
||||
QUERY_GETTER_NAME = "getSupportChildDeviceCategory"
|
||||
QUERY_MODULE_NAME = "childControl"
|
||||
_categories: list[str] = []
|
||||
|
||||
# Supported child device categories will hardly ever change
|
||||
MINIMUM_UPDATE_INTERVAL_SECS = 60 * 60 * 24
|
||||
|
||||
def _initialize_features(self) -> None:
|
||||
"""Initialize features."""
|
||||
self._add_feature(
|
||||
Feature(
|
||||
self._device,
|
||||
id="pair",
|
||||
name="Pair",
|
||||
container=self,
|
||||
attribute_setter="pair",
|
||||
category=Feature.Category.Config,
|
||||
type=Feature.Type.Action,
|
||||
)
|
||||
)
|
||||
|
||||
async def _post_update_hook(self) -> None:
|
||||
self._categories = [
|
||||
cat["category"].replace("ipcamera", "camera")
|
||||
for cat in self.data["device_category_list"]
|
||||
]
|
||||
|
||||
@property
|
||||
def supported_categories(self) -> list[str]:
|
||||
"""Supported child device categories."""
|
||||
return self._categories
|
||||
|
||||
async def pair(self, *, timeout: int = 10) -> list[dict]:
|
||||
"""Scan for new devices and pair them."""
|
||||
await self.call(
|
||||
"startScanChildDevice", {"childControl": {"category": self._categories}}
|
||||
)
|
||||
|
||||
_LOGGER.info("Waiting %s seconds for discovering new devices", timeout)
|
||||
|
||||
await asyncio.sleep(timeout)
|
||||
res = await self.call(
|
||||
"getScanChildDeviceList", {"childControl": {"category": self._categories}}
|
||||
)
|
||||
|
||||
detected_list = res["getScanChildDeviceList"]["child_device_list"]
|
||||
if not detected_list:
|
||||
_LOGGER.warning(
|
||||
"No devices found, make sure to activate pairing "
|
||||
"mode on the devices to be added."
|
||||
)
|
||||
return []
|
||||
|
||||
_LOGGER.info(
|
||||
"Discovery done, found %s devices: %s",
|
||||
len(detected_list),
|
||||
detected_list,
|
||||
)
|
||||
return await self._add_devices(detected_list)
|
||||
|
||||
async def _add_devices(self, detected_list: list[dict]) -> list[dict]:
|
||||
"""Add devices based on getScanChildDeviceList response."""
|
||||
await self.call(
|
||||
"addScanChildDeviceList",
|
||||
{"childControl": {"child_device_list": detected_list}},
|
||||
)
|
||||
|
||||
await self._device.update()
|
||||
|
||||
successes = []
|
||||
for detected in detected_list:
|
||||
device_id = detected["device_id"]
|
||||
|
||||
result = "not added"
|
||||
if device_id in self._device._children:
|
||||
result = "added"
|
||||
successes.append(detected)
|
||||
|
||||
msg = f"{detected['device_model']} - {device_id} - {result}"
|
||||
_LOGGER.info("Adding child to %s: %s", self._device.host, msg)
|
||||
|
||||
return successes
|
||||
|
||||
async def unpair(self, device_id: str) -> dict:
|
||||
"""Remove device from the hub."""
|
||||
_LOGGER.info("Going to unpair %s from %s", device_id, self)
|
||||
|
||||
payload = {"childControl": {"child_device_list": [{"device_id": device_id}]}}
|
||||
res = await self.call("removeChildDeviceList", payload)
|
||||
await self._device.update()
|
||||
return res
|
@ -16,6 +16,11 @@ class DeviceModule(SmartCamModule):
|
||||
|
||||
def query(self) -> dict:
|
||||
"""Query to execute during the update cycle."""
|
||||
if self._device._is_hub_child:
|
||||
# Child devices get their device info updated by the parent device.
|
||||
# and generally don't support connection type as they're not
|
||||
# connected to the network
|
||||
return {}
|
||||
q = super().query()
|
||||
q["getConnectionType"] = {"network": {"get_connection_type": []}}
|
||||
|
||||
@ -70,14 +75,14 @@ class DeviceModule(SmartCamModule):
|
||||
@property
|
||||
def device_id(self) -> str:
|
||||
"""Return the device id."""
|
||||
return self.data[self.QUERY_GETTER_NAME]["basic_info"]["dev_id"]
|
||||
return self._device._info["device_id"]
|
||||
|
||||
@property
|
||||
def rssi(self) -> int | None:
|
||||
"""Return the device id."""
|
||||
return self.data["getConnectionType"].get("rssiValue")
|
||||
return self.data.get("getConnectionType", {}).get("rssiValue")
|
||||
|
||||
@property
|
||||
def signal_level(self) -> int | None:
|
||||
"""Return the device id."""
|
||||
return self.data["getConnectionType"].get("rssi")
|
||||
return self.data.get("getConnectionType", {}).get("rssi")
|
||||
|
@ -3,6 +3,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from ...interfaces.led import Led as LedInterface
|
||||
from ...smart.smartmodule import allow_update_after
|
||||
from ..smartcammodule import SmartCamModule
|
||||
|
||||
|
||||
@ -19,6 +20,7 @@ class Led(SmartCamModule, LedInterface):
|
||||
"""Return current led status."""
|
||||
return self.data["config"]["enabled"] == "on"
|
||||
|
||||
@allow_update_after
|
||||
async def set_led(self, enable: bool) -> dict:
|
||||
"""Set led.
|
||||
|
||||
|
@ -4,6 +4,7 @@ from __future__ import annotations
|
||||
|
||||
import logging
|
||||
|
||||
from ...smart.smartmodule import allow_update_after
|
||||
from ..smartcammodule import SmartCamModule
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
@ -23,6 +24,7 @@ class LensMask(SmartCamModule):
|
||||
"""Return the lens mask state."""
|
||||
return self.data["lens_mask_info"]["enabled"] == "on"
|
||||
|
||||
@allow_update_after
|
||||
async def set_enabled(self, enable: bool) -> dict:
|
||||
"""Set the lens mask state."""
|
||||
params = {"enabled": "on" if enable else "off"}
|
||||
|
@ -5,6 +5,7 @@ from __future__ import annotations
|
||||
import logging
|
||||
|
||||
from ...feature import Feature
|
||||
from ...smart.smartmodule import allow_update_after
|
||||
from ..smartcammodule import SmartCamModule
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
@ -39,6 +40,7 @@ class MotionDetection(SmartCamModule):
|
||||
"""Return the motion detection enabled state."""
|
||||
return self.data["motion_det"]["enabled"] == "on"
|
||||
|
||||
@allow_update_after
|
||||
async def set_enabled(self, enable: bool) -> dict:
|
||||
"""Set the motion detection enabled state."""
|
||||
params = {"enabled": "on" if enable else "off"}
|
||||
|
@ -5,6 +5,7 @@ from __future__ import annotations
|
||||
import logging
|
||||
|
||||
from ...feature import Feature
|
||||
from ...smart.smartmodule import allow_update_after
|
||||
from ..smartcammodule import SmartCamModule
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
@ -39,6 +40,7 @@ class PersonDetection(SmartCamModule):
|
||||
"""Return the person detection enabled state."""
|
||||
return self.data["detection"]["enabled"] == "on"
|
||||
|
||||
@allow_update_after
|
||||
async def set_enabled(self, enable: bool) -> dict:
|
||||
"""Set the person detection enabled state."""
|
||||
params = {"enabled": "on" if enable else "off"}
|
||||
|
49
kasa/smartcam/modules/petdetection.py
Normal file
49
kasa/smartcam/modules/petdetection.py
Normal file
@ -0,0 +1,49 @@
|
||||
"""Implementation of pet detection module."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
|
||||
from ...feature import Feature
|
||||
from ...smart.smartmodule import allow_update_after
|
||||
from ..smartcammodule import SmartCamModule
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class PetDetection(SmartCamModule):
|
||||
"""Implementation of pet detection module."""
|
||||
|
||||
REQUIRED_COMPONENT = "petDetection"
|
||||
|
||||
QUERY_GETTER_NAME = "getPetDetectionConfig"
|
||||
QUERY_MODULE_NAME = "pet_detection"
|
||||
QUERY_SECTION_NAMES = "detection"
|
||||
|
||||
def _initialize_features(self) -> None:
|
||||
"""Initialize features after the initial update."""
|
||||
self._add_feature(
|
||||
Feature(
|
||||
self._device,
|
||||
id="pet_detection",
|
||||
name="Pet detection",
|
||||
container=self,
|
||||
attribute_getter="enabled",
|
||||
attribute_setter="set_enabled",
|
||||
type=Feature.Type.Switch,
|
||||
category=Feature.Category.Config,
|
||||
)
|
||||
)
|
||||
|
||||
@property
|
||||
def enabled(self) -> bool:
|
||||
"""Return the pet detection enabled state."""
|
||||
return self.data["detection"]["enabled"] == "on"
|
||||
|
||||
@allow_update_after
|
||||
async def set_enabled(self, enable: bool) -> dict:
|
||||
"""Set the pet detection enabled state."""
|
||||
params = {"enabled": "on" if enable else "off"}
|
||||
return await self._device._query_setter_helper(
|
||||
"setPetDetectionConfig", self.QUERY_MODULE_NAME, "detection", params
|
||||
)
|
@ -5,6 +5,7 @@ from __future__ import annotations
|
||||
import logging
|
||||
|
||||
from ...feature import Feature
|
||||
from ...smart.smartmodule import allow_update_after
|
||||
from ..smartcammodule import SmartCamModule
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
@ -39,6 +40,7 @@ class TamperDetection(SmartCamModule):
|
||||
"""Return the tamper detection enabled state."""
|
||||
return self.data["tamper_det"]["enabled"] == "on"
|
||||
|
||||
@allow_update_after
|
||||
async def set_enabled(self, enable: bool) -> dict:
|
||||
"""Set the tamper detection enabled state."""
|
||||
params = {"enabled": "on" if enable else "off"}
|
||||
|
@ -9,6 +9,7 @@ from zoneinfo import ZoneInfo, ZoneInfoNotFoundError
|
||||
from ...cachedzoneinfo import CachedZoneInfo
|
||||
from ...feature import Feature
|
||||
from ...interfaces import Time as TimeInterface
|
||||
from ...smart.smartmodule import allow_update_after
|
||||
from ..smartcammodule import SmartCamModule
|
||||
|
||||
|
||||
@ -73,6 +74,7 @@ class Time(SmartCamModule, TimeInterface):
|
||||
"""Return device's current datetime."""
|
||||
return self._time
|
||||
|
||||
@allow_update_after
|
||||
async def set_time(self, dt: datetime) -> dict:
|
||||
"""Set device time."""
|
||||
if not dt.tzinfo:
|
||||
|
121
kasa/smartcam/smartcamchild.py
Normal file
121
kasa/smartcam/smartcamchild.py
Normal file
@ -0,0 +1,121 @@
|
||||
"""Child device implementation."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from typing import Any
|
||||
|
||||
from ..device import DeviceInfo
|
||||
from ..device_type import DeviceType
|
||||
from ..deviceconfig import DeviceConfig
|
||||
from ..protocols.smartcamprotocol import _ChildCameraProtocolWrapper
|
||||
from ..protocols.smartprotocol import SmartProtocol
|
||||
from ..smart.smartchilddevice import SmartChildDevice
|
||||
from ..smart.smartdevice import ComponentsRaw, SmartDevice
|
||||
from .smartcamdevice import SmartCamDevice
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
# SmartCamChild devices have a different info format from getChildDeviceInfo
|
||||
# than when querying getDeviceInfo directly on the child.
|
||||
# As _get_device_info is also called by dump_devtools and generate_supported
|
||||
# this key will be expected by _get_device_info
|
||||
CHILD_INFO_FROM_PARENT = "child_info_from_parent"
|
||||
|
||||
|
||||
class SmartCamChild(SmartChildDevice, SmartCamDevice):
|
||||
"""Presentation of a child device.
|
||||
|
||||
This wraps the protocol communications and sets internal data for the child.
|
||||
"""
|
||||
|
||||
CHILD_DEVICE_TYPE_MAP = {
|
||||
"camera": DeviceType.Camera,
|
||||
}
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
parent: SmartDevice,
|
||||
info: dict,
|
||||
component_info_raw: ComponentsRaw,
|
||||
*,
|
||||
config: DeviceConfig | None = None,
|
||||
protocol: SmartProtocol | None = None,
|
||||
) -> None:
|
||||
_protocol = protocol or _ChildCameraProtocolWrapper(
|
||||
info["device_id"], parent.protocol
|
||||
)
|
||||
super().__init__(parent, info, component_info_raw, protocol=_protocol)
|
||||
self._child_info_from_parent: dict = {}
|
||||
|
||||
@property
|
||||
def device_info(self) -> DeviceInfo:
|
||||
"""Return device info.
|
||||
|
||||
Child device does not have it info and components in _last_update so
|
||||
this overrides the base implementation to call _get_device_info with
|
||||
info and components combined as they would be in _last_update.
|
||||
"""
|
||||
return self._get_device_info(
|
||||
{
|
||||
CHILD_INFO_FROM_PARENT: self._child_info_from_parent,
|
||||
},
|
||||
None,
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def _map_child_info_from_parent(device_info: dict) -> dict:
|
||||
mappings = {
|
||||
"device_model": "model",
|
||||
"sw_ver": "fw_ver",
|
||||
"hw_id": "hwId",
|
||||
}
|
||||
return {mappings.get(k, k): v for k, v in device_info.items()}
|
||||
|
||||
def _update_internal_state(self, info: dict[str, Any]) -> None:
|
||||
"""Update the internal info state.
|
||||
|
||||
This is used by the parent to push updates to its children.
|
||||
"""
|
||||
# smartcam children have info with different keys to their own
|
||||
# getDeviceInfo queries
|
||||
self._child_info_from_parent = info
|
||||
|
||||
# self._info will have the values normalized across smart and smartcam
|
||||
# devices
|
||||
self._info = self._map_child_info_from_parent(info)
|
||||
|
||||
@property
|
||||
def device_type(self) -> DeviceType:
|
||||
"""Return the device type."""
|
||||
if self._device_type == DeviceType.Unknown and self._info:
|
||||
self._device_type = self._get_device_type_from_sysinfo(self._info)
|
||||
return self._device_type
|
||||
|
||||
@staticmethod
|
||||
def _get_device_info(
|
||||
info: dict[str, Any], discovery_info: dict[str, Any] | None
|
||||
) -> DeviceInfo:
|
||||
"""Get model information for a device."""
|
||||
if not (cifp := info.get(CHILD_INFO_FROM_PARENT)):
|
||||
return SmartCamDevice._get_device_info(info, discovery_info)
|
||||
|
||||
model = cifp["device_model"]
|
||||
device_type = SmartCamDevice._get_device_type_from_sysinfo(cifp)
|
||||
fw_version_full = cifp["sw_ver"]
|
||||
if " " in fw_version_full:
|
||||
firmware_version, firmware_build = fw_version_full.split(" ", maxsplit=1)
|
||||
else:
|
||||
firmware_version, firmware_build = fw_version_full, None
|
||||
return DeviceInfo(
|
||||
short_name=model,
|
||||
long_name=model,
|
||||
brand="tapo",
|
||||
device_family=cifp["device_type"],
|
||||
device_type=device_type,
|
||||
hardware_version=cifp["hw_ver"],
|
||||
firmware_version=firmware_version,
|
||||
firmware_build=firmware_build,
|
||||
requires_auth=True,
|
||||
region=cifp.get("region"),
|
||||
)
|
@ -26,12 +26,15 @@ class SmartCamDevice(SmartDevice):
|
||||
@staticmethod
|
||||
def _get_device_type_from_sysinfo(sysinfo: dict[str, Any]) -> DeviceType:
|
||||
"""Find type to be displayed as a supported device category."""
|
||||
if (
|
||||
sysinfo
|
||||
and (device_type := sysinfo.get("device_type"))
|
||||
and device_type.endswith("HUB")
|
||||
):
|
||||
if not (device_type := sysinfo.get("device_type")):
|
||||
return DeviceType.Unknown
|
||||
|
||||
if device_type.endswith("HUB"):
|
||||
return DeviceType.Hub
|
||||
|
||||
if "DOORBELL" in device_type:
|
||||
return DeviceType.Doorbell
|
||||
|
||||
return DeviceType.Camera
|
||||
|
||||
@staticmethod
|
||||
@ -44,7 +47,10 @@ class SmartCamDevice(SmartDevice):
|
||||
long_name = discovery_info["device_model"] if discovery_info else short_name
|
||||
device_type = SmartCamDevice._get_device_type_from_sysinfo(basic_info)
|
||||
fw_version_full = basic_info["sw_version"]
|
||||
firmware_version, firmware_build = fw_version_full.split(" ", maxsplit=1)
|
||||
if " " in fw_version_full:
|
||||
firmware_version, firmware_build = fw_version_full.split(" ", maxsplit=1)
|
||||
else:
|
||||
firmware_version, firmware_build = fw_version_full, None
|
||||
return DeviceInfo(
|
||||
short_name=basic_info["device_model"],
|
||||
long_name=long_name,
|
||||
@ -63,21 +69,36 @@ class SmartCamDevice(SmartDevice):
|
||||
info = self._try_get_response(info_resp, "getDeviceInfo")
|
||||
self._info = self._map_info(info["device_info"])
|
||||
|
||||
def _update_children_info(self) -> None:
|
||||
"""Update the internal child device info from the parent info."""
|
||||
def _update_internal_state(self, info: dict[str, Any]) -> None:
|
||||
"""Update the internal info state.
|
||||
|
||||
This is used by the parent to push updates to its children.
|
||||
"""
|
||||
self._info = self._map_info(info)
|
||||
|
||||
async def _update_children_info(self) -> bool:
|
||||
"""Update the internal child device info from the parent info.
|
||||
|
||||
Return true if children added or deleted.
|
||||
"""
|
||||
changed = False
|
||||
if child_info := self._try_get_response(
|
||||
self._last_update, "getChildDeviceList", {}
|
||||
):
|
||||
changed = await self._create_delete_children(
|
||||
child_info, self._last_update["getChildDeviceComponentList"]
|
||||
)
|
||||
|
||||
for info in child_info["child_device_list"]:
|
||||
child_id = info["device_id"]
|
||||
child_id = info.get("device_id")
|
||||
if child_id not in self._children:
|
||||
_LOGGER.debug(
|
||||
"Skipping child update for %s, probably unsupported device",
|
||||
child_id,
|
||||
)
|
||||
# _create_delete_children has already logged a message
|
||||
continue
|
||||
|
||||
self._children[child_id]._update_internal_state(info)
|
||||
|
||||
return changed
|
||||
|
||||
async def _initialize_smart_child(
|
||||
self, info: dict, child_components_raw: ComponentsRaw
|
||||
) -> SmartDevice:
|
||||
@ -99,6 +120,25 @@ class SmartCamDevice(SmartDevice):
|
||||
last_update=initial_response,
|
||||
)
|
||||
|
||||
async def _initialize_smartcam_child(
|
||||
self, info: dict, child_components_raw: ComponentsRaw
|
||||
) -> SmartDevice:
|
||||
"""Initialize a smart child device attached to a smartcam device."""
|
||||
child_id = info["device_id"]
|
||||
child_protocol = _ChildCameraProtocolWrapper(child_id, self.protocol)
|
||||
|
||||
app_component_list = {
|
||||
"app_component_list": child_components_raw["component_list"]
|
||||
}
|
||||
from .smartcamchild import SmartCamChild
|
||||
|
||||
return await SmartCamChild.create(
|
||||
parent=self,
|
||||
child_info=info,
|
||||
child_components_raw=app_component_list,
|
||||
protocol=child_protocol,
|
||||
)
|
||||
|
||||
async def _initialize_children(self) -> None:
|
||||
"""Initialize children for hubs."""
|
||||
child_info_query = {
|
||||
@ -108,25 +148,22 @@ class SmartCamDevice(SmartDevice):
|
||||
resp = await self.protocol.query(child_info_query)
|
||||
self.internal_state.update(resp)
|
||||
|
||||
smart_children_components = {
|
||||
child["device_id"]: child
|
||||
for child in resp["getChildDeviceComponentList"]["child_component_list"]
|
||||
}
|
||||
children = {}
|
||||
for info in resp["getChildDeviceList"]["child_device_list"]:
|
||||
if (
|
||||
(category := info.get("category"))
|
||||
and category in SmartChildDevice.CHILD_DEVICE_TYPE_MAP
|
||||
and (child_id := info.get("device_id"))
|
||||
and (child_components := smart_children_components.get(child_id))
|
||||
):
|
||||
children[child_id] = await self._initialize_smart_child(
|
||||
info, child_components
|
||||
)
|
||||
else:
|
||||
_LOGGER.debug("Child device type not supported: %s", info)
|
||||
async def _try_create_child(
|
||||
self, info: dict, child_components: dict
|
||||
) -> SmartDevice | None:
|
||||
if not (category := info.get("category")):
|
||||
return None
|
||||
|
||||
self._children = children
|
||||
# Smart
|
||||
if category in SmartChildDevice.CHILD_DEVICE_TYPE_MAP:
|
||||
return await self._initialize_smart_child(info, child_components)
|
||||
# Smartcam
|
||||
from .smartcamchild import SmartCamChild
|
||||
|
||||
if category in SmartCamChild.CHILD_DEVICE_TYPE_MAP:
|
||||
return await self._initialize_smartcam_child(info, child_components)
|
||||
|
||||
return None
|
||||
|
||||
async def _initialize_modules(self) -> None:
|
||||
"""Initialize modules based on component negotiation response."""
|
||||
@ -134,11 +171,6 @@ class SmartCamDevice(SmartDevice):
|
||||
if (
|
||||
mod.REQUIRED_COMPONENT
|
||||
and mod.REQUIRED_COMPONENT not in self._components
|
||||
# Always add Camera module to cameras
|
||||
and (
|
||||
mod._module_name() != Module.Camera
|
||||
or self._device_type is not DeviceType.Camera
|
||||
)
|
||||
):
|
||||
continue
|
||||
module = mod(self, mod._module_name())
|
||||
@ -152,9 +184,6 @@ class SmartCamDevice(SmartDevice):
|
||||
for feat in module._module_features.values():
|
||||
self._add_feature(feat)
|
||||
|
||||
for child in self._children.values():
|
||||
await child._initialize_features()
|
||||
|
||||
async def _query_setter_helper(
|
||||
self, method: str, module: str, section: str, params: dict | None = None
|
||||
) -> dict:
|
||||
@ -162,13 +191,6 @@ class SmartCamDevice(SmartDevice):
|
||||
|
||||
return res
|
||||
|
||||
async def _query_getter_helper(
|
||||
self, method: str, module: str, sections: str | list[str]
|
||||
) -> Any:
|
||||
res = await self.protocol.query({method: {module: {"name": sections}}})
|
||||
|
||||
return res
|
||||
|
||||
@staticmethod
|
||||
def _parse_components(components_raw: ComponentsRaw) -> dict[str, int]:
|
||||
return {
|
||||
@ -200,18 +222,17 @@ class SmartCamDevice(SmartDevice):
|
||||
await self._initialize_children()
|
||||
|
||||
def _map_info(self, device_info: dict) -> dict:
|
||||
"""Map the basic keys to the keys used by SmartDevices."""
|
||||
basic_info = device_info["basic_info"]
|
||||
return {
|
||||
"model": basic_info["device_model"],
|
||||
"device_type": basic_info["device_type"],
|
||||
"alias": basic_info["device_alias"],
|
||||
"fw_ver": basic_info["sw_version"],
|
||||
"hw_ver": basic_info["hw_version"],
|
||||
"mac": basic_info["mac"],
|
||||
"hwId": basic_info.get("hw_id"),
|
||||
"oem_id": basic_info["oem_id"],
|
||||
"device_id": basic_info["dev_id"],
|
||||
mappings = {
|
||||
"device_model": "model",
|
||||
"device_alias": "alias",
|
||||
"sw_version": "fw_ver",
|
||||
"hw_version": "hw_ver",
|
||||
"hw_id": "hwId",
|
||||
"dev_id": "device_id",
|
||||
}
|
||||
return {mappings.get(k, k): v for k, v in basic_info.items()}
|
||||
|
||||
@property
|
||||
def is_on(self) -> bool:
|
||||
@ -231,7 +252,7 @@ class SmartCamDevice(SmartDevice):
|
||||
@property
|
||||
def device_type(self) -> DeviceType:
|
||||
"""Return the device type."""
|
||||
if self._device_type == DeviceType.Unknown:
|
||||
if self._device_type == DeviceType.Unknown and self._info:
|
||||
self._device_type = self._get_device_type_from_sysinfo(self._info)
|
||||
return self._device_type
|
||||
|
||||
|
@ -3,7 +3,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from typing import TYPE_CHECKING, Any, Final, cast
|
||||
from typing import TYPE_CHECKING, Final
|
||||
|
||||
from ..exceptions import DeviceError, KasaException, SmartErrorCode
|
||||
from ..modulemapping import ModuleName
|
||||
@ -26,6 +26,9 @@ class SmartCamModule(SmartModule):
|
||||
SmartCamPersonDetection: Final[ModuleName[modules.PersonDetection]] = ModuleName(
|
||||
"PersonDetection"
|
||||
)
|
||||
SmartCamPetDetection: Final[ModuleName[modules.PetDetection]] = ModuleName(
|
||||
"PetDetection"
|
||||
)
|
||||
SmartCamTamperDetection: Final[ModuleName[modules.TamperDetection]] = ModuleName(
|
||||
"TamperDetection"
|
||||
)
|
||||
@ -33,6 +36,8 @@ class SmartCamModule(SmartModule):
|
||||
"BabyCryDetection"
|
||||
)
|
||||
|
||||
SmartCamBattery: Final[ModuleName[modules.Battery]] = ModuleName("Battery")
|
||||
|
||||
SmartCamDeviceModule: Final[ModuleName[modules.DeviceModule]] = ModuleName(
|
||||
"devicemodule"
|
||||
)
|
||||
@ -63,21 +68,7 @@ class SmartCamModule(SmartModule):
|
||||
|
||||
Just a helper method.
|
||||
"""
|
||||
if params:
|
||||
module = next(iter(params))
|
||||
section = next(iter(params[module]))
|
||||
else:
|
||||
module = "system"
|
||||
section = "null"
|
||||
|
||||
if method[:3] == "get":
|
||||
return await self._device._query_getter_helper(method, module, section)
|
||||
|
||||
if TYPE_CHECKING:
|
||||
params = cast(dict[str, dict[str, Any]], params)
|
||||
return await self._device._query_setter_helper(
|
||||
method, module, section, params[module][section]
|
||||
)
|
||||
return await self._device._query_helper(method, params)
|
||||
|
||||
@property
|
||||
def data(self) -> dict:
|
||||
|
@ -120,6 +120,8 @@ class AesTransport(BaseTransport):
|
||||
@property
|
||||
def default_port(self) -> int:
|
||||
"""Default port for the transport."""
|
||||
if port := self._config.connection_type.http_port:
|
||||
return port
|
||||
return self.DEFAULT_PORT
|
||||
|
||||
@property
|
||||
|
@ -48,6 +48,7 @@ import datetime
|
||||
import hashlib
|
||||
import logging
|
||||
import secrets
|
||||
import ssl
|
||||
import struct
|
||||
import time
|
||||
from asyncio import Future
|
||||
@ -92,8 +93,21 @@ class KlapTransport(BaseTransport):
|
||||
"""
|
||||
|
||||
DEFAULT_PORT: int = 80
|
||||
DEFAULT_HTTPS_PORT: int = 4433
|
||||
|
||||
SESSION_COOKIE_NAME = "TP_SESSIONID"
|
||||
TIMEOUT_COOKIE_NAME = "TIMEOUT"
|
||||
# Copy & paste from sslaestransport
|
||||
CIPHERS = ":".join(
|
||||
[
|
||||
"AES256-GCM-SHA384",
|
||||
"AES256-SHA256",
|
||||
"AES128-GCM-SHA256",
|
||||
"AES128-SHA256",
|
||||
"AES256-SHA",
|
||||
]
|
||||
)
|
||||
_ssl_context: ssl.SSLContext | None = None
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
@ -125,12 +139,20 @@ class KlapTransport(BaseTransport):
|
||||
self._session_cookie: dict[str, Any] | None = None
|
||||
|
||||
_LOGGER.debug("Created KLAP transport for %s", self._host)
|
||||
self._app_url = URL(f"http://{self._host}:{self._port}/app")
|
||||
protocol = "https" if config.connection_type.https else "http"
|
||||
self._app_url = URL(f"{protocol}://{self._host}:{self._port}/app")
|
||||
self._request_url = self._app_url / "request"
|
||||
|
||||
@property
|
||||
def default_port(self) -> int:
|
||||
"""Default port for the transport."""
|
||||
config = self._config
|
||||
if port := config.connection_type.http_port:
|
||||
return port
|
||||
|
||||
if config.connection_type.https:
|
||||
return self.DEFAULT_HTTPS_PORT
|
||||
|
||||
return self.DEFAULT_PORT
|
||||
|
||||
@property
|
||||
@ -152,7 +174,9 @@ class KlapTransport(BaseTransport):
|
||||
|
||||
url = self._app_url / "handshake1"
|
||||
|
||||
response_status, response_data = await self._http_client.post(url, data=payload)
|
||||
response_status, response_data = await self._http_client.post(
|
||||
url, data=payload, ssl=await self._get_ssl_context()
|
||||
)
|
||||
|
||||
if _LOGGER.isEnabledFor(logging.DEBUG):
|
||||
_LOGGER.debug(
|
||||
@ -263,6 +287,7 @@ class KlapTransport(BaseTransport):
|
||||
url,
|
||||
data=payload,
|
||||
cookies_dict=self._session_cookie,
|
||||
ssl=await self._get_ssl_context(),
|
||||
)
|
||||
|
||||
if _LOGGER.isEnabledFor(logging.DEBUG):
|
||||
@ -337,6 +362,7 @@ class KlapTransport(BaseTransport):
|
||||
params={"seq": seq},
|
||||
data=payload,
|
||||
cookies_dict=self._session_cookie,
|
||||
ssl=await self._get_ssl_context(),
|
||||
)
|
||||
|
||||
msg = (
|
||||
@ -413,6 +439,23 @@ class KlapTransport(BaseTransport):
|
||||
un = creds.username
|
||||
return md5(un.encode())
|
||||
|
||||
# Copy & paste from sslaestransport.
|
||||
def _create_ssl_context(self) -> ssl.SSLContext:
|
||||
context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
|
||||
context.set_ciphers(self.CIPHERS)
|
||||
context.check_hostname = False
|
||||
context.verify_mode = ssl.CERT_NONE
|
||||
return context
|
||||
|
||||
# Copy & paste from sslaestransport.
|
||||
async def _get_ssl_context(self) -> ssl.SSLContext:
|
||||
if not self._ssl_context:
|
||||
loop = asyncio.get_running_loop()
|
||||
self._ssl_context = await loop.run_in_executor(
|
||||
None, self._create_ssl_context
|
||||
)
|
||||
return self._ssl_context
|
||||
|
||||
|
||||
class KlapTransportV2(KlapTransport):
|
||||
"""Implementation of the KLAP encryption protocol with v2 hanshake hashes."""
|
||||
|
@ -55,6 +55,8 @@ class LinkieTransportV2(BaseTransport):
|
||||
@property
|
||||
def default_port(self) -> int:
|
||||
"""Default port for the transport."""
|
||||
if port := self._config.connection_type.http_port:
|
||||
return port
|
||||
return self.DEFAULT_PORT
|
||||
|
||||
@property
|
||||
|
@ -133,6 +133,8 @@ class SslAesTransport(BaseTransport):
|
||||
@property
|
||||
def default_port(self) -> int:
|
||||
"""Default port for the transport."""
|
||||
if port := self._config.connection_type.http_port:
|
||||
return port
|
||||
return self.DEFAULT_PORT
|
||||
|
||||
@staticmethod
|
||||
|
@ -94,6 +94,8 @@ class SslTransport(BaseTransport):
|
||||
@property
|
||||
def default_port(self) -> int:
|
||||
"""Default port for the transport."""
|
||||
if port := self._config.connection_type.http_port:
|
||||
return port
|
||||
return self.DEFAULT_PORT
|
||||
|
||||
@property
|
||||
@ -215,7 +217,7 @@ class SslTransport(BaseTransport):
|
||||
|
||||
async def send(self, request: str) -> dict[str, Any]:
|
||||
"""Send the request."""
|
||||
_LOGGER.info("Going to send %s", request)
|
||||
_LOGGER.debug("Going to send %s", request)
|
||||
if self._state is not TransportState.ESTABLISHED or self._session_expired():
|
||||
_LOGGER.debug("Transport not established or session expired, logging in")
|
||||
await self.perform_login()
|
||||
|
@ -142,18 +142,16 @@ class XorTransport(BaseTransport):
|
||||
await self.reset()
|
||||
if ex.errno in _NO_RETRY_ERRORS:
|
||||
raise KasaException(
|
||||
f"Unable to connect to the device:"
|
||||
f" {self._host}:{self._port}: {ex}"
|
||||
f"Unable to connect to the device: {self._host}:{self._port}: {ex}"
|
||||
) from ex
|
||||
else:
|
||||
raise _RetryableError(
|
||||
f"Unable to connect to the device:"
|
||||
f" {self._host}:{self._port}: {ex}"
|
||||
f"Unable to connect to the device: {self._host}:{self._port}: {ex}"
|
||||
) from ex
|
||||
except Exception as ex:
|
||||
await self.reset()
|
||||
raise _RetryableError(
|
||||
f"Unable to connect to the device:" f" {self._host}:{self._port}: {ex}"
|
||||
f"Unable to connect to the device: {self._host}:{self._port}: {ex}"
|
||||
) from ex
|
||||
except BaseException:
|
||||
# Likely something cancelled the task so we need to close the connection
|
||||
|
@ -1,6 +1,6 @@
|
||||
[project]
|
||||
name = "python-kasa"
|
||||
version = "0.9.1"
|
||||
version = "0.10.2"
|
||||
description = "Python API for TP-Link Kasa and Tapo devices"
|
||||
license = {text = "GPL-3.0-or-later"}
|
||||
authors = [ { name = "python-kasa developers" }]
|
||||
@ -61,7 +61,7 @@ dev-dependencies = [
|
||||
"mypy~=1.0",
|
||||
"pytest-xdist>=3.6.1",
|
||||
"pytest-socket>=0.7.0",
|
||||
"ruff==0.7.4",
|
||||
"ruff>=0.9.0",
|
||||
]
|
||||
|
||||
|
||||
@ -112,7 +112,7 @@ markers = [
|
||||
]
|
||||
asyncio_mode = "auto"
|
||||
asyncio_default_fixture_loop_scope = "function"
|
||||
timeout = 10
|
||||
#timeout = 10
|
||||
# dist=loadgroup enables grouping of tests into single worker.
|
||||
# required as caplog doesn't play nicely with multiple workers.
|
||||
addopts = "--disable-socket --allow-unix-socket --dist=loadgroup"
|
||||
@ -146,8 +146,6 @@ select = [
|
||||
ignore = [
|
||||
"D105", # Missing docstring in magic method
|
||||
"D107", # Missing docstring in `__init__`
|
||||
"ANN101", # Missing type annotation for `self`
|
||||
"ANN102", # Missing type annotation for `cls` in classmethod
|
||||
"ANN003", # Missing type annotation for `**kwargs`
|
||||
"ANN401", # allow any
|
||||
]
|
||||
|
0
tests/cli/__init__.py
Normal file
0
tests/cli/__init__.py
Normal file
53
tests/cli/test_hub.py
Normal file
53
tests/cli/test_hub.py
Normal file
@ -0,0 +1,53 @@
|
||||
import pytest
|
||||
from pytest_mock import MockerFixture
|
||||
|
||||
from kasa import DeviceType, Module
|
||||
from kasa.cli.hub import hub
|
||||
|
||||
from ..device_fixtures import hubs, plug_iot
|
||||
|
||||
|
||||
@hubs
|
||||
async def test_hub_pair(dev, mocker: MockerFixture, runner, caplog):
|
||||
"""Test that pair calls the expected methods."""
|
||||
cs = dev.modules.get(Module.ChildSetup)
|
||||
# Patch if the device supports the module
|
||||
if cs is not None:
|
||||
mock_pair = mocker.patch.object(cs, "pair")
|
||||
|
||||
res = await runner.invoke(hub, ["pair"], obj=dev, catch_exceptions=False)
|
||||
if cs is None:
|
||||
assert "is not a hub" in res.output
|
||||
return
|
||||
|
||||
mock_pair.assert_awaited()
|
||||
assert "Finding new devices for 10 seconds" in res.output
|
||||
assert res.exit_code == 0
|
||||
|
||||
|
||||
@hubs
|
||||
async def test_hub_unpair(dev, mocker: MockerFixture, runner):
|
||||
"""Test that unpair calls the expected method."""
|
||||
if not dev.children:
|
||||
pytest.skip("Cannot test without child devices")
|
||||
|
||||
id_ = next(iter(dev.children)).device_id
|
||||
|
||||
cs = dev.modules.get(Module.ChildSetup)
|
||||
mock_unpair = mocker.spy(cs, "unpair")
|
||||
|
||||
res = await runner.invoke(hub, ["unpair", id_], obj=dev, catch_exceptions=False)
|
||||
|
||||
mock_unpair.assert_awaited()
|
||||
assert f"Unpaired {id_}" in res.output
|
||||
assert res.exit_code == 0
|
||||
|
||||
|
||||
@plug_iot
|
||||
async def test_non_hub(dev, mocker: MockerFixture, runner):
|
||||
"""Test that hub commands return an error if executed on a non-hub."""
|
||||
assert dev.device_type is not DeviceType.Hub
|
||||
res = await runner.invoke(
|
||||
hub, ["unpair", "dummy_id"], obj=dev, catch_exceptions=False
|
||||
)
|
||||
assert "is not a hub" in res.output
|
114
tests/cli/test_vacuum.py
Normal file
114
tests/cli/test_vacuum.py
Normal file
@ -0,0 +1,114 @@
|
||||
from pytest_mock import MockerFixture
|
||||
|
||||
from kasa import DeviceType, Module
|
||||
from kasa.cli.vacuum import vacuum
|
||||
|
||||
from ..device_fixtures import plug_iot
|
||||
from ..device_fixtures import vacuum as vacuum_devices
|
||||
|
||||
|
||||
@vacuum_devices
|
||||
async def test_vacuum_records_group(dev, mocker: MockerFixture, runner):
|
||||
"""Test that vacuum records calls the expected methods."""
|
||||
rec = dev.modules.get(Module.CleanRecords)
|
||||
assert rec
|
||||
|
||||
res = await runner.invoke(vacuum, ["records"], obj=dev, catch_exceptions=False)
|
||||
|
||||
latest = rec.parsed_data.last_clean
|
||||
expected = (
|
||||
f"Totals: {rec.total_clean_area} {rec.area_unit} in {rec.total_clean_time} "
|
||||
f"(cleaned {rec.total_clean_count} times)\n"
|
||||
f"Last clean: {latest.clean_area} {rec.area_unit} @ {latest.clean_time}"
|
||||
)
|
||||
assert expected in res.output
|
||||
assert res.exit_code == 0
|
||||
|
||||
|
||||
@vacuum_devices
|
||||
async def test_vacuum_records_list(dev, mocker: MockerFixture, runner):
|
||||
"""Test that vacuum records list calls the expected methods."""
|
||||
rec = dev.modules.get(Module.CleanRecords)
|
||||
assert rec
|
||||
|
||||
res = await runner.invoke(
|
||||
vacuum, ["records", "list"], obj=dev, catch_exceptions=False
|
||||
)
|
||||
|
||||
data = rec.parsed_data
|
||||
for record in data.records:
|
||||
expected = (
|
||||
f"* {record.timestamp}: cleaned {record.clean_area} {rec.area_unit}"
|
||||
f" in {record.clean_time}"
|
||||
)
|
||||
assert expected in res.output
|
||||
assert res.exit_code == 0
|
||||
|
||||
|
||||
@vacuum_devices
|
||||
async def test_vacuum_consumables(dev, runner):
|
||||
"""Test that vacuum consumables calls the expected methods."""
|
||||
cons = dev.modules.get(Module.Consumables)
|
||||
assert cons
|
||||
|
||||
res = await runner.invoke(vacuum, ["consumables"], obj=dev, catch_exceptions=False)
|
||||
|
||||
expected = ""
|
||||
for c in cons.consumables.values():
|
||||
expected += f"{c.name} ({c.id}): {c.used} used, {c.remaining} remaining\n"
|
||||
|
||||
assert expected in res.output
|
||||
assert res.exit_code == 0
|
||||
|
||||
|
||||
@vacuum_devices
|
||||
async def test_vacuum_consumables_reset(dev, mocker: MockerFixture, runner):
|
||||
"""Test that vacuum consumables reset calls the expected methods."""
|
||||
cons = dev.modules.get(Module.Consumables)
|
||||
assert cons
|
||||
|
||||
reset_consumable_mock = mocker.spy(cons, "reset_consumable")
|
||||
for c_id in cons.consumables:
|
||||
reset_consumable_mock.reset_mock()
|
||||
res = await runner.invoke(
|
||||
vacuum, ["consumables", "reset", c_id], obj=dev, catch_exceptions=False
|
||||
)
|
||||
reset_consumable_mock.assert_awaited_once_with(c_id)
|
||||
assert f"Consumable {c_id} reset" in res.output
|
||||
assert res.exit_code == 0
|
||||
|
||||
res = await runner.invoke(
|
||||
vacuum, ["consumables", "reset", "foobar"], obj=dev, catch_exceptions=False
|
||||
)
|
||||
expected = (
|
||||
"Consumable foobar not found in "
|
||||
f"device consumables: {', '.join(cons.consumables.keys())}."
|
||||
)
|
||||
assert expected in res.output.replace("\n", "")
|
||||
assert res.exit_code != 0
|
||||
|
||||
|
||||
@plug_iot
|
||||
async def test_non_vacuum(dev, mocker: MockerFixture, runner):
|
||||
"""Test that vacuum commands return an error if executed on a non-vacuum."""
|
||||
assert dev.device_type is not DeviceType.Vacuum
|
||||
|
||||
res = await runner.invoke(vacuum, ["records"], obj=dev, catch_exceptions=False)
|
||||
assert "This device does not support records" in res.output
|
||||
assert res.exit_code != 0
|
||||
|
||||
res = await runner.invoke(
|
||||
vacuum, ["records", "list"], obj=dev, catch_exceptions=False
|
||||
)
|
||||
assert "This device does not support records" in res.output
|
||||
assert res.exit_code != 0
|
||||
|
||||
res = await runner.invoke(vacuum, ["consumables"], obj=dev, catch_exceptions=False)
|
||||
assert "This device does not support consumables" in res.output
|
||||
assert res.exit_code != 0
|
||||
|
||||
res = await runner.invoke(
|
||||
vacuum, ["consumables", "reset", "foobar"], obj=dev, catch_exceptions=False
|
||||
)
|
||||
assert "This device does not support consumables" in res.output
|
||||
assert res.exit_code != 0
|
@ -1,6 +1,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import os
|
||||
import sys
|
||||
import warnings
|
||||
from pathlib import Path
|
||||
@ -8,6 +9,9 @@ from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
# TODO: this and runner fixture could be moved to tests/cli/conftest.py
|
||||
from asyncclick.testing import CliRunner
|
||||
|
||||
from kasa import (
|
||||
DeviceConfig,
|
||||
SmartProtocol,
|
||||
@ -149,3 +153,12 @@ def mock_datagram_endpoint(request): # noqa: PT004
|
||||
side_effect=_create_datagram_endpoint,
|
||||
):
|
||||
yield
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def runner():
|
||||
"""Runner fixture that unsets the KASA_ environment variables for tests."""
|
||||
KASA_VARS = {k: None for k, v in os.environ.items() if k.startswith("KASA_")}
|
||||
runner = CliRunner(env=KASA_VARS)
|
||||
|
||||
return runner
|
||||
|
@ -131,9 +131,12 @@ SENSORS_SMART = {
|
||||
"S200D",
|
||||
"S210",
|
||||
"S220",
|
||||
"D100C", # needs a home category?
|
||||
}
|
||||
THERMOSTATS_SMART = {"KE100"}
|
||||
|
||||
VACUUMS_SMART = {"RV20"}
|
||||
|
||||
WITH_EMETER_IOT = {"HS110", "HS300", "KP115", "KP125", *BULBS_IOT}
|
||||
WITH_EMETER_SMART = {"P110", "P110M", "P115", "KP125M", "EP25", "P304M"}
|
||||
WITH_EMETER = {*WITH_EMETER_IOT, *WITH_EMETER_SMART}
|
||||
@ -151,6 +154,7 @@ ALL_DEVICES_SMART = (
|
||||
.union(SENSORS_SMART)
|
||||
.union(SWITCHES_SMART)
|
||||
.union(THERMOSTATS_SMART)
|
||||
.union(VACUUMS_SMART)
|
||||
)
|
||||
ALL_DEVICES = ALL_DEVICES_IOT.union(ALL_DEVICES_SMART)
|
||||
|
||||
@ -335,13 +339,25 @@ device_smartcam = parametrize("devices smartcam", protocol_filter={"SMARTCAM"})
|
||||
camera_smartcam = parametrize(
|
||||
"camera smartcam",
|
||||
device_type_filter=[DeviceType.Camera],
|
||||
protocol_filter={"SMARTCAM"},
|
||||
protocol_filter={"SMARTCAM", "SMARTCAM.CHILD"},
|
||||
)
|
||||
hub_smartcam = parametrize(
|
||||
"hub smartcam",
|
||||
device_type_filter=[DeviceType.Hub],
|
||||
protocol_filter={"SMARTCAM"},
|
||||
)
|
||||
hubs = parametrize_combine([hubs_smart, hub_smartcam])
|
||||
doobell_smartcam = parametrize(
|
||||
"doorbell smartcam",
|
||||
device_type_filter=[DeviceType.Doorbell],
|
||||
protocol_filter={"SMARTCAM", "SMARTCAM.CHILD"},
|
||||
)
|
||||
chime_smart = parametrize(
|
||||
"chime smart",
|
||||
device_type_filter=[DeviceType.Chime],
|
||||
protocol_filter={"SMART"},
|
||||
)
|
||||
vacuum = parametrize("vacuums", device_type_filter=[DeviceType.Vacuum])
|
||||
|
||||
|
||||
def check_categories():
|
||||
@ -358,8 +374,11 @@ def check_categories():
|
||||
+ hubs_smart.args[1]
|
||||
+ sensors_smart.args[1]
|
||||
+ thermostats_smart.args[1]
|
||||
+ chime_smart.args[1]
|
||||
+ camera_smartcam.args[1]
|
||||
+ doobell_smartcam.args[1]
|
||||
+ hub_smartcam.args[1]
|
||||
+ vacuum.args[1]
|
||||
)
|
||||
diffs: set[FixtureInfo] = set(FIXTURE_DATA) - set(categorized_fixtures)
|
||||
if diffs:
|
||||
@ -377,7 +396,7 @@ check_categories()
|
||||
def device_for_fixture_name(model, protocol):
|
||||
if protocol in {"SMART", "SMART.CHILD"}:
|
||||
return SmartDevice
|
||||
elif protocol == "SMARTCAM":
|
||||
elif protocol in {"SMARTCAM", "SMARTCAM.CHILD"}:
|
||||
return SmartCamDevice
|
||||
else:
|
||||
for d in STRIPS_IOT:
|
||||
@ -430,11 +449,20 @@ async def get_device_for_fixture(
|
||||
d = device_for_fixture_name(fixture_data.name, fixture_data.protocol)(
|
||||
host="127.0.0.123"
|
||||
)
|
||||
|
||||
# smart child devices sometimes check _is_hub_child which needs a parent
|
||||
# of DeviceType.Hub
|
||||
class DummyParent:
|
||||
device_type = DeviceType.Hub
|
||||
|
||||
if fixture_data.protocol in {"SMARTCAM.CHILD"}:
|
||||
d._parent = DummyParent()
|
||||
|
||||
if fixture_data.protocol in {"SMART", "SMART.CHILD"}:
|
||||
d.protocol = FakeSmartProtocol(
|
||||
fixture_data.data, fixture_data.name, verbatim=verbatim
|
||||
)
|
||||
elif fixture_data.protocol == "SMARTCAM":
|
||||
elif fixture_data.protocol in {"SMARTCAM", "SMARTCAM.CHILD"}:
|
||||
d.protocol = FakeSmartCamProtocol(
|
||||
fixture_data.data, fixture_data.name, verbatim=verbatim
|
||||
)
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user