Handle a Netatmo home without a selected schedule (#187314)

This commit is contained in:
Tobias Sauerwein
2026-10-20 07:44:54 +02:00
committed by GitHub
parent 4fae6b2b3d
commit 05d97f14a3
2 changed files with 87 additions and 16 deletions
+11 -6
View File
@@ -62,13 +62,20 @@ class NetatmoScheduleSelect(NetatmoBaseEntity, SelectEntity):
# Legacy format, kept as migrating existing unique IDs is not worth the risk
self._attr_unique_id = f"{self.home.entity_id}-schedule-select" # pylint: disable=home-assistant-entity-unique-id-redundant-platform
schedule = self.home.get_selected_schedule()
assert schedule
self._attr_current_option = schedule.name
self._attr_current_option = self._selected_schedule_name()
self._attr_options = [
schedule.name for schedule in self.home.schedules.values() if schedule.name
]
def _selected_schedule_name(self) -> str | None:
"""Return the name of the schedule selected for the current mode.
None when no schedule of the home's temperature control mode is selected,
such as after switching to cooling without a cooling schedule.
"""
schedule = self.home.get_selected_schedule()
return schedule.name if schedule else None
@override
async def async_added_to_hass(self) -> None:
"""Entity created."""
@@ -116,9 +123,7 @@ class NetatmoScheduleSelect(NetatmoBaseEntity, SelectEntity):
@override
def async_update_callback(self) -> None:
"""Update the entity's state."""
schedule = self.home.get_selected_schedule()
assert schedule
self._attr_current_option = schedule.name
self._attr_current_option = self._selected_schedule_name()
self.data_handler.schedules[self.home.entity_id] = self.home.schedules
self._attr_options = [
schedule.name for schedule in self.home.schedules.values() if schedule.name
+76 -10
View File
@@ -9,6 +9,7 @@ import pytest
from pytest_homeassistant.common import MockConfigEntry
from syrupy.assertion import SnapshotAssertion
from homeassistant.components.netatmo.coordinator import HOME
from homeassistant.components.select import (
ATTR_OPTION,
ATTR_OPTIONS,
@@ -18,6 +19,7 @@ from homeassistant.const import (
ATTR_ENTITY_ID,
CONF_WEBHOOK_ID,
SERVICE_SELECT_OPTION,
STATE_UNKNOWN,
Platform,
)
from homeassistant.core import HomeAssistant
@@ -31,6 +33,8 @@ from .common import (
snapshot_platform_entities,
)
SELECT_ENTITY = "select.myhome_schedule"
async def test_entity(
hass: HomeAssistant,
@@ -62,9 +66,8 @@ async def test_select_schedule_thermostats(
await hass.async_block_till_done()
webhook_id = config_entry.data[CONF_WEBHOOK_ID]
select_entity = "select.myhome_schedule"
assert hass.states.get(select_entity).state == "Default"
assert hass.states.get(SELECT_ENTITY).state == "Default"
# Fake backend response changing schedule
response = {
@@ -76,8 +79,8 @@ async def test_select_schedule_thermostats(
await simulate_webhook(hass, webhook_id, response)
await hass.async_block_till_done()
assert hass.states.get(select_entity).state == "Winter"
assert hass.states.get(select_entity).attributes[ATTR_OPTIONS] == [
assert hass.states.get(SELECT_ENTITY).state == "Winter"
assert hass.states.get(SELECT_ENTITY).attributes[ATTR_OPTIONS] == [
"Default",
"Winter",
]
@@ -88,7 +91,7 @@ async def test_select_schedule_thermostats(
SELECT_DOMAIN,
SERVICE_SELECT_OPTION,
{
ATTR_ENTITY_ID: select_entity,
ATTR_ENTITY_ID: SELECT_ENTITY,
ATTR_OPTION: "Default",
},
blocking=True,
@@ -107,7 +110,7 @@ async def test_select_schedule_thermostats(
}
await simulate_webhook(hass, webhook_id, response)
assert hass.states.get(select_entity).state == "Default"
assert hass.states.get(SELECT_ENTITY).state == "Default"
async def test_select_schedule_unknown_schedule_id(
@@ -121,8 +124,7 @@ async def test_select_schedule_unknown_schedule_id(
await hass.async_block_till_done()
webhook_id = config_entry.data[CONF_WEBHOOK_ID]
select_entity = "select.myhome_schedule"
original_state = hass.states.get(select_entity).state
original_state = hass.states.get(SELECT_ENTITY).state
response = {
"event_type": "schedule",
@@ -133,7 +135,71 @@ async def test_select_schedule_unknown_schedule_id(
await simulate_webhook(hass, webhook_id, response)
await hass.async_block_till_done()
assert hass.states.get(select_entity).state == original_state
assert hass.states.get(SELECT_ENTITY).state == original_state
async def test_select_without_selected_schedule_at_setup(
hass: HomeAssistant,
config_entry: MockConfigEntry,
) -> None:
"""Test the select is created when no schedule is selected for the mode."""
def unselect_schedules(payload: dict[str, Any]) -> None:
body = payload.get("body")
if not isinstance(body, dict):
return
for home in body.get("homes", []):
if home["id"] != HOME_ID:
continue
for schedule in home.get("schedules", []):
schedule.pop("selected", None)
async def fake_post(*args: Any, **kwargs: Any):
return await fake_post_request(
hass, *args, msg_callback=unselect_schedules, **kwargs
)
with (
selected_platforms([Platform.SELECT]),
patch(
"homeassistant.components.netatmo.api.AsyncConfigEntryNetatmoAuth"
) as mock_auth,
):
mock_auth.return_value.async_post_api_request.side_effect = fake_post
mock_auth.return_value.async_addwebhook.side_effect = AsyncMock()
mock_auth.return_value.async_dropwebhook.side_effect = AsyncMock()
assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
state = hass.states.get(SELECT_ENTITY)
assert state.state == STATE_UNKNOWN
assert state.attributes[ATTR_OPTIONS] == ["Default", "Winter"]
@pytest.mark.usefixtures("netatmo_auth")
async def test_select_survives_selected_schedule_disappearing(
hass: HomeAssistant,
config_entry: MockConfigEntry,
) -> None:
"""Test an update without a selected schedule does not break polling."""
with selected_platforms([Platform.SELECT]):
assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
data_handler = config_entry.runtime_data
signal_name = f"{HOME}-{HOME_ID}"
assert hass.states.get(SELECT_ENTITY).state == "Default"
# Raising here would abort the update round for every other publisher too
with patch("pyatmo.home.Home.get_selected_schedule", return_value=None):
await data_handler.async_fetch_data(signal_name)
assert hass.states.get(SELECT_ENTITY).state == STATE_UNKNOWN
await data_handler.async_fetch_data(signal_name)
assert hass.states.get(SELECT_ENTITY).state == "Default"
class _ClimateLastFeatures(set[str]):
@@ -202,4 +268,4 @@ async def test_select_created_when_climate_is_not_first_room_feature(
assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
assert hass.states.get("select.myhome_schedule").state == "Default"
assert hass.states.get(SELECT_ENTITY).state == "Default"