htd-client-ha 0.1.2__py3-none-any.whl

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.
htd_client/__init__.py ADDED
@@ -0,0 +1,107 @@
1
+ """
2
+ .. code-block:: python
3
+
4
+ # import the client
5
+ from htd_client import HtdClient
6
+
7
+ # Call its only function
8
+ client = HtdClient("192.168.1.2")
9
+
10
+ model_info = client.get_model_info()
11
+ zone_info = client.query_zone(1)
12
+ updated_zone_info = client.volume_up(1)
13
+ """
14
+ import asyncio
15
+ import logging
16
+ from typing import Tuple
17
+
18
+ import htd_client.utils
19
+ from .base_client import BaseClient
20
+ from .constants import HtdCommonCommands, HtdModelInfo, HtdDeviceKind, HtdConstants
21
+ from .lync_client import HtdLyncClient
22
+ from .mca_client import HtdMcaClient
23
+
24
+ _LOGGER = logging.getLogger(__name__)
25
+
26
+
27
+ async def async_get_client(
28
+ serial_address: str = None,
29
+ network_address: Tuple[str, int] = None,
30
+ loop: asyncio.AbstractEventLoop = None,
31
+ retry_attempts: int = HtdConstants.DEFAULT_RETRY_ATTEMPTS,
32
+ ) -> BaseClient:
33
+ """
34
+ Create a new client object.
35
+
36
+ Args:
37
+ network_address (str): The address to communicate with over TCP.
38
+ serial_address (str): The location of the serial port.
39
+ loop (asyncio.AbstractEventLoop): The event loop to use.
40
+ retry_attempts (int): Number of times to retry a command before failing.
41
+
42
+ Returns:
43
+ HtdClient: The new client object.
44
+ """
45
+
46
+ model_info = await async_get_model_info(
47
+ loop if loop is not None else asyncio.get_running_loop(),
48
+ network_address=network_address,
49
+ serial_address=serial_address
50
+ )
51
+
52
+ if model_info["kind"] == HtdDeviceKind.mca:
53
+ client = HtdMcaClient(
54
+ loop if loop is not None else asyncio.get_running_loop(),
55
+ model_info,
56
+ network_address=network_address,
57
+ serial_address=serial_address,
58
+ retry_attempts=retry_attempts,
59
+ )
60
+
61
+ elif model_info["kind"] == HtdDeviceKind.lync:
62
+ client = HtdLyncClient(
63
+ loop if loop is not None else asyncio.get_running_loop(),
64
+ model_info,
65
+ network_address=network_address,
66
+ serial_address=serial_address,
67
+ retry_attempts=retry_attempts,
68
+ )
69
+
70
+ else:
71
+ raise ValueError(f"Unknown Device Kind: {model_info['kind']}")
72
+
73
+ await client.async_connect()
74
+
75
+ return client
76
+
77
+
78
+ async def async_get_model_info(
79
+ loop: asyncio.AbstractEventLoop = None,
80
+ network_address: Tuple[str, int] = None,
81
+ serial_address:str=None,
82
+ ) -> HtdModelInfo | None:
83
+ """
84
+ Get the model information from the gateway.
85
+
86
+ Returns:
87
+ (str, str): the raw model name from the gateway and the friendly
88
+ name, in a Tuple.
89
+ """
90
+
91
+ cmd = htd_client.utils.build_command(
92
+ 1, HtdCommonCommands.MODEL_QUERY_COMMAND_CODE, 0
93
+ )
94
+
95
+ model_id = await htd_client.utils.async_send_command(
96
+ loop if loop is not None else asyncio.get_running_loop(),
97
+ cmd,
98
+ network_address=network_address,
99
+ serial_address=serial_address
100
+ )
101
+
102
+ for model_name in HtdConstants.SUPPORTED_MODELS:
103
+ model = HtdConstants.SUPPORTED_MODELS[model_name]
104
+ if model["identifier"] in model_id:
105
+ return model
106
+
107
+ return None