Skip to content

Commit a5670a6

Browse files
authored
Fix logging in Aqara quirks _find_zcl_cluster (#4225)
* Fix logging * Add a unit test to ensure this won't happen again
1 parent eadd1bd commit a5670a6

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

tests/conftest.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Fixtures for all tests."""
22

3+
import logging
34
from unittest.mock import AsyncMock, Mock
45

56
import pytest
@@ -305,3 +306,31 @@ def __getattr__(self, key):
305306
assert isinstance(device, quirk)
306307

307308
return _check
309+
310+
311+
class FailOnBadFormattingHandler(logging.Handler):
312+
"""Logging handler that fails the test if a log message cannot be formatted."""
313+
314+
def emit(self, record):
315+
"""No-op record emitter."""
316+
try:
317+
record.msg % record.args
318+
except Exception as e: # noqa: BLE001
319+
pytest.fail(
320+
f"Failed to format log message {record.msg!r} with {record.args!r}: {e}"
321+
)
322+
323+
324+
@pytest.fixture(autouse=True)
325+
def raise_on_bad_log_formatting():
326+
"""Fixture to ensure that all log messages can be formatted correctly."""
327+
handler = FailOnBadFormattingHandler()
328+
329+
root = logging.getLogger()
330+
root.addHandler(handler)
331+
root.setLevel(logging.DEBUG)
332+
333+
try:
334+
yield
335+
finally:
336+
root.removeHandler(handler)

zhaquirks/xiaomi/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,7 @@ def _find_zcl_cluster(
133133
return super()._find_zcl_cluster(hdr, packet)
134134
except KeyError:
135135
_LOGGER.debug(
136-
"Packet is coming in the wrong direction for cluster %s on endpoint %s,"
137-
" swapping direction and trying again"
136+
"Packet is coming in the wrong direction, swapping direction and trying again",
138137
)
139138

140139
return super()._find_zcl_cluster(

0 commit comments

Comments
 (0)