Add ruff (lint + format) and ty (type checking) to the project

Adds [tool.ruff] and [tool.ty] config to pyproject.toml with a curated
ruff rule set (E, F, W, I, UP, B, C4, SIM, RUF, ASYNC, LOG, G, PIE, RET,
PERF, N), pins ruff/ty in requirements-dev.txt, reformats the whole tree
with `ruff format`, and fixes the pre-existing lint and type-check debt
those tools surfaced so both run clean.

Production-code type fixes include: HA's ConfigFlowResult vs. the
generic FlowResult in config_flow.py, narrowing BoundEntity.desc to its
platform-specific subclass (SelectDesc/NumberDesc/SensorDesc/etc.) via
cast() instead of an unchecked annotation, converting HA device_class
strings to their proper enum types, a resolve_registry callback typed
as `object` instead of `DeviceRegistry | None`, and a couple of other
narrow correctness fixes (CA key type validation, an index-out-of-bounds
false positive from an empty-tuple fallback, a bool/dict argument swap).

Test-file fixes are mechanical: narrowing SamsungEntityDescription to
the correct subclass via isinstance()/cast() before accessing
subclass-only fields, and asserting Optional write_fn/unit_fn fields
are set before calling them.
This commit is contained in:
Marc Billow committed 2026-08-02 23:56:38 +00:00
1 parent b0eab93b58
commit daf7e3787f
147 files changed
+9977 -7584

No files matched your search

