Skip to content

Commit 2465bec

Browse files
committed
Improve pair_plus_device()
1 parent 42fbea5 commit 2465bec

File tree

1 file changed

+20
-29
lines changed

1 file changed

+20
-29
lines changed

plugwise_usb/network/__init__.py

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -169,41 +169,32 @@ def registry(self) -> list[str]:
169169
"""
170170
_LOGGER.debug("Pair Plus-device with mac: %s", mac)
171171
if not validate_mac(mac):
172-
raise NodeError(f"MAC {mac} invalid")
172+
raise NodeError(f"Pairing failed: MAC {mac} invalid")
173173

174174
# Collect network info
175-
request = StickNetworkInfoRequest(self._controller.send, None)
176-
if (info_response := await request.send()) is not None:
177-
if not isinstance(info_response, StickNetworkInfoResponse):
178-
raise MessageError(
179-
f"Invalid response message type ({info_response.__class__.__name__}) received, expected StickNetworkInfoResponse"
180-
)
175+
try:
176+
request = StickNetworkInfoRequest(self._controller.send, None)
177+
info_response = await request.send()
178+
except MessageError as exc:
179+
raise NodeError(f"Pairing failed: {exc}")
180+
if info_response is None:
181+
raise NodeError("Pairing failed, StickNetworkInfoResponse is None")
181182

182183
# Init Stick
183184
try:
184-
request = StickInitRequest(self._controller.send)
185-
init_response: StickInitResponse | None = await request.send()
186-
except StickError as err:
187-
raise StickError(
188-
"No response from USB-Stick to initialization request."
189-
+ " Validate USB-stick is connected to port "
190-
+ f"' {self._manager.serial_path}'"
191-
) from err
192-
if init_response is None:
193-
raise StickError(
194-
"No response from USB-Stick to initialization request."
195-
+ " Validate USB-stick is connected to port "
196-
+ f"' {self._manager.serial_path}'"
197-
)
198-
199-
request = CirclePlusConnectRequest(self._controller.send, bytes(mac, UTF8))
200-
if (response := await request.send()) is None:
201-
raise NodeError("No response for CirclePlusConnectRequest.")
185+
await self._controller.initialize_stick()
186+
except StickError as exc:
187+
raise NodeError(f"Pairing failed, failed to initialize Stick: {exc}")
202188

203-
# how do we check for a succesfull pairing?
204-
# there should be a 0005 wxyz 0001 response
205-
# followed by a StickInitResponse (0011)?
206-
189+
try:
190+
request = CirclePlusConnectRequest(self._controller.send, bytes(mac, UTF8))
191+
response = await request.send())
192+
except MessageError as exc:
193+
raise NodeError(f"Pairing failed: {exc}")
194+
if response is None:
195+
raise NodeError("Pairing failed, CirclePlusConnectResponse is None")
196+
if response.allowed.value != 1:
197+
raise NodeError("Pairing failed, not allowed")
207198

208199
async def register_node(self, mac: str) -> bool:
209200
"""Register node to Plugwise network."""

0 commit comments

Comments
 (0)