Files
localthings/tests/test_entity_naming.py
T
Marc Billow 10c850f2b9 refactor(i18n): drop the vestigial descriptor name field
Since entity.py started routing named descriptors through the catalog
under desc.key, SamsungEntityDescription.name has been read for its
value nowhere -- only twice as a flag, to decide whether an entity was
translated at all. That left 148 English names duplicated between Python
and translations/en.json with nothing keeping them honest: six had
already drifted, invisibly, because editing the Python side changes
nothing a user sees.

So the field is gone, and translation_key defaults to desc.key. A
descriptor now sets translation_key only to share one catalog entry
across descriptors or to point at a differently-named one, and the
catalog is the only place an entity name exists.

Every descriptor resolves to exactly the translation key, icon, entity
category, enabled-default and gating it did before -- with one
deliberate exception: the hood fan, previously the sole descriptor with
no key at all, now resolves to 'fan'. That is inert, because fan.py sets
_attr_name = None so the entity presents as the device itself.

The three helpers that forwarded a name into a descriptor
(laundry.bool_option_switch, washer._bool_option_switch, air_purifier's
sensor table) lose that parameter. test_translations.py now requires a
catalog entry for every descriptor rather than only translated ones.

Claude-Session: https://claude.ai/code/session_01GiibJZZLWVvyxq7mc7EDNp
2026-07-24 19:45:37 +00:00

52 lines
2.2 KiB
Python

"""Tests for translated entity naming and dynamic instance placeholders."""
from custom_components.localthings.entity import LocalThingsEntity
from custom_components.localthings.registry.capability import Capability
from custom_components.localthings.registry.discovery import BoundEntity
from custom_components.localthings.registry.entities import BinarySensorDesc
class _FakeCoordinator:
device_serial = 'TEST-SERIAL'
def __init__(self, last_resources=None):
self.last_resources = last_resources or {}
def _make_entity(desc, href='/x/vs/0', key_override=None, instance='', instance_name=None):
capability = Capability(href=href, entities=(desc,))
bound = BoundEntity(href=href, capability=capability, desc=desc,
instance=instance, key_override=key_override,
instance_name=instance_name)
return LocalThingsEntity(_FakeCoordinator(), bound)
def test_descriptor_key_is_the_default_translation_key():
"""A descriptor names itself through the catalog, keyed by its own key.
Nothing sets _attr_name: that would take precedence over HA's
translation catalog and make the entity untranslatable.
"""
desc = BinarySensorDesc(key='enabled')
entity = _make_entity(desc, instance_name='Cubed Ice')
assert entity.translation_key == 'enabled'
assert not hasattr(entity, '_attr_name')
def test_device_instance_name_becomes_translation_placeholder():
desc = BinarySensorDesc(
key='enabled', translation_key='instance_enabled', use_instance_name=True
)
entity = _make_entity(desc, key_override='icemaker_one_enabled',
instance_name='Cubed Ice')
assert entity.translation_key == 'instance_enabled'
assert entity.translation_placeholders == {'instance_name': 'Cubed Ice'}
assert not hasattr(entity, '_attr_name')
def test_href_instance_name_becomes_translation_placeholder():
desc = BinarySensorDesc(
key='enabled', translation_key='instance_enabled', use_instance_name=True
)
entity = _make_entity(desc, key_override='icemaker_one_enabled')
assert entity.translation_placeholders == {'instance_name': 'Icemaker One'}