Name devices by type alone, so entity_ids stay readable

Home Assistant mints an entity_id from the device name plus the entity
name, never from the unique_id, so the OCF device UUID the entries are now
keyed on (#381) was never at risk of reaching one. What did reach one is
the model: `device_display_name` folded modelNum into the device name, and
modelNum is a board string, so a fridge registered 46 entities along the
lines of `sensor.samsung_refrigerator_artik051_dongle_ref_energy`.

The name is now the appliance type alone -- `sensor.samsung_refrigerator_
energy`. The model is unchanged on DeviceInfo's own `model` field, where
the device page shows it and nothing derives an entity_id from it. Two
registry type names get an explicit display spelling, since they are
SmartThings/Samsung strings rather than English: 'airconditioner' and
'ehs'.

Subdevice devices (#177) had the same problem twice over, taking the
sibling's own board string ('Samsung Air Conditioner Tp2X Fac Bora Rac
21K'), so they are numbered by position instead: the master is unit 1 and
the first sibling is Unit 2.

Upgrading renames devices, which is display-only -- an entity_id is
assigned once, at first registration, so existing ids and the automations
referencing them are untouched. Only newly discovered entities get the
shorter form.

Three tests read entity_ids out of the registry after a real entry setup,
which is the only place the device name and the translated entity name are
combined: one pins the readable form, one asserts no id carries the device
key (or the literal 'None' HA appends when an entity has no name at all),
and one asserts the board string stays off.
This commit is contained in:
Marc Billow committed 2026-08-18 03:29:35 +00:00
1 parent 01c9dc9801
commit f2e0485b03
8 files changed
+130 -30

No files matched your search

+1 -1
View File
@@ -216,7 +216,7 @@ def test_identity_is_resolved_before_any_poll(hass: HomeAssistant, mock_entry) -
assert coordinator.device_key == MOCK_DEVICE_KEY
assert coordinator.device_info["identifiers"] == {(DOMAIN, MOCK_DEVICE_KEY)}
assert coordinator.device_info["model"] == MOCK_MODEL
assert coordinator.device_info["name"] == f"Samsung Refrigerator ({MOCK_MODEL})"
assert coordinator.device_info["name"] == "Samsung Refrigerator"
assert mock_entry.data[CONF_HOST] not in str(coordinator.device_info["identifiers"])
+61
View File
@@ -0,0 +1,61 @@
"""What HA actually names the entities this integration registers.
Every other naming test checks an input to that -- a translation key, a
device name, a unique_id. This one checks the output: it sets an entry up
for real and reads the entity_ids out of the registry, which is the only
place the two halves (device name + translated entity name) are combined.
"""
from __future__ import annotations
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from .conftest import MOCK_DEVICE_KEY, MOCK_MODEL
async def _entity_ids(hass: HomeAssistant, entry) -> list[str]:
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
registry = er.async_get(hass)
return sorted(e.entity_id for e in er.async_entries_for_config_entry(registry, entry.entry_id))
async def test_entity_ids_read_as_device_type_plus_entity(
hass: HomeAssistant, mock_entry, mock_coordinator_session
) -> None:
ids = await _entity_ids(hass, mock_entry)
assert "sensor.samsung_refrigerator_energy" in ids
assert "binary_sensor.samsung_refrigerator_door_freezer_open" in ids
async def test_no_entity_id_carries_the_device_key(
hass: HomeAssistant, mock_entry, mock_coordinator_session
) -> None:
"""The device key is a 36-character UUID since issue #381, and every
entity's unique_id is minted from it -- but HA builds entity_ids from
the device and entity names, so it stays internal. An entity that
registers with no name is the one way it leaks: HA appends a literal
'None' to the device name, and falls back to `<platform>_<unique_id>`
outright for an entity with no device. test_translations stops that at
the source by requiring a catalog entry per descriptor; this is the
same check at the far end of the pipeline.
"""
ids = await _entity_ids(hass, mock_entry)
assert ids
key_fragment = MOCK_DEVICE_KEY.split("-")[0]
assert not [entity_id for entity_id in ids if key_fragment in entity_id]
assert not [entity_id for entity_id in ids if "none" in entity_id.split(".")[1].split("_")]
async def test_the_board_model_stays_off_the_entity_id(
hass: HomeAssistant, mock_entry, mock_coordinator_session
) -> None:
"""modelNum is a board string ('ARTIK051_DONGLE_REF'), so it belongs on
the device's `model` field, not slugified into 46 entity_ids."""
ids = await _entity_ids(hass, mock_entry)
slug = MOCK_MODEL.lower().replace("-", "_")
assert not [entity_id for entity_id in ids if slug in entity_id]
+17
View File
@@ -2,6 +2,7 @@ import cbor2
from custom_components.localthings.registry.identity import (
DeviceIdentity,
device_display_name,
is_usable_device_id,
ocf_device_key,
read_identity,
@@ -252,3 +253,19 @@ def test_ocf_device_key_reports_absence_rather_than_collapsing_to_the_serial():
assert ocf_device_key(None) is None
assert ocf_device_key(_identity(serial="REAL-SERIAL")) is None
assert ocf_device_key(_identity(device_id="abc-123")) == "abc-123"
def test_the_device_name_is_the_device_type_alone():
"""The name is what HA slugifies into every entity_id on the device, so
the board string that modelNum reports stays out of it -- it is still
registered as the device's `model` (see coordinator.device_info)."""
assert device_display_name("refrigerator") == "Samsung Refrigerator"
assert device_display_name("range_hood") == "Samsung Range Hood"
assert device_display_name(None) == "Samsung Appliance"
def test_registry_type_names_that_dont_title_case_into_english():
"""Two registry names are SmartThings/Samsung spellings, not what the
appliance is called -- and this one is the user-visible half."""
assert device_display_name("airconditioner") == "Samsung Air Conditioner"
assert device_display_name("ehs") == "Samsung Heat Pump"
+6 -1
View File
@@ -151,8 +151,10 @@ async def test_pattern_a_sub1_device_info_links_via_device_to_master(hass: HomeA
assert info["identifiers"] == {(DOMAIN, f"{master_serial}_1")}
assert info["via_device"] == (DOMAIN, master_serial)
# The subdevice's own /information/vs/1 (real, ARTIK051_DONGLE_FAC_RAC_18K)
# is what names/models this device, not the master's.
# is what models this device, not the master's -- but it never reaches
# the name, which HA slugifies into this subdevice's entity_ids.
assert info["model"] == "ARTIK051_DONGLE_FAC_RAC_18K"
assert info["name"] == "Samsung Air Conditioner Unit 2"
# ---------------------------------------------------------------------------
@@ -251,6 +253,9 @@ async def test_fac_bora_2in1_subdevice_device_info(hass: HomeAssistant):
# Confirmed live by the reporter (DESIGN-177.md section 1): the wall
# subdevice's own identity, distinct from the master's TP2X_FAC_BORA_21K.
assert info["model"] == "TP2X_FAC_BORA_RAC_21K"
# Neither the board string nor the subdevice UUID is a name a user
# wants slugified into `climate.<this>_...`.
assert info["name"] == "Samsung Air Conditioner Unit 2"
async def test_fac_bora_2in1_unique_ids_include_subdevice_prefix(hass: HomeAssistant):