pyimouapi 1.4.4__tar.gz → 2.0.0__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 (138) hide show
  1. pyimouapi-2.0.0/PKG-INFO +88 -0
  2. pyimouapi-2.0.0/README.md +68 -0
  3. {pyimouapi-1.4.4 → pyimouapi-2.0.0}/pyimouapi/__init__.py +1 -1
  4. {pyimouapi-1.4.4 → pyimouapi-2.0.0}/pyimouapi/alarm_mode.py +1 -1
  5. {pyimouapi-1.4.4 → pyimouapi-2.0.0}/pyimouapi/bulb_light.py +79 -50
  6. pyimouapi-2.0.0/pyimouapi/capability/__init__.py +35 -0
  7. pyimouapi-2.0.0/pyimouapi/capability/binding.py +206 -0
  8. pyimouapi-2.0.0/pyimouapi/capability/catalog.py +149 -0
  9. pyimouapi-2.0.0/pyimouapi/capability/client.py +133 -0
  10. pyimouapi-2.0.0/pyimouapi/capability/setup.py +356 -0
  11. pyimouapi-2.0.0/pyimouapi/capability/v2_invoke.py +43 -0
  12. pyimouapi-2.0.0/pyimouapi/cloud_record.py +85 -0
  13. {pyimouapi-1.4.4 → pyimouapi-2.0.0}/pyimouapi/collection_point.py +0 -12
  14. pyimouapi-2.0.0/pyimouapi/const.py +154 -0
  15. pyimouapi-2.0.0/pyimouapi/data/v2_target_map.yaml +305 -0
  16. pyimouapi-2.0.0/pyimouapi/device.py +1116 -0
  17. {pyimouapi-1.4.4 → pyimouapi-2.0.0}/pyimouapi/dormant_retry.py +1 -2
  18. {pyimouapi-1.4.4 → pyimouapi-2.0.0}/pyimouapi/exceptions.py +9 -1
  19. pyimouapi-2.0.0/pyimouapi/ha_device.py +2262 -0
  20. pyimouapi-2.0.0/pyimouapi/insight/__init__.py +35 -0
  21. pyimouapi-2.0.0/pyimouapi/insight/cloud.py +128 -0
  22. pyimouapi-2.0.0/pyimouapi/insight/search.py +104 -0
  23. pyimouapi-2.0.0/pyimouapi/insight/statistics.py +349 -0
  24. pyimouapi-2.0.0/pyimouapi/local_record.py +210 -0
  25. {pyimouapi-1.4.4 → pyimouapi-2.0.0}/pyimouapi/openapi.py +454 -328
  26. {pyimouapi-1.4.4 → pyimouapi-2.0.0}/pyimouapi/pic_decode.py +9 -8
  27. pyimouapi-2.0.0/pyimouapi/play_token.py +101 -0
  28. pyimouapi-2.0.0/pyimouapi/playback/__init__.py +1 -0
  29. pyimouapi-2.0.0/pyimouapi/playback/flv.py +108 -0
  30. pyimouapi-2.0.0/pyimouapi/playback/hls.py +168 -0
  31. pyimouapi-2.0.0/pyimouapi/product_detail.py +29 -0
  32. pyimouapi-2.0.0/pyimouapi/property_on.py +20 -0
  33. pyimouapi-2.0.0/pyimouapi/push.py +368 -0
  34. pyimouapi-2.0.0/pyimouapi/push_dispatch_allowlist.py +181 -0
  35. {pyimouapi-1.4.4 → pyimouapi-2.0.0}/pyimouapi/select_option.py +45 -3
  36. {pyimouapi-1.4.4 → pyimouapi-2.0.0}/pyimouapi/sensor.py +6 -4
  37. pyimouapi-2.0.0/pyimouapi/siren.py +10 -0
  38. pyimouapi-2.0.0/pyimouapi/snap_detect.py +69 -0
  39. pyimouapi-2.0.0/pyimouapi.egg-info/PKG-INFO +88 -0
  40. pyimouapi-2.0.0/pyimouapi.egg-info/SOURCES.txt +101 -0
  41. pyimouapi-2.0.0/pyimouapi.egg-info/requires.txt +3 -0
  42. {pyimouapi-1.4.4 → pyimouapi-2.0.0}/pyproject.toml +4 -4
  43. {pyimouapi-1.4.4 → pyimouapi-2.0.0}/setup.py +4 -3
  44. {pyimouapi-1.4.4 → pyimouapi-2.0.0}/tests/test_accessory_device_id.py +1 -26
  45. pyimouapi-2.0.0/tests/test_alarm_control_panel.py +51 -0
  46. pyimouapi-2.0.0/tests/test_alarm_message_statistics_request.py +70 -0
  47. pyimouapi-2.0.0/tests/test_applicable_capability_ids.py +46 -0
  48. {pyimouapi-1.4.4 → pyimouapi-2.0.0}/tests/test_auth_failure_propagates.py +4 -55
  49. pyimouapi-2.0.0/tests/test_bulb_composite_gate.py +83 -0
  50. pyimouapi-2.0.0/tests/test_bulb_light.py +304 -0
  51. pyimouapi-2.0.0/tests/test_bulb_v2.py +184 -0
  52. pyimouapi-2.0.0/tests/test_capability_client.py +232 -0
  53. pyimouapi-2.0.0/tests/test_capability_setup.py +457 -0
  54. pyimouapi-2.0.0/tests/test_channel_id_and_api_guards.py +34 -0
  55. pyimouapi-2.0.0/tests/test_cloud_record.py +28 -0
  56. pyimouapi-2.0.0/tests/test_cloud_record_paging.py +350 -0
  57. pyimouapi-2.0.0/tests/test_collection_point.py +44 -0
  58. pyimouapi-2.0.0/tests/test_device_ai_insight_refresh.py +75 -0
  59. pyimouapi-2.0.0/tests/test_device_cloud_refresh.py +82 -0
  60. pyimouapi-2.0.0/tests/test_device_snap_detect.py +68 -0
  61. pyimouapi-2.0.0/tests/test_device_status_get.py +72 -0
  62. {pyimouapi-1.4.4 → pyimouapi-2.0.0}/tests/test_dormant_write_retry.py +29 -27
  63. pyimouapi-2.0.0/tests/test_get_devices.py +189 -0
  64. pyimouapi-2.0.0/tests/test_ha_get_devices_v2.py +308 -0
  65. pyimouapi-2.0.0/tests/test_hls_playlist.py +274 -0
  66. pyimouapi-2.0.0/tests/test_insight_cloud.py +83 -0
  67. pyimouapi-2.0.0/tests/test_insight_search_parse.py +29 -0
  68. pyimouapi-2.0.0/tests/test_insight_search_request.py +58 -0
  69. pyimouapi-2.0.0/tests/test_insight_statistics.py +93 -0
  70. pyimouapi-2.0.0/tests/test_live_stream_source.py +155 -0
  71. pyimouapi-2.0.0/tests/test_local_record.py +339 -0
  72. pyimouapi-2.0.0/tests/test_message_callback.py +109 -0
  73. {pyimouapi-1.4.4 → pyimouapi-2.0.0}/tests/test_openapi_http.py +168 -10
  74. pyimouapi-2.0.0/tests/test_packaging.py +83 -0
  75. {pyimouapi-1.4.4 → pyimouapi-2.0.0}/tests/test_pic_decode.py +4 -4
  76. pyimouapi-2.0.0/tests/test_play_token.py +59 -0
  77. pyimouapi-2.0.0/tests/test_playback_flv.py +21 -0
  78. pyimouapi-2.0.0/tests/test_product_detail.py +219 -0
  79. pyimouapi-2.0.0/tests/test_ptz_relative_fallback.py +194 -0
  80. pyimouapi-2.0.0/tests/test_push.py +429 -0
  81. {pyimouapi-1.4.4 → pyimouapi-2.0.0}/tests/test_select_option.py +4 -30
  82. pyimouapi-2.0.0/tests/test_sensor_normalize.py +41 -0
  83. pyimouapi-2.0.0/tests/test_shared_status_reads.py +585 -0
  84. pyimouapi-2.0.0/tests/test_siren.py +17 -0
  85. pyimouapi-2.0.0/tests/test_status_read_concurrency.py +55 -0
  86. pyimouapi-2.0.0/tests/test_update_from_detail.py +311 -0
  87. pyimouapi-2.0.0/tests/test_v2_control_no_legacy.py +103 -0
  88. pyimouapi-2.0.0/tests/test_v2_countdown.py +66 -0
  89. pyimouapi-2.0.0/tests/test_v2_property_io.py +979 -0
  90. pyimouapi-2.0.0/tests/test_v2_service_io.py +74 -0
  91. pyimouapi-2.0.0/tests/test_v2_target_map.py +87 -0
  92. pyimouapi-1.4.4/PKG-INFO +0 -74
  93. pyimouapi-1.4.4/README.md +0 -55
  94. pyimouapi-1.4.4/pyimouapi/const.py +0 -677
  95. pyimouapi-1.4.4/pyimouapi/device.py +0 -862
  96. pyimouapi-1.4.4/pyimouapi/ha_device.py +0 -2152
  97. pyimouapi-1.4.4/pyimouapi/push.py +0 -187
  98. pyimouapi-1.4.4/pyimouapi/siren.py +0 -15
  99. pyimouapi-1.4.4/pyimouapi.egg-info/PKG-INFO +0 -74
  100. pyimouapi-1.4.4/pyimouapi.egg-info/SOURCES.txt +0 -63
  101. pyimouapi-1.4.4/pyimouapi.egg-info/requires.txt +0 -2
  102. pyimouapi-1.4.4/tests/test_alarm_control_panel.py +0 -65
  103. pyimouapi-1.4.4/tests/test_alarm_pir_switch.py +0 -37
  104. pyimouapi-1.4.4/tests/test_bulb_light.py +0 -252
  105. pyimouapi-1.4.4/tests/test_channel_id_and_api_guards.py +0 -97
  106. pyimouapi-1.4.4/tests/test_collect_property_entities.py +0 -49
  107. pyimouapi-1.4.4/tests/test_collection_point.py +0 -205
  108. pyimouapi-1.4.4/tests/test_entity_tables_golden.py +0 -36
  109. pyimouapi-1.4.4/tests/test_event_identifier_map.py +0 -112
  110. pyimouapi-1.4.4/tests/test_feature_switches.py +0 -88
  111. pyimouapi-1.4.4/tests/test_get_devices.py +0 -198
  112. pyimouapi-1.4.4/tests/test_lookup_property.py +0 -46
  113. pyimouapi-1.4.4/tests/test_message_callback.py +0 -50
  114. pyimouapi-1.4.4/tests/test_motion_detect_excepts.py +0 -89
  115. pyimouapi-1.4.4/tests/test_packaging.py +0 -37
  116. pyimouapi-1.4.4/tests/test_product_id_guard.py +0 -45
  117. pyimouapi-1.4.4/tests/test_push.py +0 -205
  118. pyimouapi-1.4.4/tests/test_sensor_normalize.py +0 -78
  119. pyimouapi-1.4.4/tests/test_services_entities_concurrency.py +0 -123
  120. pyimouapi-1.4.4/tests/test_set_text_value.py +0 -85
  121. pyimouapi-1.4.4/tests/test_shared_status_reads.py +0 -219
  122. pyimouapi-1.4.4/tests/test_siren.py +0 -272
  123. pyimouapi-1.4.4/tests/test_status_read_concurrency.py +0 -156
  124. pyimouapi-1.4.4/tests/test_switch_status_aggregation.py +0 -111
  125. pyimouapi-1.4.4/tests/test_update_from_detail.py +0 -194
  126. pyimouapi-1.4.4/tests/test_write_optimistic_state.py +0 -123
  127. {pyimouapi-1.4.4 → pyimouapi-2.0.0}/LICENSE +0 -0
  128. {pyimouapi-1.4.4 → pyimouapi-2.0.0}/pyimouapi/py.typed +0 -0
  129. {pyimouapi-1.4.4 → pyimouapi-2.0.0}/pyimouapi.egg-info/dependency_links.txt +0 -0
  130. {pyimouapi-1.4.4 → pyimouapi-2.0.0}/pyimouapi.egg-info/not-zip-safe +0 -0
  131. {pyimouapi-1.4.4 → pyimouapi-2.0.0}/pyimouapi.egg-info/top_level.txt +0 -0
  132. {pyimouapi-1.4.4 → pyimouapi-2.0.0}/setup.cfg +0 -0
  133. {pyimouapi-1.4.4 → pyimouapi-2.0.0}/tests/test_alarm_mode.py +0 -0
  134. {pyimouapi-1.4.4 → pyimouapi-2.0.0}/tests/test_bind_device.py +0 -0
  135. {pyimouapi-1.4.4 → pyimouapi-2.0.0}/tests/test_connection_pool.py +0 -0
  136. {pyimouapi-1.4.4 → pyimouapi-2.0.0}/tests/test_device_image.py +0 -0
  137. {pyimouapi-1.4.4 → pyimouapi-2.0.0}/tests/test_device_stream.py +0 -0
  138. {pyimouapi-1.4.4 → pyimouapi-2.0.0}/tests/test_device_summaries.py +0 -0
