From 457cb7ace05c87e58dc29623be2119a375a54170 Mon Sep 17 00:00:00 2001 From: Roelf Zomerman Date: Tue, 10 Sep 2024 20:10:52 +0400 Subject: [PATCH] Add velbus HVAC options (#106570) * Added HVAC options * Update manifest.json required aio to 2023.12.0 * Update manifest.json * Add files via upload * Update homeassistant/components/velbus/climate.py Co-authored-by: Joost Lekkerkerker * Update climate.py removed unused variables for cool and heat * Update climate.py removed unused functions * Update homeassistant/components/velbus/climate.py Co-authored-by: Erik Montnemery * Update climate.py accepted changes * Update climate.py remove state None for HVAC-MODE * Update climate.py changed set_hvac_mode to remove none and only switch when state /= requested mode * Update climate.py indent on line 94/95 * Update climate.py changed set_hvac_mode attribute type to match superclass ClimateEntity (HVACMode) * Update climate.py changed def hvac_mode to 2 return options (to avoid any) * Update climate.py ruff formatting * Update climate.py added serviceValidationError section in hvac_mode setting * Update climate.py * Update climate.py * Update climate.py * Update climate.py * Update climate.py * Update climate.py * Update climate.py * Update climate.py * Update strings.json * Update strings.json * Apply suggestions from code review --------- Co-authored-by: Joost Lekkerkerker Co-authored-by: Erik Montnemery --- homeassistant/components/velbus/climate.py | 21 ++++++++++++++++++-- homeassistant/components/velbus/strings.json | 5 +++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/velbus/climate.py b/homeassistant/components/velbus/climate.py index 34a565c2b37..ed47d8b0a91 100644 --- a/homeassistant/components/velbus/climate.py +++ b/homeassistant/components/velbus/climate.py @@ -14,6 +14,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 ServiceValidationError from homeassistant.helpers.entity_platform import AddEntitiesCallback from .const import DOMAIN, PRESET_MODES @@ -39,8 +40,7 @@ class VelbusClimate(VelbusEntity, ClimateEntity): ClimateEntityFeature.TARGET_TEMPERATURE | ClimateEntityFeature.PRESET_MODE ) _attr_temperature_unit = UnitOfTemperature.CELSIUS - _attr_hvac_mode = HVACMode.HEAT - _attr_hvac_modes = [HVACMode.HEAT] + _attr_hvac_modes = [HVACMode.HEAT, HVACMode.COOL] _attr_preset_modes = list(PRESET_MODES) _enable_turn_on_off_backwards_compatibility = False @@ -66,6 +66,11 @@ class VelbusClimate(VelbusEntity, ClimateEntity): """Return the current temperature.""" return self._channel.get_state() + @property + def hvac_mode(self) -> HVACMode: + """Return the current hvac mode based on cool_mode message.""" + return HVACMode.COOL if self._channel.get_cool_mode() else HVACMode.HEAT + @api_call async def async_set_temperature(self, **kwargs: Any) -> None: """Set new target temperatures.""" @@ -79,3 +84,15 @@ class VelbusClimate(VelbusEntity, ClimateEntity): """Set the new preset mode.""" await self._channel.set_preset(PRESET_MODES[preset_mode]) self.async_write_ha_state() + + @api_call + async def async_set_hvac_mode(self, hvac_mode: HVACMode) -> None: + """Set the new hvac mode.""" + if hvac_mode not in self._attr_hvac_modes: + raise ServiceValidationError( + translation_domain=DOMAIN, + translation_key="invalid_hvac_mode", + translation_placeholders={"hvac_mode": str(hvac_mode)}, + ) + await self._channel.set_mode(hvac_mode) + self.async_write_ha_state() diff --git a/homeassistant/components/velbus/strings.json b/homeassistant/components/velbus/strings.json index 948c079444d..55c7fda84ac 100644 --- a/homeassistant/components/velbus/strings.json +++ b/homeassistant/components/velbus/strings.json @@ -17,6 +17,11 @@ "already_configured": "[%key:common::config_flow::abort::already_configured_device%]" } }, + "exceptions": { + "invalid_hvac_mode": { + "message": "Climate mode {hvac_mode} is not supported." + } + }, "services": { "sync_clock": { "name": "Sync clock",