Skip to content

Commit 1cec057

Browse files
committed
Improve registering for message replies
1 parent 7c3ad68 commit 1cec057

File tree

1 file changed

+35
-38
lines changed

1 file changed

+35
-38
lines changed

plugwise_usb/messages/requests.py

Lines changed: 35 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def __init__(
8383
[
8484
Callable[[StickResponse], Coroutine[Any, Any, None]],
8585
bytes | None,
86-
StickResponseType | None,
86+
tuple[StickResponseType, ...] | None,
8787
],
8888
Callable[[], None],
8989
]
@@ -141,56 +141,53 @@ def seq_id(self, seq_id: bytes) -> None:
141141
f"Unable to set seq_id to {seq_id!r}. Already set to {self._seq_id!r}"
142142
)
143143
self._seq_id = seq_id
144-
# Subscribe to receive the response messages
145-
if self._stick_subscription_fn is not None:
146-
self._unsubscribe_stick_response = self._stick_subscription_fn(
147-
self._process_stick_response, self._seq_id, None
148-
)
149-
if self._node_subscription_fn is not None:
150-
self._unsubscribe_node_response = self._node_subscription_fn(
151-
self._process_node_response,
152-
self._mac,
153-
(self._reply_identifier,),
154-
self._seq_id,
155-
)
156-
157-
def _unsubscribe_from_stick(self) -> None:
158-
"""Unsubscribe from StickResponse messages."""
159-
if self._unsubscribe_stick_response is not None:
160-
self._unsubscribe_stick_response()
161-
self._unsubscribe_stick_response = None
162144

163-
def _unsubscribe_from_node(self) -> None:
164-
"""Unsubscribe from NodeResponse messages."""
165-
if self._unsubscribe_node_response is not None:
166-
self._unsubscribe_node_response()
167-
self._unsubscribe_node_response = None
168-
169-
def subscribe_to_responses(
145+
async def subscribe_to_response(
170146
self,
171147
stick_subscription_fn: Callable[
172148
[
173149
Callable[[StickResponse], Coroutine[Any, Any, None]],
174150
bytes | None,
175-
StickResponseType | None,
151+
tuple[StickResponseType, ...] | None,
176152
],
177-
Callable[[], None],
178-
]
179-
| None,
153+
Coroutine[Any, Any, Callable[[], None]],
154+
],
180155
node_subscription_fn: Callable[
181156
[
182157
Callable[[PlugwiseResponse], Coroutine[Any, Any, bool]],
183158
bytes | None,
184159
tuple[bytes, ...] | None,
185160
bytes | None,
186161
],
187-
Callable[[], None],
188-
]
189-
| None,
162+
Coroutine[Any, Any, Callable[[], None]],
163+
],
190164
) -> None:
191-
"""Register for response messages."""
192-
self._node_subscription_fn = node_subscription_fn
193-
self._stick_subscription_fn = stick_subscription_fn
165+
"""Subscribe to receive the response messages."""
166+
if self._seq_id is None:
167+
raise MessageError(
168+
"Unable to subscribe to response because seq_id is not set"
169+
)
170+
self._unsubscribe_stick_response = await stick_subscription_fn(
171+
self._process_stick_response, self._seq_id, None
172+
)
173+
self._unsubscribe_node_response = await node_subscription_fn(
174+
self.process_node_response,
175+
self._mac,
176+
(self._reply_identifier,),
177+
self._seq_id,
178+
)
179+
180+
def _unsubscribe_from_stick(self) -> None:
181+
"""Unsubscribe from StickResponse messages."""
182+
if self._unsubscribe_stick_response is not None:
183+
self._unsubscribe_stick_response()
184+
self._unsubscribe_stick_response = None
185+
186+
def _unsubscribe_from_node(self) -> None:
187+
"""Unsubscribe from NodeResponse messages."""
188+
if self._unsubscribe_node_response is not None:
189+
self._unsubscribe_node_response()
190+
self._unsubscribe_node_response = None
194191

195192
def start_response_timeout(self) -> None:
196193
"""Start timeout for node response."""
@@ -237,7 +234,7 @@ def assign_error(self, error: BaseException) -> None:
237234
return
238235
self._response_future.set_exception(error)
239236

240-
async def _process_node_response(self, response: PlugwiseResponse) -> bool:
237+
async def process_node_response(self, response: PlugwiseResponse) -> bool:
241238
"""Process incoming message from node."""
242239
if self._seq_id is None:
243240
_LOGGER.warning(
@@ -262,7 +259,7 @@ async def _process_node_response(self, response: PlugwiseResponse) -> bool:
262259
self._unsubscribe_from_stick()
263260
self._unsubscribe_from_node()
264261
if self._send_counter > 1:
265-
_LOGGER.info(
262+
_LOGGER.debug(
266263
"Received %s after %s retries as reply to %s",
267264
self._response,
268265
self._send_counter,

0 commit comments

Comments
 (0)