From 2c8d183c85bcf617be3d52e5b3cab76f85f279aa Mon Sep 17 00:00:00 2001 From: Hans Oischinger Date: Sun, 13 Feb 2022 17:38:24 +0100 Subject: [PATCH] Improve hvac_mode compytibility of vicare Viessmann devices can have varying vicare_modes. This integration maps hvac_modes to vicare_modes without even checking if those vicare_modes are available. This PR improves the situation by a) checking if the vicare_mode for the desired hvac_mode actually exits b) providing only the hvac_modes that are actually supported (meaning: a matching vicare_mode exists) c) Defining an order of vicare_modes that should be used for a given hvac_mode. Example for c): HVACMode.OFF should be VICARE_MODE_FORCEDREDUCED if supported. If not it should be VICARE_MODE_OFF or VICARE_MODE_DHW --- homeassistant/components/vicare/climate.py | 46 +++++++++++++++------- 1 file changed, 31 insertions(+), 15 deletions(-) diff --git a/homeassistant/components/vicare/climate.py b/homeassistant/components/vicare/climate.py index 4773101f1b94e4..fb8e60c3318abb 100644 --- a/homeassistant/components/vicare/climate.py +++ b/homeassistant/components/vicare/climate.py @@ -71,19 +71,13 @@ VICARE_TEMP_HEATING_MAX = 37 VICARE_TO_HA_HVAC_HEATING = { + VICARE_MODE_FORCEDREDUCED: HVACMode.OFF, + VICARE_MODE_OFF: HVACMode.OFF, VICARE_MODE_DHW: HVACMode.OFF, - VICARE_MODE_HEATING: HVACMode.HEAT, - VICARE_MODE_DHWANDHEATING: HVACMode.AUTO, VICARE_MODE_DHWANDHEATINGCOOLING: HVACMode.AUTO, - VICARE_MODE_FORCEDREDUCED: HVACMode.OFF, + VICARE_MODE_DHWANDHEATING: HVACMode.AUTO, + VICARE_MODE_HEATING: HVACMode.AUTO, VICARE_MODE_FORCEDNORMAL: HVACMode.HEAT, - VICARE_MODE_OFF: HVACMode.OFF, -} - -HA_TO_VICARE_HVAC_HEATING = { - HVACMode.HEAT: VICARE_MODE_FORCEDNORMAL, - HVACMode.OFF: VICARE_MODE_FORCEDREDUCED, - HVACMode.AUTO: VICARE_MODE_DHWANDHEATING, } VICARE_TO_HA_PRESET_HEATING = { @@ -276,19 +270,41 @@ def hvac_mode(self) -> HVACMode | None: def set_hvac_mode(self, hvac_mode: HVACMode) -> None: """Set a new hvac mode on the ViCare API.""" - vicare_mode = HA_TO_VICARE_HVAC_HEATING.get(hvac_mode) + if "vicare_modes" not in self._attributes: + raise ValueError("Cannot set hvac mode when vicare_modes are not known") + + vicare_mode = self.vicare_mode_from_hvac_mode(hvac_mode) if vicare_mode is None: - raise ValueError( - f"Cannot set invalid vicare mode: {hvac_mode} / {vicare_mode}" - ) + raise ValueError(f"Cannot set invalid hvac mode: {hvac_mode}") _LOGGER.debug("Setting hvac mode to %s / %s", hvac_mode, vicare_mode) self._circuit.setMode(vicare_mode) + def vicare_mode_from_hvac_mode(self, hvac_mode): + """Return the corresponding vicare mode for an hvac_mode.""" + if "vicare_modes" not in self._attributes: + return None + + supported_modes = self._attributes["vicare_modes"] + for key, value in VICARE_TO_HA_HVAC_HEATING.items(): + if key in supported_modes and value == hvac_mode: + return key + return None + @property def hvac_modes(self) -> list[HVACMode]: """Return the list of available hvac modes.""" - return list(HA_TO_VICARE_HVAC_HEATING) + if "vicare_modes" not in self._attributes: + return [] + + supported_modes = self._attributes["vicare_modes"] + hvac_modes = [] + for key, value in VICARE_TO_HA_HVAC_HEATING.items(): + if value in hvac_modes: + continue + if key in supported_modes: + hvac_modes.append(value) + return hvac_modes @property def hvac_action(self) -> HVACAction: