diff --git a/homeassistant/components/climate/device_condition.py b/homeassistant/components/climate/device_condition.py index 6252de6e491..dd5842cd2a8 100644 --- a/homeassistant/components/climate/device_condition.py +++ b/homeassistant/components/climate/device_condition.py @@ -75,15 +75,19 @@ def async_condition_from_config( hass: HomeAssistant, config: ConfigType ) -> condition.ConditionCheckerType: """Create a function to test a device condition.""" - if config[CONF_TYPE] == "is_hvac_mode": - attribute = const.ATTR_HVAC_MODE - else: - attribute = const.ATTR_PRESET_MODE def test_is_state(hass: HomeAssistant, variables: TemplateVarsType) -> bool: """Test if an entity is a certain state.""" - state = hass.states.get(config[ATTR_ENTITY_ID]) - return state.attributes.get(attribute) == config[attribute] if state else False + if (state := hass.states.get(config[ATTR_ENTITY_ID])) is None: + return False + + if config[CONF_TYPE] == "is_hvac_mode": + return state.state == config[const.ATTR_HVAC_MODE] + + return ( + state.attributes.get(const.ATTR_PRESET_MODE) + == config[const.ATTR_PRESET_MODE] + ) return test_is_state diff --git a/tests/components/climate/test_device_condition.py b/tests/components/climate/test_device_condition.py index 65c1e17048b..ca3f4388c0a 100644 --- a/tests/components/climate/test_device_condition.py +++ b/tests/components/climate/test_device_condition.py @@ -92,15 +92,6 @@ async def test_get_conditions( async def test_if_state(hass, calls): """Test for turn_on and turn_off conditions.""" - hass.states.async_set( - "climate.entity", - const.HVAC_MODE_COOL, - { - const.ATTR_HVAC_MODE: const.HVAC_MODE_COOL, - const.ATTR_PRESET_MODE: const.PRESET_AWAY, - }, - ) - assert await async_setup_component( hass, automation.DOMAIN, @@ -147,6 +138,20 @@ async def test_if_state(hass, calls): ] }, ) + + # Should not fire, entity doesn't exist yet + hass.bus.async_fire("test_event1") + await hass.async_block_till_done() + assert len(calls) == 0 + + hass.states.async_set( + "climate.entity", + const.HVAC_MODE_COOL, + { + const.ATTR_PRESET_MODE: const.PRESET_AWAY, + }, + ) + hass.bus.async_fire("test_event1") await hass.async_block_till_done() assert len(calls) == 1 @@ -156,7 +161,6 @@ async def test_if_state(hass, calls): "climate.entity", const.HVAC_MODE_AUTO, { - const.ATTR_HVAC_MODE: const.HVAC_MODE_AUTO, const.ATTR_PRESET_MODE: const.PRESET_AWAY, }, ) @@ -176,7 +180,6 @@ async def test_if_state(hass, calls): "climate.entity", const.HVAC_MODE_AUTO, { - const.ATTR_HVAC_MODE: const.HVAC_MODE_AUTO, const.ATTR_PRESET_MODE: const.PRESET_HOME, }, )