Browse Source

fix: silence spurious _update_parsed_data log noise (#285) (#493)

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Bluetooth Devices Bot 2 days ago
parent
commit
ece305af0f
2 changed files with 28 additions and 2 deletions
  1. 2 2
      switchbot/devices/device.py
  2. 26 0
      tests/test_device.py

+ 2 - 2
switchbot/devices/device.py

@@ -851,8 +851,8 @@ class SwitchbotBaseDevice:
         Returns true if data has changed and False if not.
         """
         if not self._sb_adv_data:
-            _LOGGER.exception("No advertisement data to update")
-            return None
+            _LOGGER.debug("%s: No advertisement data to update", self.name)
+            return False
         old_data = self._sb_adv_data.data.get("data") or {}
         merged_data = _merge_data(old_data, new_data)
         if merged_data == old_data:

+ 26 - 0
tests/test_device.py

@@ -414,3 +414,29 @@ async def test_send_command_sequence(
     result = await device._send_command_sequence(list(commands))
 
     assert result is final_result
+
+
+def test_update_parsed_data_without_advertisement_does_not_log_exception(
+    caplog: pytest.LogCaptureFixture,
+) -> None:
+    """
+    Calling _update_parsed_data before any advertisement is a no-op, not an error.
+
+    Regression for #285: previously emitted ``_LOGGER.exception(...)`` outside an
+    ``except`` block, which logged "No advertisement data to update / NoneType: None"
+    on every press()/turn_on()/update() until the first advertisement arrived.
+    """
+    ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
+    device = SwitchbotDevice(ble_device)
+
+    assert device._sb_adv_data is None
+
+    with caplog.at_level(logging.DEBUG, logger="switchbot.devices.device"):
+        result = device._update_parsed_data({"isOn": True})
+
+    assert result is False
+    exception_records = [
+        record for record in caplog.records if record.levelno >= logging.WARNING
+    ]
+    assert exception_records == []
+    assert all(record.exc_info is None for record in caplog.records)