feat(diagnostics): capture /oic/p and /oic/d identity

Device-type detection currently parses board part numbers out of
/information/vs/0's modelNum. OCF has a standard field for exactly this
question -- /oic/d's `rt` -- and read_identity() already fetches the
resource, but kept only `n` and threw the rest away. No captured dump has
ever included it either: /device/0 batch responses don't carry /oic/d, and
diagnostics didn't report it, so there's no evidence on whether real
hardware populates it usefully.

Keep `rt` as DeviceIdentity.device_types, keep both raw payloads whole
(we don't yet know which of their fields identify a type), and surface
them in diagnostics so incoming issue reports answer the question.

Nothing routes on it yet.

/oic/d and /oic/p identify the unit with bare two-letter keys -- 'di' and
'pi' -- as sensitive as the serial number redact.py already covers but far
too short to match on: 'di' alone is a substring of 'condition', 'display'
and 'dispenser'. Add a whole-key match alongside the substring rules.
This commit is contained in:
Marc Billow committed 2026-07-29 01:56:52 +00:00
1 parent 668aec401a
commit 288ba02cc5
6 files changed
+166 -1

No files matched your search

+49
View File
@@ -38,6 +38,55 @@ async def test_diagnostics_shape_and_redaction(
assert resources['/status/lock/vs/0']['x.com.samsung.da.ado.devicecontrol'] == 'On'
async def test_diagnostics_include_ocf_identity(
hass: HomeAssistant, mock_entry, mock_coordinator_session
) -> None:
"""/oic/p and /oic/d are outside the /device/0 batch, so diagnostics is
the only place an issue report can carry them -- and `rt` there is OCF's
own device-type declaration."""
from custom_components.localthings.registry.identity import DeviceIdentity
await hass.config_entries.async_setup(mock_entry.entry_id)
await hass.async_block_till_done()
coordinator = hass.data[DOMAIN][mock_entry.entry_id]
coordinator._identity = DeviceIdentity(
manufacturer='Samsung Electronics',
model='RF9000B',
name='Family Hub',
serial=None,
device_types=('oic.wk.d', 'oic.d.refrigerator'),
raw={
'/oic/p': {'mnmn': 'Samsung Electronics', 'pi': '12-34-56'},
'/oic/d': {'n': 'Family Hub', 'di': 'ab-cd-ef'},
},
)
diag = await async_get_config_entry_diagnostics(hass, mock_entry)
identity = diag['identity']
assert identity['model'] == 'RF9000B'
assert identity['device_types'] == ['oic.wk.d', 'oic.d.refrigerator']
# The raw payloads ride along redacted -- we don't yet know which of
# their fields identify a device type, so none are dropped up front.
assert identity['resources']['/oic/p']['mnmn'] == 'Samsung Electronics'
assert identity['resources']['/oic/d']['di'] == REDACTED
assert identity['resources']['/oic/p']['pi'] == REDACTED
async def test_diagnostics_identity_none_when_unavailable(
hass: HomeAssistant, mock_entry, mock_coordinator_session
) -> None:
"""read_identity is best-effort: a device that answers neither resource
(or a session that never connected) must not break the download."""
await hass.config_entries.async_setup(mock_entry.entry_id)
await hass.async_block_till_done()
diag = await async_get_config_entry_diagnostics(hass, mock_entry)
assert diag['identity'] is None
async def test_diagnostics_include_observe_mode_fields(
hass: HomeAssistant, mock_entry, mock_coordinator_session
) -> None:
+38
View File
@@ -31,3 +31,41 @@ def test_read_identity_tolerates_missing_resources():
assert ident.manufacturer == 'Samsung'
assert ident.model == ''
assert ident.serial is None
assert ident.device_types == ()
assert ident.raw == {'/oic/p': {}, '/oic/d': {}}
def test_read_identity_captures_oic_d_device_types():
"""/oic/d's `rt` is OCF's own device-type declaration -- captured so
diagnostics can show whether real hardware populates it usefully."""
sess = FakeSession({
('oic', 'd'): {
'n': 'Living Room AC',
'rt': ['oic.wk.d', 'oic.d.airconditioner'],
},
})
ident = read_identity(sess, serial=None)
assert ident.device_types == ('oic.wk.d', 'oic.d.airconditioner')
def test_read_identity_normalizes_scalar_and_malformed_rt():
"""Firmware that reports a bare string, or a non-list, must not explode."""
assert read_identity(
FakeSession({('oic', 'd'): {'rt': 'oic.d.refrigerator'}}), None
).device_types == ('oic.d.refrigerator',)
assert read_identity(
FakeSession({('oic', 'd'): {'rt': 42}}), None
).device_types == ()
assert read_identity(
FakeSession({('oic', 'd'): {'rt': ['oic.wk.d', 7, None]}}), None
).device_types == ('oic.wk.d',)
def test_read_identity_keeps_raw_payloads_for_diagnostics():
sess = FakeSession({
('oic', 'p'): {'mnmn': 'Samsung Electronics', 'mnmo': 'RF9000B'},
('oic', 'd'): {'n': 'Family Hub', 'di': 'abc-123'},
})
ident = read_identity(sess, serial=None)
assert ident.raw['/oic/p']['mnmo'] == 'RF9000B'
assert ident.raw['/oic/d']['di'] == 'abc-123'
+31
View File
@@ -72,3 +72,34 @@ def test_redact_resources_does_not_mutate_input():
redact_resources(resources)
assert resources['/information/vs/0']['x.com.samsung.da.serialNum'] == original_serial
def test_redacts_bare_ocf_identity_keys():
"""/oic/d and /oic/p identify the unit with two-letter keys ('di', 'pi')
that the substring rules can't see."""
redacted = redact_resources({
'/oic/d': {'di': 'ab-cd-ef', 'n': 'Family Hub'},
'/oic/p': {'pi': '12-34-56', 'mnmo': 'RF9000B'},
})
assert redacted['/oic/d']['di'] == REDACTED
assert redacted['/oic/p']['pi'] == REDACTED
# Non-identifying neighbours in the same payloads survive.
assert redacted['/oic/d']['n'] == 'Family Hub'
assert redacted['/oic/p']['mnmo'] == 'RF9000B'
def test_bare_key_redaction_does_not_leak_into_substring_matching():
"""'di'/'pi' are whole-key matches only -- plenty of ordinary appliance
fields contain those two letters and must survive untouched."""
redacted = redact_resources({
'/x': {
'condition': 'Normal', 'display': 'On', 'dispenser': 'Cubed',
'humidity': '45', 'spinSpeed': '1200',
},
})
assert redacted['/x'] == {
'condition': 'Normal', 'display': 'On', 'dispenser': 'Cubed',
'humidity': '45', 'spinSpeed': '1200',
}