python-kasa 0.6.2.1__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 +7 -5
- 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 -562
- kasa/tests/device_fixtures.py +427 -0
- kasa/tests/discovery_fixtures.py +197 -0
- kasa/tests/fakeprotocol_iot.py +64 -6
- kasa/tests/fakeprotocol_smart.py +215 -22
- 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/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 -23
- kasa/tests/test_bulb.py +115 -85
- kasa/tests/test_childdevice.py +19 -7
- kasa/tests/test_cli.py +453 -99
- kasa/tests/test_common_modules.py +216 -0
- kasa/tests/test_device.py +268 -0
- kasa/tests/test_device_factory.py +61 -35
- kasa/tests/test_device_type.py +1 -1
- kasa/tests/test_deviceconfig.py +2 -2
- kasa/tests/test_dimmer.py +33 -24
- kasa/tests/test_discovery.py +112 -63
- kasa/tests/test_emeter.py +14 -10
- 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 -37
- kasa/tests/test_lightstrip.py +24 -19
- kasa/tests/test_plug.py +46 -8
- kasa/tests/test_protocol.py +8 -10
- kasa/tests/test_readme_examples.py +87 -26
- kasa/tests/test_smartdevice.py +203 -311
- kasa/tests/test_smartprotocol.py +130 -25
- kasa/tests/test_strip.py +15 -6
- kasa/tests/test_usage.py +1 -1
- kasa/xortransport.py +15 -12
- {python_kasa-0.6.2.1.dist-info → python_kasa-0.7.0.dev0.dist-info}/METADATA +45 -143
- python_kasa-0.7.0.dev0.dist-info/RECORD +239 -0
- 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 -41
- kasa/tapo/tapobulb.py +0 -262
- kasa/tapo/tapodevice.py +0 -409
- 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.1.dist-info/RECORD +0 -167
- {python_kasa-0.6.2.1.dist-info → python_kasa-0.7.0.dev0.dist-info}/LICENSE +0 -0
- {python_kasa-0.6.2.1.dist-info → python_kasa-0.7.0.dev0.dist-info}/WHEEL +0 -0
- {python_kasa-0.6.2.1.dist-info → python_kasa-0.7.0.dev0.dist-info}/entry_points.txt +0 -0
kasa/tapo/tapobulb.py
DELETED
|
@@ -1,262 +0,0 @@
|
|
|
1
|
-
"""Module for tapo-branded smart bulbs (L5**)."""
|
|
2
|
-
from typing import Any, Dict, List, Optional
|
|
3
|
-
|
|
4
|
-
from ..exceptions import SmartDeviceException
|
|
5
|
-
from ..smartbulb import HSV, ColorTempRange, SmartBulb, SmartBulbPreset
|
|
6
|
-
from .tapodevice import TapoDevice
|
|
7
|
-
|
|
8
|
-
AVAILABLE_EFFECTS = {
|
|
9
|
-
"L1": "Party",
|
|
10
|
-
"L2": "Relax",
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
class TapoBulb(TapoDevice, SmartBulb):
|
|
15
|
-
"""Representation of a TP-Link Tapo Bulb.
|
|
16
|
-
|
|
17
|
-
Documentation TBD. See :class:`~kasa.smartbulb.SmartBulb` for now.
|
|
18
|
-
"""
|
|
19
|
-
|
|
20
|
-
@property
|
|
21
|
-
def is_color(self) -> bool:
|
|
22
|
-
"""Whether the bulb supports color changes."""
|
|
23
|
-
# TODO: this makes an assumption that only color bulbs report this
|
|
24
|
-
return "hue" in self._info
|
|
25
|
-
|
|
26
|
-
@property
|
|
27
|
-
def is_dimmable(self) -> bool:
|
|
28
|
-
"""Whether the bulb supports brightness changes."""
|
|
29
|
-
# TODO: this makes an assumption that only dimmables report this
|
|
30
|
-
return "brightness" in self._info
|
|
31
|
-
|
|
32
|
-
@property
|
|
33
|
-
def is_variable_color_temp(self) -> bool:
|
|
34
|
-
"""Whether the bulb supports color temperature changes."""
|
|
35
|
-
ct = self._info.get("color_temp_range")
|
|
36
|
-
# L900 reports [9000, 9000] even when it doesn't support changing the ct
|
|
37
|
-
return ct is not None and ct[0] != ct[1]
|
|
38
|
-
|
|
39
|
-
@property
|
|
40
|
-
def valid_temperature_range(self) -> ColorTempRange:
|
|
41
|
-
"""Return the device-specific white temperature range (in Kelvin).
|
|
42
|
-
|
|
43
|
-
:return: White temperature range in Kelvin (minimum, maximum)
|
|
44
|
-
"""
|
|
45
|
-
if not self.is_variable_color_temp:
|
|
46
|
-
raise SmartDeviceException("Color temperature not supported")
|
|
47
|
-
|
|
48
|
-
ct_range = self._info.get("color_temp_range", [0, 0])
|
|
49
|
-
return ColorTempRange(min=ct_range[0], max=ct_range[1])
|
|
50
|
-
|
|
51
|
-
@property
|
|
52
|
-
def has_effects(self) -> bool:
|
|
53
|
-
"""Return True if the device supports effects."""
|
|
54
|
-
return "dynamic_light_effect_enable" in self._info
|
|
55
|
-
|
|
56
|
-
@property
|
|
57
|
-
def effect(self) -> Dict:
|
|
58
|
-
"""Return effect state.
|
|
59
|
-
|
|
60
|
-
This follows the format used by SmartLightStrip.
|
|
61
|
-
|
|
62
|
-
Example:
|
|
63
|
-
{'brightness': 50,
|
|
64
|
-
'custom': 0,
|
|
65
|
-
'enable': 0,
|
|
66
|
-
'id': '',
|
|
67
|
-
'name': ''}
|
|
68
|
-
"""
|
|
69
|
-
# If no effect is active, dynamic_light_effect_id does not appear in info
|
|
70
|
-
current_effect = self._info.get("dynamic_light_effect_id", "")
|
|
71
|
-
data = {
|
|
72
|
-
"brightness": self.brightness,
|
|
73
|
-
"enable": current_effect != "",
|
|
74
|
-
"id": current_effect,
|
|
75
|
-
"name": AVAILABLE_EFFECTS.get(current_effect, ""),
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
return data
|
|
79
|
-
|
|
80
|
-
@property
|
|
81
|
-
def effect_list(self) -> Optional[List[str]]:
|
|
82
|
-
"""Return built-in effects list.
|
|
83
|
-
|
|
84
|
-
Example:
|
|
85
|
-
['Party', 'Relax', ...]
|
|
86
|
-
"""
|
|
87
|
-
return list(AVAILABLE_EFFECTS.keys()) if self.has_effects else None
|
|
88
|
-
|
|
89
|
-
@property
|
|
90
|
-
def hsv(self) -> HSV:
|
|
91
|
-
"""Return the current HSV state of the bulb.
|
|
92
|
-
|
|
93
|
-
:return: hue, saturation and value (degrees, %, %)
|
|
94
|
-
"""
|
|
95
|
-
if not self.is_color:
|
|
96
|
-
raise SmartDeviceException("Bulb does not support color.")
|
|
97
|
-
|
|
98
|
-
h, s, v = (
|
|
99
|
-
self._info.get("hue", 0),
|
|
100
|
-
self._info.get("saturation", 0),
|
|
101
|
-
self._info.get("brightness", 0),
|
|
102
|
-
)
|
|
103
|
-
|
|
104
|
-
return HSV(hue=h, saturation=s, value=v)
|
|
105
|
-
|
|
106
|
-
@property
|
|
107
|
-
def color_temp(self) -> int:
|
|
108
|
-
"""Whether the bulb supports color temperature changes."""
|
|
109
|
-
if not self.is_variable_color_temp:
|
|
110
|
-
raise SmartDeviceException("Bulb does not support colortemp.")
|
|
111
|
-
|
|
112
|
-
return self._info.get("color_temp", -1)
|
|
113
|
-
|
|
114
|
-
@property
|
|
115
|
-
def brightness(self) -> int:
|
|
116
|
-
"""Return the current brightness in percentage."""
|
|
117
|
-
if not self.is_dimmable: # pragma: no cover
|
|
118
|
-
raise SmartDeviceException("Bulb is not dimmable.")
|
|
119
|
-
|
|
120
|
-
return self._info.get("brightness", -1)
|
|
121
|
-
|
|
122
|
-
async def set_hsv(
|
|
123
|
-
self,
|
|
124
|
-
hue: int,
|
|
125
|
-
saturation: int,
|
|
126
|
-
value: Optional[int] = None,
|
|
127
|
-
*,
|
|
128
|
-
transition: Optional[int] = None,
|
|
129
|
-
) -> Dict:
|
|
130
|
-
"""Set new HSV.
|
|
131
|
-
|
|
132
|
-
Note, transition is not supported and will be ignored.
|
|
133
|
-
|
|
134
|
-
:param int hue: hue in degrees
|
|
135
|
-
:param int saturation: saturation in percentage [0,100]
|
|
136
|
-
:param int value: value in percentage [0, 100]
|
|
137
|
-
:param int transition: transition in milliseconds.
|
|
138
|
-
"""
|
|
139
|
-
if not self.is_color:
|
|
140
|
-
raise SmartDeviceException("Bulb does not support color.")
|
|
141
|
-
|
|
142
|
-
if not isinstance(hue, int) or not (0 <= hue <= 360):
|
|
143
|
-
raise ValueError(f"Invalid hue value: {hue} (valid range: 0-360)")
|
|
144
|
-
|
|
145
|
-
if not isinstance(saturation, int) or not (0 <= saturation <= 100):
|
|
146
|
-
raise ValueError(
|
|
147
|
-
f"Invalid saturation value: {saturation} (valid range: 0-100%)"
|
|
148
|
-
)
|
|
149
|
-
|
|
150
|
-
if value is not None:
|
|
151
|
-
self._raise_for_invalid_brightness(value)
|
|
152
|
-
|
|
153
|
-
request_payload = {
|
|
154
|
-
"color_temp": 0, # If set, color_temp takes precedence over hue&sat
|
|
155
|
-
"hue": hue,
|
|
156
|
-
"saturation": saturation,
|
|
157
|
-
}
|
|
158
|
-
# The device errors on invalid brightness values.
|
|
159
|
-
if value is not None:
|
|
160
|
-
request_payload["brightness"] = value
|
|
161
|
-
|
|
162
|
-
return await self.protocol.query({"set_device_info": {**request_payload}})
|
|
163
|
-
|
|
164
|
-
async def set_color_temp(
|
|
165
|
-
self, temp: int, *, brightness=None, transition: Optional[int] = None
|
|
166
|
-
) -> Dict:
|
|
167
|
-
"""Set the color temperature of the device in kelvin.
|
|
168
|
-
|
|
169
|
-
Note, transition is not supported and will be ignored.
|
|
170
|
-
|
|
171
|
-
:param int temp: The new color temperature, in Kelvin
|
|
172
|
-
:param int transition: transition in milliseconds.
|
|
173
|
-
"""
|
|
174
|
-
# TODO: Note, trying to set brightness at the same time
|
|
175
|
-
# with color_temp causes error -1008
|
|
176
|
-
if not self.is_variable_color_temp:
|
|
177
|
-
raise SmartDeviceException("Bulb does not support colortemp.")
|
|
178
|
-
|
|
179
|
-
valid_temperature_range = self.valid_temperature_range
|
|
180
|
-
if temp < valid_temperature_range[0] or temp > valid_temperature_range[1]:
|
|
181
|
-
raise ValueError(
|
|
182
|
-
"Temperature should be between {} and {}, was {}".format(
|
|
183
|
-
*valid_temperature_range, temp
|
|
184
|
-
)
|
|
185
|
-
)
|
|
186
|
-
|
|
187
|
-
return await self.protocol.query({"set_device_info": {"color_temp": temp}})
|
|
188
|
-
|
|
189
|
-
async def set_brightness(
|
|
190
|
-
self, brightness: int, *, transition: Optional[int] = None
|
|
191
|
-
) -> Dict:
|
|
192
|
-
"""Set the brightness in percentage.
|
|
193
|
-
|
|
194
|
-
Note, transition is not supported and will be ignored.
|
|
195
|
-
|
|
196
|
-
:param int brightness: brightness in percent
|
|
197
|
-
:param int transition: transition in milliseconds.
|
|
198
|
-
"""
|
|
199
|
-
if not self.is_dimmable: # pragma: no cover
|
|
200
|
-
raise SmartDeviceException("Bulb is not dimmable.")
|
|
201
|
-
|
|
202
|
-
return await self.protocol.query(
|
|
203
|
-
{"set_device_info": {"brightness": brightness}}
|
|
204
|
-
)
|
|
205
|
-
|
|
206
|
-
# Default state information, should be made to settings
|
|
207
|
-
"""
|
|
208
|
-
"info": {
|
|
209
|
-
"default_states": {
|
|
210
|
-
"re_power_type": "always_on",
|
|
211
|
-
"type": "last_states",
|
|
212
|
-
"state": {
|
|
213
|
-
"brightness": 36,
|
|
214
|
-
"hue": 0,
|
|
215
|
-
"saturation": 0,
|
|
216
|
-
"color_temp": 2700,
|
|
217
|
-
},
|
|
218
|
-
},
|
|
219
|
-
"""
|
|
220
|
-
|
|
221
|
-
async def set_effect(
|
|
222
|
-
self,
|
|
223
|
-
effect: str,
|
|
224
|
-
*,
|
|
225
|
-
brightness: Optional[int] = None,
|
|
226
|
-
transition: Optional[int] = None,
|
|
227
|
-
) -> None:
|
|
228
|
-
"""Set an effect on the device."""
|
|
229
|
-
raise NotImplementedError()
|
|
230
|
-
# TODO: the code below does to activate the effect but gives no error
|
|
231
|
-
return await self.protocol.query(
|
|
232
|
-
{
|
|
233
|
-
"set_device_info": {
|
|
234
|
-
"dynamic_light_effect_enable": 1,
|
|
235
|
-
"dynamic_light_effect_id": effect,
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
)
|
|
239
|
-
|
|
240
|
-
@property # type: ignore
|
|
241
|
-
def state_information(self) -> Dict[str, Any]:
|
|
242
|
-
"""Return bulb-specific state information."""
|
|
243
|
-
info: Dict[str, Any] = {
|
|
244
|
-
# TODO: re-enable after we don't inherit from smartbulb
|
|
245
|
-
# **super().state_information
|
|
246
|
-
"Is dimmable": self.is_dimmable,
|
|
247
|
-
}
|
|
248
|
-
if self.is_dimmable:
|
|
249
|
-
info["Brightness"] = self.brightness
|
|
250
|
-
if self.is_variable_color_temp:
|
|
251
|
-
info["Color temperature"] = self.color_temp
|
|
252
|
-
info["Valid temperature range"] = self.valid_temperature_range
|
|
253
|
-
if self.is_color:
|
|
254
|
-
info["HSV"] = self.hsv
|
|
255
|
-
info["Presets"] = self.presets
|
|
256
|
-
|
|
257
|
-
return info
|
|
258
|
-
|
|
259
|
-
@property
|
|
260
|
-
def presets(self) -> List[SmartBulbPreset]:
|
|
261
|
-
"""Return a list of available bulb setting presets."""
|
|
262
|
-
return []
|
kasa/tapo/tapodevice.py
DELETED
|
@@ -1,409 +0,0 @@
|
|
|
1
|
-
"""Module for a TAPO device."""
|
|
2
|
-
import base64
|
|
3
|
-
import logging
|
|
4
|
-
from datetime import datetime, timedelta, timezone
|
|
5
|
-
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Set, cast
|
|
6
|
-
|
|
7
|
-
from ..aestransport import AesTransport
|
|
8
|
-
from ..device_type import DeviceType
|
|
9
|
-
from ..deviceconfig import DeviceConfig
|
|
10
|
-
from ..emeterstatus import EmeterStatus
|
|
11
|
-
from ..exceptions import AuthenticationException, SmartDeviceException
|
|
12
|
-
from ..modules import Emeter
|
|
13
|
-
from ..smartdevice import SmartDevice, WifiNetwork
|
|
14
|
-
from ..smartprotocol import SmartProtocol
|
|
15
|
-
|
|
16
|
-
_LOGGER = logging.getLogger(__name__)
|
|
17
|
-
|
|
18
|
-
if TYPE_CHECKING:
|
|
19
|
-
from .childdevice import ChildDevice
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
class TapoDevice(SmartDevice):
|
|
23
|
-
"""Base class to represent a TAPO device."""
|
|
24
|
-
|
|
25
|
-
def __init__(
|
|
26
|
-
self,
|
|
27
|
-
host: str,
|
|
28
|
-
*,
|
|
29
|
-
config: Optional[DeviceConfig] = None,
|
|
30
|
-
protocol: Optional[SmartProtocol] = None,
|
|
31
|
-
) -> None:
|
|
32
|
-
_protocol = protocol or SmartProtocol(
|
|
33
|
-
transport=AesTransport(config=config or DeviceConfig(host=host)),
|
|
34
|
-
)
|
|
35
|
-
super().__init__(host=host, config=config, protocol=_protocol)
|
|
36
|
-
self.protocol: SmartProtocol
|
|
37
|
-
self._components_raw: Optional[Dict[str, Any]] = None
|
|
38
|
-
self._components: Dict[str, int] = {}
|
|
39
|
-
self._children: Dict[str, "ChildDevice"] = {}
|
|
40
|
-
self._energy: Dict[str, Any] = {}
|
|
41
|
-
self._state_information: Dict[str, Any] = {}
|
|
42
|
-
|
|
43
|
-
async def _initialize_children(self):
|
|
44
|
-
"""Initialize children for power strips."""
|
|
45
|
-
children = self._last_update["child_info"]["child_device_list"]
|
|
46
|
-
# TODO: Use the type information to construct children,
|
|
47
|
-
# as hubs can also have them.
|
|
48
|
-
from .childdevice import ChildDevice
|
|
49
|
-
|
|
50
|
-
self._children = {
|
|
51
|
-
child["device_id"]: ChildDevice(parent=self, child_id=child["device_id"])
|
|
52
|
-
for child in children
|
|
53
|
-
}
|
|
54
|
-
self._device_type = DeviceType.Strip
|
|
55
|
-
|
|
56
|
-
@property
|
|
57
|
-
def children(self):
|
|
58
|
-
"""Return list of children.
|
|
59
|
-
|
|
60
|
-
This is just to keep the existing SmartDevice API intact.
|
|
61
|
-
"""
|
|
62
|
-
return list(self._children.values())
|
|
63
|
-
|
|
64
|
-
@children.setter
|
|
65
|
-
def children(self, children):
|
|
66
|
-
"""Initialize from a list of children.
|
|
67
|
-
|
|
68
|
-
This is just to keep the existing SmartDevice API intact.
|
|
69
|
-
"""
|
|
70
|
-
self._children = {child["device_id"]: child for child in children}
|
|
71
|
-
|
|
72
|
-
async def update(self, update_children: bool = True):
|
|
73
|
-
"""Update the device."""
|
|
74
|
-
if self.credentials is None and self.credentials_hash is None:
|
|
75
|
-
raise AuthenticationException("Tapo plug requires authentication.")
|
|
76
|
-
|
|
77
|
-
if self._components_raw is None:
|
|
78
|
-
resp = await self.protocol.query("component_nego")
|
|
79
|
-
self._components_raw = resp["component_nego"]
|
|
80
|
-
self._components = {
|
|
81
|
-
comp["id"]: comp["ver_code"]
|
|
82
|
-
for comp in self._components_raw["component_list"]
|
|
83
|
-
}
|
|
84
|
-
await self._initialize_modules()
|
|
85
|
-
|
|
86
|
-
extra_reqs: Dict[str, Any] = {}
|
|
87
|
-
|
|
88
|
-
if "child_device" in self._components:
|
|
89
|
-
extra_reqs = {**extra_reqs, "get_child_device_list": None}
|
|
90
|
-
|
|
91
|
-
if "energy_monitoring" in self._components:
|
|
92
|
-
extra_reqs = {
|
|
93
|
-
**extra_reqs,
|
|
94
|
-
"get_energy_usage": None,
|
|
95
|
-
"get_current_power": None,
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
req = {
|
|
99
|
-
"get_device_info": None,
|
|
100
|
-
"get_device_usage": None,
|
|
101
|
-
"get_device_time": None,
|
|
102
|
-
**extra_reqs,
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
resp = await self.protocol.query(req)
|
|
106
|
-
|
|
107
|
-
self._info = resp["get_device_info"]
|
|
108
|
-
self._usage = resp["get_device_usage"]
|
|
109
|
-
self._time = resp["get_device_time"]
|
|
110
|
-
# Emeter is not always available, but we set them still for now.
|
|
111
|
-
self._energy = resp.get("get_energy_usage", {})
|
|
112
|
-
self._emeter = resp.get("get_current_power", {})
|
|
113
|
-
|
|
114
|
-
self._last_update = {
|
|
115
|
-
"components": self._components_raw,
|
|
116
|
-
"info": self._info,
|
|
117
|
-
"usage": self._usage,
|
|
118
|
-
"time": self._time,
|
|
119
|
-
"energy": self._energy,
|
|
120
|
-
"emeter": self._emeter,
|
|
121
|
-
"child_info": resp.get("get_child_device_list", {}),
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
if child_info := self._last_update.get("child_info"):
|
|
125
|
-
if not self.children:
|
|
126
|
-
await self._initialize_children()
|
|
127
|
-
for info in child_info["child_device_list"]:
|
|
128
|
-
self._children[info["device_id"]].update_internal_state(info)
|
|
129
|
-
|
|
130
|
-
_LOGGER.debug("Got an update: %s", self._last_update)
|
|
131
|
-
|
|
132
|
-
async def _initialize_modules(self):
|
|
133
|
-
"""Initialize modules based on component negotiation response."""
|
|
134
|
-
if "energy_monitoring" in self._components:
|
|
135
|
-
self.emeter_type = "emeter"
|
|
136
|
-
self.modules["emeter"] = Emeter(self, self.emeter_type)
|
|
137
|
-
|
|
138
|
-
@property
|
|
139
|
-
def sys_info(self) -> Dict[str, Any]:
|
|
140
|
-
"""Returns the device info."""
|
|
141
|
-
return self._info # type: ignore
|
|
142
|
-
|
|
143
|
-
@property
|
|
144
|
-
def model(self) -> str:
|
|
145
|
-
"""Returns the device model."""
|
|
146
|
-
return str(self._info.get("model"))
|
|
147
|
-
|
|
148
|
-
@property
|
|
149
|
-
def alias(self) -> Optional[str]:
|
|
150
|
-
"""Returns the device alias or nickname."""
|
|
151
|
-
if self._info and (nickname := self._info.get("nickname")):
|
|
152
|
-
return base64.b64decode(nickname).decode()
|
|
153
|
-
else:
|
|
154
|
-
return None
|
|
155
|
-
|
|
156
|
-
@property
|
|
157
|
-
def time(self) -> datetime:
|
|
158
|
-
"""Return the time."""
|
|
159
|
-
td = timedelta(minutes=cast(float, self._time.get("time_diff")))
|
|
160
|
-
if self._time.get("region"):
|
|
161
|
-
tz = timezone(td, str(self._time.get("region")))
|
|
162
|
-
else:
|
|
163
|
-
# in case the device returns a blank region this will result in the
|
|
164
|
-
# tzname being a UTC offset
|
|
165
|
-
tz = timezone(td)
|
|
166
|
-
return datetime.fromtimestamp(
|
|
167
|
-
cast(float, self._time.get("timestamp")),
|
|
168
|
-
tz=tz,
|
|
169
|
-
)
|
|
170
|
-
|
|
171
|
-
@property
|
|
172
|
-
def timezone(self) -> Dict:
|
|
173
|
-
"""Return the timezone and time_difference."""
|
|
174
|
-
ti = self.time
|
|
175
|
-
return {"timezone": ti.tzname()}
|
|
176
|
-
|
|
177
|
-
@property
|
|
178
|
-
def hw_info(self) -> Dict:
|
|
179
|
-
"""Return hardware info for the device."""
|
|
180
|
-
return {
|
|
181
|
-
"sw_ver": self._info.get("fw_ver"),
|
|
182
|
-
"hw_ver": self._info.get("hw_ver"),
|
|
183
|
-
"mac": self._info.get("mac"),
|
|
184
|
-
"type": self._info.get("type"),
|
|
185
|
-
"hwId": self._info.get("device_id"),
|
|
186
|
-
"dev_name": self.alias,
|
|
187
|
-
"oemId": self._info.get("oem_id"),
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
@property
|
|
191
|
-
def location(self) -> Dict:
|
|
192
|
-
"""Return the device location."""
|
|
193
|
-
loc = {
|
|
194
|
-
"latitude": cast(float, self._info.get("latitude", 0)) / 10_000,
|
|
195
|
-
"longitude": cast(float, self._info.get("longitude", 0)) / 10_000,
|
|
196
|
-
}
|
|
197
|
-
return loc
|
|
198
|
-
|
|
199
|
-
@property
|
|
200
|
-
def rssi(self) -> Optional[int]:
|
|
201
|
-
"""Return the rssi."""
|
|
202
|
-
rssi = self._info.get("rssi")
|
|
203
|
-
return int(rssi) if rssi else None
|
|
204
|
-
|
|
205
|
-
@property
|
|
206
|
-
def mac(self) -> str:
|
|
207
|
-
"""Return the mac formatted with colons."""
|
|
208
|
-
return str(self._info.get("mac")).replace("-", ":")
|
|
209
|
-
|
|
210
|
-
@property
|
|
211
|
-
def device_id(self) -> str:
|
|
212
|
-
"""Return the device id."""
|
|
213
|
-
return str(self._info.get("device_id"))
|
|
214
|
-
|
|
215
|
-
@property
|
|
216
|
-
def internal_state(self) -> Any:
|
|
217
|
-
"""Return all the internal state data."""
|
|
218
|
-
return self._last_update
|
|
219
|
-
|
|
220
|
-
async def _query_helper(
|
|
221
|
-
self, target: str, cmd: str, arg: Optional[Dict] = None, child_ids=None
|
|
222
|
-
) -> Any:
|
|
223
|
-
res = await self.protocol.query({cmd: arg})
|
|
224
|
-
|
|
225
|
-
return res
|
|
226
|
-
|
|
227
|
-
@property
|
|
228
|
-
def state_information(self) -> Dict[str, Any]:
|
|
229
|
-
"""Return the key state information."""
|
|
230
|
-
ssid = self._info.get("ssid")
|
|
231
|
-
ssid = base64.b64decode(ssid).decode() if ssid else "No SSID"
|
|
232
|
-
|
|
233
|
-
return {
|
|
234
|
-
"overheated": self._info.get("overheated"),
|
|
235
|
-
"signal_level": self._info.get("signal_level"),
|
|
236
|
-
"SSID": ssid,
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
@property
|
|
240
|
-
def features(self) -> Set[str]:
|
|
241
|
-
"""Return the list of supported features."""
|
|
242
|
-
# TODO:
|
|
243
|
-
return set()
|
|
244
|
-
|
|
245
|
-
@property
|
|
246
|
-
def has_emeter(self) -> bool:
|
|
247
|
-
"""Return if the device has emeter."""
|
|
248
|
-
return "energy_monitoring" in self._components
|
|
249
|
-
|
|
250
|
-
@property
|
|
251
|
-
def is_on(self) -> bool:
|
|
252
|
-
"""Return true if the device is on."""
|
|
253
|
-
return bool(self._info.get("device_on"))
|
|
254
|
-
|
|
255
|
-
async def turn_on(self, **kwargs):
|
|
256
|
-
"""Turn on the device."""
|
|
257
|
-
await self.protocol.query({"set_device_info": {"device_on": True}})
|
|
258
|
-
|
|
259
|
-
async def turn_off(self, **kwargs):
|
|
260
|
-
"""Turn off the device."""
|
|
261
|
-
await self.protocol.query({"set_device_info": {"device_on": False}})
|
|
262
|
-
|
|
263
|
-
def update_from_discover_info(self, info):
|
|
264
|
-
"""Update state from info from the discover call."""
|
|
265
|
-
self._discovery_info = info
|
|
266
|
-
self._info = info
|
|
267
|
-
|
|
268
|
-
async def get_emeter_realtime(self) -> EmeterStatus:
|
|
269
|
-
"""Retrieve current energy readings."""
|
|
270
|
-
self._verify_emeter()
|
|
271
|
-
resp = await self.protocol.query("get_energy_usage")
|
|
272
|
-
self._energy = resp["get_energy_usage"]
|
|
273
|
-
return self.emeter_realtime
|
|
274
|
-
|
|
275
|
-
def _convert_energy_data(self, data, scale) -> Optional[float]:
|
|
276
|
-
"""Return adjusted emeter information."""
|
|
277
|
-
return data if not data else data * scale
|
|
278
|
-
|
|
279
|
-
@property
|
|
280
|
-
def emeter_realtime(self) -> EmeterStatus:
|
|
281
|
-
"""Get the emeter status."""
|
|
282
|
-
return EmeterStatus(
|
|
283
|
-
{
|
|
284
|
-
"power_mw": self._energy.get("current_power"),
|
|
285
|
-
"total": self._convert_energy_data(
|
|
286
|
-
self._energy.get("today_energy"), 1 / 1000
|
|
287
|
-
),
|
|
288
|
-
}
|
|
289
|
-
)
|
|
290
|
-
|
|
291
|
-
@property
|
|
292
|
-
def emeter_this_month(self) -> Optional[float]:
|
|
293
|
-
"""Get the emeter value for this month."""
|
|
294
|
-
return self._convert_energy_data(self._energy.get("month_energy"), 1 / 1000)
|
|
295
|
-
|
|
296
|
-
@property
|
|
297
|
-
def emeter_today(self) -> Optional[float]:
|
|
298
|
-
"""Get the emeter value for today."""
|
|
299
|
-
return self._convert_energy_data(self._energy.get("today_energy"), 1 / 1000)
|
|
300
|
-
|
|
301
|
-
async def wifi_scan(self) -> List[WifiNetwork]:
|
|
302
|
-
"""Scan for available wifi networks."""
|
|
303
|
-
|
|
304
|
-
def _net_for_scan_info(res):
|
|
305
|
-
return WifiNetwork(
|
|
306
|
-
ssid=base64.b64decode(res["ssid"]).decode(),
|
|
307
|
-
cipher_type=res["cipher_type"],
|
|
308
|
-
key_type=res["key_type"],
|
|
309
|
-
channel=res["channel"],
|
|
310
|
-
signal_level=res["signal_level"],
|
|
311
|
-
bssid=res["bssid"],
|
|
312
|
-
)
|
|
313
|
-
|
|
314
|
-
async def _query_networks(networks=None, start_index=0):
|
|
315
|
-
_LOGGER.debug("Querying networks using start_index=%s", start_index)
|
|
316
|
-
if networks is None:
|
|
317
|
-
networks = []
|
|
318
|
-
|
|
319
|
-
resp = await self.protocol.query(
|
|
320
|
-
{"get_wireless_scan_info": {"start_index": start_index}}
|
|
321
|
-
)
|
|
322
|
-
network_list = [
|
|
323
|
-
_net_for_scan_info(net)
|
|
324
|
-
for net in resp["get_wireless_scan_info"]["ap_list"]
|
|
325
|
-
]
|
|
326
|
-
networks.extend(network_list)
|
|
327
|
-
|
|
328
|
-
if resp["get_wireless_scan_info"].get("sum", 0) > start_index + 10:
|
|
329
|
-
return await _query_networks(networks, start_index=start_index + 10)
|
|
330
|
-
|
|
331
|
-
return networks
|
|
332
|
-
|
|
333
|
-
return await _query_networks()
|
|
334
|
-
|
|
335
|
-
async def wifi_join(self, ssid: str, password: str, keytype: str = "wpa2_psk"):
|
|
336
|
-
"""Join the given wifi network.
|
|
337
|
-
|
|
338
|
-
This method returns nothing as the device tries to activate the new
|
|
339
|
-
settings immediately instead of responding to the request.
|
|
340
|
-
|
|
341
|
-
If joining the network fails, the device will return to the previous state
|
|
342
|
-
after some delay.
|
|
343
|
-
"""
|
|
344
|
-
if not self.credentials:
|
|
345
|
-
raise AuthenticationException("Device requires authentication.")
|
|
346
|
-
|
|
347
|
-
payload = {
|
|
348
|
-
"account": {
|
|
349
|
-
"username": base64.b64encode(
|
|
350
|
-
self.credentials.username.encode()
|
|
351
|
-
).decode(),
|
|
352
|
-
"password": base64.b64encode(
|
|
353
|
-
self.credentials.password.encode()
|
|
354
|
-
).decode(),
|
|
355
|
-
},
|
|
356
|
-
"wireless": {
|
|
357
|
-
"key_type": keytype,
|
|
358
|
-
"password": base64.b64encode(password.encode()).decode(),
|
|
359
|
-
"ssid": base64.b64encode(ssid.encode()).decode(),
|
|
360
|
-
},
|
|
361
|
-
"time": self.internal_state["time"],
|
|
362
|
-
}
|
|
363
|
-
|
|
364
|
-
# The device does not respond to the request but changes the settings
|
|
365
|
-
# immediately which causes us to timeout.
|
|
366
|
-
# Thus, We limit retries and suppress the raised exception as useless.
|
|
367
|
-
try:
|
|
368
|
-
return await self.protocol.query({"set_qs_info": payload}, retry_count=0)
|
|
369
|
-
except SmartDeviceException as ex:
|
|
370
|
-
if ex.error_code: # Re-raise on device-reported errors
|
|
371
|
-
raise
|
|
372
|
-
|
|
373
|
-
_LOGGER.debug("Received an expected for wifi join, but this is expected")
|
|
374
|
-
|
|
375
|
-
async def update_credentials(self, username: str, password: str):
|
|
376
|
-
"""Update device credentials.
|
|
377
|
-
|
|
378
|
-
This will replace the existing authentication credentials on the device.
|
|
379
|
-
"""
|
|
380
|
-
t = self.internal_state["time"]
|
|
381
|
-
payload = {
|
|
382
|
-
"account": {
|
|
383
|
-
"username": base64.b64encode(username.encode()).decode(),
|
|
384
|
-
"password": base64.b64encode(password.encode()).decode(),
|
|
385
|
-
},
|
|
386
|
-
"time": t,
|
|
387
|
-
}
|
|
388
|
-
return await self.protocol.query({"set_qs_info": payload})
|
|
389
|
-
|
|
390
|
-
async def set_alias(self, alias: str):
|
|
391
|
-
"""Set the device name (alias)."""
|
|
392
|
-
return await self.protocol.query(
|
|
393
|
-
{"set_device_info": {"nickname": base64.b64encode(alias.encode()).decode()}}
|
|
394
|
-
)
|
|
395
|
-
|
|
396
|
-
async def reboot(self, delay: int = 1) -> None:
|
|
397
|
-
"""Reboot the device.
|
|
398
|
-
|
|
399
|
-
Note that giving a delay of zero causes this to block,
|
|
400
|
-
as the device reboots immediately without responding to the call.
|
|
401
|
-
"""
|
|
402
|
-
await self.protocol.query({"device_reboot": {"delay": delay}})
|
|
403
|
-
|
|
404
|
-
async def factory_reset(self) -> None:
|
|
405
|
-
"""Reset device back to factory settings.
|
|
406
|
-
|
|
407
|
-
Note, this does not downgrade the firmware.
|
|
408
|
-
"""
|
|
409
|
-
await self.protocol.query("device_reset")
|