diff --git a/homeassistant/components/device_tracker/device_trigger.py b/homeassistant/components/device_tracker/device_trigger.py index 150b5872275..a96f9affb1d 100644 --- a/homeassistant/components/device_tracker/device_trigger.py +++ b/homeassistant/components/device_tracker/device_trigger.py @@ -27,7 +27,7 @@ TRIGGER_TYPES: Final[set[str]] = {"enters", "leaves"} 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_ZONE): cv.entity_domain(DOMAIN_ZONE), } @@ -51,7 +51,7 @@ async def async_get_triggers( CONF_PLATFORM: "device", CONF_DEVICE_ID: device_id, CONF_DOMAIN: DOMAIN, - CONF_ENTITY_ID: entry.entity_id, + CONF_ENTITY_ID: entry.id, CONF_TYPE: "enters", } ) @@ -60,7 +60,7 @@ async def async_get_triggers( CONF_PLATFORM: "device", CONF_DEVICE_ID: device_id, CONF_DOMAIN: DOMAIN, - CONF_ENTITY_ID: entry.entity_id, + CONF_ENTITY_ID: entry.id, CONF_TYPE: "leaves", } ) diff --git a/tests/components/device_tracker/test_device_trigger.py b/tests/components/device_tracker/test_device_trigger.py index b48ff93bb4b..75209ec607b 100644 --- a/tests/components/device_tracker/test_device_trigger.py +++ b/tests/components/device_tracker/test_device_trigger.py @@ -73,7 +73,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 = [ @@ -82,7 +82,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 ["leaves", "enters"] @@ -116,7 +116,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", @@ -130,7 +130,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 ["leaves", "enters"] @@ -141,10 +141,14 @@ async def test_get_triggers_hidden_auxiliary( 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.""" + entry = entity_registry.async_get_or_create(DOMAIN, "test", "5678") + hass.states.async_set( - "device_tracker.entity", + entry.entity_id, "state", {"latitude": AWAY_LATITUDE, "longitude": AWAY_LONGITUDE}, ) @@ -159,7 +163,7 @@ async def test_if_fires_on_zone_change(hass: HomeAssistant, calls) -> None: "platform": "device", "domain": DOMAIN, "device_id": "", - "entity_id": "device_tracker.entity", + "entity_id": entry.id, "type": "enters", "zone": "zone.test", }, @@ -183,7 +187,7 @@ async def test_if_fires_on_zone_change(hass: HomeAssistant, calls) -> None: "platform": "device", "domain": DOMAIN, "device_id": "", - "entity_id": "device_tracker.entity", + "entity_id": entry.id, "type": "leaves", "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. hass.states.async_set( - "device_tracker.entity", + 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"] == "enter - device - {} - -117.235 - -117.238".format( - "device_tracker.entity" + assert ( + calls[0].data["some"] + == f"enter - device - {entry.entity_id} - -117.235 - -117.238" ) # Fake that the entity is leaving. hass.states.async_set( - "device_tracker.entity", + entry.entity_id, "state", {"latitude": AWAY_LATITUDE, "longitude": AWAY_LONGITUDE}, ) await hass.async_block_till_done() assert len(calls) == 2 - assert calls[1].data["some"] == "leave - device - {} - -117.238 - -117.235".format( - "device_tracker.entity" + assert ( + 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, 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 ) capabilities = await device_trigger.async_get_trigger_capabilities( @@ -253,7 +318,46 @@ async def test_get_trigger_capabilities( "domain": DOMAIN, "type": "enters", "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