From 7f0be78ebbf058076fe3366f3de74097297f4df9 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Wed, 21 Jun 2023 14:51:24 +0200 Subject: [PATCH] Teach netatmo device trigger about entity registry ids (#94980) --- .../components/netatmo/device_trigger.py | 6 +- .../components/netatmo/test_device_trigger.py | 101 ++++++++++++++++-- 2 files changed, 95 insertions(+), 12 deletions(-) diff --git a/homeassistant/components/netatmo/device_trigger.py b/homeassistant/components/netatmo/device_trigger.py index f3f45458d78..f4719badcfa 100644 --- a/homeassistant/components/netatmo/device_trigger.py +++ b/homeassistant/components/netatmo/device_trigger.py @@ -56,7 +56,7 @@ TRIGGER_TYPES = OUTDOOR_CAMERA_TRIGGERS + INDOOR_CAMERA_TRIGGERS + CLIMATE_TRIGG 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.Optional(CONF_SUBTYPE): str, } @@ -111,7 +111,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: trigger, CONF_SUBTYPE: subtype, } @@ -122,7 +122,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: trigger, } ) diff --git a/tests/components/netatmo/test_device_trigger.py b/tests/components/netatmo/test_device_trigger.py index 29a0b46a97c..c7cbaf4d131 100644 --- a/tests/components/netatmo/test_device_trigger.py +++ b/tests/components/netatmo/test_device_trigger.py @@ -56,7 +56,7 @@ async def test_get_triggers( connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")}, model=device_type, ) - entity_registry.async_get_or_create( + entity_entry = entity_registry.async_get_or_create( platform, NETATMO_DOMAIN, "5678", device_id=device_entry.id ) expected_triggers = [] @@ -70,7 +70,7 @@ async def test_get_triggers( "type": event_type, "subtype": subtype, "device_id": device_entry.id, - "entity_id": f"{platform}.{NETATMO_DOMAIN}_5678", + "entity_id": entity_entry.id, "metadata": {"secondary": False}, } ) @@ -81,7 +81,7 @@ async def test_get_triggers( "domain": NETATMO_DOMAIN, "type": event_type, "device_id": device_entry.id, - "entity_id": f"{platform}.{NETATMO_DOMAIN}_5678", + "entity_id": entity_entry.id, "metadata": {"secondary": False}, } ) @@ -130,7 +130,7 @@ async def test_if_fires_on_event( identifiers={(NETATMO_DOMAIN, mac_address)}, model=camera_type, ) - entity_registry.async_get_or_create( + entity_entry = entity_registry.async_get_or_create( platform, NETATMO_DOMAIN, "5678", device_id=device_entry.id ) events = async_capture_events(hass, "netatmo_event") @@ -145,7 +145,90 @@ async def test_if_fires_on_event( "platform": "device", "domain": NETATMO_DOMAIN, "device_id": device_entry.id, - "entity_id": f"{platform}.{NETATMO_DOMAIN}_5678", + "entity_id": entity_entry.id, + "type": event_type, + }, + "action": { + "service": "test.automation", + "data_template": { + "some": ( + "{{trigger.event.data.type}} - {{trigger.platform}} - {{trigger.event.data.device_id}}" + ) + }, + }, + }, + ] + }, + ) + + device = device_registry.async_get_device(set(), {connection}) + assert device is not None + + # Fake that the entity is turning on. + hass.bus.async_fire( + event_type=NETATMO_EVENT, + event_data={ + "type": event_type, + ATTR_DEVICE_ID: device.id, + }, + ) + await hass.async_block_till_done() + assert len(events) == 1 + assert len(calls) == 1 + assert calls[0].data["some"] == f"{event_type} - device - {device.id}" + + +@pytest.mark.parametrize( + ("platform", "camera_type", "event_type"), + [("camera", "Smart Outdoor Camera", trigger) for trigger in OUTDOOR_CAMERA_TRIGGERS] + + [("camera", "Smart Indoor Camera", trigger) for trigger in INDOOR_CAMERA_TRIGGERS] + + [ + ("climate", "Smart Valve", trigger) + for trigger in CLIMATE_TRIGGERS + if trigger not in SUBTYPES + ] + + [ + ("climate", "Smart Thermostat", trigger) + for trigger in CLIMATE_TRIGGERS + if trigger not in SUBTYPES + ], +) +async def test_if_fires_on_event_legacy( + hass: HomeAssistant, + calls, + device_registry: dr.DeviceRegistry, + entity_registry: er.EntityRegistry, + platform, + camera_type, + event_type, +) -> None: + """Test for event triggers firing.""" + mac_address = "12:34:56:AB:CD:EF" + connection = (dr.CONNECTION_NETWORK_MAC, mac_address) + config_entry = MockConfigEntry(domain=NETATMO_DOMAIN, data={}) + config_entry.add_to_hass(hass) + device_entry = device_registry.async_get_or_create( + config_entry_id=config_entry.entry_id, + connections={connection}, + identifiers={(NETATMO_DOMAIN, mac_address)}, + model=camera_type, + ) + entity_entry = entity_registry.async_get_or_create( + platform, NETATMO_DOMAIN, "5678", device_id=device_entry.id + ) + events = async_capture_events(hass, "netatmo_event") + + assert await async_setup_component( + hass, + automation.DOMAIN, + { + automation.DOMAIN: [ + { + "trigger": { + "platform": "device", + "domain": NETATMO_DOMAIN, + "device_id": device_entry.id, + "entity_id": entity_entry.entity_id, "type": event_type, }, "action": { @@ -212,7 +295,7 @@ async def test_if_fires_on_event_with_subtype( identifiers={(NETATMO_DOMAIN, mac_address)}, model=camera_type, ) - entity_registry.async_get_or_create( + entity_entry = entity_registry.async_get_or_create( platform, NETATMO_DOMAIN, "5678", device_id=device_entry.id ) events = async_capture_events(hass, "netatmo_event") @@ -227,7 +310,7 @@ async def test_if_fires_on_event_with_subtype( "platform": "device", "domain": NETATMO_DOMAIN, "device_id": device_entry.id, - "entity_id": f"{platform}.{NETATMO_DOMAIN}_5678", + "entity_id": entity_entry.id, "type": event_type, "subtype": sub_type, }, @@ -288,7 +371,7 @@ async def test_if_invalid_device( identifiers={(NETATMO_DOMAIN, mac_address)}, model=device_type, ) - entity_registry.async_get_or_create( + entity_entry = entity_registry.async_get_or_create( platform, NETATMO_DOMAIN, "5678", device_id=device_entry.id ) @@ -302,7 +385,7 @@ async def test_if_invalid_device( "platform": "device", "domain": NETATMO_DOMAIN, "device_id": device_entry.id, - "entity_id": f"{platform}.{NETATMO_DOMAIN}_5678", + "entity_id": entity_entry.id, "type": event_type, }, "action": {