Teach lock device trigger about entity registry ids (#94975)

This commit is contained in:
Erik Montnemery 2023-06-21 14:51:09 +02:00 committed by GitHub
parent 20be441c9f
commit e404441e8c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 121 additions and 35 deletions

View file

@ -53,7 +53,7 @@ async def test_get_triggers(
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_triggers = [
@ -62,7 +62,7 @@ async def test_get_triggers(
"domain": DOMAIN,
"type": trigger,
"device_id": device_entry.id,
"entity_id": f"{DOMAIN}.test_5678",
"entity_id": entity_entry.id,
"metadata": {"secondary": False},
}
for trigger in ["locked", "unlocked", "unlocking", "locking", "jammed"]
@ -96,7 +96,7 @@ async def test_get_triggers_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",
@ -110,7 +110,7 @@ async def test_get_triggers_hidden_auxiliary(
"domain": DOMAIN,
"type": trigger,
"device_id": device_entry.id,
"entity_id": f"{DOMAIN}.test_5678",
"entity_id": entity_entry.id,
"metadata": {"secondary": True},
}
for trigger in ["locked", "unlocked", "unlocking", "locking", "jammed"]
@ -152,9 +152,45 @@ async def test_get_trigger_capabilities(
}
async def test_if_fires_on_state_change(hass: HomeAssistant, calls) -> None:
async def test_get_trigger_capabilities_legacy(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry,
) -> None:
"""Test we get the expected capabilities from a lock."""
config_entry = MockConfigEntry(domain="test", data={})
config_entry.add_to_hass(hass)
device_entry = device_registry.async_get_or_create(
config_entry_id=config_entry.entry_id,
connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
)
entity_registry.async_get_or_create(
DOMAIN, "test", "5678", device_id=device_entry.id
)
triggers = await async_get_device_automations(
hass, DeviceAutomationType.TRIGGER, device_entry.id
)
assert len(triggers) == 5
for trigger in triggers:
trigger["entity_id"] = entity_registry.async_get(trigger["entity_id"]).entity_id
capabilities = await async_get_device_automation_capabilities(
hass, DeviceAutomationType.TRIGGER, trigger
)
assert capabilities == {
"extra_fields": [
{"name": "for", "optional": True, "type": "positive_time_period_dict"}
]
}
async def test_if_fires_on_state_change(
hass: HomeAssistant, entity_registry: er.EntityRegistry, calls
) -> None:
"""Test for turn_on and turn_off triggers firing."""
hass.states.async_set("lock.entity", STATE_UNLOCKED)
entry = entity_registry.async_get_or_create(DOMAIN, "test", "5678")
hass.states.async_set(entry.entity_id, STATE_UNLOCKED)
assert await async_setup_component(
hass,
@ -166,7 +202,7 @@ async def test_if_fires_on_state_change(hass: HomeAssistant, calls) -> None:
"platform": "device",
"domain": DOMAIN,
"device_id": "",
"entity_id": "lock.entity",
"entity_id": entry.id,
"type": "locked",
},
"action": {
@ -185,7 +221,7 @@ async def test_if_fires_on_state_change(hass: HomeAssistant, calls) -> None:
"platform": "device",
"domain": DOMAIN,
"device_id": "",
"entity_id": "lock.entity",
"entity_id": entry.id,
"type": "unlocked",
},
"action": {
@ -204,26 +240,31 @@ async def test_if_fires_on_state_change(hass: HomeAssistant, calls) -> None:
)
# Fake that the entity is turning on.
hass.states.async_set("lock.entity", STATE_LOCKED)
hass.states.async_set(entry.entity_id, STATE_LOCKED)
await hass.async_block_till_done()
assert len(calls) == 1
assert calls[0].data[
"some"
] == "locked - device - {} - unlocked - locked - None".format("lock.entity")
assert (
calls[0].data["some"]
== f"locked - device - {entry.entity_id} - unlocked - locked - None"
)
# Fake that the entity is turning off.
hass.states.async_set("lock.entity", STATE_UNLOCKED)
hass.states.async_set(entry.entity_id, STATE_UNLOCKED)
await hass.async_block_till_done()
assert len(calls) == 2
assert calls[1].data[
"some"
] == "unlocked - device - {} - locked - unlocked - None".format("lock.entity")
assert (
calls[1].data["some"]
== f"unlocked - device - {entry.entity_id} - locked - unlocked - None"
)
async def test_if_fires_on_state_change_with_for(hass: HomeAssistant, calls) -> None:
"""Test for triggers firing with delay."""
entity_id = f"{DOMAIN}.entity"
hass.states.async_set(entity_id, STATE_UNLOCKED)
async def test_if_fires_on_state_change_legacy(
hass: HomeAssistant, entity_registry: er.EntityRegistry, calls
) -> None:
"""Test for turn_on and turn_off triggers firing."""
entry = entity_registry.async_get_or_create(DOMAIN, "test", "5678")
hass.states.async_set(entry.entity_id, STATE_UNLOCKED)
assert await async_setup_component(
hass,
@ -235,7 +276,53 @@ async def test_if_fires_on_state_change_with_for(hass: HomeAssistant, calls) ->
"platform": "device",
"domain": DOMAIN,
"device_id": "",
"entity_id": entity_id,
"entity_id": entry.entity_id,
"type": "locked",
},
"action": {
"service": "test.automation",
"data_template": {
"some": (
"locked - {{ trigger.platform}} - "
"{{ trigger.entity_id}} - {{ trigger.from_state.state}} - "
"{{ trigger.to_state.state}} - {{ trigger.for }}"
)
},
},
},
]
},
)
# Fake that the entity is turning on.
hass.states.async_set(entry.entity_id, STATE_LOCKED)
await hass.async_block_till_done()
assert len(calls) == 1
assert (
calls[0].data["some"]
== f"locked - device - {entry.entity_id} - unlocked - locked - None"
)
async def test_if_fires_on_state_change_with_for(
hass: HomeAssistant, entity_registry: er.EntityRegistry, calls
) -> None:
"""Test for triggers firing with delay."""
entry = entity_registry.async_get_or_create(DOMAIN, "test", "5678")
hass.states.async_set(entry.entity_id, STATE_UNLOCKED)
assert await async_setup_component(
hass,
automation.DOMAIN,
{
automation.DOMAIN: [
{
"trigger": {
"platform": "device",
"domain": DOMAIN,
"device_id": "",
"entity_id": entry.id,
"type": "locked",
"for": {"seconds": 5},
},
@ -260,7 +347,7 @@ async def test_if_fires_on_state_change_with_for(hass: HomeAssistant, calls) ->
"platform": "device",
"domain": DOMAIN,
"device_id": "",
"entity_id": entity_id,
"entity_id": entry.id,
"type": "unlocking",
"for": {"seconds": 5},
},
@ -285,7 +372,7 @@ async def test_if_fires_on_state_change_with_for(hass: HomeAssistant, calls) ->
"platform": "device",
"domain": DOMAIN,
"device_id": "",
"entity_id": entity_id,
"entity_id": entry.id,
"type": "jammed",
"for": {"seconds": 5},
},
@ -310,7 +397,7 @@ async def test_if_fires_on_state_change_with_for(hass: HomeAssistant, calls) ->
"platform": "device",
"domain": DOMAIN,
"device_id": "",
"entity_id": entity_id,
"entity_id": entry.id,
"type": "locking",
"for": {"seconds": 5},
},
@ -334,10 +421,9 @@ async def test_if_fires_on_state_change_with_for(hass: HomeAssistant, calls) ->
},
)
await hass.async_block_till_done()
assert hass.states.get(entity_id).state == STATE_UNLOCKED
assert len(calls) == 0
hass.states.async_set(entity_id, STATE_LOCKED)
hass.states.async_set(entry.entity_id, STATE_LOCKED)
await hass.async_block_till_done()
assert len(calls) == 0
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=10))
@ -346,10 +432,10 @@ async def test_if_fires_on_state_change_with_for(hass: HomeAssistant, calls) ->
await hass.async_block_till_done()
assert (
calls[0].data["some"]
== f"turn_off device - {entity_id} - unlocked - locked - 0:00:05"
== f"turn_off device - {entry.entity_id} - unlocked - locked - 0:00:05"
)
hass.states.async_set(entity_id, STATE_UNLOCKING)
hass.states.async_set(entry.entity_id, STATE_UNLOCKING)
await hass.async_block_till_done()
assert len(calls) == 1
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=16))
@ -358,10 +444,10 @@ async def test_if_fires_on_state_change_with_for(hass: HomeAssistant, calls) ->
await hass.async_block_till_done()
assert (
calls[1].data["some"]
== f"turn_on device - {entity_id} - locked - unlocking - 0:00:05"
== f"turn_on device - {entry.entity_id} - locked - unlocking - 0:00:05"
)
hass.states.async_set(entity_id, STATE_JAMMED)
hass.states.async_set(entry.entity_id, STATE_JAMMED)
await hass.async_block_till_done()
assert len(calls) == 2
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=21))
@ -370,10 +456,10 @@ async def test_if_fires_on_state_change_with_for(hass: HomeAssistant, calls) ->
await hass.async_block_till_done()
assert (
calls[2].data["some"]
== f"turn_off device - {entity_id} - unlocking - jammed - 0:00:05"
== f"turn_off device - {entry.entity_id} - unlocking - jammed - 0:00:05"
)
hass.states.async_set(entity_id, STATE_LOCKING)
hass.states.async_set(entry.entity_id, STATE_LOCKING)
await hass.async_block_till_done()
assert len(calls) == 3
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=27))
@ -382,5 +468,5 @@ async def test_if_fires_on_state_change_with_for(hass: HomeAssistant, calls) ->
await hass.async_block_till_done()
assert (
calls[3].data["some"]
== f"turn_on device - {entity_id} - jammed - locking - 0:00:05"
== f"turn_on device - {entry.entity_id} - jammed - locking - 0:00:05"
)