Commit Graph

38 Commits

Author SHA1 Message Date
sdb9696
f6fd898faf
Add DeviceConfig to allow specifying configuration parameters (#569)
* Add DeviceConfig handling

* Update post review

* Further update post latest review

* Update following latest review

* Update docstrings and docs
2023-12-29 20:17:15 +01:00
Teemu R
c431dbb832
Use ruff and ruff format (#534)
Replaces the previously used linting and code formatting tools with ruff.
2023-10-29 23:15:42 +01:00
J. Nick Koston
0ec0826cc7
Make timeout adjustable (#494) 2023-10-07 20:58:00 +02:00
cobryan05
a2444da9df
Split queries to avoid overflowing device buffers (#502)
Several KASA devices seem to have pretty strict buffer size limitations on incoming/outgoing data transfers.

Testing on KL125-US and HL103 has shown that sending a request size larger than about ~768 bytes will immediately crash the device. Additionally, a query that generates a response larger than ~4096 bytes will crash the KL125-US. I was unable to generate such a large response to test the HL103.

The KL125-US will only return such large queries when its monthly usage stats have been populated. This means that a new bulb would work fine, but after a month of data collection the bulb would break the 4K limit and start to crash.

To work around this issue, an estimated worst-case response size is calculated before sending a request by summing up all modules estimated response size. If the estimated size is greater than the device's max_response_payload_size then the query will be split into multiple queries.

This PR implements splitting queries expected to have large responses and also removes the module 'skip list' which was a previous workaround to the crash (which worked by simply reducing the number of modules queried, which prevented the overflow) since it is no longer necessary.

This PR does not attempt to address the "input buffer size limit." Thus far this limit has not been an issue.
2023-09-14 20:51:40 +02:00
sdb9696
7bb4a456a2
Add plumbing for passing credentials to devices (#507)
* Add plumbing for passing credentials as far as discovery

* Pass credentials to Smart devices

* Rename authentication exception

* Fix tests failure due to test_json_output leaving echo as nop

* Fix test_credentials test

* Do not print credentials, fix echo function bug and improve get type parameter

* Add device class constructor test

* Add comment for echo handling and move assignment
2023-09-13 15:46:38 +02:00
Teemu R
53021f07fe
Add support for pydantic v2 using v1 shims (#504) 2023-08-28 17:48:49 +02:00
Viktar Karpach
9b039d8374
Make device port configurable (#471) 2023-07-10 01:55:27 +02:00
Teemu R
233f1c9534
Cleanup fixture filenames (#448)
* Mark mocked fixtures as such

* Use consistent filenames including the swver

* Remove executable bit

* Remove duplicate KL130(US)

* Remove unnecessary mocks where we have real ones available

* Fix filenames in tests
2023-04-01 17:10:46 +02:00
Teemu R
505b63dd55
Allow effect presets seen on light strips (#440)
* Make hue, saturation and color_temp optional for smartbulbpresets

* Adjust bulb preset attributes for effect mode

* Don't send None values on save_preset

* Add tests for save_preset payloads
2023-04-01 16:15:58 +02:00
Teemu R
1212715dde
Minor fixes to smartbulb docs (#431) 2023-02-18 21:17:19 +01:00
Teemu R
327efb6c65
Update pre-commit hooks (#401)
* Update pre-commit hooks

* Fix implicit optionals
2022-11-15 19:05:08 +01:00
Teemu R
9cb2a56405
Add a note that transition is not supported by all devices (#398) 2022-11-11 21:06:54 +01:00
Teemu R
ef98c2aed9
Implement changing the bulb turn-on behavior (#381)
* Implement changing the bulb turn-on behavior

* Improve docstrings a bit

* Improve docs and expose TurnOnBehavior(s)

* fix typing
2022-10-27 17:40:54 +02:00
Teemu R
f32f7f3925
Add support for bulb presets (#379)
* Add support for bulb presets

* Update docs
2022-10-23 00:15:47 +02:00
Teemu R
a39cef9a8c
Export modules & make sphinx happy (#334) 2022-04-06 01:41:08 +02:00
Teemu R
3926f3224f Add module support & query their information during update cycle (#243)
* Add module support & modularize existing query

This creates a base to expose more features on the supported devices.
At the moment, the most visible change is that each update cycle gets information from all available modules:
* Basic system info
* Cloud (new)
* Countdown (new)
* Antitheft (new)
* Schedule (new)
* Time (existing, implements the time/timezone handling)
* Emeter (existing, partially separated from smartdevice)

* Fix imports

* Fix linting

* Use device host instead of alias in module repr

* Add property to list available modules, print them in cli state report

* usage: fix the get_realtime query

* separate usage from schedule to avoid multi-inheritance

* Fix module querying

* Add is_supported property to modules
2022-04-05 19:27:46 +02:00
J. Nick Koston
766819a2a4
Ensure bulb state is restored when turning back on (#330)
* Ensure state is restored when turning back on

Fixes https://github.com/home-assistant/core/issues/69039

* Update kasa/tests/test_bulb.py

Co-authored-by: Teemu R. <tpr@iki.fi>

Co-authored-by: Teemu R. <tpr@iki.fi>
2022-04-05 18:51:36 +02:00
J. Nick Koston
58f6517445
Add effect support for light strips (#293)
* Add effect support for KL430

* KL400 supports effects

* Add KL400 fixture

* Comments from review

* actually commit the remove
2022-03-21 22:10:12 +01:00
Teemu R
a468d520c0
Add KL135 color temperature range (#256) 2021-11-19 18:08:20 +01:00
Teemu R
36c412a9c2
Improve emeterstatus API, move into own module (#205)
Adds the following properties to EmeterStatus for saner API:
* voltage (in V)
* power (in W)
* current (in A)
* total (in kWh)
2021-09-23 17:58:19 +02:00
Teemu R
2c83d8ee6d
bulb: allow set_hsv without v, add fallback ct range (#200)
* bulb: allow set_hsv without v, add fallback ct range

* add ColorTempRange and HSV named tuples
* add a fallback color temp range if unknown, log a warning
* set_hsv: the value is now optional

* Fix tests, change fallback range to 2700-5000
2021-09-21 13:23:56 +02:00
Teemu R
016d030245
Improve bulb support (alias, time settings) (#198)
* Fix set_alias and time related functions for bulbs

* Fix tests for smartlife.iot.common.timesetting and smartlife.iot.common.system
2021-09-19 23:53:17 +02:00
Teemu R
4e8a3185fb
Use less strict matcher for kl430 color temperature (#190) 2021-08-16 16:49:28 +02:00
mdarnol
2fe1b209d0
Add KL125 bulb definition (#143)
Co-authored-by: Mark Arnold <marnold@nvidia.com>
2021-02-27 15:09:33 +01:00
Teemu R
d30d00a3ff
Add support for lightstrips (KL430) (#74)
* Preliminary support for light strips

* Add color temperature range and cleanup, thanks to @darkoppressor

* Use lightstrip instead of {led,light}strip consistently everywhere
* The cli flag is now --lightstrip

* add apidocs

* Add fixture file for KL430

Signed-off-by: Kevin Wells <darkoppressor@gmail.com>

* Add discovery support, expose effect and length of the strip

* use set_light_state instead of transition_light_state

* Add tests for lightstrip

* add doctests

* Add KL430 to supported devices in README

Co-authored-by: Kevin Wells <darkoppressor@gmail.com>
2020-07-19 22:32:17 +02:00
Teemu R
4d722e25c1
Add --transition to bulb-specific cli commands, fix turn_{on,off} signatures (#81) 2020-07-06 16:10:28 +02:00
Teemu R
44e2998705
Improve bulb API, force turn on for all light changes as offline changes are not supported (#76)
* Add ignore_default to lights to allow setting to specific light state, force bulb on when changing the settings, allow defining brightness for set_color_temp, add a couple of new API methods

* Fix and simplify transition_light_state to make tests pass
2020-07-06 16:09:30 +02:00
Teemu R
f9a987ca18
Simplify API documentation by using doctests (#73)
* Add doctests to SmartBulb

* Add SmartDevice doctests, cleanup README.md

* add doctests for smartplug and smartstrip

* add discover doctests

* Fix bulb mock

* add smartdimmer doctests

* add sphinx-generated docs, cleanup readme a bit

* remove sphinx-click as it does not work with asyncclick

* in preparation for rtd hooking, move doc deps to be separate from dev deps

* pytestmark needs to be applied separately for each and every file, this fixes the tests

* use pathlib for resolving relative paths

* Skip discovery doctest on python3.7

The code is just fine, but some reason the mocking behaves differently between 3.7 and 3.8.
The latter seems to accept a discrete object for asyncio.run where the former expects a coroutine..
2020-06-30 02:29:52 +02:00
Teemu R
99e0c4a418
Bulbs: allow specifying transition for state changes (#70)
All state changing functions now allow defining transition time in milliseconds
2020-06-14 20:21:55 +02:00
Teemu R
644a10a0d1
General cleanups all around (janitoring) (#63)
* Move tests to device-type specific test files to make improvements more approachable

* protocol: remove the port parameter from query, as there are no other known ports, fix docstrings

* Revise docstrings, remove superfluous information and remove unused methods ({set,get_icon} and set_time)

* cli: indent device output to make it more easily readable when having multiple devices

* remove adjust flake8 ignores (we have no setup.py anymore)

* pyproject: include cli tool to coverage, add config for interrogate (docstring coverage)

* bulb: raise exception on color_temp error cases instead of returning zero values

* improve bulb tests, simplify conftest

* strip: rename plugs property to children and move it to smartdevice
2020-05-27 16:55:18 +02:00
Teemu R
836f1701b9
Optimize I/O access (#59)
* Optimize I/O access

A single update() will now fetch information from all interesting modules,
including the current device state and the emeter information.

In practice, this will allow dropping the number of I/O reqs per homeassistant update cycle to 1,
which is paramount at least for bulbs which are very picky about sequential accesses.
This can be further extend to other modules/methods, if needed.

Currently fetched data:
* sysinfo
* realtime, today's and this months emeter stats

New properties:
* emeter_realtime: return the most recent emeter information, update()-version of get_emeter_realtime()
* emeter_today: returning today's energy consumption
* emeter_this_month: same for this month

Other changes:
* Accessing @requires_update properties will cause SmartDeviceException if the device has not ever been update()d
* Fix __repr__ for devices that haven't been updated
* Smartbulb uses now the state data from get_sysinfo instead of separately querying the bulb service
* SmartStrip's state_information no longer lists onsince for separate plugs
* The above mentioned properties are now printed out by cli
* Simplify is_on handling for bulbs

* remove implicit updates, return device responses for actions, update README.md instructions. fixes #61
2020-05-24 17:57:54 +02:00
Teemu R
ed57563e8b
Convert to use poetry & pyproject.toml for dep & build management (#54)
* Convert to use poetry and pyproject.toml, update README

* add some resources for contributors

* minor adjustments

* ci: separate tests from linting, run using poetry

* add pytest-mock to dev requirements

* combine running tests and reporting to codecov

* generate both xml and html coverage reports

* add codecov to dev dependencies
2020-05-12 12:11:47 +02:00
Teemu R
c90465c5dd
Remove unnecessary cache (#40)
The cache was useful trick when the property accesses caused I/O,
which is unnecessary now as dev.update() does explicitly cache results until its called again.
2020-04-12 15:57:49 +02:00
Teemu R
00276e34b7
Move child socket handling to its own SmartStripPlug class (#26)
* All child device handling is moved out from the main smartdevice class, which simplifies the code.
* This will also cleanup the constructors as only the subdevices require the ID and the parent reference.
* SmartStripPlug offers SmartPlug like interface, but does not allow separate updates
  * Trying to update() on the children will cause a warning.
2020-03-16 14:52:40 +01:00
Teemu R
72d6205ad2 Cleanup constructors by removing ioloop and protocol arguments (#23)
* Cleanup constructors by removing ioloop and protocol

* force kwarg for other arguments besides the host
2020-01-15 07:46:21 +01:00
Teemu R
0f0df481a0
Add device_id property, rename context to child_id (#15)
For regular devices, device_id is the mac address and for child devices it is a combination of the mac address and the child_id.
2020-01-13 00:17:45 +01:00
Teemu R
524d28abbc
Remove sync interface, add asyncio discovery (#14)
* do not update inside __repr__

* Convert discovery to asyncio

* Use asyncio.DatagramProtocol
* Cleanup parameters, no more positional arguments

Closes #7

* Remove sync interface

* This requires #13 to be merged. Closes #12.
* Converts cli to use asyncio.run() where needed.
* The children from smartstrips is being initialized during the first update call.

* Convert on and off commands to use asyncio.run

* conftest: do the initial update automatically for the device, cleans up tests a bit

* return subdevices alias for strip plugs, remove sync from docstrings

* Make tests pass using pytest-asyncio

* Simplify tests and use pytest-asyncio.
* Removed the emeter tests for child devices, as this information do not seem to exist (based on the dummy sysinfo data). Can be added again if needed.
* Remove sync from docstrings.

* Fix incorrect type hint

* Add type hints and some docstrings to discovery
2020-01-12 22:44:19 +01:00
Teemu R
3ef5086ffb mass rename to (python-)kasa (#1) 2019-12-18 09:11:18 +01:00