@@ -0,0 +1,88 @@
1
+ Metadata-Version: 2.4
2
+ Name: pyimouapi
3
+ Version: 2.0.0
4
+ Summary: A package for imou open api
5
+ Home-page: https://github.com/Imou-OpenPlatform/Py-Imou-Open-Api
6
+ Author: Imou-OpenPlatform
7
+ Author-email: Imou-OpenPlatform <cloud_openteam_service@imou.com>
8
+ License: MIT
9
+ Project-URL: Homepage, https://github.com/Imou-OpenPlatform/Py-Imou-Open-Api
10
+ Requires-Python: >=3.11
11
+ Description-Content-Type: text/markdown
12
+ License-File: LICENSE
13
+ Requires-Dist: aiohttp<4.0,>=3.11.9
14
+ Requires-Dist: cryptography>=42.0
15
+ Requires-Dist: pyyaml>=6.0
16
+ Dynamic: author
17
+ Dynamic: home-page
18
+ Dynamic: license-file
19
+ Dynamic: requires-python
20
+
21
+ # pyimouapi
22
+
23
+ Async Python client for the **Imou Open Platform** cloud APIs. Built on **aiohttp**, it handles authentication and requests, and exposes device helpers plus higher-level types for integrations (for example Home Assistant **Imou Life 2.0**).
24
+
25
+ - **Repository:** [Imou-OpenPlatform/Py-Imou-Open-Api](https://github.com/Imou-OpenPlatform/Py-Imou-Open-Api)
26
+ - **Version:** Same as `pyproject.toml` / PyPI (`pyimouapi.__version__`) — **2.0.0** on the `dev` / release line that ships with Imou Life 2.0
27
+ - **Python:** `>= 3.11`
28
+
29
+ ## What 2.0 does
30
+
31
+ Entity registration and control use Open API **V2** capability ids from the device list, intersected with packaged `v2_target_map.yaml`. `device.capability.list` enriches enums, ranges, and scopes for selected devices. Select options use stable friendly keys (`select_option`), including night-vision `starlight` ↔ raw `"5"` and five-level volume `"1"`–`"5"`.
32
+
33
+ ## Package layout
34
+
35
+ | Module | Role |
36
+ |--------|------|
37
+ | `pyimouapi.openapi` | `ImouOpenApiClient` — auth, signing, token lifecycle, HTTP calls |
38
+ | `pyimouapi.device` | `ImouDeviceManager`, `ImouDevice`, `ImouChannel` — listing and related endpoints |
39
+ | `pyimouapi.capability` | V2 capability list / property / service clients and `configure_imou_ha_device_from_capability` |
40
+ | `pyimouapi.data` | Packaged `v2_target_map.yaml` (`ha_key` → V2 `target_id`) |
41
+ | `pyimouapi.ha_device` | `ImouHaDeviceManager`, `ImouHaDevice` — aggregated device model for automation stacks |
42
+ | `pyimouapi.select_option` | Friendly keys for `device_volume`, `night_vision_mode`, `wide_dynamic` |
43
+ | `pyimouapi.alarm_mode` | Alarm panel mode friendly ↔ raw mapping |
44
+ | `pyimouapi.push` | Event-push helpers: normalize fields, alarm vs status, IoT envelopes, picture URLs |
45
+ | `pyimouapi.pic_decode` | `LCOpenPicDecoder` — decrypts alarm pictures you already downloaded (official LCOpenSDK `.so`, linux x86-64) |
46
+ | `pyimouapi.play_token` | Play-token / encrypt-key helpers for picture decrypt |
47
+ | `pyimouapi.cloud_record` / `local_record` | Cloud and local recording listing |
48
+ | `pyimouapi.playback` | HLS / FLV playback URL helpers |
49
+ | `pyimouapi.insight` | AI Insight cloud status, alarm search, daily statistics |
50
+ | `pyimouapi.snap_detect` | `detectDeviceSnap` / `deviceSnapDetect` client and result parsing |
51
+ | `pyimouapi.bulb_light` | Color-bulb composite property writes |
52
+ | `pyimouapi.collection_point` | Collection-point name parsing |
53
+ | `pyimouapi.exceptions` | `ImouException` and typed errors |
54
+
55
+ The top-level `pyimouapi` package re-exports common symbols. Import submodules directly when needed, for example `from pyimouapi.ha_device import ImouHaDeviceManager`.
56
+
57
+ ## Dependencies
58
+
59
+ As declared in `pyproject.toml` / `requirements.txt`:
60
+
61
+ - `aiohttp>=3.11.9,<4.0`
62
+
63
+ ## Install
64
+
65
+ ```bash
66
+ pip install pyimouapi
67
+ ```
68
+
69
+ From a checkout:
70
+
71
+ ```bash
72
+ pip install .
73
+ ```
74
+
75
+ ## Quick example (async)
76
+
77
+ ```python
78
+ from pyimouapi.openapi import ImouOpenApiClient
79
+
80
+ # host: Imou Open Platform gateway, e.g. openapi-sg.easy4ip.com
81
+ client = ImouOpenApiClient("your_app_id", "your_app_secret", "openapi-sg.easy4ip.com")
82
+ await client.async_get_token()
83
+ # Use the client with pyimouapi.ha_device.ImouHaDeviceManager for integrations
84
+ ```
85
+
86
+ ## License
87
+
88
+ MIT — see `LICENSE` in this repository.
@@ -0,0 +1,68 @@
1
+ # pyimouapi
2
+
3
+ Async Python client for the **Imou Open Platform** cloud APIs. Built on **aiohttp**, it handles authentication and requests, and exposes device helpers plus higher-level types for integrations (for example Home Assistant **Imou Life 2.0**).
4
+
5
+ - **Repository:** [Imou-OpenPlatform/Py-Imou-Open-Api](https://github.com/Imou-OpenPlatform/Py-Imou-Open-Api)
6
+ - **Version:** Same as `pyproject.toml` / PyPI (`pyimouapi.__version__`) — **2.0.0** on the `dev` / release line that ships with Imou Life 2.0
7
+ - **Python:** `>= 3.11`
8
+
9
+ ## What 2.0 does
10
+
11
+ Entity registration and control use Open API **V2** capability ids from the device list, intersected with packaged `v2_target_map.yaml`. `device.capability.list` enriches enums, ranges, and scopes for selected devices. Select options use stable friendly keys (`select_option`), including night-vision `starlight` ↔ raw `"5"` and five-level volume `"1"`–`"5"`.
12
+
13
+ ## Package layout
14
+
15
+ | Module | Role |
16
+ |--------|------|
17
+ | `pyimouapi.openapi` | `ImouOpenApiClient` — auth, signing, token lifecycle, HTTP calls |
18
+ | `pyimouapi.device` | `ImouDeviceManager`, `ImouDevice`, `ImouChannel` — listing and related endpoints |
19
+ | `pyimouapi.capability` | V2 capability list / property / service clients and `configure_imou_ha_device_from_capability` |
20
+ | `pyimouapi.data` | Packaged `v2_target_map.yaml` (`ha_key` → V2 `target_id`) |
21
+ | `pyimouapi.ha_device` | `ImouHaDeviceManager`, `ImouHaDevice` — aggregated device model for automation stacks |
22
+ | `pyimouapi.select_option` | Friendly keys for `device_volume`, `night_vision_mode`, `wide_dynamic` |
23
+ | `pyimouapi.alarm_mode` | Alarm panel mode friendly ↔ raw mapping |
24
+ | `pyimouapi.push` | Event-push helpers: normalize fields, alarm vs status, IoT envelopes, picture URLs |
25
+ | `pyimouapi.pic_decode` | `LCOpenPicDecoder` — decrypts alarm pictures you already downloaded (official LCOpenSDK `.so`, linux x86-64) |
26
+ | `pyimouapi.play_token` | Play-token / encrypt-key helpers for picture decrypt |
27
+ | `pyimouapi.cloud_record` / `local_record` | Cloud and local recording listing |
28
+ | `pyimouapi.playback` | HLS / FLV playback URL helpers |
29
+ | `pyimouapi.insight` | AI Insight cloud status, alarm search, daily statistics |
30
+ | `pyimouapi.snap_detect` | `detectDeviceSnap` / `deviceSnapDetect` client and result parsing |
31
+ | `pyimouapi.bulb_light` | Color-bulb composite property writes |
32
+ | `pyimouapi.collection_point` | Collection-point name parsing |
33
+ | `pyimouapi.exceptions` | `ImouException` and typed errors |
34
+
35
+ The top-level `pyimouapi` package re-exports common symbols. Import submodules directly when needed, for example `from pyimouapi.ha_device import ImouHaDeviceManager`.
36
+
37
+ ## Dependencies
38
+
39
+ As declared in `pyproject.toml` / `requirements.txt`:
40
+
41
+ - `aiohttp>=3.11.9,<4.0`
42
+
43
+ ## Install
44
+
45
+ ```bash
46
+ pip install pyimouapi
47
+ ```
48
+
49
+ From a checkout:
50
+
51
+ ```bash
52
+ pip install .
53
+ ```
54
+
55
+ ## Quick example (async)
56
+
57
+ ```python
58
+ from pyimouapi.openapi import ImouOpenApiClient
59
+
60
+ # host: Imou Open Platform gateway, e.g. openapi-sg.easy4ip.com
61
+ client = ImouOpenApiClient("your_app_id", "your_app_secret", "openapi-sg.easy4ip.com")
62
+ await client.async_get_token()
63
+ # Use the client with pyimouapi.ha_device.ImouHaDeviceManager for integrations
64
+ ```
65
+
66
+ ## License
67
+
68
+ MIT — see `LICENSE` in this repository.
@@ -1,4 +1,4 @@
1
- __version__ = "1.4.4"
1
+ __version__ = "2.0.0"
2
2
 
3
3
  from .device import ImouChannel, ImouDevice, ImouDeviceManager
4
4
  from .exceptions import (
@@ -1,4 +1,4 @@
1
- """Friendly keys for IoT alarm mode (ref 15200)."""
1
+ """Friendly keys for the arming mode property."""
2
2
 
3
3
  from __future__ import annotations
4
4
 
@@ -6,16 +6,18 @@ light itself, not a separate switch.
6
6
 
7
7
  from __future__ import annotations
8
8
 
9
+ from collections.abc import Set as AbstractSet
10
+ from math import isfinite
9
11
  from typing import Any
10
12
 
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"
13
+ BULB_PROP_SWITCH = "control.outlet.enabled"
14
+ BULB_PROP_MODE = "light.white.mode"
15
+ BULB_PROP_BRIGHTNESS = "light.white.level"
16
+ BULB_PROP_COLOR_TEMP = "light.white.colorTemperature"
17
+ BULB_PROP_HUE = "light.white.hue"
18
+ BULB_PROP_SATURATION = "light.white.saturation"
19
+ BULB_PROP_VALUE = "light.white.colorLevel"
20
+ BULB_PROP_SCENE = "light.white.scene"
19
21
 
20
22
  MODE_WHITE = 0
21
23
  MODE_COLOR = 1
@@ -24,17 +26,25 @@ MODE_SCENE = 2
24
26
  MIN_KELVIN = 2700
25
27
  MAX_KELVIN = 6500
26
28
 
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,
29
+ BULB_V2_PROPERTY_IDS = (
30
+ BULB_PROP_SWITCH,
31
+ BULB_PROP_MODE,
32
+ BULB_PROP_BRIGHTNESS,
33
+ BULB_PROP_COLOR_TEMP,
34
+ BULB_PROP_HUE,
35
+ BULB_PROP_SATURATION,
36
+ BULB_PROP_VALUE,
37
+ BULB_PROP_SCENE,
36
38
  )
37
39
 
40
+
41
+ def bulb_v2_property_ids_for_device(
42
+ supported_property_ids: AbstractSet[str],
43
+ ) -> tuple[str, ...]:
44
+ """Property ids to read for a v2 bulb, limited to what capability.list reported."""
45
+ return tuple(pid for pid in BULB_V2_PROPERTY_IDS if pid in supported_property_ids)
46
+
47
+
38
48
  # Effect name shown on the light, and the scene enum the bulb stores.
39
49
  BULB_SCENES: tuple[tuple[str, int], ...] = (
40
50
  ("Daylight", 0),
@@ -52,7 +62,7 @@ _SCENE_BY_NAME = dict(BULB_SCENES)
52
62
  _NAME_BY_SCENE = {value: name for name, value in BULB_SCENES}
53
63
 
54
64
 
55
- def empty_bulb_state() -> dict[str, int | bool]:
65
+ def empty_bulb_state() -> dict[str, Any]:
56
66
  """Defaults match the thing model until the first property read."""
57
67
  return {
58
68
  "on": False,
@@ -90,10 +100,12 @@ def brightness_to_percent(brightness: int) -> int:
90
100
 
91
101
  def _as_int(raw: Any) -> int | None:
92
102
  if isinstance(raw, bool):
93
- return int(raw)
103
+ return None
94
104
  if isinstance(raw, int):
95
105
  return raw
96
106
  if isinstance(raw, float):
107
+ if not isfinite(raw):
108
+ return None
97
109
  return int(raw)
98
110
  if isinstance(raw, str):
99
111
  text = raw.strip()
@@ -102,36 +114,51 @@ def _as_int(raw: Any) -> int | None:
102
114
  return None
103
115
 
104
116
 
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]
117
+ def apply_bulb_v2_properties(state: dict[str, Any], values: dict[str, Any]) -> bool:
118
+ """Apply accepted property ids and report whether any were assigned."""
119
+ return apply_bulb_properties(state, values)
120
+
121
+
122
+ def apply_bulb_properties(state: dict[str, Any], values: dict[str, Any]) -> bool:
123
+ """Apply accepted property ids and report whether normalized state changed."""
124
+ changed = False
125
+ if BULB_PROP_SWITCH in values:
126
+ raw = values[BULB_PROP_SWITCH]
109
127
  if isinstance(raw, bool):
110
- state["on"] = raw
128
+ if state.get("on") is not raw:
129
+ state["on"] = raw
130
+ changed = True
111
131
  else:
112
132
  parsed = _as_int(raw)
113
133
  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),
134
+ is_on = parsed == 1
135
+ if state.get("on") is not is_on:
136
+ state["on"] = is_on
137
+ changed = True
138
+ for key, property_id in (
139
+ ("mode", BULB_PROP_MODE),
140
+ ("brightness", BULB_PROP_BRIGHTNESS),
141
+ ("color_temp", BULB_PROP_COLOR_TEMP),
142
+ ("hue", BULB_PROP_HUE),
143
+ ("saturation", BULB_PROP_SATURATION),
144
+ ("value", BULB_PROP_VALUE),
145
+ ("scene", BULB_PROP_SCENE),
123
146
  ):
124
- if ref not in values:
147
+ if property_id not in values:
125
148
  continue
126
- parsed = _as_int(values[ref])
149
+ parsed = _as_int(values[property_id])
127
150
  if parsed is None:
128
151
  continue
129
- state[key] = _clamp_read(key, parsed)
152
+ normalized = _clamp_read(key, parsed)
153
+ if state.get(key) != normalized:
154
+ state[key] = normalized
155
+ changed = True
156
+ return changed
130
157
 
131
158
 
132
159
  def properties_for_turn_off() -> dict[str, int]:
133
160
  """Turn the bulb off."""
134
- return {REF_SWITCH: 0}
161
+ return {BULB_PROP_SWITCH: 0}
135
162
 
136
163
 
137
164
  def properties_for_turn_on(
@@ -149,42 +176,44 @@ def properties_for_turn_on(
149
176
  caller passed, so moving color temperature does not copy the color
150
177
  brightness, and moving the color does not copy the white brightness.
151
178
  """
152
- payload: dict[str, int] = {REF_SWITCH: 1}
179
+ payload: dict[str, int] = {BULB_PROP_SWITCH: 1}
153
180
  if effect is not None:
154
181
  scene = _SCENE_BY_NAME.get(effect)
155
182
  if scene is None:
156
183
  raise ValueError(f"unknown bulb effect: {effect}")
157
- payload[REF_MODE] = MODE_SCENE
158
- payload[REF_SCENE] = scene
184
+ payload[BULB_PROP_MODE] = MODE_SCENE
185
+ payload[BULB_PROP_SCENE] = scene
159
186
  return payload
160
187
  if hs_color is not None:
161
188
  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)))
189
+ payload[BULB_PROP_MODE] = MODE_COLOR
190
+ payload[BULB_PROP_HUE] = max(0, min(360, round(hue)))
191
+ payload[BULB_PROP_SATURATION] = max(0, min(100, round(saturation)))
165
192
  if brightness is not None:
166
- payload[REF_VALUE] = max(1, min(100, brightness_to_percent(brightness)))
193
+ payload[BULB_PROP_VALUE] = max(
194
+ 1, min(100, brightness_to_percent(brightness))
195
+ )
167
196
  return payload
168
197
  if color_temp_kelvin is not None:
169
- payload[REF_MODE] = MODE_WHITE
170
- payload[REF_COLOR_TEMP] = max(
198
+ payload[BULB_PROP_MODE] = MODE_WHITE
199
+ payload[BULB_PROP_COLOR_TEMP] = max(
171
200
  MIN_KELVIN, min(MAX_KELVIN, int(color_temp_kelvin))
172
201
  )
173
202
  if brightness is not None:
174
- payload[REF_BRIGHTNESS] = max(
203
+ payload[BULB_PROP_BRIGHTNESS] = max(
175
204
  1, min(100, brightness_to_percent(brightness))
176
205
  )
177
206
  return payload
178
207
  if brightness is not None:
179
208
  level = brightness_to_percent(brightness)
180
209
  if int(state["mode"]) == MODE_COLOR:
181
- payload[REF_VALUE] = level
210
+ payload[BULB_PROP_VALUE] = level
182
211
  else:
183
212
  # A scene (or any non-white mode) does not use the white slider.
184
213
  # Brightness alone moves the bulb back to white so the lamp matches.
185
214
  if int(state["mode"]) != MODE_WHITE:
186
- payload[REF_MODE] = MODE_WHITE
187
- payload[REF_BRIGHTNESS] = level
215
+ payload[BULB_PROP_MODE] = MODE_WHITE
216
+ payload[BULB_PROP_BRIGHTNESS] = level
188
217
  return payload
189
218
 
190
219
 
@@ -0,0 +1,35 @@
1
+ """Standard capability (Open API v2) helpers."""
2
+
3
+ from .binding import (
4
+ TargetBinding,
5
+ bulb_gate_satisfied,
6
+ load_v2_target_map,
7
+ resolve_ptz_target_id,
8
+ )
9
+ from .catalog import (
10
+ CapabilitySet,
11
+ PropertyCapability,
12
+ capability_set_from_list_response,
13
+ )
14
+ from .client import (
15
+ async_capability_list,
16
+ async_property_get,
17
+ async_property_set,
18
+ async_service_invoke,
19
+ )
20
+ from .setup import configure_imou_ha_device_from_capability
21
+
22
+ __all__ = [
23
+ "CapabilitySet",
24
+ "PropertyCapability",
25
+ "TargetBinding",
26
+ "async_capability_list",
27
+ "async_property_get",
28
+ "async_property_set",
29
+ "async_service_invoke",
30
+ "bulb_gate_satisfied",
31
+ "capability_set_from_list_response",
32
+ "configure_imou_ha_device_from_capability",
33
+ "load_v2_target_map",
34
+ "resolve_ptz_target_id",
35
+ ]
@@ -0,0 +1,206 @@
1
+ """Load Ha V2 target bindings and resolve PTZ / bulb gate rules."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from collections.abc import Iterable
6
+ from collections.abc import Set as AbstractSet
7
+ from dataclasses import dataclass, field
8
+ from functools import lru_cache
9
+ from importlib.resources import files
10
+ from pathlib import Path
11
+ from typing import Any
12
+
13
+ import yaml
14
+
15
+ from ..bulb_light import BULB_PROP_SWITCH
16
+
17
+ PACKAGE_DATA = files("pyimouapi").joinpath("data")
18
+ MAP_FILE = "v2_target_map.yaml"
19
+
20
+
21
+ @dataclass(frozen=True, slots=True)
22
+ class TargetBinding:
23
+ """One ha_key row from v2_target_map.yaml."""
24
+
25
+ ha_key: str
26
+ kind: str
27
+ ha_platform: str
28
+ scope: str = "channel"
29
+ target_id: str | None = None
30
+ capability_id: str | None = None
31
+ write_target_id: str | None = None
32
+ alternate_target_id: str | None = None
33
+ fallback_target_id: str | None = None
34
+ resolve: str | None = None
35
+ output_field: str | None = None
36
+ struct_field: str | None = None
37
+ gate_property: str | None = None
38
+ properties: tuple[str, ...] = field(default_factory=tuple)
39
+ extra_services: tuple[str, ...] = field(default_factory=tuple)
40
+
41
+
42
+ def _coerce_tuple(value: Any) -> tuple[str, ...]:
43
+ if not value:
44
+ return ()
45
+ if isinstance(value, str):
46
+ return (value,)
47
+ return tuple(str(item) for item in value)
48
+
49
+
50
+ def _parse_binding(raw: dict[str, Any]) -> TargetBinding:
51
+ return TargetBinding(
52
+ ha_key=str(raw["ha_key"]),
53
+ kind=str(raw["kind"]),
54
+ ha_platform=str(raw["ha_platform"]),
55
+ scope=str(raw.get("scope", "channel")),
56
+ target_id=raw.get("target_id"),
57
+ capability_id=raw.get("capability_id"),
58
+ write_target_id=raw.get("write_target_id"),
59
+ alternate_target_id=raw.get("alternate_target_id"),
60
+ fallback_target_id=raw.get("fallback_target_id"),
61
+ resolve=raw.get("resolve"),
62
+ output_field=raw.get("output_field"),
63
+ struct_field=raw.get("struct_field"),
64
+ gate_property=raw.get("gate_property"),
65
+ properties=_coerce_tuple(raw.get("properties")),
66
+ extra_services=_coerce_tuple(raw.get("extra_services")),
67
+ )
68
+
69
+
70
+ def load_v2_target_map(path: Path | None = None) -> list[TargetBinding]:
71
+ """Load bindings from packaged yaml unless an explicit path is given."""
72
+ if path is None:
73
+ data = (PACKAGE_DATA / MAP_FILE).read_text(encoding="utf-8")
74
+ else:
75
+ data = path.read_text(encoding="utf-8")
76
+ document = yaml.safe_load(data)
77
+ if not isinstance(document, dict):
78
+ msg = "v2_target_map root must be a mapping"
79
+ raise ValueError(msg)
80
+ rows = document.get("bindings")
81
+ if not isinstance(rows, list):
82
+ msg = "v2_target_map missing bindings list"
83
+ raise ValueError(msg)
84
+ return [_parse_binding(row) for row in rows]
85
+
86
+
87
+ @lru_cache
88
+ def cached_v2_target_map() -> tuple[TargetBinding, ...]:
89
+ """Cached packaged map for repeated gate checks."""
90
+ return tuple(load_v2_target_map())
91
+
92
+
93
+ # Load once at import so discovery does not read yaml on the event loop.
94
+ cached_v2_target_map()
95
+
96
+
97
+ def resolve_ptz_target_id(
98
+ capability_services: set[str],
99
+ primary: str = "ptz.aim.step",
100
+ alternate: str = "ptz.aim.relative",
101
+ ) -> str | None:
102
+ """Pick PTZ invoke id: prefer step when both are listed."""
103
+ has_primary = primary in capability_services
104
+ has_alternate = alternate in capability_services
105
+ if has_primary:
106
+ return primary
107
+ if has_alternate:
108
+ return alternate
109
+ return None
110
+
111
+
112
+ def bulb_gate_satisfied(
113
+ device_type: str | None,
114
+ property_ids: AbstractSet[str],
115
+ ) -> bool:
116
+ """True for a Lamp that exposes its controllable power property."""
117
+ return (
118
+ isinstance(device_type, str)
119
+ and device_type.casefold() == "lamp"
120
+ and BULB_PROP_SWITCH in property_ids
121
+ )
122
+
123
+
124
+ def binding_by_ha_key(bindings: list[TargetBinding]) -> dict[str, TargetBinding]:
125
+ """Index bindings by ha_key (last wins if duplicates)."""
126
+ return {row.ha_key: row for row in bindings}
127
+
128
+
129
+ @lru_cache
130
+ def property_scope_from_target_map(property_id: str) -> str | None:
131
+ """Return device/channel from the packaged map when capability.list omits scope."""
132
+ for row in cached_v2_target_map():
133
+ if row.kind != "property":
134
+ continue
135
+ if property_id in (
136
+ row.target_id,
137
+ row.fallback_target_id,
138
+ row.write_target_id,
139
+ ) and row.scope in ("device", "channel"):
140
+ return row.scope
141
+ return None
142
+
143
+
144
+ def applicable_capability_ids(
145
+ device_ids: Iterable[str],
146
+ channel_ids: Iterable[str],
147
+ *,
148
+ is_ipc: bool,
149
+ channel_id: str | None,
150
+ ) -> frozenset[str]:
151
+ """Ids that may create entities on this device or channel row."""
152
+ if channel_id is None:
153
+ return frozenset(device_ids)
154
+ ids = set(channel_ids)
155
+ if is_ipc and channel_id == "0":
156
+ ids.update(device_ids)
157
+ return frozenset(ids)
158
+
159
+
160
+ def binding_resolved_service_id(
161
+ binding: TargetBinding, capability_services: set[str]
162
+ ) -> str | None:
163
+ """Resolve PTZ and similar rows to the invoke id present on the device."""
164
+ if binding.resolve == "ptz_prefer_step":
165
+ return resolve_ptz_target_id(capability_services)
166
+ return binding.target_id
167
+
168
+
169
+ def binding_supported(
170
+ binding: TargetBinding,
171
+ capability_ids: AbstractSet[str],
172
+ *,
173
+ device_type: str | None = None,
174
+ ) -> bool:
175
+ """True when the page ids contain this row's required targets."""
176
+ if binding.kind == "named_api":
177
+ gate_id = binding.capability_id or binding.target_id
178
+ return bool(gate_id and gate_id in capability_ids)
179
+ if binding.kind == "composite":
180
+ return bulb_gate_satisfied(device_type, capability_ids)
181
+ if binding.kind == "property":
182
+ return bool(
183
+ (binding.target_id and binding.target_id in capability_ids)
184
+ or (
185
+ binding.fallback_target_id
186
+ and binding.fallback_target_id in capability_ids
187
+ )
188
+ )
189
+ if binding.kind == "service":
190
+ target = binding.target_id
191
+ if binding.resolve:
192
+ target = binding_resolved_service_id(binding, set(capability_ids))
193
+ if not target or target not in capability_ids:
194
+ return False
195
+ write_id = binding.write_target_id
196
+ if write_id is not None and write_id not in capability_ids:
197
+ return False
198
+ list_services = [
199
+ service for service in binding.extra_services if service.endswith(".list")
200
+ ]
201
+ return not list_services or any(
202
+ service in capability_ids for service in list_services
203
+ )
204
+ if binding.kind == "event":
205
+ return bool(binding.target_id and binding.target_id in capability_ids)
206
+ return False