-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path__init__.py
More file actions
254 lines (222 loc) · 9.09 KB
/
Copy path__init__.py
File metadata and controls
254 lines (222 loc) · 9.09 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
"""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 NodeError, StickError
from ..helpers.util import version_to_model
from ..messages.requests import (
NodeInfoRequest,
NodePingRequest,
PlugwiseRequest,
StickInitRequest,
)
from ..messages.responses import (
NodeInfoResponse,
NodePingResponse,
PlugwiseResponse,
StickInitResponse,
)
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:
"""MAC address of USB-Stick. Raises StickError when not connected."""
if not self._manager.is_connected or self._mac_stick is None:
raise StickError(
"No mac address available. Connect and initialize USB-Stick first."
)
return self._mac_stick
@property
def mac_coordinator(self) -> str:
"""Return MAC address of the Zigbee network coordinator (Circle+).
Raises StickError when not connected.
"""
if not self._manager.is_connected or self._mac_nc is None:
raise StickError(
"No mac address available. Connect and initialize USB-Stick first."
)
return self._mac_nc
@property
def network_id(self) -> int:
"""Returns the Zigbee network ID. Raises StickError when not connected."""
if not self._manager.is_connected or self._network_id is None:
raise StickError(
"No network ID available. Connect and initialize USB-Stick first."
)
return self._network_id
@property
def network_online(self) -> bool:
"""Return the network state."""
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) -> 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 | 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
# 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
# Add Stick NodeInfoRequest
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
hardware, _ = version_to_model(node_info.hardware)
self._hw_stick = hardware
if not self._network_online:
raise StickError("Zigbee network connection to Circle+ is down.")
async def get_node_details(
self, mac: str, ping_first: bool
) -> tuple[NodeInfoResponse | None, NodePingResponse | None]:
"""Return node discovery type."""
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()