pyimouapi 1.4.0__tar.gz → 1.4.3__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.0 → pyimouapi-1.4.3}/PKG-INFO +1 -1
- {pyimouapi-1.4.0 → pyimouapi-1.4.3}/pyimouapi/__init__.py +1 -1
- {pyimouapi-1.4.0 → pyimouapi-1.4.3}/pyimouapi/const.py +13 -0
- {pyimouapi-1.4.0 → pyimouapi-1.4.3}/pyimouapi/device.py +29 -0
- pyimouapi-1.4.3/pyimouapi/dormant_retry.py +70 -0
- {pyimouapi-1.4.0 → pyimouapi-1.4.3}/pyimouapi/ha_device.py +130 -104
- {pyimouapi-1.4.0 → pyimouapi-1.4.3}/pyimouapi.egg-info/PKG-INFO +1 -1
- {pyimouapi-1.4.0 → pyimouapi-1.4.3}/pyimouapi.egg-info/SOURCES.txt +4 -0
- {pyimouapi-1.4.0 → pyimouapi-1.4.3}/pyproject.toml +1 -1
- {pyimouapi-1.4.0 → pyimouapi-1.4.3}/setup.py +1 -1
- pyimouapi-1.4.3/tests/test_alarm_pir_switch.py +37 -0
- pyimouapi-1.4.3/tests/test_device_stream.py +123 -0
- pyimouapi-1.4.3/tests/test_dormant_write_retry.py +120 -0
- {pyimouapi-1.4.0 → pyimouapi-1.4.3}/tests/test_event_identifier_map.py +9 -0
- {pyimouapi-1.4.0 → pyimouapi-1.4.3}/tests/test_pic_decode.py +27 -1
- {pyimouapi-1.4.0 → pyimouapi-1.4.3}/LICENSE +0 -0
- {pyimouapi-1.4.0 → pyimouapi-1.4.3}/README.md +0 -0
- {pyimouapi-1.4.0 → pyimouapi-1.4.3}/pyimouapi/alarm_mode.py +0 -0
- {pyimouapi-1.4.0 → pyimouapi-1.4.3}/pyimouapi/collection_point.py +0 -0
- {pyimouapi-1.4.0 → pyimouapi-1.4.3}/pyimouapi/exceptions.py +0 -0
- {pyimouapi-1.4.0 → pyimouapi-1.4.3}/pyimouapi/openapi.py +0 -0
- {pyimouapi-1.4.0 → pyimouapi-1.4.3}/pyimouapi/pic_decode.py +0 -0
- {pyimouapi-1.4.0 → pyimouapi-1.4.3}/pyimouapi/push.py +0 -0
- {pyimouapi-1.4.0 → pyimouapi-1.4.3}/pyimouapi/py.typed +0 -0
- {pyimouapi-1.4.0 → pyimouapi-1.4.3}/pyimouapi/select_option.py +0 -0
- {pyimouapi-1.4.0 → pyimouapi-1.4.3}/pyimouapi/sensor.py +0 -0
- {pyimouapi-1.4.0 → pyimouapi-1.4.3}/pyimouapi/siren.py +0 -0
- {pyimouapi-1.4.0 → pyimouapi-1.4.3}/pyimouapi.egg-info/dependency_links.txt +0 -0
- {pyimouapi-1.4.0 → pyimouapi-1.4.3}/pyimouapi.egg-info/not-zip-safe +0 -0
- {pyimouapi-1.4.0 → pyimouapi-1.4.3}/pyimouapi.egg-info/requires.txt +0 -0
- {pyimouapi-1.4.0 → pyimouapi-1.4.3}/pyimouapi.egg-info/top_level.txt +0 -0
- {pyimouapi-1.4.0 → pyimouapi-1.4.3}/setup.cfg +0 -0
- {pyimouapi-1.4.0 → pyimouapi-1.4.3}/tests/test_accessory_device_id.py +0 -0
- {pyimouapi-1.4.0 → pyimouapi-1.4.3}/tests/test_alarm_control_panel.py +0 -0
- {pyimouapi-1.4.0 → pyimouapi-1.4.3}/tests/test_alarm_mode.py +0 -0
- {pyimouapi-1.4.0 → pyimouapi-1.4.3}/tests/test_auth_failure_propagates.py +0 -0
- {pyimouapi-1.4.0 → pyimouapi-1.4.3}/tests/test_bind_device.py +0 -0
- {pyimouapi-1.4.0 → pyimouapi-1.4.3}/tests/test_channel_id_and_api_guards.py +0 -0
- {pyimouapi-1.4.0 → pyimouapi-1.4.3}/tests/test_collect_property_entities.py +0 -0
- {pyimouapi-1.4.0 → pyimouapi-1.4.3}/tests/test_collection_point.py +0 -0
- {pyimouapi-1.4.0 → pyimouapi-1.4.3}/tests/test_connection_pool.py +0 -0
- {pyimouapi-1.4.0 → pyimouapi-1.4.3}/tests/test_device_image.py +0 -0
- {pyimouapi-1.4.0 → pyimouapi-1.4.3}/tests/test_device_summaries.py +0 -0
- {pyimouapi-1.4.0 → pyimouapi-1.4.3}/tests/test_entity_tables_golden.py +0 -0
- {pyimouapi-1.4.0 → pyimouapi-1.4.3}/tests/test_feature_switches.py +0 -0
- {pyimouapi-1.4.0 → pyimouapi-1.4.3}/tests/test_get_devices.py +0 -0
- {pyimouapi-1.4.0 → pyimouapi-1.4.3}/tests/test_lookup_property.py +0 -0
- {pyimouapi-1.4.0 → pyimouapi-1.4.3}/tests/test_message_callback.py +0 -0
- {pyimouapi-1.4.0 → pyimouapi-1.4.3}/tests/test_motion_detect_excepts.py +0 -0
- {pyimouapi-1.4.0 → pyimouapi-1.4.3}/tests/test_openapi_http.py +0 -0
- {pyimouapi-1.4.0 → pyimouapi-1.4.3}/tests/test_packaging.py +0 -0
- {pyimouapi-1.4.0 → pyimouapi-1.4.3}/tests/test_product_id_guard.py +0 -0
- {pyimouapi-1.4.0 → pyimouapi-1.4.3}/tests/test_push.py +0 -0
- {pyimouapi-1.4.0 → pyimouapi-1.4.3}/tests/test_select_option.py +0 -0
- {pyimouapi-1.4.0 → pyimouapi-1.4.3}/tests/test_sensor_normalize.py +0 -0
- {pyimouapi-1.4.0 → pyimouapi-1.4.3}/tests/test_services_entities_concurrency.py +0 -0
- {pyimouapi-1.4.0 → pyimouapi-1.4.3}/tests/test_set_text_value.py +0 -0
- {pyimouapi-1.4.0 → pyimouapi-1.4.3}/tests/test_shared_status_reads.py +0 -0
- {pyimouapi-1.4.0 → pyimouapi-1.4.3}/tests/test_siren.py +0 -0
- {pyimouapi-1.4.0 → pyimouapi-1.4.3}/tests/test_status_read_concurrency.py +0 -0
- {pyimouapi-1.4.0 → pyimouapi-1.4.3}/tests/test_switch_status_aggregation.py +0 -0
- {pyimouapi-1.4.0 → pyimouapi-1.4.3}/tests/test_update_from_detail.py +0 -0
- {pyimouapi-1.4.0 → pyimouapi-1.4.3}/tests/test_write_optimistic_state.py +0 -0
|
@@ -16,6 +16,7 @@ API_ENDPOINT_BIND_DEVICE = "/openapi/bindDevice"
|
|
|
16
16
|
API_ENDPOINT_BIND_DEVICE_LIVE = "/openapi/bindDeviceLive"
|
|
17
17
|
API_ENDPOINT_GET_DEVICE_ONLINE = "/openapi/deviceOnline"
|
|
18
18
|
API_ENDPOINT_GET_DEVICE_LIVE_INFO = "/openapi/getLiveStreamInfo"
|
|
19
|
+
API_ENDPOINT_GET_STREAM_URL = "/openapi/getStreamUrl"
|
|
19
20
|
API_ENDPOINT_SET_DEVICE_SNAP = "/openapi/setDeviceSnapEnhanced"
|
|
20
21
|
API_ENDPOINT_GET_IOT_DEVICE_PROPERTIES = "/openapi/getIotDeviceProperties"
|
|
21
22
|
API_ENDPOINT_SET_IOT_DEVICE_PROPERTIES = "/openapi/setIotDeviceProperties"
|
|
@@ -228,6 +229,18 @@ SWITCH_TYPE_ABILITY: dict[str, list[dict[str, Any]]] = {
|
|
|
228
229
|
"function_type": "smdHuman",
|
|
229
230
|
},
|
|
230
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
|
+
],
|
|
231
244
|
"frame_reverse": [
|
|
232
245
|
{
|
|
233
246
|
"ability": "FrameReverse",
|
|
@@ -21,6 +21,7 @@ from .const import (
|
|
|
21
21
|
API_ENDPOINT_GET_IOT_DEVICE_DETAIL_INFO,
|
|
22
22
|
API_ENDPOINT_GET_IOT_DEVICE_PROPERTIES,
|
|
23
23
|
API_ENDPOINT_GET_PRODUCT_MODEL,
|
|
24
|
+
API_ENDPOINT_GET_STREAM_URL,
|
|
24
25
|
API_ENDPOINT_IOT_DEVICE_CONTROL,
|
|
25
26
|
API_ENDPOINT_LIST_DEVICE_DETAILS,
|
|
26
27
|
API_ENDPOINT_MODIFY_DEVICE_ALARM_STATUS,
|
|
@@ -321,6 +322,12 @@ class ImouDeviceManager:
|
|
|
321
322
|
return
|
|
322
323
|
self._event_maps[product_id] = _parse_event_ref_map(data)
|
|
323
324
|
|
|
325
|
+
def cached_event_map(self, product_id: str) -> dict[str, str]:
|
|
326
|
+
"""Return the cached events ref→identifier map, or {} if not loaded."""
|
|
327
|
+
if not product_id:
|
|
328
|
+
return {}
|
|
329
|
+
return dict(self._event_maps.get(product_id) or {})
|
|
330
|
+
|
|
324
331
|
async def async_resolve_event_identifier(
|
|
325
332
|
self, product_id: str, ref: str
|
|
326
333
|
) -> str | None:
|
|
@@ -603,6 +610,28 @@ class ImouDeviceManager:
|
|
|
603
610
|
API_ENDPOINT_GET_DEVICE_LIVE_INFO, params
|
|
604
611
|
)
|
|
605
612
|
|
|
613
|
+
async def async_get_rtsp_stream_url(
|
|
614
|
+
self,
|
|
615
|
+
device_id: str,
|
|
616
|
+
channel_id: str | None,
|
|
617
|
+
stream_id: int = 0,
|
|
618
|
+
product_id: str | None = None,
|
|
619
|
+
) -> dict[str, Any]:
|
|
620
|
+
"""Obtain a short-lived cloud RTSP URL (getStreamUrl).
|
|
621
|
+
|
|
622
|
+
``productId`` is only sent for IoT devices that have one.
|
|
623
|
+
"""
|
|
624
|
+
params: dict[str, Any] = {
|
|
625
|
+
PARAM_DEVICE_ID: device_id,
|
|
626
|
+
PARAM_CHANNEL_ID: channel_id,
|
|
627
|
+
PARAM_STREAM_ID: stream_id,
|
|
628
|
+
}
|
|
629
|
+
if product_id:
|
|
630
|
+
params[PARAM_PRODUCT_ID] = product_id
|
|
631
|
+
return await self._imou_api_client.async_request_api(
|
|
632
|
+
API_ENDPOINT_GET_STREAM_URL, params
|
|
633
|
+
)
|
|
634
|
+
|
|
606
635
|
async def async_get_device_snap(
|
|
607
636
|
self, device_id: str, channel_id: str | None
|
|
608
637
|
) -> dict[str, Any]:
|
|
@@ -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 = 10
|
|
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()
|
|
@@ -22,9 +22,6 @@ from .const import (
|
|
|
22
22
|
BUTTON_TYPE_ABILITY,
|
|
23
23
|
BUTTON_TYPE_PARAM_VALUE,
|
|
24
24
|
BUTTON_TYPE_REF,
|
|
25
|
-
ERROR_CODE_DEVICE_SLEEPING,
|
|
26
|
-
ERROR_CODE_LIVE_ALREADY_EXIST,
|
|
27
|
-
ERROR_CODE_LIVE_NOT_EXIST,
|
|
28
25
|
ERROR_CODE_NO_STORAGE_MEDIUM,
|
|
29
26
|
PARAM_ABILITY,
|
|
30
27
|
PARAM_ALKELEC,
|
|
@@ -42,7 +39,6 @@ from .const import (
|
|
|
42
39
|
PARAM_EXPRESSION,
|
|
43
40
|
PARAM_FUNCTION_TYPE,
|
|
44
41
|
PARAM_HD,
|
|
45
|
-
PARAM_HLS,
|
|
46
42
|
PARAM_HUMIDITY_CURRENT,
|
|
47
43
|
PARAM_INPUT_REF,
|
|
48
44
|
PARAM_LITELEC,
|
|
@@ -65,8 +61,6 @@ from .const import (
|
|
|
65
61
|
PARAM_STATE,
|
|
66
62
|
PARAM_STATUS,
|
|
67
63
|
PARAM_STORAGE_USED,
|
|
68
|
-
PARAM_STREAM_ID,
|
|
69
|
-
PARAM_STREAMS,
|
|
70
64
|
PARAM_SUPPORTED,
|
|
71
65
|
PARAM_TEMPERATURE_CURRENT,
|
|
72
66
|
PARAM_TOTAL_BYTES,
|
|
@@ -84,6 +78,7 @@ from .const import (
|
|
|
84
78
|
TEXT_TYPE_REF,
|
|
85
79
|
)
|
|
86
80
|
from .device import ImouDevice, ImouDeviceManager, compose_iot_device_id
|
|
81
|
+
from .dormant_retry import DORMANT_WRITE_RETRY_WAIT_SECONDS, async_dormant_write_retry
|
|
87
82
|
from .exceptions import InvalidAppIdOrSecretException, RequestFailedException
|
|
88
83
|
from .select_option import normalize_options, to_friendly, to_raw
|
|
89
84
|
from .sensor import apply_sensor_state
|
|
@@ -305,6 +300,7 @@ class ImouHaDevice:
|
|
|
305
300
|
self._parent_product_id: str | None = None
|
|
306
301
|
self._parent_device_id: str | None = None
|
|
307
302
|
self._device_ability = "unknown"
|
|
303
|
+
self._channel_ability = "unknown"
|
|
308
304
|
|
|
309
305
|
@property
|
|
310
306
|
def device_id(self) -> str:
|
|
@@ -382,6 +378,10 @@ class ImouHaDevice:
|
|
|
382
378
|
def device_ability(self) -> str:
|
|
383
379
|
return self._device_ability
|
|
384
380
|
|
|
381
|
+
@property
|
|
382
|
+
def channel_ability(self) -> str:
|
|
383
|
+
return self._channel_ability
|
|
384
|
+
|
|
385
385
|
@property
|
|
386
386
|
def device_name(self) -> str:
|
|
387
387
|
return self._device_name
|
|
@@ -398,6 +398,9 @@ class ImouHaDevice:
|
|
|
398
398
|
def set_device_ability(self, device_ability: str) -> None:
|
|
399
399
|
self._device_ability = device_ability
|
|
400
400
|
|
|
401
|
+
def set_channel_ability(self, channel_ability: str) -> None:
|
|
402
|
+
self._channel_ability = channel_ability
|
|
403
|
+
|
|
401
404
|
def __str__(self) -> str:
|
|
402
405
|
return (
|
|
403
406
|
f"device_id: {self._device_id}, product_id:{self._product_id},parent_device_id:{self._parent_product_id},device_name: {self._device_name}, manufacturer: {self._manufacturer}, "
|
|
@@ -424,13 +427,34 @@ class ImouHaDeviceManager:
|
|
|
424
427
|
caller has to be told so it can ask the user to sign in again.
|
|
425
428
|
"""
|
|
426
429
|
|
|
427
|
-
def __init__(
|
|
430
|
+
def __init__(
|
|
431
|
+
self,
|
|
432
|
+
device_manager: ImouDeviceManager,
|
|
433
|
+
*,
|
|
434
|
+
dormant_write_retry_wait_seconds: int = DORMANT_WRITE_RETRY_WAIT_SECONDS,
|
|
435
|
+
):
|
|
428
436
|
self._delegate = device_manager
|
|
437
|
+
self._dormant_write_retry_wait_seconds = dormant_write_retry_wait_seconds
|
|
429
438
|
|
|
430
439
|
@property
|
|
431
440
|
def delegate(self) -> ImouDeviceManager:
|
|
432
441
|
return self._delegate
|
|
433
442
|
|
|
443
|
+
def set_dormant_write_retry_wait_seconds(self, wait_seconds: int) -> None:
|
|
444
|
+
"""How long to wait before retrying a control write on a sleeping device."""
|
|
445
|
+
self._dormant_write_retry_wait_seconds = wait_seconds
|
|
446
|
+
|
|
447
|
+
async def _async_dormant_write_retry(
|
|
448
|
+
self,
|
|
449
|
+
device: ImouHaDevice,
|
|
450
|
+
operation: Callable[[], Coroutine[Any, Any, None]],
|
|
451
|
+
) -> None:
|
|
452
|
+
await async_dormant_write_retry(
|
|
453
|
+
device,
|
|
454
|
+
operation,
|
|
455
|
+
wait_seconds=self._dormant_write_retry_wait_seconds,
|
|
456
|
+
)
|
|
457
|
+
|
|
434
458
|
async def async_close(self) -> None:
|
|
435
459
|
"""Close the underlying Open API HTTP session."""
|
|
436
460
|
await self._delegate.async_close()
|
|
@@ -955,41 +979,29 @@ class ImouHaDeviceManager:
|
|
|
955
979
|
apply_sensor_state(device.sensors, PARAM_STORAGE_USED, "e2")
|
|
956
980
|
|
|
957
981
|
async def async_get_device_stream(
|
|
958
|
-
self, device: ImouHaDevice, live_resolution: str, live_protocol: str
|
|
959
|
-
):
|
|
960
|
-
|
|
961
|
-
return await self._async_get_device_exist_stream(
|
|
962
|
-
device, live_resolution, live_protocol
|
|
963
|
-
)
|
|
964
|
-
except RequestFailedException as exception:
|
|
965
|
-
if ERROR_CODE_LIVE_NOT_EXIST in exception.message:
|
|
966
|
-
try:
|
|
967
|
-
return await self._async_create_device_stream(
|
|
968
|
-
device, live_resolution, live_protocol
|
|
969
|
-
)
|
|
970
|
-
except RequestFailedException as ex:
|
|
971
|
-
if ERROR_CODE_LIVE_ALREADY_EXIST in ex.message:
|
|
972
|
-
return await self._async_get_device_exist_stream(
|
|
973
|
-
device, live_resolution, live_protocol
|
|
974
|
-
)
|
|
975
|
-
raise ex
|
|
976
|
-
raise exception
|
|
977
|
-
|
|
978
|
-
async def _async_get_device_exist_stream(
|
|
979
|
-
self, device: ImouHaDevice, resolution: str, protocol: str
|
|
980
|
-
):
|
|
981
|
-
data = await self.delegate.async_get_stream_url(
|
|
982
|
-
device.device_id, device.channel_id
|
|
983
|
-
)
|
|
984
|
-
return await self.async_get_stream_url(data, resolution, protocol)
|
|
982
|
+
self, device: ImouHaDevice, live_resolution: str, live_protocol: str = ""
|
|
983
|
+
) -> str:
|
|
984
|
+
"""Return a fresh cloud RTSP URL from getStreamUrl.
|
|
985
985
|
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
986
|
+
``live_protocol`` is unused (kept so Home Assistant callers that
|
|
987
|
+
still pass https keep working). Shared-account viewers cannot use
|
|
988
|
+
HLS live addresses; getStreamUrl works for the owner and sharers.
|
|
989
|
+
"""
|
|
990
|
+
del live_protocol
|
|
991
|
+
stream_id = 0 if live_resolution == PARAM_HD else 1
|
|
992
|
+
data = await self.delegate.async_get_rtsp_stream_url(
|
|
993
|
+
device.device_id,
|
|
994
|
+
device.channel_id,
|
|
995
|
+
stream_id,
|
|
996
|
+
device.product_id,
|
|
991
997
|
)
|
|
992
|
-
|
|
998
|
+
url = data.get(PARAM_URL) if data else None
|
|
999
|
+
if not url:
|
|
1000
|
+
raise RequestFailedException(
|
|
1001
|
+
f"device {device.device_id} answered getStreamUrl without a url"
|
|
1002
|
+
)
|
|
1003
|
+
_LOGGER.debug("get_device_stream %s", url)
|
|
1004
|
+
return url
|
|
993
1005
|
|
|
994
1006
|
async def async_get_device_image(
|
|
995
1007
|
self, device: ImouHaDevice, wait_seconds: int
|
|
@@ -1028,6 +1040,7 @@ class ImouHaDeviceManager:
|
|
|
1028
1040
|
imou_ha_device = self.build_device(device)
|
|
1029
1041
|
imou_ha_device.set_channel_id(channel.channel_id)
|
|
1030
1042
|
imou_ha_device.set_channel_name(channel.channel_name)
|
|
1043
|
+
imou_ha_device.set_channel_ability(channel.channel_ability)
|
|
1031
1044
|
if device.product_id is not None:
|
|
1032
1045
|
_LOGGER.debug(
|
|
1033
1046
|
"channels and product_id is not none, device_id:%s,product_id:%s",
|
|
@@ -1163,19 +1176,25 @@ class ImouHaDeviceManager:
|
|
|
1163
1176
|
value = str(text_value)
|
|
1164
1177
|
else:
|
|
1165
1178
|
value = text_value
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
{ref_id: value},
|
|
1171
|
-
)
|
|
1172
|
-
if device.channel_id is not None and device.channel_id == "0":
|
|
1179
|
+
product_id = self._require_product_id(device)
|
|
1180
|
+
payload = {ref_id: value}
|
|
1181
|
+
|
|
1182
|
+
async def _write_text() -> None:
|
|
1173
1183
|
await self.delegate.async_set_iot_device_properties(
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1184
|
+
device_id,
|
|
1185
|
+
device.channel_id,
|
|
1186
|
+
product_id,
|
|
1187
|
+
payload,
|
|
1178
1188
|
)
|
|
1189
|
+
if device.channel_id is not None and device.channel_id == "0":
|
|
1190
|
+
await self.delegate.async_set_iot_device_properties(
|
|
1191
|
+
device.device_id,
|
|
1192
|
+
None,
|
|
1193
|
+
product_id,
|
|
1194
|
+
payload,
|
|
1195
|
+
)
|
|
1196
|
+
|
|
1197
|
+
await self._async_dormant_write_retry(device, _write_text)
|
|
1179
1198
|
device.texts[text_type][PARAM_STATE] = str(text_value)
|
|
1180
1199
|
|
|
1181
1200
|
async def async_switch_operation(
|
|
@@ -1186,9 +1205,13 @@ class ImouHaDeviceManager:
|
|
|
1186
1205
|
device, switch_type, enable, ref_id
|
|
1187
1206
|
)
|
|
1188
1207
|
elif switch_type == PARAM_MOTION_DETECT:
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1208
|
+
|
|
1209
|
+
async def _modify_motion() -> None:
|
|
1210
|
+
await self.delegate.async_modify_device_alarm_status(
|
|
1211
|
+
device.device_id, device.channel_id, enable
|
|
1212
|
+
)
|
|
1213
|
+
|
|
1214
|
+
await self._async_dormant_write_retry(device, _modify_motion)
|
|
1192
1215
|
else:
|
|
1193
1216
|
result = await asyncio.gather(
|
|
1194
1217
|
*[
|
|
@@ -1240,9 +1263,13 @@ class ImouHaDeviceManager:
|
|
|
1240
1263
|
)
|
|
1241
1264
|
device.selects[select_type][PARAM_CURRENT_OPTION] = option
|
|
1242
1265
|
elif select_type == PARAM_NIGHT_VISION_MODE:
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1266
|
+
|
|
1267
|
+
async def _set_night_vision() -> None:
|
|
1268
|
+
await self.delegate.async_set_device_night_vision_mode(
|
|
1269
|
+
device.device_id, device.channel_id, option
|
|
1270
|
+
)
|
|
1271
|
+
|
|
1272
|
+
await self._async_dormant_write_retry(device, _set_night_vision)
|
|
1246
1273
|
device.selects[PARAM_NIGHT_VISION_MODE][PARAM_CURRENT_OPTION] = to_friendly(
|
|
1247
1274
|
PARAM_NIGHT_VISION_MODE, option
|
|
1248
1275
|
)
|
|
@@ -1266,9 +1293,13 @@ class ImouHaDeviceManager:
|
|
|
1266
1293
|
async def _async_set_device_switch_status_by_ability(
|
|
1267
1294
|
self, device: ImouHaDevice, ability_type: str, enable: bool
|
|
1268
1295
|
) -> None:
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1296
|
+
|
|
1297
|
+
async def _set() -> None:
|
|
1298
|
+
await self.delegate.async_set_device_status(
|
|
1299
|
+
device.device_id, device.channel_id, ability_type, enable
|
|
1300
|
+
)
|
|
1301
|
+
|
|
1302
|
+
await self._async_dormant_write_retry(device, _set)
|
|
1272
1303
|
|
|
1273
1304
|
async def _async_update_device_select_status_by_type(
|
|
1274
1305
|
self, device: ImouHaDevice, select_type: str
|
|
@@ -1437,19 +1468,6 @@ class ImouHaDeviceManager:
|
|
|
1437
1468
|
PARAM_FUNCTION_TYPE: ability.get(PARAM_FUNCTION_TYPE),
|
|
1438
1469
|
}
|
|
1439
1470
|
|
|
1440
|
-
@staticmethod
|
|
1441
|
-
async def async_get_stream_url(data: dict, resolution: str, protocol: str) -> str:
|
|
1442
|
-
if data.get(PARAM_STREAMS):
|
|
1443
|
-
for stream in data[PARAM_STREAMS]:
|
|
1444
|
-
if (
|
|
1445
|
-
stream[PARAM_HLS].startswith(protocol + ":")
|
|
1446
|
-
and (0 if resolution == PARAM_HD else 1) == stream[PARAM_STREAM_ID]
|
|
1447
|
-
):
|
|
1448
|
-
_LOGGER.debug("get_device_stream %s", stream[PARAM_HLS])
|
|
1449
|
-
return stream[PARAM_HLS]
|
|
1450
|
-
return data[PARAM_STREAMS][0][PARAM_HLS]
|
|
1451
|
-
return ""
|
|
1452
|
-
|
|
1453
1471
|
async def _async_configure_device_by_ref(
|
|
1454
1472
|
self,
|
|
1455
1473
|
channel_ability_refs: list[str],
|
|
@@ -1733,13 +1751,19 @@ class ImouHaDeviceManager:
|
|
|
1733
1751
|
value = int(option)
|
|
1734
1752
|
else:
|
|
1735
1753
|
value = option
|
|
1736
|
-
|
|
1737
|
-
|
|
1738
|
-
|
|
1739
|
-
|
|
1754
|
+
product_id = self._require_product_id(device)
|
|
1755
|
+
payload = {ref: value}
|
|
1756
|
+
|
|
1757
|
+
async def _write() -> None:
|
|
1740
1758
|
await self.delegate.async_set_iot_device_properties(
|
|
1741
|
-
|
|
1759
|
+
device_id, device.channel_id, product_id, payload
|
|
1742
1760
|
)
|
|
1761
|
+
if device.channel_id is not None and device.channel_id == "0":
|
|
1762
|
+
await self.delegate.async_set_iot_device_properties(
|
|
1763
|
+
device.device_id, None, product_id, payload
|
|
1764
|
+
)
|
|
1765
|
+
|
|
1766
|
+
await self._async_dormant_write_retry(device, _write)
|
|
1743
1767
|
|
|
1744
1768
|
async def async_set_alarm_mode(self, device: ImouHaDevice, mode: str) -> None:
|
|
1745
1769
|
panel = device.alarm_control_panel
|
|
@@ -1760,19 +1784,25 @@ class ImouHaDeviceManager:
|
|
|
1760
1784
|
self, device: ImouHaDevice, switch_type: str, enable: bool, ref: str
|
|
1761
1785
|
):
|
|
1762
1786
|
device_id = self._resolve_device_id(device)
|
|
1763
|
-
|
|
1764
|
-
|
|
1765
|
-
|
|
1766
|
-
|
|
1767
|
-
{ref: 1 if enable else 0},
|
|
1768
|
-
)
|
|
1769
|
-
if device.channel_id is not None and device.channel_id == "0":
|
|
1787
|
+
product_id = self._require_product_id(device)
|
|
1788
|
+
payload = {ref: 1 if enable else 0}
|
|
1789
|
+
|
|
1790
|
+
async def _write() -> None:
|
|
1770
1791
|
await self.delegate.async_set_iot_device_properties(
|
|
1771
|
-
|
|
1772
|
-
|
|
1773
|
-
|
|
1774
|
-
|
|
1792
|
+
device_id,
|
|
1793
|
+
device.channel_id,
|
|
1794
|
+
product_id,
|
|
1795
|
+
payload,
|
|
1775
1796
|
)
|
|
1797
|
+
if device.channel_id is not None and device.channel_id == "0":
|
|
1798
|
+
await self.delegate.async_set_iot_device_properties(
|
|
1799
|
+
device.device_id,
|
|
1800
|
+
None,
|
|
1801
|
+
product_id,
|
|
1802
|
+
payload,
|
|
1803
|
+
)
|
|
1804
|
+
|
|
1805
|
+
await self._async_dormant_write_retry(device, _write)
|
|
1776
1806
|
device.switches[switch_type][PARAM_STATE] = enable
|
|
1777
1807
|
|
|
1778
1808
|
@staticmethod
|
|
@@ -1832,7 +1862,7 @@ class ImouHaDeviceManager:
|
|
|
1832
1862
|
"_async_update_device_binary_sensor_status_by_ref fail:%s", e
|
|
1833
1863
|
)
|
|
1834
1864
|
|
|
1835
|
-
async def _async_update_device_battery(self, device
|
|
1865
|
+
async def _async_update_device_battery(self, device):
|
|
1836
1866
|
try:
|
|
1837
1867
|
data = await self.delegate.async_get_device_power_info(device.device_id)
|
|
1838
1868
|
battery_level = "0"
|
|
@@ -1846,17 +1876,8 @@ class ImouHaDeviceManager:
|
|
|
1846
1876
|
battery_level = electricity[PARAM_ELECTRIC]
|
|
1847
1877
|
apply_sensor_state(device.sensors, PARAM_BATTERY, battery_level)
|
|
1848
1878
|
except RequestFailedException as exception:
|
|
1849
|
-
|
|
1850
|
-
|
|
1851
|
-
try:
|
|
1852
|
-
await self.delegate.async_wake_up_device(device.device_id)
|
|
1853
|
-
await self._async_update_device_battery(device, True)
|
|
1854
|
-
except RequestFailedException as e:
|
|
1855
|
-
_LOGGER.error("_async_update_device_battery error: %s", e)
|
|
1856
|
-
apply_sensor_state(device.sensors, PARAM_BATTERY, "0")
|
|
1857
|
-
else:
|
|
1858
|
-
_LOGGER.error("_async_update_device_battery error: %s", exception)
|
|
1859
|
-
apply_sensor_state(device.sensors, PARAM_BATTERY, "0")
|
|
1879
|
+
_LOGGER.error("_async_update_device_battery error: %s", exception)
|
|
1880
|
+
apply_sensor_state(device.sensors, PARAM_BATTERY, "0")
|
|
1860
1881
|
|
|
1861
1882
|
@staticmethod
|
|
1862
1883
|
def configure_text_by_ref(
|
|
@@ -1920,9 +1941,14 @@ class ImouHaDeviceManager:
|
|
|
1920
1941
|
else:
|
|
1921
1942
|
# 如果是关的,则倒计时打开
|
|
1922
1943
|
param["28603"] = 1
|
|
1923
|
-
|
|
1924
|
-
|
|
1925
|
-
)
|
|
1944
|
+
product_id = self._require_product_id(device)
|
|
1945
|
+
|
|
1946
|
+
async def _write_countdown() -> None:
|
|
1947
|
+
await self.delegate.async_iot_device_control(
|
|
1948
|
+
device_id, product_id, "28600", param
|
|
1949
|
+
)
|
|
1950
|
+
|
|
1951
|
+
await self._async_dormant_write_retry(device, _write_countdown)
|
|
1926
1952
|
device.texts["count_down_switch"][PARAM_STATE] = str(text_value)
|
|
1927
1953
|
|
|
1928
1954
|
|
|
@@ -7,6 +7,7 @@ pyimouapi/alarm_mode.py
|
|
|
7
7
|
pyimouapi/collection_point.py
|
|
8
8
|
pyimouapi/const.py
|
|
9
9
|
pyimouapi/device.py
|
|
10
|
+
pyimouapi/dormant_retry.py
|
|
10
11
|
pyimouapi/exceptions.py
|
|
11
12
|
pyimouapi/ha_device.py
|
|
12
13
|
pyimouapi/openapi.py
|
|
@@ -25,6 +26,7 @@ pyimouapi.egg-info/top_level.txt
|
|
|
25
26
|
tests/test_accessory_device_id.py
|
|
26
27
|
tests/test_alarm_control_panel.py
|
|
27
28
|
tests/test_alarm_mode.py
|
|
29
|
+
tests/test_alarm_pir_switch.py
|
|
28
30
|
tests/test_auth_failure_propagates.py
|
|
29
31
|
tests/test_bind_device.py
|
|
30
32
|
tests/test_channel_id_and_api_guards.py
|
|
@@ -32,7 +34,9 @@ tests/test_collect_property_entities.py
|
|
|
32
34
|
tests/test_collection_point.py
|
|
33
35
|
tests/test_connection_pool.py
|
|
34
36
|
tests/test_device_image.py
|
|
37
|
+
tests/test_device_stream.py
|
|
35
38
|
tests/test_device_summaries.py
|
|
39
|
+
tests/test_dormant_write_retry.py
|
|
36
40
|
tests/test_entity_tables_golden.py
|
|
37
41
|
tests/test_event_identifier_map.py
|
|
38
42
|
tests/test_feature_switches.py
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"""PaaS PIR detection switch from AlarmPIR ability generations."""
|
|
2
|
+
|
|
3
|
+
from pyimouapi.const import PARAM_FUNCTION_TYPE, PARAM_STATE
|
|
4
|
+
from pyimouapi.ha_device import ImouHaDevice, ImouHaDeviceManager
|
|
5
|
+
|
|
6
|
+
ALARM_PIR_ABILITIES = ("AlarmPIRV2", "AlarmPIRV3")
|
|
7
|
+
LEGACY_ALARM_PIR_ABILITIES = ("AlarmPIR", "AlarmPIRV4")
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def _device() -> ImouHaDevice:
|
|
11
|
+
device = ImouHaDevice("dev-1", "Cam", "Imou", "IPC-K42", "1.0")
|
|
12
|
+
device.set_product_id("product-not-in-any-excepts")
|
|
13
|
+
return device
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def test_each_alarm_pir_ability_registers_alarm_pir_switch() -> None:
|
|
17
|
+
"""Every PIR ability generation writes the same alarmPIR switch."""
|
|
18
|
+
for ability in ALARM_PIR_ABILITIES:
|
|
19
|
+
device = _device()
|
|
20
|
+
ImouHaDeviceManager.configure_switch_by_ability([ability], False, [], device)
|
|
21
|
+
assert device.switches["alarm_pir"][PARAM_FUNCTION_TYPE] == "alarmPIR"
|
|
22
|
+
assert device.switches["alarm_pir"][PARAM_STATE] is False
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def test_legacy_alarm_pir_abilities_do_not_register_switch() -> None:
|
|
26
|
+
"""Older PIR ability names are ignored until mapped explicitly."""
|
|
27
|
+
for ability in LEGACY_ALARM_PIR_ABILITIES:
|
|
28
|
+
device = _device()
|
|
29
|
+
ImouHaDeviceManager.configure_switch_by_ability([ability], False, [], device)
|
|
30
|
+
assert "alarm_pir" not in device.switches
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def test_device_without_alarm_pir_ability_has_no_switch() -> None:
|
|
34
|
+
"""Picture-change ability must not invent a PIR switch."""
|
|
35
|
+
device = _device()
|
|
36
|
+
ImouHaDeviceManager.configure_switch_by_ability(["AlarmMD"], False, [], device)
|
|
37
|
+
assert "alarm_pir" not in device.switches
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
"""Tests for getStreamUrl live addresses."""
|
|
2
|
+
|
|
3
|
+
from unittest.mock import AsyncMock, MagicMock
|
|
4
|
+
|
|
5
|
+
import pytest
|
|
6
|
+
from pyimouapi.const import (
|
|
7
|
+
PARAM_CHANNEL_ID,
|
|
8
|
+
PARAM_DEVICE_ID,
|
|
9
|
+
PARAM_HD,
|
|
10
|
+
PARAM_PRODUCT_ID,
|
|
11
|
+
PARAM_STREAM_ID,
|
|
12
|
+
PARAM_URL,
|
|
13
|
+
)
|
|
14
|
+
from pyimouapi.device import ImouDeviceManager
|
|
15
|
+
from pyimouapi.exceptions import RequestFailedException
|
|
16
|
+
from pyimouapi.ha_device import ImouHaDevice, ImouHaDeviceManager
|
|
17
|
+
|
|
18
|
+
RTSP_URL = "rtsp://rtspproxy.example.com:8554/clip"
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def camera(*, product_id: str | None = None) -> ImouHaDevice:
|
|
22
|
+
"""Return a camera channel, optionally as an IoT device."""
|
|
23
|
+
ha_device = ImouHaDevice("dev1", "Cam", "Imou", "IPC", "1.0")
|
|
24
|
+
ha_device.set_channel_id("0")
|
|
25
|
+
if product_id is not None:
|
|
26
|
+
ha_device.set_product_id(product_id)
|
|
27
|
+
return ha_device
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
@pytest.mark.asyncio
|
|
31
|
+
async def test_rtsp_request_omits_product_id_for_paas_devices() -> None:
|
|
32
|
+
"""productId is only for IoT; a PaaS camera must not send it."""
|
|
33
|
+
client = MagicMock()
|
|
34
|
+
client.async_request_api = AsyncMock(return_value={PARAM_URL: RTSP_URL})
|
|
35
|
+
manager = ImouDeviceManager(client)
|
|
36
|
+
|
|
37
|
+
await manager.async_get_rtsp_stream_url("dev1", "0", 0)
|
|
38
|
+
|
|
39
|
+
client.async_request_api.assert_awaited_once_with(
|
|
40
|
+
"/openapi/getStreamUrl",
|
|
41
|
+
{
|
|
42
|
+
PARAM_DEVICE_ID: "dev1",
|
|
43
|
+
PARAM_CHANNEL_ID: "0",
|
|
44
|
+
PARAM_STREAM_ID: 0,
|
|
45
|
+
},
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
@pytest.mark.asyncio
|
|
50
|
+
async def test_rtsp_request_sends_product_id_for_iot_devices() -> None:
|
|
51
|
+
"""IoT cameras pass the product they were listed with."""
|
|
52
|
+
client = MagicMock()
|
|
53
|
+
client.async_request_api = AsyncMock(return_value={PARAM_URL: RTSP_URL})
|
|
54
|
+
manager = ImouDeviceManager(client)
|
|
55
|
+
|
|
56
|
+
await manager.async_get_rtsp_stream_url("dev1", "0", 1, "BF5W2WL4")
|
|
57
|
+
|
|
58
|
+
client.async_request_api.assert_awaited_once_with(
|
|
59
|
+
"/openapi/getStreamUrl",
|
|
60
|
+
{
|
|
61
|
+
PARAM_DEVICE_ID: "dev1",
|
|
62
|
+
PARAM_CHANNEL_ID: "0",
|
|
63
|
+
PARAM_STREAM_ID: 1,
|
|
64
|
+
PARAM_PRODUCT_ID: "BF5W2WL4",
|
|
65
|
+
},
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
@pytest.mark.asyncio
|
|
70
|
+
async def test_live_stream_uses_get_stream_url_only() -> None:
|
|
71
|
+
"""Live view fetches getStreamUrl; it does not look up or create HLS."""
|
|
72
|
+
delegate = MagicMock()
|
|
73
|
+
delegate.async_get_stream_url = AsyncMock()
|
|
74
|
+
delegate.async_create_stream_url = AsyncMock()
|
|
75
|
+
delegate.async_get_rtsp_stream_url = AsyncMock(return_value={PARAM_URL: RTSP_URL})
|
|
76
|
+
manager = ImouHaDeviceManager(delegate)
|
|
77
|
+
|
|
78
|
+
assert (
|
|
79
|
+
await manager.async_get_device_stream(camera(), PARAM_HD, "https") == RTSP_URL
|
|
80
|
+
)
|
|
81
|
+
|
|
82
|
+
delegate.async_get_stream_url.assert_not_awaited()
|
|
83
|
+
delegate.async_create_stream_url.assert_not_awaited()
|
|
84
|
+
delegate.async_get_rtsp_stream_url.assert_awaited_once_with("dev1", "0", 0, None)
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
@pytest.mark.asyncio
|
|
88
|
+
async def test_live_stream_passes_product_id_for_iot() -> None:
|
|
89
|
+
"""The request must include productId when the device has one."""
|
|
90
|
+
delegate = MagicMock()
|
|
91
|
+
delegate.async_get_rtsp_stream_url = AsyncMock(return_value={PARAM_URL: RTSP_URL})
|
|
92
|
+
manager = ImouHaDeviceManager(delegate)
|
|
93
|
+
|
|
94
|
+
await manager.async_get_device_stream(
|
|
95
|
+
camera(product_id="BF5W2WL4"), PARAM_HD, "https"
|
|
96
|
+
)
|
|
97
|
+
|
|
98
|
+
delegate.async_get_rtsp_stream_url.assert_awaited_once_with(
|
|
99
|
+
"dev1", "0", 0, "BF5W2WL4"
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
@pytest.mark.asyncio
|
|
104
|
+
async def test_live_stream_uses_substream_for_sd() -> None:
|
|
105
|
+
"""SD live resolution maps to streamId 1."""
|
|
106
|
+
delegate = MagicMock()
|
|
107
|
+
delegate.async_get_rtsp_stream_url = AsyncMock(return_value={PARAM_URL: RTSP_URL})
|
|
108
|
+
manager = ImouHaDeviceManager(delegate)
|
|
109
|
+
|
|
110
|
+
await manager.async_get_device_stream(camera(), "SD", "https")
|
|
111
|
+
|
|
112
|
+
delegate.async_get_rtsp_stream_url.assert_awaited_once_with("dev1", "0", 1, None)
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
@pytest.mark.asyncio
|
|
116
|
+
async def test_live_stream_without_url_reaches_the_caller() -> None:
|
|
117
|
+
"""An answer with no url must not look like a working stream."""
|
|
118
|
+
delegate = MagicMock()
|
|
119
|
+
delegate.async_get_rtsp_stream_url = AsyncMock(return_value={})
|
|
120
|
+
manager = ImouHaDeviceManager(delegate)
|
|
121
|
+
|
|
122
|
+
with pytest.raises(RequestFailedException, match="without a url"):
|
|
123
|
+
await manager.async_get_device_stream(camera(), PARAM_HD, "https")
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
"""Tests for dormant control write retry."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from unittest.mock import AsyncMock, MagicMock, patch
|
|
6
|
+
|
|
7
|
+
import pytest
|
|
8
|
+
from pyimouapi.const import PARAM_REF, PARAM_STATE, PARAM_STATUS
|
|
9
|
+
from pyimouapi.dormant_retry import async_dormant_write_retry
|
|
10
|
+
from pyimouapi.exceptions import RequestFailedException
|
|
11
|
+
from pyimouapi.ha_device import ImouHaDevice, ImouHaDeviceManager
|
|
12
|
+
from pyimouapi.sensor import apply_sensor_state
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def _sleeping_device() -> ImouHaDevice:
|
|
16
|
+
device = ImouHaDevice("DEV001", "Camera", "Imou", "IPC", "1.0")
|
|
17
|
+
device.set_channel_id("0")
|
|
18
|
+
device.set_product_id("prod1")
|
|
19
|
+
apply_sensor_state(device.sensors, PARAM_STATUS, "sleep")
|
|
20
|
+
device.switches["close_camera"] = {
|
|
21
|
+
PARAM_REF: "13100",
|
|
22
|
+
PARAM_STATE: False,
|
|
23
|
+
}
|
|
24
|
+
return device
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
@pytest.mark.asyncio
|
|
28
|
+
async def test_sleeping_device_retries_close_camera_after_dv1026() -> None:
|
|
29
|
+
device = _sleeping_device()
|
|
30
|
+
delegate = MagicMock()
|
|
31
|
+
delegate.async_set_iot_device_properties = AsyncMock(
|
|
32
|
+
side_effect=[
|
|
33
|
+
RequestFailedException("DV1026:The device does not support the call"),
|
|
34
|
+
None,
|
|
35
|
+
None,
|
|
36
|
+
]
|
|
37
|
+
)
|
|
38
|
+
manager = ImouHaDeviceManager(delegate)
|
|
39
|
+
|
|
40
|
+
with patch("pyimouapi.dormant_retry.asyncio.sleep", new=AsyncMock()):
|
|
41
|
+
await manager.async_switch_operation(device, "close_camera", True)
|
|
42
|
+
|
|
43
|
+
assert delegate.async_set_iot_device_properties.await_count == 3
|
|
44
|
+
assert device.switches["close_camera"][PARAM_STATE] is True
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
@pytest.mark.asyncio
|
|
48
|
+
async def test_online_device_does_not_retry() -> None:
|
|
49
|
+
device = _sleeping_device()
|
|
50
|
+
apply_sensor_state(device.sensors, PARAM_STATUS, "online")
|
|
51
|
+
delegate = MagicMock()
|
|
52
|
+
delegate.async_set_iot_device_properties = AsyncMock(
|
|
53
|
+
side_effect=RequestFailedException("DV1026:unsupported")
|
|
54
|
+
)
|
|
55
|
+
manager = ImouHaDeviceManager(delegate)
|
|
56
|
+
|
|
57
|
+
with (
|
|
58
|
+
patch("pyimouapi.dormant_retry.asyncio.sleep", new=AsyncMock()) as sleep,
|
|
59
|
+
pytest.raises(RequestFailedException),
|
|
60
|
+
):
|
|
61
|
+
await manager.async_switch_operation(device, "close_camera", True)
|
|
62
|
+
|
|
63
|
+
sleep.assert_not_awaited()
|
|
64
|
+
assert delegate.async_set_iot_device_properties.await_count == 1
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
@pytest.mark.asyncio
|
|
68
|
+
async def test_sleeping_device_no_retry_on_unrelated_error() -> None:
|
|
69
|
+
device = _sleeping_device()
|
|
70
|
+
delegate = MagicMock()
|
|
71
|
+
delegate.async_set_iot_device_properties = AsyncMock(
|
|
72
|
+
side_effect=RequestFailedException("OP9999:other")
|
|
73
|
+
)
|
|
74
|
+
manager = ImouHaDeviceManager(delegate)
|
|
75
|
+
|
|
76
|
+
with (
|
|
77
|
+
patch("pyimouapi.dormant_retry.asyncio.sleep", new=AsyncMock()) as sleep,
|
|
78
|
+
pytest.raises(RequestFailedException),
|
|
79
|
+
):
|
|
80
|
+
await manager.async_switch_operation(device, "close_camera", True)
|
|
81
|
+
|
|
82
|
+
sleep.assert_not_awaited()
|
|
83
|
+
assert delegate.async_set_iot_device_properties.await_count == 1
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
@pytest.mark.asyncio
|
|
87
|
+
async def test_custom_wait_seconds() -> None:
|
|
88
|
+
device = _sleeping_device()
|
|
89
|
+
calls: list[int] = []
|
|
90
|
+
|
|
91
|
+
async def _fail_then_ok() -> None:
|
|
92
|
+
if len(calls) == 0:
|
|
93
|
+
calls.append(1)
|
|
94
|
+
raise RequestFailedException("DV1030:sleeping")
|
|
95
|
+
calls.append(2)
|
|
96
|
+
|
|
97
|
+
with patch("pyimouapi.dormant_retry.asyncio.sleep", new=AsyncMock()) as sleep:
|
|
98
|
+
await async_dormant_write_retry(device, _fail_then_ok, wait_seconds=25)
|
|
99
|
+
|
|
100
|
+
sleep.assert_awaited_once_with(25)
|
|
101
|
+
assert calls == [1, 2]
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
@pytest.mark.asyncio
|
|
105
|
+
async def test_manager_uses_configured_wait_seconds() -> None:
|
|
106
|
+
device = _sleeping_device()
|
|
107
|
+
delegate = MagicMock()
|
|
108
|
+
delegate.async_set_iot_device_properties = AsyncMock(
|
|
109
|
+
side_effect=[
|
|
110
|
+
RequestFailedException("DV1030:sleep"),
|
|
111
|
+
None,
|
|
112
|
+
None,
|
|
113
|
+
]
|
|
114
|
+
)
|
|
115
|
+
manager = ImouHaDeviceManager(delegate, dormant_write_retry_wait_seconds=42)
|
|
116
|
+
|
|
117
|
+
with patch("pyimouapi.dormant_retry.asyncio.sleep", new=AsyncMock()) as sleep:
|
|
118
|
+
await manager.async_switch_operation(device, "close_camera", True)
|
|
119
|
+
|
|
120
|
+
sleep.assert_awaited_once_with(42)
|
|
@@ -48,10 +48,18 @@ async def test_ensure_event_map_fetches_once_and_caches():
|
|
|
48
48
|
API_ENDPOINT_GET_PRODUCT_MODEL,
|
|
49
49
|
{PARAM_PRODUCT_ID: "pidA"},
|
|
50
50
|
)
|
|
51
|
+
assert manager.cached_event_map("pidA") == {"33000": "doorOpen"}
|
|
51
52
|
assert await manager.async_resolve_event_identifier("pidA", "33000") == "doorOpen"
|
|
52
53
|
assert await manager.async_resolve_event_identifier("pidA", "missing") is None
|
|
53
54
|
|
|
54
55
|
|
|
56
|
+
def test_cached_event_map_empty_until_fetched() -> None:
|
|
57
|
+
"""Setup-time entity gating must not invent events that were never loaded."""
|
|
58
|
+
manager = ImouDeviceManager(MagicMock())
|
|
59
|
+
assert manager.cached_event_map("pidA") == {}
|
|
60
|
+
assert manager.cached_event_map("") == {}
|
|
61
|
+
|
|
62
|
+
|
|
55
63
|
@pytest.mark.asyncio
|
|
56
64
|
async def test_ensure_event_map_failure_does_not_poison_cache():
|
|
57
65
|
client = MagicMock()
|
|
@@ -64,6 +72,7 @@ async def test_ensure_event_map_failure_does_not_poison_cache():
|
|
|
64
72
|
manager = ImouDeviceManager(client)
|
|
65
73
|
|
|
66
74
|
await manager.async_ensure_event_map("pidB")
|
|
75
|
+
assert manager.cached_event_map("pidB") == {}
|
|
67
76
|
assert await manager.async_resolve_event_identifier("pidB", "1") == "ok"
|
|
68
77
|
assert client.async_request_api.await_count == 2
|
|
69
78
|
|
|
@@ -3,8 +3,9 @@
|
|
|
3
3
|
import ctypes
|
|
4
4
|
import hashlib
|
|
5
5
|
from pathlib import Path
|
|
6
|
-
from unittest.mock import MagicMock
|
|
6
|
+
from unittest.mock import AsyncMock, MagicMock
|
|
7
7
|
|
|
8
|
+
import pytest
|
|
8
9
|
from pyimouapi.ha_device import ImouHaDeviceManager
|
|
9
10
|
from pyimouapi.pic_decode import (
|
|
10
11
|
LCOpenPicDecoder,
|
|
@@ -29,6 +30,31 @@ def test_build_device_copies_device_ability() -> None:
|
|
|
29
30
|
src.device_ability = "WLAN,TCM"
|
|
30
31
|
ha = ImouHaDeviceManager.build_device(src)
|
|
31
32
|
assert ha.device_ability == "WLAN,TCM"
|
|
33
|
+
assert ha.channel_ability == "unknown"
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
@pytest.mark.asyncio
|
|
37
|
+
async def test_ha_get_devices_copies_channel_ability() -> None:
|
|
38
|
+
"""PaaS channel abilities (CallAbility, MobileDetect, …) must reach HA devices."""
|
|
39
|
+
from pyimouapi.device import ImouChannel
|
|
40
|
+
|
|
41
|
+
src = MagicMock()
|
|
42
|
+
src.device_id = "SN1"
|
|
43
|
+
src.device_name = "Cam"
|
|
44
|
+
src.brand = "Imou"
|
|
45
|
+
src.device_model = "IPC"
|
|
46
|
+
src.device_version = "1"
|
|
47
|
+
src.product_id = None
|
|
48
|
+
src.parent_product_id = None
|
|
49
|
+
src.parent_device_id = None
|
|
50
|
+
src.is_ipc = True
|
|
51
|
+
src.device_ability = "WLAN"
|
|
52
|
+
src.channels = [ImouChannel("0", "Front", "1", "WLAN,CallAbility,MobileDetect")]
|
|
53
|
+
delegate = MagicMock()
|
|
54
|
+
delegate.async_get_devices = AsyncMock(return_value=[src])
|
|
55
|
+
devices = await ImouHaDeviceManager(delegate).async_get_devices()
|
|
56
|
+
assert devices[0].channel_ability == "WLAN,CallAbility,MobileDetect"
|
|
57
|
+
assert devices[0].channel_id == "0"
|
|
32
58
|
|
|
33
59
|
|
|
34
60
|
def test_is_tcm_ability_token() -> None:
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|