hyperhdr-py-sickkick 0.1.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.
- hyperhdr_py_sickkick-0.1.0/LICENSE +21 -0
- hyperhdr_py_sickkick-0.1.0/PKG-INFO +573 -0
- hyperhdr_py_sickkick-0.1.0/README.md +548 -0
- hyperhdr_py_sickkick-0.1.0/hyperhdr/__init__.py +13 -0
- hyperhdr_py_sickkick-0.1.0/hyperhdr/client.py +1847 -0
- hyperhdr_py_sickkick-0.1.0/hyperhdr/const.py +203 -0
- hyperhdr_py_sickkick-0.1.0/hyperhdr/py.typed +0 -0
- hyperhdr_py_sickkick-0.1.0/pyproject.toml +76 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020 Dermot Duffy
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,573 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: hyperhdr-py-sickkick
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: HyperHDR Ambient Lighting Python Package
|
|
5
|
+
License: MIT
|
|
6
|
+
License-File: LICENSE
|
|
7
|
+
Keywords: hyperhdr,ambient-lighting,led,hdr
|
|
8
|
+
Author: mjoshd
|
|
9
|
+
Author-email: mjoshd@live.com
|
|
10
|
+
Requires-Python: >=3.9,<4.0
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
13
|
+
Classifier: Operating System :: OS Independent
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
21
|
+
Classifier: Topic :: Home Automation
|
|
22
|
+
Project-URL: Repository, https://github.com/mjoshd/hyperhdr-py
|
|
23
|
+
Description-Content-Type: text/markdown
|
|
24
|
+
|
|
25
|
+
# hyperhdr-py
|
|
26
|
+
|
|
27
|
+
A Python library for controlling [HyperHDR](https://github.com/awawa-dev/HyperHDR) ambient lighting systems.
|
|
28
|
+
|
|
29
|
+
This library is a modified version of [Dermot Duffy](https://github.com/dermotduffy)'s excellent [hyperion-py](https://github.com/dermotduffy/hyperion-py).
|
|
30
|
+
|
|
31
|
+
<img src="https://github.com/mjoshd/hyperhdr-py/blob/main/images/hyperhdrlogo.png?raw=true"
|
|
32
|
+
alt="HyperHDR logo"
|
|
33
|
+
width="160px"
|
|
34
|
+
align="left"
|
|
35
|
+
style="float: left;" />
|
|
36
|
+
|
|
37
|
+
## Features
|
|
38
|
+
|
|
39
|
+
- Full async support with `asyncio`
|
|
40
|
+
- Connect to HyperHDR JSON API
|
|
41
|
+
- Control colors, effects, components, and more
|
|
42
|
+
- Subscribe to real-time state updates
|
|
43
|
+
- Support for HyperHDR v19 through v22+
|
|
44
|
+
|
|
45
|
+
### New in v0.1.0 (HyperHDR v20-v22 support)
|
|
46
|
+
|
|
47
|
+
- **Average color calculation** - Get the average color of current LED output
|
|
48
|
+
- **Smoothing control** - Dynamically adjust smoothing parameters including new v22 interpolators
|
|
49
|
+
- **HDR tone mapping** - Control HDR tone mapping mode including automatic detection (v21+)
|
|
50
|
+
- **Service discovery** - Discover HyperHDR services on the network
|
|
51
|
+
- **Current LED colors** - Get the current colors being sent to LEDs
|
|
52
|
+
- **Performance benchmarking** - Run performance benchmarks
|
|
53
|
+
- **Config management** - Save/load configuration database
|
|
54
|
+
-
|
|
55
|
+
## Installation
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
pip install hyperhdr-py-sickkick
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## Quick Start
|
|
62
|
+
|
|
63
|
+
```python
|
|
64
|
+
import asyncio
|
|
65
|
+
from hyperhdr import client, const
|
|
66
|
+
|
|
67
|
+
async def main():
|
|
68
|
+
async with client.HyperHDRClient("hyperhdr.local") as hc:
|
|
69
|
+
if hc:
|
|
70
|
+
# Get brightness
|
|
71
|
+
adjustment = hc.adjustment
|
|
72
|
+
if adjustment:
|
|
73
|
+
print(f"Brightness: {adjustment[0][const.KEY_BRIGHTNESS]}%")
|
|
74
|
+
|
|
75
|
+
# Set a color
|
|
76
|
+
await hc.async_set_color(color=[255, 0, 0], priority=50)
|
|
77
|
+
|
|
78
|
+
# Set an effect
|
|
79
|
+
await hc.async_set_effect(effect={"name": "Rainbow swirl"}, priority=50)
|
|
80
|
+
|
|
81
|
+
asyncio.run(main())
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
## Thank you dermotduffy!
|
|
85
|
+
|
|
86
|
+
This library would not exist without the excellent work on [hyperion-py](https://github.com/dermotduffy/hyperion-py).
|
|
87
|
+
|
|
88
|
+
Feel free to open an issue if you run into any problems!
|
|
89
|
+
|
|
90
|
+
<!-- [HyperHDR](https://github.com/awawa-dev/HyperHDR). See [JSON
|
|
91
|
+
API](https://docs.hyperhdr-project.org/en/json/) for more details about the
|
|
92
|
+
inputs and outputs of this library.
|
|
93
|
+
|
|
94
|
+
# Installation
|
|
95
|
+
|
|
96
|
+
```bash
|
|
97
|
+
pip3 install hyperhdr-py
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
# Usage
|
|
101
|
+
|
|
102
|
+
## Data model philosophy
|
|
103
|
+
|
|
104
|
+
Whilst not universally true, this library attempts to precisely represent the
|
|
105
|
+
data model, API and parameters as defined in the [HyperHDR JSON
|
|
106
|
+
documentation](https://docs.hyperhdr-project.org/en/json/). Thus no attempt is
|
|
107
|
+
made (intentionally) to present convenient accessors/calls at a finer level of
|
|
108
|
+
granularity than the model already supports. This is to ensure the client has a
|
|
109
|
+
decent chance at staying functional regardless of underlying data model changes
|
|
110
|
+
from the server, and the responsibility to match the changes to the server's
|
|
111
|
+
data model (e.g. new HyperHDR server features) belong to the caller.
|
|
112
|
+
|
|
113
|
+
### Constructor Arguments
|
|
114
|
+
|
|
115
|
+
The following arguments may be passed to the `HyperHDRClient` constructor:
|
|
116
|
+
|
|
117
|
+
|Argument|Type|Default|Description|
|
|
118
|
+
|--------|----|-------|-----------|
|
|
119
|
+
|host |`str`||Host or IP to connect to|
|
|
120
|
+
|port |`int`|19444|Port to connect to|
|
|
121
|
+
|default_callback|`callable`|None|A callable for HyperHDR callbacks. See [callbacks](#callbacks)|
|
|
122
|
+
|callbacks|`dict`|None|A dictionary of callables keyed by the update name. See [callbacks](#callbacks)|
|
|
123
|
+
|token|`str`|None|An authentication token|
|
|
124
|
+
|instance|`int`|0|An instance id to switch to upon connection|
|
|
125
|
+
|origin|`str`|"hyperhdr-py"|An arbitrary string describing the calling application|
|
|
126
|
+
|timeout_secs|`float`|5.0|The number of seconds to wait for a server response or connection attempt before giving up. See [timeouts](#timeouts)|
|
|
127
|
+
|retry_secs|`float`|30.0|The number of seconds between connection attempts|
|
|
128
|
+
|raw_connection|`bool`|False|If True, the connect call will establish the network connection but not attempt to authenticate, switch to the required instance or load state. The client must call `async_client_login` to login, `async_client_switch_instance` to switch to the configured instance and `async_get_serverinfo` to load the state manually. This may be useful if the caller wishes to communicate with the server prior to authentication.|
|
|
129
|
+
|
|
130
|
+
### Connection, disconnection and client control calls
|
|
131
|
+
|
|
132
|
+
* `async_client_connect()`: Connect the client.
|
|
133
|
+
* `async_client_disconnect()`: Disconnect the client.
|
|
134
|
+
* `async_client_login()`: Login a connected client. Automatically called by
|
|
135
|
+
`async_client_connect()` unless the `raw_connection` constructor argument is True.
|
|
136
|
+
* `async_client_switch_instance()`: Switch to the configured instance on the HyperHDR
|
|
137
|
+
server. Automatically called by `async_client_connect()` unless the `raw_connection`
|
|
138
|
+
constructor argument is True.
|
|
139
|
+
|
|
140
|
+
### Native API Calls
|
|
141
|
+
|
|
142
|
+
All API calls can be found in
|
|
143
|
+
[client.py](https://github.com/dermotduffy/hyperhdr-py/blob/master/hyperhdr/client.py).
|
|
144
|
+
All async calls start with `async_`.
|
|
145
|
+
|
|
146
|
+
|Send request and await response|Send request only|Documentation|
|
|
147
|
+
|-------------------------------|-----------------|-------------|
|
|
148
|
+
|async_clear|async_send_clear|[Docs](https://docs.hyperhdr-project.org/en/json/Control.html#clear)|
|
|
149
|
+
|async_image_stream_start|async_send_image_stream_start|[Docs](https://docs.hyperhdr-project.org/en/json/Control.html#live-image-stream)|
|
|
150
|
+
|async_image_stream_stop|async_send_image_stream_stop|[Docs](https://docs.hyperhdr-project.org/en/json/Control.html#live-image-stream)|
|
|
151
|
+
|async_is_auth_required|async_send_is_auth_required|[Docs](https://docs.hyperhdr-project.org/en/json/Authorization.html#authorization-check)|
|
|
152
|
+
|async_led_stream_start|async_send_led_stream_start|[Docs](https://docs.hyperhdr-project.org/en/json/Control.html#live-led-color-stream)|
|
|
153
|
+
|async_led_stream_stop|async_send_led_stream_stop|[Docs](https://docs.hyperhdr-project.org/en/json/Control.html#live-led-color-stream)|
|
|
154
|
+
|async_login|async_send_login|[Docs](https://docs.hyperhdr-project.org/en/json/Authorization.html#login-with-token)|
|
|
155
|
+
|async_logout|async_send_logout|[Docs](https://docs.hyperhdr-project.org/en/json/Authorization.html#logout)|
|
|
156
|
+
|async_request_token|async_send_request_token|[Docs](https://docs.hyperhdr-project.org/en/json/Authorization.html#request-a-token)|
|
|
157
|
+
|async_request_token_abort|async_send_request_token_abort|[Docs](https://docs.hyperhdr-project.org/en/json/Authorization.html#request-a-token)|
|
|
158
|
+
|async_get_serverinfo|async_send_get_serverinfo|[Docs](https://docs.hyperhdr-project.org/en/json/ServerInfo.html#parts)|
|
|
159
|
+
|async_set_adjustment|async_send_set_adjustment|[Docs](https://docs.hyperhdr-project.org/en/json/Control.html#adjustments)|
|
|
160
|
+
|async_set_color|async_send_set_color|[Docs](https://docs.hyperhdr-project.org/en/json/Control.html#set-color)|
|
|
161
|
+
|async_set_component|async_send_set_component|[Docs](https://docs.hyperhdr-project.org/en/json/Control.html#control-components)|
|
|
162
|
+
|async_set_effect|async_send_set_effect|[Docs](https://docs.hyperhdr-project.org/en/json/Control.html#set-effect)|
|
|
163
|
+
|async_set_image|async_send_set_image|[Docs](https://docs.hyperhdr-project.org/en/json/Control.html#set-image)|
|
|
164
|
+
|async_set_led_mapping_type|async_send_set_led_mapping_type|[Docs](https://docs.hyperhdr-project.org/en/json/Control.html#led-mapping)|
|
|
165
|
+
|async_set_sourceselect|async_send_set_sourceselect|[Docs](https://docs.hyperhdr-project.org/en/json/Control.html#source-selection)|
|
|
166
|
+
|async_set_videomode|async_send_set_videomode|[Docs](https://docs.hyperhdr-project.org/en/json/Control.html#video-mode)|
|
|
167
|
+
|async_start_instance|async_send_start_instance|[Docs](https://docs.hyperhdr-project.org/en/json/Control.html#control-instances)|
|
|
168
|
+
|async_stop_instance|async_send_stop_instance|[Docs](https://docs.hyperhdr-project.org/en/json/Control.html#control-instances)|
|
|
169
|
+
|async_switch_instance|async_send_switch_instance|[Docs](https://docs.hyperhdr-project.org/en/json/Control.html#api-instance-handling)|
|
|
170
|
+
|async_sysinfo|async_send_sysinfo|[Docs](https://docs.hyperhdr-project.org/en/json/ServerInfo.html#system-hyperhdr)|
|
|
171
|
+
|
|
172
|
+
Note that the `command` and `subcommand` keys shown in the above linked
|
|
173
|
+
documentation will automatically be included in the calls the client sends, and
|
|
174
|
+
do not need to be specified.
|
|
175
|
+
|
|
176
|
+
## Client inputs / outputs
|
|
177
|
+
|
|
178
|
+
The API parameters and output are all as defined in the [JSON API
|
|
179
|
+
documentation](https://docs.hyperhdr-project.org/en/json/).
|
|
180
|
+
|
|
181
|
+
## Example usage
|
|
182
|
+
|
|
183
|
+
```python
|
|
184
|
+
#!/usr/bin/env python
|
|
185
|
+
"""Simple HyperHDR client read demonstration."""
|
|
186
|
+
|
|
187
|
+
import asyncio
|
|
188
|
+
|
|
189
|
+
from hyperhdr import client, const
|
|
190
|
+
|
|
191
|
+
HOST = "hyperhdr"
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
async def print_brightness() -> None:
|
|
195
|
+
"""Print HyperHDR brightness."""
|
|
196
|
+
|
|
197
|
+
async with client.HyperHDRClient(HOST) as hyperhdr_client:
|
|
198
|
+
assert hyperhdr_client
|
|
199
|
+
|
|
200
|
+
adjustment = hyperhdr_client.adjustment
|
|
201
|
+
assert adjustment
|
|
202
|
+
|
|
203
|
+
print("Brightness: %i%%" % adjustment[0][const.KEY_BRIGHTNESS])
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
if __name__ == "__main__":
|
|
207
|
+
asyncio.get_event_loop().run_until_complete(print_brightness())
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
## Running in the background
|
|
211
|
+
|
|
212
|
+
A background `asyncio task` runs to process all post-connection inbound data
|
|
213
|
+
(e.g. request responses, or subscription updates from state changes on the
|
|
214
|
+
server side). This background task must either be started post-connection, or
|
|
215
|
+
start (and it will itself establish connection).
|
|
216
|
+
|
|
217
|
+
Optionally, this background task can call callbacks back to the user.
|
|
218
|
+
|
|
219
|
+
### Waiting for responses
|
|
220
|
+
|
|
221
|
+
If the user makes a call that does not have `_send_` in the name (see table
|
|
222
|
+
above), the function call will wait for the response and return it to the
|
|
223
|
+
caller. This matching of request & response is done via the `tan` parameter. If
|
|
224
|
+
not specified, the client will automatically attach a `tan` integer, and this
|
|
225
|
+
will be visible in the returned output data. This matching is necessary to
|
|
226
|
+
differentiate between responses due to requests, and "spontaneous data" from
|
|
227
|
+
subscription updates.
|
|
228
|
+
|
|
229
|
+
#### Example: Waiting for a response
|
|
230
|
+
|
|
231
|
+
```python
|
|
232
|
+
#!/usr/bin/env python
|
|
233
|
+
"""Simple HyperHDR client request demonstration."""
|
|
234
|
+
|
|
235
|
+
import asyncio
|
|
236
|
+
|
|
237
|
+
from hyperhdr import client
|
|
238
|
+
|
|
239
|
+
HOST = "hyperhdr"
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
async def print_if_auth_required() -> None:
|
|
243
|
+
"""Print whether auth is required."""
|
|
244
|
+
|
|
245
|
+
hc = client.HyperHDRClient(HOST)
|
|
246
|
+
await hc.async_client_connect()
|
|
247
|
+
|
|
248
|
+
result = await hc.async_is_auth_required()
|
|
249
|
+
print("Result: %s" % result)
|
|
250
|
+
|
|
251
|
+
await hc.async_client_disconnect()
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
asyncio.get_event_loop().run_until_complete(print_if_auth_required())
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
Output:
|
|
258
|
+
|
|
259
|
+
```
|
|
260
|
+
Result: {'command': 'authorize-tokenRequired', 'info': {'required': False}, 'success': True, 'tan': 1}
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
#### Example: Sending commands
|
|
264
|
+
|
|
265
|
+
A slightly more complex example that sends commands (clears the HyperHDR source
|
|
266
|
+
select at a given priority, then sets color at that same priority).
|
|
267
|
+
|
|
268
|
+
```python
|
|
269
|
+
#!/usr/bin/env python
|
|
270
|
+
"""Simple HyperHDR client request demonstration."""
|
|
271
|
+
|
|
272
|
+
import asyncio
|
|
273
|
+
import logging
|
|
274
|
+
import sys
|
|
275
|
+
|
|
276
|
+
from hyperhdr import client
|
|
277
|
+
|
|
278
|
+
HOST = "hyperhdr"
|
|
279
|
+
PRIORITY = 20
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
async def set_color() -> None:
|
|
283
|
+
"""Set red color on HyperHDR."""
|
|
284
|
+
|
|
285
|
+
async with client.HyperHDRClient(HOST) as hc:
|
|
286
|
+
assert hc
|
|
287
|
+
|
|
288
|
+
if not await hc.async_client_connect():
|
|
289
|
+
logging.error("Could not connect to: %s", HOST)
|
|
290
|
+
return
|
|
291
|
+
|
|
292
|
+
if not client.ResponseOK(
|
|
293
|
+
await hc.async_clear(priority=PRIORITY)
|
|
294
|
+
) or not client.ResponseOK(
|
|
295
|
+
await hc.async_set_color(
|
|
296
|
+
color=[255, 0, 0], priority=PRIORITY, origin=sys.argv[0]
|
|
297
|
+
)
|
|
298
|
+
):
|
|
299
|
+
logging.error("Could not clear/set_color on: %s", HOST)
|
|
300
|
+
return
|
|
301
|
+
|
|
302
|
+
|
|
303
|
+
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
|
|
304
|
+
asyncio.get_event_loop().run_until_complete(set_color())
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
#### Example: Starting and switching instances
|
|
308
|
+
|
|
309
|
+
The following example will start a stopped instance, wait for it to be ready,
|
|
310
|
+
then switch to it. Uses [callbacks](#callbacks), discussed below.
|
|
311
|
+
|
|
312
|
+
```python
|
|
313
|
+
#!/usr/bin/env python
|
|
314
|
+
"""Simple HyperHDR client request demonstration."""
|
|
315
|
+
|
|
316
|
+
from __future__ import annotations
|
|
317
|
+
|
|
318
|
+
import asyncio
|
|
319
|
+
import logging
|
|
320
|
+
import sys
|
|
321
|
+
from typing import Any
|
|
322
|
+
|
|
323
|
+
from hyperhdr import client
|
|
324
|
+
|
|
325
|
+
HOST = "hyperhdr"
|
|
326
|
+
PRIORITY = 20
|
|
327
|
+
|
|
328
|
+
|
|
329
|
+
async def instance_start_and_switch() -> None:
|
|
330
|
+
"""Wait for an instance to start."""
|
|
331
|
+
|
|
332
|
+
instance_ready = asyncio.Event()
|
|
333
|
+
|
|
334
|
+
def instance_update(json: dict[str, Any]) -> None:
|
|
335
|
+
for data in json["data"]:
|
|
336
|
+
if data["instance"] == 1 and data["running"]:
|
|
337
|
+
instance_ready.set()
|
|
338
|
+
|
|
339
|
+
async with client.HyperHDRClient(
|
|
340
|
+
HOST, callbacks={"instance-update": instance_update}
|
|
341
|
+
) as hc:
|
|
342
|
+
assert hc
|
|
343
|
+
|
|
344
|
+
if not client.ResponseOK(await hc.async_start_instance(instance=1)):
|
|
345
|
+
logging.error("Could not start instance on: %s", HOST)
|
|
346
|
+
return
|
|
347
|
+
|
|
348
|
+
# Blocks waiting for the instance to start.
|
|
349
|
+
await instance_ready.wait()
|
|
350
|
+
|
|
351
|
+
if not client.ResponseOK(await hc.async_switch_instance(instance=1)):
|
|
352
|
+
logging.error("Could not switch instance on: %s", HOST)
|
|
353
|
+
return
|
|
354
|
+
|
|
355
|
+
|
|
356
|
+
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
|
|
357
|
+
asyncio.get_event_loop().run_until_complete(instance_start_and_switch())
|
|
358
|
+
```
|
|
359
|
+
|
|
360
|
+
<a name="callbacks"></a>
|
|
361
|
+
|
|
362
|
+
### Callbacks
|
|
363
|
+
|
|
364
|
+
The client can be configured to callback as the HyperHDR server reports new
|
|
365
|
+
values. There are two classes of callbacks supported:
|
|
366
|
+
|
|
367
|
+
* **default_callback**: This callback will be called when a more specific callback is not specified.
|
|
368
|
+
* **callbacks**: A dict of callbacks keyed on the HyperHDR subscription 'command' (see [JSON API documentation](https://docs.hyperhdr-project.org/en/json/))
|
|
369
|
+
|
|
370
|
+
Callbacks can be specified in the `HyperHDRClient` constructor
|
|
371
|
+
(`default_callback=` or `callbacks=` arguments) or after construction via the
|
|
372
|
+
`set_callbacks()` and `set_default_callback()` methods.
|
|
373
|
+
|
|
374
|
+
As above, the `callbacks` dict is keyed on the relevant HyperHDR subscription
|
|
375
|
+
`command` (e.g. `components-update`, `priorities-update`). The client also
|
|
376
|
+
provides a custom callback with command `client-update` of the following
|
|
377
|
+
form:
|
|
378
|
+
|
|
379
|
+
```python
|
|
380
|
+
{"command": "client-update",
|
|
381
|
+
"connected": True,
|
|
382
|
+
"logged-in": True,
|
|
383
|
+
"instance": 0,
|
|
384
|
+
"loaded-state": True}
|
|
385
|
+
```
|
|
386
|
+
|
|
387
|
+
This can be used to take special action as the client connects or disconnects from the server.
|
|
388
|
+
|
|
389
|
+
#### Example: Callbacks
|
|
390
|
+
|
|
391
|
+
```python
|
|
392
|
+
#!/usr/bin/env python
|
|
393
|
+
"""Simple HyperHDR client callback demonstration."""
|
|
394
|
+
|
|
395
|
+
from __future__ import annotations
|
|
396
|
+
|
|
397
|
+
import asyncio
|
|
398
|
+
from typing import Any
|
|
399
|
+
|
|
400
|
+
from hyperhdr import client
|
|
401
|
+
|
|
402
|
+
HOST = "hyperhdr"
|
|
403
|
+
|
|
404
|
+
|
|
405
|
+
def callback(json: dict[str, Any]) -> None:
|
|
406
|
+
"""Sample callback function."""
|
|
407
|
+
|
|
408
|
+
print("Received HyperHDR callback: %s" % json)
|
|
409
|
+
|
|
410
|
+
|
|
411
|
+
async def show_callback() -> None:
|
|
412
|
+
"""Show a default callback is called."""
|
|
413
|
+
|
|
414
|
+
async with client.HyperHDRClient(HOST, default_callback=callback):
|
|
415
|
+
pass
|
|
416
|
+
|
|
417
|
+
|
|
418
|
+
if __name__ == "__main__":
|
|
419
|
+
asyncio.get_event_loop().run_until_complete(show_callback())
|
|
420
|
+
```
|
|
421
|
+
|
|
422
|
+
Output, showing the progression of connection stages:
|
|
423
|
+
|
|
424
|
+
```
|
|
425
|
+
Received HyperHDR callback: {'connected': True, 'logged-in': False, 'instance': None, 'loaded-state': False, 'command': 'client-update'}
|
|
426
|
+
Received HyperHDR callback: {'connected': True, 'logged-in': True, 'instance': None, 'loaded-state': False, 'command': 'client-update'}
|
|
427
|
+
Received HyperHDR callback: {'connected': True, 'logged-in': True, 'instance': 0, 'loaded-state': False, 'command': 'client-update'}
|
|
428
|
+
Received HyperHDR callback: {'command': 'serverinfo', ... }
|
|
429
|
+
Received HyperHDR callback: {'connected': True, 'logged-in': True, 'instance': 0, 'loaded-state': True, 'command': 'client-update'}
|
|
430
|
+
```
|
|
431
|
+
|
|
432
|
+
## ThreadedHyperHDRClient
|
|
433
|
+
|
|
434
|
+
A `ThreadedHyperHDRClient` is also provided as a convenience wrapper to for
|
|
435
|
+
non-async code. The `ThreadedHyperHDRClient` wraps the async calls with
|
|
436
|
+
non-async versions (methods are named as shown above, except do not start with
|
|
437
|
+
`async_`).
|
|
438
|
+
|
|
439
|
+
### Waiting for the thread to initialize the client
|
|
440
|
+
|
|
441
|
+
The thread must be given a chance to initialize the client prior to interaction
|
|
442
|
+
with it. This method call will block the caller until the client has been initialized.
|
|
443
|
+
|
|
444
|
+
* wait_for_client_init()
|
|
445
|
+
|
|
446
|
+
### Example use of Threaded client
|
|
447
|
+
|
|
448
|
+
```python
|
|
449
|
+
#!/usr/bin/env python
|
|
450
|
+
"""Simple Threaded HyperHDR client demonstration."""
|
|
451
|
+
|
|
452
|
+
from hyperhdr import client, const
|
|
453
|
+
|
|
454
|
+
HOST = "hyperhdr"
|
|
455
|
+
|
|
456
|
+
if __name__ == "__main__":
|
|
457
|
+
hyperhdr_client = client.ThreadedHyperHDRClient(HOST)
|
|
458
|
+
|
|
459
|
+
# Start the asyncio loop in a new thread.
|
|
460
|
+
hyperhdr_client.start()
|
|
461
|
+
|
|
462
|
+
# Wait for the client to initialize in the new thread.
|
|
463
|
+
hyperhdr_client.wait_for_client_init()
|
|
464
|
+
|
|
465
|
+
# Connect the client.
|
|
466
|
+
hyperhdr_client.client_connect()
|
|
467
|
+
|
|
468
|
+
print("Brightness: %i%%" % hyperhdr_client.adjustment[0][const.KEY_BRIGHTNESS])
|
|
469
|
+
|
|
470
|
+
# Disconnect the client.
|
|
471
|
+
hyperhdr_client.client_disconnect()
|
|
472
|
+
|
|
473
|
+
# Stop the loop (will stop the thread).
|
|
474
|
+
hyperhdr_client.stop()
|
|
475
|
+
|
|
476
|
+
# Join the created thread.
|
|
477
|
+
hyperhdr_client.join()
|
|
478
|
+
```
|
|
479
|
+
|
|
480
|
+
Output:
|
|
481
|
+
|
|
482
|
+
```
|
|
483
|
+
Brightness: 59%
|
|
484
|
+
```
|
|
485
|
+
|
|
486
|
+
## Exceptions / Errors
|
|
487
|
+
|
|
488
|
+
### Philosophy
|
|
489
|
+
|
|
490
|
+
HyperHDRClient strives not to throw an exception regardless of network
|
|
491
|
+
circumstances, reconnection will automatically happen in the background.
|
|
492
|
+
Exceptions are only raised (intentionally) for instances of likely programmer
|
|
493
|
+
error.
|
|
494
|
+
|
|
495
|
+
### HyperHDRError
|
|
496
|
+
|
|
497
|
+
Not directly raised, but other exceptions inherit from this.
|
|
498
|
+
|
|
499
|
+
### HyperHDRClientTanNotAvailable
|
|
500
|
+
|
|
501
|
+
Exception raised if a `tan` parameter is provided to an API call, but that
|
|
502
|
+
`tan` parameter is already being used by another in-progress call. Users
|
|
503
|
+
should either not specify `tan` at all (and the client library will
|
|
504
|
+
automatically manage it in an incremental fashion), or if specified manually,
|
|
505
|
+
it is the caller's responsibility to ensure no two simultaneous calls share a
|
|
506
|
+
`tan` (as otherwise the client would not be able to match the call to the
|
|
507
|
+
response, and this exception will be raised automatically prior to the call).
|
|
508
|
+
|
|
509
|
+
### "Task was destroyed but it is pending!"
|
|
510
|
+
|
|
511
|
+
If a `HyperHDRClient` object is connected but destroyed prior to disconnection, a warning message may be printed ("Task was destroyed but it is pending!"). To avoid this, ensure to always call `async_client_disconnect` prior to destruction of a connected client. Alternatively use the async context manager:
|
|
512
|
+
|
|
513
|
+
```python
|
|
514
|
+
async with client.HyperHDRClient(TEST_HOST, TEST_PORT) as hc:
|
|
515
|
+
if not hc:
|
|
516
|
+
return
|
|
517
|
+
...
|
|
518
|
+
```
|
|
519
|
+
|
|
520
|
+
<a name="timeouts"></a>
|
|
521
|
+
|
|
522
|
+
## Timeouts
|
|
523
|
+
|
|
524
|
+
The client makes liberal use of timeouts, which may be specified at multiple levels:
|
|
525
|
+
|
|
526
|
+
* In the client constructor argument `timeout_secs`, used for connection and requests.
|
|
527
|
+
* In each request using a `timeout_secs` argument to the individual calls
|
|
528
|
+
|
|
529
|
+
Timeout values:
|
|
530
|
+
|
|
531
|
+
* `None`: If `None` is used as a timeout, the client will wait forever.
|
|
532
|
+
* `0`: If `0` is used as a timeout, the client default (specified in the constructor) will be used.
|
|
533
|
+
* `>0.0`: This number of seconds (or partial seconds) will be used.
|
|
534
|
+
|
|
535
|
+
By default, all requests will honour the `timeout_secs` specified in the client constructor unless explicitly overridden and defaults to 5 seconds (see [const.py](https://github.com/dermotduffy/hyperhdr-py/blob/master/hyperhdr/const.py#L95)). The one exception to this is the `async_send_request_token` which has a much larger default (180 seconds, see [const.py](https://github.com/dermotduffy/hyperhdr-py/blob/master/hyperhdr/const.py#L96)) as this request involves the user needing the interact with the HyperHDR UI prior to the call being able to return.
|
|
536
|
+
|
|
537
|
+
## Helpers
|
|
538
|
+
|
|
539
|
+
### ResponseOK
|
|
540
|
+
|
|
541
|
+
A handful of convenience callable classes are provided to determine whether
|
|
542
|
+
server responses were successful.
|
|
543
|
+
|
|
544
|
+
* `ResponseOK`: Whether any HyperHDR command response was successful (general).
|
|
545
|
+
* `ServerInfoResponseOK`: Whether a `async_get_serverinfo` was successful.
|
|
546
|
+
* `LoginResponseOK`: Whether an `async_login` was successful.
|
|
547
|
+
* `SwitchInstanceResponseOK`: Whether an `async_switch_instance` command was successful.
|
|
548
|
+
|
|
549
|
+
#### Example usage
|
|
550
|
+
|
|
551
|
+
```
|
|
552
|
+
if not client.ResponseOK(await hc.async_clear(priority=PRIORITY))
|
|
553
|
+
```
|
|
554
|
+
|
|
555
|
+
### Auth ID
|
|
556
|
+
|
|
557
|
+
When requesting an auth token, a 5-character ID can be specified to ensure the
|
|
558
|
+
admin user is authorizing the right request from the right origin. By default
|
|
559
|
+
the `async_request_token` will randomly generate an ID, but if one is required
|
|
560
|
+
to allow the user to confirm a match, it can be explicitly provided. In this case,
|
|
561
|
+
this helper method is made available.
|
|
562
|
+
|
|
563
|
+
* `generate_random_auth_id`: Generate a random 5-character auth ID for external display and inclusion in a call to `async_request_token`.
|
|
564
|
+
|
|
565
|
+
#### Example usage
|
|
566
|
+
|
|
567
|
+
```
|
|
568
|
+
auth_id = hc.generate_random_auth_id()
|
|
569
|
+
hc.async_send_login(comment="Trustworthy actor", id=auth_id)
|
|
570
|
+
# Show auth_id to the user to allow them to verify the origin of the request,
|
|
571
|
+
# then have them visit the HyperHDR UI.
|
|
572
|
+
``` -->
|
|
573
|
+
|