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
@@ -7,6 +7,7 @@ from custom_components.localthings.registry.by_type import (
|
||||
)
|
||||
from custom_components.localthings.registry.capabilities import microwave
|
||||
from custom_components.localthings.registry.discovery import discover
|
||||
from custom_components.localthings.registry.entities import NumberDesc, SelectDesc, SwitchDesc
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Device-type detection + full-dump coverage
|
||||
@@ -76,16 +77,23 @@ def test_qooker_fixture_resolves_as_microwave_and_has_no_unbound_hrefs():
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
def _microwave_setpoint_desc():
|
||||
return next(e for e in microwave.MICROWAVE_SETPOINT.entities if isinstance(e, NumberDesc))
|
||||
|
||||
|
||||
def test_microwave_setpoint_write_is_read_modify_write():
|
||||
desc = microwave.MICROWAVE_SETPOINT.entities[0]
|
||||
desc = _microwave_setpoint_desc()
|
||||
rep = {"x.com.samsung.da.items": [{"x.com.samsung.da.desired": "0"}]}
|
||||
path, body = desc.write_fn(180, rep)
|
||||
assert desc.write_fn is not None
|
||||
result = desc.write_fn(180, rep)
|
||||
assert result is not None
|
||||
path, body = result
|
||||
assert path == ["temperatures", "vs", "0"]
|
||||
assert body["x.com.samsung.da.items"][0]["x.com.samsung.da.desired"] == "180"
|
||||
|
||||
|
||||
def test_microwave_setpoint_rmw_preserves_other_item_fields():
|
||||
desc = microwave.MICROWAVE_SETPOINT.entities[0]
|
||||
desc = _microwave_setpoint_desc()
|
||||
rep = {
|
||||
"x.com.samsung.da.items": [
|
||||
{
|
||||
@@ -94,28 +102,36 @@ def test_microwave_setpoint_rmw_preserves_other_item_fields():
|
||||
}
|
||||
]
|
||||
}
|
||||
_path, body = desc.write_fn(180, rep)
|
||||
assert desc.write_fn is not None
|
||||
result = desc.write_fn(180, rep)
|
||||
assert result is not None
|
||||
_path, body = result
|
||||
item = body["x.com.samsung.da.items"][0]
|
||||
assert item["x.com.samsung.da.desired"] == "180"
|
||||
assert item["x.com.samsung.da.current"] == "150"
|
||||
|
||||
|
||||
def test_microwave_setpoint_clamps_to_step():
|
||||
desc = microwave.MICROWAVE_SETPOINT.entities[0]
|
||||
desc = _microwave_setpoint_desc()
|
||||
rep = {"x.com.samsung.da.items": [{"x.com.samsung.da.desired": "0"}]}
|
||||
_, body = desc.write_fn(182, rep) # nearest 5 = 180
|
||||
assert desc.write_fn is not None
|
||||
result = desc.write_fn(182, rep) # nearest 5 = 180
|
||||
assert result is not None
|
||||
_, body = result
|
||||
assert body["x.com.samsung.da.items"][0]["x.com.samsung.da.desired"] == "180"
|
||||
|
||||
|
||||
def test_microwave_setpoint_rejects_out_of_range():
|
||||
desc = microwave.MICROWAVE_SETPOINT.entities[0]
|
||||
desc = _microwave_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(20, rep) is None # below min (40)
|
||||
assert desc.write_fn(210, rep) is None # above max (200)
|
||||
|
||||
|
||||
def test_microwave_setpoint_rejects_missing_items():
|
||||
desc = microwave.MICROWAVE_SETPOINT.entities[0]
|
||||
desc = _microwave_setpoint_desc()
|
||||
assert desc.write_fn is not None
|
||||
assert desc.write_fn(180, {}) is None
|
||||
|
||||
|
||||
@@ -123,9 +139,10 @@ def test_microwave_setpoint_exists_only_for_celsius():
|
||||
"""No Fahrenheit dump exists for this family (unlike oven.py's, verified
|
||||
against issue #44) -- the writable setpoint stays hidden rather than
|
||||
showing unverified bounds under the wrong unit."""
|
||||
desc = microwave.MICROWAVE_SETPOINT.entities[0]
|
||||
desc = _microwave_setpoint_desc()
|
||||
celsius_rep = {"x.com.samsung.da.items": [{"x.com.samsung.da.unit": "Celsius"}]}
|
||||
fahrenheit_rep = {"x.com.samsung.da.items": [{"x.com.samsung.da.unit": "Fahrenheit"}]}
|
||||
assert desc.exists_fn is not None
|
||||
assert desc.exists_fn(celsius_rep, {}) is True
|
||||
assert desc.exists_fn(fahrenheit_rep, {}) is False
|
||||
|
||||
@@ -157,8 +174,12 @@ def test_power_level_handles_missing_value():
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
def _microwave_cooking_mode_desc():
|
||||
return next(e for e in microwave.MICROWAVE_MODE.entities if isinstance(e, SelectDesc))
|
||||
|
||||
|
||||
def test_microwave_mode_options_nonempty():
|
||||
desc = microwave.MICROWAVE_MODE.entities[0]
|
||||
desc = _microwave_cooking_mode_desc()
|
||||
assert callable(desc.options)
|
||||
options = desc.options({})
|
||||
assert len(options) > 0
|
||||
@@ -171,7 +192,7 @@ def test_microwave_mode_options_reads_live_supported_modes():
|
||||
_MICROWAVE_MODES -- the live supportedModes list is used verbatim when
|
||||
present, same live-first pattern as oven._oven_mode_options, instead of
|
||||
offering users modes their own unit doesn't have."""
|
||||
desc = microwave.MICROWAVE_MODE.entities[0]
|
||||
desc = _microwave_cooking_mode_desc()
|
||||
resources = {
|
||||
"/mode/vs/0": {
|
||||
"x.com.samsung.da.supportedModes": ["NoOperation", "MicroWave", "Autocook", "KeepWarm"],
|
||||
@@ -181,14 +202,18 @@ def test_microwave_mode_options_reads_live_supported_modes():
|
||||
|
||||
|
||||
def test_microwave_mode_write_round_trips():
|
||||
desc = microwave.MICROWAVE_MODE.entities[0]
|
||||
path, body = desc.write_fn("MicroWave", {})
|
||||
desc = _microwave_cooking_mode_desc()
|
||||
assert desc.write_fn is not None
|
||||
result = desc.write_fn("MicroWave", {})
|
||||
assert result is not None
|
||||
path, body = result
|
||||
assert path == ["mode", "vs", "0"]
|
||||
assert body["x.com.samsung.da.modes"] == ["MicroWave"]
|
||||
|
||||
|
||||
def test_microwave_mode_rejects_unknown():
|
||||
desc = microwave.MICROWAVE_MODE.entities[0]
|
||||
desc = _microwave_cooking_mode_desc()
|
||||
assert desc.write_fn is not None
|
||||
assert desc.write_fn("SpaghettiMode", {}) is None
|
||||
|
||||
|
||||
@@ -197,9 +222,12 @@ def test_microwave_mode_write_validates_against_live_supported_modes():
|
||||
list, not the static union-of-all-dumps fallback -- 'AirFryer' is a
|
||||
valid _MICROWAVE_MODES entry but must still be rejected for a unit
|
||||
whose own supportedModes doesn't include it."""
|
||||
desc = microwave.MICROWAVE_MODE.entities[0]
|
||||
desc = _microwave_cooking_mode_desc()
|
||||
rep = {"x.com.samsung.da.supportedModes": ["NoOperation", "MicroWave", "Autocook", "KeepWarm"]}
|
||||
path, body = desc.write_fn("MicroWave", rep)
|
||||
assert desc.write_fn is not None
|
||||
result = desc.write_fn("MicroWave", rep)
|
||||
assert result is not None
|
||||
path, body = result
|
||||
assert path == ["mode", "vs", "0"]
|
||||
assert body["x.com.samsung.da.modes"] == ["MicroWave"]
|
||||
assert desc.write_fn("AirFryer", rep) is None
|
||||
@@ -211,9 +239,16 @@ def test_microwave_mode_write_validates_against_live_supported_modes():
|
||||
|
||||
|
||||
def test_sound_write_is_single_token():
|
||||
desc = next(e for e in microwave.MICROWAVE_MODE.entities if e.key == "sound")
|
||||
desc = next(
|
||||
e
|
||||
for e in microwave.MICROWAVE_MODE.entities
|
||||
if e.key == "sound" and isinstance(e, SwitchDesc)
|
||||
)
|
||||
rep = {"x.com.samsung.da.options": ["Sound_On"]}
|
||||
path, body = desc.write_fn("Off", rep)
|
||||
assert desc.write_fn is not None
|
||||
result = desc.write_fn("Off", rep)
|
||||
assert result is not None
|
||||
path, body = result
|
||||
assert path == ["mode", "vs", "0"]
|
||||
assert body == {"x.com.samsung.da.options": ["Sound_Off"]}
|
||||
|
||||
@@ -221,30 +256,52 @@ def test_sound_write_is_single_token():
|
||||
def test_lamp_gated_absent_when_no_lamp_option():
|
||||
"""Issue #121's combi dump has no 'Lamp_*' token at all -- unlike
|
||||
oven.py's lamp switch (assumed universal), this one self-gates off."""
|
||||
desc = next(e for e in microwave.MICROWAVE_MODE.entities if e.key == "lamp")
|
||||
desc = next(
|
||||
e
|
||||
for e in microwave.MICROWAVE_MODE.entities
|
||||
if e.key == "lamp" and isinstance(e, SwitchDesc)
|
||||
)
|
||||
rep = {"x.com.samsung.da.options": ["DeviceType_MW7300B-/EU1", "Sound_Off"]}
|
||||
assert desc.exists_fn is not None
|
||||
assert desc.exists_fn(rep, {}) is False
|
||||
|
||||
|
||||
def test_lamp_gated_present_when_lamp_option_reported():
|
||||
"""Issue #137's plain microwave reports 'Lamp_Off'."""
|
||||
desc = next(e for e in microwave.MICROWAVE_MODE.entities if e.key == "lamp")
|
||||
desc = next(
|
||||
e
|
||||
for e in microwave.MICROWAVE_MODE.entities
|
||||
if e.key == "lamp" and isinstance(e, SwitchDesc)
|
||||
)
|
||||
rep = {"x.com.samsung.da.options": ["Lamp_Off", "Sound_On"]}
|
||||
assert desc.exists_fn is not None
|
||||
assert desc.exists_fn(rep, {}) is True
|
||||
|
||||
|
||||
def test_lamp_write_is_single_token():
|
||||
"""issue #152: the device has never been observed accepting 'On' --
|
||||
only 'High'/'Off' -- so the switch's "on" write uses 'High'."""
|
||||
desc = next(e for e in microwave.MICROWAVE_MODE.entities if e.key == "lamp")
|
||||
desc = next(
|
||||
e
|
||||
for e in microwave.MICROWAVE_MODE.entities
|
||||
if e.key == "lamp" and isinstance(e, SwitchDesc)
|
||||
)
|
||||
rep = {"x.com.samsung.da.options": ["Lamp_Off"]}
|
||||
path, body = desc.write_fn("On", rep)
|
||||
assert desc.write_fn is not None
|
||||
result = desc.write_fn("On", rep)
|
||||
assert result is not None
|
||||
path, body = result
|
||||
assert path == ["mode", "vs", "0"]
|
||||
assert body == {"x.com.samsung.da.options": ["Lamp_High"]}
|
||||
|
||||
|
||||
def test_lamp_write_requires_existing_options():
|
||||
desc = next(e for e in microwave.MICROWAVE_MODE.entities if e.key == "lamp")
|
||||
desc = next(
|
||||
e
|
||||
for e in microwave.MICROWAVE_MODE.entities
|
||||
if e.key == "lamp" and isinstance(e, SwitchDesc)
|
||||
)
|
||||
assert desc.write_fn is not None
|
||||
assert desc.write_fn("On", {}) is None
|
||||
|
||||
|
||||
@@ -267,14 +324,24 @@ def test_lamp_reads_any_non_off_level_as_true():
|
||||
|
||||
|
||||
def test_filter_remind_gated_absent_when_no_option():
|
||||
desc = next(e for e in microwave.MICROWAVE_MODE.entities if e.key == "filter_remind")
|
||||
desc = next(
|
||||
e
|
||||
for e in microwave.MICROWAVE_MODE.entities
|
||||
if e.key == "filter_remind" and isinstance(e, SwitchDesc)
|
||||
)
|
||||
rep = {"x.com.samsung.da.options": ["DeviceType_MW7300B-/EU1", "Sound_Off"]}
|
||||
assert desc.exists_fn is not None
|
||||
assert desc.exists_fn(rep, {}) is False
|
||||
|
||||
|
||||
def test_filter_remind_gated_present_when_option_reported():
|
||||
desc = next(e for e in microwave.MICROWAVE_MODE.entities if e.key == "filter_remind")
|
||||
desc = next(
|
||||
e
|
||||
for e in microwave.MICROWAVE_MODE.entities
|
||||
if e.key == "filter_remind" and isinstance(e, SwitchDesc)
|
||||
)
|
||||
rep = {"x.com.samsung.da.options": ["FilterRemind_Off"]}
|
||||
assert desc.exists_fn is not None
|
||||
assert desc.exists_fn(rep, {}) is True
|
||||
|
||||
|
||||
@@ -285,27 +352,49 @@ def test_filter_remind_reads_on_off():
|
||||
|
||||
|
||||
def test_filter_remind_write_is_single_token():
|
||||
desc = next(e for e in microwave.MICROWAVE_MODE.entities if e.key == "filter_remind")
|
||||
desc = next(
|
||||
e
|
||||
for e in microwave.MICROWAVE_MODE.entities
|
||||
if e.key == "filter_remind" and isinstance(e, SwitchDesc)
|
||||
)
|
||||
rep = {"x.com.samsung.da.options": ["FilterRemind_Off"]}
|
||||
path, body = desc.write_fn("On", rep)
|
||||
assert desc.write_fn is not None
|
||||
result = desc.write_fn("On", rep)
|
||||
assert result is not None
|
||||
path, body = result
|
||||
assert path == ["mode", "vs", "0"]
|
||||
assert body == {"x.com.samsung.da.options": ["FilterRemind_On"]}
|
||||
|
||||
|
||||
def test_filter_remind_write_requires_existing_options():
|
||||
desc = next(e for e in microwave.MICROWAVE_MODE.entities if e.key == "filter_remind")
|
||||
desc = next(
|
||||
e
|
||||
for e in microwave.MICROWAVE_MODE.entities
|
||||
if e.key == "filter_remind" and isinstance(e, SwitchDesc)
|
||||
)
|
||||
assert desc.write_fn is not None
|
||||
assert desc.write_fn("On", {}) is None
|
||||
|
||||
|
||||
def test_remind_beep_gated_absent_when_no_option():
|
||||
desc = next(e for e in microwave.MICROWAVE_MODE.entities if e.key == "remind_beep")
|
||||
desc = next(
|
||||
e
|
||||
for e in microwave.MICROWAVE_MODE.entities
|
||||
if e.key == "remind_beep" and isinstance(e, SwitchDesc)
|
||||
)
|
||||
rep = {"x.com.samsung.da.options": ["DeviceType_MW7300B-/EU1", "Sound_Off"]}
|
||||
assert desc.exists_fn is not None
|
||||
assert desc.exists_fn(rep, {}) is False
|
||||
|
||||
|
||||
def test_remind_beep_gated_present_when_option_reported():
|
||||
desc = next(e for e in microwave.MICROWAVE_MODE.entities if e.key == "remind_beep")
|
||||
desc = next(
|
||||
e
|
||||
for e in microwave.MICROWAVE_MODE.entities
|
||||
if e.key == "remind_beep" and isinstance(e, SwitchDesc)
|
||||
)
|
||||
rep = {"x.com.samsung.da.options": ["RemindBeep_On"]}
|
||||
assert desc.exists_fn is not None
|
||||
assert desc.exists_fn(rep, {}) is True
|
||||
|
||||
|
||||
@@ -316,13 +405,25 @@ def test_remind_beep_reads_on_off():
|
||||
|
||||
|
||||
def test_remind_beep_write_is_single_token():
|
||||
desc = next(e for e in microwave.MICROWAVE_MODE.entities if e.key == "remind_beep")
|
||||
desc = next(
|
||||
e
|
||||
for e in microwave.MICROWAVE_MODE.entities
|
||||
if e.key == "remind_beep" and isinstance(e, SwitchDesc)
|
||||
)
|
||||
rep = {"x.com.samsung.da.options": ["RemindBeep_On"]}
|
||||
path, body = desc.write_fn("Off", rep)
|
||||
assert desc.write_fn is not None
|
||||
result = desc.write_fn("Off", rep)
|
||||
assert result is not None
|
||||
path, body = result
|
||||
assert path == ["mode", "vs", "0"]
|
||||
assert body == {"x.com.samsung.da.options": ["RemindBeep_Off"]}
|
||||
|
||||
|
||||
def test_remind_beep_write_requires_existing_options():
|
||||
desc = next(e for e in microwave.MICROWAVE_MODE.entities if e.key == "remind_beep")
|
||||
desc = next(
|
||||
e
|
||||
for e in microwave.MICROWAVE_MODE.entities
|
||||
if e.key == "remind_beep" and isinstance(e, SwitchDesc)
|
||||
)
|
||||
assert desc.write_fn is not None
|
||||
assert desc.write_fn("On", {}) is None
|
||||
Reference in new issue
Block a user