Fix pre-existing ty diagnostics in a second batch of test files
Same isinstance/cast narrowing and Optional-field assert pattern as the prior commits, covering the airconditioner, fridge, washer, operational, subdevices, sensor_hysteresis, laundry, select_options, identity and entities test files.
This commit is contained in:
17 files changed
+322
-100
No files matched your search
@@ -2,6 +2,7 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Iterable, cast
|
||||
from unittest.mock import patch
|
||||
|
||||
from homeassistant.core import HomeAssistant
|
||||
@@ -32,8 +33,10 @@ async def test_form_first_device(hass: HomeAssistant) -> None:
|
||||
result = await hass.config_entries.flow.async_init(DOMAIN, context={"source": "user"})
|
||||
assert result["type"] == FlowResultType.FORM
|
||||
assert result["step_id"] == "user"
|
||||
assert CONF_CA_CERT_PEM in result["data_schema"].schema
|
||||
assert CONF_CA_KEY_PEM in result["data_schema"].schema
|
||||
data_schema = result["data_schema"]
|
||||
assert data_schema is not None
|
||||
assert CONF_CA_CERT_PEM in data_schema.schema
|
||||
assert CONF_CA_KEY_PEM in data_schema.schema
|
||||
|
||||
|
||||
async def test_form_second_device_reuses_creds(hass: HomeAssistant) -> None:
|
||||
@@ -44,8 +47,10 @@ async def test_form_second_device_reuses_creds(hass: HomeAssistant) -> None:
|
||||
result = await hass.config_entries.flow.async_init(DOMAIN, context={"source": "user"})
|
||||
assert result["type"] == FlowResultType.FORM
|
||||
assert result["step_id"] == "user_reuse"
|
||||
assert CONF_CA_CERT_PEM not in result["data_schema"].schema
|
||||
assert CONF_CA_KEY_PEM not in result["data_schema"].schema
|
||||
data_schema = result["data_schema"]
|
||||
assert data_schema is not None
|
||||
assert CONF_CA_CERT_PEM not in data_schema.schema
|
||||
assert CONF_CA_KEY_PEM not in data_schema.schema
|
||||
|
||||
|
||||
async def test_successful_setup(hass: HomeAssistant, mock_probe) -> None:
|
||||
@@ -243,7 +248,9 @@ async def test_cannot_connect(hass: HomeAssistant) -> None:
|
||||
},
|
||||
)
|
||||
assert result["type"] == FlowResultType.FORM
|
||||
assert result["errors"]["base"] == "cannot_connect"
|
||||
errors = result["errors"]
|
||||
assert errors is not None
|
||||
assert errors["base"] == "cannot_connect"
|
||||
|
||||
|
||||
async def test_recognized_type_skips_confirmation_step(hass: HomeAssistant, mock_probe) -> None:
|
||||
@@ -368,7 +375,7 @@ async def test_options_flow_init_shows_menu(hass: HomeAssistant) -> None:
|
||||
|
||||
assert result["type"] == FlowResultType.MENU
|
||||
assert result["step_id"] == "init"
|
||||
assert set(result["menu_options"]) == {"settings", "debug_write"}
|
||||
assert set(cast(Iterable[str], result["menu_options"])) == {"settings", "debug_write"}
|
||||
|
||||
|
||||
async def test_options_flow_default_is_off(hass: HomeAssistant) -> None:
|
||||
@@ -384,7 +391,9 @@ async def test_options_flow_default_is_off(hass: HomeAssistant) -> None:
|
||||
|
||||
assert result["type"] == FlowResultType.FORM
|
||||
assert result["step_id"] == "settings"
|
||||
assert result["data_schema"]({})[CONF_BYPASS_REMOTE_CONTROL] is False
|
||||
data_schema = result["data_schema"]
|
||||
assert data_schema is not None
|
||||
assert data_schema({})[CONF_BYPASS_REMOTE_CONTROL] is False
|
||||
|
||||
|
||||
async def test_options_flow_can_enable_bypass(hass: HomeAssistant) -> None:
|
||||
@@ -421,7 +430,9 @@ async def test_options_flow_reflects_previously_saved_value(hass: HomeAssistant)
|
||||
result["flow_id"], user_input={"next_step_id": "settings"}
|
||||
)
|
||||
|
||||
assert result["data_schema"]({})[CONF_BYPASS_REMOTE_CONTROL] is True
|
||||
data_schema = result["data_schema"]
|
||||
assert data_schema is not None
|
||||
assert data_schema({})[CONF_BYPASS_REMOTE_CONTROL] is True
|
||||
|
||||
|
||||
async def test_options_flow_debug_write_shows_hrefs_from_coordinator(
|
||||
@@ -495,7 +506,9 @@ async def test_options_flow_debug_edit_writes_and_shows_result(
|
||||
|
||||
assert result["type"] == FlowResultType.MENU
|
||||
assert result["step_id"] == "debug_result"
|
||||
assert result["description_placeholders"]["code"] == "2.04 (0x44)"
|
||||
description_placeholders = result["description_placeholders"]
|
||||
assert description_placeholders is not None
|
||||
assert description_placeholders["code"] == "2.04 (0x44)"
|
||||
|
||||
|
||||
async def test_options_flow_debug_edit_rejects_empty_payload(
|
||||
|
||||
@@ -291,7 +291,9 @@ def test_logger_is_scoped_to_device_host(hass: HomeAssistant, mock_entry) -> Non
|
||||
makes multi-device logs ambiguous."""
|
||||
coordinator = LocalThingsCoordinator(hass, mock_entry)
|
||||
|
||||
assert coordinator._log.name.endswith(ENTRY_DATA[CONF_HOST])
|
||||
host = ENTRY_DATA[CONF_HOST]
|
||||
assert isinstance(host, str)
|
||||
assert coordinator._log.name.endswith(host)
|
||||
assert coordinator.logger is coordinator._log
|
||||
assert coordinator._observe.log is coordinator._log
|
||||
|
||||
@@ -393,7 +395,9 @@ async def test_reconnect_while_observe_mode_downgrades_to_poll(
|
||||
# still alive (see test_poll_failure_skips_reconnect_when_push_is_healthy)
|
||||
# and would not trigger a reconnect at all. This test covers a
|
||||
# genuinely dead channel: no recent push, poll fails, reconnect fires.
|
||||
coordinator._observe._last_notify_ts -= PUSH_HEALTH_WINDOW_S + 1
|
||||
last_notify_ts = coordinator._observe._last_notify_ts
|
||||
assert last_notify_ts is not None
|
||||
coordinator._observe._last_notify_ts = last_notify_ts - (PUSH_HEALTH_WINDOW_S + 1)
|
||||
|
||||
# Simulate the existing "poll failed, reconnecting" branch: _poll_once
|
||||
# fails once (triggering the reconnect/backoff path), then succeeds.
|
||||
@@ -490,7 +494,9 @@ async def test_poll_timeout_reconnects_after_consecutive_limit_even_with_push(
|
||||
|
||||
# No notify is recent any more, so every timeout below counts toward
|
||||
# the consecutive-timeout limit instead of being deferred.
|
||||
coordinator._observe._last_notify_ts -= PUSH_HEALTH_WINDOW_S + 1
|
||||
last_notify_ts = coordinator._observe._last_notify_ts
|
||||
assert last_notify_ts is not None
|
||||
coordinator._observe._last_notify_ts = last_notify_ts - (PUSH_HEALTH_WINDOW_S + 1)
|
||||
|
||||
# Call _async_update_data directly rather than via
|
||||
# async_request_refresh() — the coordinator's built-in debouncer
|
||||
@@ -553,7 +559,9 @@ async def test_poll_timeout_counter_resets_when_push_is_healthy_again(
|
||||
assert coordinator.observe_mode == MODE_OBSERVE
|
||||
|
||||
# Age the notify out so timeouts accrue toward the limit.
|
||||
coordinator._observe._last_notify_ts -= PUSH_HEALTH_WINDOW_S + 1
|
||||
last_notify_ts = coordinator._observe._last_notify_ts
|
||||
assert last_notify_ts is not None
|
||||
coordinator._observe._last_notify_ts = last_notify_ts - (PUSH_HEALTH_WINDOW_S + 1)
|
||||
with patch(
|
||||
"custom_components.localthings.coordinator.LocalThingsCoordinator._poll_once",
|
||||
side_effect=TimeoutError("GET /device/0 block 11 timeout"),
|
||||
@@ -605,7 +613,9 @@ async def test_reconnect_from_observe_mode_resubscribes_immediately(
|
||||
# failure below actually triggers a reconnect (see
|
||||
# test_poll_failure_skips_reconnect_when_push_is_healthy for the
|
||||
# healthy-push case, which now skips reconnecting entirely).
|
||||
coordinator._observe._last_notify_ts -= PUSH_HEALTH_WINDOW_S + 1
|
||||
last_notify_ts = coordinator._observe._last_notify_ts
|
||||
assert last_notify_ts is not None
|
||||
coordinator._observe._last_notify_ts = last_notify_ts - (PUSH_HEALTH_WINDOW_S + 1)
|
||||
|
||||
# If a stale retry timer (rather than an immediate resubscribe) were
|
||||
# driving recovery, mode would still be 'poll' right after this single
|
||||
@@ -734,7 +744,7 @@ async def test_write_marks_href_pending_before_post(
|
||||
await hass.async_block_till_done()
|
||||
coordinator: LocalThingsCoordinator = hass.data[DOMAIN][mock_entry.entry_id]
|
||||
|
||||
def _write_fn(payload, rep, href):
|
||||
def _write_fn(payload, rep, href=None):
|
||||
return (["some", "path"], {"value": payload})
|
||||
|
||||
desc = NumberDesc(key="test", field="value", write_fn=_write_fn)
|
||||
@@ -771,7 +781,7 @@ async def test_send_command_applies_write_optimistically_before_settling(
|
||||
await hass.async_block_till_done()
|
||||
coordinator: LocalThingsCoordinator = hass.data[DOMAIN][mock_entry.entry_id]
|
||||
|
||||
def _write_fn(payload, rep, href):
|
||||
def _write_fn(payload, rep, href=None):
|
||||
return (["some", "path"], {"value": payload})
|
||||
|
||||
desc = NumberDesc(key="test", field="value", write_fn=_write_fn)
|
||||
@@ -812,8 +822,10 @@ async def test_climate_power_write_applies_to_its_own_href_not_bound_href(
|
||||
await hass.async_block_till_done()
|
||||
coordinator: LocalThingsCoordinator = hass.data[DOMAIN][mock_entry.entry_id]
|
||||
|
||||
climate_href = airconditioner.CLIMATE.href
|
||||
assert climate_href is not None
|
||||
bound = BoundEntity(
|
||||
href=airconditioner.CLIMATE.href,
|
||||
href=climate_href,
|
||||
capability=coordinator.bound[0].capability,
|
||||
desc=airconditioner.CLIMATE.entities[0],
|
||||
)
|
||||
@@ -823,9 +835,7 @@ async def test_climate_power_write_applies_to_its_own_href_not_bound_href(
|
||||
await coordinator.async_send_command(bound, ("power", True))
|
||||
|
||||
assert (coordinator._cache.get("/power/vs/0") or {}).get("x.com.samsung.da.power") == "On"
|
||||
assert "x.com.samsung.da.power" not in (
|
||||
coordinator._cache.get(airconditioner.CLIMATE.href) or {}
|
||||
)
|
||||
assert "x.com.samsung.da.power" not in (coordinator._cache.get(climate_href) or {})
|
||||
assert coordinator._observe._settle_until.get("/power/vs/0") is not None
|
||||
|
||||
|
||||
@@ -858,7 +868,7 @@ async def test_send_command_survives_stale_confirm_poll(
|
||||
await hass.async_block_till_done()
|
||||
coordinator: LocalThingsCoordinator = hass.data[DOMAIN][mock_entry.entry_id]
|
||||
|
||||
def _write_fn(payload, rep, href):
|
||||
def _write_fn(payload, rep, href=None):
|
||||
return (["test", "vs", "0"], {"value": payload})
|
||||
|
||||
desc = NumberDesc(key="test", field="value", write_fn=_write_fn)
|
||||
@@ -917,12 +927,12 @@ async def test_second_write_to_same_href_lands_during_first_writes_settle_window
|
||||
desc_a = NumberDesc(
|
||||
key="cycle",
|
||||
field="cycle",
|
||||
write_fn=lambda p, rep, href: (["test", "vs", "0"], {"cycle": p}),
|
||||
write_fn=lambda p, rep, href=None: (["test", "vs", "0"], {"cycle": p}),
|
||||
)
|
||||
desc_b = NumberDesc(
|
||||
key="detergent",
|
||||
field="detergent",
|
||||
write_fn=lambda p, rep, href: (["test", "vs", "0"], {"detergent": p}),
|
||||
write_fn=lambda p, rep, href=None: (["test", "vs", "0"], {"detergent": p}),
|
||||
)
|
||||
bound_a = BoundEntity(
|
||||
href="/test/vs/0", capability=coordinator.bound[0].capability, desc=desc_a
|
||||
@@ -1023,7 +1033,7 @@ async def test_send_command_blocked_when_remote_control_disabled(
|
||||
source="test",
|
||||
)
|
||||
|
||||
def _write_fn(payload, rep, href):
|
||||
def _write_fn(payload, rep, href=None):
|
||||
return (["some", "path"], {"value": payload})
|
||||
|
||||
desc = NumberDesc(key="test", field="value", write_fn=_write_fn)
|
||||
@@ -1064,7 +1074,7 @@ async def test_send_command_allowed_when_remote_control_enabled(
|
||||
source="test",
|
||||
)
|
||||
|
||||
def _write_fn(payload, rep, href):
|
||||
def _write_fn(payload, rep, href=None):
|
||||
return (["some", "path"], {"value": payload})
|
||||
|
||||
desc = NumberDesc(key="test", field="value", write_fn=_write_fn)
|
||||
@@ -1139,7 +1149,7 @@ async def test_send_command_bypasses_remote_control_when_option_enabled(
|
||||
source="test",
|
||||
)
|
||||
|
||||
def _write_fn(payload, rep, href):
|
||||
def _write_fn(payload, rep, href=None):
|
||||
return (["some", "path"], {"value": payload})
|
||||
|
||||
desc = NumberDesc(key="test", field="value", write_fn=_write_fn)
|
||||
|
||||
@@ -53,6 +53,7 @@ def test_apply_merges_partial_update_onto_prior_rep():
|
||||
mgr.apply("/mode/vs/0", partial, source="observe")
|
||||
|
||||
cached = mgr.cache.get("/mode/vs/0")
|
||||
assert cached is not None
|
||||
assert cached["x.com.samsung.da.modes"] == ["CVN_CONVERTIBLE_ZONE", "WATERFILTER_ENABLE"]
|
||||
assert cached["x.com.samsung.da.supportedOptions"] == ["CV_FDR_WINE", "CV_FDR_MEAT"]
|
||||
|
||||
@@ -435,6 +436,7 @@ def test_close_stops_the_refresh_thread():
|
||||
session = _FakeSession()
|
||||
mgr.start_refresh_task(session)
|
||||
thread = mgr._refresh_thread
|
||||
assert thread is not None
|
||||
|
||||
mgr.close()
|
||||
thread.join(timeout=2.0)
|
||||
@@ -449,6 +451,7 @@ def test_downgrade_to_poll_stops_refresh_task():
|
||||
session = _FakeSession()
|
||||
mgr.start_refresh_task(session)
|
||||
thread = mgr._refresh_thread
|
||||
assert thread is not None
|
||||
|
||||
mgr.downgrade_to_poll()
|
||||
thread.join(timeout=2.0)
|
||||
|
||||
Reference in new issue
Block a user