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:
147 files changed
+9977
-7584
No files matched your search
@@ -2,6 +2,9 @@
|
||||
(custom_components/localthings/select.py) -- the static tuple, options_field,
|
||||
and callable forms of SelectDesc.options.
|
||||
"""
|
||||
|
||||
from typing import ClassVar
|
||||
|
||||
from custom_components.localthings.registry.capability import Capability
|
||||
from custom_components.localthings.registry.discovery import BoundEntity
|
||||
from custom_components.localthings.registry.entities import SelectDesc
|
||||
@@ -9,7 +12,7 @@ from custom_components.localthings.select import LocalThingsSelect
|
||||
|
||||
|
||||
class _FakeCoordinator:
|
||||
device_serial = 'TEST-SERIAL'
|
||||
device_serial = "TEST-SERIAL"
|
||||
|
||||
def __init__(self, last_resources):
|
||||
self.last_resources = last_resources
|
||||
@@ -28,15 +31,15 @@ def _make_select(desc, href, last_resources):
|
||||
|
||||
|
||||
def test_static_options_unaffected():
|
||||
desc = SelectDesc(key='x', options=('A', 'B'))
|
||||
entity = _make_select(desc, '/x/vs/0', {})
|
||||
assert entity.options == ['A', 'B']
|
||||
desc = SelectDesc(key="x", options=("A", "B"))
|
||||
entity = _make_select(desc, "/x/vs/0", {})
|
||||
assert entity.options == ["A", "B"]
|
||||
|
||||
|
||||
def test_options_field_unaffected():
|
||||
desc = SelectDesc(key='x', options_field='supported')
|
||||
entity = _make_select(desc, '/x/vs/0', {'/x/vs/0': {'supported': ['Lo', 'Hi']}})
|
||||
assert entity.options == ['Lo', 'Hi']
|
||||
desc = SelectDesc(key="x", options_field="supported")
|
||||
entity = _make_select(desc, "/x/vs/0", {"/x/vs/0": {"supported": ["Lo", "Hi"]}})
|
||||
assert entity.options == ["Lo", "Hi"]
|
||||
|
||||
|
||||
def test_callable_options_receives_full_resource_snapshot():
|
||||
@@ -47,21 +50,21 @@ def test_callable_options_receives_full_resource_snapshot():
|
||||
|
||||
def _options_fn(resources):
|
||||
calls.append(resources)
|
||||
return list(resources.get('/other/vs/0', {}).get('codes', []))
|
||||
return list(resources.get("/other/vs/0", {}).get("codes", []))
|
||||
|
||||
desc = SelectDesc(key='cycle', translation_key='fake_cycle', options=_options_fn)
|
||||
desc = SelectDesc(key="cycle", translation_key="fake_cycle", options=_options_fn)
|
||||
resources = {
|
||||
'/x/vs/0': {},
|
||||
'/other/vs/0': {'codes': ['1C', '1D']},
|
||||
"/x/vs/0": {},
|
||||
"/other/vs/0": {"codes": ["1C", "1D"]},
|
||||
}
|
||||
entity = _make_select(desc, '/x/vs/0', resources)
|
||||
assert entity.options == ['1C', '1D']
|
||||
entity = _make_select(desc, "/x/vs/0", resources)
|
||||
assert entity.options == ["1C", "1D"]
|
||||
assert calls == [resources]
|
||||
|
||||
|
||||
def test_callable_options_empty_result():
|
||||
desc = SelectDesc(key='cycle', options=lambda resources: [])
|
||||
entity = _make_select(desc, '/x/vs/0', {})
|
||||
desc = SelectDesc(key="cycle", options=lambda resources: [])
|
||||
entity = _make_select(desc, "/x/vs/0", {})
|
||||
assert entity.options == []
|
||||
|
||||
|
||||
@@ -73,19 +76,20 @@ def test_callable_translation_key_reresolves_live_not_once_at_construction():
|
||||
(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)
|
||||
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'
|
||||
resources["key"] = "washer_cycle_table_02"
|
||||
assert entity.translation_key == "washer_cycle_table_02"
|
||||
|
||||
|
||||
async def test_unknown_vendor_option_round_trips_to_exact_raw_value():
|
||||
"""Readable fallback labels must still write the exact Samsung token."""
|
||||
|
||||
class _WritableCoordinator(_FakeCoordinator):
|
||||
data = {'mode': 'FutureVendorMode'}
|
||||
data: ClassVar[dict] = {"mode": "FutureVendorMode"}
|
||||
|
||||
def __init__(self, last_resources):
|
||||
super().__init__(last_resources)
|
||||
@@ -95,14 +99,16 @@ async def test_unknown_vendor_option_round_trips_to_exact_raw_value():
|
||||
self.writes.append(value)
|
||||
|
||||
desc = SelectDesc(
|
||||
key='mode', translation_key='door_alert',
|
||||
options=('Known', 'FutureVendorMode'), write_fn=lambda *args: None,
|
||||
key="mode",
|
||||
translation_key="door_alert",
|
||||
options=("Known", "FutureVendorMode"),
|
||||
write_fn=lambda *args: None,
|
||||
)
|
||||
capability = Capability(href='/x/vs/0', entities=(desc,))
|
||||
bound = BoundEntity(href='/x/vs/0', capability=capability, desc=desc)
|
||||
capability = Capability(href="/x/vs/0", entities=(desc,))
|
||||
bound = BoundEntity(href="/x/vs/0", capability=capability, desc=desc)
|
||||
coordinator = _WritableCoordinator({})
|
||||
entity = LocalThingsSelect(coordinator, bound)
|
||||
|
||||
assert entity.options[-1] == 'Future Vendor Mode'
|
||||
await entity.async_select_option('Future Vendor Mode')
|
||||
assert coordinator.writes == ['FutureVendorMode']
|
||||
assert entity.options[-1] == "Future Vendor Mode"
|
||||
await entity.async_select_option("Future Vendor Mode")
|
||||
assert coordinator.writes == ["FutureVendorMode"]
|
||||
Reference in new issue
Block a user