Teach lock device trigger about entity registry ids (#94975)
This commit is contained in:
parent
20be441c9f
commit
e404441e8c
2 changed files with 121 additions and 35 deletions
|
@ -29,7 +29,7 @@ TRIGGER_TYPES = {"locked", "unlocked", "locking", "unlocking", "jammed"}
|
||||||
|
|
||||||
TRIGGER_SCHEMA = DEVICE_TRIGGER_BASE_SCHEMA.extend(
|
TRIGGER_SCHEMA = DEVICE_TRIGGER_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(TRIGGER_TYPES),
|
vol.Required(CONF_TYPE): vol.In(TRIGGER_TYPES),
|
||||||
vol.Optional(CONF_FOR): cv.positive_time_period_dict,
|
vol.Optional(CONF_FOR): cv.positive_time_period_dict,
|
||||||
}
|
}
|
||||||
|
@ -54,7 +54,7 @@ async def async_get_triggers(
|
||||||
CONF_PLATFORM: "device",
|
CONF_PLATFORM: "device",
|
||||||
CONF_DEVICE_ID: device_id,
|
CONF_DEVICE_ID: device_id,
|
||||||
CONF_DOMAIN: DOMAIN,
|
CONF_DOMAIN: DOMAIN,
|
||||||
CONF_ENTITY_ID: entry.entity_id,
|
CONF_ENTITY_ID: entry.id,
|
||||||
CONF_TYPE: trigger,
|
CONF_TYPE: trigger,
|
||||||
}
|
}
|
||||||
for trigger in TRIGGER_TYPES
|
for trigger in TRIGGER_TYPES
|
||||||
|
|
|
@ -53,7 +53,7 @@ async def test_get_triggers(
|
||||||
config_entry_id=config_entry.entry_id,
|
config_entry_id=config_entry.entry_id,
|
||||||
connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
|
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
|
DOMAIN, "test", "5678", device_id=device_entry.id
|
||||||
)
|
)
|
||||||
expected_triggers = [
|
expected_triggers = [
|
||||||
|
@ -62,7 +62,7 @@ async def test_get_triggers(
|
||||||
"domain": DOMAIN,
|
"domain": DOMAIN,
|
||||||
"type": trigger,
|
"type": trigger,
|
||||||
"device_id": device_entry.id,
|
"device_id": device_entry.id,
|
||||||
"entity_id": f"{DOMAIN}.test_5678",
|
"entity_id": entity_entry.id,
|
||||||
"metadata": {"secondary": False},
|
"metadata": {"secondary": False},
|
||||||
}
|
}
|
||||||
for trigger in ["locked", "unlocked", "unlocking", "locking", "jammed"]
|
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,
|
config_entry_id=config_entry.entry_id,
|
||||||
connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
|
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,
|
DOMAIN,
|
||||||
"test",
|
"test",
|
||||||
"5678",
|
"5678",
|
||||||
|
@ -110,7 +110,7 @@ async def test_get_triggers_hidden_auxiliary(
|
||||||
"domain": DOMAIN,
|
"domain": DOMAIN,
|
||||||
"type": trigger,
|
"type": trigger,
|
||||||
"device_id": device_entry.id,
|
"device_id": device_entry.id,
|
||||||
"entity_id": f"{DOMAIN}.test_5678",
|
"entity_id": entity_entry.id,
|
||||||
"metadata": {"secondary": True},
|
"metadata": {"secondary": True},
|
||||||
}
|
}
|
||||||
for trigger in ["locked", "unlocked", "unlocking", "locking", "jammed"]
|
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."""
|
"""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(
|
assert await async_setup_component(
|
||||||
hass,
|
hass,
|
||||||
|
@ -166,7 +202,7 @@ async def test_if_fires_on_state_change(hass: HomeAssistant, calls) -> None:
|
||||||
"platform": "device",
|
"platform": "device",
|
||||||
"domain": DOMAIN,
|
"domain": DOMAIN,
|
||||||
"device_id": "",
|
"device_id": "",
|
||||||
"entity_id": "lock.entity",
|
"entity_id": entry.id,
|
||||||
"type": "locked",
|
"type": "locked",
|
||||||
},
|
},
|
||||||
"action": {
|
"action": {
|
||||||
|
@ -185,7 +221,7 @@ async def test_if_fires_on_state_change(hass: HomeAssistant, calls) -> None:
|
||||||
"platform": "device",
|
"platform": "device",
|
||||||
"domain": DOMAIN,
|
"domain": DOMAIN,
|
||||||
"device_id": "",
|
"device_id": "",
|
||||||
"entity_id": "lock.entity",
|
"entity_id": entry.id,
|
||||||
"type": "unlocked",
|
"type": "unlocked",
|
||||||
},
|
},
|
||||||
"action": {
|
"action": {
|
||||||
|
@ -204,26 +240,31 @@ async def test_if_fires_on_state_change(hass: HomeAssistant, calls) -> None:
|
||||||
)
|
)
|
||||||
|
|
||||||
# Fake that the entity is turning on.
|
# 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()
|
await hass.async_block_till_done()
|
||||||
assert len(calls) == 1
|
assert len(calls) == 1
|
||||||
assert calls[0].data[
|
assert (
|
||||||
"some"
|
calls[0].data["some"]
|
||||||
] == "locked - device - {} - unlocked - locked - None".format("lock.entity")
|
== f"locked - device - {entry.entity_id} - unlocked - locked - None"
|
||||||
|
)
|
||||||
|
|
||||||
# Fake that the entity is turning off.
|
# 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()
|
await hass.async_block_till_done()
|
||||||
assert len(calls) == 2
|
assert len(calls) == 2
|
||||||
assert calls[1].data[
|
assert (
|
||||||
"some"
|
calls[1].data["some"]
|
||||||
] == "unlocked - device - {} - locked - unlocked - None".format("lock.entity")
|
== f"unlocked - device - {entry.entity_id} - locked - unlocked - None"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
async def test_if_fires_on_state_change_with_for(hass: HomeAssistant, calls) -> None:
|
async def test_if_fires_on_state_change_legacy(
|
||||||
"""Test for triggers firing with delay."""
|
hass: HomeAssistant, entity_registry: er.EntityRegistry, calls
|
||||||
entity_id = f"{DOMAIN}.entity"
|
) -> None:
|
||||||
hass.states.async_set(entity_id, STATE_UNLOCKED)
|
"""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(
|
assert await async_setup_component(
|
||||||
hass,
|
hass,
|
||||||
|
@ -235,7 +276,53 @@ async def test_if_fires_on_state_change_with_for(hass: HomeAssistant, calls) ->
|
||||||
"platform": "device",
|
"platform": "device",
|
||||||
"domain": DOMAIN,
|
"domain": DOMAIN,
|
||||||
"device_id": "",
|
"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",
|
"type": "locked",
|
||||||
"for": {"seconds": 5},
|
"for": {"seconds": 5},
|
||||||
},
|
},
|
||||||
|
@ -260,7 +347,7 @@ async def test_if_fires_on_state_change_with_for(hass: HomeAssistant, calls) ->
|
||||||
"platform": "device",
|
"platform": "device",
|
||||||
"domain": DOMAIN,
|
"domain": DOMAIN,
|
||||||
"device_id": "",
|
"device_id": "",
|
||||||
"entity_id": entity_id,
|
"entity_id": entry.id,
|
||||||
"type": "unlocking",
|
"type": "unlocking",
|
||||||
"for": {"seconds": 5},
|
"for": {"seconds": 5},
|
||||||
},
|
},
|
||||||
|
@ -285,7 +372,7 @@ async def test_if_fires_on_state_change_with_for(hass: HomeAssistant, calls) ->
|
||||||
"platform": "device",
|
"platform": "device",
|
||||||
"domain": DOMAIN,
|
"domain": DOMAIN,
|
||||||
"device_id": "",
|
"device_id": "",
|
||||||
"entity_id": entity_id,
|
"entity_id": entry.id,
|
||||||
"type": "jammed",
|
"type": "jammed",
|
||||||
"for": {"seconds": 5},
|
"for": {"seconds": 5},
|
||||||
},
|
},
|
||||||
|
@ -310,7 +397,7 @@ async def test_if_fires_on_state_change_with_for(hass: HomeAssistant, calls) ->
|
||||||
"platform": "device",
|
"platform": "device",
|
||||||
"domain": DOMAIN,
|
"domain": DOMAIN,
|
||||||
"device_id": "",
|
"device_id": "",
|
||||||
"entity_id": entity_id,
|
"entity_id": entry.id,
|
||||||
"type": "locking",
|
"type": "locking",
|
||||||
"for": {"seconds": 5},
|
"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()
|
await hass.async_block_till_done()
|
||||||
assert hass.states.get(entity_id).state == STATE_UNLOCKED
|
|
||||||
assert len(calls) == 0
|
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()
|
await hass.async_block_till_done()
|
||||||
assert len(calls) == 0
|
assert len(calls) == 0
|
||||||
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=10))
|
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()
|
await hass.async_block_till_done()
|
||||||
assert (
|
assert (
|
||||||
calls[0].data["some"]
|
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()
|
await hass.async_block_till_done()
|
||||||
assert len(calls) == 1
|
assert len(calls) == 1
|
||||||
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=16))
|
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()
|
await hass.async_block_till_done()
|
||||||
assert (
|
assert (
|
||||||
calls[1].data["some"]
|
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()
|
await hass.async_block_till_done()
|
||||||
assert len(calls) == 2
|
assert len(calls) == 2
|
||||||
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=21))
|
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()
|
await hass.async_block_till_done()
|
||||||
assert (
|
assert (
|
||||||
calls[2].data["some"]
|
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()
|
await hass.async_block_till_done()
|
||||||
assert len(calls) == 3
|
assert len(calls) == 3
|
||||||
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=27))
|
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()
|
await hass.async_block_till_done()
|
||||||
assert (
|
assert (
|
||||||
calls[3].data["some"]
|
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"
|
||||||
)
|
)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue