mirror of
https://github.com/home-assistant/core.git
synced 2026-12-02 19:57:03 -04:00
Fix TOLO availability on failed updates (#185505)
This commit is contained in:
@@ -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
|
||||
)
|
||||
|
||||
|
||||
@@ -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
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user