|
9 | 9 | from ..api import StickEvent |
10 | 10 | from ..constants import UTF8 |
11 | 11 | from ..exceptions import NodeError, StickError |
12 | | -from ..messages.requests import NodeInfoRequest, PlugwiseRequest, StickInitRequest |
13 | | -from ..messages.responses import PlugwiseResponse, StickInitResponse |
| 12 | +from ..messages.requests import ( |
| 13 | + NodeInfoRequest, |
| 14 | + NodePingRequest, |
| 15 | + PlugwiseRequest, |
| 16 | + StickInitRequest, |
| 17 | +) |
| 18 | +from ..messages.responses import ( |
| 19 | + NodeInfoResponse, |
| 20 | + NodePingResponse, |
| 21 | + PlugwiseResponse, |
| 22 | + StickInitResponse, |
| 23 | +) |
14 | 24 | from .manager import StickConnectionManager |
15 | 25 | from .queue import StickQueue |
16 | 26 |
|
@@ -181,16 +191,40 @@ async def initialize_stick(self) -> None: |
181 | 191 | self._is_initialized = True |
182 | 192 |
|
183 | 193 | # add Stick NodeInfoRequest |
184 | | - info_request = NodeInfoRequest( |
185 | | - self.send, bytes(self._mac_stick, UTF8), retries=1 |
186 | | - ) |
187 | | - info_response = await info_request.send() |
188 | | - self._fw_stick = info_response.firmware |
189 | | - self._hw_stick = info_response.hardware |
| 194 | + node_info, _ = await self.get_node_details(self._mac_stick, False) |
| 195 | + if node_info is not None: |
| 196 | + self._fw_stick = node_info.firmware |
| 197 | + self._hw_stick = node_info.hardware |
190 | 198 |
|
191 | 199 | if not self._network_online: |
192 | 200 | raise StickError("Zigbee network connection to Circle+ is down.") |
193 | 201 |
|
| 202 | + async def get_node_details( |
| 203 | + self, mac: str, ping_first: bool |
| 204 | + ) -> tuple[NodeInfoResponse | None, NodePingResponse | None]: |
| 205 | + """Return node discovery type.""" |
| 206 | + ping_response: NodePingResponse | None = None |
| 207 | + if ping_first: |
| 208 | + # Define ping request with one retry |
| 209 | + ping_request = NodePingRequest( |
| 210 | + self.send, bytes(mac, UTF8), retries=1 |
| 211 | + ) |
| 212 | + try: |
| 213 | + ping_response = await ping_request.send(suppress_node_errors=True) |
| 214 | + except StickError: |
| 215 | + return (None, None) |
| 216 | + if ping_response is None: |
| 217 | + return (None, None) |
| 218 | + |
| 219 | + info_request = NodeInfoRequest( |
| 220 | + self.send, bytes(mac, UTF8), retries=1 |
| 221 | + ) |
| 222 | + try: |
| 223 | + info_response = await info_request.send() |
| 224 | + except StickError: |
| 225 | + return (None, None) |
| 226 | + return (info_response, ping_response) |
| 227 | + |
194 | 228 | async def send( |
195 | 229 | self, request: PlugwiseRequest, suppress_node_errors: bool = True |
196 | 230 | ) -> PlugwiseResponse | None: |
|
0 commit comments