Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle preset change errors in ViCare integration #103992

Merged
merged 12 commits into from
Nov 27, 2023
46 changes: 35 additions & 11 deletions homeassistant/components/vicare/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
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
Expand Down Expand Up @@ -288,22 +289,45 @@ def preset_mode(self):

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):
Expand Down
11 changes: 11 additions & 0 deletions homeassistant/components/vicare/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down