pyimouapi 1.4.1__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.
Files changed (63) hide show
  1. {pyimouapi-1.4.1 → pyimouapi-1.4.3}/PKG-INFO +1 -1
  2. {pyimouapi-1.4.1 → pyimouapi-1.4.3}/pyimouapi/__init__.py +1 -1
  3. {pyimouapi-1.4.1 → pyimouapi-1.4.3}/pyimouapi/const.py +12 -0
  4. pyimouapi-1.4.3/pyimouapi/dormant_retry.py +70 -0
  5. {pyimouapi-1.4.1 → pyimouapi-1.4.3}/pyimouapi/ha_device.py +97 -40
  6. {pyimouapi-1.4.1 → pyimouapi-1.4.3}/pyimouapi.egg-info/PKG-INFO +1 -1
  7. {pyimouapi-1.4.1 → pyimouapi-1.4.3}/pyimouapi.egg-info/SOURCES.txt +3 -0
  8. {pyimouapi-1.4.1 → pyimouapi-1.4.3}/pyproject.toml +1 -1
  9. {pyimouapi-1.4.1 → pyimouapi-1.4.3}/setup.py +1 -1
  10. pyimouapi-1.4.3/tests/test_alarm_pir_switch.py +37 -0
  11. pyimouapi-1.4.3/tests/test_dormant_write_retry.py +120 -0
  12. {pyimouapi-1.4.1 → pyimouapi-1.4.3}/LICENSE +0 -0
  13. {pyimouapi-1.4.1 → pyimouapi-1.4.3}/README.md +0 -0
  14. {pyimouapi-1.4.1 → pyimouapi-1.4.3}/pyimouapi/alarm_mode.py +0 -0
  15. {pyimouapi-1.4.1 → pyimouapi-1.4.3}/pyimouapi/collection_point.py +0 -0
  16. {pyimouapi-1.4.1 → pyimouapi-1.4.3}/pyimouapi/device.py +0 -0
  17. {pyimouapi-1.4.1 → pyimouapi-1.4.3}/pyimouapi/exceptions.py +0 -0
  18. {pyimouapi-1.4.1 → pyimouapi-1.4.3}/pyimouapi/openapi.py +0 -0
  19. {pyimouapi-1.4.1 → pyimouapi-1.4.3}/pyimouapi/pic_decode.py +0 -0
  20. {pyimouapi-1.4.1 → pyimouapi-1.4.3}/pyimouapi/push.py +0 -0
  21. {pyimouapi-1.4.1 → pyimouapi-1.4.3}/pyimouapi/py.typed +0 -0
  22. {pyimouapi-1.4.1 → pyimouapi-1.4.3}/pyimouapi/select_option.py +0 -0
  23. {pyimouapi-1.4.1 → pyimouapi-1.4.3}/pyimouapi/sensor.py +0 -0
  24. {pyimouapi-1.4.1 → pyimouapi-1.4.3}/pyimouapi/siren.py +0 -0
  25. {pyimouapi-1.4.1 → pyimouapi-1.4.3}/pyimouapi.egg-info/dependency_links.txt +0 -0
  26. {pyimouapi-1.4.1 → pyimouapi-1.4.3}/pyimouapi.egg-info/not-zip-safe +0 -0
  27. {pyimouapi-1.4.1 → pyimouapi-1.4.3}/pyimouapi.egg-info/requires.txt +0 -0
  28. {pyimouapi-1.4.1 → pyimouapi-1.4.3}/pyimouapi.egg-info/top_level.txt +0 -0
  29. {pyimouapi-1.4.1 → pyimouapi-1.4.3}/setup.cfg +0 -0
  30. {pyimouapi-1.4.1 → pyimouapi-1.4.3}/tests/test_accessory_device_id.py +0 -0
  31. {pyimouapi-1.4.1 → pyimouapi-1.4.3}/tests/test_alarm_control_panel.py +0 -0
  32. {pyimouapi-1.4.1 → pyimouapi-1.4.3}/tests/test_alarm_mode.py +0 -0
  33. {pyimouapi-1.4.1 → pyimouapi-1.4.3}/tests/test_auth_failure_propagates.py +0 -0
  34. {pyimouapi-1.4.1 → pyimouapi-1.4.3}/tests/test_bind_device.py +0 -0
  35. {pyimouapi-1.4.1 → pyimouapi-1.4.3}/tests/test_channel_id_and_api_guards.py +0 -0
  36. {pyimouapi-1.4.1 → pyimouapi-1.4.3}/tests/test_collect_property_entities.py +0 -0
  37. {pyimouapi-1.4.1 → pyimouapi-1.4.3}/tests/test_collection_point.py +0 -0
  38. {pyimouapi-1.4.1 → pyimouapi-1.4.3}/tests/test_connection_pool.py +0 -0
  39. {pyimouapi-1.4.1 → pyimouapi-1.4.3}/tests/test_device_image.py +0 -0
  40. {pyimouapi-1.4.1 → pyimouapi-1.4.3}/tests/test_device_stream.py +0 -0
  41. {pyimouapi-1.4.1 → pyimouapi-1.4.3}/tests/test_device_summaries.py +0 -0
  42. {pyimouapi-1.4.1 → pyimouapi-1.4.3}/tests/test_entity_tables_golden.py +0 -0
  43. {pyimouapi-1.4.1 → pyimouapi-1.4.3}/tests/test_event_identifier_map.py +0 -0
  44. {pyimouapi-1.4.1 → pyimouapi-1.4.3}/tests/test_feature_switches.py +0 -0
  45. {pyimouapi-1.4.1 → pyimouapi-1.4.3}/tests/test_get_devices.py +0 -0
  46. {pyimouapi-1.4.1 → pyimouapi-1.4.3}/tests/test_lookup_property.py +0 -0
  47. {pyimouapi-1.4.1 → pyimouapi-1.4.3}/tests/test_message_callback.py +0 -0
  48. {pyimouapi-1.4.1 → pyimouapi-1.4.3}/tests/test_motion_detect_excepts.py +0 -0
  49. {pyimouapi-1.4.1 → pyimouapi-1.4.3}/tests/test_openapi_http.py +0 -0
  50. {pyimouapi-1.4.1 → pyimouapi-1.4.3}/tests/test_packaging.py +0 -0
  51. {pyimouapi-1.4.1 → pyimouapi-1.4.3}/tests/test_pic_decode.py +0 -0
  52. {pyimouapi-1.4.1 → pyimouapi-1.4.3}/tests/test_product_id_guard.py +0 -0
  53. {pyimouapi-1.4.1 → pyimouapi-1.4.3}/tests/test_push.py +0 -0
  54. {pyimouapi-1.4.1 → pyimouapi-1.4.3}/tests/test_select_option.py +0 -0
  55. {pyimouapi-1.4.1 → pyimouapi-1.4.3}/tests/test_sensor_normalize.py +0 -0
  56. {pyimouapi-1.4.1 → pyimouapi-1.4.3}/tests/test_services_entities_concurrency.py +0 -0
  57. {pyimouapi-1.4.1 → pyimouapi-1.4.3}/tests/test_set_text_value.py +0 -0
  58. {pyimouapi-1.4.1 → pyimouapi-1.4.3}/tests/test_shared_status_reads.py +0 -0
  59. {pyimouapi-1.4.1 → pyimouapi-1.4.3}/tests/test_siren.py +0 -0
  60. {pyimouapi-1.4.1 → pyimouapi-1.4.3}/tests/test_status_read_concurrency.py +0 -0
  61. {pyimouapi-1.4.1 → pyimouapi-1.4.3}/tests/test_switch_status_aggregation.py +0 -0
  62. {pyimouapi-1.4.1 → pyimouapi-1.4.3}/tests/test_update_from_detail.py +0 -0
  63. {pyimouapi-1.4.1 → pyimouapi-1.4.3}/tests/test_write_optimistic_state.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyimouapi
