Skip to content

Commit

Permalink
airzone: reload entry on new devices
Browse files Browse the repository at this point in the history
Signed-off-by: Álvaro Fernández Rojas <[email protected]>
  • Loading branch information
Noltari committed Jul 13, 2024
1 parent 9e259cb commit 3ecc084
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 2 deletions.
14 changes: 13 additions & 1 deletion homeassistant/components/airzone/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import logging
from typing import Any

from aioairzone.const import AZD_NEW_SYSTEMS, AZD_NEW_ZONES
from aioairzone.exceptions import AirzoneError
from aioairzone.localapi import AirzoneLocalApi

Expand Down Expand Up @@ -34,11 +35,22 @@ def __init__(self, hass: HomeAssistant, airzone: AirzoneLocalApi) -> None:
update_interval=SCAN_INTERVAL,
)

async def _async_check_new_devices(self, data: dict[str, Any]) -> None:
"""Reload entry if there are new devices."""
if (
len(data.get(AZD_NEW_SYSTEMS, [])) > 0
or len(data.get(AZD_NEW_ZONES, [])) > 0
):
assert self.config_entry
self.hass.config_entries.async_schedule_reload(self.config_entry.entry_id)

async def _async_update_data(self) -> dict[str, Any]:
"""Update data via library."""
async with timeout(AIOAIRZONE_DEVICE_TIMEOUT_SEC):
try:
await self.airzone.update()
except AirzoneError as error:
raise UpdateFailed(error) from error
return self.airzone.data()
data = self.airzone.data()
await self._async_check_new_devices(data)
return data
57 changes: 56 additions & 1 deletion tests/components/airzone/test_coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from homeassistant.core import HomeAssistant
from homeassistant.util.dt import utcnow

from .util import CONFIG, HVAC_MOCK, HVAC_VERSION_MOCK
from .util import CONFIG, HVAC_MOCK, HVAC_MOCK_NEW_ZONES, HVAC_VERSION_MOCK

from tests.common import MockConfigEntry, async_fire_time_changed

Expand Down Expand Up @@ -64,3 +64,58 @@ async def test_coordinator_client_connector_error(hass: HomeAssistant) -> None:

state = hass.states.get("sensor.despacho_temperature")
assert state.state == STATE_UNAVAILABLE


async def test_coordinator_new_devices(hass: HomeAssistant) -> None:
"""Test new devices on coordinator update."""

config_entry = MockConfigEntry(
data=CONFIG,
domain=DOMAIN,
unique_id="airzone_unique_id",
)
config_entry.add_to_hass(hass)

with (
patch(
"homeassistant.components.airzone.AirzoneLocalApi.get_dhw",
side_effect=HotWaterNotAvailable,
),
patch(
"homeassistant.components.airzone.AirzoneLocalApi.get_hvac",
return_value=HVAC_MOCK_NEW_ZONES,
) as mock_hvac,
patch(
"homeassistant.components.airzone.AirzoneLocalApi.get_hvac_systems",
side_effect=SystemOutOfRange,
),
patch(
"homeassistant.components.airzone.AirzoneLocalApi.get_version",
return_value=HVAC_VERSION_MOCK,
),
patch(
"homeassistant.components.airzone.AirzoneLocalApi.get_webserver",
side_effect=InvalidMethod,
),
):
await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
mock_hvac.assert_called_once()
mock_hvac.reset_mock()

state = hass.states.get("sensor.salon_temperature")
assert state.state == "19.6"

state = hass.states.get("sensor.dorm_ppal_temperature")
assert state is None

mock_hvac.return_value = HVAC_MOCK
async_fire_time_changed(hass, utcnow() + SCAN_INTERVAL)
await hass.async_block_till_done()
mock_hvac.assert_called()

state = hass.states.get("sensor.salon_temperature")
assert state.state == "19.6"

state = hass.states.get("sensor.dorm_ppal_temperature")
assert state.state == "21.1"
11 changes: 11 additions & 0 deletions tests/components/airzone/util.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Tests for the Airzone integration."""

from copy import deepcopy
from unittest.mock import patch

from aioairzone.const import (
Expand Down Expand Up @@ -274,6 +275,16 @@
]
}

HVAC_MOCK_NEW_ZONES = {
API_SYSTEMS: [
{
API_DATA: [
deepcopy(HVAC_MOCK[API_SYSTEMS][0][API_DATA][0]),
]
}
]
}

HVAC_DHW_MOCK = {
API_DATA: {
API_SYSTEM_ID: 0,
Expand Down

0 comments on commit 3ecc084

Please sign in to comment.