Skip to content

Commit f3d7c42

Browse files
committed
Move pair_plus_device() to connection
1 parent 1ecefe0 commit f3d7c42

File tree

3 files changed

+56
-56
lines changed

3 files changed

+56
-56
lines changed

plugwise_usb/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,10 @@ async def connect(self, port: str | None = None) -> None:
266266
self._port,
267267
)
268268

269+
async def plus_pair_request(self, mac: str) -> bool:
270+
"""Send a pair request to a Plus device."""
271+
return await self._controller.pair_plus_device(mac)
272+
269273
@raise_not_connected
270274
async def initialize(self, create_root_cache_folder: bool = False) -> None:
271275
"""Initialize connection to USB-Stick."""
@@ -278,10 +282,6 @@ async def initialize(self, create_root_cache_folder: bool = False) -> None:
278282
if self._cache_enabled:
279283
await self._network.initialize_cache()
280284

281-
async def plus_pair_request(self, mac: str) -> bool:
282-
"""Send a pair request to a Plus device."""
283-
return await self._network.pair_plus_device(mac)
284-
285285
@raise_not_connected
286286
@raise_not_initialized
287287
async def start_network(self) -> None:

plugwise_usb/connection/__init__.py

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@
99
from ..api import StickEvent
1010
from ..constants import UTF8
1111
from ..exceptions import NodeError, StickError
12-
from ..helpers.util import version_to_model
12+
from ..helpers.util import validate_mac, version_to_model
1313
from ..messages.requests import (
14+
CirclePlusConnectRequest,
1415
NodeInfoRequest,
1516
NodePingRequest,
1617
PlugwiseRequest,
1718
StickInitRequest,
19+
StickNetworkInfoRequest,
1820
)
1921
from ..messages.responses import (
2022
NodeInfoResponse,
@@ -202,6 +204,55 @@ async def initialize_stick(self) -> None:
202204
if not self._network_online:
203205
raise StickError("Zigbee network connection to Circle+ is down.")
204206

207+
async def pair_plus_device(self, mac: str) -> bool:
208+
"""Pair Plus-device to Plugwise Stick.
209+
210+
According to https://roheve.wordpress.com/author/roheve/page/2/
211+
The pairing process should look like:
212+
0001 - 0002 (- 0003): StickNetworkInfoRequest - StickNetworkInfoResponse - (PlugwiseQueryCirclePlusEndResponse - @SevenW),
213+
000A - 0011: StickInitRequest - StickInitResponse,
214+
0004 - 0005: CirclePlusConnectRequest - CirclePlusConnectResponse,
215+
the Plus-device will then send a NodeRejoinResponse (0061).
216+
217+
Todo(?): Does this need repeating until pairing is successful?
218+
"""
219+
_LOGGER.debug("Pair Plus-device with mac: %s", mac)
220+
if not validate_mac(mac):
221+
raise NodeError(f"Pairing failed: MAC {mac} invalid")
222+
223+
# Collect network info
224+
try:
225+
request = StickNetworkInfoRequest(self.send, None)
226+
info_response = await request.send()
227+
except MessageError as exc:
228+
raise NodeError(f"Pairing failed: {exc}") from exc
229+
if info_response is None:
230+
raise NodeError(
231+
"Pairing failed, StickNetworkInfoResponse is None"
232+
) from None
233+
234+
# Init Stick
235+
try:
236+
await self.initialize_stick()
237+
except StickError as exc:
238+
raise NodeError(
239+
f"Pairing failed, failed to initialize Stick: {exc}"
240+
) from exc
241+
242+
try:
243+
request = CirclePlusConnectRequest(self.send, bytes(mac, UTF8))
244+
response = await request.send()
245+
except MessageError as exc:
246+
raise NodeError(f"Pairing failed: {exc}") from exc
247+
if response is None:
248+
raise NodeError(
249+
"Pairing failed, CirclePlusConnectResponse is None"
250+
) from None
251+
if response.allowed.value != 1:
252+
raise NodeError("Pairing failed, not allowed")
253+
254+
return True
255+
205256
async def get_node_details(
206257
self, mac: str, ping_first: bool
207258
) -> tuple[NodeInfoResponse | None, NodePingResponse | None]:

plugwise_usb/network/__init__.py

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@
2222
from ..helpers.util import validate_mac
2323
from ..messages.requests import (
2424
CircleMeasureIntervalRequest,
25-
CirclePlusConnectRequest,
2625
NodePingRequest,
27-
StickNetworkInfoRequest,
2826
)
2927
from ..messages.responses import (
3028
NODE_AWAKE_RESPONSE_ID,
@@ -157,55 +155,6 @@ def registry(self) -> list[str]:
157155

158156
# endregion
159157

160-
async def pair_plus_device(self, mac: str) -> bool:
161-
"""Pair Plus-device to Plugwise Stick.
162-
163-
According to https://roheve.wordpress.com/author/roheve/page/2/
164-
The pairing process should look like:
165-
0001 - 0002 (- 0003): StickNetworkInfoRequest - StickNetworkInfoResponse - (PlugwiseQueryCirclePlusEndResponse - @SevenW),
166-
000A - 0011: StickInitRequest - StickInitResponse,
167-
0004 - 0005: CirclePlusConnectRequest - CirclePlusConnectResponse,
168-
the Plus-device will then send a NodeRejoinResponse (0061).
169-
170-
Todo(?): Does this need repeating until pairing is successful?
171-
"""
172-
_LOGGER.debug("Pair Plus-device with mac: %s", mac)
173-
if not validate_mac(mac):
174-
raise NodeError(f"Pairing failed: MAC {mac} invalid")
175-
176-
# Collect network info
177-
try:
178-
request = StickNetworkInfoRequest(self._controller.send, None)
179-
info_response = await request.send()
180-
except MessageError as exc:
181-
raise NodeError(f"Pairing failed: {exc}") from exc
182-
if info_response is None:
183-
raise NodeError(
184-
"Pairing failed, StickNetworkInfoResponse is None"
185-
) from None
186-
187-
# Init Stick
188-
try:
189-
await self._controller.initialize_stick()
190-
except StickError as exc:
191-
raise NodeError(
192-
f"Pairing failed, failed to initialize Stick: {exc}"
193-
) from exc
194-
195-
try:
196-
request = CirclePlusConnectRequest(self._controller.send, bytes(mac, UTF8))
197-
response = await request.send()
198-
except MessageError as exc:
199-
raise NodeError(f"Pairing failed: {exc}") from exc
200-
if response is None:
201-
raise NodeError(
202-
"Pairing failed, CirclePlusConnectResponse is None"
203-
) from None
204-
if response.allowed.value != 1:
205-
raise NodeError("Pairing failed, not allowed")
206-
207-
return True
208-
209158
async def register_node(self, mac: str) -> bool:
210159
"""Register node to Plugwise network."""
211160
try:

0 commit comments

Comments
 (0)