Fix more pre-existing ty diagnostics in test files
Continues narrowing SamsungEntityDescription accesses to the correct subclass and asserting Optional write_fn/match_fn/exists_fn fields are set before calling them, per the pattern established in the previous commit.
This commit is contained in:
18 files changed
+430
-122
No files matched your search
@@ -3,6 +3,11 @@
|
||||
from custom_components.localthings.registry.by_type import for_device_by_model
|
||||
from custom_components.localthings.registry.capabilities import oven
|
||||
from custom_components.localthings.registry.discovery import discover
|
||||
from custom_components.localthings.registry.entities import (
|
||||
NumberDesc,
|
||||
SelectDesc,
|
||||
SwitchDesc,
|
||||
)
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Device-type detection + full-dump coverage (issue #55)
|
||||
@@ -33,15 +38,21 @@ def test_oven_fixture_resolves_and_has_no_unbound_hrefs():
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
def _oven_setpoint_desc():
|
||||
return next(e for e in oven.OVEN_SETPOINT.entities if isinstance(e, NumberDesc))
|
||||
|
||||
|
||||
def test_oven_setpoint_write_is_read_modify_write():
|
||||
cap = oven.OVEN_SETPOINT
|
||||
desc = cap.entities[0]
|
||||
desc = _oven_setpoint_desc()
|
||||
rep = {
|
||||
"x.com.samsung.da.items": [
|
||||
{"x.com.samsung.da.id": "Target", "x.com.samsung.da.temperature": 0}
|
||||
]
|
||||
}
|
||||
path, body = desc.write_fn(200, rep)
|
||||
assert desc.write_fn is not None
|
||||
result = desc.write_fn(200, rep)
|
||||
assert result is not None
|
||||
path, body = result
|
||||
assert path[-1] == "0" # writes back to the resource
|
||||
# the produced body preserves the items-array shape the oven expects
|
||||
assert "x.com.samsung.da.items" in body or "x.com.samsung.da.temperature" in str(body)
|
||||
@@ -49,7 +60,7 @@ def test_oven_setpoint_write_is_read_modify_write():
|
||||
|
||||
def test_oven_setpoint_rmw_preserves_other_item_fields():
|
||||
"""RMW must not drop sibling fields (e.g. x.com.samsung.da.current)."""
|
||||
desc = oven.OVEN_SETPOINT.entities[0]
|
||||
desc = _oven_setpoint_desc()
|
||||
rep = {
|
||||
"x.com.samsung.da.items": [
|
||||
{
|
||||
@@ -58,7 +69,10 @@ def test_oven_setpoint_rmw_preserves_other_item_fields():
|
||||
}
|
||||
]
|
||||
}
|
||||
_path, body = desc.write_fn(200, rep)
|
||||
assert desc.write_fn is not None
|
||||
result = desc.write_fn(200, rep)
|
||||
assert result is not None
|
||||
_path, body = result
|
||||
item = body["x.com.samsung.da.items"][0]
|
||||
assert item["x.com.samsung.da.desired"] == "200"
|
||||
# sibling field preserved
|
||||
@@ -67,22 +81,27 @@ def test_oven_setpoint_rmw_preserves_other_item_fields():
|
||||
|
||||
def test_oven_setpoint_clamps_to_step():
|
||||
"""Setpoint must be a multiple of SETPOINT_STEP_C."""
|
||||
desc = oven.OVEN_SETPOINT.entities[0]
|
||||
desc = _oven_setpoint_desc()
|
||||
rep = {"x.com.samsung.da.items": [{"x.com.samsung.da.desired": "0"}]}
|
||||
_, body = desc.write_fn(202, rep)
|
||||
assert desc.write_fn is not None
|
||||
result = desc.write_fn(202, rep)
|
||||
assert result is not None
|
||||
_, body = result
|
||||
# 202 → nearest 5 = 200
|
||||
assert body["x.com.samsung.da.items"][0]["x.com.samsung.da.desired"] == "200"
|
||||
|
||||
|
||||
def test_oven_setpoint_rejects_out_of_range():
|
||||
desc = oven.OVEN_SETPOINT.entities[0]
|
||||
desc = _oven_setpoint_desc()
|
||||
rep = {"x.com.samsung.da.items": [{"x.com.samsung.da.desired": "100"}]}
|
||||
assert desc.write_fn is not None
|
||||
assert desc.write_fn(10, rep) is None # below min (30)
|
||||
assert desc.write_fn(300, rep) is None # above max (270)
|
||||
|
||||
|
||||
def test_oven_setpoint_rejects_missing_items():
|
||||
desc = oven.OVEN_SETPOINT.entities[0]
|
||||
desc = _oven_setpoint_desc()
|
||||
assert desc.write_fn is not None
|
||||
assert desc.write_fn(200, {}) is None
|
||||
|
||||
|
||||
@@ -105,24 +124,31 @@ def _fahrenheit_rep(desired="0"):
|
||||
|
||||
|
||||
def test_oven_setpoint_write_uses_fahrenheit_bounds():
|
||||
desc = oven.OVEN_SETPOINT.entities[0]
|
||||
desc = _oven_setpoint_desc()
|
||||
rep = _fahrenheit_rep()
|
||||
# 350 is within F bounds (175-550) but above the C max (270) --
|
||||
# confirms the write path isn't silently still clamping to Celsius.
|
||||
_, body = desc.write_fn(350, rep)
|
||||
assert desc.write_fn is not None
|
||||
result = desc.write_fn(350, rep)
|
||||
assert result is not None
|
||||
_, body = result
|
||||
assert body["x.com.samsung.da.items"][0]["x.com.samsung.da.desired"] == "350"
|
||||
|
||||
|
||||
def test_oven_setpoint_rejects_out_of_range_fahrenheit():
|
||||
desc = oven.OVEN_SETPOINT.entities[0]
|
||||
desc = _oven_setpoint_desc()
|
||||
rep = _fahrenheit_rep()
|
||||
assert desc.write_fn is not None
|
||||
assert desc.write_fn(100, rep) is None # below F min (175)
|
||||
assert desc.write_fn(600, rep) is None # above F max (550)
|
||||
|
||||
|
||||
def test_oven_setpoint_native_bounds_track_live_unit():
|
||||
desc = oven.OVEN_SETPOINT.entities[0]
|
||||
desc = _oven_setpoint_desc()
|
||||
celsius_rep = {"x.com.samsung.da.items": [{"x.com.samsung.da.unit": "Celsius"}]}
|
||||
assert desc.native_min_fn is not None
|
||||
assert desc.native_max_fn is not None
|
||||
assert desc.step_fn is not None
|
||||
assert desc.native_min_fn(celsius_rep) == 30.0
|
||||
assert desc.native_max_fn(celsius_rep) == 270.0
|
||||
fahrenheit_rep = _fahrenheit_rep()
|
||||
@@ -136,21 +162,25 @@ def test_oven_setpoint_native_bounds_track_live_unit():
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
def _oven_mode_desc():
|
||||
return next(e for e in oven.OVEN_MODE.entities if isinstance(e, SelectDesc))
|
||||
|
||||
|
||||
def test_oven_mode_options_nonempty():
|
||||
desc = oven.OVEN_MODE.entities[0]
|
||||
desc = _oven_mode_desc()
|
||||
assert callable(desc.options)
|
||||
assert len(desc.options({})) > 0
|
||||
|
||||
|
||||
def test_oven_mode_options_falls_back_when_no_live_supported_modes():
|
||||
desc = oven.OVEN_MODE.entities[0]
|
||||
desc = _oven_mode_desc()
|
||||
assert desc.options({}) == list(oven._OVEN_MODES)
|
||||
|
||||
|
||||
def test_oven_mode_options_reads_live_supported_modes():
|
||||
"""issue #138: the device's own supportedModes list is used verbatim
|
||||
when present, instead of the static _OVEN_MODES guess."""
|
||||
desc = oven.OVEN_MODE.entities[0]
|
||||
desc = _oven_mode_desc()
|
||||
resources = {
|
||||
"/mode/vs/0": {
|
||||
"x.com.samsung.da.supportedModes": ["Bake", "AirFryer", "SelfClean"],
|
||||
@@ -160,15 +190,19 @@ def test_oven_mode_options_reads_live_supported_modes():
|
||||
|
||||
|
||||
def test_oven_mode_write_round_trips():
|
||||
desc = oven.OVEN_MODE.entities[0]
|
||||
desc = _oven_mode_desc()
|
||||
valid_mode = desc.options({})[1] # e.g. 'Bake'
|
||||
path, body = desc.write_fn(valid_mode, {})
|
||||
assert desc.write_fn is not None
|
||||
result = desc.write_fn(valid_mode, {})
|
||||
assert result is not None
|
||||
path, body = result
|
||||
assert path == ["mode", "vs", "0"]
|
||||
assert body["x.com.samsung.da.modes"] == [valid_mode]
|
||||
|
||||
|
||||
def test_oven_mode_rejects_unknown():
|
||||
desc = oven.OVEN_MODE.entities[0]
|
||||
desc = _oven_mode_desc()
|
||||
assert desc.write_fn is not None
|
||||
assert desc.write_fn("SpaghettiMode", {}) is None
|
||||
|
||||
|
||||
@@ -177,9 +211,12 @@ def test_oven_mode_write_validates_against_live_supported_modes():
|
||||
list, not the static fallback -- issue #138's AirFryer/SelfClean/etc.
|
||||
are accepted, and a mode outside the device's own list is rejected even
|
||||
if some other model's static guess would have allowed it."""
|
||||
desc = oven.OVEN_MODE.entities[0]
|
||||
desc = _oven_mode_desc()
|
||||
rep = {"x.com.samsung.da.supportedModes": ["Bake", "AirFryer", "SelfClean"]}
|
||||
path, body = desc.write_fn("AirFryer", rep)
|
||||
assert desc.write_fn is not None
|
||||
result = desc.write_fn("AirFryer", rep)
|
||||
assert result is not None
|
||||
path, body = result
|
||||
assert path == ["mode", "vs", "0"]
|
||||
assert body["x.com.samsung.da.modes"] == ["AirFryer"]
|
||||
assert desc.write_fn("FrozenPizzaPlus", rep) is None
|
||||
@@ -205,20 +242,29 @@ def _mode_rep(*extra_opts):
|
||||
|
||||
|
||||
def test_lamp_write_is_single_token():
|
||||
desc = next(e for e in oven.OVEN_MODE.entities if e.key == "lamp")
|
||||
path, body = desc.write_fn("On", _mode_rep())
|
||||
desc = next(e for e in oven.OVEN_MODE.entities if e.key == "lamp" and isinstance(e, SwitchDesc))
|
||||
assert desc.write_fn is not None
|
||||
result = desc.write_fn("On", _mode_rep())
|
||||
assert result is not None
|
||||
path, body = result
|
||||
assert path == ["mode", "vs", "0"]
|
||||
assert body == {"x.com.samsung.da.options": ["UpperLamp_On"]}
|
||||
|
||||
|
||||
def test_lamp_write_requires_existing_options():
|
||||
desc = next(e for e in oven.OVEN_MODE.entities if e.key == "lamp")
|
||||
desc = next(e for e in oven.OVEN_MODE.entities if e.key == "lamp" and isinstance(e, SwitchDesc))
|
||||
assert desc.write_fn is not None
|
||||
assert desc.write_fn("On", {}) is None
|
||||
|
||||
|
||||
def test_sound_write_is_single_token():
|
||||
desc = next(e for e in oven.OVEN_MODE.entities if e.key == "sound")
|
||||
_path, body = desc.write_fn("Off", _mode_rep())
|
||||
desc = next(
|
||||
e for e in oven.OVEN_MODE.entities if e.key == "sound" and isinstance(e, SwitchDesc)
|
||||
)
|
||||
assert desc.write_fn is not None
|
||||
result = desc.write_fn("Off", _mode_rep())
|
||||
assert result is not None
|
||||
_path, body = result
|
||||
assert body == {"x.com.samsung.da.options": ["Sound_Off"]}
|
||||
|
||||
|
||||
@@ -226,8 +272,13 @@ def test_natural_steam_write_is_single_token():
|
||||
"""NaturalSteam's slot may be absent from the live rep until first
|
||||
write -- the single-token write covers both the insert and replace case
|
||||
identically, since the device merges by prefix either way."""
|
||||
desc = next(e for e in oven.OVEN_MODE.entities if e.key == "natural_steam")
|
||||
_path, body = desc.write_fn("On", _mode_rep()) # no NaturalSteam_* in rep
|
||||
desc = next(
|
||||
e for e in oven.OVEN_MODE.entities if e.key == "natural_steam" and isinstance(e, SwitchDesc)
|
||||
)
|
||||
assert desc.write_fn is not None
|
||||
result = desc.write_fn("On", _mode_rep()) # no NaturalSteam_* in rep
|
||||
assert result is not None
|
||||
_path, body = result
|
||||
assert body == {"x.com.samsung.da.options": ["NaturalSteam_On"]}
|
||||
|
||||
|
||||
@@ -254,14 +305,26 @@ def test_cycle_active_false_when_idle():
|
||||
|
||||
|
||||
def test_cook_time_write_produces_hms():
|
||||
desc = next(e for e in oven.OVEN_OPERATIONAL_STATE.entities if e.key == "cook_time")
|
||||
path, body = desc.write_fn(90, {})
|
||||
desc = next(
|
||||
e
|
||||
for e in oven.OVEN_OPERATIONAL_STATE.entities
|
||||
if e.key == "cook_time" and isinstance(e, NumberDesc)
|
||||
)
|
||||
assert desc.write_fn is not None
|
||||
result = desc.write_fn(90, {})
|
||||
assert result is not None
|
||||
path, body = result
|
||||
assert path == ["operational", "state", "vs", "0"]
|
||||
assert body["x.com.samsung.da.operationTime"] == "01:30:00"
|
||||
assert body["x.com.samsung.da.remainingTime"] == "01:30:00"
|
||||
|
||||
|
||||
def test_cook_time_rejects_out_of_range():
|
||||
desc = next(e for e in oven.OVEN_OPERATIONAL_STATE.entities if e.key == "cook_time")
|
||||
desc = next(
|
||||
e
|
||||
for e in oven.OVEN_OPERATIONAL_STATE.entities
|
||||
if e.key == "cook_time" and isinstance(e, NumberDesc)
|
||||
)
|
||||
assert desc.write_fn is not None
|
||||
assert desc.write_fn(-1, {}) is None
|
||||
assert desc.write_fn(1440, {}) is None
|
||||
Reference in new issue
Block a user