Skip to content

Commit d3f2414

Browse files
committed
PlugwiseRequest class: handle no NodeResponse expected
1 parent 86710f0 commit d3f2414

File tree

3 files changed

+27
-25
lines changed

3 files changed

+27
-25
lines changed

plugwise_usb/connection/__init__.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,13 +233,12 @@ async def send(
233233
self,
234234
request: PlugwiseRequest,
235235
suppress_node_errors=True,
236-
no_response_expected=False,
237236
) -> PlugwiseResponse | None:
238237
"""Submit request to queue and return response."""
239238
if not suppress_node_errors:
240-
return await self._queue.submit(request, no_response_expected)
239+
return await self._queue.submit(request)
241240
try:
242-
return await self._queue.submit(request, no_response_expected)
241+
return await self._queue.submit(request)
243242
except (NodeError, StickError):
244243
return None
245244

plugwise_usb/connection/queue.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,7 @@ async def stop(self) -> None:
7474
self._stick = None
7575
_LOGGER.debug("queue stopped")
7676

77-
async def submit(
78-
self, request: PlugwiseRequest, no_response_expected: bool
79-
) -> PlugwiseResponse | None:
77+
async def submit(self, request: PlugwiseRequest) -> PlugwiseResponse | None:
8078
"""Add request to queue and return the response of node. Raises an error when something fails."""
8179
if request.waiting_for_response:
8280
raise MessageError(
@@ -92,7 +90,7 @@ async def submit(
9290
)
9391
await self._add_request_to_queue(request)
9492
try:
95-
if no_response_expected:
93+
if not request.node_response_expected:
9694
return None
9795
response: PlugwiseResponse = await request.response_future()
9896
return response

plugwise_usb/messages/requests.py

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ def __init__(
6868
send_fn: Callable[[PlugwiseRequest, bool], Awaitable[PlugwiseResponse | None]]
6969
| None,
7070
mac: bytes | None,
71+
node_response=True,
7172
) -> None:
7273
"""Initialize request message."""
7374
super().__init__()
@@ -104,9 +105,13 @@ def __init__(
104105
self._unsubscribe_stick_response: Callable[[], None] | None = None
105106
self._unsubscribe_node_response: Callable[[], None] | None = None
106107
self._response_timeout: TimerHandle | None = None
107-
self._response_future: Future[PlugwiseResponse] = self._loop.create_future()
108+
self._response_future: Future[PlugwiseResponse] | None = None
109+
if node_response:
110+
self._response_future = self._loop.create_future()
108111
self._waiting_for_response = False
109112

113+
self.node_response_expected: bool = node_response
114+
110115
def __repr__(self) -> str:
111116
"""Convert request into writable str."""
112117
if self._seq_id is None:
@@ -171,12 +176,13 @@ async def subscribe_to_response(
171176
self._unsubscribe_stick_response = await stick_subscription_fn(
172177
self._process_stick_response, self._seq_id, None
173178
)
174-
self._unsubscribe_node_response = await node_subscription_fn(
175-
self.process_node_response,
176-
self._mac,
177-
(self._reply_identifier,),
178-
self._seq_id,
179-
)
179+
if self.node_response_expected:
180+
self._unsubscribe_node_response = await node_subscription_fn(
181+
self.process_node_response,
182+
self._mac,
183+
(self._reply_identifier,),
184+
self._seq_id,
185+
)
180186

181187
def _unsubscribe_from_stick(self) -> None:
182188
"""Unsubscribe from StickResponse messages."""
@@ -221,7 +227,8 @@ def _response_timeout_expired(self, stick_timeout: bool = False) -> None:
221227
)
222228
self._seq_id = None
223229
self._unsubscribe_from_stick()
224-
self._unsubscribe_from_node()
230+
if self.node_response_expected:
231+
self._unsubscribe_from_node()
225232
if stick_timeout:
226233
self._response_future.set_exception(
227234
StickTimeout(f"USB-stick responded with time out to {self}")
@@ -237,7 +244,8 @@ def assign_error(self, error: BaseException) -> None:
237244
"""Assign error for this request."""
238245
self.stop_response_timeout()
239246
self._unsubscribe_from_stick()
240-
self._unsubscribe_from_node()
247+
if self.node_response_expected:
248+
self._unsubscribe_from_node()
241249
if self._response_future.done():
242250
return
243251
self._waiting_for_response = False
@@ -266,7 +274,8 @@ async def process_node_response(self, response: PlugwiseResponse) -> bool:
266274
self._response = copy(response)
267275
self.stop_response_timeout()
268276
self._unsubscribe_from_stick()
269-
self._unsubscribe_from_node()
277+
if self.node_response_expected:
278+
self._unsubscribe_from_node()
270279
if self._send_counter > 1:
271280
_LOGGER.debug(
272281
"Received %s after %s retries as reply to %s",
@@ -303,15 +312,11 @@ async def _process_stick_response(self, stick_response: StickResponse) -> None:
303312
self,
304313
)
305314

306-
async def _send_request(
307-
self, suppress_node_errors=False, no_response_expected=False
308-
) -> PlugwiseResponse | None:
315+
async def _send_request(self, suppress_node_errors=False) -> PlugwiseResponse | None:
309316
"""Send request."""
310317
if self._send_fn is None:
311318
return None
312-
return await self._send_fn(
313-
self, suppress_node_errors, no_response_expected
314-
)
319+
return await self._send_fn(self, suppress_node_errors)
315320

316321
@property
317322
def max_retries(self) -> int:
@@ -423,14 +428,14 @@ def __init__(
423428
accept: bool,
424429
) -> None:
425430
"""Initialize NodeAddRequest message object."""
426-
super().__init__(send_fn, mac)
431+
super().__init__(send_fn, mac, node_response=False)
427432
accept_value = 1 if accept else 0
428433
self._args.append(Int(accept_value, length=2))
429434

430435
async def send(self) -> None:
431436
"""Send request."""
432437
if (
433-
result := await self._send_request(no_response_expected=True)
438+
result := await self._send_request()
434439
) is not None:
435440
raise MessageError(
436441
f"Invalid response message. Received {result.__class__.__name__}, expected no Response"

0 commit comments

Comments
 (0)