From 9b7e189ebabcda87056ae80ce3beae6bf6ad3aea Mon Sep 17 00:00:00 2001 From: Erwin Douna Date: Fri, 9 Oct 2026 17:32:35 +0200 Subject: [PATCH] Fix TOLO availability on failed updates (#185505) --- homeassistant/components/tolo/button.py | 4 +- tests/components/tolo/conftest.py | 31 +++++++++++++++ tests/components/tolo/test_button.py | 50 +++++++++++++++++++++++++ 3 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 tests/components/tolo/conftest.py create mode 100644 tests/components/tolo/test_button.py diff --git a/homeassistant/components/tolo/button.py b/homeassistant/components/tolo/button.py index b11a774a83f4..15e29cbf2a39 100644 --- a/homeassistant/components/tolo/button.py +++ b/homeassistant/components/tolo/button.py @@ -43,11 +43,11 @@ class ToloLampNextColorButton(ToloSaunaCoordinatorEntity, ButtonEntity): @property @override - # pylint: disable-next=home-assistant-coordinator-entity-available def available(self) -> bool: """Return if entity is available.""" return ( - self.coordinator.data.status.lamp_on + super().available + and self.coordinator.data.status.lamp_on and self.coordinator.data.settings.lamp_mode == LampMode.MANUAL ) diff --git a/tests/components/tolo/conftest.py b/tests/components/tolo/conftest.py new file mode 100644 index 000000000000..db4e522b4c8b --- /dev/null +++ b/tests/components/tolo/conftest.py @@ -0,0 +1,31 @@ +"""Fixtures for the TOLO Sauna tests.""" + +from collections.abc import Generator +from unittest.mock import MagicMock, patch + +import pytest +from tololib import LampMode + +from homeassistant.components.tolo.const import DOMAIN +from homeassistant.const import CONF_HOST + +from tests.common import MockConfigEntry + + +@pytest.fixture +def mock_config_entry() -> MockConfigEntry: + """Return a TOLO Sauna config entry.""" + return MockConfigEntry(domain=DOMAIN, data={CONF_HOST: "127.0.0.1"}) + + +@pytest.fixture +def mock_tolo_client() -> Generator[MagicMock]: + """Mock the TOLO client used by the coordinator.""" + with patch( + "homeassistant.components.tolo.coordinator.ToloClient", autospec=True + ) as client_class: + client = client_class.return_value + client.get_status.return_value = MagicMock(lamp_on=True) + client.get_status.return_value.model.name = "SAUNA" + client.get_settings.return_value = MagicMock(lamp_mode=LampMode.MANUAL) + yield client diff --git a/tests/components/tolo/test_button.py b/tests/components/tolo/test_button.py new file mode 100644 index 000000000000..cb8cf339a3a6 --- /dev/null +++ b/tests/components/tolo/test_button.py @@ -0,0 +1,50 @@ +"""Tests for the TOLO Sauna buttons.""" + +from collections.abc import Generator +from datetime import timedelta +from unittest.mock import MagicMock, patch + +from freezegun.api import FrozenDateTimeFactory +import pytest + +from homeassistant.const import STATE_UNAVAILABLE, STATE_UNKNOWN, Platform +from homeassistant.core import HomeAssistant + +from tests.common import MockConfigEntry, async_fire_time_changed + +ENTITY_ID = "button.tolo_sauna_next_color" + + +@pytest.fixture(autouse=True) +def button_platform_only() -> Generator[None]: + """Only set up the button platform.""" + with patch("homeassistant.components.tolo.PLATFORMS", [Platform.BUTTON]): + yield + + +async def test_button_unavailable_on_failed_update( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + mock_tolo_client: MagicMock, + freezer: FrozenDateTimeFactory, +) -> None: + """Test the button becomes unavailable when an update fails.""" + mock_config_entry.add_to_hass(hass) + await hass.config_entries.async_setup(mock_config_entry.entry_id) + await hass.async_block_till_done() + + assert hass.states.get(ENTITY_ID).state == STATE_UNKNOWN + + mock_tolo_client.get_status.side_effect = TimeoutError + freezer.tick(timedelta(seconds=5)) + async_fire_time_changed(hass) + await hass.async_block_till_done(wait_background_tasks=True) + + assert hass.states.get(ENTITY_ID).state == STATE_UNAVAILABLE + + mock_tolo_client.get_status.side_effect = None + freezer.tick(timedelta(seconds=5)) + async_fire_time_changed(hass) + await hass.async_block_till_done(wait_background_tasks=True) + + assert hass.states.get(ENTITY_ID).state == STATE_UNKNOWN