python-kasa 0.6.2__py3-none-any.whl → 0.7.0.dev0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- kasa/__init__.py +88 -27
- kasa/aestransport.py +51 -34
- kasa/cli.py +326 -130
- kasa/device.py +406 -0
- kasa/device_factory.py +83 -44
- kasa/device_type.py +7 -3
- kasa/deviceconfig.py +16 -9
- kasa/discover.py +100 -81
- kasa/emeterstatus.py +12 -7
- kasa/exceptions.py +41 -26
- kasa/feature.py +201 -0
- kasa/httpclient.py +21 -14
- kasa/interfaces/__init__.py +16 -0
- kasa/interfaces/fan.py +20 -0
- kasa/interfaces/led.py +38 -0
- kasa/interfaces/light.py +133 -0
- kasa/interfaces/lighteffect.py +80 -0
- kasa/interfaces/lightpreset.py +76 -0
- kasa/iot/__init__.py +18 -0
- kasa/{effects.py → iot/effects.py} +4 -2
- kasa/{smartbulb.py → iot/iotbulb.py} +100 -147
- kasa/{smartdevice.py → iot/iotdevice.py} +157 -286
- kasa/{smartdimmer.py → iot/iotdimmer.py} +42 -33
- kasa/iot/iotlightstrip.py +71 -0
- kasa/{modules/module.py → iot/iotmodule.py} +22 -42
- kasa/iot/iotplug.py +94 -0
- kasa/{smartstrip.py → iot/iotstrip.py} +57 -75
- kasa/{modules → iot/modules}/__init__.py +10 -2
- kasa/{modules → iot/modules}/ambientlight.py +31 -3
- kasa/{modules → iot/modules}/antitheft.py +1 -0
- kasa/{modules → iot/modules}/cloud.py +25 -6
- kasa/{modules → iot/modules}/countdown.py +1 -0
- kasa/iot/modules/emeter.py +210 -0
- kasa/iot/modules/led.py +32 -0
- kasa/iot/modules/light.py +221 -0
- kasa/iot/modules/lighteffect.py +122 -0
- kasa/iot/modules/lightpreset.py +151 -0
- kasa/{modules → iot/modules}/motion.py +8 -8
- kasa/{modules → iot/modules}/rulemodule.py +11 -12
- kasa/{modules → iot/modules}/schedule.py +1 -0
- kasa/{modules → iot/modules}/time.py +5 -4
- kasa/{modules → iot/modules}/usage.py +10 -8
- kasa/iotprotocol.py +22 -20
- kasa/klaptransport.py +25 -15
- kasa/module.py +136 -0
- kasa/modulemapping.py +25 -0
- kasa/modulemapping.pyi +96 -0
- kasa/protocol.py +6 -4
- kasa/smart/__init__.py +6 -0
- kasa/smart/effects.py +429 -0
- kasa/smart/modules/__init__.py +57 -0
- kasa/smart/modules/alarm.py +142 -0
- kasa/smart/modules/autooff.py +96 -0
- kasa/smart/modules/batterysensor.py +54 -0
- kasa/smart/modules/brightness.py +66 -0
- kasa/smart/modules/childdevice.py +10 -0
- kasa/smart/modules/cloud.py +42 -0
- kasa/smart/modules/color.py +96 -0
- kasa/smart/modules/colortemperature.py +78 -0
- kasa/smart/modules/contactsensor.py +42 -0
- kasa/smart/modules/devicemodule.py +22 -0
- kasa/smart/modules/energy.py +101 -0
- kasa/smart/modules/fan.py +80 -0
- kasa/smart/modules/firmware.py +216 -0
- kasa/smart/modules/frostprotection.py +59 -0
- kasa/smart/modules/humiditysensor.py +55 -0
- kasa/smart/modules/led.py +49 -0
- kasa/smart/modules/light.py +139 -0
- kasa/smart/modules/lighteffect.py +115 -0
- kasa/smart/modules/lightpreset.py +142 -0
- kasa/smart/modules/lightstripeffect.py +135 -0
- kasa/smart/modules/lighttransition.py +183 -0
- kasa/smart/modules/reportmode.py +36 -0
- kasa/smart/modules/temperaturecontrol.py +177 -0
- kasa/smart/modules/temperaturesensor.py +77 -0
- kasa/smart/modules/time.py +57 -0
- kasa/smart/modules/waterleaksensor.py +66 -0
- kasa/smart/smartchilddevice.py +65 -0
- kasa/smart/smartdevice.py +653 -0
- kasa/smart/smartmodule.py +112 -0
- kasa/smartprotocol.py +107 -41
- kasa/tests/conftest.py +27 -551
- kasa/tests/device_fixtures.py +427 -0
- kasa/tests/discovery_fixtures.py +197 -0
- kasa/tests/{newfakes.py → fakeprotocol_iot.py} +61 -291
- kasa/tests/fakeprotocol_smart.py +318 -0
- kasa/tests/fixtureinfo.py +154 -0
- kasa/tests/fixtures/smart/H100(EU)_1.0_1.2.3.json +221 -0
- kasa/tests/fixtures/smart/H100(EU)_1.0_1.5.10.json +547 -0
- kasa/tests/fixtures/smart/H100(EU)_1.0_1.5.5.json +391 -0
- kasa/tests/fixtures/smart/KH100(UK)_1.0_1.5.6.json +1557 -0
- kasa/tests/fixtures/smart/KS240(US)_1.0_1.0.4.json +899 -0
- kasa/tests/fixtures/smart/KS240(US)_1.0_1.0.5.json +479 -0
- kasa/tests/fixtures/smart/L530E(EU)_3.0_1.1.6.json +449 -0
- kasa/tests/fixtures/smart/P100_1.0.0_1.1.3.json +173 -0
- kasa/tests/fixtures/smart/P100_1.0.0_1.4.0.json +204 -0
- kasa/tests/fixtures/smart/P110(EU)_1.0_1.0.7.json +566 -0
- kasa/tests/fixtures/smart/P300(EU)_1.0_1.0.13.json +13 -23
- kasa/tests/fixtures/smart/P300(EU)_1.0_1.0.7.json +6 -6
- kasa/tests/fixtures/smart/TP15(US)_1.0_1.0.3.json +295 -0
- kasa/tests/fixtures/smart/TP25(US)_1.0_1.0.2.json +423 -0
- kasa/tests/fixtures/smart/child/KE100(EU)_1.0_2.4.0.json +170 -0
- kasa/tests/fixtures/smart/child/KE100(EU)_1.0_2.8.0.json +171 -0
- kasa/tests/fixtures/smart/child/KE100(UK)_1.0_2.8.0.json +171 -0
- kasa/tests/fixtures/smart/child/T110(EU)_1.0_1.8.0.json +526 -0
- kasa/tests/fixtures/smart/child/T300(EU)_1.0_1.7.0.json +533 -0
- kasa/tests/fixtures/smart/child/T310(EU)_1.0_1.5.0.json +530 -0
- kasa/tests/fixtures/smart/child/T315(EU)_1.0_1.7.0.json +537 -0
- kasa/tests/smart/__init__.py +0 -0
- kasa/tests/smart/features/__init__.py +0 -0
- kasa/tests/smart/features/test_brightness.py +55 -0
- kasa/tests/smart/features/test_colortemp.py +30 -0
- kasa/tests/smart/modules/__init__.py +0 -0
- kasa/tests/smart/modules/test_contact.py +28 -0
- kasa/tests/smart/modules/test_fan.py +79 -0
- kasa/tests/smart/modules/test_firmware.py +108 -0
- kasa/tests/smart/modules/test_humidity.py +28 -0
- kasa/tests/smart/modules/test_light_effect.py +41 -0
- kasa/tests/smart/modules/test_temperature.py +47 -0
- kasa/tests/smart/modules/test_temperaturecontrol.py +137 -0
- kasa/tests/smart/modules/test_waterleak.py +42 -0
- kasa/tests/test_aestransport.py +115 -25
- kasa/tests/test_bulb.py +169 -86
- kasa/tests/test_childdevice.py +84 -0
- kasa/tests/test_cli.py +452 -100
- kasa/tests/test_common_modules.py +216 -0
- kasa/tests/test_device.py +268 -0
- kasa/tests/test_device_factory.py +61 -41
- kasa/tests/test_device_type.py +1 -1
- kasa/tests/test_deviceconfig.py +2 -5
- kasa/tests/test_dimmer.py +33 -24
- kasa/tests/test_discovery.py +112 -69
- kasa/tests/test_emeter.py +39 -12
- kasa/tests/test_feature.py +205 -0
- kasa/tests/test_httpclient.py +9 -9
- kasa/tests/test_iotdevice.py +286 -0
- kasa/tests/test_klapprotocol.py +94 -41
- kasa/tests/test_lightstrip.py +24 -19
- kasa/tests/test_plug.py +51 -9
- kasa/tests/test_protocol.py +8 -12
- kasa/tests/test_readme_examples.py +86 -27
- kasa/tests/test_smartdevice.py +211 -247
- kasa/tests/test_smartprotocol.py +129 -26
- kasa/tests/test_strip.py +15 -6
- kasa/tests/test_usage.py +1 -3
- kasa/xortransport.py +15 -12
- {python_kasa-0.6.2.dist-info → python_kasa-0.7.0.dev0.dist-info}/METADATA +45 -141
- python_kasa-0.7.0.dev0.dist-info/RECORD +239 -0
- {python_kasa-0.6.2.dist-info → python_kasa-0.7.0.dev0.dist-info}/WHEEL +1 -1
- kasa/modules/emeter.py +0 -111
- kasa/smartlightstrip.py +0 -143
- kasa/smartplug.py +0 -91
- kasa/tapo/__init__.py +0 -7
- kasa/tapo/childdevice.py +0 -44
- kasa/tapo/tapobulb.py +0 -262
- kasa/tapo/tapodevice.py +0 -383
- kasa/tapo/tapoplug.py +0 -45
- kasa/tests/fixtures/HS105(US)_1.0_mocked.json +0 -47
- kasa/tests/fixtures/HS110(EU)_2.0_mocked.json +0 -50
- kasa/tests/fixtures/HS110(US)_1.0_mocked.json +0 -50
- kasa/tests/fixtures/HS200(US)_1.0_mocked.json +0 -47
- kasa/tests/fixtures/HS220(US)_1.0_mocked.json +0 -75
- kasa/tests/fixtures/LB100(US)_1.0_mocked.json +0 -103
- kasa/tests/fixtures/LB120(US)_1.0_mocked.json +0 -103
- kasa/tests/fixtures/LB130(US)_1.0_mocked.json +0 -104
- python_kasa-0.6.2.dist-info/RECORD +0 -163
- {python_kasa-0.6.2.dist-info → python_kasa-0.7.0.dev0.dist-info}/LICENSE +0 -0
- {python_kasa-0.6.2.dist-info → python_kasa-0.7.0.dev0.dist-info}/entry_points.txt +0 -0
|
@@ -1,50 +1,31 @@
|
|
|
1
1
|
"""Module for bulbs (LB*, KL*, KB*)."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
2
5
|
import logging
|
|
3
6
|
import re
|
|
4
7
|
from enum import Enum
|
|
5
|
-
from typing import
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
from
|
|
13
|
-
from
|
|
14
|
-
from .
|
|
15
|
-
from .
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
"""Hue-saturation-value."""
|
|
27
|
-
|
|
28
|
-
hue: int
|
|
29
|
-
saturation: int
|
|
30
|
-
value: int
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
class SmartBulbPreset(BaseModel):
|
|
34
|
-
"""Bulb configuration preset."""
|
|
35
|
-
|
|
36
|
-
index: int
|
|
37
|
-
brightness: int
|
|
38
|
-
|
|
39
|
-
# These are not available for effect mode presets on light strips
|
|
40
|
-
hue: Optional[int]
|
|
41
|
-
saturation: Optional[int]
|
|
42
|
-
color_temp: Optional[int]
|
|
43
|
-
|
|
44
|
-
# Variables for effect mode presets
|
|
45
|
-
custom: Optional[int]
|
|
46
|
-
id: Optional[str]
|
|
47
|
-
mode: Optional[int]
|
|
8
|
+
from typing import Optional, cast
|
|
9
|
+
|
|
10
|
+
from pydantic.v1 import BaseModel, Field, root_validator
|
|
11
|
+
|
|
12
|
+
from ..device_type import DeviceType
|
|
13
|
+
from ..deviceconfig import DeviceConfig
|
|
14
|
+
from ..interfaces.light import HSV, ColorTempRange
|
|
15
|
+
from ..module import Module
|
|
16
|
+
from ..protocol import BaseProtocol
|
|
17
|
+
from .iotdevice import IotDevice, KasaException, requires_update
|
|
18
|
+
from .modules import (
|
|
19
|
+
Antitheft,
|
|
20
|
+
Cloud,
|
|
21
|
+
Countdown,
|
|
22
|
+
Emeter,
|
|
23
|
+
Light,
|
|
24
|
+
LightPreset,
|
|
25
|
+
Schedule,
|
|
26
|
+
Time,
|
|
27
|
+
Usage,
|
|
28
|
+
)
|
|
48
29
|
|
|
49
30
|
|
|
50
31
|
class BehaviorMode(str, Enum):
|
|
@@ -68,7 +49,7 @@ class TurnOnBehavior(BaseModel):
|
|
|
68
49
|
"""
|
|
69
50
|
|
|
70
51
|
#: Index of preset to use, or ``None`` for the last known state.
|
|
71
|
-
preset: Optional[int] = Field(alias="index", default=None)
|
|
52
|
+
preset: Optional[int] = Field(alias="index", default=None) # noqa: UP007
|
|
72
53
|
#: Wanted behavior
|
|
73
54
|
mode: BehaviorMode
|
|
74
55
|
|
|
@@ -116,7 +97,7 @@ NON_COLOR_MODE_FLAGS = {"transition_period", "on_off"}
|
|
|
116
97
|
_LOGGER = logging.getLogger(__name__)
|
|
117
98
|
|
|
118
99
|
|
|
119
|
-
class
|
|
100
|
+
class IotBulb(IotDevice):
|
|
120
101
|
r"""Representation of a TP-Link Smart Bulb.
|
|
121
102
|
|
|
122
103
|
To initialize, you have to await :func:`update()` at least once.
|
|
@@ -127,12 +108,12 @@ class SmartBulb(SmartDevice):
|
|
|
127
108
|
so you must await :func:`update()` to fetch updates values from the device.
|
|
128
109
|
|
|
129
110
|
Errors reported by the device are raised as
|
|
130
|
-
:class:`
|
|
111
|
+
:class:`KasaException <kasa.exceptions.KasaException>`,
|
|
131
112
|
and should be handled by the user of the library.
|
|
132
113
|
|
|
133
114
|
Examples:
|
|
134
115
|
>>> import asyncio
|
|
135
|
-
>>> bulb =
|
|
116
|
+
>>> bulb = IotBulb("127.0.0.1")
|
|
136
117
|
>>> asyncio.run(bulb.update())
|
|
137
118
|
>>> print(bulb.alias)
|
|
138
119
|
Bulb2
|
|
@@ -198,9 +179,9 @@ class SmartBulb(SmartDevice):
|
|
|
198
179
|
Bulb configuration presets can be accessed using the :func:`presets` property:
|
|
199
180
|
|
|
200
181
|
>>> bulb.presets
|
|
201
|
-
[
|
|
182
|
+
[IotLightPreset(index=0, brightness=50, hue=0, saturation=0, color_temp=2700, custom=None, id=None, mode=None), IotLightPreset(index=1, brightness=100, hue=0, saturation=75, color_temp=0, custom=None, id=None, mode=None), IotLightPreset(index=2, brightness=100, hue=120, saturation=75, color_temp=0, custom=None, id=None, mode=None), IotLightPreset(index=3, brightness=100, hue=240, saturation=75, color_temp=0, custom=None, id=None, mode=None)]
|
|
202
183
|
|
|
203
|
-
To modify an existing preset, pass :class:`~kasa.
|
|
184
|
+
To modify an existing preset, pass :class:`~kasa.interfaces.light.LightPreset`
|
|
204
185
|
instance to :func:`save_preset` method:
|
|
205
186
|
|
|
206
187
|
>>> preset = bulb.presets[0]
|
|
@@ -208,6 +189,7 @@ class SmartBulb(SmartDevice):
|
|
|
208
189
|
50
|
|
209
190
|
>>> preset.brightness = 100
|
|
210
191
|
>>> asyncio.run(bulb.save_preset(preset))
|
|
192
|
+
>>> asyncio.run(bulb.update())
|
|
211
193
|
>>> bulb.presets[0].brightness
|
|
212
194
|
100
|
|
213
195
|
|
|
@@ -221,49 +203,59 @@ class SmartBulb(SmartDevice):
|
|
|
221
203
|
self,
|
|
222
204
|
host: str,
|
|
223
205
|
*,
|
|
224
|
-
config:
|
|
225
|
-
protocol:
|
|
206
|
+
config: DeviceConfig | None = None,
|
|
207
|
+
protocol: BaseProtocol | None = None,
|
|
226
208
|
) -> None:
|
|
227
209
|
super().__init__(host=host, config=config, protocol=protocol)
|
|
228
210
|
self._device_type = DeviceType.Bulb
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
self.add_module(
|
|
234
|
-
|
|
235
|
-
|
|
211
|
+
|
|
212
|
+
async def _initialize_modules(self):
|
|
213
|
+
"""Initialize modules not added in init."""
|
|
214
|
+
await super()._initialize_modules()
|
|
215
|
+
self.add_module(
|
|
216
|
+
Module.IotSchedule, Schedule(self, "smartlife.iot.common.schedule")
|
|
217
|
+
)
|
|
218
|
+
self.add_module(Module.IotUsage, Usage(self, "smartlife.iot.common.schedule"))
|
|
219
|
+
self.add_module(
|
|
220
|
+
Module.IotAntitheft, Antitheft(self, "smartlife.iot.common.anti_theft")
|
|
221
|
+
)
|
|
222
|
+
self.add_module(Module.IotTime, Time(self, "smartlife.iot.common.timesetting"))
|
|
223
|
+
self.add_module(Module.IotEmeter, Emeter(self, self.emeter_type))
|
|
224
|
+
self.add_module(Module.IotCountdown, Countdown(self, "countdown"))
|
|
225
|
+
self.add_module(Module.IotCloud, Cloud(self, "smartlife.iot.common.cloud"))
|
|
226
|
+
self.add_module(Module.Light, Light(self, self.LIGHT_SERVICE))
|
|
227
|
+
self.add_module(Module.LightPreset, LightPreset(self, self.LIGHT_SERVICE))
|
|
236
228
|
|
|
237
229
|
@property # type: ignore
|
|
238
230
|
@requires_update
|
|
239
|
-
def
|
|
231
|
+
def _is_color(self) -> bool:
|
|
240
232
|
"""Whether the bulb supports color changes."""
|
|
241
233
|
sys_info = self.sys_info
|
|
242
234
|
return bool(sys_info["is_color"])
|
|
243
235
|
|
|
244
236
|
@property # type: ignore
|
|
245
237
|
@requires_update
|
|
246
|
-
def
|
|
238
|
+
def _is_dimmable(self) -> bool:
|
|
247
239
|
"""Whether the bulb supports brightness changes."""
|
|
248
240
|
sys_info = self.sys_info
|
|
249
241
|
return bool(sys_info["is_dimmable"])
|
|
250
242
|
|
|
251
243
|
@property # type: ignore
|
|
252
244
|
@requires_update
|
|
253
|
-
def
|
|
245
|
+
def _is_variable_color_temp(self) -> bool:
|
|
254
246
|
"""Whether the bulb supports color temperature changes."""
|
|
255
247
|
sys_info = self.sys_info
|
|
256
248
|
return bool(sys_info["is_variable_color_temp"])
|
|
257
249
|
|
|
258
250
|
@property # type: ignore
|
|
259
251
|
@requires_update
|
|
260
|
-
def
|
|
252
|
+
def _valid_temperature_range(self) -> ColorTempRange:
|
|
261
253
|
"""Return the device-specific white temperature range (in Kelvin).
|
|
262
254
|
|
|
263
255
|
:return: White temperature range in Kelvin (minimum, maximum)
|
|
264
256
|
"""
|
|
265
|
-
if not self.
|
|
266
|
-
raise
|
|
257
|
+
if not self._is_variable_color_temp:
|
|
258
|
+
raise KasaException("Color temperature not supported")
|
|
267
259
|
|
|
268
260
|
for model, temp_range in TPLINK_KELVIN.items():
|
|
269
261
|
sys_info = self.sys_info
|
|
@@ -275,11 +267,11 @@ class SmartBulb(SmartDevice):
|
|
|
275
267
|
|
|
276
268
|
@property # type: ignore
|
|
277
269
|
@requires_update
|
|
278
|
-
def light_state(self) ->
|
|
270
|
+
def light_state(self) -> dict[str, str]:
|
|
279
271
|
"""Query the light state."""
|
|
280
272
|
light_state = self.sys_info["light_state"]
|
|
281
273
|
if light_state is None:
|
|
282
|
-
raise
|
|
274
|
+
raise KasaException(
|
|
283
275
|
"The device has no light_state or you have not called update()"
|
|
284
276
|
)
|
|
285
277
|
|
|
@@ -294,11 +286,11 @@ class SmartBulb(SmartDevice):
|
|
|
294
286
|
|
|
295
287
|
@property # type: ignore
|
|
296
288
|
@requires_update
|
|
297
|
-
def
|
|
289
|
+
def _has_effects(self) -> bool:
|
|
298
290
|
"""Return True if the device supports effects."""
|
|
299
291
|
return "lighting_effect_state" in self.sys_info
|
|
300
292
|
|
|
301
|
-
async def get_light_details(self) ->
|
|
293
|
+
async def get_light_details(self) -> dict[str, int]:
|
|
302
294
|
"""Return light details.
|
|
303
295
|
|
|
304
296
|
Example::
|
|
@@ -325,14 +317,14 @@ class SmartBulb(SmartDevice):
|
|
|
325
317
|
self.LIGHT_SERVICE, "set_default_behavior", behavior.dict(by_alias=True)
|
|
326
318
|
)
|
|
327
319
|
|
|
328
|
-
async def get_light_state(self) ->
|
|
320
|
+
async def get_light_state(self) -> dict[str, dict]:
|
|
329
321
|
"""Query the light state."""
|
|
330
322
|
# TODO: add warning and refer to use light.state?
|
|
331
323
|
return await self._query_helper(self.LIGHT_SERVICE, "get_light_state")
|
|
332
324
|
|
|
333
|
-
async def
|
|
334
|
-
self, state:
|
|
335
|
-
) ->
|
|
325
|
+
async def _set_light_state(
|
|
326
|
+
self, state: dict, *, transition: int | None = None
|
|
327
|
+
) -> dict:
|
|
336
328
|
"""Set the light state."""
|
|
337
329
|
if transition is not None:
|
|
338
330
|
state["transition_period"] = transition
|
|
@@ -357,13 +349,13 @@ class SmartBulb(SmartDevice):
|
|
|
357
349
|
|
|
358
350
|
@property # type: ignore
|
|
359
351
|
@requires_update
|
|
360
|
-
def
|
|
352
|
+
def _hsv(self) -> HSV:
|
|
361
353
|
"""Return the current HSV state of the bulb.
|
|
362
354
|
|
|
363
355
|
:return: hue, saturation and value (degrees, %, %)
|
|
364
356
|
"""
|
|
365
|
-
if not self.
|
|
366
|
-
raise
|
|
357
|
+
if not self._is_color:
|
|
358
|
+
raise KasaException("Bulb does not support color.")
|
|
367
359
|
|
|
368
360
|
light_state = cast(dict, self.light_state)
|
|
369
361
|
|
|
@@ -373,19 +365,15 @@ class SmartBulb(SmartDevice):
|
|
|
373
365
|
|
|
374
366
|
return HSV(hue, saturation, value)
|
|
375
367
|
|
|
376
|
-
def _raise_for_invalid_brightness(self, value):
|
|
377
|
-
if not isinstance(value, int) or not (0 <= value <= 100):
|
|
378
|
-
raise ValueError(f"Invalid brightness value: {value} (valid range: 0-100%)")
|
|
379
|
-
|
|
380
368
|
@requires_update
|
|
381
|
-
async def
|
|
369
|
+
async def _set_hsv(
|
|
382
370
|
self,
|
|
383
371
|
hue: int,
|
|
384
372
|
saturation: int,
|
|
385
|
-
value:
|
|
373
|
+
value: int | None = None,
|
|
386
374
|
*,
|
|
387
|
-
transition:
|
|
388
|
-
) ->
|
|
375
|
+
transition: int | None = None,
|
|
376
|
+
) -> dict:
|
|
389
377
|
"""Set new HSV.
|
|
390
378
|
|
|
391
379
|
:param int hue: hue in degrees
|
|
@@ -393,8 +381,8 @@ class SmartBulb(SmartDevice):
|
|
|
393
381
|
:param int value: value in percentage [0, 100]
|
|
394
382
|
:param int transition: transition in milliseconds.
|
|
395
383
|
"""
|
|
396
|
-
if not self.
|
|
397
|
-
raise
|
|
384
|
+
if not self._is_color:
|
|
385
|
+
raise KasaException("Bulb does not support color.")
|
|
398
386
|
|
|
399
387
|
if not isinstance(hue, int) or not (0 <= hue <= 360):
|
|
400
388
|
raise ValueError(f"Invalid hue value: {hue} (valid range: 0-360)")
|
|
@@ -414,29 +402,29 @@ class SmartBulb(SmartDevice):
|
|
|
414
402
|
self._raise_for_invalid_brightness(value)
|
|
415
403
|
light_state["brightness"] = value
|
|
416
404
|
|
|
417
|
-
return await self.
|
|
405
|
+
return await self._set_light_state(light_state, transition=transition)
|
|
418
406
|
|
|
419
407
|
@property # type: ignore
|
|
420
408
|
@requires_update
|
|
421
|
-
def
|
|
409
|
+
def _color_temp(self) -> int:
|
|
422
410
|
"""Return color temperature of the device in kelvin."""
|
|
423
|
-
if not self.
|
|
424
|
-
raise
|
|
411
|
+
if not self._is_variable_color_temp:
|
|
412
|
+
raise KasaException("Bulb does not support colortemp.")
|
|
425
413
|
|
|
426
414
|
light_state = self.light_state
|
|
427
415
|
return int(light_state["color_temp"])
|
|
428
416
|
|
|
429
417
|
@requires_update
|
|
430
|
-
async def
|
|
431
|
-
self, temp: int, *, brightness=None, transition:
|
|
432
|
-
) ->
|
|
418
|
+
async def _set_color_temp(
|
|
419
|
+
self, temp: int, *, brightness=None, transition: int | None = None
|
|
420
|
+
) -> dict:
|
|
433
421
|
"""Set the color temperature of the device in kelvin.
|
|
434
422
|
|
|
435
423
|
:param int temp: The new color temperature, in Kelvin
|
|
436
424
|
:param int transition: transition in milliseconds.
|
|
437
425
|
"""
|
|
438
|
-
if not self.
|
|
439
|
-
raise
|
|
426
|
+
if not self._is_variable_color_temp:
|
|
427
|
+
raise KasaException("Bulb does not support colortemp.")
|
|
440
428
|
|
|
441
429
|
valid_temperature_range = self.valid_temperature_range
|
|
442
430
|
if temp < valid_temperature_range[0] or temp > valid_temperature_range[1]:
|
|
@@ -450,51 +438,38 @@ class SmartBulb(SmartDevice):
|
|
|
450
438
|
if brightness is not None:
|
|
451
439
|
light_state["brightness"] = brightness
|
|
452
440
|
|
|
453
|
-
return await self.
|
|
441
|
+
return await self._set_light_state(light_state, transition=transition)
|
|
442
|
+
|
|
443
|
+
def _raise_for_invalid_brightness(self, value):
|
|
444
|
+
if not isinstance(value, int) or not (0 <= value <= 100):
|
|
445
|
+
raise ValueError(f"Invalid brightness value: {value} (valid range: 0-100%)")
|
|
454
446
|
|
|
455
447
|
@property # type: ignore
|
|
456
448
|
@requires_update
|
|
457
|
-
def
|
|
449
|
+
def _brightness(self) -> int:
|
|
458
450
|
"""Return the current brightness in percentage."""
|
|
459
|
-
if not self.
|
|
460
|
-
raise
|
|
451
|
+
if not self._is_dimmable: # pragma: no cover
|
|
452
|
+
raise KasaException("Bulb is not dimmable.")
|
|
461
453
|
|
|
462
454
|
light_state = self.light_state
|
|
463
455
|
return int(light_state["brightness"])
|
|
464
456
|
|
|
465
457
|
@requires_update
|
|
466
|
-
async def
|
|
467
|
-
self, brightness: int, *, transition:
|
|
468
|
-
) ->
|
|
458
|
+
async def _set_brightness(
|
|
459
|
+
self, brightness: int, *, transition: int | None = None
|
|
460
|
+
) -> dict:
|
|
469
461
|
"""Set the brightness in percentage.
|
|
470
462
|
|
|
471
463
|
:param int brightness: brightness in percent
|
|
472
464
|
:param int transition: transition in milliseconds.
|
|
473
465
|
"""
|
|
474
|
-
if not self.
|
|
475
|
-
raise
|
|
466
|
+
if not self._is_dimmable: # pragma: no cover
|
|
467
|
+
raise KasaException("Bulb is not dimmable.")
|
|
476
468
|
|
|
477
469
|
self._raise_for_invalid_brightness(brightness)
|
|
478
470
|
|
|
479
471
|
light_state = {"brightness": brightness}
|
|
480
|
-
return await self.
|
|
481
|
-
|
|
482
|
-
@property # type: ignore
|
|
483
|
-
@requires_update
|
|
484
|
-
def state_information(self) -> Dict[str, Any]:
|
|
485
|
-
"""Return bulb-specific state information."""
|
|
486
|
-
info: Dict[str, Any] = {
|
|
487
|
-
"Brightness": self.brightness,
|
|
488
|
-
"Is dimmable": self.is_dimmable,
|
|
489
|
-
}
|
|
490
|
-
if self.is_variable_color_temp:
|
|
491
|
-
info["Color temperature"] = self.color_temp
|
|
492
|
-
info["Valid temperature range"] = self.valid_temperature_range
|
|
493
|
-
if self.is_color:
|
|
494
|
-
info["HSV"] = self.hsv
|
|
495
|
-
info["Presets"] = self.presets
|
|
496
|
-
|
|
497
|
-
return info
|
|
472
|
+
return await self._set_light_state(light_state, transition=transition)
|
|
498
473
|
|
|
499
474
|
@property # type: ignore
|
|
500
475
|
@requires_update
|
|
@@ -503,19 +478,19 @@ class SmartBulb(SmartDevice):
|
|
|
503
478
|
light_state = self.light_state
|
|
504
479
|
return bool(light_state["on_off"])
|
|
505
480
|
|
|
506
|
-
async def turn_off(self, *, transition:
|
|
481
|
+
async def turn_off(self, *, transition: int | None = None, **kwargs) -> dict:
|
|
507
482
|
"""Turn the bulb off.
|
|
508
483
|
|
|
509
484
|
:param int transition: transition in milliseconds.
|
|
510
485
|
"""
|
|
511
|
-
return await self.
|
|
486
|
+
return await self._set_light_state({"on_off": 0}, transition=transition)
|
|
512
487
|
|
|
513
|
-
async def turn_on(self, *, transition:
|
|
488
|
+
async def turn_on(self, *, transition: int | None = None, **kwargs) -> dict:
|
|
514
489
|
"""Turn the bulb on.
|
|
515
490
|
|
|
516
491
|
:param int transition: transition in milliseconds.
|
|
517
492
|
"""
|
|
518
|
-
return await self.
|
|
493
|
+
return await self._set_light_state({"on_off": 1}, transition=transition)
|
|
519
494
|
|
|
520
495
|
@property # type: ignore
|
|
521
496
|
@requires_update
|
|
@@ -532,28 +507,6 @@ class SmartBulb(SmartDevice):
|
|
|
532
507
|
"smartlife.iot.common.system", "set_dev_alias", {"alias": alias}
|
|
533
508
|
)
|
|
534
509
|
|
|
535
|
-
@property # type: ignore
|
|
536
|
-
@requires_update
|
|
537
|
-
def presets(self) -> List[SmartBulbPreset]:
|
|
538
|
-
"""Return a list of available bulb setting presets."""
|
|
539
|
-
return [SmartBulbPreset(**vals) for vals in self.sys_info["preferred_state"]]
|
|
540
|
-
|
|
541
|
-
async def save_preset(self, preset: SmartBulbPreset):
|
|
542
|
-
"""Save a setting preset.
|
|
543
|
-
|
|
544
|
-
You can either construct a preset object manually, or pass an existing one
|
|
545
|
-
obtained using :func:`presets`.
|
|
546
|
-
"""
|
|
547
|
-
if len(self.presets) == 0:
|
|
548
|
-
raise SmartDeviceException("Device does not supported saving presets")
|
|
549
|
-
|
|
550
|
-
if preset.index >= len(self.presets):
|
|
551
|
-
raise SmartDeviceException("Invalid preset index")
|
|
552
|
-
|
|
553
|
-
return await self._query_helper(
|
|
554
|
-
self.LIGHT_SERVICE, "set_preferred_state", preset.dict(exclude_none=True)
|
|
555
|
-
)
|
|
556
|
-
|
|
557
510
|
@property
|
|
558
511
|
def max_device_response_size(self) -> int:
|
|
559
512
|
"""Returns the maximum response size the device can safely construct."""
|