Improve deCONZ climate platform handling unsupported commands (#41780)

* Improve deCONZ climate platform

* Raise valueerror if missing input

* Fix linting
This commit is contained in:
Robert Svensson 2020-10-13 22:11:09 +02:00 committed by GitHub
parent 3a59f7c2b7
commit dc71b7421c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 18 deletions

View File

@ -16,7 +16,7 @@ from .const import ATTR_OFFSET, ATTR_VALVE, NEW_SENSOR
from .deconz_device import DeconzDevice from .deconz_device import DeconzDevice
from .gateway import get_gateway_from_config_entry from .gateway import get_gateway_from_config_entry
SUPPORT_HVAC = [HVAC_MODE_AUTO, HVAC_MODE_HEAT, HVAC_MODE_OFF] HVAC_MODES = {HVAC_MODE_AUTO: "auto", HVAC_MODE_HEAT: "heat", HVAC_MODE_OFF: "off"}
async def async_setup_entry(hass, config_entry, async_add_entities): async def async_setup_entry(hass, config_entry, async_add_entities):
@ -72,19 +72,19 @@ class DeconzThermostat(DeconzDevice, ClimateEntity):
Need to be one of HVAC_MODE_*. Need to be one of HVAC_MODE_*.
""" """
if self._device.mode in SUPPORT_HVAC: for hass_hvac_mode, device_mode in HVAC_MODES.items():
return self._device.mode if self._device.mode == device_mode:
return hass_hvac_mode
if self._device.state_on: if self._device.state_on:
return HVAC_MODE_HEAT return HVAC_MODE_HEAT
return HVAC_MODE_OFF return HVAC_MODE_OFF
@property @property
def hvac_modes(self): def hvac_modes(self) -> list:
"""Return the list of available hvac operation modes. """Return the list of available hvac operation modes."""
return list(HVAC_MODES)
Need to be a subset of HVAC_MODES.
"""
return SUPPORT_HVAC
@property @property
def current_temperature(self): def current_temperature(self):
@ -98,21 +98,19 @@ class DeconzThermostat(DeconzDevice, ClimateEntity):
async def async_set_temperature(self, **kwargs): async def async_set_temperature(self, **kwargs):
"""Set new target temperature.""" """Set new target temperature."""
data = {} if ATTR_TEMPERATURE not in kwargs:
raise ValueError(f"Expected attribute {ATTR_TEMPERATURE}")
if ATTR_TEMPERATURE in kwargs: data = {"heatsetpoint": kwargs[ATTR_TEMPERATURE] * 100}
data["heatsetpoint"] = kwargs[ATTR_TEMPERATURE] * 100
await self._device.async_set_config(data) await self._device.async_set_config(data)
async def async_set_hvac_mode(self, hvac_mode): async def async_set_hvac_mode(self, hvac_mode):
"""Set new target hvac mode.""" """Set new target hvac mode."""
if hvac_mode == HVAC_MODE_AUTO: if hvac_mode not in HVAC_MODES:
data = {"mode": "auto"} raise ValueError(f"Unsupported mode {hvac_mode}")
elif hvac_mode == HVAC_MODE_HEAT:
data = {"mode": "heat"} data = {"mode": HVAC_MODES[hvac_mode]}
elif hvac_mode == HVAC_MODE_OFF:
data = {"mode": "off"}
await self._device.async_set_config(data) await self._device.async_set_config(data)

View File

@ -1,6 +1,8 @@
"""deCONZ climate platform tests.""" """deCONZ climate platform tests."""
from copy import deepcopy from copy import deepcopy
import pytest
from homeassistant.components import deconz from homeassistant.components import deconz
import homeassistant.components.climate as climate import homeassistant.components.climate as climate
from homeassistant.components.deconz.gateway import get_gateway_from_config_entry from homeassistant.components.deconz.gateway import get_gateway_from_config_entry
@ -155,6 +157,18 @@ async def test_climate_devices(hass):
"put", "/sensors/1/config", json={"mode": "off"} "put", "/sensors/1/config", json={"mode": "off"}
) )
# Service set HVAC mode to unsupported value
with patch.object(
thermostat_device, "_request", return_value=True
) as set_callback, pytest.raises(ValueError):
await hass.services.async_call(
climate.DOMAIN,
climate.SERVICE_SET_HVAC_MODE,
{"entity_id": "climate.thermostat", "hvac_mode": "cool"},
blocking=True,
)
# Service set temperature to 20 # Service set temperature to 20
with patch.object(thermostat_device, "_request", return_value=True) as set_callback: with patch.object(thermostat_device, "_request", return_value=True) as set_callback:
@ -168,6 +182,22 @@ async def test_climate_devices(hass):
"put", "/sensors/1/config", json={"heatsetpoint": 2000.0} "put", "/sensors/1/config", json={"heatsetpoint": 2000.0}
) )
# Service set temperature without providing temperature attribute
with patch.object(
thermostat_device, "_request", return_value=True
) as set_callback, pytest.raises(ValueError):
await hass.services.async_call(
climate.DOMAIN,
climate.SERVICE_SET_TEMPERATURE,
{
"entity_id": "climate.thermostat",
"target_temp_high": 30,
"target_temp_low": 10,
},
blocking=True,
)
await hass.config_entries.async_unload(config_entry.entry_id) await hass.config_entries.async_unload(config_entry.entry_id)
assert len(hass.states.async_all()) == 0 assert len(hass.states.async_all()) == 0