Simplify table-scoped translation key; fix stale resolution; correct docstring
Simplification (feedback: this was overcomplicated): drop the validated_table gate entirely. cycle_select's table_href now just builds the translation key directly from whatever course table the device reports (washer_cycle + Table_02 -> washer_cycle_table_02) instead of comparing against a hardcoded known-good value and falling back to no key on any mismatch. A table we haven't shipped translations for yet (e.g. FlexWash's Table_00) still gets a key built for it -- Home Assistant's own missing-translation handling takes it from there, the same graceful fallback already relied on for any individual untranslated code within an existing table. Adding a newly-confirmed table later is just new strings.json entries, no code change. Independent (Opus) review of the prior version caught two real issues, fixed here regardless of the simplification above: - translation_key was resolved once at entity construction from whatever coordinator.last_resources held at that moment. Discovery can run while a sibling resource is still an empty stub (documented precedent: see _is_included), so a callable translation_key could permanently bake in a stale value for the entity's lifetime. Moved resolution into a translation_key property override (Entity.translation_key is a property upstream, not a plain attribute), re-evaluated against live coordinator data on every access, matching how options/current_option already work. - The supportedOptions fallback's "smallest passing K wins" docstring claimed every larger passing K is an exact multiple of the true one. False: the shipped dishwasher fixture has passing K=7 (true) alongside 10, 14, and 35, none of which are multiples of 7 -- position 0 always lands on the same real course code regardless of K, which alone satisfies the current-course guard for several unrelated splits. Corrected the reasoning to what's actually true (an empirically-matched heuristic across six real dumps, not a proof) and added a regression test locking in the real dishwasher case so this isn't silently lost.
This commit is contained in:
9 files changed
+140
-59
No files matched your search
@@ -59,16 +59,16 @@ def test_power_watts_gated_for_dead_sentinel():
|
||||
|
||||
|
||||
def test_course_bound_to_shared_course_vs_0():
|
||||
"""Dryer course uses the shared /course/vs/0 cycle select with
|
||||
dryer_cycle_table_03 translations, consistent with washer/dishwasher --
|
||||
gated on the device's own course table matching Table_03, the only one
|
||||
dryer_cycle_table_03's names were confirmed against (see
|
||||
laundry.cycle_select)."""
|
||||
"""Dryer course uses the shared /course/vs/0 cycle select, with the
|
||||
translation key built from the device's own course table (see
|
||||
laundry.cycle_select) -- confirmed dryers report Table_03, matching
|
||||
the shipped dryer_cycle_table_03 translations, consistent with
|
||||
washer/dishwasher."""
|
||||
assert dryer.DRYER_COURSE.href == '/course/vs/0'
|
||||
desc = next(e for e in dryer.DRYER_COURSE.entities if e.key == 'cycle')
|
||||
assert callable(desc.translation_key)
|
||||
validated = {'/st/dryercourse/vs/0': {'x.com.samsung.da.st.courseTable': 'Table_03'}}
|
||||
assert desc.translation_key(validated) == 'dryer_cycle_table_03'
|
||||
table_03 = {'/st/dryercourse/vs/0': {'x.com.samsung.da.st.courseTable': 'Table_03'}}
|
||||
assert desc.translation_key(table_03) == 'dryer_cycle_table_03'
|
||||
assert desc.translation_key({}) is None
|
||||
assert desc.options is laundry.cycle_options
|
||||
rep = {'x.com.samsung.da.options': ['Course_16', 'GMT_02']}
|
||||
|
||||
@@ -109,6 +109,28 @@ class TestCourseCodesFromSupportedOptions:
|
||||
assert laundry.cycle_options({}) == []
|
||||
assert laundry.cycle_options({'/course/vs/0': {}}) == []
|
||||
|
||||
def test_smallest_wins_even_when_a_larger_pass_is_not_a_multiple(self):
|
||||
"""Real dishwasher dump (K=7, 10 courses): K=10, 14, and 35 also
|
||||
pass both checks here, and none of them are multiples of 7 --
|
||||
position 0 lands on the same real course code ('0e') regardless of
|
||||
K, which alone satisfies the current-course guard for several
|
||||
unrelated splits. Smallest-K-wins is a heuristic that matches every
|
||||
real dump checked so far, not a proven guarantee -- see
|
||||
_course_codes_from_supported_options's docstring."""
|
||||
resources = {
|
||||
'/course/vs/0': {
|
||||
'x.com.samsung.da.options': ['Course_0E'],
|
||||
'x.com.samsung.da.supportedOptions': [
|
||||
'30E5434B102D102835034B002D002845034B002D002805034B000D000'
|
||||
'865034B002D002075000B000D000905000B000D0008D5034B002D0028'
|
||||
'E5034B000D0008F5034B000D000'
|
||||
],
|
||||
},
|
||||
}
|
||||
assert laundry.cycle_options(resources) == [
|
||||
'0E', '83', '84', '80', '86', '07', '90', '8D', '8E', '8F',
|
||||
]
|
||||
|
||||
|
||||
class TestCycleSelect:
|
||||
def test_builds_labelled_cycle_select(self):
|
||||
@@ -147,39 +169,46 @@ class TestCycleSelect:
|
||||
|
||||
class TestCycleSelectTableGating:
|
||||
"""translation_key becomes a resolver, not a plain string, once
|
||||
table_href/validated_table are given -- washer/dryer's real call sites
|
||||
(issue: course codes aren't guaranteed consistent across board
|
||||
generations sharing the same /course/vs/0 contract; FlexWash's older
|
||||
board reports a different course table than every device the shipped
|
||||
translations were confirmed against)."""
|
||||
table_href is given -- washer/dryer's real call sites (issue: course
|
||||
codes aren't guaranteed consistent across board generations sharing
|
||||
the same /course/vs/0 contract; FlexWash's older board reports a
|
||||
different course table than every device the shipped translations
|
||||
were confirmed against). The resolved key is built from whatever table
|
||||
the device actually reports -- a table with no strings.json entries
|
||||
yet just falls through Home Assistant's own missing-translation
|
||||
handling to raw-code display, the same as any individual untranslated
|
||||
code within an existing table."""
|
||||
|
||||
def _desc(self):
|
||||
return laundry.cycle_select(
|
||||
translation_key='washer_cycle_table_02', icon='x',
|
||||
table_href='/st/washercourse/vs/0', validated_table='Table_02',
|
||||
translation_key='washer_cycle', icon='x',
|
||||
table_href='/st/washercourse/vs/0',
|
||||
)
|
||||
|
||||
def test_static_string_when_no_table_params_given(self):
|
||||
def test_static_string_when_no_table_href_given(self):
|
||||
"""dishwasher's call site -- no equivalent table-id resource in any
|
||||
dump seen, no evidence of the same cross-board risk -- keeps the
|
||||
plain static key unconditionally."""
|
||||
desc = laundry.cycle_select(translation_key='dishwasher_cycle', icon='x')
|
||||
assert desc.translation_key == 'dishwasher_cycle'
|
||||
|
||||
def test_resolves_to_the_key_when_table_matches(self):
|
||||
def test_resolved_key_is_built_from_the_reported_table(self):
|
||||
desc = self._desc()
|
||||
resources = {'/st/washercourse/vs/0': {'x.com.samsung.da.st.courseTable': 'Table_02'}}
|
||||
assert callable(desc.translation_key)
|
||||
assert desc.translation_key(resources) == 'washer_cycle_table_02'
|
||||
|
||||
def test_resolves_to_none_for_a_different_table(self):
|
||||
def test_resolved_key_reflects_an_unbuilt_table_too(self):
|
||||
"""No gating against a hardcoded 'known good' table -- a table we
|
||||
haven't shipped translations for yet still gets a key built for
|
||||
it, just one strings.json has nothing under (raw-code display)."""
|
||||
desc = self._desc()
|
||||
resources = {'/st/washercourse/vs/0': {'x.com.samsung.da.st.courseTable': 'Table_00'}}
|
||||
assert desc.translation_key(resources) is None
|
||||
assert desc.translation_key(resources) == 'washer_cycle_table_00'
|
||||
|
||||
def test_resolves_to_none_when_table_id_is_unknown(self):
|
||||
"""Absence isn't evidence of a match -- no href, or an empty rep,
|
||||
gets no translation_key either, not the family default."""
|
||||
"""No href, or an empty rep, gets no translation_key at all --
|
||||
there's nothing to build a key from."""
|
||||
desc = self._desc()
|
||||
assert desc.translation_key({}) is None
|
||||
assert desc.translation_key({'/st/washercourse/vs/0': {}}) is None
|
||||
|
||||
@@ -57,3 +57,20 @@ def test_callable_options_empty_result():
|
||||
desc = SelectDesc(key='cycle', options=lambda resources: [])
|
||||
entity = _make_select(desc, '/x/vs/0', {})
|
||||
assert entity.options == []
|
||||
|
||||
|
||||
def test_callable_translation_key_reresolves_live_not_once_at_construction():
|
||||
"""A callable translation_key (laundry.cycle_select's table-id-gated
|
||||
resolver) must be re-evaluated against current coordinator data on
|
||||
every access, not baked in once at __init__ -- discovery can run while
|
||||
a sibling resource (e.g. /st/washercourse/vs/0) is still an empty stub
|
||||
(see entity.py's _is_included docstring), and a one-time resolution
|
||||
would permanently show untranslated codes even after a later poll
|
||||
populates the real value."""
|
||||
desc = SelectDesc(key='cycle', translation_key=lambda resources: resources.get('key'))
|
||||
resources = {'key': None}
|
||||
entity = _make_select(desc, '/x/vs/0', resources)
|
||||
assert entity.translation_key is None
|
||||
|
||||
resources['key'] = 'washer_cycle_table_02'
|
||||
assert entity.translation_key == 'washer_cycle_table_02'
|
||||
@@ -61,15 +61,18 @@ class TestWasherCourse:
|
||||
assert washer.WASHER_COURSE.href == '/course/vs/0'
|
||||
|
||||
def test_translation_key(self):
|
||||
"""Table-gated (issue: FlexWash's older board reports a different
|
||||
course table, Table_00, than every device washer_cycle_table_02's
|
||||
names were confirmed against, Table_02) -- see laundry.cycle_select."""
|
||||
"""Table-scoped (issue: course codes aren't guaranteed consistent
|
||||
across board generations sharing /course/vs/0 -- FlexWash's older
|
||||
board reports Table_00, not the Table_02 every washer_cycle_table_02
|
||||
name was confirmed against) -- see laundry.cycle_select. The key is
|
||||
built from whatever table the device reports, not gated against a
|
||||
hardcoded 'known good' value."""
|
||||
desc = next(e for e in washer.WASHER_COURSE.entities if e.key == 'cycle')
|
||||
assert callable(desc.translation_key)
|
||||
validated = {'/st/washercourse/vs/0': {'x.com.samsung.da.st.courseTable': 'Table_02'}}
|
||||
assert desc.translation_key(validated) == 'washer_cycle_table_02'
|
||||
other_table = {'/st/washercourse/vs/0': {'x.com.samsung.da.st.courseTable': 'Table_00'}}
|
||||
assert desc.translation_key(other_table) is None
|
||||
table_02 = {'/st/washercourse/vs/0': {'x.com.samsung.da.st.courseTable': 'Table_02'}}
|
||||
assert desc.translation_key(table_02) == 'washer_cycle_table_02'
|
||||
table_00 = {'/st/washercourse/vs/0': {'x.com.samsung.da.st.courseTable': 'Table_00'}}
|
||||
assert desc.translation_key(table_00) == 'washer_cycle_table_00'
|
||||
assert desc.translation_key({}) is None
|
||||
|
||||
def test_reads_raw_course_code_from_options_array(self):
|
||||
|
||||
Reference in new issue
Block a user