Tolerate missed LIFX polls before marking a light unavailable (#189538)

Signed-off-by: Avi Miller <me@dje.li>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Avi Miller
2026-10-30 14:25:54 +01:00
committed by GitHub
co-authored by Claude Opus 5.5
parent 81a4a24234
commit 1e1a563102
4 changed files with 125 additions and 6 deletions
+3
View File
@@ -57,6 +57,9 @@ SERVICE_SET_STATE = "set_state"
HEV_CYCLE_STATE = "hev_cycle_state"
LIFX_IDENTIFY_DELAY = 3.0
# A bulb often misses one poll and answers the next, so it is only shown as
# unavailable once it has missed this many in a row
MAX_MISSED_POLLS = 3
INFRARED_BRIGHTNESS = "infrared_brightness"
INFRARED_LEVELS = {
"Disabled": 0.0,
+30 -1
View File
@@ -34,7 +34,13 @@ from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.debounce import Debouncer
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
from .const import DOMAIN, INFRARED_LEVELS, LIFX_IDENTIFY_DELAY, LOGGER
from .const import (
DOMAIN,
INFRARED_LEVELS,
LIFX_IDENTIFY_DELAY,
LOGGER,
MAX_MISSED_POLLS,
)
from .util import device_error
type LIFXState = (
@@ -59,6 +65,7 @@ class LIFXUpdateCoordinator(DataUpdateCoordinator[LIFXState]):
"""Initialize the coordinator."""
self.device = device
self.last_used_theme: str | None = None
self._missed_polls = 0
super().__init__(
hass,
LOGGER,
@@ -82,15 +89,37 @@ class LIFXUpdateCoordinator(DataUpdateCoordinator[LIFXState]):
try:
await self.device.refresh_state()
except LifxError as err:
self._missed_polls += 1
# Before the first read there is no last state to keep showing, and
# returning it would bring back a light that is already unavailable
if (
self.data is not None
and self.last_update_success
and self._missed_polls < MAX_MISSED_POLLS
):
LOGGER.debug(
"%s missed %s of %s polls: %s",
self.name,
self._missed_polls,
MAX_MISSED_POLLS,
err,
)
return self.data
raise UpdateFailed(
translation_domain=DOMAIN,
translation_key="update_failed",
translation_placeholders={"error": str(err)},
) from err
self._missed_polls = 0
state = self.device.state
assert state is not None
return cast(LIFXState, state)
@property
def last_read_succeeded(self) -> bool:
"""Return whether the device answered the latest read of its state."""
return self._missed_polls == 0
@property
def serial_number(self) -> str:
"""Return the raw LIFX serial that identifies the device."""
+4 -1
View File
@@ -98,7 +98,10 @@ class LIFXEntity(CoordinatorEntity[LIFXUpdateCoordinator]):
async def async_refresh_before_merge(self) -> None:
"""Read what a partial write is merged over, or refuse to write it."""
await self.coordinator.async_refresh()
if not self.coordinator.last_update_success:
if not (
self.coordinator.last_update_success
and self.coordinator.last_read_succeeded
):
# The coordinator keeps the state it last read, which is what the
# write would be merged over and would then be written back
raise HomeAssistantError(
+88 -4
View File
@@ -1585,6 +1585,13 @@ async def test_matrix_color_preserves_tile_brightness(
)
@pytest.mark.parametrize(
"error",
[
pytest.param(LifxError("device unreachable"), id="library-error"),
pytest.param(OSError("network down"), id="unexpected-error"),
],
)
@pytest.mark.parametrize(
("factory", "method"),
[
@@ -1595,13 +1602,16 @@ async def test_matrix_color_preserves_tile_brightness(
],
)
async def test_partial_write_is_refused_when_the_state_cannot_be_read(
hass: HomeAssistant, factory: Callable[[], MockDevice], method: str
hass: HomeAssistant,
factory: Callable[[], MockDevice],
method: str,
error: Exception,
) -> None:
"""Test a partial write is not merged over the state of a failed read."""
device = factory()
device.state.power = 65535
await async_setup_lifx_entry(hass, device)
device.refresh_state.side_effect = LifxError("device unreachable")
device.refresh_state.side_effect = error
with pytest.raises(HomeAssistantError, match="could not be read"):
await hass.services.async_call(
@@ -1939,18 +1949,92 @@ async def test_library_errors_become_home_assistant_errors(
)
async def test_failed_refresh_marks_light_unavailable(hass: HomeAssistant) -> None:
"""Test coordinator availability remains visible on the light entity."""
@pytest.mark.parametrize(
("missed_polls", "expected_state"),
[
pytest.param(1, STATE_ON, id="one_missed_poll"),
pytest.param(2, STATE_ON, id="two_missed_polls"),
pytest.param(3, STATE_UNAVAILABLE, id="three_missed_polls"),
],
)
async def test_light_is_unavailable_after_three_missed_polls(
hass: HomeAssistant, missed_polls: int, expected_state: str
) -> None:
"""Test a light keeps its last state until it misses three polls in a row."""
device = create_mock_light()
device.state.power = 65535
await async_setup_lifx_entry(hass, device)
device.refresh_state.side_effect = LifxError("offline")
for _ in range(missed_polls):
await async_trigger_update(hass)
state = hass.states.get(ENTITY_ID)
assert state
assert state.state == expected_state
async def test_answered_poll_resets_missed_polls(hass: HomeAssistant) -> None:
"""Test the missed polls are counted again after the light answers."""
device = create_mock_light()
device.state.power = 65535
await async_setup_lifx_entry(hass, device)
device.refresh_state.side_effect = LifxError("offline")
await async_trigger_update(hass)
await async_trigger_update(hass)
device.refresh_state.side_effect = None
await async_trigger_update(hass)
device.refresh_state.side_effect = LifxError("offline")
await async_trigger_update(hass)
await async_trigger_update(hass)
state = hass.states.get(ENTITY_ID)
assert state
assert state.state == STATE_ON
async def test_light_recovers_after_being_unavailable(hass: HomeAssistant) -> None:
"""Test a light is available again as soon as it answers a poll."""
device = create_mock_light()
device.state.power = 65535
await async_setup_lifx_entry(hass, device)
device.refresh_state.side_effect = LifxError("offline")
for _ in range(3):
await async_trigger_update(hass)
device.refresh_state.side_effect = None
await async_trigger_update(hass)
state = hass.states.get(ENTITY_ID)
assert state
assert state.state == STATE_ON
async def test_unavailable_light_only_recovers_by_answering(
hass: HomeAssistant,
) -> None:
"""Test a missed poll does not bring back a light that went unavailable."""
device = create_mock_light()
device.state.power = 65535
await async_setup_lifx_entry(hass, device)
device.refresh_state.side_effect = OSError("network down")
await async_trigger_update(hass)
device.refresh_state.side_effect = LifxError("offline")
await async_trigger_update(hass)
state = hass.states.get(ENTITY_ID)
assert state
assert state.state == STATE_UNAVAILABLE
device.refresh_state.side_effect = None
await async_trigger_update(hass)
state = hass.states.get(ENTITY_ID)
assert state
assert state.state == STATE_ON
@pytest.mark.parametrize(
"data",