Raise HomeAssistantError/ValueError for service calls in Honeywell (#100041)

This commit is contained in:
mkmer 2023-09-10 09:58:59 -04:00 committed by GitHub
parent d56ad14673
commit 59e87c0864
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 310 additions and 216 deletions

View File

@ -27,6 +27,7 @@ from homeassistant.components.climate import (
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_TEMPERATURE, UnitOfTemperature
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
@ -315,6 +316,9 @@ class HoneywellUSThermostat(ClimateEntity):
except SomeComfortError as err:
_LOGGER.error("Invalid temperature %.1f: %s", temperature, err)
raise ValueError(
f"Honeywell set temperature failed: invalid temperature {temperature}."
) from err
async def async_set_temperature(self, **kwargs: Any) -> None:
"""Set new target temperature."""
@ -328,14 +332,23 @@ class HoneywellUSThermostat(ClimateEntity):
except SomeComfortError as err:
_LOGGER.error("Invalid temperature %.1f: %s", temperature, err)
raise ValueError(
f"Honeywell set temperature failed: invalid temperature: {temperature}."
) from err
async def async_set_fan_mode(self, fan_mode: str) -> None:
"""Set new target fan mode."""
try:
await self._device.set_fan_mode(self._fan_mode_map[fan_mode])
except SomeComfortError as err:
raise HomeAssistantError("Honeywell could not set fan mode.") from err
async def async_set_hvac_mode(self, hvac_mode: HVACMode) -> None:
"""Set new target hvac mode."""
try:
await self._device.set_system_mode(self._hvac_mode_map[hvac_mode])
except SomeComfortError as err:
raise HomeAssistantError("Honeywell could not set system mode.") from err
async def _turn_away_mode_on(self) -> None:
"""Turn away on.
@ -355,13 +368,16 @@ class HoneywellUSThermostat(ClimateEntity):
if mode in HEATING_MODES:
await self._device.set_hold_heat(True, self._heat_away_temp)
except SomeComfortError:
except SomeComfortError as err:
_LOGGER.error(
"Temperature out of range. Mode: %s, Heat Temperature: %.1f, Cool Temperature: %.1f",
mode,
self._heat_away_temp,
self._cool_away_temp,
)
raise ValueError(
f"Honeywell set temperature failed: temperature out of range. Mode: {mode}, Heat Temperuature: {self._heat_away_temp}, Cool Temperature: {self._cool_away_temp}."
) from err
async def _turn_hold_mode_on(self) -> None:
"""Turn permanent hold on."""
@ -376,10 +392,14 @@ class HoneywellUSThermostat(ClimateEntity):
if mode in HEATING_MODES:
await self._device.set_hold_heat(True)
except SomeComfortError:
except SomeComfortError as err:
_LOGGER.error("Couldn't set permanent hold")
raise HomeAssistantError(
"Honeywell couldn't set permanent hold."
) from err
else:
_LOGGER.error("Invalid system mode returned: %s", mode)
raise HomeAssistantError(f"Honeywell invalid system mode returned {mode}.")
async def _turn_away_mode_off(self) -> None:
"""Turn away/hold off."""
@ -388,8 +408,9 @@ class HoneywellUSThermostat(ClimateEntity):
# Disabling all hold modes
await self._device.set_hold_cool(False)
await self._device.set_hold_heat(False)
except SomeComfortError:
except SomeComfortError as err:
_LOGGER.error("Can not stop hold mode")
raise HomeAssistantError("Honeywell could not stop hold mode") from err
async def async_set_preset_mode(self, preset_mode: str) -> None:
"""Set new preset mode."""
@ -403,14 +424,22 @@ class HoneywellUSThermostat(ClimateEntity):
async def async_turn_aux_heat_on(self) -> None:
"""Turn auxiliary heater on."""
try:
await self._device.set_system_mode("emheat")
except SomeComfortError as err:
raise HomeAssistantError(
"Honeywell could not set system mode to aux heat."
) from err
async def async_turn_aux_heat_off(self) -> None:
"""Turn auxiliary heater off."""
try:
if HVACMode.HEAT in self.hvac_modes:
await self.async_set_hvac_mode(HVACMode.HEAT)
else:
await self.async_set_hvac_mode(HVACMode.OFF)
except HomeAssistantError as err:
raise HomeAssistantError("Honeywell could turn off aux heat mode.") from err
async def async_update(self) -> None:
"""Get the latest state from the service."""

View File

@ -37,6 +37,7 @@ from homeassistant.const import (
STATE_UNAVAILABLE,
)
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import entity_registry as er
from homeassistant.util.dt import utcnow
@ -193,6 +194,15 @@ async def test_mode_service_calls(
device.set_system_mode.assert_called_once_with("auto")
device.set_system_mode.reset_mock()
device.set_system_mode.side_effect = aiosomecomfort.SomeComfortError
with pytest.raises(HomeAssistantError):
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_HVAC_MODE,
{ATTR_ENTITY_ID: entity_id, ATTR_HVAC_MODE: HVACMode.HEAT_COOL},
blocking=True,
)
device.set_system_mode.assert_called_once_with("auto")
async def test_auxheat_service_calls(
@ -211,6 +221,7 @@ async def test_auxheat_service_calls(
device.set_system_mode.assert_called_once_with("emheat")
device.set_system_mode.reset_mock()
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_AUX_HEAT,
@ -219,6 +230,27 @@ async def test_auxheat_service_calls(
)
device.set_system_mode.assert_called_once_with("heat")
device.set_system_mode.reset_mock()
device.set_system_mode.side_effect = aiosomecomfort.SomeComfortError
with pytest.raises(HomeAssistantError):
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_AUX_HEAT,
{ATTR_ENTITY_ID: entity_id, ATTR_AUX_HEAT: True},
blocking=True,
)
device.set_system_mode.assert_called_once_with("emheat")
device.set_system_mode.reset_mock()
device.set_system_mode.side_effect = aiosomecomfort.SomeComfortError
with pytest.raises(HomeAssistantError):
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_AUX_HEAT,
{ATTR_ENTITY_ID: entity_id, ATTR_AUX_HEAT: False},
blocking=True,
)
async def test_fan_modes_service_calls(
hass: HomeAssistant, device: MagicMock, config_entry: MagicMock
@ -256,6 +288,17 @@ async def test_fan_modes_service_calls(
device.set_fan_mode.assert_called_once_with("circulate")
device.set_fan_mode.reset_mock()
device.set_fan_mode.side_effect = aiosomecomfort.SomeComfortError
with pytest.raises(HomeAssistantError):
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_FAN_MODE,
{ATTR_ENTITY_ID: entity_id, ATTR_FAN_MODE: FAN_DIFFUSE},
blocking=True,
)
async def test_service_calls_off_mode(
hass: HomeAssistant,
@ -299,6 +342,8 @@ async def test_service_calls_off_mode(
device.set_setpoint_heat.reset_mock()
device.set_setpoint_heat.side_effect = aiosomecomfort.SomeComfortError
caplog.clear()
with pytest.raises(ValueError):
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_TEMPERATURE,
@ -387,7 +432,6 @@ async def test_service_calls_off_mode(
device.set_hold_heat.side_effect = aiosomecomfort.SomeComfortError
device.raw_ui_data["StatusHeat"] = 2
device.raw_ui_data["StatusCool"] = 2
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_PRESET_MODE,
@ -443,6 +487,8 @@ async def test_service_calls_cool_mode(
caplog.clear()
device.set_setpoint_cool.reset_mock()
device.set_setpoint_cool.side_effect = aiosomecomfort.SomeComfortError
with pytest.raises(ValueError):
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_TEMPERATURE,
@ -474,6 +520,7 @@ async def test_service_calls_cool_mode(
device.set_hold_cool.side_effect = aiosomecomfort.SomeComfortError
caplog.clear()
with pytest.raises(ValueError):
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_PRESET_MODE,
@ -491,6 +538,7 @@ async def test_service_calls_cool_mode(
device.raw_ui_data["StatusHeat"] = 2
device.raw_ui_data["StatusCool"] = 2
with pytest.raises(HomeAssistantError):
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_PRESET_MODE,
@ -504,6 +552,7 @@ async def test_service_calls_cool_mode(
device.hold_heat = True
device.hold_cool = True
with pytest.raises(ValueError):
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_TEMPERATURE,
@ -519,7 +568,7 @@ async def test_service_calls_cool_mode(
device.raw_ui_data["StatusHeat"] = 2
device.raw_ui_data["StatusCool"] = 2
caplog.clear()
with pytest.raises(HomeAssistantError):
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_PRESET_MODE,
@ -531,7 +580,7 @@ async def test_service_calls_cool_mode(
assert "Couldn't set permanent hold" in caplog.text
reset_mock(device)
with pytest.raises(HomeAssistantError):
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_PRESET_MODE,
@ -546,7 +595,7 @@ async def test_service_calls_cool_mode(
caplog.clear()
device.set_hold_cool.side_effect = aiosomecomfort.SomeComfortError
with pytest.raises(HomeAssistantError):
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_PRESET_MODE,
@ -563,6 +612,7 @@ async def test_service_calls_cool_mode(
device.raw_ui_data["StatusHeat"] = 2
device.raw_ui_data["StatusCool"] = 2
with pytest.raises(HomeAssistantError):
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_PRESET_MODE,
@ -580,7 +630,7 @@ async def test_service_calls_cool_mode(
device.raw_ui_data["StatusHeat"] = 2
device.raw_ui_data["StatusCool"] = 2
with pytest.raises(HomeAssistantError):
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_PRESET_MODE,
@ -599,6 +649,7 @@ async def test_service_calls_cool_mode(
device.raw_ui_data["StatusCool"] = 2
device.system_mode = "Junk"
with pytest.raises(HomeAssistantError):
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_PRESET_MODE,
@ -640,7 +691,7 @@ async def test_service_calls_heat_mode(
device.set_hold_heat.reset_mock()
device.set_hold_heat.side_effect = aiosomecomfort.SomeComfortError
with pytest.raises(ValueError):
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_TEMPERATURE,
@ -667,6 +718,7 @@ async def test_service_calls_heat_mode(
device.set_setpoint_heat.reset_mock()
device.set_setpoint_heat.side_effect = aiosomecomfort.SomeComfortError
with pytest.raises(ValueError):
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_TEMPERATURE,
@ -685,6 +737,7 @@ async def test_service_calls_heat_mode(
device.raw_ui_data["StatusHeat"] = 2
device.raw_ui_data["StatusCool"] = 2
with pytest.raises(HomeAssistantError):
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_PRESET_MODE,
@ -698,6 +751,7 @@ async def test_service_calls_heat_mode(
device.hold_heat = True
device.hold_cool = True
with pytest.raises(ValueError):
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_TEMPERATURE,
@ -715,6 +769,7 @@ async def test_service_calls_heat_mode(
device.raw_ui_data["StatusHeat"] = 2
device.raw_ui_data["StatusCool"] = 2
with pytest.raises(HomeAssistantError):
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_PRESET_MODE,
@ -727,6 +782,7 @@ async def test_service_calls_heat_mode(
reset_mock(device)
with pytest.raises(ValueError):
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_PRESET_MODE,
@ -743,6 +799,7 @@ async def test_service_calls_heat_mode(
device.set_hold_heat.side_effect = aiosomecomfort.SomeComfortError
with pytest.raises(ValueError):
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_PRESET_MODE,
@ -757,7 +814,7 @@ async def test_service_calls_heat_mode(
reset_mock(device)
caplog.clear()
with pytest.raises(HomeAssistantError):
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_PRESET_MODE,
@ -771,7 +828,7 @@ async def test_service_calls_heat_mode(
device.set_hold_heat.reset_mock()
device.set_hold_cool.reset_mock()
device.set_hold_heat.side_effect = aiosomecomfort.SomeComfortError
with pytest.raises(HomeAssistantError):
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_PRESET_MODE,
@ -786,6 +843,7 @@ async def test_service_calls_heat_mode(
device.raw_ui_data["StatusHeat"] = 2
device.raw_ui_data["StatusCool"] = 2
with pytest.raises(HomeAssistantError):
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_PRESET_MODE,
@ -802,6 +860,7 @@ async def test_service_calls_heat_mode(
device.raw_ui_data["StatusHeat"] = 2
device.raw_ui_data["StatusCool"] = 2
with pytest.raises(HomeAssistantError):
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_PRESET_MODE,
@ -863,7 +922,7 @@ async def test_service_calls_auto_mode(
device.set_hold_cool.side_effect = aiosomecomfort.SomeComfortError
device.set_hold_heat.side_effect = aiosomecomfort.SomeComfortError
with pytest.raises(ValueError):
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_TEMPERATURE,
@ -878,6 +937,7 @@ async def test_service_calls_auto_mode(
device.set_setpoint_heat.side_effect = aiosomecomfort.SomeComfortError
device.set_setpoint_cool.side_effect = aiosomecomfort.SomeComfortError
with pytest.raises(ValueError):
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_TEMPERATURE,
@ -917,6 +977,7 @@ async def test_service_calls_auto_mode(
device.raw_ui_data["StatusHeat"] = 2
device.raw_ui_data["StatusCool"] = 2
with pytest.raises(HomeAssistantError):
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_PRESET_MODE,
@ -931,6 +992,7 @@ async def test_service_calls_auto_mode(
device.set_setpoint_heat.side_effect = None
device.set_setpoint_cool.side_effect = None
with pytest.raises(ValueError):
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_PRESET_MODE,
@ -944,6 +1006,7 @@ async def test_service_calls_auto_mode(
reset_mock(device)
caplog.clear()
with pytest.raises(HomeAssistantError):
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_PRESET_MODE,
@ -956,7 +1019,7 @@ async def test_service_calls_auto_mode(
reset_mock(device)
device.set_hold_cool.side_effect = aiosomecomfort.SomeComfortError
with pytest.raises(HomeAssistantError):
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_PRESET_MODE,
@ -974,6 +1037,7 @@ async def test_service_calls_auto_mode(
device.raw_ui_data["StatusHeat"] = 2
device.raw_ui_data["StatusCool"] = 2
with pytest.raises(HomeAssistantError):
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_PRESET_MODE,
@ -990,6 +1054,7 @@ async def test_service_calls_auto_mode(
device.raw_ui_data["StatusHeat"] = 2
device.raw_ui_data["StatusCool"] = 2
with pytest.raises(HomeAssistantError):
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_PRESET_MODE,