Redact serial from unknown component model name in diagnostics (#176524)

This commit is contained in:
Øyvind Matheson Wergeland
2026-07-14 22:26:52 +02:00
committed by GitHub
parent 886f76c9c4
commit f37df79107
2 changed files with 40 additions and 7 deletions
@@ -2,9 +2,9 @@
from typing import Any
from pynobo import ComponentInfo
from pynobo import ComponentInfo, nobo
from homeassistant.components.diagnostics import async_redact_data
from homeassistant.components.diagnostics import REDACTED, async_redact_data
from homeassistant.const import CONF_IP_ADDRESS, CONF_MAC
from homeassistant.core import HomeAssistant
@@ -26,11 +26,12 @@ _MODEL_FIELDS = (
def _component_to_dict(component: ComponentInfo) -> dict[str, Any]:
formatted = dict(component)
if (model := formatted.get("model")) is not None:
formatted["model"] = {
field: getattr(model, field, None) for field in _MODEL_FIELDS
}
model = component["model"]
formatted: dict[str, Any] = dict(component)
formatted["model"] = {field: getattr(model, field, None) for field in _MODEL_FIELDS}
if model.type == nobo.Model.UNKNOWN:
# Unknown models carry the serial number in the name.
formatted["model"]["name"] = REDACTED
return formatted
@@ -1,7 +1,11 @@
"""Tests for the Nobø Ecohub diagnostics."""
from unittest.mock import MagicMock
from pynobo import nobo as pynobo_nobo
from syrupy.assertion import SnapshotAssertion
from homeassistant.components.diagnostics import REDACTED
from homeassistant.core import HomeAssistant
from tests.common import MockConfigEntry
@@ -19,3 +23,31 @@ async def test_entry_diagnostics(
result = await get_diagnostics_for_config_entry(hass, hass_client, init_integration)
assert result == snapshot
async def test_entry_diagnostics_redacts_unknown_model_name(
hass: HomeAssistant,
hass_client: ClientSessionGenerator,
init_integration: MockConfigEntry,
mock_nobo_hub: MagicMock,
) -> None:
"""An unknown model's name embeds the serial, so it is dropped; model_id is kept."""
mock_nobo_hub.components = {
"999000012345": {
"serial": "999000012345",
"name": "Mystery device",
"zone_id": "1",
"model": pynobo_nobo.Model(
model_id="999",
type=pynobo_nobo.Model.UNKNOWN,
name="Unknown (serial number: 999 000 012 345)",
),
},
}
result = await get_diagnostics_for_config_entry(hass, hass_client, init_integration)
component = result["components"][0]
assert component["serial"] == REDACTED
assert component["model"]["model_id"] == "999"
assert component["model"]["name"] == REDACTED