diff --git a/homeassistant/components/climate/__init__.py b/homeassistant/components/climate/__init__.py index 5c8f27bc5188f84af971dda587ba68570dd38734..91681c9566ad8b59d34ac35ac888958b9796727d 100644 --- a/homeassistant/components/climate/__init__.py +++ b/homeassistant/components/climate/__init__.py @@ -75,6 +75,7 @@ from .const import ( # noqa: F401 SUPPORT_TARGET_TEMPERATURE, SUPPORT_TARGET_TEMPERATURE_RANGE, ClimateEntityFeature, + HVACAction, HVACMode, ) @@ -188,7 +189,7 @@ class ClimateEntity(Entity): _attr_current_temperature: float | None = None _attr_fan_mode: str | None _attr_fan_modes: list[str] | None - _attr_hvac_action: str | None = None + _attr_hvac_action: HVACAction | str | None = None _attr_hvac_mode: HVACMode | str | None _attr_hvac_modes: list[HVACMode | str] _attr_is_aux_heat: bool | None @@ -345,11 +346,8 @@ class ClimateEntity(Entity): return self._attr_hvac_modes @property - def hvac_action(self) -> str | None: - """Return the current running hvac operation if supported. - - Need to be one of CURRENT_HVAC_*. - """ + def hvac_action(self) -> HVACAction | str | None: + """Return the current running hvac operation if supported.""" return self._attr_hvac_action @property diff --git a/homeassistant/components/climate/const.py b/homeassistant/components/climate/const.py index 8552df943e42153b93a4683021f49c00c0e08e4b..202da1af597bc8f46e72df833f2fb0968f0a2618 100644 --- a/homeassistant/components/climate/const.py +++ b/homeassistant/components/climate/const.py @@ -87,24 +87,26 @@ SWING_VERTICAL = "vertical" SWING_HORIZONTAL = "horizontal" -# This are support current states of HVAC +class HVACAction(StrEnum): + """HVAC action for climate devices.""" + + COOLING = "cooling" + DRYING = "drying" + FAN = "fan" + HEATING = "heating" + IDLE = "idle" + OFF = "off" + + +# These CURRENT_HVAC_* constants are deprecated as of Home Assistant 2022.5. +# Please use the HVACAction enum instead. CURRENT_HVAC_OFF = "off" CURRENT_HVAC_HEAT = "heating" CURRENT_HVAC_COOL = "cooling" CURRENT_HVAC_DRY = "drying" CURRENT_HVAC_IDLE = "idle" CURRENT_HVAC_FAN = "fan" - - -# A list of possible HVAC actions. -CURRENT_HVAC_ACTIONS = [ - CURRENT_HVAC_OFF, - CURRENT_HVAC_HEAT, - CURRENT_HVAC_COOL, - CURRENT_HVAC_DRY, - CURRENT_HVAC_IDLE, - CURRENT_HVAC_FAN, -] +CURRENT_HVAC_ACTIONS = [cls.value for cls in HVACAction] ATTR_AUX_HEAT = "aux_heat" diff --git a/homeassistant/components/demo/climate.py b/homeassistant/components/demo/climate.py index ff6a535f4f95a11400ac2a41161336bb7c9984da..a4d6ca6da071a24e47a1c72f83aa03b1c2502583 100644 --- a/homeassistant/components/demo/climate.py +++ b/homeassistant/components/demo/climate.py @@ -5,9 +5,8 @@ from homeassistant.components.climate import ClimateEntity from homeassistant.components.climate.const import ( ATTR_TARGET_TEMP_HIGH, ATTR_TARGET_TEMP_LOW, - CURRENT_HVAC_COOL, - CURRENT_HVAC_HEAT, ClimateEntityFeature, + HVACAction, HVACMode, ) from homeassistant.config_entries import ConfigEntry @@ -43,7 +42,7 @@ async def async_setup_platform( current_humidity=None, swing_mode=None, hvac_mode=HVACMode.HEAT, - hvac_action=CURRENT_HVAC_HEAT, + hvac_action=HVACAction.HEATING, aux=None, target_temp_high=None, target_temp_low=None, @@ -61,7 +60,7 @@ async def async_setup_platform( current_humidity=54, swing_mode="Off", hvac_mode=HVACMode.COOL, - hvac_action=CURRENT_HVAC_COOL, + hvac_action=HVACAction.COOLING, aux=False, target_temp_high=None, target_temp_low=None, diff --git a/tests/components/climate/test_device_trigger.py b/tests/components/climate/test_device_trigger.py index 523a9f04a8b4041b645c7bba6af85046745f9ce5..91691d130a54641d35fff24012b39cde33c71e53 100644 --- a/tests/components/climate/test_device_trigger.py +++ b/tests/components/climate/test_device_trigger.py @@ -52,7 +52,7 @@ async def test_get_triggers(hass, device_reg, entity_reg): entity_id, const.HVAC_MODE_COOL, { - const.ATTR_HVAC_ACTION: const.CURRENT_HVAC_IDLE, + const.ATTR_HVAC_ACTION: const.HVACAction.IDLE, const.ATTR_CURRENT_HUMIDITY: 23, const.ATTR_CURRENT_TEMPERATURE: 18, }, @@ -92,7 +92,7 @@ async def test_if_fires_on_state_change(hass, calls): "climate.entity", const.HVAC_MODE_COOL, { - const.ATTR_HVAC_ACTION: const.CURRENT_HVAC_IDLE, + const.ATTR_HVAC_ACTION: const.HVACAction.IDLE, const.ATTR_CURRENT_HUMIDITY: 23, const.ATTR_CURRENT_TEMPERATURE: 18, }, @@ -154,7 +154,7 @@ async def test_if_fires_on_state_change(hass, calls): "climate.entity", const.HVAC_MODE_AUTO, { - const.ATTR_HVAC_ACTION: const.CURRENT_HVAC_COOL, + const.ATTR_HVAC_ACTION: const.HVACAction.COOLING, const.ATTR_CURRENT_HUMIDITY: 23, const.ATTR_CURRENT_TEMPERATURE: 18, }, @@ -168,7 +168,7 @@ async def test_if_fires_on_state_change(hass, calls): "climate.entity", const.HVAC_MODE_AUTO, { - const.ATTR_HVAC_ACTION: const.CURRENT_HVAC_COOL, + const.ATTR_HVAC_ACTION: const.HVACAction.COOLING, const.ATTR_CURRENT_HUMIDITY: 23, const.ATTR_CURRENT_TEMPERATURE: 23, }, @@ -182,7 +182,7 @@ async def test_if_fires_on_state_change(hass, calls): "climate.entity", const.HVAC_MODE_AUTO, { - const.ATTR_HVAC_ACTION: const.CURRENT_HVAC_COOL, + const.ATTR_HVAC_ACTION: const.HVACAction.COOLING, const.ATTR_CURRENT_HUMIDITY: 7, const.ATTR_CURRENT_TEMPERATURE: 23, }, diff --git a/tests/components/demo/test_climate.py b/tests/components/demo/test_climate.py index 1d130203bb1ef12021e8c3c76e33f7d372ebb1dd..332d2bf4f2e11016de7717eaf8db4393bcac4631 100644 --- a/tests/components/demo/test_climate.py +++ b/tests/components/demo/test_climate.py @@ -20,7 +20,6 @@ from homeassistant.components.climate.const import ( ATTR_SWING_MODE, ATTR_TARGET_TEMP_HIGH, ATTR_TARGET_TEMP_LOW, - CURRENT_HVAC_COOL, DOMAIN, PRESET_AWAY, PRESET_ECO, @@ -31,6 +30,7 @@ from homeassistant.components.climate.const import ( SERVICE_SET_PRESET_MODE, SERVICE_SET_SWING_MODE, SERVICE_SET_TEMPERATURE, + HVACAction, HVACMode, ) from homeassistant.const import ( @@ -290,7 +290,7 @@ async def test_set_hvac_bad_attr_and_state(hass): Also check the state. """ state = hass.states.get(ENTITY_CLIMATE) - assert state.attributes.get(ATTR_HVAC_ACTION) == CURRENT_HVAC_COOL + assert state.attributes.get(ATTR_HVAC_ACTION) == HVACAction.COOLING assert state.state == HVACMode.COOL with pytest.raises(vol.Invalid): @@ -302,7 +302,7 @@ async def test_set_hvac_bad_attr_and_state(hass): ) state = hass.states.get(ENTITY_CLIMATE) - assert state.attributes.get(ATTR_HVAC_ACTION) == CURRENT_HVAC_COOL + assert state.attributes.get(ATTR_HVAC_ACTION) == HVACAction.COOLING assert state.state == HVACMode.COOL diff --git a/tests/components/mqtt/test_climate.py b/tests/components/mqtt/test_climate.py index 1228b0c575b3965167866c2ebbf66710e6e6b2bf..0c1db16d6fe209b93bd7f304ae190ee8df476134 100644 --- a/tests/components/mqtt/test_climate.py +++ b/tests/components/mqtt/test_climate.py @@ -1161,7 +1161,7 @@ async def test_get_with_templates(hass, mqtt_mock, caplog): state = hass.states.get(ENTITY_CLIMATE) assert state.attributes.get("hvac_action") == "cooling" assert ( - "Invalid ['off', 'heating', 'cooling', 'drying', 'idle', 'fan'] action: None, ignoring" + "Invalid ['cooling', 'drying', 'fan', 'heating', 'idle', 'off'] action: None, ignoring" in caplog.text )