airzone: implement turn on/off (#70095)

Signed-off-by: Álvaro Fernández Rojas <noltari@gmail.com>
This commit is contained in:
Álvaro Fernández Rojas 2022-04-15 15:13:59 +02:00 committed by GitHub
parent 8ab9cd1695
commit abea7d3245
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 83 additions and 3 deletions

View file

@ -26,7 +26,6 @@ from aioairzone.const import (
AZD_ZONES,
)
from aioairzone.exceptions import AirzoneError
from aiohttp.client_exceptions import ClientConnectorError
from homeassistant.components.climate import ClimateEntity
from homeassistant.components.climate.const import (
@ -128,13 +127,33 @@ class AirzoneClimate(AirzoneEntity, ClimateEntity):
"""Send HVAC parameters to API."""
try:
await self.coordinator.airzone.put_hvac(params)
except (AirzoneError, ClientConnectorError) as error:
except AirzoneError as error:
raise HomeAssistantError(
f"Failed to set zone {self.name}: {error}"
) from error
else:
self.coordinator.async_set_updated_data(self.coordinator.airzone.data())
async def async_turn_on(self) -> None:
"""Turn the entity on."""
params = {
API_SYSTEM_ID: self.system_id,
API_ZONE_ID: self.zone_id,
API_ON: 1,
}
_LOGGER.debug("Turn off params=%s", params)
await self._async_update_hvac_params(params)
async def async_turn_off(self) -> None:
"""Turn the entity off."""
params = {
API_SYSTEM_ID: self.system_id,
API_ZONE_ID: self.zone_id,
API_ON: 0,
}
_LOGGER.debug("Turn off params=%s", params)
await self._async_update_hvac_params(params)
async def async_set_hvac_mode(self, hvac_mode: str) -> None:
"""Set hvac mode."""
params = {

View file

@ -36,7 +36,12 @@ from homeassistant.components.climate.const import (
SERVICE_SET_HVAC_MODE,
SERVICE_SET_TEMPERATURE,
)
from homeassistant.const import ATTR_ENTITY_ID, ATTR_TEMPERATURE
from homeassistant.const import (
ATTR_ENTITY_ID,
ATTR_TEMPERATURE,
SERVICE_TURN_OFF,
SERVICE_TURN_ON,
)
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
@ -134,6 +139,62 @@ async def test_airzone_create_climates(hass: HomeAssistant) -> None:
assert state.attributes.get(ATTR_TEMPERATURE) == 19.1
async def test_airzone_climate_turn_on_off(hass: HomeAssistant) -> None:
"""Test turning on."""
await async_init_integration(hass)
HVAC_MOCK = {
API_DATA: [
{
API_SYSTEM_ID: 1,
API_ZONE_ID: 1,
API_ON: 1,
}
]
}
with patch(
"homeassistant.components.airzone.AirzoneLocalApi.http_request",
return_value=HVAC_MOCK,
):
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_TURN_ON,
{
ATTR_ENTITY_ID: "climate.salon",
},
blocking=True,
)
state = hass.states.get("climate.salon")
assert state.state == HVAC_MODE_HEAT
HVAC_MOCK = {
API_DATA: [
{
API_SYSTEM_ID: 1,
API_ZONE_ID: 1,
API_ON: 0,
}
]
}
with patch(
"homeassistant.components.airzone.AirzoneLocalApi.http_request",
return_value=HVAC_MOCK,
):
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_TURN_OFF,
{
ATTR_ENTITY_ID: "climate.salon",
},
blocking=True,
)
state = hass.states.get("climate.salon")
assert state.state == HVAC_MODE_OFF
async def test_airzone_climate_set_hvac_mode(hass: HomeAssistant) -> None:
"""Test setting the HVAC mode."""