Teach device_tracker device trigger about entity registry ids (#94972)
This commit is contained in:
parent
49c7d2ff89
commit
20be441c9f
2 changed files with 123 additions and 19 deletions
|
@ -27,7 +27,7 @@ TRIGGER_TYPES: Final[set[str]] = {"enters", "leaves"}
|
||||||
|
|
||||||
TRIGGER_SCHEMA: Final = DEVICE_TRIGGER_BASE_SCHEMA.extend(
|
TRIGGER_SCHEMA: Final = 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.Required(CONF_ZONE): cv.entity_domain(DOMAIN_ZONE),
|
vol.Required(CONF_ZONE): cv.entity_domain(DOMAIN_ZONE),
|
||||||
}
|
}
|
||||||
|
@ -51,7 +51,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: "enters",
|
CONF_TYPE: "enters",
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
@ -60,7 +60,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: "leaves",
|
CONF_TYPE: "leaves",
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
|
@ -73,7 +73,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 = [
|
||||||
|
@ -82,7 +82,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 ["leaves", "enters"]
|
for trigger in ["leaves", "enters"]
|
||||||
|
@ -116,7 +116,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",
|
||||||
|
@ -130,7 +130,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 ["leaves", "enters"]
|
for trigger in ["leaves", "enters"]
|
||||||
|
@ -141,10 +141,14 @@ async def test_get_triggers_hidden_auxiliary(
|
||||||
assert triggers == unordered(expected_triggers)
|
assert triggers == unordered(expected_triggers)
|
||||||
|
|
||||||
|
|
||||||
async def test_if_fires_on_zone_change(hass: HomeAssistant, calls) -> None:
|
async def test_if_fires_on_zone_change(
|
||||||
|
hass: HomeAssistant, entity_registry: er.EntityRegistry, calls
|
||||||
|
) -> None:
|
||||||
"""Test for enter and leave triggers firing."""
|
"""Test for enter and leave triggers firing."""
|
||||||
|
entry = entity_registry.async_get_or_create(DOMAIN, "test", "5678")
|
||||||
|
|
||||||
hass.states.async_set(
|
hass.states.async_set(
|
||||||
"device_tracker.entity",
|
entry.entity_id,
|
||||||
"state",
|
"state",
|
||||||
{"latitude": AWAY_LATITUDE, "longitude": AWAY_LONGITUDE},
|
{"latitude": AWAY_LATITUDE, "longitude": AWAY_LONGITUDE},
|
||||||
)
|
)
|
||||||
|
@ -159,7 +163,7 @@ async def test_if_fires_on_zone_change(hass: HomeAssistant, calls) -> None:
|
||||||
"platform": "device",
|
"platform": "device",
|
||||||
"domain": DOMAIN,
|
"domain": DOMAIN,
|
||||||
"device_id": "",
|
"device_id": "",
|
||||||
"entity_id": "device_tracker.entity",
|
"entity_id": entry.id,
|
||||||
"type": "enters",
|
"type": "enters",
|
||||||
"zone": "zone.test",
|
"zone": "zone.test",
|
||||||
},
|
},
|
||||||
|
@ -183,7 +187,7 @@ async def test_if_fires_on_zone_change(hass: HomeAssistant, calls) -> None:
|
||||||
"platform": "device",
|
"platform": "device",
|
||||||
"domain": DOMAIN,
|
"domain": DOMAIN,
|
||||||
"device_id": "",
|
"device_id": "",
|
||||||
"entity_id": "device_tracker.entity",
|
"entity_id": entry.id,
|
||||||
"type": "leaves",
|
"type": "leaves",
|
||||||
"zone": "zone.test",
|
"zone": "zone.test",
|
||||||
},
|
},
|
||||||
|
@ -208,26 +212,87 @@ async def test_if_fires_on_zone_change(hass: HomeAssistant, calls) -> None:
|
||||||
|
|
||||||
# Fake that the entity is entering.
|
# Fake that the entity is entering.
|
||||||
hass.states.async_set(
|
hass.states.async_set(
|
||||||
"device_tracker.entity",
|
entry.entity_id,
|
||||||
"state",
|
"state",
|
||||||
{"latitude": HOME_LATITUDE, "longitude": HOME_LONGITUDE},
|
{"latitude": HOME_LATITUDE, "longitude": HOME_LONGITUDE},
|
||||||
)
|
)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
assert len(calls) == 1
|
assert len(calls) == 1
|
||||||
assert calls[0].data["some"] == "enter - device - {} - -117.235 - -117.238".format(
|
assert (
|
||||||
"device_tracker.entity"
|
calls[0].data["some"]
|
||||||
|
== f"enter - device - {entry.entity_id} - -117.235 - -117.238"
|
||||||
)
|
)
|
||||||
|
|
||||||
# Fake that the entity is leaving.
|
# Fake that the entity is leaving.
|
||||||
hass.states.async_set(
|
hass.states.async_set(
|
||||||
"device_tracker.entity",
|
entry.entity_id,
|
||||||
"state",
|
"state",
|
||||||
{"latitude": AWAY_LATITUDE, "longitude": AWAY_LONGITUDE},
|
{"latitude": AWAY_LATITUDE, "longitude": AWAY_LONGITUDE},
|
||||||
)
|
)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
assert len(calls) == 2
|
assert len(calls) == 2
|
||||||
assert calls[1].data["some"] == "leave - device - {} - -117.238 - -117.235".format(
|
assert (
|
||||||
"device_tracker.entity"
|
calls[1].data["some"]
|
||||||
|
== f"leave - device - {entry.entity_id} - -117.238 - -117.235"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def test_if_fires_on_zone_change_legacy(
|
||||||
|
hass: HomeAssistant, entity_registry: er.EntityRegistry, calls
|
||||||
|
) -> None:
|
||||||
|
"""Test for enter and leave triggers firing."""
|
||||||
|
entry = entity_registry.async_get_or_create(DOMAIN, "test", "5678")
|
||||||
|
|
||||||
|
hass.states.async_set(
|
||||||
|
entry.entity_id,
|
||||||
|
"state",
|
||||||
|
{"latitude": AWAY_LATITUDE, "longitude": AWAY_LONGITUDE},
|
||||||
|
)
|
||||||
|
|
||||||
|
assert await async_setup_component(
|
||||||
|
hass,
|
||||||
|
automation.DOMAIN,
|
||||||
|
{
|
||||||
|
automation.DOMAIN: [
|
||||||
|
{
|
||||||
|
"trigger": {
|
||||||
|
"platform": "device",
|
||||||
|
"domain": DOMAIN,
|
||||||
|
"device_id": "",
|
||||||
|
"entity_id": entry.entity_id,
|
||||||
|
"type": "enters",
|
||||||
|
"zone": "zone.test",
|
||||||
|
},
|
||||||
|
"action": {
|
||||||
|
"service": "test.automation",
|
||||||
|
"data_template": {
|
||||||
|
"some": (
|
||||||
|
"enter "
|
||||||
|
"- {{ trigger.platform }} "
|
||||||
|
"- {{ trigger.entity_id }} "
|
||||||
|
"- {{ "
|
||||||
|
" trigger.from_state.attributes.longitude|round(3) "
|
||||||
|
" }} "
|
||||||
|
"- {{ trigger.to_state.attributes.longitude|round(3) }}"
|
||||||
|
)
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
]
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
# Fake that the entity is entering.
|
||||||
|
hass.states.async_set(
|
||||||
|
entry.entity_id,
|
||||||
|
"state",
|
||||||
|
{"latitude": HOME_LATITUDE, "longitude": HOME_LONGITUDE},
|
||||||
|
)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
assert len(calls) == 1
|
||||||
|
assert (
|
||||||
|
calls[0].data["some"]
|
||||||
|
== f"enter - device - {entry.entity_id} - -117.235 - -117.238"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -243,7 +308,7 @@ async def test_get_trigger_capabilities(
|
||||||
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
|
||||||
)
|
)
|
||||||
capabilities = await device_trigger.async_get_trigger_capabilities(
|
capabilities = await device_trigger.async_get_trigger_capabilities(
|
||||||
|
@ -253,7 +318,46 @@ async def test_get_trigger_capabilities(
|
||||||
"domain": DOMAIN,
|
"domain": DOMAIN,
|
||||||
"type": "enters",
|
"type": "enters",
|
||||||
"device_id": device_entry.id,
|
"device_id": device_entry.id,
|
||||||
"entity_id": f"{DOMAIN}.test_5678",
|
"entity_id": entity_entry.id,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
assert capabilities and "extra_fields" in capabilities
|
||||||
|
|
||||||
|
assert voluptuous_serialize.convert(
|
||||||
|
capabilities["extra_fields"], custom_serializer=cv.custom_serializer
|
||||||
|
) == [
|
||||||
|
{
|
||||||
|
"name": "zone",
|
||||||
|
"required": True,
|
||||||
|
"type": "select",
|
||||||
|
"options": [("zone.test", "test"), ("zone.home", "test home")],
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
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 device_tracker trigger."""
|
||||||
|
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_entry = entity_registry.async_get_or_create(
|
||||||
|
DOMAIN, "test", "5678", device_id=device_entry.id
|
||||||
|
)
|
||||||
|
capabilities = await device_trigger.async_get_trigger_capabilities(
|
||||||
|
hass,
|
||||||
|
{
|
||||||
|
"platform": "device",
|
||||||
|
"domain": DOMAIN,
|
||||||
|
"type": "enters",
|
||||||
|
"device_id": device_entry.id,
|
||||||
|
"entity_id": entity_entry.entity_id,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
assert capabilities and "extra_fields" in capabilities
|
assert capabilities and "extra_fields" in capabilities
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue