From bfc82b030f1cc31ede2ca18a989c2d3f8961c565 Mon Sep 17 00:00:00 2001 From: Franck Nijhof <git@frenck.dev> Date: Wed, 20 Apr 2022 22:41:05 +0200 Subject: [PATCH] Replace Climate CURRENT_HVAC_* constants with HVACAction enum (#70319) --- homeassistant/components/climate/__init__.py | 10 +++---- homeassistant/components/climate/const.py | 26 ++++++++++--------- homeassistant/components/demo/climate.py | 7 +++-- .../components/climate/test_device_trigger.py | 10 +++---- tests/components/demo/test_climate.py | 6 ++--- tests/components/mqtt/test_climate.py | 2 +- 6 files changed, 30 insertions(+), 31 deletions(-) diff --git a/homeassistant/components/climate/__init__.py b/homeassistant/components/climate/__init__.py index 5c8f27bc518..91681c9566a 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 8552df943e4..202da1af597 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 ff6a535f4f9..a4d6ca6da07 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 523a9f04a8b..91691d130a5 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 1d130203bb1..332d2bf4f2e 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 1228b0c575b..0c1db16d6fe 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 ) -- GitLab