Add new climate feature flags to esphome (#109428)

This commit is contained in:
G Johansson 2024-02-04 08:57:26 -05:00 committed by GitHub
parent baa511b808
commit 9831da34ce
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 98 additions and 0 deletions

View File

@ -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:

View File

@ -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': <HVACAction.COOLING: 'cooling'>,
'hvac_modes': list([
<HVACMode.COOL: 'cool'>,
<HVACMode.HEAT: 'heat'>,
<HVACMode.AUTO: 'auto'>,
<HVACMode.OFF: 'off'>,
]),
'max_temp': 30,
'min_temp': 10,
'preset_mode': 'none',
'preset_modes': list([
'away',
'activity',
'preset1',
'preset2',
]),
'supported_features': <ClimateEntityFeature: 441>,
'swing_mode': 'both',
'swing_modes': list([
'both',
'off',
]),
'target_temp_step': 2,
'temperature': 20,
})
# ---

View File

@ -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")