From 49ec806046f1a8c548f6b561ffb9d10e40d376ae Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Wed, 21 Jun 2023 14:50:11 +0200 Subject: [PATCH] Teach button device trigger about entity registry ids (#94965) * Teach button device trigger about entity registry ids * Update homekit_controller tests --- .../components/button/device_trigger.py | 4 +- .../components/button/test_device_trigger.py | 76 ++++++++++++++++--- .../homekit_controller/test_device_trigger.py | 27 ++++--- 3 files changed, 81 insertions(+), 26 deletions(-) diff --git a/homeassistant/components/button/device_trigger.py b/homeassistant/components/button/device_trigger.py index fbf054996c3..1b206337f33 100644 --- a/homeassistant/components/button/device_trigger.py +++ b/homeassistant/components/button/device_trigger.py @@ -26,7 +26,7 @@ TRIGGER_TYPES = {"pressed"} 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), } ) @@ -42,7 +42,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: "pressed", } for entry in er.async_entries_for_device(registry, device_id) diff --git a/tests/components/button/test_device_trigger.py b/tests/components/button/test_device_trigger.py index 4e3be3eba67..32f10044206 100644 --- a/tests/components/button/test_device_trigger.py +++ b/tests/components/button/test_device_trigger.py @@ -37,7 +37,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 = [ @@ -46,7 +46,7 @@ async def test_get_triggers( "domain": DOMAIN, "type": "pressed", "device_id": device_entry.id, - "entity_id": f"{DOMAIN}.test_5678", + "entity_id": entity_entry.id, "metadata": {"secondary": False}, } ] @@ -79,7 +79,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", @@ -93,7 +93,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 ["pressed"] @@ -104,9 +104,13 @@ async def test_get_triggers_hidden_auxiliary( assert triggers == unordered(expected_triggers) -async def test_if_fires_on_state_change(hass: HomeAssistant, calls) -> None: +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("button.entity", "unknown") + entry = entity_registry.async_get_or_create(DOMAIN, "test", "5678") + + hass.states.async_set(entry.entity_id, "unknown") assert await async_setup_component( hass, @@ -118,7 +122,7 @@ async def test_if_fires_on_state_change(hass: HomeAssistant, calls) -> None: "platform": "device", "domain": DOMAIN, "device_id": "", - "entity_id": "button.entity", + "entity_id": entry.id, "type": "pressed", }, "action": { @@ -140,11 +144,59 @@ async def test_if_fires_on_state_change(hass: HomeAssistant, calls) -> None: ) # Test triggering device trigger with a to state - hass.states.async_set("button.entity", "2021-01-01T23:59:59+00:00") + hass.states.async_set(entry.entity_id, "2021-01-01T23:59:59+00:00") await hass.async_block_till_done() assert len(calls) == 1 - assert calls[0].data[ - "some" - ] == "to - device - {} - unknown - 2021-01-01T23:59:59+00:00 - None - 0".format( - "button.entity" + assert ( + calls[0].data["some"] + == f"to - device - {entry.entity_id} - unknown - 2021-01-01T23:59:59+00:00 - None - 0" + ) + + +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, "unknown") + + assert await async_setup_component( + hass, + automation.DOMAIN, + { + automation.DOMAIN: [ + { + "trigger": { + "platform": "device", + "domain": DOMAIN, + "device_id": "", + "entity_id": entry.entity_id, + "type": "pressed", + }, + "action": { + "service": "test.automation", + "data": { + "some": ( + "to - {{ trigger.platform }} " + "- {{ trigger.entity_id }} " + "- {{ trigger.from_state.state }} " + "- {{ trigger.to_state.state }} " + "- {{ trigger.for }} " + "- {{ trigger.id }}" + ) + }, + }, + } + ] + }, + ) + + # Test triggering device trigger with a to state + hass.states.async_set(entry.entity_id, "2021-01-01T23:59:59+00:00") + await hass.async_block_till_done() + assert len(calls) == 1 + assert ( + calls[0].data["some"] + == f"to - device - {entry.entity_id} - unknown - 2021-01-01T23:59:59+00:00 - None - 0" ) diff --git a/tests/components/homekit_controller/test_device_trigger.py b/tests/components/homekit_controller/test_device_trigger.py index 4588941bcbf..e38952785df 100644 --- a/tests/components/homekit_controller/test_device_trigger.py +++ b/tests/components/homekit_controller/test_device_trigger.py @@ -91,16 +91,17 @@ async def test_enumerate_remote(hass: HomeAssistant, utcnow) -> None: await setup_test_component(hass, create_remote) entity_registry = er.async_get(hass) - entry = entity_registry.async_get("sensor.testdevice_battery") + bat_sensor = entity_registry.async_get("sensor.testdevice_battery") + identify_button = entity_registry.async_get("button.testdevice_identify") device_registry = dr.async_get(hass) - device = device_registry.async_get(entry.device_id) + device = device_registry.async_get(bat_sensor.device_id) expected = [ { "device_id": device.id, "domain": "sensor", - "entity_id": "sensor.testdevice_battery", + "entity_id": bat_sensor.entity_id, "platform": "device", "type": "battery_level", "metadata": {"secondary": True}, @@ -108,7 +109,7 @@ async def test_enumerate_remote(hass: HomeAssistant, utcnow) -> None: { "device_id": device.id, "domain": "button", - "entity_id": "button.testdevice_identify", + "entity_id": identify_button.id, "platform": "device", "type": "pressed", "metadata": {"secondary": True}, @@ -139,16 +140,17 @@ async def test_enumerate_button(hass: HomeAssistant, utcnow) -> None: await setup_test_component(hass, create_button) entity_registry = er.async_get(hass) - entry = entity_registry.async_get("sensor.testdevice_battery") + bat_sensor = entity_registry.async_get("sensor.testdevice_battery") + identify_button = entity_registry.async_get("button.testdevice_identify") device_registry = dr.async_get(hass) - device = device_registry.async_get(entry.device_id) + device = device_registry.async_get(bat_sensor.device_id) expected = [ { "device_id": device.id, "domain": "sensor", - "entity_id": "sensor.testdevice_battery", + "entity_id": bat_sensor.entity_id, "platform": "device", "type": "battery_level", "metadata": {"secondary": True}, @@ -156,7 +158,7 @@ async def test_enumerate_button(hass: HomeAssistant, utcnow) -> None: { "device_id": device.id, "domain": "button", - "entity_id": "button.testdevice_identify", + "entity_id": identify_button.id, "platform": "device", "type": "pressed", "metadata": {"secondary": True}, @@ -186,16 +188,17 @@ async def test_enumerate_doorbell(hass: HomeAssistant, utcnow) -> None: await setup_test_component(hass, create_doorbell) entity_registry = er.async_get(hass) - entry = entity_registry.async_get("sensor.testdevice_battery") + bat_sensor = entity_registry.async_get("sensor.testdevice_battery") + identify_button = entity_registry.async_get("button.testdevice_identify") device_registry = dr.async_get(hass) - device = device_registry.async_get(entry.device_id) + device = device_registry.async_get(bat_sensor.device_id) expected = [ { "device_id": device.id, "domain": "sensor", - "entity_id": "sensor.testdevice_battery", + "entity_id": bat_sensor.entity_id, "platform": "device", "type": "battery_level", "metadata": {"secondary": True}, @@ -203,7 +206,7 @@ async def test_enumerate_doorbell(hass: HomeAssistant, utcnow) -> None: { "device_id": device.id, "domain": "button", - "entity_id": "button.testdevice_identify", + "entity_id": identify_button.id, "platform": "device", "type": "pressed", "metadata": {"secondary": True},