Skip to content

Commit

Permalink
Replace Climate CURRENT_HVAC_* constants with HVACAction enum (#70319)
Browse files Browse the repository at this point in the history
  • Loading branch information
frenck authored Apr 20, 2022
1 parent 9e213ca commit bfc82b0
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 31 deletions.
10 changes: 4 additions & 6 deletions homeassistant/components/climate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
SUPPORT_TARGET_TEMPERATURE,
SUPPORT_TARGET_TEMPERATURE_RANGE,
ClimateEntityFeature,
HVACAction,
HVACMode,
)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -345,11 +346,8 @@ def hvac_modes(self) -> list[HVACMode | str]:
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
Expand Down
26 changes: 14 additions & 12 deletions homeassistant/components/climate/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,24 +87,26 @@ class HVACMode(StrEnum):
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"
Expand Down
7 changes: 3 additions & 4 deletions homeassistant/components/demo/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
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
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
10 changes: 5 additions & 5 deletions tests/components/climate/test_device_trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down Expand Up @@ -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,
},
Expand Down Expand Up @@ -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,
},
Expand All @@ -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,
},
Expand All @@ -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,
},
Expand Down
6 changes: 3 additions & 3 deletions tests/components/demo/test_climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
ATTR_SWING_MODE,
ATTR_TARGET_TEMP_HIGH,
ATTR_TARGET_TEMP_LOW,
CURRENT_HVAC_COOL,
DOMAIN,
PRESET_AWAY,
PRESET_ECO,
Expand All @@ -31,6 +30,7 @@
SERVICE_SET_PRESET_MODE,
SERVICE_SET_SWING_MODE,
SERVICE_SET_TEMPERATURE,
HVACAction,
HVACMode,
)
from homeassistant.const import (
Expand Down Expand Up @@ -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):
Expand All @@ -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


Expand Down
2 changes: 1 addition & 1 deletion tests/components/mqtt/test_climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down

0 comments on commit bfc82b0

Please sign in to comment.