Nest improve error message on climate actions (#86853)

* Nest - Climate - Error Messages

1. Error messages were incorrect for some methods, for example async_set_temperature was reporting failure to set hvac_mode. This is corrected.
2. Error messages were incomplete and were not including the entity_id,and the operation being performed that failed.
3. Add unit tests to test the exception handling

* Move tests into exiisting error test

* Improve readability of error message
pull/86862/head
PeteRager 2023-01-28 15:58:28 -05:00 committed by GitHub
parent 733798f483
commit a7ddd592fb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 11 deletions

View File

@ -290,7 +290,9 @@ class ThermostatEntity(ClimateEntity):
try: try:
await trait.set_mode(api_mode) await trait.set_mode(api_mode)
except ApiException as err: except ApiException as err:
raise HomeAssistantError(f"Error setting HVAC mode: {err}") from err raise HomeAssistantError(
f"Error setting {self.entity_id} HVAC mode to {hvac_mode}: {err}"
) from err
async def async_set_temperature(self, **kwargs: Any) -> None: async def async_set_temperature(self, **kwargs: Any) -> None:
"""Set new target temperature.""" """Set new target temperature."""
@ -313,7 +315,9 @@ class ThermostatEntity(ClimateEntity):
elif hvac_mode == HVACMode.HEAT and temp: elif hvac_mode == HVACMode.HEAT and temp:
await trait.set_heat(temp) await trait.set_heat(temp)
except ApiException as err: except ApiException as err:
raise HomeAssistantError(f"Error setting temperature: {err}") from err raise HomeAssistantError(
f"Error setting {self.entity_id} temperature to {kwargs}: {err}"
) from err
async def async_set_preset_mode(self, preset_mode: str) -> None: async def async_set_preset_mode(self, preset_mode: str) -> None:
"""Set new target preset mode.""" """Set new target preset mode."""
@ -325,7 +329,9 @@ class ThermostatEntity(ClimateEntity):
try: try:
await trait.set_mode(PRESET_INV_MODE_MAP[preset_mode]) await trait.set_mode(PRESET_INV_MODE_MAP[preset_mode])
except ApiException as err: except ApiException as err:
raise HomeAssistantError(f"Error setting HVAC mode: {err}") from err raise HomeAssistantError(
f"Error setting {self.entity_id} preset mode to {preset_mode}: {err}"
) from err
async def async_set_fan_mode(self, fan_mode: str) -> None: async def async_set_fan_mode(self, fan_mode: str) -> None:
"""Set new target fan mode.""" """Set new target fan mode."""
@ -342,4 +348,6 @@ class ThermostatEntity(ClimateEntity):
try: try:
await trait.set_timer(FAN_INV_MODE_MAP[fan_mode], duration=duration) await trait.set_timer(FAN_INV_MODE_MAP[fan_mode], duration=duration)
except ApiException as err: except ApiException as err:
raise HomeAssistantError(f"Error setting HVAC mode: {err}") from err raise HomeAssistantError(
f"Error setting {self.entity_id} fan mode to {fan_mode}: {err}"
) from err

View File

@ -1428,6 +1428,9 @@ async def test_thermostat_hvac_mode_failure(
"availableModes": ["HEAT", "COOL", "HEATCOOL", "OFF"], "availableModes": ["HEAT", "COOL", "HEATCOOL", "OFF"],
"mode": "COOL", "mode": "COOL",
}, },
"sdm.devices.traits.ThermostatTemperatureSetpoint": {
"coolCelsius": 25.0,
},
"sdm.devices.traits.Fan": { "sdm.devices.traits.Fan": {
"timerMode": "OFF", "timerMode": "OFF",
"timerTimeout": "2019-05-10T03:22:54Z", "timerTimeout": "2019-05-10T03:22:54Z",
@ -1449,26 +1452,36 @@ async def test_thermostat_hvac_mode_failure(
assert thermostat.attributes[ATTR_HVAC_ACTION] == HVACAction.IDLE assert thermostat.attributes[ATTR_HVAC_ACTION] == HVACAction.IDLE
auth.responses = [aiohttp.web.Response(status=HTTPStatus.BAD_REQUEST)] auth.responses = [aiohttp.web.Response(status=HTTPStatus.BAD_REQUEST)]
with pytest.raises(HomeAssistantError): with pytest.raises(HomeAssistantError) as e_info:
await common.async_set_hvac_mode(hass, HVACMode.HEAT) await common.async_set_hvac_mode(hass, HVACMode.HEAT)
await hass.async_block_till_done() await hass.async_block_till_done()
assert "HVAC mode" in str(e_info)
assert "climate.my_thermostat" in str(e_info)
assert HVACMode.HEAT in str(e_info)
auth.responses = [aiohttp.web.Response(status=HTTPStatus.BAD_REQUEST)] auth.responses = [aiohttp.web.Response(status=HTTPStatus.BAD_REQUEST)]
with pytest.raises(HomeAssistantError): with pytest.raises(HomeAssistantError) as e_info:
await common.async_set_temperature( await common.async_set_temperature(hass, temperature=25.0)
hass, hvac_mode=HVACMode.HEAT, temperature=25.0
)
await hass.async_block_till_done() await hass.async_block_till_done()
assert "temperature" in str(e_info)
assert "climate.my_thermostat" in str(e_info)
assert "25.0" in str(e_info)
auth.responses = [aiohttp.web.Response(status=HTTPStatus.BAD_REQUEST)] auth.responses = [aiohttp.web.Response(status=HTTPStatus.BAD_REQUEST)]
with pytest.raises(HomeAssistantError): with pytest.raises(HomeAssistantError) as e_info:
await common.async_set_fan_mode(hass, FAN_ON) await common.async_set_fan_mode(hass, FAN_ON)
await hass.async_block_till_done() await hass.async_block_till_done()
assert "fan mode" in str(e_info)
assert "climate.my_thermostat" in str(e_info)
assert FAN_ON in str(e_info)
auth.responses = [aiohttp.web.Response(status=HTTPStatus.BAD_REQUEST)] auth.responses = [aiohttp.web.Response(status=HTTPStatus.BAD_REQUEST)]
with pytest.raises(HomeAssistantError): with pytest.raises(HomeAssistantError) as e_info:
await common.async_set_preset_mode(hass, PRESET_ECO) await common.async_set_preset_mode(hass, PRESET_ECO)
await hass.async_block_till_done() await hass.async_block_till_done()
assert "preset mode" in str(e_info)
assert "climate.my_thermostat" in str(e_info)
assert PRESET_ECO in str(e_info)
async def test_thermostat_available( async def test_thermostat_available(