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
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
"""Base implementation for SMART modules."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import logging
|
|
6
|
+
from typing import TYPE_CHECKING
|
|
7
|
+
|
|
8
|
+
from ..exceptions import KasaException
|
|
9
|
+
from ..module import Module
|
|
10
|
+
|
|
11
|
+
if TYPE_CHECKING:
|
|
12
|
+
from .smartdevice import SmartDevice
|
|
13
|
+
|
|
14
|
+
_LOGGER = logging.getLogger(__name__)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class SmartModule(Module):
|
|
18
|
+
"""Base class for SMART modules."""
|
|
19
|
+
|
|
20
|
+
NAME: str
|
|
21
|
+
#: Module is initialized, if the given component is available
|
|
22
|
+
REQUIRED_COMPONENT: str | None = None
|
|
23
|
+
#: Module is initialized, if the given key available in the main sysinfo
|
|
24
|
+
REQUIRED_KEY_ON_PARENT: str | None = None
|
|
25
|
+
#: Query to execute during the main update cycle
|
|
26
|
+
QUERY_GETTER_NAME: str
|
|
27
|
+
|
|
28
|
+
REGISTERED_MODULES: dict[str, type[SmartModule]] = {}
|
|
29
|
+
|
|
30
|
+
def __init__(self, device: SmartDevice, module: str):
|
|
31
|
+
self._device: SmartDevice
|
|
32
|
+
super().__init__(device, module)
|
|
33
|
+
|
|
34
|
+
def __init_subclass__(cls, **kwargs):
|
|
35
|
+
name = getattr(cls, "NAME", cls.__name__)
|
|
36
|
+
_LOGGER.debug("Registering %s" % cls)
|
|
37
|
+
cls.REGISTERED_MODULES[name] = cls
|
|
38
|
+
|
|
39
|
+
@property
|
|
40
|
+
def name(self) -> str:
|
|
41
|
+
"""Name of the module."""
|
|
42
|
+
return getattr(self, "NAME", self.__class__.__name__)
|
|
43
|
+
|
|
44
|
+
def query(self) -> dict:
|
|
45
|
+
"""Query to execute during the update cycle.
|
|
46
|
+
|
|
47
|
+
Default implementation uses the raw query getter w/o parameters.
|
|
48
|
+
"""
|
|
49
|
+
return {self.QUERY_GETTER_NAME: None}
|
|
50
|
+
|
|
51
|
+
def call(self, method, params=None):
|
|
52
|
+
"""Call a method.
|
|
53
|
+
|
|
54
|
+
Just a helper method.
|
|
55
|
+
"""
|
|
56
|
+
return self._device._query_helper(method, params)
|
|
57
|
+
|
|
58
|
+
@property
|
|
59
|
+
def data(self):
|
|
60
|
+
"""Return response data for the module.
|
|
61
|
+
|
|
62
|
+
If the module performs only a single query, the resulting response is unwrapped.
|
|
63
|
+
If the module does not define a query, this property returns a reference
|
|
64
|
+
to the main "get_device_info" response.
|
|
65
|
+
"""
|
|
66
|
+
dev = self._device
|
|
67
|
+
q = self.query()
|
|
68
|
+
|
|
69
|
+
if not q:
|
|
70
|
+
return dev.sys_info
|
|
71
|
+
|
|
72
|
+
q_keys = list(q.keys())
|
|
73
|
+
query_key = q_keys[0]
|
|
74
|
+
|
|
75
|
+
# TODO: hacky way to check if update has been called.
|
|
76
|
+
# The way this falls back to parent may not always be wanted.
|
|
77
|
+
# Especially, devices can have their own firmware updates.
|
|
78
|
+
if query_key not in dev._last_update:
|
|
79
|
+
if dev._parent and query_key in dev._parent._last_update:
|
|
80
|
+
_LOGGER.debug("%s not found child, but found on parent", query_key)
|
|
81
|
+
dev = dev._parent
|
|
82
|
+
else:
|
|
83
|
+
raise KasaException(
|
|
84
|
+
f"You need to call update() prior accessing module data"
|
|
85
|
+
f" for '{self._module}'"
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
filtered_data = {k: v for k, v in dev._last_update.items() if k in q_keys}
|
|
89
|
+
|
|
90
|
+
if len(filtered_data) == 1:
|
|
91
|
+
return next(iter(filtered_data.values()))
|
|
92
|
+
|
|
93
|
+
return filtered_data
|
|
94
|
+
|
|
95
|
+
@property
|
|
96
|
+
def supported_version(self) -> int:
|
|
97
|
+
"""Return version supported by the device.
|
|
98
|
+
|
|
99
|
+
If the module has no required component, this will return -1.
|
|
100
|
+
"""
|
|
101
|
+
if self.REQUIRED_COMPONENT is not None:
|
|
102
|
+
return self._device._components[self.REQUIRED_COMPONENT]
|
|
103
|
+
return -1
|
|
104
|
+
|
|
105
|
+
async def _check_supported(self) -> bool:
|
|
106
|
+
"""Additional check to see if the module is supported by the device.
|
|
107
|
+
|
|
108
|
+
Used for parents who report components on the parent that are only available
|
|
109
|
+
on the child or for modules where the device has a pointless component like
|
|
110
|
+
color_temp_range but only supports one value.
|
|
111
|
+
"""
|
|
112
|
+
return True
|
kasa/smartprotocol.py
CHANGED
|
@@ -4,24 +4,26 @@ Based on the work of https://github.com/petretiandrea/plugp100
|
|
|
4
4
|
under compatible GNU GPL3 license.
|
|
5
5
|
"""
|
|
6
6
|
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
7
9
|
import asyncio
|
|
8
10
|
import base64
|
|
9
11
|
import logging
|
|
10
12
|
import time
|
|
11
13
|
import uuid
|
|
12
14
|
from pprint import pformat as pf
|
|
13
|
-
from typing import Any
|
|
15
|
+
from typing import Any
|
|
14
16
|
|
|
15
17
|
from .exceptions import (
|
|
16
18
|
SMART_AUTHENTICATION_ERRORS,
|
|
17
19
|
SMART_RETRYABLE_ERRORS,
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
RetryableException,
|
|
22
|
-
SmartDeviceException,
|
|
20
|
+
AuthenticationError,
|
|
21
|
+
DeviceError,
|
|
22
|
+
KasaException,
|
|
23
23
|
SmartErrorCode,
|
|
24
|
-
|
|
24
|
+
TimeoutError,
|
|
25
|
+
_ConnectionError,
|
|
26
|
+
_RetryableError,
|
|
25
27
|
)
|
|
26
28
|
from .json import dumps as json_dumps
|
|
27
29
|
from .protocol import BaseProtocol, BaseTransport, md5
|
|
@@ -57,40 +59,43 @@ class SmartProtocol(BaseProtocol):
|
|
|
57
59
|
}
|
|
58
60
|
return json_dumps(request)
|
|
59
61
|
|
|
60
|
-
async def query(self, request:
|
|
62
|
+
async def query(self, request: str | dict, retry_count: int = 3) -> dict:
|
|
61
63
|
"""Query the device retrying for retry_count on failure."""
|
|
62
64
|
async with self._query_lock:
|
|
63
65
|
return await self._query(request, retry_count)
|
|
64
66
|
|
|
65
|
-
async def _query(self, request:
|
|
67
|
+
async def _query(self, request: str | dict, retry_count: int = 3) -> dict:
|
|
66
68
|
for retry in range(retry_count + 1):
|
|
67
69
|
try:
|
|
68
|
-
return await self._execute_query(
|
|
69
|
-
|
|
70
|
+
return await self._execute_query(
|
|
71
|
+
request, retry_count=retry, iterate_list_pages=True
|
|
72
|
+
)
|
|
73
|
+
except _ConnectionError as sdex:
|
|
70
74
|
if retry >= retry_count:
|
|
71
75
|
_LOGGER.debug("Giving up on %s after %s retries", self._host, retry)
|
|
72
76
|
raise sdex
|
|
73
77
|
continue
|
|
74
|
-
except
|
|
78
|
+
except AuthenticationError as auex:
|
|
75
79
|
await self._transport.reset()
|
|
76
80
|
_LOGGER.debug(
|
|
77
81
|
"Unable to authenticate with %s, not retrying", self._host
|
|
78
82
|
)
|
|
79
83
|
raise auex
|
|
80
|
-
except
|
|
84
|
+
except _RetryableError as ex:
|
|
81
85
|
await self._transport.reset()
|
|
82
86
|
if retry >= retry_count:
|
|
83
87
|
_LOGGER.debug("Giving up on %s after %s retries", self._host, retry)
|
|
84
88
|
raise ex
|
|
89
|
+
await asyncio.sleep(self.BACKOFF_SECONDS_AFTER_TIMEOUT)
|
|
85
90
|
continue
|
|
86
|
-
except
|
|
91
|
+
except TimeoutError as ex:
|
|
87
92
|
await self._transport.reset()
|
|
88
93
|
if retry >= retry_count:
|
|
89
94
|
_LOGGER.debug("Giving up on %s after %s retries", self._host, retry)
|
|
90
95
|
raise ex
|
|
91
96
|
await asyncio.sleep(self.BACKOFF_SECONDS_AFTER_TIMEOUT)
|
|
92
97
|
continue
|
|
93
|
-
except
|
|
98
|
+
except KasaException as ex:
|
|
94
99
|
await self._transport.reset()
|
|
95
100
|
_LOGGER.debug(
|
|
96
101
|
"Unable to query the device: %s, not retrying: %s",
|
|
@@ -100,23 +105,23 @@ class SmartProtocol(BaseProtocol):
|
|
|
100
105
|
raise ex
|
|
101
106
|
|
|
102
107
|
# make mypy happy, this should never be reached..
|
|
103
|
-
raise
|
|
108
|
+
raise KasaException("Query reached somehow to unreachable")
|
|
104
109
|
|
|
105
|
-
async def _execute_multiple_query(self,
|
|
110
|
+
async def _execute_multiple_query(self, requests: dict, retry_count: int) -> dict:
|
|
106
111
|
debug_enabled = _LOGGER.isEnabledFor(logging.DEBUG)
|
|
107
|
-
multi_result:
|
|
112
|
+
multi_result: dict[str, Any] = {}
|
|
108
113
|
smart_method = "multipleRequest"
|
|
109
|
-
|
|
110
|
-
{"method": method, "params": params} for method, params in
|
|
114
|
+
multi_requests = [
|
|
115
|
+
{"method": method, "params": params} for method, params in requests.items()
|
|
111
116
|
]
|
|
112
117
|
|
|
113
|
-
end = len(
|
|
118
|
+
end = len(multi_requests)
|
|
114
119
|
# Break the requests down as there can be a size limit
|
|
115
120
|
step = (
|
|
116
121
|
self._transport._config.batch_size or self.DEFAULT_MULTI_REQUEST_BATCH_SIZE
|
|
117
122
|
)
|
|
118
123
|
for i in range(0, end, step):
|
|
119
|
-
requests_step =
|
|
124
|
+
requests_step = multi_requests[i : i + step]
|
|
120
125
|
|
|
121
126
|
smart_params = {"requests": requests_step}
|
|
122
127
|
smart_request = self.get_smart_request(smart_method, smart_params)
|
|
@@ -128,22 +133,37 @@ class SmartProtocol(BaseProtocol):
|
|
|
128
133
|
pf(smart_request),
|
|
129
134
|
)
|
|
130
135
|
response_step = await self._transport.send(smart_request)
|
|
136
|
+
batch_name = f"multi-request-batch-{i+1}"
|
|
131
137
|
if debug_enabled:
|
|
132
138
|
_LOGGER.debug(
|
|
133
|
-
"%s
|
|
139
|
+
"%s %s << %s",
|
|
134
140
|
self._host,
|
|
135
|
-
|
|
141
|
+
batch_name,
|
|
136
142
|
pf(response_step),
|
|
137
143
|
)
|
|
138
|
-
self._handle_response_error_code(response_step)
|
|
144
|
+
self._handle_response_error_code(response_step, batch_name)
|
|
139
145
|
responses = response_step["result"]["responses"]
|
|
140
146
|
for response in responses:
|
|
141
|
-
|
|
147
|
+
method = response["method"]
|
|
148
|
+
self._handle_response_error_code(response, method, raise_on_error=False)
|
|
142
149
|
result = response.get("result", None)
|
|
143
|
-
|
|
150
|
+
await self._handle_response_lists(
|
|
151
|
+
result, method, retry_count=retry_count
|
|
152
|
+
)
|
|
153
|
+
multi_result[method] = result
|
|
154
|
+
# Multi requests don't continue after errors so requery any missing
|
|
155
|
+
for method, params in requests.items():
|
|
156
|
+
if method not in multi_result:
|
|
157
|
+
resp = await self._transport.send(
|
|
158
|
+
self.get_smart_request(method, params)
|
|
159
|
+
)
|
|
160
|
+
self._handle_response_error_code(resp, method, raise_on_error=False)
|
|
161
|
+
multi_result[method] = resp.get("result")
|
|
144
162
|
return multi_result
|
|
145
163
|
|
|
146
|
-
async def _execute_query(
|
|
164
|
+
async def _execute_query(
|
|
165
|
+
self, request: str | dict, *, retry_count: int, iterate_list_pages: bool = True
|
|
166
|
+
) -> dict:
|
|
147
167
|
debug_enabled = _LOGGER.isEnabledFor(logging.DEBUG)
|
|
148
168
|
|
|
149
169
|
if isinstance(request, dict):
|
|
@@ -172,29 +192,69 @@ class SmartProtocol(BaseProtocol):
|
|
|
172
192
|
pf(response_data),
|
|
173
193
|
)
|
|
174
194
|
|
|
175
|
-
self._handle_response_error_code(response_data)
|
|
195
|
+
self._handle_response_error_code(response_data, smart_method)
|
|
176
196
|
|
|
177
197
|
# Single set_ requests do not return a result
|
|
178
198
|
result = response_data.get("result")
|
|
199
|
+
if iterate_list_pages and result:
|
|
200
|
+
await self._handle_response_lists(
|
|
201
|
+
result, smart_method, retry_count=retry_count
|
|
202
|
+
)
|
|
179
203
|
return {smart_method: result}
|
|
180
204
|
|
|
181
|
-
def
|
|
205
|
+
async def _handle_response_lists(
|
|
206
|
+
self, response_result: dict[str, Any], method, retry_count
|
|
207
|
+
):
|
|
208
|
+
if (
|
|
209
|
+
response_result is None
|
|
210
|
+
or isinstance(response_result, SmartErrorCode)
|
|
211
|
+
or "start_index" not in response_result
|
|
212
|
+
or (list_sum := response_result.get("sum")) is None
|
|
213
|
+
):
|
|
214
|
+
return
|
|
215
|
+
|
|
216
|
+
response_list_name = next(
|
|
217
|
+
iter(
|
|
218
|
+
[
|
|
219
|
+
key
|
|
220
|
+
for key in response_result
|
|
221
|
+
if isinstance(response_result[key], list)
|
|
222
|
+
]
|
|
223
|
+
)
|
|
224
|
+
)
|
|
225
|
+
while (list_length := len(response_result[response_list_name])) < list_sum:
|
|
226
|
+
response = await self._execute_query(
|
|
227
|
+
{method: {"start_index": list_length}},
|
|
228
|
+
retry_count=retry_count,
|
|
229
|
+
iterate_list_pages=False,
|
|
230
|
+
)
|
|
231
|
+
next_batch = response[method]
|
|
232
|
+
# In case the device returns empty lists avoid infinite looping
|
|
233
|
+
if not next_batch[response_list_name]:
|
|
234
|
+
_LOGGER.error(
|
|
235
|
+
f"Device {self._host} returned empty "
|
|
236
|
+
+ f"results list for method {method}"
|
|
237
|
+
)
|
|
238
|
+
break
|
|
239
|
+
response_result[response_list_name].extend(next_batch[response_list_name])
|
|
240
|
+
|
|
241
|
+
def _handle_response_error_code(self, resp_dict: dict, method, raise_on_error=True):
|
|
182
242
|
error_code = SmartErrorCode(resp_dict.get("error_code")) # type: ignore[arg-type]
|
|
183
243
|
if error_code == SmartErrorCode.SUCCESS:
|
|
184
244
|
return
|
|
245
|
+
if not raise_on_error:
|
|
246
|
+
resp_dict["result"] = error_code
|
|
247
|
+
return
|
|
185
248
|
msg = (
|
|
186
249
|
f"Error querying device: {self._host}: "
|
|
187
250
|
+ f"{error_code.name}({error_code.value})"
|
|
251
|
+
+ f" for method: {method}"
|
|
188
252
|
)
|
|
189
|
-
if method := resp_dict.get("method"):
|
|
190
|
-
msg += f" for method: {method}"
|
|
191
|
-
if error_code in SMART_TIMEOUT_ERRORS:
|
|
192
|
-
raise TimeoutException(msg, error_code=error_code)
|
|
193
253
|
if error_code in SMART_RETRYABLE_ERRORS:
|
|
194
|
-
raise
|
|
254
|
+
raise _RetryableError(msg, error_code=error_code)
|
|
195
255
|
if error_code in SMART_AUTHENTICATION_ERRORS:
|
|
196
|
-
raise
|
|
197
|
-
raise
|
|
256
|
+
raise AuthenticationError(msg, error_code=error_code)
|
|
257
|
+
raise DeviceError(msg, error_code=error_code)
|
|
198
258
|
|
|
199
259
|
async def close(self) -> None:
|
|
200
260
|
"""Close the underlying transport."""
|
|
@@ -319,7 +379,7 @@ class _ChildProtocolWrapper(SmartProtocol):
|
|
|
319
379
|
|
|
320
380
|
return smart_method, smart_params
|
|
321
381
|
|
|
322
|
-
async def query(self, request:
|
|
382
|
+
async def query(self, request: str | dict, retry_count: int = 3) -> dict:
|
|
323
383
|
"""Wrap request inside control_child envelope."""
|
|
324
384
|
method, params = self._get_method_and_params_for_request(request)
|
|
325
385
|
request_data = {
|
|
@@ -337,10 +397,16 @@ class _ChildProtocolWrapper(SmartProtocol):
|
|
|
337
397
|
result = response.get("control_child")
|
|
338
398
|
# Unwrap responseData for control_child
|
|
339
399
|
if result and (response_data := result.get("responseData")):
|
|
340
|
-
self._handle_response_error_code(response_data)
|
|
341
400
|
result = response_data.get("result")
|
|
342
|
-
|
|
343
|
-
|
|
401
|
+
if result and (multi_responses := result.get("responses")):
|
|
402
|
+
ret_val = {}
|
|
403
|
+
for multi_response in multi_responses:
|
|
404
|
+
method = multi_response["method"]
|
|
405
|
+
self._handle_response_error_code(multi_response, method)
|
|
406
|
+
ret_val[method] = multi_response.get("result")
|
|
407
|
+
return ret_val
|
|
408
|
+
|
|
409
|
+
self._handle_response_error_code(response_data, "control_child")
|
|
344
410
|
|
|
345
411
|
return {method: result}
|
|
346
412
|
|