Skip to content

Commit 8d22fdb

Browse files
committed
Use existing function
1 parent bf31cac commit 8d22fdb

File tree

2 files changed

+44
-40
lines changed

2 files changed

+44
-40
lines changed

plugwise_usb/connection/__init__.py

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,18 @@
99
from ..api import StickEvent
1010
from ..constants import UTF8
1111
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+
)
1424
from .manager import StickConnectionManager
1525
from .queue import StickQueue
1626

@@ -181,16 +191,40 @@ async def initialize_stick(self) -> None:
181191
self._is_initialized = True
182192

183193
# 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
190198

191199
if not self._network_online:
192200
raise StickError("Zigbee network connection to Circle+ is down.")
193201

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+
194228
async def send(
195229
self, request: PlugwiseRequest, suppress_node_errors: bool = True
196230
) -> PlugwiseResponse | None:

plugwise_usb/network/__init__.py

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,7 @@
1515
from ..constants import UTF8
1616
from ..exceptions import CacheError, MessageError, NodeError, StickError, StickTimeout
1717
from ..helpers.util import validate_mac
18-
from ..messages.requests import (
19-
CirclePlusAllowJoiningRequest,
20-
NodeInfoRequest,
21-
NodePingRequest,
22-
)
18+
from ..messages.requests import CirclePlusAllowJoiningRequest, NodePingRequest
2319
from ..messages.responses import (
2420
NODE_AWAKE_RESPONSE_ID,
2521
NODE_JOIN_ID,
@@ -365,32 +361,6 @@ def _create_node_object(
365361
self._nodes[mac].cache_folder_create = self._cache_folder_create
366362
self._nodes[mac].cache_enabled = True
367363

368-
async def get_node_details(
369-
self, mac: str, ping_first: bool
370-
) -> tuple[NodeInfoResponse | None, NodePingResponse | None]:
371-
"""Return node discovery type."""
372-
ping_response: NodePingResponse | None = None
373-
if ping_first:
374-
# Define ping request with one retry
375-
ping_request = NodePingRequest(
376-
self._controller.send, bytes(mac, UTF8), retries=1
377-
)
378-
try:
379-
ping_response = await ping_request.send(suppress_node_errors=True)
380-
except StickError:
381-
return (None, None)
382-
if ping_response is None:
383-
return (None, None)
384-
385-
info_request = NodeInfoRequest(
386-
self._controller.send, bytes(mac, UTF8), retries=1
387-
)
388-
try:
389-
info_response = await info_request.send()
390-
except StickError:
391-
return (None, None)
392-
return (info_response, ping_response)
393-
394364
async def _discover_battery_powered_node(
395365
self,
396366
address: int,
@@ -432,7 +402,7 @@ async def _discover_node(
432402

433403
# Node type is unknown, so we need to discover it first
434404
_LOGGER.debug("Starting the discovery of node %s", mac)
435-
node_info, node_ping = await self.get_node_details(mac, ping_first)
405+
node_info, node_ping = await self._controller.get_node_details(mac, ping_first)
436406
if node_info is None:
437407
return False
438408
self._create_node_object(mac, address, node_info.node_type)

0 commit comments

Comments
 (0)