Handle preset change errors in ViCare integration (#103992)

pull/104627/head
Christopher Fenner 2023-11-27 15:35:43 +01:00 committed by GitHub
parent 706add4a57
commit a5934e9acc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 11 deletions

View File

@ -34,6 +34,7 @@ from homeassistant.const import (
UnitOfTemperature,
)
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ServiceValidationError
from homeassistant.helpers import entity_platform
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback
@ -292,22 +293,45 @@ class ViCareClimate(ViCareEntity, ClimateEntity):
def set_preset_mode(self, preset_mode: str) -> None:
"""Set new preset mode and deactivate any existing programs."""
vicare_program = HA_TO_VICARE_PRESET_HEATING.get(preset_mode)
if vicare_program is None:
raise ValueError(
f"Cannot set invalid vicare program: {preset_mode}/{vicare_program}"
target_program = HA_TO_VICARE_PRESET_HEATING.get(preset_mode)
if target_program is None:
raise ServiceValidationError(
translation_domain=DOMAIN,
translation_key="program_unknown",
translation_placeholders={
"preset": preset_mode,
},
)
_LOGGER.debug("Setting preset to %s / %s", preset_mode, vicare_program)
if self._current_program != VICARE_PROGRAM_NORMAL:
_LOGGER.debug("Current preset %s", self._current_program)
if self._current_program and self._current_program != VICARE_PROGRAM_NORMAL:
# We can't deactivate "normal"
_LOGGER.debug("deactivating %s", self._current_program)
try:
self._circuit.deactivateProgram(self._current_program)
except PyViCareCommandError:
_LOGGER.debug("Unable to deactivate program %s", self._current_program)
if vicare_program != VICARE_PROGRAM_NORMAL:
# And we can't explicitly activate normal, either
self._circuit.activateProgram(vicare_program)
except PyViCareCommandError as err:
raise ServiceValidationError(
translation_domain=DOMAIN,
translation_key="program_not_deactivated",
translation_placeholders={
"program": self._current_program,
},
) from err
_LOGGER.debug("Setting preset to %s / %s", preset_mode, target_program)
if target_program != VICARE_PROGRAM_NORMAL:
# And we can't explicitly activate "normal", either
_LOGGER.debug("activating %s", target_program)
try:
self._circuit.activateProgram(target_program)
except PyViCareCommandError as err:
raise ServiceValidationError(
translation_domain=DOMAIN,
translation_key="program_not_activated",
translation_placeholders={
"program": target_program,
},
) from err
@property
def extra_state_attributes(self):

View File

@ -288,6 +288,17 @@
}
}
},
"exceptions": {
"program_unknown": {
"message": "Cannot translate preset {preset} into a valid ViCare program"
},
"program_not_activated": {
"message": "Unable to activate ViCare program {program}"
},
"program_not_deactivated": {
"message": "Unable to deactivate ViCare program {program}"
}
},
"services": {
"set_vicare_mode": {
"name": "Set ViCare mode",