diff --git a/homeassistant/components/lock/device_condition.py b/homeassistant/components/lock/device_condition.py index c439fe99d14..5ba93554aec 100644 --- a/homeassistant/components/lock/device_condition.py +++ b/homeassistant/components/lock/device_condition.py @@ -39,7 +39,7 @@ CONDITION_TYPES = { CONDITION_SCHEMA = DEVICE_CONDITION_BASE_SCHEMA.extend( { - vol.Required(CONF_ENTITY_ID): cv.entity_id, + vol.Required(CONF_ENTITY_ID): cv.entity_id_or_uuid, vol.Required(CONF_TYPE): vol.In(CONDITION_TYPES), } ) @@ -62,7 +62,7 @@ async def async_get_conditions( CONF_CONDITION: "device", CONF_DEVICE_ID: device_id, CONF_DOMAIN: DOMAIN, - CONF_ENTITY_ID: entry.entity_id, + CONF_ENTITY_ID: entry.id, } conditions += [{**base_condition, CONF_TYPE: cond} for cond in CONDITION_TYPES] @@ -86,8 +86,11 @@ def async_condition_from_config( else: state = STATE_UNLOCKED + registry = er.async_get(hass) + entity_id = er.async_resolve_entity_id(registry, config[ATTR_ENTITY_ID]) + def test_is_state(hass: HomeAssistant, variables: TemplateVarsType) -> bool: """Test if an entity is a certain state.""" - return condition.state(hass, config[ATTR_ENTITY_ID], state) + return condition.state(hass, entity_id, state) return test_is_state diff --git a/tests/components/lock/test_device_condition.py b/tests/components/lock/test_device_condition.py index 765e5d3b5a7..43513930f2e 100644 --- a/tests/components/lock/test_device_condition.py +++ b/tests/components/lock/test_device_condition.py @@ -48,7 +48,7 @@ async def test_get_conditions( config_entry_id=config_entry.entry_id, connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")}, ) - entity_registry.async_get_or_create( + entity_entry = entity_registry.async_get_or_create( DOMAIN, "test", "5678", device_id=device_entry.id ) expected_conditions = [ @@ -57,7 +57,7 @@ async def test_get_conditions( "domain": DOMAIN, "type": condition, "device_id": device_entry.id, - "entity_id": f"{DOMAIN}.test_5678", + "entity_id": entity_entry.id, "metadata": {"secondary": False}, } for condition in [ @@ -97,7 +97,7 @@ async def test_get_conditions_hidden_auxiliary( config_entry_id=config_entry.entry_id, connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")}, ) - entity_registry.async_get_or_create( + entity_entry = entity_registry.async_get_or_create( DOMAIN, "test", "5678", @@ -111,7 +111,7 @@ async def test_get_conditions_hidden_auxiliary( "domain": DOMAIN, "type": condition, "device_id": device_entry.id, - "entity_id": f"{DOMAIN}.test_5678", + "entity_id": entity_entry.id, "metadata": {"secondary": True}, } for condition in [ @@ -128,9 +128,13 @@ async def test_get_conditions_hidden_auxiliary( assert conditions == unordered(expected_conditions) -async def test_if_state(hass: HomeAssistant, calls) -> None: +async def test_if_state( + hass: HomeAssistant, entity_registry: er.EntityRegistry, calls +) -> None: """Test for turn_on and turn_off conditions.""" - hass.states.async_set("lock.entity", STATE_LOCKED) + entry = entity_registry.async_get_or_create(DOMAIN, "test", "5678") + + hass.states.async_set(entry.entity_id, STATE_LOCKED) assert await async_setup_component( hass, @@ -144,7 +148,7 @@ async def test_if_state(hass: HomeAssistant, calls) -> None: "condition": "device", "domain": DOMAIN, "device_id": "", - "entity_id": "lock.entity", + "entity_id": entry.id, "type": "is_locked", } ], @@ -162,7 +166,7 @@ async def test_if_state(hass: HomeAssistant, calls) -> None: "condition": "device", "domain": DOMAIN, "device_id": "", - "entity_id": "lock.entity", + "entity_id": entry.id, "type": "is_unlocked", } ], @@ -180,7 +184,7 @@ async def test_if_state(hass: HomeAssistant, calls) -> None: "condition": "device", "domain": DOMAIN, "device_id": "", - "entity_id": "lock.entity", + "entity_id": entry.id, "type": "is_unlocking", } ], @@ -198,7 +202,7 @@ async def test_if_state(hass: HomeAssistant, calls) -> None: "condition": "device", "domain": DOMAIN, "device_id": "", - "entity_id": "lock.entity", + "entity_id": entry.id, "type": "is_locking", } ], @@ -216,7 +220,7 @@ async def test_if_state(hass: HomeAssistant, calls) -> None: "condition": "device", "domain": DOMAIN, "device_id": "", - "entity_id": "lock.entity", + "entity_id": entry.id, "type": "is_jammed", } ], @@ -236,27 +240,68 @@ async def test_if_state(hass: HomeAssistant, calls) -> None: assert len(calls) == 1 assert calls[0].data["some"] == "is_locked - event - test_event1" - hass.states.async_set("lock.entity", STATE_UNLOCKED) + hass.states.async_set(entry.entity_id, STATE_UNLOCKED) hass.bus.async_fire("test_event1") hass.bus.async_fire("test_event2") await hass.async_block_till_done() assert len(calls) == 2 assert calls[1].data["some"] == "is_unlocked - event - test_event2" - hass.states.async_set("lock.entity", STATE_UNLOCKING) + hass.states.async_set(entry.entity_id, STATE_UNLOCKING) hass.bus.async_fire("test_event3") await hass.async_block_till_done() assert len(calls) == 3 assert calls[2].data["some"] == "is_unlocking - event - test_event3" - hass.states.async_set("lock.entity", STATE_LOCKING) + hass.states.async_set(entry.entity_id, STATE_LOCKING) hass.bus.async_fire("test_event4") await hass.async_block_till_done() assert len(calls) == 4 assert calls[3].data["some"] == "is_locking - event - test_event4" - hass.states.async_set("lock.entity", STATE_JAMMED) + hass.states.async_set(entry.entity_id, STATE_JAMMED) hass.bus.async_fire("test_event5") await hass.async_block_till_done() assert len(calls) == 5 assert calls[4].data["some"] == "is_jammed - event - test_event5" + + +async def test_if_state_legacy( + hass: HomeAssistant, entity_registry: er.EntityRegistry, calls +) -> None: + """Test for turn_on and turn_off conditions.""" + entry = entity_registry.async_get_or_create(DOMAIN, "test", "5678") + + hass.states.async_set(entry.entity_id, STATE_LOCKED) + + assert await async_setup_component( + hass, + automation.DOMAIN, + { + automation.DOMAIN: [ + { + "trigger": {"platform": "event", "event_type": "test_event1"}, + "condition": [ + { + "condition": "device", + "domain": DOMAIN, + "device_id": "", + "entity_id": entry.entity_id, + "type": "is_locked", + } + ], + "action": { + "service": "test.automation", + "data_template": { + "some": "is_locked - {{ trigger.platform }} - {{ trigger.event.event_type }}" + }, + }, + }, + ] + }, + ) + hass.bus.async_fire("test_event1") + hass.bus.async_fire("test_event2") + await hass.async_block_till_done() + assert len(calls) == 1 + assert calls[0].data["some"] == "is_locked - event - test_event1"