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

+33 -21
View File
@@ -4,11 +4,15 @@ Module-level dicts/constants with no coordinator/entity dependency, so --
like `_temps_vs_item` in test_climate_temperature_fallback.py -- they're
testable directly.
"""
from homeassistant.components.climate import HVACMode
from custom_components.localthings.climate import (
_AI_COMFORT_MODE, _DEVICE_TO_HVAC, _HVAC_TO_DEVICE,
PRESET_AI_COMFORT, _preset_to_ha,
_AI_COMFORT_MODE,
_DEVICE_TO_HVAC,
_HVAC_TO_DEVICE,
PRESET_AI_COMFORT,
_preset_to_ha,
)
@@ -18,7 +22,7 @@ def test_auto_maps_to_hvac_auto():
two-setpoint heat+cool range these single-setpoint units (including
cool-only models) don't have. AIComfort is handled separately, not
folded into this map."""
assert _DEVICE_TO_HVAC['Auto'] == HVACMode.AUTO
assert _DEVICE_TO_HVAC["Auto"] == HVACMode.AUTO
def test_aicomfort_not_in_flat_hvac_map():
@@ -34,19 +38,19 @@ def test_hvac_auto_writes_back_to_plain_auto_not_aicomfort():
ai_comfort preset, since it isn't a flat _DEVICE_TO_HVAC entry (see
test_aicomfort_not_in_flat_hvac_map) and so can never win the reverse
{v: k} dict even though both map to HVACMode.AUTO conceptually."""
assert _HVAC_TO_DEVICE[HVACMode.AUTO] == 'Auto'
assert _HVAC_TO_DEVICE[HVACMode.AUTO] == "Auto"
def test_fan_only_still_reachable_via_wind():
"""Guard against regressing the existing 'Wind' -> FAN_ONLY entry while
editing this map."""
assert _DEVICE_TO_HVAC['Wind'] == HVACMode.FAN_ONLY
assert _DEVICE_TO_HVAC["Wind"] == HVACMode.FAN_ONLY
def test_fan_only_still_reachable_via_fan():
"""'Fan' (e.g. TP1X_DA-AC-RAC-01011) is a second FAN_ONLY spelling
alongside 'Wind' -- issue #91."""
assert _DEVICE_TO_HVAC['Fan'] == HVACMode.FAN_ONLY
assert _DEVICE_TO_HVAC["Fan"] == HVACMode.FAN_ONLY
def test_fan_only_reverse_fallback_prefers_wind():
@@ -56,24 +60,25 @@ def test_fan_only_reverse_fallback_prefers_wind():
original single spelling, predating 'Fan') rather than silently
flipping to whichever of the two duplicate-value entries happens to
come last in _DEVICE_TO_HVAC."""
assert _HVAC_TO_DEVICE[HVACMode.FAN_ONLY] == 'Wind'
assert _HVAC_TO_DEVICE[HVACMode.FAN_ONLY] == "Wind"
def test_preset_ai_comfort_constant():
assert PRESET_AI_COMFORT == 'ai_comfort'
assert PRESET_AI_COMFORT == "ai_comfort"
def test_preset_to_ha_off_maps_to_preset_none():
from homeassistant.components.climate import PRESET_NONE
assert _preset_to_ha('Off') == PRESET_NONE
assert _preset_to_ha("Off") == PRESET_NONE
def test_preset_to_ha_lowercases_other_codes():
"""Every other device code is exposed as its lowercased self -- resolved
dynamically, not via a per-model table (issue #91)."""
assert _preset_to_ha('Sleep') == 'sleep'
assert _preset_to_ha('NanoSleep') == 'nanosleep'
assert _preset_to_ha('MotionIndirect') == 'motionindirect'
assert _preset_to_ha("Sleep") == "sleep"
assert _preset_to_ha("NanoSleep") == "nanosleep"
assert _preset_to_ha("MotionIndirect") == "motionindirect"
def test_fac_bora_wind_strength_codes_fit_the_standard_scale():
@@ -89,16 +94,19 @@ def test_fac_bora_wind_strength_codes_fit_the_standard_scale():
even if fan_modes/fan_mode were completely broken, since it never
touches the entity at all.
"""
from typing import ClassVar, cast
from custom_components.localthings.climate import LocalThingsClimate
from custom_components.localthings.coordinator import LocalThingsCoordinator
from custom_components.localthings.registry import by_type
from custom_components.localthings.registry.discovery import discover
from custom_components.localthings.registry.entities import ClimateDesc
from tests.conftest import _load_device
class _FakeCoordinator:
device_serial = 'TEST-FAC-BORA-SERIAL'
device_info = {}
data = {}
device_serial = "TEST-FAC-BORA-SERIAL"
device_info: ClassVar[dict] = {}
data: ClassVar[dict] = {}
def __init__(self, resources):
self.last_resources = resources
@@ -111,13 +119,17 @@ def test_fac_bora_wind_strength_codes_fit_the_standard_scale():
# canonical view is just the raw snapshot (issue #177).
return self.last_resources
resources = _load_device('airconditioner_fac_bora')
info = resources['/information/vs/0']
resources = _load_device("airconditioner_fac_bora")
info = resources["/information/vs/0"]
reg = by_type.for_device_by_model(
info['x.com.samsung.da.modelNum'], info['x.com.samsung.da.description'])
info["x.com.samsung.da.modelNum"], info["x.com.samsung.da.description"]
)
assert reg is not None
bound = discover(resources, reg.capabilities, reg.pattern_capabilities)
climate_bound = next(item for item in bound if isinstance(item.desc, ClimateDesc))
entity = LocalThingsClimate(_FakeCoordinator(resources), climate_bound)
entity = LocalThingsClimate(
cast(LocalThingsCoordinator, _FakeCoordinator(resources)), climate_bound
)
assert entity.fan_modes == ['auto', 'medium', 'high', 'turbo']
assert entity.fan_mode == 'auto'
assert entity.fan_modes == ["auto", "medium", "high", "turbo"]
assert entity.fan_mode == "auto"