From 9831da34cea3ea070be2c0f584a1133bd002004b Mon Sep 17 00:00:00 2001 From: G Johansson Date: Sun, 4 Feb 2024 08:57:26 -0500 Subject: [PATCH] Add new climate feature flags to esphome (#109428) --- homeassistant/components/esphome/climate.py | 3 + .../esphome/snapshots/test_climate.ambr | 38 +++++++++++++ tests/components/esphome/test_climate.py | 57 +++++++++++++++++++ 3 files changed, 98 insertions(+) create mode 100644 tests/components/esphome/snapshots/test_climate.ambr diff --git a/homeassistant/components/esphome/climate.py b/homeassistant/components/esphome/climate.py index 5c265068216..9c2177800f3 100644 --- a/homeassistant/components/esphome/climate.py +++ b/homeassistant/components/esphome/climate.py @@ -137,6 +137,7 @@ class EsphomeClimateEntity(EsphomeEntity[ClimateInfo, ClimateState], ClimateEnti _attr_temperature_unit = UnitOfTemperature.CELSIUS _attr_translation_key = "climate" + _enable_turn_on_off_backwards_compatibility = False @callback def _on_static_info_update(self, static_info: EntityInfo) -> None: @@ -179,6 +180,8 @@ class EsphomeClimateEntity(EsphomeEntity[ClimateInfo, ClimateState], ClimateEnti features |= ClimateEntityFeature.FAN_MODE if self.swing_modes: features |= ClimateEntityFeature.SWING_MODE + if len(self.hvac_modes) > 1 and HVACMode.OFF in self.hvac_modes: + features |= ClimateEntityFeature.TURN_OFF | ClimateEntityFeature.TURN_ON self._attr_supported_features = features def _get_precision(self) -> float: diff --git a/tests/components/esphome/snapshots/test_climate.ambr b/tests/components/esphome/snapshots/test_climate.ambr new file mode 100644 index 00000000000..69d721ecb94 --- /dev/null +++ b/tests/components/esphome/snapshots/test_climate.ambr @@ -0,0 +1,38 @@ +# serializer version: 1 +# name: test_climate_entity_attributes[climate-entity-attributes] + ReadOnlyDict({ + 'current_temperature': 30, + 'fan_mode': 'auto', + 'fan_modes': list([ + 'low', + 'high', + 'fan1', + 'fan2', + ]), + 'friendly_name': 'Test my climate', + 'hvac_action': , + 'hvac_modes': list([ + , + , + , + , + ]), + 'max_temp': 30, + 'min_temp': 10, + 'preset_mode': 'none', + 'preset_modes': list([ + 'away', + 'activity', + 'preset1', + 'preset2', + ]), + 'supported_features': , + 'swing_mode': 'both', + 'swing_modes': list([ + 'both', + 'off', + ]), + 'target_temp_step': 2, + 'temperature': 20, + }) +# --- diff --git a/tests/components/esphome/test_climate.py b/tests/components/esphome/test_climate.py index cb9a084d094..dbdee826137 100644 --- a/tests/components/esphome/test_climate.py +++ b/tests/components/esphome/test_climate.py @@ -14,6 +14,7 @@ from aioesphomeapi import ( ClimateState, ClimateSwingMode, ) +from syrupy import SnapshotAssertion from homeassistant.components.climate import ( ATTR_CURRENT_HUMIDITY, @@ -432,3 +433,59 @@ async def test_climate_entity_with_inf_value( assert attributes[ATTR_MIN_HUMIDITY] == 10 assert ATTR_TEMPERATURE not in attributes assert attributes[ATTR_CURRENT_TEMPERATURE] is None + + +async def test_climate_entity_attributes( + hass: HomeAssistant, + mock_client: APIClient, + mock_generic_device_entry, + snapshot: SnapshotAssertion, +) -> None: + """Test a climate entity sets correct attributes.""" + entity_info = [ + ClimateInfo( + object_id="myclimate", + key=1, + name="my climate", + unique_id="my_climate", + supports_current_temperature=True, + visual_target_temperature_step=2, + visual_current_temperature_step=2, + supports_action=True, + visual_min_temperature=10.0, + visual_max_temperature=30.0, + supported_fan_modes=[ClimateFanMode.LOW, ClimateFanMode.HIGH], + supported_modes=[ + ClimateMode.COOL, + ClimateMode.HEAT, + ClimateMode.AUTO, + ClimateMode.OFF, + ], + supported_presets=[ClimatePreset.AWAY, ClimatePreset.ACTIVITY], + supported_custom_presets=["preset1", "preset2"], + supported_custom_fan_modes=["fan1", "fan2"], + supported_swing_modes=[ClimateSwingMode.BOTH, ClimateSwingMode.OFF], + ) + ] + states = [ + ClimateState( + key=1, + mode=ClimateMode.COOL, + action=ClimateAction.COOLING, + current_temperature=30, + target_temperature=20, + fan_mode=ClimateFanMode.AUTO, + swing_mode=ClimateSwingMode.BOTH, + ) + ] + user_service = [] + await mock_generic_device_entry( + mock_client=mock_client, + entity_info=entity_info, + user_service=user_service, + states=states, + ) + state = hass.states.get("climate.test_myclimate") + assert state is not None + assert state.state == HVACMode.COOL + assert state.attributes == snapshot(name="climate-entity-attributes")