Skip to content

Commit 70727f8

Browse files
committed
Improve log messages
1 parent 9078b14 commit 70727f8

File tree

7 files changed

+38
-18
lines changed

7 files changed

+38
-18
lines changed

plugwise_usb/connection/manager.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ async def setup_connection_to_stick(
184184

185185
async def write_to_stick(self, request: PlugwiseRequest) -> None:
186186
"""Write message to USB stick."""
187+
_LOGGER.debug("Write to USB-stick: %s", request)
187188
if not request.resend:
188189
raise StickError(
189190
f"Failed to send {request.__class__.__name__} " +

plugwise_usb/connection/queue.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ async def submit(
8282
self, request: PlugwiseRequest
8383
) -> PlugwiseResponse:
8484
"""Add request to queue and return the response of node. Raises an error when something fails."""
85-
_LOGGER.debug("Queueing %s", request)
85+
_LOGGER.debug("Submit %s", request)
8686
while request.resend:
8787
if not self._running or self._stick is None:
8888
raise StickError(
@@ -93,13 +93,13 @@ async def submit(
9393
try:
9494
response: PlugwiseResponse = await request.response_future()
9595
except (NodeTimeout, StickTimeout) as e:
96-
if request.resend:
97-
_LOGGER.debug("%s, retrying", e)
98-
elif isinstance(request, NodePingRequest):
96+
if isinstance(request, NodePingRequest):
9997
# For ping requests it is expected to receive timeouts, so lower log level
100-
_LOGGER.debug("%s after %s attempts. Cancel request", e, request.max_retries)
98+
_LOGGER.debug("%s, cancel because timeout is expected for NodePingRequests", e)
99+
elif request.resend:
100+
_LOGGER.info("%s, retrying", e)
101101
else:
102-
_LOGGER.info("%s after %s attempts, cancel request", e, request.max_retries)
102+
_LOGGER.warning("%s, cancel request", e)
103103
except StickError as exception:
104104
_LOGGER.error(exception)
105105
raise StickError(
@@ -122,6 +122,7 @@ async def submit(
122122

123123
async def _add_request_to_queue(self, request: PlugwiseRequest) -> None:
124124
"""Add request to send queue."""
125+
_LOGGER.debug("Add request to queue: %s", request)
125126
await self._submit_queue.put(request)
126127
if self._submit_worker_task is None or self._submit_worker_task.done():
127128
self._submit_worker_task = self._loop.create_task(
@@ -131,10 +132,14 @@ async def _add_request_to_queue(self, request: PlugwiseRequest) -> None:
131132

132133
async def _send_queue_worker(self) -> None:
133134
"""Send messages from queue at the order of priority."""
135+
_LOGGER.debug("Send_queue_worker started")
134136
while self._running:
135137
request = await self._submit_queue.get()
138+
_LOGGER.debug("Send from queue %s", request)
136139
if request.priority == Priority.CANCEL:
137140
self._submit_queue.task_done()
138141
return
139142
await self._stick.write_to_stick(request)
140143
self._submit_queue.task_done()
144+
_LOGGER.debug("Sent from queue %s", request)
145+
_LOGGER.debug("Send_queue_worker finished")

plugwise_usb/connection/receiver.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,9 @@ def __init__(
104104

105105
def connection_lost(self, exc: Exception | None = None) -> None:
106106
"""Call when port was closed expectedly or unexpectedly."""
107-
_LOGGER.debug("Connection lost")
107+
_LOGGER.info("Connection lost")
108108
if exc is not None:
109-
_LOGGER.warning("Connection lost %s", exc)
109+
_LOGGER.warning("Connection to Plugwise USB-stick lost %s", exc)
110110
self._loop.create_task(self.close())
111111
if len(self._stick_event_subscribers) > 0:
112112
self._loop.create_task(
@@ -132,7 +132,7 @@ def reduce_logging(self, reduce_logging: bool) -> None:
132132

133133
def connection_made(self, transport: SerialTransport) -> None:
134134
"""Call when the serial connection to USB-Stick is established."""
135-
_LOGGER.debug("Connection made")
135+
_LOGGER.info("Connection made")
136136
self._transport = transport
137137
if (
138138
self._connected_future is not None
@@ -168,6 +168,7 @@ def data_received(self, data: bytes) -> None:
168168
169169
This function is called by inherited asyncio.Protocol class
170170
"""
171+
_LOGGER.debug("Received data from USB-Stick: %s", data)
171172
self._buffer += data
172173
if MESSAGE_FOOTER in self._buffer:
173174
msgs = self._buffer.split(MESSAGE_FOOTER)
@@ -182,6 +183,7 @@ def data_received(self, data: bytes) -> None:
182183

183184
def _put_message_in_receiver_queue(self, response: PlugwiseResponse) -> None:
184185
"""Put message in queue."""
186+
_LOGGER.debug("Add response to queue: %s", response)
185187
self._receive_queue.put_nowait(response)
186188
if self._msg_processing_task is None or self._msg_processing_task.done():
187189
self._msg_processing_task = self._loop.create_task(
@@ -194,6 +196,7 @@ def extract_message_from_line_buffer(self, msg: bytes) -> PlugwiseResponse:
194196
# Lookup header of message, there are stray \x83
195197
if (_header_index := msg.find(MESSAGE_HEADER)) == -1:
196198
return False
199+
_LOGGER.debug("Extract message from data: %s", msg)
197200
msg = msg[_header_index:]
198201
# Detect response message type
199202
identifier = msg[4:8]
@@ -211,23 +214,25 @@ def extract_message_from_line_buffer(self, msg: bytes) -> PlugwiseResponse:
211214
_LOGGER.warning(err)
212215
return None
213216

214-
_LOGGER.debug("Reading '%s' from USB-Stick", response)
217+
_LOGGER.debug("Data %s converted into %s", msg, response)
215218
return response
216219

217220
async def _receive_queue_worker(self):
218221
"""Process queue items."""
222+
_LOGGER.debug("Receive_queue_worker started")
219223
while self.is_connected:
220224
response: PlugwiseResponse | None = await self._receive_queue.get()
221-
_LOGGER.debug("Processing started for %s", response)
225+
_LOGGER.debug("Process from queue: %s", response)
222226
if isinstance(response, StickResponse):
227+
_LOGGER.debug("Received %s", response)
223228
await self._notify_stick_response_subscribers(response)
224229
elif response is None:
225230
self._receive_queue.task_done()
226231
return
227232
else:
228233
await self._notify_node_response_subscribers(response)
229-
_LOGGER.debug("Processing finished for %s", response)
230234
self._receive_queue.task_done()
235+
_LOGGER.debug("Receive_queue_worker finished")
231236

232237
def _reset_buffer(self, new_buffer: bytes) -> None:
233238
if new_buffer[:2] == MESSAGE_FOOTER:
@@ -295,6 +300,7 @@ async def _notify_stick_response_subscribers(
295300
continue
296301
if response_type is not None and response_type != stick_response.response_type:
297302
continue
303+
_LOGGER.debug("Notify stick response subscriber for %s", stick_response)
298304
await callback(stick_response)
299305

300306
def subscribe_to_node_responses(
@@ -320,7 +326,7 @@ def remove_listener() -> None:
320326
async def _notify_node_response_subscribers(self, node_response: PlugwiseResponse) -> None:
321327
"""Call callback for all node response message subscribers."""
322328
if node_response.seq_id in self._last_processed_messages:
323-
_LOGGER.debug("Drop duplicate already processed %s", node_response)
329+
_LOGGER.debug("Drop previously processed duplicate %s", node_response)
324330
return
325331

326332
notify_tasks: list[Callable] = []
@@ -336,6 +342,7 @@ async def _notify_node_response_subscribers(self, node_response: PlugwiseRespons
336342
notify_tasks.append(callback(node_response))
337343

338344
if len(notify_tasks) > 0:
345+
_LOGGER.debug("Notify node response subscribers (%s) about %s", len(notify_tasks), node_response)
339346
if node_response.seq_id not in BROADCAST_IDS:
340347
self._last_processed_messages.append(node_response.seq_id)
341348
if node_response.seq_id in self._delayed_processing_tasks:

plugwise_usb/connection/sender.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ async def write_request_to_port(self, request: PlugwiseRequest) -> None:
5656
raise StickError("USB-Stick transport missing.")
5757

5858
await self._stick_lock.acquire()
59+
_LOGGER.debug("Send %s", request)
5960
self._current_request = request
6061
self._stick_response: Future[bytes] = self._loop.create_future()
6162

@@ -65,9 +66,9 @@ async def write_request_to_port(self, request: PlugwiseRequest) -> None:
6566
self._receiver.subscribe_to_node_responses,
6667
)
6768

68-
_LOGGER.debug("Writing '%s' to USB-Stick", request)
6969
# Write message to serial port buffer
7070
serialized_data = request.serialize()
71+
_LOGGER.debug("Write %s to port: %s", request, serialized_data)
7172
self._transport.write(serialized_data)
7273
request.start_response_timeout()
7374

@@ -85,12 +86,14 @@ async def write_request_to_port(self, request: PlugwiseRequest) -> None:
8586
)
8687
)
8788
except BaseException as exc: # pylint: disable=broad-exception-caught
89+
_LOGGER.warning("Exception for %s: %s", request, exc)
8890
request.assign_error(exc)
8991
else:
90-
_LOGGER.debug("Request '%s' was accepted by USB-stick with seq_id %s", request, str(request.seq_id))
9192
if response.response_type == StickResponseType.ACCEPT:
9293
request.seq_id = response.seq_id
94+
_LOGGER.info("Sent %s", request)
9395
elif response.response_type == StickResponseType.TIMEOUT:
96+
_LOGGER.warning("USB-Stick responded with communication timeout for %s", request)
9497
request.assign_error(
9598
BaseException(
9699
StickError(
@@ -99,6 +102,7 @@ async def write_request_to_port(self, request: PlugwiseRequest) -> None:
99102
)
100103
)
101104
elif response.response_type == StickResponseType.FAILED:
105+
_LOGGER.warning("USB-Stick failed communication for %s", request)
102106
request.assign_error(
103107
BaseException(
104108
StickError(

plugwise_usb/helpers/cache.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ async def write_cache(self, data: dict[str, str], rewrite: bool = False) -> None
9696
encoding=UTF8,
9797
) as file_data:
9898
await file_data.writelines(data_to_write)
99-
_LOGGER.info(
100-
"Saved %s lines to network cache file %s",
99+
_LOGGER.debug(
100+
"Saved %s lines to cache file %s",
101101
str(len(data)),
102102
self._cache_file
103103
)

plugwise_usb/messages/requests.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ def seq_id(self) -> bytes | None:
102102
def seq_id(self, seq_id: bytes) -> None:
103103
"""Assign sequence id."""
104104
if self._seq_id is not None:
105+
_LOGGER.warning("Unable to change seq_id into %s for request %s", seq_id, self)
105106
raise MessageError(f"Unable to set seq_id to {seq_id}. Already set to {self._seq_id}")
106107
self._seq_id = seq_id
107108
# Subscribe to receive the response messages
@@ -159,12 +160,14 @@ def _response_timeout_expired(self, stick_timeout: bool = False) -> None:
159160
self._unsubscribe_from_stick()
160161
self._unsubscribe_from_node()
161162
if stick_timeout:
163+
_LOGGER.warning("USB-stick responded with time out to %s", self)
162164
self._response_future.set_exception(
163165
StickTimeout(
164166
f"USB-stick responded with time out to {self}"
165167
)
166168
)
167169
else:
170+
_LOGGER.warning("No response received for %s within timeout (%s seconds)", self, self._response, NODE_TIME_OUT)
168171
self._response_future.set_exception(
169172
NodeTimeout(
170173
f"No response to {self} within {NODE_TIME_OUT} seconds"

plugwise_usb/network/registry.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ async def update_missing_registrations(
232232
_LOGGER.debug("Full network registration finished, pre")
233233
await self.save_registry_to_cache()
234234
_LOGGER.debug("Full network registration finished, post")
235-
_LOGGER.info("Full network registration discovery completed")
235+
_LOGGER.info("Full network discovery completed")
236236
if self._full_scan_finished is not None:
237237
await self._full_scan_finished()
238238
self._full_scan_finished = None

0 commit comments

Comments
 (0)