Handle connection error in honeywell (#108168)

* Handle connection error

* Catch connection error

* Add tests

* Add translation strings

* Clean up overlapping exceptions

* ServiceValidationError

* HomeAssistant Error translations

---------

Co-authored-by: Erik Montnemery <erik@montnemery.com>
This commit is contained in:
mkmer 2024-04-18 07:53:32 -04:00 committed by GitHub
parent 47f0d5ed1f
commit 28da10ad0d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 140 additions and 31 deletions

View File

@ -34,7 +34,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.exceptions import HomeAssistantError, ServiceValidationError
from homeassistant.helpers import device_registry as dr, issue_registry as ir
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
@ -361,15 +361,18 @@ class HoneywellUSThermostat(ClimateEntity):
if mode in ["heat", "emheat"]:
await self._device.set_setpoint_heat(temperature)
except UnexpectedResponse as err:
except (AscConnectionError, UnexpectedResponse) as err:
raise HomeAssistantError(
"Honeywell set temperature failed: Invalid Response"
translation_domain=DOMAIN,
translation_key="temp_failed",
) from err
except SomeComfortError as err:
_LOGGER.error("Invalid temperature %.1f: %s", temperature, err)
raise ValueError(
f"Honeywell set temperature failed: invalid temperature {temperature}."
raise ServiceValidationError(
translation_domain=DOMAIN,
translation_key="temp_failed_value",
translation_placeholders={"temp": temperature},
) from err
async def async_set_temperature(self, **kwargs: Any) -> None:
@ -382,30 +385,41 @@ class HoneywellUSThermostat(ClimateEntity):
if temperature := kwargs.get(ATTR_TARGET_TEMP_LOW):
await self._device.set_setpoint_heat(temperature)
except UnexpectedResponse as err:
except (AscConnectionError, UnexpectedResponse) as err:
raise HomeAssistantError(
"Honeywell set temperature failed: Invalid Response"
translation_domain=DOMAIN,
translation_key="temp_failed",
) from err
except SomeComfortError as err:
_LOGGER.error("Invalid temperature %.1f: %s", temperature, err)
raise ValueError(
f"Honeywell set temperature failed: invalid temperature: {temperature}."
raise ServiceValidationError(
translation_domain=DOMAIN,
translation_key="temp_failed_value",
translation_placeholders={"temp": str(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
raise HomeAssistantError(
translation_domain=DOMAIN,
translation_key="fan_mode_failed",
) 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
raise HomeAssistantError(
translation_domain=DOMAIN,
translation_key="sys_mode_failed",
) from err
async def _turn_away_mode_on(self) -> None:
"""Turn away on.
@ -425,6 +439,12 @@ class HoneywellUSThermostat(ClimateEntity):
if mode in HEATING_MODES:
await self._device.set_hold_heat(True, self._heat_away_temp)
except (AscConnectionError, UnexpectedResponse) as err:
raise HomeAssistantError(
translation_domain=DOMAIN,
translation_key="away_mode_failed",
) from err
except SomeComfortError as err:
_LOGGER.error(
"Temperature out of range. Mode: %s, Heat Temperature: %.1f, Cool Temperature: %.1f",
@ -432,8 +452,14 @@ class HoneywellUSThermostat(ClimateEntity):
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}."
raise ServiceValidationError(
translation_domain=DOMAIN,
translation_key="temp_failed_range",
translation_placeholders={
"heat": str(self._heat_away_temp),
"cool": str(self._cool_away_temp),
"mode": mode,
},
) from err
async def _turn_hold_mode_on(self) -> None:
@ -452,11 +478,16 @@ class HoneywellUSThermostat(ClimateEntity):
except SomeComfortError as err:
_LOGGER.error("Couldn't set permanent hold")
raise HomeAssistantError(
"Honeywell couldn't set permanent hold."
translation_domain=DOMAIN,
translation_key="set_hold_failed",
) from err
else:
_LOGGER.error("Invalid system mode returned: %s", mode)
raise HomeAssistantError(f"Honeywell invalid system mode returned {mode}.")
raise HomeAssistantError(
translation_domain=DOMAIN,
translation_key="set_mode_failed",
translation_placeholders={"mode": mode},
)
async def _turn_away_mode_off(self) -> None:
"""Turn away/hold off."""
@ -465,9 +496,13 @@ class HoneywellUSThermostat(ClimateEntity):
# Disabling all hold modes
await self._device.set_hold_cool(False)
await self._device.set_hold_heat(False)
except SomeComfortError as err:
_LOGGER.error("Can not stop hold mode")
raise HomeAssistantError("Honeywell could not stop hold mode") from err
raise HomeAssistantError(
translation_domain=DOMAIN,
translation_key="stop_hold_failed",
) from err
async def async_set_preset_mode(self, preset_mode: str) -> None:
"""Set new preset mode."""
@ -493,9 +528,11 @@ class HoneywellUSThermostat(ClimateEntity):
)
try:
await self._device.set_system_mode("emheat")
except SomeComfortError as err:
raise HomeAssistantError(
"Honeywell could not set system mode to aux heat."
translation_domain=DOMAIN,
translation_key="set_aux_failed",
) from err
async def async_turn_aux_heat_off(self) -> None:
@ -517,8 +554,12 @@ class HoneywellUSThermostat(ClimateEntity):
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
raise HomeAssistantError(
translation_domain=DOMAIN,
translation_key="disable_aux_failed",
) from err
async def async_update(self) -> None:
"""Get the latest state from the service."""

View File

@ -61,6 +61,39 @@
}
},
"exceptions": {
"temp_failed": {
"message": "Honeywell set temperature failed"
},
"sys_mode_failed": {
"message": "Honeywell could not set system mode"
},
"fan_mode_failed": {
"message": "Honeywell could not set fan mode"
},
"away_mode_failed": {
"message": "Honeywell set away mode failed"
},
"temp_failed_value": {
"message": "Honeywell set temperature failed: invalid temperature {temperature}"
},
"temp_failed_range": {
"message": "Honeywell set temperature failed: temperature out of range. Mode: {mode}, Heat Temperuature: {heat}, Cool Temperature: {cool}"
},
"set_hold_failed": {
"message": "Honeywell could not set permanent hold"
},
"set_mode_failed": {
"message": "Honeywell invalid system mode returned {mode}"
},
"stop_hold_failed": {
"message": "Honeywell could not stop hold mode"
},
"set_aux_failed": {
"message": "Honeywell could not set system mode to aux heat"
},
"disable_aux_failed": {
"message": "Honeywell could turn off aux heat mode"
},
"switch_failed_off": {
"message": "Honeywell could turn off emergency heat mode."
},

View File

@ -38,7 +38,7 @@ from homeassistant.const import (
STATE_UNAVAILABLE,
)
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.exceptions import HomeAssistantError, ServiceValidationError
from homeassistant.helpers import entity_registry as er
from homeassistant.util.dt import utcnow
@ -205,6 +205,16 @@ 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.UnexpectedResponse
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,
)
async def test_auxheat_service_calls(
hass: HomeAssistant, device: MagicMock, config_entry: MagicMock
@ -300,6 +310,15 @@ async def test_fan_modes_service_calls(
blocking=True,
)
device.set_fan_mode.side_effect = aiosomecomfort.UnexpectedResponse
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,
@ -344,7 +363,7 @@ async def test_service_calls_off_mode(
device.set_setpoint_heat.side_effect = aiosomecomfort.SomeComfortError
caplog.clear()
with pytest.raises(ValueError):
with pytest.raises(ServiceValidationError):
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_TEMPERATURE,
@ -431,6 +450,12 @@ async def test_service_calls_off_mode(
device.set_hold_heat.assert_called_once_with(False)
device.set_hold_cool.assert_called_once_with(False)
device.set_hold_heat.reset_mock()
device.set_hold_cool.reset_mock()
device.set_setpoint_cool.reset_mock()
device.set_setpoint_heat.reset_mock()
reset_mock(device)
device.raw_ui_data["StatusHeat"] = 2
@ -506,7 +531,7 @@ async def test_service_calls_cool_mode(
device.set_setpoint_cool.reset_mock()
device.set_setpoint_cool.side_effect = aiosomecomfort.SomeComfortError
with pytest.raises(ValueError):
with pytest.raises(ServiceValidationError):
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_TEMPERATURE,
@ -538,7 +563,7 @@ async def test_service_calls_cool_mode(
device.set_hold_cool.side_effect = aiosomecomfort.SomeComfortError
caplog.clear()
with pytest.raises(ValueError):
with pytest.raises(ServiceValidationError):
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_PRESET_MODE,
@ -570,7 +595,7 @@ async def test_service_calls_cool_mode(
device.hold_heat = True
device.hold_cool = True
with pytest.raises(ValueError):
with pytest.raises(ServiceValidationError):
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_TEMPERATURE,
@ -709,7 +734,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):
with pytest.raises(ServiceValidationError):
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_TEMPERATURE,
@ -747,7 +772,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):
with pytest.raises(ServiceValidationError):
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_TEMPERATURE,
@ -780,7 +805,7 @@ async def test_service_calls_heat_mode(
device.hold_heat = True
device.hold_cool = True
with pytest.raises(ValueError):
with pytest.raises(ServiceValidationError):
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_TEMPERATURE,
@ -811,7 +836,7 @@ async def test_service_calls_heat_mode(
reset_mock(device)
with pytest.raises(ValueError):
with pytest.raises(ServiceValidationError):
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_PRESET_MODE,
@ -828,7 +853,7 @@ async def test_service_calls_heat_mode(
device.set_hold_heat.side_effect = aiosomecomfort.SomeComfortError
with pytest.raises(ValueError):
with pytest.raises(ServiceValidationError):
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_PRESET_MODE,
@ -841,6 +866,16 @@ async def test_service_calls_heat_mode(
device.set_setpoint_cool.assert_not_called()
assert "Temperature out of range" in caplog.text
device.set_hold_heat.side_effect = aiosomecomfort.UnexpectedResponse
with pytest.raises(HomeAssistantError):
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_PRESET_MODE,
{ATTR_ENTITY_ID: entity_id, ATTR_PRESET_MODE: PRESET_AWAY},
blocking=True,
)
reset_mock(device)
caplog.clear()
with pytest.raises(HomeAssistantError):
@ -951,7 +986,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):
with pytest.raises(ServiceValidationError):
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_TEMPERATURE,
@ -966,7 +1001,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):
with pytest.raises(ServiceValidationError):
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_TEMPERATURE,
@ -1021,7 +1056,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):
with pytest.raises(ServiceValidationError):
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_PRESET_MODE,