3
- Version: 1.4.1
3
+ Version: 1.4.3
4
4
  Summary: A package for imou open api
5
5
  Home-page: https://github.com/Imou-OpenPlatform/Py-Imou-Open-Api
6
6
  Author: Imou-OpenPlatform
@@ -1,4 +1,4 @@
1
- __version__ = "1.4.1"
1
+ __version__ = "1.4.3"
2
2
 
3
3
  from .device import ImouChannel, ImouDevice, ImouDeviceManager
4
4
  from .exceptions import (
@@ -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",
@@ -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()
@@ -78,6 +78,7 @@ from .const import (
78
78
  TEXT_TYPE_REF,
79
79
  )
80
80
  from .device import ImouDevice, ImouDeviceManager, compose_iot_device_id
81
+ from .dormant_retry import DORMANT_WRITE_RETRY_WAIT_SECONDS, async_dormant_write_retry
81
82
  from .exceptions import InvalidAppIdOrSecretException, RequestFailedException
82
83
  from .select_option import normalize_options, to_friendly, to_raw
83
84
  from .sensor import apply_sensor_state
@@ -426,13 +427,34 @@ class ImouHaDeviceManager:
426
427
  caller has to be told so it can ask the user to sign in again.
427
428
  """
428
429
 
429
- def __init__(self, device_manager: ImouDeviceManager):
430
+ def __init__(
431
+ self,
432
+ device_manager: ImouDeviceManager,
433
+ *,
434
+ dormant_write_retry_wait_seconds: int = DORMANT_WRITE_RETRY_WAIT_SECONDS,
435
+ ):
430
436
  self._delegate = device_manager
437
+ self._dormant_write_retry_wait_seconds = dormant_write_retry_wait_seconds
431
438
 
432
439
  @property
433
440
  def delegate(self) -> ImouDeviceManager:
434
441
  return self._delegate
435
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
+
436
458
  async def async_close(self) -> None:
437
459
  """Close the underlying Open API HTTP session."""
438
460
  await self._delegate.async_close()
@@ -1154,19 +1176,25 @@ class ImouHaDeviceManager:
1154
1176
  value = str(text_value)
1155
1177
  else:
1156
1178
  value = text_value
1157
- await self.delegate.async_set_iot_device_properties(
1158
- device_id,
1159
- device.channel_id,
1160
- self._require_product_id(device),
1161
- {ref_id: value},
1162
- )
1163
- 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:
1164
1183
  await self.delegate.async_set_iot_device_properties(
1165
- device.device_id,
1166
- None,
1167
- self._require_product_id(device),
1168
- {ref_id: value},
1184
+ device_id,
1185
+ device.channel_id,
1186
+ product_id,
1187
+ payload,
1169
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)
1170
1198
  device.texts[text_type][PARAM_STATE] = str(text_value)
1171
1199
 
1172
1200
  async def async_switch_operation(
@@ -1177,9 +1205,13 @@ class ImouHaDeviceManager:
1177
1205
  device, switch_type, enable, ref_id
1178
1206
  )
1179
1207
  elif switch_type == PARAM_MOTION_DETECT:
1180
- await self.delegate.async_modify_device_alarm_status(
1181
- device.device_id, device.channel_id, enable
1182
- )
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)
1183
1215
  else:
1184
1216
  result = await asyncio.gather(
1185
1217
  *[
@@ -1231,9 +1263,13 @@ class ImouHaDeviceManager:
1231
1263
  )
1232
1264
  device.selects[select_type][PARAM_CURRENT_OPTION] = option
1233
1265
  elif select_type == PARAM_NIGHT_VISION_MODE:
1234
- await self.delegate.async_set_device_night_vision_mode(
1235
- device.device_id, device.channel_id, option
1236
- )
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)
1237
1273
  device.selects[PARAM_NIGHT_VISION_MODE][PARAM_CURRENT_OPTION] = to_friendly(
1238
1274
  PARAM_NIGHT_VISION_MODE, option
1239
1275
  )
@@ -1257,9 +1293,13 @@ class ImouHaDeviceManager:
1257
1293
  async def _async_set_device_switch_status_by_ability(
1258
1294
  self, device: ImouHaDevice, ability_type: str, enable: bool
1259
1295
  ) -> None:
1260
- await self.delegate.async_set_device_status(
1261
- device.device_id, device.channel_id, ability_type, enable
1262
- )
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)
1263
1303
 
1264
1304
  async def _async_update_device_select_status_by_type(
1265
1305
  self, device: ImouHaDevice, select_type: str
@@ -1711,13 +1751,19 @@ class ImouHaDeviceManager:
1711
1751
  value = int(option)
1712
1752
  else:
1713
1753
  value = option
1714
- await self.delegate.async_set_iot_device_properties(
1715
- device_id, device.channel_id, self._require_product_id(device), {ref: value}
1716
- )
1717
- if device.channel_id is not None and device.channel_id == "0":
1754
+ product_id = self._require_product_id(device)
1755
+ payload = {ref: value}
1756
+
1757
+ async def _write() -> None:
1718
1758
  await self.delegate.async_set_iot_device_properties(
1719
- device.device_id, None, self._require_product_id(device), {ref: value}
1759
+ device_id, device.channel_id, product_id, payload
1720
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)
1721
1767
 
1722
1768
  async def async_set_alarm_mode(self, device: ImouHaDevice, mode: str) -> None:
1723
1769
  panel = device.alarm_control_panel
@@ -1738,19 +1784,25 @@ class ImouHaDeviceManager:
1738
1784
  self, device: ImouHaDevice, switch_type: str, enable: bool, ref: str
1739
1785
  ):
1740
1786
  device_id = self._resolve_device_id(device)
1741
- await self.delegate.async_set_iot_device_properties(
1742
- device_id,
1743
- device.channel_id,
1744
- self._require_product_id(device),
1745
- {ref: 1 if enable else 0},
1746
- )
1747
- 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:
1748
1791
  await self.delegate.async_set_iot_device_properties(
1749
- device.device_id,
1750
- None,
1751
- self._require_product_id(device),
1752
- {ref: 1 if enable else 0},
1792
+ device_id,
1793
+ device.channel_id,
1794
+ product_id,
1795
+ payload,
1753
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)
1754
1806
  device.switches[switch_type][PARAM_STATE] = enable
1755
1807
 
1756
1808
  @staticmethod
@@ -1889,9 +1941,14 @@ class ImouHaDeviceManager:
1889
1941
  else:
1890
1942
  # 如果是关的,则倒计时打开
1891
1943
  param["28603"] = 1
1892
- await self.delegate.async_iot_device_control(
1893
- device_id, self._require_product_id(device), "28600", param
1894
- )
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)
1895
1952
  device.texts["count_down_switch"][PARAM_STATE] = str(text_value)
1896
1953
 
1897
1954
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyimouapi
3
- Version: 1.4.1
3
+ Version: 1.4.3
4
4
  Summary: A package for imou open api
5
5
  Home-page: https://github.com/Imou-OpenPlatform/Py-Imou-Open-Api
6
6
  Author: Imou-OpenPlatform
@@ -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
@@ -34,6 +36,7 @@ tests/test_connection_pool.py
34
36
  tests/test_device_image.py
35
37
  tests/test_device_stream.py
36
38
  tests/test_device_summaries.py
39
+ tests/test_dormant_write_retry.py
37
40
  tests/test_entity_tables_golden.py
38
41
  tests/test_event_identifier_map.py
39
42
  tests/test_feature_switches.py
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "pyimouapi"
7
- version = "1.4.1"
7
+ version = "1.4.3"
8
8
  description = "A package for imou open api"
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.11"
@@ -4,7 +4,7 @@ from setuptools import find_packages, setup
4
4
 
5
5
  setup(
6
6
  name="pyimouapi",
7
- version="1.4.1",
7
+ version="1.4.3",
8
8
  packages=find_packages(),
9
9
  package_data={"pyimouapi": ["py.typed"]},
10
10
  zip_safe=False,
@@ -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,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)
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