+128 -83
View File
@@ -1,39 +1,62 @@
"""Tests for the shared laundry capabilities (washer/dryer/dishwasher)."""
from custom_components.localthings.registry.capabilities import laundry
class TestCourseHelpers:
def test_parses_edit_course_list(self):
raw = 'EditCourseList_1C1D211B1E29243328262722202325322F2E30662D8F962B2A'
raw = "EditCourseList_1C1D211B1E29243328262722202325322F2E30662D8F962B2A"
assert laundry.parse_edit_course_list(raw) == [
'1C', '1D', '21', '1B', '1E', '29', '24', '33', '28', '26', '27',
'22', '20', '23', '25', '32', '2F', '2E', '30', '66', '2D', '8F', '96',
'2B', '2A'
"1C",
"1D",
"21",
"1B",
"1E",
"29",
"24",
"33",
"28",
"26",
"27",
"22",
"20",
"23",
"25",
"32",
"2F",
"2E",
"30",
"66",
"2D",
"8F",
"96",
"2B",
"2A",
]
def test_parse_edit_course_list_handles_missing_or_malformed(self):
assert laundry.parse_edit_course_list(None) == []
assert laundry.parse_edit_course_list('') == []
assert laundry.parse_edit_course_list('no underscore') == []
assert laundry.parse_edit_course_list("") == []
assert laundry.parse_edit_course_list("no underscore") == []
def test_cycle_options_reads_live_edit_course_list(self):
"""The device's own course list -- including a code ('65') never seen
on the primary dump -- is used as-is; there is no hardcoded table."""
resources = {
'/wm/editcourse/vs/0': {
'x.com.samsung.da.editCourseList': 'EditCourseList_651C',
"/wm/editcourse/vs/0": {
"x.com.samsung.da.editCourseList": "EditCourseList_651C",
},
}
assert laundry.cycle_options(resources) == ['65', '1C']
assert laundry.cycle_options(resources) == ["65", "1C"]
def test_cycle_options_empty_when_resource_absent_or_empty(self):
assert laundry.cycle_options({}) == []
assert laundry.cycle_options({'/wm/editcourse/vs/0': {}}) == []
assert laundry.cycle_options({"/wm/editcourse/vs/0": {}}) == []
def test_option_value(self):
opts = ['DeviceType_0167', 'Course_1C', 'GMT_04']
assert laundry.option_value(opts, 'Course') == '1C'
assert laundry.option_value(opts, 'Missing') is None
opts = ["DeviceType_0167", "Course_1C", "GMT_04"]
assert laundry.option_value(opts, "Course") == "1C"
assert laundry.option_value(opts, "Missing") is None
class TestCourseCodesFromSupportedOptions:
@@ -49,45 +72,57 @@ class TestCourseCodesFromSupportedOptions:
# checked item, and by six other independent devices' already-shipped
# translations agreeing on the same code -> name mapping.
_REAL_SUPPORTED_OPTIONS = (
'31C8410923FA67F1B847E923FA67F25843E933FA57F20857E943FA67F'
'088000913FA67F7485209204A5208780009000A00006841E930FA30F'
'7F841E920FA30F65841E943FA57F8F8102923FA57F96841E920FA37F'
'34841E923FA67FA0811E933FA33F'
"31C8410923FA67F1B847E923FA67F25843E933FA57F20857E943FA67F"
"088000913FA67F7485209204A5208780009000A00006841E930FA30F"
"7F841E920FA30F65841E943FA57F8F8102923FA57F96841E920FA37F"
"34841E923FA67FA0811E933FA33F"
)
def test_derives_codes_when_edit_course_list_is_empty(self):
resources = {
'/wm/editcourse/vs/0': {'x.com.samsung.da.editCourseList': ''},
'/course/vs/0': {
'x.com.samsung.da.options': ['Course_1C'],
'x.com.samsung.da.supportedOptions': [self._REAL_SUPPORTED_OPTIONS],
"/wm/editcourse/vs/0": {"x.com.samsung.da.editCourseList": ""},
"/course/vs/0": {
"x.com.samsung.da.options": ["Course_1C"],
"x.com.samsung.da.supportedOptions": [self._REAL_SUPPORTED_OPTIONS],
},
}
assert laundry.cycle_options(resources) == [
'1C', '1B', '25', '20', '08', '74', '87', '06',
'7F', '65', '8F', '96', '34', 'A0',
"1C",
"1B",
"25",
"20",
"08",
"74",
"87",
"06",
"7F",
"65",
"8F",
"96",
"34",
"A0",
]
def test_edit_course_list_still_takes_priority(self):
"""A live editCourseList wins even with supportedOptions present --
no reason to prefer a derived list over the authoritative one."""
resources = {
'/wm/editcourse/vs/0': {'x.com.samsung.da.editCourseList': 'EditCourseList_651C'},
'/course/vs/0': {
'x.com.samsung.da.options': ['Course_1C'],
'x.com.samsung.da.supportedOptions': [self._REAL_SUPPORTED_OPTIONS],
"/wm/editcourse/vs/0": {"x.com.samsung.da.editCourseList": "EditCourseList_651C"},
"/course/vs/0": {
"x.com.samsung.da.options": ["Course_1C"],
"x.com.samsung.da.supportedOptions": [self._REAL_SUPPORTED_OPTIONS],
},
}
assert laundry.cycle_options(resources) == ['65', '1C']
assert laundry.cycle_options(resources) == ["65", "1C"]
def test_rejects_a_table_missing_the_current_course(self):
"""The device's own currently-selected course must be a member of
its derived list -- a mismatch means the guess is wrong, not that
the device selected something outside its own supported set."""
resources = {
'/course/vs/0': {
'x.com.samsung.da.options': ['Course_FF'],
'x.com.samsung.da.supportedOptions': [self._REAL_SUPPORTED_OPTIONS],
"/course/vs/0": {
"x.com.samsung.da.options": ["Course_FF"],
"x.com.samsung.da.supportedOptions": [self._REAL_SUPPORTED_OPTIONS],
},
}
assert laundry.cycle_options(resources) == []
@@ -99,16 +134,16 @@ class TestCourseCodesFromSupportedOptions:
current course" carry over for free) -- but K=1 is the real, most
specific table and must be the one returned."""
resources = {
'/course/vs/0': {
'x.com.samsung.da.options': ['Course_AA'],
'x.com.samsung.da.supportedOptions': ['0AABBCCDD'],
"/course/vs/0": {
"x.com.samsung.da.options": ["Course_AA"],
"x.com.samsung.da.supportedOptions": ["0AABBCCDD"],
},
}
assert laundry.cycle_options(resources) == ['AA', 'BB', 'CC', 'DD']
assert laundry.cycle_options(resources) == ["AA", "BB", "CC", "DD"]
def test_empty_without_supported_options_or_course_href(self):
assert laundry.cycle_options({}) == []
assert laundry.cycle_options({'/course/vs/0': {}}) == []
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
@@ -119,54 +154,63 @@ class TestCourseCodesFromSupportedOptions:
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'
"/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',
"0E",
"83",
"84",
"80",
"86",
"07",
"90",
"8D",
"8E",
"8F",
]
class TestCycleSelect:
def test_builds_labelled_cycle_select(self):
desc = laundry.cycle_select(translation_key='dryer_cycle', icon='mdi:tumble-dryer')
assert desc.key == 'cycle'
assert desc.translation_key == 'dryer_cycle'
assert desc.icon == 'mdi:tumble-dryer'
desc = laundry.cycle_select(translation_key="dryer_cycle", icon="mdi:tumble-dryer")
assert desc.key == "cycle"
assert desc.translation_key == "dryer_cycle"
assert desc.icon == "mdi:tumble-dryer"
assert desc.options is laundry.cycle_options
def test_reads_raw_course_code_from_options(self):
desc = laundry.cycle_select(translation_key='dryer_cycle', icon='x')
rep = {'x.com.samsung.da.options': ['DeviceType_0167', 'Course_16', 'GMT_04']}
assert desc.rep_fn(rep) == '16'
assert desc.rep_fn({'x.com.samsung.da.options': ['GMT_04']}) is None
desc = laundry.cycle_select(translation_key="dryer_cycle", icon="x")
rep = {"x.com.samsung.da.options": ["DeviceType_0167", "Course_16", "GMT_04"]}
assert desc.rep_fn(rep) == "16"
assert desc.rep_fn({"x.com.samsung.da.options": ["GMT_04"]}) is None
def test_exists_only_when_edit_course_list_is_live(self):
desc = laundry.cycle_select(translation_key='dryer_cycle', icon='x')
desc = laundry.cycle_select(translation_key="dryer_cycle", icon="x")
assert desc.exists_fn({}, {}) is False
assert desc.exists_fn({}, {'/wm/editcourse/vs/0': {}}) is False
live = {'/wm/editcourse/vs/0': {'x.com.samsung.da.editCourseList': 'EditCourseList_16'}}
assert desc.exists_fn({}, {"/wm/editcourse/vs/0": {}}) is False
live = {"/wm/editcourse/vs/0": {"x.com.samsung.da.editCourseList": "EditCourseList_16"}}
assert desc.exists_fn({}, live) is True
def test_cycle_write_is_single_token(self):
"""Confirmed on real hardware (issue #54): the device merges a
single-token options[] write by prefix itself, so the write only
needs to carry the changed token, not the whole rewritten array."""
desc = laundry.cycle_select(translation_key='dryer_cycle', icon='x')
rep = {'x.com.samsung.da.options': ['DeviceType_0167', 'Course_16', 'GMT_04']}
path, body = desc.write_fn('1D', rep)
assert path == ['course', 'vs', '0']
assert body == {'x.com.samsung.da.options': ['Course_1D']}
desc = laundry.cycle_select(translation_key="dryer_cycle", icon="x")
rep = {"x.com.samsung.da.options": ["DeviceType_0167", "Course_16", "GMT_04"]}
path, body = desc.write_fn("1D", rep)
assert path == ["course", "vs", "0"]
assert body == {"x.com.samsung.da.options": ["Course_1D"]}
def test_cycle_write_noop_without_options(self):
desc = laundry.cycle_select(translation_key='dryer_cycle', icon='x')
assert desc.write_fn('1D', {}) is None
desc = laundry.cycle_select(translation_key="dryer_cycle", icon="x")
assert desc.write_fn("1D", {}) is None
class TestCycleSelectTableGating:
@@ -181,56 +225,57 @@ class TestCycleSelectTableGating:
def _desc(self):
return laundry.cycle_select(
translation_key='washer_cycle', icon='x',
table_href='/st/washercourse/vs/0',
translation_key="washer_cycle",
icon="x",
table_href="/st/washercourse/vs/0",
)
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'
desc = laundry.cycle_select(translation_key="dishwasher_cycle", icon="x")
assert desc.translation_key == "dishwasher_cycle"
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'}}
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'
assert desc.translation_key(resources) == "washer_cycle_table_02"
def test_untranslated_table_uses_generic_cycle_key(self):
"""An unknown table does not claim another board's state labels."""
desc = self._desc()
resources = {'/st/washercourse/vs/0': {'x.com.samsung.da.st.courseTable': 'Table_00'}}
assert desc.translation_key(resources) == 'cycle'
resources = {"/st/washercourse/vs/0": {"x.com.samsung.da.st.courseTable": "Table_00"}}
assert desc.translation_key(resources) == "cycle"
def test_resolves_to_generic_cycle_when_table_id_is_unknown(self):
"""An absent table id still gets a translated generic entity name."""
desc = self._desc()
assert desc.translation_key({}) == 'cycle'
assert desc.translation_key({'/st/washercourse/vs/0': {}}) == 'cycle'
assert desc.translation_key({}) == "cycle"
assert desc.translation_key({"/st/washercourse/vs/0": {}}) == "cycle"
class TestBuzzerSound:
def test_href(self):
assert laundry.BUZZER_SOUND.href == '/buzzersound/vs/0'
assert laundry.BUZZER_SOUND.href == "/buzzersound/vs/0"
def test_buzzer_sound_write(self):
desc = next(e for e in laundry.BUZZER_SOUND.entities if e.key == 'buzzer_sound')
assert desc.options_field == 'supportedBuzzerSound'
path, body = desc.write_fn('On', {})
assert path == ['buzzersound', 'vs', '0']
assert body == {'setBuzzerSound': 'On'}
desc = next(e for e in laundry.BUZZER_SOUND.entities if e.key == "buzzer_sound")
assert desc.options_field == "supportedBuzzerSound"
path, body = desc.write_fn("On", {})
assert path == ["buzzersound", "vs", "0"]
assert body == {"setBuzzerSound": "On"}
def test_finish_sound_exists_only_when_supported(self):
desc = next(e for e in laundry.BUZZER_SOUND.entities if e.key == 'finish_sound')
assert desc.exists_fn({'setBuzzerSound': 'On'}, {}) is False
assert desc.exists_fn({'supportedFinishSound': ['FinishSound_1']}, {}) is True
desc = next(e for e in laundry.BUZZER_SOUND.entities if e.key == "finish_sound")
assert desc.exists_fn({"setBuzzerSound": "On"}, {}) is False
assert desc.exists_fn({"supportedFinishSound": ["FinishSound_1"]}, {}) is True
class TestJobBeginningStatus:
def test_href_and_field(self):
assert laundry.JOB_BEGINNING_STATUS.href == '/wm/jobbeginingstatus/vs/0'
assert laundry.JOB_BEGINNING_STATUS.href == "/wm/jobbeginingstatus/vs/0"
desc = laundry.JOB_BEGINNING_STATUS.entities[0]
assert desc.field == 'x.com.samsung.da.currentStatus'
assert desc.entity_category == 'diagnostic'
assert desc.field == "x.com.samsung.da.currentStatus"
assert desc.entity_category == "diagnostic"