-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path__init__.py
More file actions
302 lines (263 loc) · 11 KB
/
__init__.py
File metadata and controls
302 lines (263 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
"""Manage the connection and communication flow through the USB-Stick."""
from __future__ import annotations
from collections.abc import Awaitable, Callable, Coroutine
import logging
from typing import Any
from ..api import StickEvent
from ..constants import UTF8
from ..exceptions import MessageError, NodeError, StickError
from ..helpers.util import validate_mac, version_to_model
from ..messages.requests import (
CirclePlusConnectRequest,
NodeInfoRequest,
NodePingRequest,
PlugwiseRequest,
StickInitRequest,
StickNetworkInfoRequest,
)
from ..messages.responses import (
NodeInfoResponse,
NodePingResponse,
PlugwiseResponse,
StickInitResponse,
StickInitShortResponse,
)
from .manager import StickConnectionManager
from .queue import StickQueue
_LOGGER = logging.getLogger(__name__)
class StickController:
"""Manage the connection and communication towards USB-Stick."""
def __init__(self) -> None:
"""Initialize Stick controller."""
self._manager = StickConnectionManager()
self._queue = StickQueue()
self._unsubscribe_stick_event: Callable[[], None] | None = None
self._init_sequence_id: bytes | None = None
self._is_initialized = False
self._fw_stick: str | None = None
self._hw_stick: str | None = None
self._mac_stick: str | None = None
self._mac_nc: str | None = None
self._network_id: int | None = None
self._network_online = False
self.stick_name: str | None = None
@property
def is_initialized(self) -> bool:
"""Returns True if UBS-Stick connection is active and initialized."""
if not self._manager.is_connected:
return False
return self._is_initialized
@property
def is_connected(self) -> bool:
"""Return connection state from connection manager."""
return self._manager.is_connected
@property
def firmware_stick(self) -> str | None:
"""Firmware version of the Stick."""
return self._fw_stick
@property
def hardware_stick(self) -> str | None:
"""Hardware version of the Stick."""
return self._hw_stick
@property
def mac_stick(self) -> str | None:
"""MAC address of USB-Stick."""
return self._mac_stick
@property
def mac_coordinator(self) -> str | None:
"""Return MAC address of the Zigbee network coordinator (Circle+)."""
return self._mac_nc
@property
def network_id(self) -> int | None:
"""Returns the Zigbee network ID."""
return self._network_id
@property
def network_online(self) -> bool:
"""Return the network state.
The ZigBee network is online when the Stick is connected and a
StickInitResponse indicates that the ZigBee network is online.
"""
if not self._manager.is_connected:
raise StickError(
"Network status not available. Connect and initialize USB-Stick first."
)
return self._network_online
async def connect_to_stick(self, serial_path: str) -> None:
"""Connect to USB stick."""
if self._manager.is_connected:
raise StickError("Already connected")
await self._manager.setup_connection_to_stick(serial_path)
if self._unsubscribe_stick_event is None:
self._unsubscribe_stick_event = self._manager.subscribe_to_stick_events(
self._handle_stick_event,
(StickEvent.CONNECTED, StickEvent.DISCONNECTED),
)
self._queue.start(self._manager)
def subscribe_to_stick_events(
self,
stick_event_callback: Callable[[StickEvent], Awaitable[None]],
events: tuple[StickEvent, ...],
) -> Callable[[], None]:
"""Subscribe callback when specified StickEvent occurs.
Returns the function to be called to unsubscribe later.
"""
if self._manager is None:
raise StickError("Connect to stick before subscribing to events")
return self._manager.subscribe_to_stick_events(
stick_event_callback,
events,
)
async def subscribe_to_messages(
self,
node_response_callback: Callable[[PlugwiseResponse], Coroutine[Any, Any, bool]],
mac: bytes | None = None,
message_ids: tuple[bytes] | None = None,
seq_id: bytes | None = None,
) -> Callable[[], None]:
"""Subscribe a awaitable callback to be called when a specific message is received.
Returns function to unsubscribe.
"""
return await self._manager.subscribe_to_messages(
node_response_callback, mac, message_ids, seq_id
)
async def _handle_stick_event(self, event: StickEvent) -> None:
"""Handle stick event."""
if event == StickEvent.CONNECTED:
if not self._queue.is_running:
self._queue.start(self._manager)
await self.initialize_stick()
elif event == StickEvent.DISCONNECTED and self._queue.is_running:
await self._queue.stop()
async def initialize_stick(self, node_info=True) -> None:
"""Initialize connection to the USB-stick."""
if not self._manager.is_connected:
raise StickError(
"Cannot initialize USB-stick, connected to USB-stick first"
)
if not self._queue.is_running:
raise StickError("Cannot initialize, queue manager not running")
try:
request = StickInitRequest(self.send)
init_response: (
StickInitResponse | StickInitShortResponse | None
) = await request.send()
except StickError as err:
raise StickError(
"No response from USB-Stick to initialization request."
+ " Validate USB-stick is connected to port "
+ f"' {self._manager.serial_path}'"
) from err
if init_response is None:
raise StickError(
"No response from USB-Stick to initialization request."
+ " Validate USB-stick is connected to port "
+ f"' {self._manager.serial_path}'"
)
self._mac_stick = init_response.mac_decoded
self.stick_name = f"Stick {self._mac_stick[-5:]}"
self._network_online = init_response.network_online
if self._network_online:
# Replace first 2 characters by 00 for mac of circle+ node
self._mac_nc = init_response.mac_network_controller
self._network_id = init_response.network_id
self._is_initialized = True
if not node_info:
return
# Collect Stick NodeInfo
node_info, _ = await self.get_node_details(self._mac_stick, ping_first=False)
if node_info is not None:
self._fw_stick = node_info.firmware # type: ignore
hardware, _ = version_to_model(node_info.hardware)
self._hw_stick = hardware
async def pair_plus_device(self, mac: str) -> bool:
"""Pair Plus-device to Plugwise Stick.
According to https://roheve.wordpress.com/author/roheve/page/2/
The pairing process should look like:
0001 - 0002 - 0003: StickNetworkInfoRequest - StickNetworkInfoResponse - NodeSpecificResponse,
000A - 0011: StickInitRequest - StickInitShortResponse/StickInitResponse,
0004 - 0005: CirclePlusConnectRequest - CirclePlusConnectResponse,
the Plus-device will then send a NodeRejoinResponse (0061).
In the first occurrence of this process a 0004 0001 .... message is sent.
A StickInitShortResponse is received indicating the network is offline.
In the second occurrence of this process a 0004 0101 .... message is sent.
Again a StickInitShortResponse is received.
In the third occurrence only 000A is sent and a StickInitResponse indicating the network is online, is received.
"""
_LOGGER.debug("Pair Plus-device with mac: %s", mac)
if not validate_mac(mac):
raise NodeError(f"Pairing failed: MAC {mac} invalid")
# Collect network info
try:
request = StickNetworkInfoRequest(self.send)
info_response = await request.send()
except MessageError as exc:
raise NodeError(f"Pairing failed: {exc}") from exc
if info_response is None:
raise NodeError(
"Pairing failed, StickNetworkInfoResponse is None"
) from None
# Init Stick
try:
await self.initialize_stick(node_info=False)
except StickError as exc:
raise NodeError(
f"Pairing failed, failed to initialize Stick: {exc}"
) from exc
try:
request = CirclePlusConnectRequest(self.send, bytes(mac, UTF8))
response = await request.send()
except MessageError as exc:
raise NodeError(f"Pairing failed: {exc}") from exc
if response is None:
raise NodeError(
"Pairing failed, CirclePlusConnectResponse is None"
) from None
if response.allowed.value != 1:
raise NodeError("Pairing failed, not allowed")
return True
async def get_node_details(
self, mac: str, ping_first: bool
) -> tuple[NodeInfoResponse | None, NodePingResponse | None]:
"""Collect NodeInfo data from the Stick."""
ping_response: NodePingResponse | None = None
if ping_first:
# Define ping request with one retry
ping_request = NodePingRequest(self.send, bytes(mac, UTF8), retries=1)
try:
ping_response = await ping_request.send()
except StickError:
return (None, None)
if ping_response is None:
return (None, None)
info_request = NodeInfoRequest(self.send, bytes(mac, UTF8), retries=1)
try:
info_response = await info_request.send()
except StickError:
return (None, None)
return (info_response, ping_response)
async def send(
self,
request: PlugwiseRequest,
suppress_node_errors=True,
) -> PlugwiseResponse | None:
"""Submit request to queue and return response."""
if not suppress_node_errors:
return await self._queue.submit(request)
try:
return await self._queue.submit(request)
except NodeError, StickError:
return None
def _reset_states(self) -> None:
"""Reset internal connection information."""
self._mac_stick = None
self._mac_nc = None
self._network_id = None
self._network_online = False
async def disconnect_from_stick(self) -> None:
"""Disconnect from USB-Stick."""
if self._unsubscribe_stick_event is not None:
self._unsubscribe_stick_event()
self._unsubscribe_stick_event = None
if self._queue.is_running:
await self._queue.stop()
await self._manager.disconnect_from_stick()