Issue #27: the flex-zone/cooler-drawer select vanished after a device stopped including supportedOptions on an update for /mode/vs/0. ObserveManager.apply() handed reps straight to StateCache.apply_rep, which fully replaces the cached rep -- so a partial update (missing a field the select's exists_fn/options_field gate on) silently erased data a fuller update had previously supplied. apply() now merges incoming reps onto whatever's already cached instead. Also give ice-maker entities (and any future pattern-cap instance) a device-given display name instead of the href-derived "Icemaker One"/"Icemaker Two": Capability.name_field lets a pattern capability read and normalize an instance name (e.g. iceMaker.name's "CUBED_ICE") for use as the entity name prefix, independent of the stable key/unique_id.
46 lines
1.9 KiB
Python
46 lines
1.9 KiB
Python
"""Tests for LocalThingsEntity's display-name derivation
|
|
(custom_components/localthings/entity.py) -- the explicit-name,
|
|
device-given-instance-name, and href-derived fallbacks.
|
|
"""
|
|
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_explicit_name_wins_over_everything():
|
|
desc = BinarySensorDesc(key='enabled', name='Explicit Name')
|
|
entity = _make_entity(desc, instance_name='Cubed Ice')
|
|
assert entity._attr_name == 'Explicit Name'
|
|
|
|
|
|
def test_instance_name_prefixes_the_derived_suffix():
|
|
"""Issue #27: an ice maker's device-given name ("Cubed Ice") replaces
|
|
the href-derived instance label ("Icemaker One") as the name prefix,
|
|
keeping the same entity-specific suffix."""
|
|
desc = BinarySensorDesc(key='enabled')
|
|
entity = _make_entity(desc, key_override='icemaker_one_enabled',
|
|
instance_name='Cubed Ice')
|
|
assert entity._attr_name == 'Cubed Ice Enabled'
|
|
|
|
|
|
def test_no_instance_name_falls_back_to_derived_state_key():
|
|
desc = BinarySensorDesc(key='enabled')
|
|
entity = _make_entity(desc, key_override='icemaker_one_enabled')
|
|
assert entity._attr_name == 'Icemaker One Enabled'
|