2020-05-27 14:55:18 +00:00
|
|
|
"""Module for bulbs (LB*, KL*, KB*)."""
|
2024-04-16 18:21:20 +00:00
|
|
|
|
2021-09-21 11:23:56 +00:00
|
|
|
import logging
|
2018-06-17 17:01:18 +00:00
|
|
|
import re
|
2022-10-27 15:40:54 +00:00
|
|
|
from enum import Enum
|
2024-03-26 18:28:39 +00:00
|
|
|
from typing import Dict, List, Optional, cast
|
2022-10-22 22:15:47 +00:00
|
|
|
|
2023-08-28 15:48:49 +00:00
|
|
|
try:
|
|
|
|
from pydantic.v1 import BaseModel, Field, root_validator
|
|
|
|
except ImportError:
|
|
|
|
from pydantic import BaseModel, Field, root_validator
|
2017-04-14 12:24:58 +00:00
|
|
|
|
2024-02-04 15:20:08 +00:00
|
|
|
from ..bulb import HSV, Bulb, BulbPreset, ColorTempRange
|
|
|
|
from ..device_type import DeviceType
|
|
|
|
from ..deviceconfig import DeviceConfig
|
2024-03-06 16:45:08 +00:00
|
|
|
from ..feature import Feature, FeatureType
|
2024-02-04 15:20:08 +00:00
|
|
|
from ..protocol import BaseProtocol
|
2024-02-21 15:52:55 +00:00
|
|
|
from .iotdevice import IotDevice, KasaException, requires_update
|
2021-11-07 01:41:12 +00:00
|
|
|
from .modules import Antitheft, Cloud, Countdown, Emeter, Schedule, Time, Usage
|
2022-10-22 22:15:47 +00:00
|
|
|
|
|
|
|
|
2022-10-27 15:40:54 +00:00
|
|
|
class BehaviorMode(str, Enum):
|
|
|
|
"""Enum to present type of turn on behavior."""
|
|
|
|
|
2023-02-18 20:17:19 +00:00
|
|
|
#: Return to the last state known state.
|
2022-10-27 15:40:54 +00:00
|
|
|
Last = "last_status"
|
2023-02-18 20:17:19 +00:00
|
|
|
#: Use chosen preset.
|
2022-10-27 15:40:54 +00:00
|
|
|
Preset = "customize_preset"
|
|
|
|
|
|
|
|
|
|
|
|
class TurnOnBehavior(BaseModel):
|
|
|
|
"""Model to present a single turn on behavior.
|
|
|
|
|
|
|
|
:param int preset: the index number of wanted preset.
|
2023-10-29 22:15:42 +00:00
|
|
|
:param BehaviorMode mode: last status or preset mode.
|
|
|
|
If you are changing existing settings, you should not set this manually.
|
2022-10-27 15:40:54 +00:00
|
|
|
|
2023-02-18 20:17:19 +00:00
|
|
|
To change the behavior, it is only necessary to change the :attr:`preset` field
|
2022-10-27 15:40:54 +00:00
|
|
|
to contain either the preset index, or ``None`` for the last known state.
|
|
|
|
"""
|
|
|
|
|
2023-02-18 20:17:19 +00:00
|
|
|
#: Index of preset to use, or ``None`` for the last known state.
|
2022-10-27 15:40:54 +00:00
|
|
|
preset: Optional[int] = Field(alias="index", default=None)
|
2023-02-18 20:17:19 +00:00
|
|
|
#: Wanted behavior
|
2022-10-27 15:40:54 +00:00
|
|
|
mode: BehaviorMode
|
|
|
|
|
|
|
|
@root_validator
|
2023-02-18 20:17:19 +00:00
|
|
|
def _mode_based_on_preset(cls, values):
|
2022-10-27 15:40:54 +00:00
|
|
|
"""Set the mode based on the preset value."""
|
|
|
|
if values["preset"] is not None:
|
|
|
|
values["mode"] = BehaviorMode.Preset
|
|
|
|
else:
|
|
|
|
values["mode"] = BehaviorMode.Last
|
|
|
|
|
|
|
|
return values
|
|
|
|
|
|
|
|
class Config:
|
|
|
|
"""Configuration to make the validator run when changing the values."""
|
|
|
|
|
|
|
|
validate_assignment = True
|
|
|
|
|
|
|
|
|
|
|
|
class TurnOnBehaviors(BaseModel):
|
2023-02-18 20:17:19 +00:00
|
|
|
"""Model to contain turn on behaviors."""
|
2022-10-27 15:40:54 +00:00
|
|
|
|
2023-02-18 20:17:19 +00:00
|
|
|
#: The behavior when the bulb is turned on programmatically.
|
2022-10-27 15:40:54 +00:00
|
|
|
soft: TurnOnBehavior = Field(alias="soft_on")
|
2023-02-18 20:17:19 +00:00
|
|
|
#: The behavior when the bulb has been off from mains power.
|
2022-10-27 15:40:54 +00:00
|
|
|
hard: TurnOnBehavior = Field(alias="hard_on")
|
|
|
|
|
|
|
|
|
API and tests cleanup (#151)
* Add new cli commands: raw_command and dump_discover
- raw_command can be used to execute raw commands with given parameters
* Useful for testing new calls before implementing them properly
- dump_discover can be used to dump the device discovery information (into a file)
* The discovery is extended to request more modules and methods from devices
* smartlife.iot.dimmer get_dimmer_parameters
* smartlife.iot.common.emeter get_realtime
* smartlife.iot.smartbulb.lightingservice get_light_state
* This is used to dump more information for proper tests, and will also allow better discovery in the future
This commit contains also some documentation updates and dropping click_datetime in favor of click's built-in datetime
* Docstring fixes
* Major API cleanup
Properties shall no more change the state of the device, this work in still in progress, the main goal being making the API more user-friendly and to make implementing new features simpler.
The newly deprecated functionality will remain working and will simply warn the user about deprecation.
Previously deprecated 'features' property and 'identify' method are now finally removed.
Deprecate and replace the following property setters:
* state with turn_on() and turn_off()
* hsv with set_hsv()
* color_temp with set_color_temp()
* brightness with set_brightness()
* led with set_led()
* alias with set_alias()
* mac with set_mac()
And getters:
* state with is_on and is_off
The {BULB,PLUG}_STATE_{ON,OFF} is simplified to STATE_ON and STATE_OFF, UNKNOWN state is removed.
These are now deprecated and will be removed in the future.
* is_on and is_off can be used to check for the state
* turn_on() and turn_off() for changing the device state.
Trying to use functionality not supported by the device will cause SmartDeviceExceptions instead of failing silently and/or returning None.
This includes, e.g., trying to set a color temperature on non-supported bulb.
ValueErrors are raised instead of SmartDeviceExceptions where appropriate (e.g. when trying to set an invalid hsv or brightness).
New enum type DeviceType is added to allow detecting device types without resorting to isinstance() calling. SmartDevice class' device_type property can be used to query the type. is_plug and is_bulb helpers are added.
* Cleanup tests and improve test coverage
* Make writing tests easier by sharing code for common implementations
* Instead of storing test data inside python files, dump-discover based information is used
* This will simplify adding new tests and remove code duplication
* fixtures are based on https://github.com/plasticrake/tplink-smarthome-simulator
* run black on newfakes
* Add HS300 tests and update SmartStrip API according to earlier changes, still WIP
* run black and avoid wildcard imports
* Black on conftest
* bump minimum required version to 3.5
* Rename fixture_tests to test_fixtures for autocollect
* fix typoed type to _type, black
* run black on several files with -79 to fix hound issues
* Fix broken merge on hue
* Fix tests (hue update, pass context to smartdevice), add is_strip property, disable emeter tests for HS300 until a solution for API is found.
* Fix old tests
* Run black on changed files
* Add real HS220 discovery, thanks to @poiyo
* add is_dimmable and is_variable_color_temp to smartdevice class, simplifies interfacing with homeassistant
* add KL120(US) fixture
* Add a simple query cache
This commit adds a simple query cache to speed up the process for users
requesting lots of different properties from the device, as done by the
cli tool as well as homeassistant.
The logic for caching is very simple:
1. A timestamp for last fetch for each module+command is stored alongside the response.
2. If the issued command starts with `get_` and the TTL has not expired, the cache result is returned.
3. Otherwise the cache for the whole corresponding module gets invalidated, the device will be queried and the result will be stored in the cache.
* add deprecation to tox.ini
* make tests pass again
* remove old tests, add flake8 to tox reqs
* run black against pyhs100 module, add it to precommit hooks, fix flake8 configuration to conform to black standards (https://ljvmiranda921.github.io/notebook/2018/06/21/precommits-using-black-and-flake8/)
* fix syntax
* cleanup conftest
2019-06-16 21:05:00 +00:00
|
|
|
TPLINK_KELVIN = {
|
2021-09-21 11:23:56 +00:00
|
|
|
"LB130": ColorTempRange(2500, 9000),
|
|
|
|
"LB120": ColorTempRange(2700, 6500),
|
|
|
|
"LB230": ColorTempRange(2500, 9000),
|
|
|
|
"KB130": ColorTempRange(2500, 9000),
|
|
|
|
"KL130": ColorTempRange(2500, 9000),
|
|
|
|
"KL125": ColorTempRange(2500, 6500),
|
2021-11-19 17:08:20 +00:00
|
|
|
"KL135": ColorTempRange(2500, 6500),
|
2021-09-21 11:23:56 +00:00
|
|
|
r"KL120\(EU\)": ColorTempRange(2700, 6500),
|
|
|
|
r"KL120\(US\)": ColorTempRange(2700, 5000),
|
|
|
|
r"KL430": ColorTempRange(2500, 9000),
|
API and tests cleanup (#151)
* Add new cli commands: raw_command and dump_discover
- raw_command can be used to execute raw commands with given parameters
* Useful for testing new calls before implementing them properly
- dump_discover can be used to dump the device discovery information (into a file)
* The discovery is extended to request more modules and methods from devices
* smartlife.iot.dimmer get_dimmer_parameters
* smartlife.iot.common.emeter get_realtime
* smartlife.iot.smartbulb.lightingservice get_light_state
* This is used to dump more information for proper tests, and will also allow better discovery in the future
This commit contains also some documentation updates and dropping click_datetime in favor of click's built-in datetime
* Docstring fixes
* Major API cleanup
Properties shall no more change the state of the device, this work in still in progress, the main goal being making the API more user-friendly and to make implementing new features simpler.
The newly deprecated functionality will remain working and will simply warn the user about deprecation.
Previously deprecated 'features' property and 'identify' method are now finally removed.
Deprecate and replace the following property setters:
* state with turn_on() and turn_off()
* hsv with set_hsv()
* color_temp with set_color_temp()
* brightness with set_brightness()
* led with set_led()
* alias with set_alias()
* mac with set_mac()
And getters:
* state with is_on and is_off
The {BULB,PLUG}_STATE_{ON,OFF} is simplified to STATE_ON and STATE_OFF, UNKNOWN state is removed.
These are now deprecated and will be removed in the future.
* is_on and is_off can be used to check for the state
* turn_on() and turn_off() for changing the device state.
Trying to use functionality not supported by the device will cause SmartDeviceExceptions instead of failing silently and/or returning None.
This includes, e.g., trying to set a color temperature on non-supported bulb.
ValueErrors are raised instead of SmartDeviceExceptions where appropriate (e.g. when trying to set an invalid hsv or brightness).
New enum type DeviceType is added to allow detecting device types without resorting to isinstance() calling. SmartDevice class' device_type property can be used to query the type. is_plug and is_bulb helpers are added.
* Cleanup tests and improve test coverage
* Make writing tests easier by sharing code for common implementations
* Instead of storing test data inside python files, dump-discover based information is used
* This will simplify adding new tests and remove code duplication
* fixtures are based on https://github.com/plasticrake/tplink-smarthome-simulator
* run black on newfakes
* Add HS300 tests and update SmartStrip API according to earlier changes, still WIP
* run black and avoid wildcard imports
* Black on conftest
* bump minimum required version to 3.5
* Rename fixture_tests to test_fixtures for autocollect
* fix typoed type to _type, black
* run black on several files with -79 to fix hound issues
* Fix broken merge on hue
* Fix tests (hue update, pass context to smartdevice), add is_strip property, disable emeter tests for HS300 until a solution for API is found.
* Fix old tests
* Run black on changed files
* Add real HS220 discovery, thanks to @poiyo
* add is_dimmable and is_variable_color_temp to smartdevice class, simplifies interfacing with homeassistant
* add KL120(US) fixture
* Add a simple query cache
This commit adds a simple query cache to speed up the process for users
requesting lots of different properties from the device, as done by the
cli tool as well as homeassistant.
The logic for caching is very simple:
1. A timestamp for last fetch for each module+command is stored alongside the response.
2. If the issued command starts with `get_` and the TTL has not expired, the cache result is returned.
3. Otherwise the cache for the whole corresponding module gets invalidated, the device will be queried and the result will be stored in the cache.
* add deprecation to tox.ini
* make tests pass again
* remove old tests, add flake8 to tox reqs
* run black against pyhs100 module, add it to precommit hooks, fix flake8 configuration to conform to black standards (https://ljvmiranda921.github.io/notebook/2018/06/21/precommits-using-black-and-flake8/)
* fix syntax
* cleanup conftest
2019-06-16 21:05:00 +00:00
|
|
|
}
|
2018-06-17 17:01:18 +00:00
|
|
|
|
2022-04-05 16:51:36 +00:00
|
|
|
|
|
|
|
NON_COLOR_MODE_FLAGS = {"transition_period", "on_off"}
|
|
|
|
|
2021-09-21 11:23:56 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2017-04-14 12:24:58 +00:00
|
|
|
|
2024-02-04 15:20:08 +00:00
|
|
|
class IotBulb(IotDevice, Bulb):
|
2022-04-05 23:41:08 +00:00
|
|
|
r"""Representation of a TP-Link Smart Bulb.
|
2017-04-14 12:24:58 +00:00
|
|
|
|
2020-06-30 00:29:52 +00:00
|
|
|
To initialize, you have to await :func:`update()` at least once.
|
|
|
|
This will allow accessing the properties using the exposed properties.
|
|
|
|
|
|
|
|
All changes to the device are done using awaitable methods,
|
2023-10-29 22:15:42 +00:00
|
|
|
which will not change the cached values,
|
|
|
|
so you must await :func:`update()` to fetch updates values from the device.
|
2020-06-30 00:29:52 +00:00
|
|
|
|
2023-10-29 22:15:42 +00:00
|
|
|
Errors reported by the device are raised as
|
2024-02-21 15:52:55 +00:00
|
|
|
:class:`KasaException <kasa.exceptions.KasaException>`,
|
2017-04-14 12:24:58 +00:00
|
|
|
and should be handled by the user of the library.
|
2020-06-30 00:29:52 +00:00
|
|
|
|
|
|
|
Examples:
|
|
|
|
>>> import asyncio
|
2024-02-04 15:20:08 +00:00
|
|
|
>>> bulb = IotBulb("127.0.0.1")
|
2020-06-30 00:29:52 +00:00
|
|
|
>>> asyncio.run(bulb.update())
|
|
|
|
>>> print(bulb.alias)
|
2023-04-01 15:10:46 +00:00
|
|
|
Bulb2
|
2020-06-30 00:29:52 +00:00
|
|
|
|
|
|
|
Bulbs, like any other supported devices, can be turned on and off:
|
|
|
|
|
|
|
|
>>> asyncio.run(bulb.turn_off())
|
|
|
|
>>> asyncio.run(bulb.turn_on())
|
|
|
|
>>> asyncio.run(bulb.update())
|
|
|
|
>>> print(bulb.is_on)
|
|
|
|
True
|
|
|
|
|
2022-10-22 22:15:47 +00:00
|
|
|
You can use the ``is_``-prefixed properties to check for supported features:
|
|
|
|
|
2020-06-30 00:29:52 +00:00
|
|
|
>>> bulb.is_dimmable
|
|
|
|
True
|
|
|
|
>>> bulb.is_color
|
|
|
|
True
|
|
|
|
>>> bulb.is_variable_color_temp
|
|
|
|
True
|
|
|
|
|
|
|
|
All known bulbs support changing the brightness:
|
|
|
|
|
|
|
|
>>> bulb.brightness
|
|
|
|
30
|
|
|
|
>>> asyncio.run(bulb.set_brightness(50))
|
|
|
|
>>> asyncio.run(bulb.update())
|
|
|
|
>>> bulb.brightness
|
|
|
|
50
|
|
|
|
|
2023-10-29 22:15:42 +00:00
|
|
|
Bulbs supporting color temperature can be queried for the supported range:
|
2020-06-30 00:29:52 +00:00
|
|
|
|
|
|
|
>>> bulb.valid_temperature_range
|
2021-09-21 11:23:56 +00:00
|
|
|
ColorTempRange(min=2500, max=9000)
|
2020-06-30 00:29:52 +00:00
|
|
|
>>> asyncio.run(bulb.set_color_temp(3000))
|
|
|
|
>>> asyncio.run(bulb.update())
|
|
|
|
>>> bulb.color_temp
|
|
|
|
3000
|
|
|
|
|
|
|
|
Color bulbs can be adjusted by passing hue, saturation and value:
|
|
|
|
|
|
|
|
>>> asyncio.run(bulb.set_hsv(180, 100, 80))
|
|
|
|
>>> asyncio.run(bulb.update())
|
|
|
|
>>> bulb.hsv
|
2021-09-21 11:23:56 +00:00
|
|
|
HSV(hue=180, saturation=100, value=80)
|
2020-06-30 00:29:52 +00:00
|
|
|
|
2023-10-29 22:15:42 +00:00
|
|
|
If you don't want to use the default transitions,
|
|
|
|
you can pass `transition` in milliseconds.
|
|
|
|
All methods changing the state of the device support this parameter:
|
|
|
|
|
|
|
|
* :func:`turn_on`
|
|
|
|
* :func:`turn_off`
|
|
|
|
* :func:`set_hsv`
|
|
|
|
* :func:`set_color_temp`
|
|
|
|
* :func:`set_brightness`
|
|
|
|
|
|
|
|
Light strips (e.g., KL420L5) do not support this feature,
|
|
|
|
but silently ignore the parameter.
|
2020-06-30 00:29:52 +00:00
|
|
|
The following changes the brightness over a period of 10 seconds:
|
|
|
|
|
|
|
|
>>> asyncio.run(bulb.set_brightness(100, transition=10_000))
|
|
|
|
|
2022-10-22 22:15:47 +00:00
|
|
|
Bulb configuration presets can be accessed using the :func:`presets` property:
|
|
|
|
|
|
|
|
>>> bulb.presets
|
2024-02-04 15:20:08 +00:00
|
|
|
[BulbPreset(index=0, brightness=50, hue=0, saturation=0, color_temp=2700, custom=None, id=None, mode=None), BulbPreset(index=1, brightness=100, hue=0, saturation=75, color_temp=0, custom=None, id=None, mode=None), BulbPreset(index=2, brightness=100, hue=120, saturation=75, color_temp=0, custom=None, id=None, mode=None), BulbPreset(index=3, brightness=100, hue=240, saturation=75, color_temp=0, custom=None, id=None, mode=None)]
|
2022-10-22 22:15:47 +00:00
|
|
|
|
2023-10-29 22:15:42 +00:00
|
|
|
To modify an existing preset, pass :class:`~kasa.smartbulb.SmartBulbPreset`
|
|
|
|
instance to :func:`save_preset` method:
|
2022-10-22 22:15:47 +00:00
|
|
|
|
|
|
|
>>> preset = bulb.presets[0]
|
|
|
|
>>> preset.brightness
|
|
|
|
50
|
|
|
|
>>> preset.brightness = 100
|
|
|
|
>>> asyncio.run(bulb.save_preset(preset))
|
|
|
|
>>> bulb.presets[0].brightness
|
|
|
|
100
|
|
|
|
|
2023-10-29 22:15:42 +00:00
|
|
|
""" # noqa: E501
|
API and tests cleanup (#151)
* Add new cli commands: raw_command and dump_discover
- raw_command can be used to execute raw commands with given parameters
* Useful for testing new calls before implementing them properly
- dump_discover can be used to dump the device discovery information (into a file)
* The discovery is extended to request more modules and methods from devices
* smartlife.iot.dimmer get_dimmer_parameters
* smartlife.iot.common.emeter get_realtime
* smartlife.iot.smartbulb.lightingservice get_light_state
* This is used to dump more information for proper tests, and will also allow better discovery in the future
This commit contains also some documentation updates and dropping click_datetime in favor of click's built-in datetime
* Docstring fixes
* Major API cleanup
Properties shall no more change the state of the device, this work in still in progress, the main goal being making the API more user-friendly and to make implementing new features simpler.
The newly deprecated functionality will remain working and will simply warn the user about deprecation.
Previously deprecated 'features' property and 'identify' method are now finally removed.
Deprecate and replace the following property setters:
* state with turn_on() and turn_off()
* hsv with set_hsv()
* color_temp with set_color_temp()
* brightness with set_brightness()
* led with set_led()
* alias with set_alias()
* mac with set_mac()
And getters:
* state with is_on and is_off
The {BULB,PLUG}_STATE_{ON,OFF} is simplified to STATE_ON and STATE_OFF, UNKNOWN state is removed.
These are now deprecated and will be removed in the future.
* is_on and is_off can be used to check for the state
* turn_on() and turn_off() for changing the device state.
Trying to use functionality not supported by the device will cause SmartDeviceExceptions instead of failing silently and/or returning None.
This includes, e.g., trying to set a color temperature on non-supported bulb.
ValueErrors are raised instead of SmartDeviceExceptions where appropriate (e.g. when trying to set an invalid hsv or brightness).
New enum type DeviceType is added to allow detecting device types without resorting to isinstance() calling. SmartDevice class' device_type property can be used to query the type. is_plug and is_bulb helpers are added.
* Cleanup tests and improve test coverage
* Make writing tests easier by sharing code for common implementations
* Instead of storing test data inside python files, dump-discover based information is used
* This will simplify adding new tests and remove code duplication
* fixtures are based on https://github.com/plasticrake/tplink-smarthome-simulator
* run black on newfakes
* Add HS300 tests and update SmartStrip API according to earlier changes, still WIP
* run black and avoid wildcard imports
* Black on conftest
* bump minimum required version to 3.5
* Rename fixture_tests to test_fixtures for autocollect
* fix typoed type to _type, black
* run black on several files with -79 to fix hound issues
* Fix broken merge on hue
* Fix tests (hue update, pass context to smartdevice), add is_strip property, disable emeter tests for HS300 until a solution for API is found.
* Fix old tests
* Run black on changed files
* Add real HS220 discovery, thanks to @poiyo
* add is_dimmable and is_variable_color_temp to smartdevice class, simplifies interfacing with homeassistant
* add KL120(US) fixture
* Add a simple query cache
This commit adds a simple query cache to speed up the process for users
requesting lots of different properties from the device, as done by the
cli tool as well as homeassistant.
The logic for caching is very simple:
1. A timestamp for last fetch for each module+command is stored alongside the response.
2. If the issued command starts with `get_` and the TTL has not expired, the cache result is returned.
3. Otherwise the cache for the whole corresponding module gets invalidated, the device will be queried and the result will be stored in the cache.
* add deprecation to tox.ini
* make tests pass again
* remove old tests, add flake8 to tox reqs
* run black against pyhs100 module, add it to precommit hooks, fix flake8 configuration to conform to black standards (https://ljvmiranda921.github.io/notebook/2018/06/21/precommits-using-black-and-flake8/)
* fix syntax
* cleanup conftest
2019-06-16 21:05:00 +00:00
|
|
|
|
|
|
|
LIGHT_SERVICE = "smartlife.iot.smartbulb.lightingservice"
|
2020-07-19 20:32:17 +00:00
|
|
|
SET_LIGHT_METHOD = "transition_light_state"
|
2021-11-07 01:41:12 +00:00
|
|
|
emeter_type = "smartlife.iot.common.emeter"
|
API and tests cleanup (#151)
* Add new cli commands: raw_command and dump_discover
- raw_command can be used to execute raw commands with given parameters
* Useful for testing new calls before implementing them properly
- dump_discover can be used to dump the device discovery information (into a file)
* The discovery is extended to request more modules and methods from devices
* smartlife.iot.dimmer get_dimmer_parameters
* smartlife.iot.common.emeter get_realtime
* smartlife.iot.smartbulb.lightingservice get_light_state
* This is used to dump more information for proper tests, and will also allow better discovery in the future
This commit contains also some documentation updates and dropping click_datetime in favor of click's built-in datetime
* Docstring fixes
* Major API cleanup
Properties shall no more change the state of the device, this work in still in progress, the main goal being making the API more user-friendly and to make implementing new features simpler.
The newly deprecated functionality will remain working and will simply warn the user about deprecation.
Previously deprecated 'features' property and 'identify' method are now finally removed.
Deprecate and replace the following property setters:
* state with turn_on() and turn_off()
* hsv with set_hsv()
* color_temp with set_color_temp()
* brightness with set_brightness()
* led with set_led()
* alias with set_alias()
* mac with set_mac()
And getters:
* state with is_on and is_off
The {BULB,PLUG}_STATE_{ON,OFF} is simplified to STATE_ON and STATE_OFF, UNKNOWN state is removed.
These are now deprecated and will be removed in the future.
* is_on and is_off can be used to check for the state
* turn_on() and turn_off() for changing the device state.
Trying to use functionality not supported by the device will cause SmartDeviceExceptions instead of failing silently and/or returning None.
This includes, e.g., trying to set a color temperature on non-supported bulb.
ValueErrors are raised instead of SmartDeviceExceptions where appropriate (e.g. when trying to set an invalid hsv or brightness).
New enum type DeviceType is added to allow detecting device types without resorting to isinstance() calling. SmartDevice class' device_type property can be used to query the type. is_plug and is_bulb helpers are added.
* Cleanup tests and improve test coverage
* Make writing tests easier by sharing code for common implementations
* Instead of storing test data inside python files, dump-discover based information is used
* This will simplify adding new tests and remove code duplication
* fixtures are based on https://github.com/plasticrake/tplink-smarthome-simulator
* run black on newfakes
* Add HS300 tests and update SmartStrip API according to earlier changes, still WIP
* run black and avoid wildcard imports
* Black on conftest
* bump minimum required version to 3.5
* Rename fixture_tests to test_fixtures for autocollect
* fix typoed type to _type, black
* run black on several files with -79 to fix hound issues
* Fix broken merge on hue
* Fix tests (hue update, pass context to smartdevice), add is_strip property, disable emeter tests for HS300 until a solution for API is found.
* Fix old tests
* Run black on changed files
* Add real HS220 discovery, thanks to @poiyo
* add is_dimmable and is_variable_color_temp to smartdevice class, simplifies interfacing with homeassistant
* add KL120(US) fixture
* Add a simple query cache
This commit adds a simple query cache to speed up the process for users
requesting lots of different properties from the device, as done by the
cli tool as well as homeassistant.
The logic for caching is very simple:
1. A timestamp for last fetch for each module+command is stored alongside the response.
2. If the issued command starts with `get_` and the TTL has not expired, the cache result is returned.
3. Otherwise the cache for the whole corresponding module gets invalidated, the device will be queried and the result will be stored in the cache.
* add deprecation to tox.ini
* make tests pass again
* remove old tests, add flake8 to tox reqs
* run black against pyhs100 module, add it to precommit hooks, fix flake8 configuration to conform to black standards (https://ljvmiranda921.github.io/notebook/2018/06/21/precommits-using-black-and-flake8/)
* fix syntax
* cleanup conftest
2019-06-16 21:05:00 +00:00
|
|
|
|
2023-09-13 13:46:38 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
host: str,
|
|
|
|
*,
|
2023-12-29 19:17:15 +00:00
|
|
|
config: Optional[DeviceConfig] = None,
|
2024-01-22 15:28:30 +00:00
|
|
|
protocol: Optional[BaseProtocol] = None,
|
2023-09-13 13:46:38 +00:00
|
|
|
) -> None:
|
2023-12-29 19:17:15 +00:00
|
|
|
super().__init__(host=host, config=config, protocol=protocol)
|
API and tests cleanup (#151)
* Add new cli commands: raw_command and dump_discover
- raw_command can be used to execute raw commands with given parameters
* Useful for testing new calls before implementing them properly
- dump_discover can be used to dump the device discovery information (into a file)
* The discovery is extended to request more modules and methods from devices
* smartlife.iot.dimmer get_dimmer_parameters
* smartlife.iot.common.emeter get_realtime
* smartlife.iot.smartbulb.lightingservice get_light_state
* This is used to dump more information for proper tests, and will also allow better discovery in the future
This commit contains also some documentation updates and dropping click_datetime in favor of click's built-in datetime
* Docstring fixes
* Major API cleanup
Properties shall no more change the state of the device, this work in still in progress, the main goal being making the API more user-friendly and to make implementing new features simpler.
The newly deprecated functionality will remain working and will simply warn the user about deprecation.
Previously deprecated 'features' property and 'identify' method are now finally removed.
Deprecate and replace the following property setters:
* state with turn_on() and turn_off()
* hsv with set_hsv()
* color_temp with set_color_temp()
* brightness with set_brightness()
* led with set_led()
* alias with set_alias()
* mac with set_mac()
And getters:
* state with is_on and is_off
The {BULB,PLUG}_STATE_{ON,OFF} is simplified to STATE_ON and STATE_OFF, UNKNOWN state is removed.
These are now deprecated and will be removed in the future.
* is_on and is_off can be used to check for the state
* turn_on() and turn_off() for changing the device state.
Trying to use functionality not supported by the device will cause SmartDeviceExceptions instead of failing silently and/or returning None.
This includes, e.g., trying to set a color temperature on non-supported bulb.
ValueErrors are raised instead of SmartDeviceExceptions where appropriate (e.g. when trying to set an invalid hsv or brightness).
New enum type DeviceType is added to allow detecting device types without resorting to isinstance() calling. SmartDevice class' device_type property can be used to query the type. is_plug and is_bulb helpers are added.
* Cleanup tests and improve test coverage
* Make writing tests easier by sharing code for common implementations
* Instead of storing test data inside python files, dump-discover based information is used
* This will simplify adding new tests and remove code duplication
* fixtures are based on https://github.com/plasticrake/tplink-smarthome-simulator
* run black on newfakes
* Add HS300 tests and update SmartStrip API according to earlier changes, still WIP
* run black and avoid wildcard imports
* Black on conftest
* bump minimum required version to 3.5
* Rename fixture_tests to test_fixtures for autocollect
* fix typoed type to _type, black
* run black on several files with -79 to fix hound issues
* Fix broken merge on hue
* Fix tests (hue update, pass context to smartdevice), add is_strip property, disable emeter tests for HS300 until a solution for API is found.
* Fix old tests
* Run black on changed files
* Add real HS220 discovery, thanks to @poiyo
* add is_dimmable and is_variable_color_temp to smartdevice class, simplifies interfacing with homeassistant
* add KL120(US) fixture
* Add a simple query cache
This commit adds a simple query cache to speed up the process for users
requesting lots of different properties from the device, as done by the
cli tool as well as homeassistant.
The logic for caching is very simple:
1. A timestamp for last fetch for each module+command is stored alongside the response.
2. If the issued command starts with `get_` and the TTL has not expired, the cache result is returned.
3. Otherwise the cache for the whole corresponding module gets invalidated, the device will be queried and the result will be stored in the cache.
* add deprecation to tox.ini
* make tests pass again
* remove old tests, add flake8 to tox reqs
* run black against pyhs100 module, add it to precommit hooks, fix flake8 configuration to conform to black standards (https://ljvmiranda921.github.io/notebook/2018/06/21/precommits-using-black-and-flake8/)
* fix syntax
* cleanup conftest
2019-06-16 21:05:00 +00:00
|
|
|
self._device_type = DeviceType.Bulb
|
2021-11-07 01:41:12 +00:00
|
|
|
self.add_module("schedule", Schedule(self, "smartlife.iot.common.schedule"))
|
|
|
|
self.add_module("usage", Usage(self, "smartlife.iot.common.schedule"))
|
|
|
|
self.add_module("antitheft", Antitheft(self, "smartlife.iot.common.anti_theft"))
|
|
|
|
self.add_module("time", Time(self, "smartlife.iot.common.timesetting"))
|
|
|
|
self.add_module("emeter", Emeter(self, self.emeter_type))
|
|
|
|
self.add_module("countdown", Countdown(self, "countdown"))
|
|
|
|
self.add_module("cloud", Cloud(self, "smartlife.iot.common.cloud"))
|
2017-04-14 12:24:58 +00:00
|
|
|
|
2024-03-06 16:45:08 +00:00
|
|
|
async def _initialize_features(self):
|
|
|
|
await super()._initialize_features()
|
|
|
|
|
|
|
|
if bool(self.sys_info["is_dimmable"]): # pragma: no branch
|
|
|
|
self._add_feature(
|
|
|
|
Feature(
|
|
|
|
device=self,
|
|
|
|
name="Brightness",
|
|
|
|
attribute_getter="brightness",
|
|
|
|
attribute_setter="set_brightness",
|
|
|
|
minimum_value=1,
|
|
|
|
maximum_value=100,
|
|
|
|
type=FeatureType.Number,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2024-03-26 18:33:10 +00:00
|
|
|
if self.is_variable_color_temp:
|
|
|
|
self._add_feature(
|
|
|
|
Feature(
|
|
|
|
device=self,
|
|
|
|
name="Color temperature",
|
|
|
|
container=self,
|
|
|
|
attribute_getter="color_temp",
|
|
|
|
attribute_setter="set_color_temp",
|
|
|
|
range_getter="valid_temperature_range",
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2019-12-12 09:46:40 +00:00
|
|
|
@property # type: ignore
|
2019-11-15 16:48:36 +00:00
|
|
|
@requires_update
|
|
|
|
def is_color(self) -> bool:
|
2020-05-27 14:55:18 +00:00
|
|
|
"""Whether the bulb supports color changes."""
|
2019-11-15 16:48:36 +00:00
|
|
|
sys_info = self.sys_info
|
2019-11-11 16:32:44 +00:00
|
|
|
return bool(sys_info["is_color"])
|
2017-04-14 12:24:58 +00:00
|
|
|
|
2019-12-12 09:46:40 +00:00
|
|
|
@property # type: ignore
|
2019-11-15 16:48:36 +00:00
|
|
|
@requires_update
|
|
|
|
def is_dimmable(self) -> bool:
|
2020-05-27 14:55:18 +00:00
|
|
|
"""Whether the bulb supports brightness changes."""
|
2019-11-15 16:48:36 +00:00
|
|
|
sys_info = self.sys_info
|
2019-11-11 16:32:44 +00:00
|
|
|
return bool(sys_info["is_dimmable"])
|
2017-04-14 12:24:58 +00:00
|
|
|
|
2019-12-12 09:46:40 +00:00
|
|
|
@property # type: ignore
|
2019-11-15 16:48:36 +00:00
|
|
|
@requires_update
|
|
|
|
def is_variable_color_temp(self) -> bool:
|
2020-05-27 14:55:18 +00:00
|
|
|
"""Whether the bulb supports color temperature changes."""
|
2019-11-15 16:48:36 +00:00
|
|
|
sys_info = self.sys_info
|
2019-11-11 16:32:44 +00:00
|
|
|
return bool(sys_info["is_variable_color_temp"])
|
2017-04-14 12:24:58 +00:00
|
|
|
|
2019-12-12 09:46:40 +00:00
|
|
|
@property # type: ignore
|
2019-11-15 16:48:36 +00:00
|
|
|
@requires_update
|
2021-09-21 11:23:56 +00:00
|
|
|
def valid_temperature_range(self) -> ColorTempRange:
|
API and tests cleanup (#151)
* Add new cli commands: raw_command and dump_discover
- raw_command can be used to execute raw commands with given parameters
* Useful for testing new calls before implementing them properly
- dump_discover can be used to dump the device discovery information (into a file)
* The discovery is extended to request more modules and methods from devices
* smartlife.iot.dimmer get_dimmer_parameters
* smartlife.iot.common.emeter get_realtime
* smartlife.iot.smartbulb.lightingservice get_light_state
* This is used to dump more information for proper tests, and will also allow better discovery in the future
This commit contains also some documentation updates and dropping click_datetime in favor of click's built-in datetime
* Docstring fixes
* Major API cleanup
Properties shall no more change the state of the device, this work in still in progress, the main goal being making the API more user-friendly and to make implementing new features simpler.
The newly deprecated functionality will remain working and will simply warn the user about deprecation.
Previously deprecated 'features' property and 'identify' method are now finally removed.
Deprecate and replace the following property setters:
* state with turn_on() and turn_off()
* hsv with set_hsv()
* color_temp with set_color_temp()
* brightness with set_brightness()
* led with set_led()
* alias with set_alias()
* mac with set_mac()
And getters:
* state with is_on and is_off
The {BULB,PLUG}_STATE_{ON,OFF} is simplified to STATE_ON and STATE_OFF, UNKNOWN state is removed.
These are now deprecated and will be removed in the future.
* is_on and is_off can be used to check for the state
* turn_on() and turn_off() for changing the device state.
Trying to use functionality not supported by the device will cause SmartDeviceExceptions instead of failing silently and/or returning None.
This includes, e.g., trying to set a color temperature on non-supported bulb.
ValueErrors are raised instead of SmartDeviceExceptions where appropriate (e.g. when trying to set an invalid hsv or brightness).
New enum type DeviceType is added to allow detecting device types without resorting to isinstance() calling. SmartDevice class' device_type property can be used to query the type. is_plug and is_bulb helpers are added.
* Cleanup tests and improve test coverage
* Make writing tests easier by sharing code for common implementations
* Instead of storing test data inside python files, dump-discover based information is used
* This will simplify adding new tests and remove code duplication
* fixtures are based on https://github.com/plasticrake/tplink-smarthome-simulator
* run black on newfakes
* Add HS300 tests and update SmartStrip API according to earlier changes, still WIP
* run black and avoid wildcard imports
* Black on conftest
* bump minimum required version to 3.5
* Rename fixture_tests to test_fixtures for autocollect
* fix typoed type to _type, black
* run black on several files with -79 to fix hound issues
* Fix broken merge on hue
* Fix tests (hue update, pass context to smartdevice), add is_strip property, disable emeter tests for HS300 until a solution for API is found.
* Fix old tests
* Run black on changed files
* Add real HS220 discovery, thanks to @poiyo
* add is_dimmable and is_variable_color_temp to smartdevice class, simplifies interfacing with homeassistant
* add KL120(US) fixture
* Add a simple query cache
This commit adds a simple query cache to speed up the process for users
requesting lots of different properties from the device, as done by the
cli tool as well as homeassistant.
The logic for caching is very simple:
1. A timestamp for last fetch for each module+command is stored alongside the response.
2. If the issued command starts with `get_` and the TTL has not expired, the cache result is returned.
3. Otherwise the cache for the whole corresponding module gets invalidated, the device will be queried and the result will be stored in the cache.
* add deprecation to tox.ini
* make tests pass again
* remove old tests, add flake8 to tox reqs
* run black against pyhs100 module, add it to precommit hooks, fix flake8 configuration to conform to black standards (https://ljvmiranda921.github.io/notebook/2018/06/21/precommits-using-black-and-flake8/)
* fix syntax
* cleanup conftest
2019-06-16 21:05:00 +00:00
|
|
|
"""Return the device-specific white temperature range (in Kelvin).
|
2018-06-17 17:01:18 +00:00
|
|
|
|
2020-05-27 14:55:18 +00:00
|
|
|
:return: White temperature range in Kelvin (minimum, maximum)
|
2018-06-17 17:01:18 +00:00
|
|
|
"""
|
2019-11-15 16:48:36 +00:00
|
|
|
if not self.is_variable_color_temp:
|
2024-02-21 15:52:55 +00:00
|
|
|
raise KasaException("Color temperature not supported")
|
2021-09-21 11:23:56 +00:00
|
|
|
|
2018-06-17 17:01:18 +00:00
|
|
|
for model, temp_range in TPLINK_KELVIN.items():
|
2019-11-15 16:48:36 +00:00
|
|
|
sys_info = self.sys_info
|
2019-11-11 16:32:44 +00:00
|
|
|
if re.match(model, sys_info["model"]):
|
2018-06-17 17:01:18 +00:00
|
|
|
return temp_range
|
2020-05-27 14:55:18 +00:00
|
|
|
|
2021-09-21 11:23:56 +00:00
|
|
|
_LOGGER.warning("Unknown color temperature range, fallback to 2700-5000")
|
|
|
|
return ColorTempRange(2700, 5000)
|
2018-06-17 17:01:18 +00:00
|
|
|
|
2019-12-12 09:46:40 +00:00
|
|
|
@property # type: ignore
|
2019-11-15 16:48:36 +00:00
|
|
|
@requires_update
|
2020-05-24 15:57:54 +00:00
|
|
|
def light_state(self) -> Dict[str, str]:
|
2019-11-15 16:48:36 +00:00
|
|
|
"""Query the light state."""
|
2022-10-22 22:15:47 +00:00
|
|
|
light_state = self.sys_info["light_state"]
|
2020-05-24 15:57:54 +00:00
|
|
|
if light_state is None:
|
2024-02-21 15:52:55 +00:00
|
|
|
raise KasaException(
|
2020-05-24 15:57:54 +00:00
|
|
|
"The device has no light_state or you have not called update()"
|
|
|
|
)
|
|
|
|
|
|
|
|
# if the bulb is off, its state is stored under a different key
|
|
|
|
# as is_on property depends on on_off itself, we check it here manually
|
|
|
|
is_on = light_state["on_off"]
|
|
|
|
if not is_on:
|
|
|
|
off_state = {**light_state["dft_on_state"], "on_off": is_on}
|
|
|
|
return cast(dict, off_state)
|
|
|
|
|
|
|
|
return light_state
|
2019-11-15 16:48:36 +00:00
|
|
|
|
2022-03-21 21:10:12 +00:00
|
|
|
@property # type: ignore
|
|
|
|
@requires_update
|
|
|
|
def has_effects(self) -> bool:
|
|
|
|
"""Return True if the device supports effects."""
|
|
|
|
return "lighting_effect_state" in self.sys_info
|
|
|
|
|
2020-07-06 14:09:30 +00:00
|
|
|
async def get_light_details(self) -> Dict[str, int]:
|
|
|
|
"""Return light details.
|
|
|
|
|
2023-02-18 20:17:19 +00:00
|
|
|
Example::
|
|
|
|
|
2020-07-06 14:09:30 +00:00
|
|
|
{'lamp_beam_angle': 290, 'min_voltage': 220, 'max_voltage': 240,
|
|
|
|
'wattage': 5, 'incandescent_equivalent': 40, 'max_lumens': 450,
|
|
|
|
'color_rendering_index': 80}
|
|
|
|
"""
|
|
|
|
return await self._query_helper(self.LIGHT_SERVICE, "get_light_details")
|
|
|
|
|
2022-10-27 15:40:54 +00:00
|
|
|
async def get_turn_on_behavior(self) -> TurnOnBehaviors:
|
|
|
|
"""Return the behavior for turning the bulb on."""
|
|
|
|
return TurnOnBehaviors.parse_obj(
|
|
|
|
await self._query_helper(self.LIGHT_SERVICE, "get_default_behavior")
|
|
|
|
)
|
2020-07-06 14:09:30 +00:00
|
|
|
|
2022-10-27 15:40:54 +00:00
|
|
|
async def set_turn_on_behavior(self, behavior: TurnOnBehaviors):
|
|
|
|
"""Set the behavior for turning the bulb on.
|
|
|
|
|
|
|
|
If you do not want to manually construct the behavior object,
|
|
|
|
you should use :func:`get_turn_on_behavior` to get the current settings.
|
2020-07-06 14:09:30 +00:00
|
|
|
"""
|
2022-10-27 15:40:54 +00:00
|
|
|
return await self._query_helper(
|
|
|
|
self.LIGHT_SERVICE, "set_default_behavior", behavior.dict(by_alias=True)
|
|
|
|
)
|
2020-07-06 14:09:30 +00:00
|
|
|
|
2019-11-15 16:48:36 +00:00
|
|
|
async def get_light_state(self) -> Dict[str, Dict]:
|
API and tests cleanup (#151)
* Add new cli commands: raw_command and dump_discover
- raw_command can be used to execute raw commands with given parameters
* Useful for testing new calls before implementing them properly
- dump_discover can be used to dump the device discovery information (into a file)
* The discovery is extended to request more modules and methods from devices
* smartlife.iot.dimmer get_dimmer_parameters
* smartlife.iot.common.emeter get_realtime
* smartlife.iot.smartbulb.lightingservice get_light_state
* This is used to dump more information for proper tests, and will also allow better discovery in the future
This commit contains also some documentation updates and dropping click_datetime in favor of click's built-in datetime
* Docstring fixes
* Major API cleanup
Properties shall no more change the state of the device, this work in still in progress, the main goal being making the API more user-friendly and to make implementing new features simpler.
The newly deprecated functionality will remain working and will simply warn the user about deprecation.
Previously deprecated 'features' property and 'identify' method are now finally removed.
Deprecate and replace the following property setters:
* state with turn_on() and turn_off()
* hsv with set_hsv()
* color_temp with set_color_temp()
* brightness with set_brightness()
* led with set_led()
* alias with set_alias()
* mac with set_mac()
And getters:
* state with is_on and is_off
The {BULB,PLUG}_STATE_{ON,OFF} is simplified to STATE_ON and STATE_OFF, UNKNOWN state is removed.
These are now deprecated and will be removed in the future.
* is_on and is_off can be used to check for the state
* turn_on() and turn_off() for changing the device state.
Trying to use functionality not supported by the device will cause SmartDeviceExceptions instead of failing silently and/or returning None.
This includes, e.g., trying to set a color temperature on non-supported bulb.
ValueErrors are raised instead of SmartDeviceExceptions where appropriate (e.g. when trying to set an invalid hsv or brightness).
New enum type DeviceType is added to allow detecting device types without resorting to isinstance() calling. SmartDevice class' device_type property can be used to query the type. is_plug and is_bulb helpers are added.
* Cleanup tests and improve test coverage
* Make writing tests easier by sharing code for common implementations
* Instead of storing test data inside python files, dump-discover based information is used
* This will simplify adding new tests and remove code duplication
* fixtures are based on https://github.com/plasticrake/tplink-smarthome-simulator
* run black on newfakes
* Add HS300 tests and update SmartStrip API according to earlier changes, still WIP
* run black and avoid wildcard imports
* Black on conftest
* bump minimum required version to 3.5
* Rename fixture_tests to test_fixtures for autocollect
* fix typoed type to _type, black
* run black on several files with -79 to fix hound issues
* Fix broken merge on hue
* Fix tests (hue update, pass context to smartdevice), add is_strip property, disable emeter tests for HS300 until a solution for API is found.
* Fix old tests
* Run black on changed files
* Add real HS220 discovery, thanks to @poiyo
* add is_dimmable and is_variable_color_temp to smartdevice class, simplifies interfacing with homeassistant
* add KL120(US) fixture
* Add a simple query cache
This commit adds a simple query cache to speed up the process for users
requesting lots of different properties from the device, as done by the
cli tool as well as homeassistant.
The logic for caching is very simple:
1. A timestamp for last fetch for each module+command is stored alongside the response.
2. If the issued command starts with `get_` and the TTL has not expired, the cache result is returned.
3. Otherwise the cache for the whole corresponding module gets invalidated, the device will be queried and the result will be stored in the cache.
* add deprecation to tox.ini
* make tests pass again
* remove old tests, add flake8 to tox reqs
* run black against pyhs100 module, add it to precommit hooks, fix flake8 configuration to conform to black standards (https://ljvmiranda921.github.io/notebook/2018/06/21/precommits-using-black-and-flake8/)
* fix syntax
* cleanup conftest
2019-06-16 21:05:00 +00:00
|
|
|
"""Query the light state."""
|
2020-05-24 15:57:54 +00:00
|
|
|
# TODO: add warning and refer to use light.state?
|
2019-11-11 16:32:44 +00:00
|
|
|
return await self._query_helper(self.LIGHT_SERVICE, "get_light_state")
|
2017-04-14 12:24:58 +00:00
|
|
|
|
2022-11-15 18:05:08 +00:00
|
|
|
async def set_light_state(
|
|
|
|
self, state: Dict, *, transition: Optional[int] = None
|
|
|
|
) -> Dict:
|
API and tests cleanup (#151)
* Add new cli commands: raw_command and dump_discover
- raw_command can be used to execute raw commands with given parameters
* Useful for testing new calls before implementing them properly
- dump_discover can be used to dump the device discovery information (into a file)
* The discovery is extended to request more modules and methods from devices
* smartlife.iot.dimmer get_dimmer_parameters
* smartlife.iot.common.emeter get_realtime
* smartlife.iot.smartbulb.lightingservice get_light_state
* This is used to dump more information for proper tests, and will also allow better discovery in the future
This commit contains also some documentation updates and dropping click_datetime in favor of click's built-in datetime
* Docstring fixes
* Major API cleanup
Properties shall no more change the state of the device, this work in still in progress, the main goal being making the API more user-friendly and to make implementing new features simpler.
The newly deprecated functionality will remain working and will simply warn the user about deprecation.
Previously deprecated 'features' property and 'identify' method are now finally removed.
Deprecate and replace the following property setters:
* state with turn_on() and turn_off()
* hsv with set_hsv()
* color_temp with set_color_temp()
* brightness with set_brightness()
* led with set_led()
* alias with set_alias()
* mac with set_mac()
And getters:
* state with is_on and is_off
The {BULB,PLUG}_STATE_{ON,OFF} is simplified to STATE_ON and STATE_OFF, UNKNOWN state is removed.
These are now deprecated and will be removed in the future.
* is_on and is_off can be used to check for the state
* turn_on() and turn_off() for changing the device state.
Trying to use functionality not supported by the device will cause SmartDeviceExceptions instead of failing silently and/or returning None.
This includes, e.g., trying to set a color temperature on non-supported bulb.
ValueErrors are raised instead of SmartDeviceExceptions where appropriate (e.g. when trying to set an invalid hsv or brightness).
New enum type DeviceType is added to allow detecting device types without resorting to isinstance() calling. SmartDevice class' device_type property can be used to query the type. is_plug and is_bulb helpers are added.
* Cleanup tests and improve test coverage
* Make writing tests easier by sharing code for common implementations
* Instead of storing test data inside python files, dump-discover based information is used
* This will simplify adding new tests and remove code duplication
* fixtures are based on https://github.com/plasticrake/tplink-smarthome-simulator
* run black on newfakes
* Add HS300 tests and update SmartStrip API according to earlier changes, still WIP
* run black and avoid wildcard imports
* Black on conftest
* bump minimum required version to 3.5
* Rename fixture_tests to test_fixtures for autocollect
* fix typoed type to _type, black
* run black on several files with -79 to fix hound issues
* Fix broken merge on hue
* Fix tests (hue update, pass context to smartdevice), add is_strip property, disable emeter tests for HS300 until a solution for API is found.
* Fix old tests
* Run black on changed files
* Add real HS220 discovery, thanks to @poiyo
* add is_dimmable and is_variable_color_temp to smartdevice class, simplifies interfacing with homeassistant
* add KL120(US) fixture
* Add a simple query cache
This commit adds a simple query cache to speed up the process for users
requesting lots of different properties from the device, as done by the
cli tool as well as homeassistant.
The logic for caching is very simple:
1. A timestamp for last fetch for each module+command is stored alongside the response.
2. If the issued command starts with `get_` and the TTL has not expired, the cache result is returned.
3. Otherwise the cache for the whole corresponding module gets invalidated, the device will be queried and the result will be stored in the cache.
* add deprecation to tox.ini
* make tests pass again
* remove old tests, add flake8 to tox reqs
* run black against pyhs100 module, add it to precommit hooks, fix flake8 configuration to conform to black standards (https://ljvmiranda921.github.io/notebook/2018/06/21/precommits-using-black-and-flake8/)
* fix syntax
* cleanup conftest
2019-06-16 21:05:00 +00:00
|
|
|
"""Set the light state."""
|
2020-06-14 18:21:55 +00:00
|
|
|
if transition is not None:
|
|
|
|
state["transition_period"] = transition
|
|
|
|
|
2020-07-06 14:09:30 +00:00
|
|
|
# if no on/off is defined, turn on the light
|
|
|
|
if "on_off" not in state:
|
|
|
|
state["on_off"] = 1
|
|
|
|
|
2022-04-05 16:51:36 +00:00
|
|
|
# If we are turning on without any color mode flags,
|
|
|
|
# we do not want to set ignore_default to ensure
|
|
|
|
# we restore the previous state.
|
|
|
|
if state["on_off"] and NON_COLOR_MODE_FLAGS.issuperset(state):
|
|
|
|
state["ignore_default"] = 0
|
|
|
|
else:
|
|
|
|
# This is necessary to allow turning on into a specific state
|
|
|
|
state["ignore_default"] = 1
|
2020-07-06 14:09:30 +00:00
|
|
|
|
2019-11-15 16:48:36 +00:00
|
|
|
light_state = await self._query_helper(
|
2020-07-19 20:32:17 +00:00
|
|
|
self.LIGHT_SERVICE, self.SET_LIGHT_METHOD, state
|
2019-11-11 18:16:55 +00:00
|
|
|
)
|
2019-11-15 16:48:36 +00:00
|
|
|
return light_state
|
2017-04-14 12:24:58 +00:00
|
|
|
|
2019-12-12 09:46:40 +00:00
|
|
|
@property # type: ignore
|
2019-11-15 16:48:36 +00:00
|
|
|
@requires_update
|
2021-09-21 11:23:56 +00:00
|
|
|
def hsv(self) -> HSV:
|
API and tests cleanup (#151)
* Add new cli commands: raw_command and dump_discover
- raw_command can be used to execute raw commands with given parameters
* Useful for testing new calls before implementing them properly
- dump_discover can be used to dump the device discovery information (into a file)
* The discovery is extended to request more modules and methods from devices
* smartlife.iot.dimmer get_dimmer_parameters
* smartlife.iot.common.emeter get_realtime
* smartlife.iot.smartbulb.lightingservice get_light_state
* This is used to dump more information for proper tests, and will also allow better discovery in the future
This commit contains also some documentation updates and dropping click_datetime in favor of click's built-in datetime
* Docstring fixes
* Major API cleanup
Properties shall no more change the state of the device, this work in still in progress, the main goal being making the API more user-friendly and to make implementing new features simpler.
The newly deprecated functionality will remain working and will simply warn the user about deprecation.
Previously deprecated 'features' property and 'identify' method are now finally removed.
Deprecate and replace the following property setters:
* state with turn_on() and turn_off()
* hsv with set_hsv()
* color_temp with set_color_temp()
* brightness with set_brightness()
* led with set_led()
* alias with set_alias()
* mac with set_mac()
And getters:
* state with is_on and is_off
The {BULB,PLUG}_STATE_{ON,OFF} is simplified to STATE_ON and STATE_OFF, UNKNOWN state is removed.
These are now deprecated and will be removed in the future.
* is_on and is_off can be used to check for the state
* turn_on() and turn_off() for changing the device state.
Trying to use functionality not supported by the device will cause SmartDeviceExceptions instead of failing silently and/or returning None.
This includes, e.g., trying to set a color temperature on non-supported bulb.
ValueErrors are raised instead of SmartDeviceExceptions where appropriate (e.g. when trying to set an invalid hsv or brightness).
New enum type DeviceType is added to allow detecting device types without resorting to isinstance() calling. SmartDevice class' device_type property can be used to query the type. is_plug and is_bulb helpers are added.
* Cleanup tests and improve test coverage
* Make writing tests easier by sharing code for common implementations
* Instead of storing test data inside python files, dump-discover based information is used
* This will simplify adding new tests and remove code duplication
* fixtures are based on https://github.com/plasticrake/tplink-smarthome-simulator
* run black on newfakes
* Add HS300 tests and update SmartStrip API according to earlier changes, still WIP
* run black and avoid wildcard imports
* Black on conftest
* bump minimum required version to 3.5
* Rename fixture_tests to test_fixtures for autocollect
* fix typoed type to _type, black
* run black on several files with -79 to fix hound issues
* Fix broken merge on hue
* Fix tests (hue update, pass context to smartdevice), add is_strip property, disable emeter tests for HS300 until a solution for API is found.
* Fix old tests
* Run black on changed files
* Add real HS220 discovery, thanks to @poiyo
* add is_dimmable and is_variable_color_temp to smartdevice class, simplifies interfacing with homeassistant
* add KL120(US) fixture
* Add a simple query cache
This commit adds a simple query cache to speed up the process for users
requesting lots of different properties from the device, as done by the
cli tool as well as homeassistant.
The logic for caching is very simple:
1. A timestamp for last fetch for each module+command is stored alongside the response.
2. If the issued command starts with `get_` and the TTL has not expired, the cache result is returned.
3. Otherwise the cache for the whole corresponding module gets invalidated, the device will be queried and the result will be stored in the cache.
* add deprecation to tox.ini
* make tests pass again
* remove old tests, add flake8 to tox reqs
* run black against pyhs100 module, add it to precommit hooks, fix flake8 configuration to conform to black standards (https://ljvmiranda921.github.io/notebook/2018/06/21/precommits-using-black-and-flake8/)
* fix syntax
* cleanup conftest
2019-06-16 21:05:00 +00:00
|
|
|
"""Return the current HSV state of the bulb.
|
2017-04-14 12:24:58 +00:00
|
|
|
|
2017-09-16 16:57:42 +00:00
|
|
|
:return: hue, saturation and value (degrees, %, %)
|
2017-04-14 12:24:58 +00:00
|
|
|
"""
|
2019-11-15 16:48:36 +00:00
|
|
|
if not self.is_color:
|
2024-02-21 15:52:55 +00:00
|
|
|
raise KasaException("Bulb does not support color.")
|
2017-04-14 12:24:58 +00:00
|
|
|
|
2020-05-24 15:57:54 +00:00
|
|
|
light_state = cast(dict, self.light_state)
|
|
|
|
|
|
|
|
hue = light_state["hue"]
|
|
|
|
saturation = light_state["saturation"]
|
|
|
|
value = light_state["brightness"]
|
2017-04-14 12:24:58 +00:00
|
|
|
|
2021-09-21 11:23:56 +00:00
|
|
|
return HSV(hue, saturation, value)
|
2017-04-14 12:24:58 +00:00
|
|
|
|
2019-11-15 16:48:36 +00:00
|
|
|
@requires_update
|
2020-06-14 18:21:55 +00:00
|
|
|
async def set_hsv(
|
2022-11-15 18:05:08 +00:00
|
|
|
self,
|
|
|
|
hue: int,
|
|
|
|
saturation: int,
|
|
|
|
value: Optional[int] = None,
|
|
|
|
*,
|
2023-10-07 18:58:00 +00:00
|
|
|
transition: Optional[int] = None,
|
2020-06-14 18:21:55 +00:00
|
|
|
) -> Dict:
|
API and tests cleanup (#151)
* Add new cli commands: raw_command and dump_discover
- raw_command can be used to execute raw commands with given parameters
* Useful for testing new calls before implementing them properly
- dump_discover can be used to dump the device discovery information (into a file)
* The discovery is extended to request more modules and methods from devices
* smartlife.iot.dimmer get_dimmer_parameters
* smartlife.iot.common.emeter get_realtime
* smartlife.iot.smartbulb.lightingservice get_light_state
* This is used to dump more information for proper tests, and will also allow better discovery in the future
This commit contains also some documentation updates and dropping click_datetime in favor of click's built-in datetime
* Docstring fixes
* Major API cleanup
Properties shall no more change the state of the device, this work in still in progress, the main goal being making the API more user-friendly and to make implementing new features simpler.
The newly deprecated functionality will remain working and will simply warn the user about deprecation.
Previously deprecated 'features' property and 'identify' method are now finally removed.
Deprecate and replace the following property setters:
* state with turn_on() and turn_off()
* hsv with set_hsv()
* color_temp with set_color_temp()
* brightness with set_brightness()
* led with set_led()
* alias with set_alias()
* mac with set_mac()
And getters:
* state with is_on and is_off
The {BULB,PLUG}_STATE_{ON,OFF} is simplified to STATE_ON and STATE_OFF, UNKNOWN state is removed.
These are now deprecated and will be removed in the future.
* is_on and is_off can be used to check for the state
* turn_on() and turn_off() for changing the device state.
Trying to use functionality not supported by the device will cause SmartDeviceExceptions instead of failing silently and/or returning None.
This includes, e.g., trying to set a color temperature on non-supported bulb.
ValueErrors are raised instead of SmartDeviceExceptions where appropriate (e.g. when trying to set an invalid hsv or brightness).
New enum type DeviceType is added to allow detecting device types without resorting to isinstance() calling. SmartDevice class' device_type property can be used to query the type. is_plug and is_bulb helpers are added.
* Cleanup tests and improve test coverage
* Make writing tests easier by sharing code for common implementations
* Instead of storing test data inside python files, dump-discover based information is used
* This will simplify adding new tests and remove code duplication
* fixtures are based on https://github.com/plasticrake/tplink-smarthome-simulator
* run black on newfakes
* Add HS300 tests and update SmartStrip API according to earlier changes, still WIP
* run black and avoid wildcard imports
* Black on conftest
* bump minimum required version to 3.5
* Rename fixture_tests to test_fixtures for autocollect
* fix typoed type to _type, black
* run black on several files with -79 to fix hound issues
* Fix broken merge on hue
* Fix tests (hue update, pass context to smartdevice), add is_strip property, disable emeter tests for HS300 until a solution for API is found.
* Fix old tests
* Run black on changed files
* Add real HS220 discovery, thanks to @poiyo
* add is_dimmable and is_variable_color_temp to smartdevice class, simplifies interfacing with homeassistant
* add KL120(US) fixture
* Add a simple query cache
This commit adds a simple query cache to speed up the process for users
requesting lots of different properties from the device, as done by the
cli tool as well as homeassistant.
The logic for caching is very simple:
1. A timestamp for last fetch for each module+command is stored alongside the response.
2. If the issued command starts with `get_` and the TTL has not expired, the cache result is returned.
3. Otherwise the cache for the whole corresponding module gets invalidated, the device will be queried and the result will be stored in the cache.
* add deprecation to tox.ini
* make tests pass again
* remove old tests, add flake8 to tox reqs
* run black against pyhs100 module, add it to precommit hooks, fix flake8 configuration to conform to black standards (https://ljvmiranda921.github.io/notebook/2018/06/21/precommits-using-black-and-flake8/)
* fix syntax
* cleanup conftest
2019-06-16 21:05:00 +00:00
|
|
|
"""Set new HSV.
|
2017-04-14 12:24:58 +00:00
|
|
|
|
2019-11-11 21:14:34 +00:00
|
|
|
:param int hue: hue in degrees
|
|
|
|
:param int saturation: saturation in percentage [0,100]
|
|
|
|
:param int value: value in percentage [0, 100]
|
2020-06-14 18:21:55 +00:00
|
|
|
:param int transition: transition in milliseconds.
|
2017-04-14 12:24:58 +00:00
|
|
|
"""
|
2019-11-15 16:48:36 +00:00
|
|
|
if not self.is_color:
|
2024-02-21 15:52:55 +00:00
|
|
|
raise KasaException("Bulb does not support color.")
|
2017-04-14 12:24:58 +00:00
|
|
|
|
API and tests cleanup (#151)
* Add new cli commands: raw_command and dump_discover
- raw_command can be used to execute raw commands with given parameters
* Useful for testing new calls before implementing them properly
- dump_discover can be used to dump the device discovery information (into a file)
* The discovery is extended to request more modules and methods from devices
* smartlife.iot.dimmer get_dimmer_parameters
* smartlife.iot.common.emeter get_realtime
* smartlife.iot.smartbulb.lightingservice get_light_state
* This is used to dump more information for proper tests, and will also allow better discovery in the future
This commit contains also some documentation updates and dropping click_datetime in favor of click's built-in datetime
* Docstring fixes
* Major API cleanup
Properties shall no more change the state of the device, this work in still in progress, the main goal being making the API more user-friendly and to make implementing new features simpler.
The newly deprecated functionality will remain working and will simply warn the user about deprecation.
Previously deprecated 'features' property and 'identify' method are now finally removed.
Deprecate and replace the following property setters:
* state with turn_on() and turn_off()
* hsv with set_hsv()
* color_temp with set_color_temp()
* brightness with set_brightness()
* led with set_led()
* alias with set_alias()
* mac with set_mac()
And getters:
* state with is_on and is_off
The {BULB,PLUG}_STATE_{ON,OFF} is simplified to STATE_ON and STATE_OFF, UNKNOWN state is removed.
These are now deprecated and will be removed in the future.
* is_on and is_off can be used to check for the state
* turn_on() and turn_off() for changing the device state.
Trying to use functionality not supported by the device will cause SmartDeviceExceptions instead of failing silently and/or returning None.
This includes, e.g., trying to set a color temperature on non-supported bulb.
ValueErrors are raised instead of SmartDeviceExceptions where appropriate (e.g. when trying to set an invalid hsv or brightness).
New enum type DeviceType is added to allow detecting device types without resorting to isinstance() calling. SmartDevice class' device_type property can be used to query the type. is_plug and is_bulb helpers are added.
* Cleanup tests and improve test coverage
* Make writing tests easier by sharing code for common implementations
* Instead of storing test data inside python files, dump-discover based information is used
* This will simplify adding new tests and remove code duplication
* fixtures are based on https://github.com/plasticrake/tplink-smarthome-simulator
* run black on newfakes
* Add HS300 tests and update SmartStrip API according to earlier changes, still WIP
* run black and avoid wildcard imports
* Black on conftest
* bump minimum required version to 3.5
* Rename fixture_tests to test_fixtures for autocollect
* fix typoed type to _type, black
* run black on several files with -79 to fix hound issues
* Fix broken merge on hue
* Fix tests (hue update, pass context to smartdevice), add is_strip property, disable emeter tests for HS300 until a solution for API is found.
* Fix old tests
* Run black on changed files
* Add real HS220 discovery, thanks to @poiyo
* add is_dimmable and is_variable_color_temp to smartdevice class, simplifies interfacing with homeassistant
* add KL120(US) fixture
* Add a simple query cache
This commit adds a simple query cache to speed up the process for users
requesting lots of different properties from the device, as done by the
cli tool as well as homeassistant.
The logic for caching is very simple:
1. A timestamp for last fetch for each module+command is stored alongside the response.
2. If the issued command starts with `get_` and the TTL has not expired, the cache result is returned.
3. Otherwise the cache for the whole corresponding module gets invalidated, the device will be queried and the result will be stored in the cache.
* add deprecation to tox.ini
* make tests pass again
* remove old tests, add flake8 to tox reqs
* run black against pyhs100 module, add it to precommit hooks, fix flake8 configuration to conform to black standards (https://ljvmiranda921.github.io/notebook/2018/06/21/precommits-using-black-and-flake8/)
* fix syntax
* cleanup conftest
2019-06-16 21:05:00 +00:00
|
|
|
if not isinstance(hue, int) or not (0 <= hue <= 360):
|
2023-10-29 22:15:42 +00:00
|
|
|
raise ValueError(f"Invalid hue value: {hue} (valid range: 0-360)")
|
2018-09-05 20:44:44 +00:00
|
|
|
|
API and tests cleanup (#151)
* Add new cli commands: raw_command and dump_discover
- raw_command can be used to execute raw commands with given parameters
* Useful for testing new calls before implementing them properly
- dump_discover can be used to dump the device discovery information (into a file)
* The discovery is extended to request more modules and methods from devices
* smartlife.iot.dimmer get_dimmer_parameters
* smartlife.iot.common.emeter get_realtime
* smartlife.iot.smartbulb.lightingservice get_light_state
* This is used to dump more information for proper tests, and will also allow better discovery in the future
This commit contains also some documentation updates and dropping click_datetime in favor of click's built-in datetime
* Docstring fixes
* Major API cleanup
Properties shall no more change the state of the device, this work in still in progress, the main goal being making the API more user-friendly and to make implementing new features simpler.
The newly deprecated functionality will remain working and will simply warn the user about deprecation.
Previously deprecated 'features' property and 'identify' method are now finally removed.
Deprecate and replace the following property setters:
* state with turn_on() and turn_off()
* hsv with set_hsv()
* color_temp with set_color_temp()
* brightness with set_brightness()
* led with set_led()
* alias with set_alias()
* mac with set_mac()
And getters:
* state with is_on and is_off
The {BULB,PLUG}_STATE_{ON,OFF} is simplified to STATE_ON and STATE_OFF, UNKNOWN state is removed.
These are now deprecated and will be removed in the future.
* is_on and is_off can be used to check for the state
* turn_on() and turn_off() for changing the device state.
Trying to use functionality not supported by the device will cause SmartDeviceExceptions instead of failing silently and/or returning None.
This includes, e.g., trying to set a color temperature on non-supported bulb.
ValueErrors are raised instead of SmartDeviceExceptions where appropriate (e.g. when trying to set an invalid hsv or brightness).
New enum type DeviceType is added to allow detecting device types without resorting to isinstance() calling. SmartDevice class' device_type property can be used to query the type. is_plug and is_bulb helpers are added.
* Cleanup tests and improve test coverage
* Make writing tests easier by sharing code for common implementations
* Instead of storing test data inside python files, dump-discover based information is used
* This will simplify adding new tests and remove code duplication
* fixtures are based on https://github.com/plasticrake/tplink-smarthome-simulator
* run black on newfakes
* Add HS300 tests and update SmartStrip API according to earlier changes, still WIP
* run black and avoid wildcard imports
* Black on conftest
* bump minimum required version to 3.5
* Rename fixture_tests to test_fixtures for autocollect
* fix typoed type to _type, black
* run black on several files with -79 to fix hound issues
* Fix broken merge on hue
* Fix tests (hue update, pass context to smartdevice), add is_strip property, disable emeter tests for HS300 until a solution for API is found.
* Fix old tests
* Run black on changed files
* Add real HS220 discovery, thanks to @poiyo
* add is_dimmable and is_variable_color_temp to smartdevice class, simplifies interfacing with homeassistant
* add KL120(US) fixture
* Add a simple query cache
This commit adds a simple query cache to speed up the process for users
requesting lots of different properties from the device, as done by the
cli tool as well as homeassistant.
The logic for caching is very simple:
1. A timestamp for last fetch for each module+command is stored alongside the response.
2. If the issued command starts with `get_` and the TTL has not expired, the cache result is returned.
3. Otherwise the cache for the whole corresponding module gets invalidated, the device will be queried and the result will be stored in the cache.
* add deprecation to tox.ini
* make tests pass again
* remove old tests, add flake8 to tox reqs
* run black against pyhs100 module, add it to precommit hooks, fix flake8 configuration to conform to black standards (https://ljvmiranda921.github.io/notebook/2018/06/21/precommits-using-black-and-flake8/)
* fix syntax
* cleanup conftest
2019-06-16 21:05:00 +00:00
|
|
|
if not isinstance(saturation, int) or not (0 <= saturation <= 100):
|
|
|
|
raise ValueError(
|
2023-10-29 22:15:42 +00:00
|
|
|
f"Invalid saturation value: {saturation} (valid range: 0-100%)"
|
API and tests cleanup (#151)
* Add new cli commands: raw_command and dump_discover
- raw_command can be used to execute raw commands with given parameters
* Useful for testing new calls before implementing them properly
- dump_discover can be used to dump the device discovery information (into a file)
* The discovery is extended to request more modules and methods from devices
* smartlife.iot.dimmer get_dimmer_parameters
* smartlife.iot.common.emeter get_realtime
* smartlife.iot.smartbulb.lightingservice get_light_state
* This is used to dump more information for proper tests, and will also allow better discovery in the future
This commit contains also some documentation updates and dropping click_datetime in favor of click's built-in datetime
* Docstring fixes
* Major API cleanup
Properties shall no more change the state of the device, this work in still in progress, the main goal being making the API more user-friendly and to make implementing new features simpler.
The newly deprecated functionality will remain working and will simply warn the user about deprecation.
Previously deprecated 'features' property and 'identify' method are now finally removed.
Deprecate and replace the following property setters:
* state with turn_on() and turn_off()
* hsv with set_hsv()
* color_temp with set_color_temp()
* brightness with set_brightness()
* led with set_led()
* alias with set_alias()
* mac with set_mac()
And getters:
* state with is_on and is_off
The {BULB,PLUG}_STATE_{ON,OFF} is simplified to STATE_ON and STATE_OFF, UNKNOWN state is removed.
These are now deprecated and will be removed in the future.
* is_on and is_off can be used to check for the state
* turn_on() and turn_off() for changing the device state.
Trying to use functionality not supported by the device will cause SmartDeviceExceptions instead of failing silently and/or returning None.
This includes, e.g., trying to set a color temperature on non-supported bulb.
ValueErrors are raised instead of SmartDeviceExceptions where appropriate (e.g. when trying to set an invalid hsv or brightness).
New enum type DeviceType is added to allow detecting device types without resorting to isinstance() calling. SmartDevice class' device_type property can be used to query the type. is_plug and is_bulb helpers are added.
* Cleanup tests and improve test coverage
* Make writing tests easier by sharing code for common implementations
* Instead of storing test data inside python files, dump-discover based information is used
* This will simplify adding new tests and remove code duplication
* fixtures are based on https://github.com/plasticrake/tplink-smarthome-simulator
* run black on newfakes
* Add HS300 tests and update SmartStrip API according to earlier changes, still WIP
* run black and avoid wildcard imports
* Black on conftest
* bump minimum required version to 3.5
* Rename fixture_tests to test_fixtures for autocollect
* fix typoed type to _type, black
* run black on several files with -79 to fix hound issues
* Fix broken merge on hue
* Fix tests (hue update, pass context to smartdevice), add is_strip property, disable emeter tests for HS300 until a solution for API is found.
* Fix old tests
* Run black on changed files
* Add real HS220 discovery, thanks to @poiyo
* add is_dimmable and is_variable_color_temp to smartdevice class, simplifies interfacing with homeassistant
* add KL120(US) fixture
* Add a simple query cache
This commit adds a simple query cache to speed up the process for users
requesting lots of different properties from the device, as done by the
cli tool as well as homeassistant.
The logic for caching is very simple:
1. A timestamp for last fetch for each module+command is stored alongside the response.
2. If the issued command starts with `get_` and the TTL has not expired, the cache result is returned.
3. Otherwise the cache for the whole corresponding module gets invalidated, the device will be queried and the result will be stored in the cache.
* add deprecation to tox.ini
* make tests pass again
* remove old tests, add flake8 to tox reqs
* run black against pyhs100 module, add it to precommit hooks, fix flake8 configuration to conform to black standards (https://ljvmiranda921.github.io/notebook/2018/06/21/precommits-using-black-and-flake8/)
* fix syntax
* cleanup conftest
2019-06-16 21:05:00 +00:00
|
|
|
)
|
2018-09-05 20:44:44 +00:00
|
|
|
|
2017-04-14 12:24:58 +00:00
|
|
|
light_state = {
|
API and tests cleanup (#151)
* Add new cli commands: raw_command and dump_discover
- raw_command can be used to execute raw commands with given parameters
* Useful for testing new calls before implementing them properly
- dump_discover can be used to dump the device discovery information (into a file)
* The discovery is extended to request more modules and methods from devices
* smartlife.iot.dimmer get_dimmer_parameters
* smartlife.iot.common.emeter get_realtime
* smartlife.iot.smartbulb.lightingservice get_light_state
* This is used to dump more information for proper tests, and will also allow better discovery in the future
This commit contains also some documentation updates and dropping click_datetime in favor of click's built-in datetime
* Docstring fixes
* Major API cleanup
Properties shall no more change the state of the device, this work in still in progress, the main goal being making the API more user-friendly and to make implementing new features simpler.
The newly deprecated functionality will remain working and will simply warn the user about deprecation.
Previously deprecated 'features' property and 'identify' method are now finally removed.
Deprecate and replace the following property setters:
* state with turn_on() and turn_off()
* hsv with set_hsv()
* color_temp with set_color_temp()
* brightness with set_brightness()
* led with set_led()
* alias with set_alias()
* mac with set_mac()
And getters:
* state with is_on and is_off
The {BULB,PLUG}_STATE_{ON,OFF} is simplified to STATE_ON and STATE_OFF, UNKNOWN state is removed.
These are now deprecated and will be removed in the future.
* is_on and is_off can be used to check for the state
* turn_on() and turn_off() for changing the device state.
Trying to use functionality not supported by the device will cause SmartDeviceExceptions instead of failing silently and/or returning None.
This includes, e.g., trying to set a color temperature on non-supported bulb.
ValueErrors are raised instead of SmartDeviceExceptions where appropriate (e.g. when trying to set an invalid hsv or brightness).
New enum type DeviceType is added to allow detecting device types without resorting to isinstance() calling. SmartDevice class' device_type property can be used to query the type. is_plug and is_bulb helpers are added.
* Cleanup tests and improve test coverage
* Make writing tests easier by sharing code for common implementations
* Instead of storing test data inside python files, dump-discover based information is used
* This will simplify adding new tests and remove code duplication
* fixtures are based on https://github.com/plasticrake/tplink-smarthome-simulator
* run black on newfakes
* Add HS300 tests and update SmartStrip API according to earlier changes, still WIP
* run black and avoid wildcard imports
* Black on conftest
* bump minimum required version to 3.5
* Rename fixture_tests to test_fixtures for autocollect
* fix typoed type to _type, black
* run black on several files with -79 to fix hound issues
* Fix broken merge on hue
* Fix tests (hue update, pass context to smartdevice), add is_strip property, disable emeter tests for HS300 until a solution for API is found.
* Fix old tests
* Run black on changed files
* Add real HS220 discovery, thanks to @poiyo
* add is_dimmable and is_variable_color_temp to smartdevice class, simplifies interfacing with homeassistant
* add KL120(US) fixture
* Add a simple query cache
This commit adds a simple query cache to speed up the process for users
requesting lots of different properties from the device, as done by the
cli tool as well as homeassistant.
The logic for caching is very simple:
1. A timestamp for last fetch for each module+command is stored alongside the response.
2. If the issued command starts with `get_` and the TTL has not expired, the cache result is returned.
3. Otherwise the cache for the whole corresponding module gets invalidated, the device will be queried and the result will be stored in the cache.
* add deprecation to tox.ini
* make tests pass again
* remove old tests, add flake8 to tox reqs
* run black against pyhs100 module, add it to precommit hooks, fix flake8 configuration to conform to black standards (https://ljvmiranda921.github.io/notebook/2018/06/21/precommits-using-black-and-flake8/)
* fix syntax
* cleanup conftest
2019-06-16 21:05:00 +00:00
|
|
|
"hue": hue,
|
|
|
|
"saturation": saturation,
|
|
|
|
"color_temp": 0,
|
|
|
|
}
|
2020-06-14 18:21:55 +00:00
|
|
|
|
2021-09-21 11:23:56 +00:00
|
|
|
if value is not None:
|
|
|
|
self._raise_for_invalid_brightness(value)
|
|
|
|
light_state["brightness"] = value
|
|
|
|
|
2020-06-14 18:21:55 +00:00
|
|
|
return await self.set_light_state(light_state, transition=transition)
|
2017-04-14 12:24:58 +00:00
|
|
|
|
2019-12-12 09:46:40 +00:00
|
|
|
@property # type: ignore
|
2019-11-15 16:48:36 +00:00
|
|
|
@requires_update
|
|
|
|
def color_temp(self) -> int:
|
2020-05-27 14:55:18 +00:00
|
|
|
"""Return color temperature of the device in kelvin."""
|
2019-11-15 16:48:36 +00:00
|
|
|
if not self.is_variable_color_temp:
|
2024-02-21 15:52:55 +00:00
|
|
|
raise KasaException("Bulb does not support colortemp.")
|
2017-04-14 12:24:58 +00:00
|
|
|
|
2019-11-15 16:48:36 +00:00
|
|
|
light_state = self.light_state
|
2020-05-24 15:57:54 +00:00
|
|
|
return int(light_state["color_temp"])
|
2017-04-14 12:24:58 +00:00
|
|
|
|
2019-11-15 16:48:36 +00:00
|
|
|
@requires_update
|
2020-07-06 14:09:30 +00:00
|
|
|
async def set_color_temp(
|
2022-11-15 18:05:08 +00:00
|
|
|
self, temp: int, *, brightness=None, transition: Optional[int] = None
|
2020-07-06 14:09:30 +00:00
|
|
|
) -> Dict:
|
2020-06-14 18:21:55 +00:00
|
|
|
"""Set the color temperature of the device in kelvin.
|
|
|
|
|
|
|
|
:param int temp: The new color temperature, in Kelvin
|
|
|
|
:param int transition: transition in milliseconds.
|
|
|
|
"""
|
2019-11-15 16:48:36 +00:00
|
|
|
if not self.is_variable_color_temp:
|
2024-02-21 15:52:55 +00:00
|
|
|
raise KasaException("Bulb does not support colortemp.")
|
API and tests cleanup (#151)
* Add new cli commands: raw_command and dump_discover
- raw_command can be used to execute raw commands with given parameters
* Useful for testing new calls before implementing them properly
- dump_discover can be used to dump the device discovery information (into a file)
* The discovery is extended to request more modules and methods from devices
* smartlife.iot.dimmer get_dimmer_parameters
* smartlife.iot.common.emeter get_realtime
* smartlife.iot.smartbulb.lightingservice get_light_state
* This is used to dump more information for proper tests, and will also allow better discovery in the future
This commit contains also some documentation updates and dropping click_datetime in favor of click's built-in datetime
* Docstring fixes
* Major API cleanup
Properties shall no more change the state of the device, this work in still in progress, the main goal being making the API more user-friendly and to make implementing new features simpler.
The newly deprecated functionality will remain working and will simply warn the user about deprecation.
Previously deprecated 'features' property and 'identify' method are now finally removed.
Deprecate and replace the following property setters:
* state with turn_on() and turn_off()
* hsv with set_hsv()
* color_temp with set_color_temp()
* brightness with set_brightness()
* led with set_led()
* alias with set_alias()
* mac with set_mac()
And getters:
* state with is_on and is_off
The {BULB,PLUG}_STATE_{ON,OFF} is simplified to STATE_ON and STATE_OFF, UNKNOWN state is removed.
These are now deprecated and will be removed in the future.
* is_on and is_off can be used to check for the state
* turn_on() and turn_off() for changing the device state.
Trying to use functionality not supported by the device will cause SmartDeviceExceptions instead of failing silently and/or returning None.
This includes, e.g., trying to set a color temperature on non-supported bulb.
ValueErrors are raised instead of SmartDeviceExceptions where appropriate (e.g. when trying to set an invalid hsv or brightness).
New enum type DeviceType is added to allow detecting device types without resorting to isinstance() calling. SmartDevice class' device_type property can be used to query the type. is_plug and is_bulb helpers are added.
* Cleanup tests and improve test coverage
* Make writing tests easier by sharing code for common implementations
* Instead of storing test data inside python files, dump-discover based information is used
* This will simplify adding new tests and remove code duplication
* fixtures are based on https://github.com/plasticrake/tplink-smarthome-simulator
* run black on newfakes
* Add HS300 tests and update SmartStrip API according to earlier changes, still WIP
* run black and avoid wildcard imports
* Black on conftest
* bump minimum required version to 3.5
* Rename fixture_tests to test_fixtures for autocollect
* fix typoed type to _type, black
* run black on several files with -79 to fix hound issues
* Fix broken merge on hue
* Fix tests (hue update, pass context to smartdevice), add is_strip property, disable emeter tests for HS300 until a solution for API is found.
* Fix old tests
* Run black on changed files
* Add real HS220 discovery, thanks to @poiyo
* add is_dimmable and is_variable_color_temp to smartdevice class, simplifies interfacing with homeassistant
* add KL120(US) fixture
* Add a simple query cache
This commit adds a simple query cache to speed up the process for users
requesting lots of different properties from the device, as done by the
cli tool as well as homeassistant.
The logic for caching is very simple:
1. A timestamp for last fetch for each module+command is stored alongside the response.
2. If the issued command starts with `get_` and the TTL has not expired, the cache result is returned.
3. Otherwise the cache for the whole corresponding module gets invalidated, the device will be queried and the result will be stored in the cache.
* add deprecation to tox.ini
* make tests pass again
* remove old tests, add flake8 to tox reqs
* run black against pyhs100 module, add it to precommit hooks, fix flake8 configuration to conform to black standards (https://ljvmiranda921.github.io/notebook/2018/06/21/precommits-using-black-and-flake8/)
* fix syntax
* cleanup conftest
2019-06-16 21:05:00 +00:00
|
|
|
|
2019-11-15 16:48:36 +00:00
|
|
|
valid_temperature_range = self.valid_temperature_range
|
2019-11-11 18:16:55 +00:00
|
|
|
if temp < valid_temperature_range[0] or temp > valid_temperature_range[1]:
|
API and tests cleanup (#151)
* Add new cli commands: raw_command and dump_discover
- raw_command can be used to execute raw commands with given parameters
* Useful for testing new calls before implementing them properly
- dump_discover can be used to dump the device discovery information (into a file)
* The discovery is extended to request more modules and methods from devices
* smartlife.iot.dimmer get_dimmer_parameters
* smartlife.iot.common.emeter get_realtime
* smartlife.iot.smartbulb.lightingservice get_light_state
* This is used to dump more information for proper tests, and will also allow better discovery in the future
This commit contains also some documentation updates and dropping click_datetime in favor of click's built-in datetime
* Docstring fixes
* Major API cleanup
Properties shall no more change the state of the device, this work in still in progress, the main goal being making the API more user-friendly and to make implementing new features simpler.
The newly deprecated functionality will remain working and will simply warn the user about deprecation.
Previously deprecated 'features' property and 'identify' method are now finally removed.
Deprecate and replace the following property setters:
* state with turn_on() and turn_off()
* hsv with set_hsv()
* color_temp with set_color_temp()
* brightness with set_brightness()
* led with set_led()
* alias with set_alias()
* mac with set_mac()
And getters:
* state with is_on and is_off
The {BULB,PLUG}_STATE_{ON,OFF} is simplified to STATE_ON and STATE_OFF, UNKNOWN state is removed.
These are now deprecated and will be removed in the future.
* is_on and is_off can be used to check for the state
* turn_on() and turn_off() for changing the device state.
Trying to use functionality not supported by the device will cause SmartDeviceExceptions instead of failing silently and/or returning None.
This includes, e.g., trying to set a color temperature on non-supported bulb.
ValueErrors are raised instead of SmartDeviceExceptions where appropriate (e.g. when trying to set an invalid hsv or brightness).
New enum type DeviceType is added to allow detecting device types without resorting to isinstance() calling. SmartDevice class' device_type property can be used to query the type. is_plug and is_bulb helpers are added.
* Cleanup tests and improve test coverage
* Make writing tests easier by sharing code for common implementations
* Instead of storing test data inside python files, dump-discover based information is used
* This will simplify adding new tests and remove code duplication
* fixtures are based on https://github.com/plasticrake/tplink-smarthome-simulator
* run black on newfakes
* Add HS300 tests and update SmartStrip API according to earlier changes, still WIP
* run black and avoid wildcard imports
* Black on conftest
* bump minimum required version to 3.5
* Rename fixture_tests to test_fixtures for autocollect
* fix typoed type to _type, black
* run black on several files with -79 to fix hound issues
* Fix broken merge on hue
* Fix tests (hue update, pass context to smartdevice), add is_strip property, disable emeter tests for HS300 until a solution for API is found.
* Fix old tests
* Run black on changed files
* Add real HS220 discovery, thanks to @poiyo
* add is_dimmable and is_variable_color_temp to smartdevice class, simplifies interfacing with homeassistant
* add KL120(US) fixture
* Add a simple query cache
This commit adds a simple query cache to speed up the process for users
requesting lots of different properties from the device, as done by the
cli tool as well as homeassistant.
The logic for caching is very simple:
1. A timestamp for last fetch for each module+command is stored alongside the response.
2. If the issued command starts with `get_` and the TTL has not expired, the cache result is returned.
3. Otherwise the cache for the whole corresponding module gets invalidated, the device will be queried and the result will be stored in the cache.
* add deprecation to tox.ini
* make tests pass again
* remove old tests, add flake8 to tox reqs
* run black against pyhs100 module, add it to precommit hooks, fix flake8 configuration to conform to black standards (https://ljvmiranda921.github.io/notebook/2018/06/21/precommits-using-black-and-flake8/)
* fix syntax
* cleanup conftest
2019-06-16 21:05:00 +00:00
|
|
|
raise ValueError(
|
2023-10-29 22:15:42 +00:00
|
|
|
"Temperature should be between {} and {}, was {}".format(
|
|
|
|
*valid_temperature_range, temp
|
|
|
|
)
|
API and tests cleanup (#151)
* Add new cli commands: raw_command and dump_discover
- raw_command can be used to execute raw commands with given parameters
* Useful for testing new calls before implementing them properly
- dump_discover can be used to dump the device discovery information (into a file)
* The discovery is extended to request more modules and methods from devices
* smartlife.iot.dimmer get_dimmer_parameters
* smartlife.iot.common.emeter get_realtime
* smartlife.iot.smartbulb.lightingservice get_light_state
* This is used to dump more information for proper tests, and will also allow better discovery in the future
This commit contains also some documentation updates and dropping click_datetime in favor of click's built-in datetime
* Docstring fixes
* Major API cleanup
Properties shall no more change the state of the device, this work in still in progress, the main goal being making the API more user-friendly and to make implementing new features simpler.
The newly deprecated functionality will remain working and will simply warn the user about deprecation.
Previously deprecated 'features' property and 'identify' method are now finally removed.
Deprecate and replace the following property setters:
* state with turn_on() and turn_off()
* hsv with set_hsv()
* color_temp with set_color_temp()
* brightness with set_brightness()
* led with set_led()
* alias with set_alias()
* mac with set_mac()
And getters:
* state with is_on and is_off
The {BULB,PLUG}_STATE_{ON,OFF} is simplified to STATE_ON and STATE_OFF, UNKNOWN state is removed.
These are now deprecated and will be removed in the future.
* is_on and is_off can be used to check for the state
* turn_on() and turn_off() for changing the device state.
Trying to use functionality not supported by the device will cause SmartDeviceExceptions instead of failing silently and/or returning None.
This includes, e.g., trying to set a color temperature on non-supported bulb.
ValueErrors are raised instead of SmartDeviceExceptions where appropriate (e.g. when trying to set an invalid hsv or brightness).
New enum type DeviceType is added to allow detecting device types without resorting to isinstance() calling. SmartDevice class' device_type property can be used to query the type. is_plug and is_bulb helpers are added.
* Cleanup tests and improve test coverage
* Make writing tests easier by sharing code for common implementations
* Instead of storing test data inside python files, dump-discover based information is used
* This will simplify adding new tests and remove code duplication
* fixtures are based on https://github.com/plasticrake/tplink-smarthome-simulator
* run black on newfakes
* Add HS300 tests and update SmartStrip API according to earlier changes, still WIP
* run black and avoid wildcard imports
* Black on conftest
* bump minimum required version to 3.5
* Rename fixture_tests to test_fixtures for autocollect
* fix typoed type to _type, black
* run black on several files with -79 to fix hound issues
* Fix broken merge on hue
* Fix tests (hue update, pass context to smartdevice), add is_strip property, disable emeter tests for HS300 until a solution for API is found.
* Fix old tests
* Run black on changed files
* Add real HS220 discovery, thanks to @poiyo
* add is_dimmable and is_variable_color_temp to smartdevice class, simplifies interfacing with homeassistant
* add KL120(US) fixture
* Add a simple query cache
This commit adds a simple query cache to speed up the process for users
requesting lots of different properties from the device, as done by the
cli tool as well as homeassistant.
The logic for caching is very simple:
1. A timestamp for last fetch for each module+command is stored alongside the response.
2. If the issued command starts with `get_` and the TTL has not expired, the cache result is returned.
3. Otherwise the cache for the whole corresponding module gets invalidated, the device will be queried and the result will be stored in the cache.
* add deprecation to tox.ini
* make tests pass again
* remove old tests, add flake8 to tox reqs
* run black against pyhs100 module, add it to precommit hooks, fix flake8 configuration to conform to black standards (https://ljvmiranda921.github.io/notebook/2018/06/21/precommits-using-black-and-flake8/)
* fix syntax
* cleanup conftest
2019-06-16 21:05:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
light_state = {"color_temp": temp}
|
2020-07-06 14:09:30 +00:00
|
|
|
if brightness is not None:
|
|
|
|
light_state["brightness"] = brightness
|
|
|
|
|
2020-06-14 18:21:55 +00:00
|
|
|
return await self.set_light_state(light_state, transition=transition)
|
2017-04-14 12:24:58 +00:00
|
|
|
|
2019-12-12 09:46:40 +00:00
|
|
|
@property # type: ignore
|
2019-11-15 16:48:36 +00:00
|
|
|
@requires_update
|
|
|
|
def brightness(self) -> int:
|
2020-05-27 14:55:18 +00:00
|
|
|
"""Return the current brightness in percentage."""
|
2019-11-15 16:48:36 +00:00
|
|
|
if not self.is_dimmable: # pragma: no cover
|
2024-02-21 15:52:55 +00:00
|
|
|
raise KasaException("Bulb is not dimmable.")
|
2017-04-14 12:24:58 +00:00
|
|
|
|
2019-11-15 16:48:36 +00:00
|
|
|
light_state = self.light_state
|
2020-05-24 15:57:54 +00:00
|
|
|
return int(light_state["brightness"])
|
2017-04-14 12:24:58 +00:00
|
|
|
|
2019-11-15 16:48:36 +00:00
|
|
|
@requires_update
|
2022-11-15 18:05:08 +00:00
|
|
|
async def set_brightness(
|
|
|
|
self, brightness: int, *, transition: Optional[int] = None
|
|
|
|
) -> Dict:
|
2020-06-14 18:21:55 +00:00
|
|
|
"""Set the brightness in percentage.
|
|
|
|
|
|
|
|
:param int brightness: brightness in percent
|
|
|
|
:param int transition: transition in milliseconds.
|
|
|
|
"""
|
2019-11-15 16:48:36 +00:00
|
|
|
if not self.is_dimmable: # pragma: no cover
|
2024-02-21 15:52:55 +00:00
|
|
|
raise KasaException("Bulb is not dimmable.")
|
2017-04-14 12:24:58 +00:00
|
|
|
|
API and tests cleanup (#151)
* Add new cli commands: raw_command and dump_discover
- raw_command can be used to execute raw commands with given parameters
* Useful for testing new calls before implementing them properly
- dump_discover can be used to dump the device discovery information (into a file)
* The discovery is extended to request more modules and methods from devices
* smartlife.iot.dimmer get_dimmer_parameters
* smartlife.iot.common.emeter get_realtime
* smartlife.iot.smartbulb.lightingservice get_light_state
* This is used to dump more information for proper tests, and will also allow better discovery in the future
This commit contains also some documentation updates and dropping click_datetime in favor of click's built-in datetime
* Docstring fixes
* Major API cleanup
Properties shall no more change the state of the device, this work in still in progress, the main goal being making the API more user-friendly and to make implementing new features simpler.
The newly deprecated functionality will remain working and will simply warn the user about deprecation.
Previously deprecated 'features' property and 'identify' method are now finally removed.
Deprecate and replace the following property setters:
* state with turn_on() and turn_off()
* hsv with set_hsv()
* color_temp with set_color_temp()
* brightness with set_brightness()
* led with set_led()
* alias with set_alias()
* mac with set_mac()
And getters:
* state with is_on and is_off
The {BULB,PLUG}_STATE_{ON,OFF} is simplified to STATE_ON and STATE_OFF, UNKNOWN state is removed.
These are now deprecated and will be removed in the future.
* is_on and is_off can be used to check for the state
* turn_on() and turn_off() for changing the device state.
Trying to use functionality not supported by the device will cause SmartDeviceExceptions instead of failing silently and/or returning None.
This includes, e.g., trying to set a color temperature on non-supported bulb.
ValueErrors are raised instead of SmartDeviceExceptions where appropriate (e.g. when trying to set an invalid hsv or brightness).
New enum type DeviceType is added to allow detecting device types without resorting to isinstance() calling. SmartDevice class' device_type property can be used to query the type. is_plug and is_bulb helpers are added.
* Cleanup tests and improve test coverage
* Make writing tests easier by sharing code for common implementations
* Instead of storing test data inside python files, dump-discover based information is used
* This will simplify adding new tests and remove code duplication
* fixtures are based on https://github.com/plasticrake/tplink-smarthome-simulator
* run black on newfakes
* Add HS300 tests and update SmartStrip API according to earlier changes, still WIP
* run black and avoid wildcard imports
* Black on conftest
* bump minimum required version to 3.5
* Rename fixture_tests to test_fixtures for autocollect
* fix typoed type to _type, black
* run black on several files with -79 to fix hound issues
* Fix broken merge on hue
* Fix tests (hue update, pass context to smartdevice), add is_strip property, disable emeter tests for HS300 until a solution for API is found.
* Fix old tests
* Run black on changed files
* Add real HS220 discovery, thanks to @poiyo
* add is_dimmable and is_variable_color_temp to smartdevice class, simplifies interfacing with homeassistant
* add KL120(US) fixture
* Add a simple query cache
This commit adds a simple query cache to speed up the process for users
requesting lots of different properties from the device, as done by the
cli tool as well as homeassistant.
The logic for caching is very simple:
1. A timestamp for last fetch for each module+command is stored alongside the response.
2. If the issued command starts with `get_` and the TTL has not expired, the cache result is returned.
3. Otherwise the cache for the whole corresponding module gets invalidated, the device will be queried and the result will be stored in the cache.
* add deprecation to tox.ini
* make tests pass again
* remove old tests, add flake8 to tox reqs
* run black against pyhs100 module, add it to precommit hooks, fix flake8 configuration to conform to black standards (https://ljvmiranda921.github.io/notebook/2018/06/21/precommits-using-black-and-flake8/)
* fix syntax
* cleanup conftest
2019-06-16 21:05:00 +00:00
|
|
|
self._raise_for_invalid_brightness(brightness)
|
|
|
|
|
|
|
|
light_state = {"brightness": brightness}
|
2020-06-14 18:21:55 +00:00
|
|
|
return await self.set_light_state(light_state, transition=transition)
|
2017-09-18 16:13:06 +00:00
|
|
|
|
2019-12-12 09:46:40 +00:00
|
|
|
@property # type: ignore
|
2019-11-15 16:48:36 +00:00
|
|
|
@requires_update
|
|
|
|
def is_on(self) -> bool:
|
API and tests cleanup (#151)
* Add new cli commands: raw_command and dump_discover
- raw_command can be used to execute raw commands with given parameters
* Useful for testing new calls before implementing them properly
- dump_discover can be used to dump the device discovery information (into a file)
* The discovery is extended to request more modules and methods from devices
* smartlife.iot.dimmer get_dimmer_parameters
* smartlife.iot.common.emeter get_realtime
* smartlife.iot.smartbulb.lightingservice get_light_state
* This is used to dump more information for proper tests, and will also allow better discovery in the future
This commit contains also some documentation updates and dropping click_datetime in favor of click's built-in datetime
* Docstring fixes
* Major API cleanup
Properties shall no more change the state of the device, this work in still in progress, the main goal being making the API more user-friendly and to make implementing new features simpler.
The newly deprecated functionality will remain working and will simply warn the user about deprecation.
Previously deprecated 'features' property and 'identify' method are now finally removed.
Deprecate and replace the following property setters:
* state with turn_on() and turn_off()
* hsv with set_hsv()
* color_temp with set_color_temp()
* brightness with set_brightness()
* led with set_led()
* alias with set_alias()
* mac with set_mac()
And getters:
* state with is_on and is_off
The {BULB,PLUG}_STATE_{ON,OFF} is simplified to STATE_ON and STATE_OFF, UNKNOWN state is removed.
These are now deprecated and will be removed in the future.
* is_on and is_off can be used to check for the state
* turn_on() and turn_off() for changing the device state.
Trying to use functionality not supported by the device will cause SmartDeviceExceptions instead of failing silently and/or returning None.
This includes, e.g., trying to set a color temperature on non-supported bulb.
ValueErrors are raised instead of SmartDeviceExceptions where appropriate (e.g. when trying to set an invalid hsv or brightness).
New enum type DeviceType is added to allow detecting device types without resorting to isinstance() calling. SmartDevice class' device_type property can be used to query the type. is_plug and is_bulb helpers are added.
* Cleanup tests and improve test coverage
* Make writing tests easier by sharing code for common implementations
* Instead of storing test data inside python files, dump-discover based information is used
* This will simplify adding new tests and remove code duplication
* fixtures are based on https://github.com/plasticrake/tplink-smarthome-simulator
* run black on newfakes
* Add HS300 tests and update SmartStrip API according to earlier changes, still WIP
* run black and avoid wildcard imports
* Black on conftest
* bump minimum required version to 3.5
* Rename fixture_tests to test_fixtures for autocollect
* fix typoed type to _type, black
* run black on several files with -79 to fix hound issues
* Fix broken merge on hue
* Fix tests (hue update, pass context to smartdevice), add is_strip property, disable emeter tests for HS300 until a solution for API is found.
* Fix old tests
* Run black on changed files
* Add real HS220 discovery, thanks to @poiyo
* add is_dimmable and is_variable_color_temp to smartdevice class, simplifies interfacing with homeassistant
* add KL120(US) fixture
* Add a simple query cache
This commit adds a simple query cache to speed up the process for users
requesting lots of different properties from the device, as done by the
cli tool as well as homeassistant.
The logic for caching is very simple:
1. A timestamp for last fetch for each module+command is stored alongside the response.
2. If the issued command starts with `get_` and the TTL has not expired, the cache result is returned.
3. Otherwise the cache for the whole corresponding module gets invalidated, the device will be queried and the result will be stored in the cache.
* add deprecation to tox.ini
* make tests pass again
* remove old tests, add flake8 to tox reqs
* run black against pyhs100 module, add it to precommit hooks, fix flake8 configuration to conform to black standards (https://ljvmiranda921.github.io/notebook/2018/06/21/precommits-using-black-and-flake8/)
* fix syntax
* cleanup conftest
2019-06-16 21:05:00 +00:00
|
|
|
"""Return whether the device is on."""
|
2019-11-15 16:48:36 +00:00
|
|
|
light_state = self.light_state
|
API and tests cleanup (#151)
* Add new cli commands: raw_command and dump_discover
- raw_command can be used to execute raw commands with given parameters
* Useful for testing new calls before implementing them properly
- dump_discover can be used to dump the device discovery information (into a file)
* The discovery is extended to request more modules and methods from devices
* smartlife.iot.dimmer get_dimmer_parameters
* smartlife.iot.common.emeter get_realtime
* smartlife.iot.smartbulb.lightingservice get_light_state
* This is used to dump more information for proper tests, and will also allow better discovery in the future
This commit contains also some documentation updates and dropping click_datetime in favor of click's built-in datetime
* Docstring fixes
* Major API cleanup
Properties shall no more change the state of the device, this work in still in progress, the main goal being making the API more user-friendly and to make implementing new features simpler.
The newly deprecated functionality will remain working and will simply warn the user about deprecation.
Previously deprecated 'features' property and 'identify' method are now finally removed.
Deprecate and replace the following property setters:
* state with turn_on() and turn_off()
* hsv with set_hsv()
* color_temp with set_color_temp()
* brightness with set_brightness()
* led with set_led()
* alias with set_alias()
* mac with set_mac()
And getters:
* state with is_on and is_off
The {BULB,PLUG}_STATE_{ON,OFF} is simplified to STATE_ON and STATE_OFF, UNKNOWN state is removed.
These are now deprecated and will be removed in the future.
* is_on and is_off can be used to check for the state
* turn_on() and turn_off() for changing the device state.
Trying to use functionality not supported by the device will cause SmartDeviceExceptions instead of failing silently and/or returning None.
This includes, e.g., trying to set a color temperature on non-supported bulb.
ValueErrors are raised instead of SmartDeviceExceptions where appropriate (e.g. when trying to set an invalid hsv or brightness).
New enum type DeviceType is added to allow detecting device types without resorting to isinstance() calling. SmartDevice class' device_type property can be used to query the type. is_plug and is_bulb helpers are added.
* Cleanup tests and improve test coverage
* Make writing tests easier by sharing code for common implementations
* Instead of storing test data inside python files, dump-discover based information is used
* This will simplify adding new tests and remove code duplication
* fixtures are based on https://github.com/plasticrake/tplink-smarthome-simulator
* run black on newfakes
* Add HS300 tests and update SmartStrip API according to earlier changes, still WIP
* run black and avoid wildcard imports
* Black on conftest
* bump minimum required version to 3.5
* Rename fixture_tests to test_fixtures for autocollect
* fix typoed type to _type, black
* run black on several files with -79 to fix hound issues
* Fix broken merge on hue
* Fix tests (hue update, pass context to smartdevice), add is_strip property, disable emeter tests for HS300 until a solution for API is found.
* Fix old tests
* Run black on changed files
* Add real HS220 discovery, thanks to @poiyo
* add is_dimmable and is_variable_color_temp to smartdevice class, simplifies interfacing with homeassistant
* add KL120(US) fixture
* Add a simple query cache
This commit adds a simple query cache to speed up the process for users
requesting lots of different properties from the device, as done by the
cli tool as well as homeassistant.
The logic for caching is very simple:
1. A timestamp for last fetch for each module+command is stored alongside the response.
2. If the issued command starts with `get_` and the TTL has not expired, the cache result is returned.
3. Otherwise the cache for the whole corresponding module gets invalidated, the device will be queried and the result will be stored in the cache.
* add deprecation to tox.ini
* make tests pass again
* remove old tests, add flake8 to tox reqs
* run black against pyhs100 module, add it to precommit hooks, fix flake8 configuration to conform to black standards (https://ljvmiranda921.github.io/notebook/2018/06/21/precommits-using-black-and-flake8/)
* fix syntax
* cleanup conftest
2019-06-16 21:05:00 +00:00
|
|
|
return bool(light_state["on_off"])
|
2017-04-14 12:24:58 +00:00
|
|
|
|
2022-11-15 18:05:08 +00:00
|
|
|
async def turn_off(self, *, transition: Optional[int] = None, **kwargs) -> Dict:
|
2020-06-14 18:21:55 +00:00
|
|
|
"""Turn the bulb off.
|
|
|
|
|
|
|
|
:param int transition: transition in milliseconds.
|
|
|
|
"""
|
|
|
|
return await self.set_light_state({"on_off": 0}, transition=transition)
|
2017-04-24 17:28:22 +00:00
|
|
|
|
2022-11-15 18:05:08 +00:00
|
|
|
async def turn_on(self, *, transition: Optional[int] = None, **kwargs) -> Dict:
|
2020-06-14 18:21:55 +00:00
|
|
|
"""Turn the bulb on.
|
|
|
|
|
|
|
|
:param int transition: transition in milliseconds.
|
|
|
|
"""
|
|
|
|
return await self.set_light_state({"on_off": 1}, transition=transition)
|
2017-04-24 17:28:22 +00:00
|
|
|
|
2019-12-12 09:46:40 +00:00
|
|
|
@property # type: ignore
|
2019-11-15 16:48:36 +00:00
|
|
|
@requires_update
|
|
|
|
def has_emeter(self) -> bool:
|
2019-11-11 21:14:34 +00:00
|
|
|
"""Return that the bulb has an emeter."""
|
2017-04-14 12:24:58 +00:00
|
|
|
return True
|
2021-09-19 21:53:17 +00:00
|
|
|
|
|
|
|
async def set_alias(self, alias: str) -> None:
|
|
|
|
"""Set the device name (alias).
|
|
|
|
|
|
|
|
Overridden to use a different module name.
|
|
|
|
"""
|
|
|
|
return await self._query_helper(
|
|
|
|
"smartlife.iot.common.system", "set_dev_alias", {"alias": alias}
|
|
|
|
)
|
2022-10-22 22:15:47 +00:00
|
|
|
|
|
|
|
@property # type: ignore
|
|
|
|
@requires_update
|
2024-02-04 15:20:08 +00:00
|
|
|
def presets(self) -> List[BulbPreset]:
|
2022-10-22 22:15:47 +00:00
|
|
|
"""Return a list of available bulb setting presets."""
|
2024-02-04 15:20:08 +00:00
|
|
|
return [BulbPreset(**vals) for vals in self.sys_info["preferred_state"]]
|
2022-10-22 22:15:47 +00:00
|
|
|
|
2024-02-04 15:20:08 +00:00
|
|
|
async def save_preset(self, preset: BulbPreset):
|
2022-10-22 22:15:47 +00:00
|
|
|
"""Save a setting preset.
|
|
|
|
|
2023-04-01 14:15:58 +00:00
|
|
|
You can either construct a preset object manually, or pass an existing one
|
2022-10-22 22:15:47 +00:00
|
|
|
obtained using :func:`presets`.
|
|
|
|
"""
|
|
|
|
if len(self.presets) == 0:
|
2024-02-21 15:52:55 +00:00
|
|
|
raise KasaException("Device does not supported saving presets")
|
2022-10-22 22:15:47 +00:00
|
|
|
|
|
|
|
if preset.index >= len(self.presets):
|
2024-02-21 15:52:55 +00:00
|
|
|
raise KasaException("Invalid preset index")
|
2022-10-22 22:15:47 +00:00
|
|
|
|
|
|
|
return await self._query_helper(
|
2023-04-01 14:15:58 +00:00
|
|
|
self.LIGHT_SERVICE, "set_preferred_state", preset.dict(exclude_none=True)
|
2022-10-22 22:15:47 +00:00
|
|
|
)
|
2023-09-14 18:51:40 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def max_device_response_size(self) -> int:
|
|
|
|
"""Returns the maximum response size the device can safely construct."""
|
|
|
|
return 4096
|