pyimouapi 1.4.1__tar.gz → 1.4.4__tar.gz
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.
- {pyimouapi-1.4.1 → pyimouapi-1.4.4}/PKG-INFO +1 -1
- {pyimouapi-1.4.1 → pyimouapi-1.4.4}/pyimouapi/__init__.py +1 -1
- pyimouapi-1.4.4/pyimouapi/bulb_light.py +201 -0
- {pyimouapi-1.4.1 → pyimouapi-1.4.4}/pyimouapi/const.py +13 -1
- {pyimouapi-1.4.1 → pyimouapi-1.4.4}/pyimouapi/device.py +39 -11
- pyimouapi-1.4.4/pyimouapi/dormant_retry.py +70 -0
- {pyimouapi-1.4.1 → pyimouapi-1.4.4}/pyimouapi/ha_device.py +354 -104
- {pyimouapi-1.4.1 → pyimouapi-1.4.4}/pyimouapi/openapi.py +49 -11
- {pyimouapi-1.4.1 → pyimouapi-1.4.4}/pyimouapi.egg-info/PKG-INFO +1 -1
- {pyimouapi-1.4.1 → pyimouapi-1.4.4}/pyimouapi.egg-info/SOURCES.txt +5 -0
- {pyimouapi-1.4.1 → pyimouapi-1.4.4}/pyproject.toml +1 -1
- {pyimouapi-1.4.1 → pyimouapi-1.4.4}/setup.py +1 -1
- pyimouapi-1.4.4/tests/test_alarm_pir_switch.py +37 -0
- pyimouapi-1.4.4/tests/test_bulb_light.py +252 -0
- pyimouapi-1.4.4/tests/test_dormant_write_retry.py +120 -0
- {pyimouapi-1.4.1 → pyimouapi-1.4.4}/tests/test_event_identifier_map.py +10 -1
- {pyimouapi-1.4.1 → pyimouapi-1.4.4}/tests/test_siren.py +2 -2
- {pyimouapi-1.4.1 → pyimouapi-1.4.4}/tests/test_switch_status_aggregation.py +5 -4
- {pyimouapi-1.4.1 → pyimouapi-1.4.4}/LICENSE +0 -0
- {pyimouapi-1.4.1 → pyimouapi-1.4.4}/README.md +0 -0
- {pyimouapi-1.4.1 → pyimouapi-1.4.4}/pyimouapi/alarm_mode.py +0 -0
- {pyimouapi-1.4.1 → pyimouapi-1.4.4}/pyimouapi/collection_point.py +0 -0
- {pyimouapi-1.4.1 → pyimouapi-1.4.4}/pyimouapi/exceptions.py +0 -0
- {pyimouapi-1.4.1 → pyimouapi-1.4.4}/pyimouapi/pic_decode.py +0 -0
- {pyimouapi-1.4.1 → pyimouapi-1.4.4}/pyimouapi/push.py +0 -0
- {pyimouapi-1.4.1 → pyimouapi-1.4.4}/pyimouapi/py.typed +0 -0
- {pyimouapi-1.4.1 → pyimouapi-1.4.4}/pyimouapi/select_option.py +0 -0
- {pyimouapi-1.4.1 → pyimouapi-1.4.4}/pyimouapi/sensor.py +0 -0
- {pyimouapi-1.4.1 → pyimouapi-1.4.4}/pyimouapi/siren.py +0 -0
- {pyimouapi-1.4.1 → pyimouapi-1.4.4}/pyimouapi.egg-info/dependency_links.txt +0 -0
- {pyimouapi-1.4.1 → pyimouapi-1.4.4}/pyimouapi.egg-info/not-zip-safe +0 -0
- {pyimouapi-1.4.1 → pyimouapi-1.4.4}/pyimouapi.egg-info/requires.txt +0 -0
- {pyimouapi-1.4.1 → pyimouapi-1.4.4}/pyimouapi.egg-info/top_level.txt +0 -0
- {pyimouapi-1.4.1 → pyimouapi-1.4.4}/setup.cfg +0 -0
- {pyimouapi-1.4.1 → pyimouapi-1.4.4}/tests/test_accessory_device_id.py +0 -0
- {pyimouapi-1.4.1 → pyimouapi-1.4.4}/tests/test_alarm_control_panel.py +0 -0
- {pyimouapi-1.4.1 → pyimouapi-1.4.4}/tests/test_alarm_mode.py +0 -0
- {pyimouapi-1.4.1 → pyimouapi-1.4.4}/tests/test_auth_failure_propagates.py +0 -0
- {pyimouapi-1.4.1 → pyimouapi-1.4.4}/tests/test_bind_device.py +0 -0
- {pyimouapi-1.4.1 → pyimouapi-1.4.4}/tests/test_channel_id_and_api_guards.py +0 -0
- {pyimouapi-1.4.1 → pyimouapi-1.4.4}/tests/test_collect_property_entities.py +0 -0
- {pyimouapi-1.4.1 → pyimouapi-1.4.4}/tests/test_collection_point.py +0 -0
- {pyimouapi-1.4.1 → pyimouapi-1.4.4}/tests/test_connection_pool.py +0 -0
- {pyimouapi-1.4.1 → pyimouapi-1.4.4}/tests/test_device_image.py +0 -0
- {pyimouapi-1.4.1 → pyimouapi-1.4.4}/tests/test_device_stream.py +0 -0
- {pyimouapi-1.4.1 → pyimouapi-1.4.4}/tests/test_device_summaries.py +0 -0
- {pyimouapi-1.4.1 → pyimouapi-1.4.4}/tests/test_entity_tables_golden.py +0 -0
- {pyimouapi-1.4.1 → pyimouapi-1.4.4}/tests/test_feature_switches.py +0 -0
- {pyimouapi-1.4.1 → pyimouapi-1.4.4}/tests/test_get_devices.py +0 -0
- {pyimouapi-1.4.1 → pyimouapi-1.4.4}/tests/test_lookup_property.py +0 -0
- {pyimouapi-1.4.1 → pyimouapi-1.4.4}/tests/test_message_callback.py +0 -0
- {pyimouapi-1.4.1 → pyimouapi-1.4.4}/tests/test_motion_detect_excepts.py +0 -0
- {pyimouapi-1.4.1 → pyimouapi-1.4.4}/tests/test_openapi_http.py +0 -0
- {pyimouapi-1.4.1 → pyimouapi-1.4.4}/tests/test_packaging.py +0 -0
- {pyimouapi-1.4.1 → pyimouapi-1.4.4}/tests/test_pic_decode.py +0 -0
- {pyimouapi-1.4.1 → pyimouapi-1.4.4}/tests/test_product_id_guard.py +0 -0
- {pyimouapi-1.4.1 → pyimouapi-1.4.4}/tests/test_push.py +0 -0
- {pyimouapi-1.4.1 → pyimouapi-1.4.4}/tests/test_select_option.py +0 -0
- {pyimouapi-1.4.1 → pyimouapi-1.4.4}/tests/test_sensor_normalize.py +0 -0
- {pyimouapi-1.4.1 → pyimouapi-1.4.4}/tests/test_services_entities_concurrency.py +0 -0
- {pyimouapi-1.4.1 → pyimouapi-1.4.4}/tests/test_set_text_value.py +0 -0
- {pyimouapi-1.4.1 → pyimouapi-1.4.4}/tests/test_shared_status_reads.py +0 -0
- {pyimouapi-1.4.1 → pyimouapi-1.4.4}/tests/test_status_read_concurrency.py +0 -0
- {pyimouapi-1.4.1 → pyimouapi-1.4.4}/tests/test_update_from_detail.py +0 -0
- {pyimouapi-1.4.1 → pyimouapi-1.4.4}/tests/test_write_optimistic_state.py +0 -0
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
"""Color bulb state for one Home Assistant light.
|
|
2
|
+
|
|
3
|
+
White and color are color modes. Named scenes are effects. The relay is the
|
|
4
|
+
light itself, not a separate switch.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
from typing import Any
|
|
10
|
+
|
|
11
|
+
REF_SWITCH = "11900"
|
|
12
|
+
REF_MODE = "131800"
|
|
13
|
+
REF_BRIGHTNESS = "131900"
|
|
14
|
+
REF_COLOR_TEMP = "132000"
|
|
15
|
+
REF_HUE = "132100"
|
|
16
|
+
REF_SATURATION = "132200"
|
|
17
|
+
REF_VALUE = "132300"
|
|
18
|
+
REF_SCENE = "132400"
|
|
19
|
+
|
|
20
|
+
MODE_WHITE = 0
|
|
21
|
+
MODE_COLOR = 1
|
|
22
|
+
MODE_SCENE = 2
|
|
23
|
+
|
|
24
|
+
MIN_KELVIN = 2700
|
|
25
|
+
MAX_KELVIN = 6500
|
|
26
|
+
|
|
27
|
+
BULB_PROPERTY_REFS = (
|
|
28
|
+
REF_SWITCH,
|
|
29
|
+
REF_MODE,
|
|
30
|
+
REF_BRIGHTNESS,
|
|
31
|
+
REF_COLOR_TEMP,
|
|
32
|
+
REF_HUE,
|
|
33
|
+
REF_SATURATION,
|
|
34
|
+
REF_VALUE,
|
|
35
|
+
REF_SCENE,
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
# Effect name shown on the light, and the scene enum the bulb stores.
|
|
39
|
+
BULB_SCENES: tuple[tuple[str, int], ...] = (
|
|
40
|
+
("Daylight", 0),
|
|
41
|
+
("Night light", 1),
|
|
42
|
+
("Movie", 2),
|
|
43
|
+
("Relax", 3),
|
|
44
|
+
("Romantic", 4),
|
|
45
|
+
("Party", 5),
|
|
46
|
+
("Christmas", 6),
|
|
47
|
+
("Halloween", 7),
|
|
48
|
+
("Wake up", 8),
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
_SCENE_BY_NAME = dict(BULB_SCENES)
|
|
52
|
+
_NAME_BY_SCENE = {value: name for name, value in BULB_SCENES}
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def empty_bulb_state() -> dict[str, int | bool]:
|
|
56
|
+
"""Defaults match the thing model until the first property read."""
|
|
57
|
+
return {
|
|
58
|
+
"on": False,
|
|
59
|
+
"mode": MODE_WHITE,
|
|
60
|
+
"brightness": 50,
|
|
61
|
+
"color_temp": 3500,
|
|
62
|
+
"hue": 0,
|
|
63
|
+
"saturation": 0,
|
|
64
|
+
"value": 100,
|
|
65
|
+
"scene": 0,
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def scene_effect_names() -> list[str]:
|
|
70
|
+
"""Effect list for the light card, in thing-model order."""
|
|
71
|
+
return [name for name, _value in BULB_SCENES]
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def scene_effect_name(scene: int) -> str | None:
|
|
75
|
+
"""Return the effect name for a scene enum, if we know it."""
|
|
76
|
+
return _NAME_BY_SCENE.get(scene)
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def percent_to_brightness(percent: int) -> int:
|
|
80
|
+
"""Map a 1-100 bulb level onto Home Assistant brightness 1-255."""
|
|
81
|
+
clamped = max(1, min(100, percent))
|
|
82
|
+
return max(1, min(255, round(clamped * 255 / 100)))
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def brightness_to_percent(brightness: int) -> int:
|
|
86
|
+
"""Map Home Assistant brightness 0-255 onto a 1-100 bulb level."""
|
|
87
|
+
clamped = max(0, min(255, brightness))
|
|
88
|
+
return max(1, min(100, round(clamped * 100 / 255)))
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def _as_int(raw: Any) -> int | None:
|
|
92
|
+
if isinstance(raw, bool):
|
|
93
|
+
return int(raw)
|
|
94
|
+
if isinstance(raw, int):
|
|
95
|
+
return raw
|
|
96
|
+
if isinstance(raw, float):
|
|
97
|
+
return int(raw)
|
|
98
|
+
if isinstance(raw, str):
|
|
99
|
+
text = raw.strip()
|
|
100
|
+
if text.lstrip("-").isdigit():
|
|
101
|
+
return int(text)
|
|
102
|
+
return None
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def apply_bulb_properties(state: dict[str, Any], values: dict[str, Any]) -> None:
|
|
106
|
+
"""Fold a ref→value map into bulb state. Unknown or empty values are skipped."""
|
|
107
|
+
if REF_SWITCH in values:
|
|
108
|
+
raw = values[REF_SWITCH]
|
|
109
|
+
if isinstance(raw, bool):
|
|
110
|
+
state["on"] = raw
|
|
111
|
+
else:
|
|
112
|
+
parsed = _as_int(raw)
|
|
113
|
+
if parsed is not None:
|
|
114
|
+
state["on"] = parsed == 1
|
|
115
|
+
for key, ref in (
|
|
116
|
+
("mode", REF_MODE),
|
|
117
|
+
("brightness", REF_BRIGHTNESS),
|
|
118
|
+
("color_temp", REF_COLOR_TEMP),
|
|
119
|
+
("hue", REF_HUE),
|
|
120
|
+
("saturation", REF_SATURATION),
|
|
121
|
+
("value", REF_VALUE),
|
|
122
|
+
("scene", REF_SCENE),
|
|
123
|
+
):
|
|
124
|
+
if ref not in values:
|
|
125
|
+
continue
|
|
126
|
+
parsed = _as_int(values[ref])
|
|
127
|
+
if parsed is None:
|
|
128
|
+
continue
|
|
129
|
+
state[key] = _clamp_read(key, parsed)
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
def properties_for_turn_off() -> dict[str, int]:
|
|
133
|
+
"""Turn the bulb off."""
|
|
134
|
+
return {REF_SWITCH: 0}
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def properties_for_turn_on(
|
|
138
|
+
state: dict[str, Any],
|
|
139
|
+
*,
|
|
140
|
+
brightness: int | None = None,
|
|
141
|
+
color_temp_kelvin: int | None = None,
|
|
142
|
+
hs_color: tuple[float, float] | None = None,
|
|
143
|
+
effect: str | None = None,
|
|
144
|
+
) -> dict[str, int]:
|
|
145
|
+
"""Build one property write for light.turn_on.
|
|
146
|
+
|
|
147
|
+
White mode is brightness plus color temperature. Color mode is hue,
|
|
148
|
+
saturation, and its own brightness. A write only includes the values the
|
|
149
|
+
caller passed, so moving color temperature does not copy the color
|
|
150
|
+
brightness, and moving the color does not copy the white brightness.
|
|
151
|
+
"""
|
|
152
|
+
payload: dict[str, int] = {REF_SWITCH: 1}
|
|
153
|
+
if effect is not None:
|
|
154
|
+
scene = _SCENE_BY_NAME.get(effect)
|
|
155
|
+
if scene is None:
|
|
156
|
+
raise ValueError(f"unknown bulb effect: {effect}")
|
|
157
|
+
payload[REF_MODE] = MODE_SCENE
|
|
158
|
+
payload[REF_SCENE] = scene
|
|
159
|
+
return payload
|
|
160
|
+
if hs_color is not None:
|
|
161
|
+
hue, saturation = hs_color
|
|
162
|
+
payload[REF_MODE] = MODE_COLOR
|
|
163
|
+
payload[REF_HUE] = max(0, min(360, round(hue)))
|
|
164
|
+
payload[REF_SATURATION] = max(0, min(100, round(saturation)))
|
|
165
|
+
if brightness is not None:
|
|
166
|
+
payload[REF_VALUE] = max(1, min(100, brightness_to_percent(brightness)))
|
|
167
|
+
return payload
|
|
168
|
+
if color_temp_kelvin is not None:
|
|
169
|
+
payload[REF_MODE] = MODE_WHITE
|
|
170
|
+
payload[REF_COLOR_TEMP] = max(
|
|
171
|
+
MIN_KELVIN, min(MAX_KELVIN, int(color_temp_kelvin))
|
|
172
|
+
)
|
|
173
|
+
if brightness is not None:
|
|
174
|
+
payload[REF_BRIGHTNESS] = max(
|
|
175
|
+
1, min(100, brightness_to_percent(brightness))
|
|
176
|
+
)
|
|
177
|
+
return payload
|
|
178
|
+
if brightness is not None:
|
|
179
|
+
level = brightness_to_percent(brightness)
|
|
180
|
+
if int(state["mode"]) == MODE_COLOR:
|
|
181
|
+
payload[REF_VALUE] = level
|
|
182
|
+
else:
|
|
183
|
+
# A scene (or any non-white mode) does not use the white slider.
|
|
184
|
+
# Brightness alone moves the bulb back to white so the lamp matches.
|
|
185
|
+
if int(state["mode"]) != MODE_WHITE:
|
|
186
|
+
payload[REF_MODE] = MODE_WHITE
|
|
187
|
+
payload[REF_BRIGHTNESS] = level
|
|
188
|
+
return payload
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
def _clamp_read(key: str, value: int) -> int:
|
|
192
|
+
"""Keep a device reading inside the thing-model range."""
|
|
193
|
+
if key == "brightness":
|
|
194
|
+
return max(1, min(100, value))
|
|
195
|
+
if key == "color_temp":
|
|
196
|
+
return max(MIN_KELVIN, min(MAX_KELVIN, value))
|
|
197
|
+
if key == "hue":
|
|
198
|
+
return max(0, min(360, value))
|
|
199
|
+
if key in ("saturation", "value"):
|
|
200
|
+
return max(0, min(100, value))
|
|
201
|
+
return value
|
|
@@ -229,6 +229,18 @@ SWITCH_TYPE_ABILITY: dict[str, list[dict[str, Any]]] = {
|
|
|
229
229
|
"function_type": "smdHuman",
|
|
230
230
|
},
|
|
231
231
|
],
|
|
232
|
+
"alarm_pir": [
|
|
233
|
+
{
|
|
234
|
+
"ability": "AlarmPIRV2",
|
|
235
|
+
"default": False,
|
|
236
|
+
"function_type": "alarmPIR",
|
|
237
|
+
},
|
|
238
|
+
{
|
|
239
|
+
"ability": "AlarmPIRV3",
|
|
240
|
+
"default": False,
|
|
241
|
+
"function_type": "alarmPIR",
|
|
242
|
+
},
|
|
243
|
+
],
|
|
232
244
|
"frame_reverse": [
|
|
233
245
|
{
|
|
234
246
|
"ability": "FrameReverse",
|
|
@@ -551,7 +563,7 @@ SENSOR_TYPE_REF: dict[str, list[dict[str, Any]]] = {
|
|
|
551
563
|
"ref": "14600",
|
|
552
564
|
"default": "e2",
|
|
553
565
|
"ref_type": "properties",
|
|
554
|
-
"expression": "('e1' if data['14603']==0 else 'e2') if data['14603'] != 1 else int(data['14602'] / data['14601'] * 100)",
|
|
566
|
+
"expression": "('e1' if data['14603']==0 else 'e2') if data['14603'] != 1 else ('e2' if not data['14601'] else int(data['14602'] / data['14601'] * 100))",
|
|
555
567
|
}
|
|
556
568
|
],
|
|
557
569
|
"battery": [
|
|
@@ -3,6 +3,7 @@ from __future__ import annotations
|
|
|
3
3
|
import asyncio
|
|
4
4
|
import logging
|
|
5
5
|
from dataclasses import dataclass
|
|
6
|
+
from time import monotonic
|
|
6
7
|
from typing import Any
|
|
7
8
|
|
|
8
9
|
from .const import (
|
|
@@ -77,6 +78,16 @@ from .openapi import ImouOpenApiClient
|
|
|
77
78
|
|
|
78
79
|
_LOGGER = logging.getLogger(__name__)
|
|
79
80
|
|
|
81
|
+
# A failed product-model fetch is retried after this, instead of on every push.
|
|
82
|
+
_EVENT_MAP_RETRY_SECONDS = 60
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def _text_or_default(value: Any, default: str = "unknown") -> str:
|
|
86
|
+
"""Return a string field, treating JSON null and blanks as the default."""
|
|
87
|
+
if isinstance(value, str) and value:
|
|
88
|
+
return value
|
|
89
|
+
return default
|
|
90
|
+
|
|
80
91
|
|
|
81
92
|
def _parse_event_ref_map(data: dict[str, Any] | None) -> dict[str, str]:
|
|
82
93
|
"""Build {event.ref: event.identifier} from a getProductModel payload."""
|
|
@@ -282,6 +293,7 @@ class ImouDeviceManager:
|
|
|
282
293
|
def __init__(self, imou_api_client: ImouOpenApiClient):
|
|
283
294
|
self._imou_api_client = imou_api_client
|
|
284
295
|
self._event_maps: dict[str, dict[str, str]] = {}
|
|
296
|
+
self._event_map_failed_at: dict[str, float] = {}
|
|
285
297
|
self._event_map_locks: dict[str, asyncio.Lock] = {}
|
|
286
298
|
self._event_map_locks_guard = asyncio.Lock()
|
|
287
299
|
|
|
@@ -311,15 +323,23 @@ class ImouDeviceManager:
|
|
|
311
323
|
async with lock:
|
|
312
324
|
if product_id in self._event_maps:
|
|
313
325
|
return
|
|
326
|
+
failed_at = self._event_map_failed_at.get(product_id)
|
|
327
|
+
if (
|
|
328
|
+
failed_at is not None
|
|
329
|
+
and monotonic() - failed_at < _EVENT_MAP_RETRY_SECONDS
|
|
330
|
+
):
|
|
331
|
+
return
|
|
314
332
|
try:
|
|
315
333
|
data = await self.async_get_product_model(product_id)
|
|
316
334
|
except Exception:
|
|
335
|
+
self._event_map_failed_at[product_id] = monotonic()
|
|
317
336
|
_LOGGER.warning(
|
|
318
337
|
"getProductModel failed for product_id=%s",
|
|
319
338
|
product_id,
|
|
320
339
|
exc_info=True,
|
|
321
340
|
)
|
|
322
341
|
return
|
|
342
|
+
self._event_map_failed_at.pop(product_id, None)
|
|
323
343
|
self._event_maps[product_id] = _parse_event_ref_map(data)
|
|
324
344
|
|
|
325
345
|
def cached_event_map(self, product_id: str) -> dict[str, str]:
|
|
@@ -364,8 +384,8 @@ class ImouDeviceManager:
|
|
|
364
384
|
data = await self._imou_api_client.async_request_api(
|
|
365
385
|
API_ENDPOINT_LIST_DEVICE_DETAILS, params
|
|
366
386
|
)
|
|
367
|
-
device_list = data.get(PARAM_DEVICE_LIST)
|
|
368
|
-
if not device_list:
|
|
387
|
+
device_list = data.get(PARAM_DEVICE_LIST) if isinstance(data, dict) else None
|
|
388
|
+
if not isinstance(device_list, list) or not device_list:
|
|
369
389
|
return []
|
|
370
390
|
devices = []
|
|
371
391
|
iot_devices = []
|
|
@@ -379,7 +399,9 @@ class ImouDeviceManager:
|
|
|
379
399
|
device_id, device_name, device_status, brand, device_model
|
|
380
400
|
)
|
|
381
401
|
if PARAM_DEVICE_ABILITY in device:
|
|
382
|
-
imou_device.set_device_ability(
|
|
402
|
+
imou_device.set_device_ability(
|
|
403
|
+
_text_or_default(device[PARAM_DEVICE_ABILITY])
|
|
404
|
+
)
|
|
383
405
|
if PARAM_DEVICE_VERSION in device:
|
|
384
406
|
imou_device.set_device_version(device[PARAM_DEVICE_VERSION])
|
|
385
407
|
if PARAM_PARENT_PRODUCT_ID in device:
|
|
@@ -393,7 +415,9 @@ class ImouDeviceManager:
|
|
|
393
415
|
if PARAM_ACCESS_TYPE in device:
|
|
394
416
|
imou_device.set_access_type(device[PARAM_ACCESS_TYPE])
|
|
395
417
|
if PARAM_CHANNEL_LIST in device:
|
|
396
|
-
channel_list = device[PARAM_CHANNEL_LIST]
|
|
418
|
+
channel_list = device[PARAM_CHANNEL_LIST] or []
|
|
419
|
+
if not isinstance(channel_list, list):
|
|
420
|
+
channel_list = []
|
|
397
421
|
channels = []
|
|
398
422
|
for channel in channel_list:
|
|
399
423
|
channel_id = (
|
|
@@ -403,7 +427,9 @@ class ImouDeviceManager:
|
|
|
403
427
|
)
|
|
404
428
|
channel_name = channel[PARAM_CHANNEL_NAME]
|
|
405
429
|
channel_status = channel[PARAM_CHANNEL_STATUS]
|
|
406
|
-
channel_ability =
|
|
430
|
+
channel_ability = _text_or_default(
|
|
431
|
+
channel.get(PARAM_CHANNEL_ABILITY)
|
|
432
|
+
)
|
|
407
433
|
channel = ImouChannel(
|
|
408
434
|
channel_id, channel_name, channel_status, channel_ability
|
|
409
435
|
)
|
|
@@ -469,8 +495,8 @@ class ImouDeviceManager:
|
|
|
469
495
|
data = await self._imou_api_client.async_request_api(
|
|
470
496
|
API_ENDPOINT_LIST_DEVICE_DETAILS, params
|
|
471
497
|
)
|
|
472
|
-
device_list = data.get(PARAM_DEVICE_LIST)
|
|
473
|
-
if not device_list:
|
|
498
|
+
device_list = data.get(PARAM_DEVICE_LIST) if isinstance(data, dict) else None
|
|
499
|
+
if not isinstance(device_list, list) or not device_list:
|
|
474
500
|
return []
|
|
475
501
|
summaries: list[ImouDeviceSummary] = []
|
|
476
502
|
for device in device_list:
|
|
@@ -673,7 +699,9 @@ class ImouDeviceManager:
|
|
|
673
699
|
PARAM_DEVICE_LIST: [
|
|
674
700
|
{
|
|
675
701
|
PARAM_DEVICE_ID: device_id,
|
|
676
|
-
PARAM_CHANNEL_ID: channel_id
|
|
702
|
+
PARAM_CHANNEL_ID: int(channel_id)
|
|
703
|
+
if channel_id is not None
|
|
704
|
+
else channel_id,
|
|
677
705
|
PARAM_PRODUCT_ID: product_id,
|
|
678
706
|
PARAM_PROPERTIES: properties,
|
|
679
707
|
}
|
|
@@ -815,13 +843,13 @@ class ImouDeviceManager:
|
|
|
815
843
|
imou_device.product_id,
|
|
816
844
|
)
|
|
817
845
|
imou_device.set_device_ability_refs(
|
|
818
|
-
device_detail.get(PARAM_ABILITY_REFS
|
|
846
|
+
_text_or_default(device_detail.get(PARAM_ABILITY_REFS))
|
|
819
847
|
)
|
|
820
848
|
channels_detail = device_detail.get(PARAM_CHANNELS)
|
|
821
849
|
if channels_detail and imou_device.channels:
|
|
822
850
|
channel_detail_map = {
|
|
823
|
-
str(channel_detail.get(PARAM_CHANNEL_ID)):
|
|
824
|
-
PARAM_ABILITY_REFS
|
|
851
|
+
str(channel_detail.get(PARAM_CHANNEL_ID)): _text_or_default(
|
|
852
|
+
channel_detail.get(PARAM_ABILITY_REFS)
|
|
825
853
|
)
|
|
826
854
|
for channel_detail in channels_detail
|
|
827
855
|
if channel_detail.get(PARAM_CHANNEL_ID) is not None
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"""Retry user-initiated writes once when the device status is Sleep."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import asyncio
|
|
6
|
+
import logging
|
|
7
|
+
from collections.abc import Awaitable, Callable
|
|
8
|
+
from typing import Any
|
|
9
|
+
|
|
10
|
+
from .const import (
|
|
11
|
+
ERROR_CODE_DEVICE_OFFLINE,
|
|
12
|
+
ERROR_CODE_DEVICE_SLEEPING,
|
|
13
|
+
PARAM_STATE,
|
|
14
|
+
PARAM_STATUS,
|
|
15
|
+
)
|
|
16
|
+
from .exceptions import InvalidAppIdOrSecretException, RequestFailedException
|
|
17
|
+
|
|
18
|
+
_LOGGER = logging.getLogger(__name__)
|
|
19
|
+
|
|
20
|
+
DEVICE_STATUS_SLEEP = "sleep"
|
|
21
|
+
|
|
22
|
+
# Seconds to wait before retrying a control write on a sleeping device.
|
|
23
|
+
DORMANT_WRITE_RETRY_WAIT_SECONDS = 5
|
|
24
|
+
|
|
25
|
+
# Platform codes seen when a dormant device cannot accept a write yet.
|
|
26
|
+
DORMANT_WRITE_RETRY_ERROR_CODES = frozenset(
|
|
27
|
+
{
|
|
28
|
+
ERROR_CODE_DEVICE_SLEEPING,
|
|
29
|
+
ERROR_CODE_DEVICE_OFFLINE,
|
|
30
|
+
"DV1026",
|
|
31
|
+
}
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def device_is_sleeping(device: Any) -> bool:
|
|
36
|
+
"""Return True when the status sensor reports sleep."""
|
|
37
|
+
status = device.sensors.get(PARAM_STATUS, {}).get(PARAM_STATE)
|
|
38
|
+
return status == DEVICE_STATUS_SLEEP
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def is_dormant_retryable_error(exc: BaseException) -> bool:
|
|
42
|
+
"""Return True when the platform error may clear after a short wait."""
|
|
43
|
+
if not isinstance(exc, RequestFailedException):
|
|
44
|
+
return False
|
|
45
|
+
message = exc.message
|
|
46
|
+
return any(code in message for code in DORMANT_WRITE_RETRY_ERROR_CODES)
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
async def async_dormant_write_retry(
|
|
50
|
+
device: Any,
|
|
51
|
+
operation: Callable[[], Awaitable[None]],
|
|
52
|
+
*,
|
|
53
|
+
wait_seconds: int = DORMANT_WRITE_RETRY_WAIT_SECONDS,
|
|
54
|
+
) -> None:
|
|
55
|
+
"""Run a control write; on asleep-device failure, wait and retry once."""
|
|
56
|
+
try:
|
|
57
|
+
await operation()
|
|
58
|
+
except InvalidAppIdOrSecretException:
|
|
59
|
+
raise
|
|
60
|
+
except RequestFailedException as first:
|
|
61
|
+
if not (device_is_sleeping(device) and is_dormant_retryable_error(first)):
|
|
62
|
+
raise
|
|
63
|
+
_LOGGER.debug(
|
|
64
|
+
"Control write for %s failed while asleep (%s); retrying after %ss",
|
|
65
|
+
device.device_name,
|
|
66
|
+
first.message,
|
|
67
|
+
wait_seconds,
|
|
68
|
+
)
|
|
69
|
+
await asyncio.sleep(wait_seconds)
|
|
70
|
+
await operation()
|