Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions src/helper/hive_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,7 @@ def get_device_data(self, product: dict):
if trv_present:
device = self.session.data.devices[product["props"]["trvs"][0]]
else:
_LOGGER.error(
"get_device_data - No TRVs found for product %s", product_id
)
raise KeyError
raise KeyError(f"no TRVs linked to trvcontrol product {product_id}")
elif (
product_type == "warmwhitelight" and product["props"]["model"] == "SIREN001"
):
Expand Down
11 changes: 10 additions & 1 deletion src/session/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,16 @@ def add_list(self, entity_type: str, data: dict, **kwargs) -> Device | None:
self.device_list[entity_type].append(device_obj)
return device_obj
except KeyError as error:
_LOGGER.error(error)
# str(KeyError()) is empty, so name the entity and key explicitly;
# a bare _LOGGER.error(error) showed up as a blank log entry.
missing = error.args[0] if error.args else "unknown key"
_LOGGER.warning(
"Skipping %s entity for Hive %s %s: missing data (%s)",
entity_type,
data.get("type", "device"),
data.get("id", "unknown"),
missing,
)
return None

@staticmethod
Expand Down
41 changes: 41 additions & 0 deletions tests/unit/test_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

# pylint: disable=protected-access,attribute-defined-outside-init

import logging
from unittest.mock import MagicMock

from apyhiveapi.helper.hivedataclasses import Device, SessionConfig
Expand Down Expand Up @@ -216,3 +217,43 @@ def test_returns_device_instance(self):
data = {"id": "heat-1", "type": "heating"}
device = d.add_list("climate", data)
assert isinstance(device, Device)


# ---------------------------------------------------------------------------
# add_list error logging (home-assistant/core#172819: blank ERROR log entry)
# ---------------------------------------------------------------------------


class TestAddListErrorLogging:
"""A skipped entity is logged with enough context to act on."""

def test_bare_keyerror_logs_non_blank_warning(self, caplog):
"""A KeyError with no args previously produced an empty log message."""
d = _make_discovery()
d.helper.get_device_data.side_effect = KeyError()
with caplog.at_level(logging.WARNING):
result = d.add_list("climate", {"id": "trv-ctl-1", "type": "trvcontrol"})
assert result is None
(record,) = caplog.records
assert record.levelno == logging.WARNING
message = record.getMessage()
assert message.strip()
assert "climate" in message
assert "trvcontrol" in message
assert "trv-ctl-1" in message
assert "unknown key" in message

def test_keyerror_message_included(self, caplog):
d = _make_discovery()
d.helper.get_device_data.side_effect = KeyError("parent-0000")
with caplog.at_level(logging.WARNING):
d.add_list("binary_sensor", {"id": "sense-1", "type": "sense"})
assert "parent-0000" in caplog.records[0].getMessage()

def test_data_without_id_or_type(self, caplog):
d = _make_discovery()
d.helper.get_device_data.side_effect = KeyError("state")
with caplog.at_level(logging.WARNING):
d.add_list("sensor", {})
message = caplog.records[0].getMessage()
assert "device unknown" in message
14 changes: 12 additions & 2 deletions tests/unit/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

# pylint: disable=protected-access

import logging
from unittest.mock import MagicMock

import pytest
Expand Down Expand Up @@ -454,9 +455,19 @@ def test_trvcontrol_no_trvs_raises_key_error(self):
"""trvcontrol with an empty trvs list raises KeyError."""
helper, _ = _make_helper()
product = {"id": "trv-1", "type": "trvcontrol", "props": {"trvs": []}}
with pytest.raises(KeyError):
with pytest.raises(
KeyError, match="no TRVs linked to trvcontrol product trv-1"
):
helper.get_device_data(product)

def test_trvcontrol_no_trvs_does_not_log_error(self, caplog):
"""The caller logs the skip once; the helper must not log a second ERROR."""
helper, _ = _make_helper()
product = {"id": "trv-1", "type": "trvcontrol", "props": {"trvs": []}}
with caplog.at_level(logging.DEBUG), pytest.raises(KeyError):
helper.get_device_data(product)
assert not [r for r in caplog.records if r.levelno >= logging.ERROR]

def test_trvcontrol_with_trv_returns_device(self):
"""trvcontrol with a valid TRV looks up the TRV device."""
devices = {
Expand Down Expand Up @@ -527,7 +538,6 @@ def test_zone_mismatch_keeps_product_as_device(self):

def test_trv_without_zone_does_not_log_warning(self, caplog):
"""TRV devices that omit 'zone' from props are silently skipped (no warning)."""
import logging

helper, _ = _make_helper(
devices={
Expand Down
Loading