Teach binary_sensor device trigger about entity registry ids (#94963)

* Teach binary_sensor device trigger about entity registry ids

* Update deconz test
This commit is contained in:
Erik Montnemery 2023-06-21 14:49:53 +02:00 committed by GitHub
parent 9d91cfa27f
commit 4414f06ed2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 147 additions and 33 deletions

View File

@ -195,7 +195,7 @@ TURNED_OFF = [trigger[1][CONF_TYPE] for trigger in ENTITY_TRIGGERS.values()]
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(TURNED_OFF + TURNED_ON), vol.Required(CONF_TYPE): vol.In(TURNED_OFF + TURNED_ON),
vol.Optional(CONF_FOR): cv.positive_time_period_dict, vol.Optional(CONF_FOR): cv.positive_time_period_dict,
} }
@ -254,7 +254,7 @@ async def async_get_triggers(
**automation, **automation,
"platform": "device", "platform": "device",
"device_id": device_id, "device_id": device_id,
"entity_id": entry.entity_id, "entity_id": entry.id,
"domain": DOMAIN, "domain": DOMAIN,
} }
for automation in templates for automation in templates

View File

@ -41,6 +41,7 @@ async def test_get_triggers(
enable_custom_integrations: None, enable_custom_integrations: None,
) -> None: ) -> None:
"""Test we get the expected triggers from a binary_sensor.""" """Test we get the expected triggers from a binary_sensor."""
registry_entries: dict[BinarySensorDeviceClass, er.RegistryEntry] = {}
platform = getattr(hass.components, f"test.{DOMAIN}") platform = getattr(hass.components, f"test.{DOMAIN}")
platform.init() platform.init()
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}}) assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
@ -53,7 +54,7 @@ async def test_get_triggers(
connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")}, connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
) )
for device_class in BinarySensorDeviceClass: for device_class in BinarySensorDeviceClass:
entity_registry.async_get_or_create( registry_entries[device_class] = entity_registry.async_get_or_create(
DOMAIN, DOMAIN,
"test", "test",
platform.ENTITIES[device_class].unique_id, platform.ENTITIES[device_class].unique_id,
@ -66,7 +67,7 @@ async def test_get_triggers(
"domain": DOMAIN, "domain": DOMAIN,
"type": trigger["type"], "type": trigger["type"],
"device_id": device_entry.id, "device_id": device_entry.id,
"entity_id": platform.ENTITIES[device_class].entity_id, "entity_id": registry_entries[device_class].id,
"metadata": {"secondary": False}, "metadata": {"secondary": False},
} }
for device_class in BinarySensorDeviceClass for device_class in BinarySensorDeviceClass
@ -101,7 +102,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( entry = entity_registry.async_get_or_create(
DOMAIN, DOMAIN,
"test", "test",
"5678", "5678",
@ -115,7 +116,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": entry.id,
"metadata": {"secondary": True}, "metadata": {"secondary": True},
} }
for trigger in ["turned_on", "turned_off"] for trigger in ["turned_on", "turned_off"]
@ -132,9 +133,9 @@ async def test_get_triggers_no_state(
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
) -> None: ) -> None:
"""Test we get the expected triggers from a binary_sensor.""" """Test we get the expected triggers from a binary_sensor."""
registry_entries: dict[BinarySensorDeviceClass, er.RegistryEntry] = {}
platform = getattr(hass.components, f"test.{DOMAIN}") platform = getattr(hass.components, f"test.{DOMAIN}")
platform.init() platform.init()
entity_ids = {}
config_entry = MockConfigEntry(domain="test", data={}) config_entry = MockConfigEntry(domain="test", data={})
config_entry.add_to_hass(hass) config_entry.add_to_hass(hass)
@ -143,13 +144,13 @@ async def test_get_triggers_no_state(
connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")}, connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
) )
for device_class in BinarySensorDeviceClass: for device_class in BinarySensorDeviceClass:
entity_ids[device_class] = entity_registry.async_get_or_create( registry_entries[device_class] = entity_registry.async_get_or_create(
DOMAIN, DOMAIN,
"test", "test",
f"5678_{device_class}", f"5678_{device_class}",
device_id=device_entry.id, device_id=device_entry.id,
original_device_class=device_class, original_device_class=device_class,
).entity_id )
await hass.async_block_till_done() await hass.async_block_till_done()
@ -159,7 +160,7 @@ async def test_get_triggers_no_state(
"domain": DOMAIN, "domain": DOMAIN,
"type": trigger["type"], "type": trigger["type"],
"device_id": device_entry.id, "device_id": device_entry.id,
"entity_id": entity_ids[device_class], "entity_id": registry_entries[device_class].id,
"metadata": {"secondary": False}, "metadata": {"secondary": False},
} }
for device_class in BinarySensorDeviceClass for device_class in BinarySensorDeviceClass
@ -201,8 +202,42 @@ async def test_get_trigger_capabilities(
assert capabilities == expected_capabilities assert capabilities == expected_capabilities
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 binary_sensor 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_registry.async_get_or_create(
DOMAIN, "test", "5678", device_id=device_entry.id
)
expected_capabilities = {
"extra_fields": [
{"name": "for", "optional": True, "type": "positive_time_period_dict"}
]
}
triggers = await async_get_device_automations(
hass, DeviceAutomationType.TRIGGER, device_entry.id
)
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 == expected_capabilities
async def test_if_fires_on_state_change( async def test_if_fires_on_state_change(
hass: HomeAssistant, calls, enable_custom_integrations: None hass: HomeAssistant,
entity_registry: er.EntityRegistry,
calls,
enable_custom_integrations: None,
) -> None: ) -> None:
"""Test for on and off triggers firing.""" """Test for on and off triggers firing."""
platform = getattr(hass.components, f"test.{DOMAIN}") platform = getattr(hass.components, f"test.{DOMAIN}")
@ -210,7 +245,11 @@ async def test_if_fires_on_state_change(
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}}) assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
await hass.async_block_till_done() await hass.async_block_till_done()
sensor1 = platform.ENTITIES["battery"] entry = entity_registry.async_get_or_create(
DOMAIN,
"test",
platform.ENTITIES["battery"].unique_id,
)
assert await async_setup_component( assert await async_setup_component(
hass, hass,
@ -222,7 +261,7 @@ async def test_if_fires_on_state_change(
"platform": "device", "platform": "device",
"domain": DOMAIN, "domain": DOMAIN,
"device_id": "", "device_id": "",
"entity_id": sensor1.entity_id, "entity_id": entry.id,
"type": "bat_low", "type": "bat_low",
}, },
"action": { "action": {
@ -246,7 +285,7 @@ async def test_if_fires_on_state_change(
"platform": "device", "platform": "device",
"domain": DOMAIN, "domain": DOMAIN,
"device_id": "", "device_id": "",
"entity_id": sensor1.entity_id, "entity_id": entry.id,
"type": "not_bat_low", "type": "not_bat_low",
}, },
"action": { "action": {
@ -269,26 +308,30 @@ async def test_if_fires_on_state_change(
}, },
) )
await hass.async_block_till_done() await hass.async_block_till_done()
assert hass.states.get(sensor1.entity_id).state == STATE_ON assert hass.states.get(entry.entity_id).state == STATE_ON
assert len(calls) == 0 assert len(calls) == 0
hass.states.async_set(sensor1.entity_id, STATE_OFF) hass.states.async_set(entry.entity_id, STATE_OFF)
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"] == "not_bat_low device - {} - on - off - None".format( assert (
sensor1.entity_id calls[0].data["some"]
== f"not_bat_low device - {entry.entity_id} - on - off - None"
) )
hass.states.async_set(sensor1.entity_id, STATE_ON) hass.states.async_set(entry.entity_id, STATE_ON)
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"] == "bat_low device - {} - off - on - None".format( assert (
sensor1.entity_id calls[1].data["some"] == f"bat_low device - {entry.entity_id} - off - on - None"
) )
async def test_if_fires_on_state_change_with_for( async def test_if_fires_on_state_change_with_for(
hass: HomeAssistant, calls, enable_custom_integrations: None hass: HomeAssistant,
entity_registry: er.EntityRegistry,
calls,
enable_custom_integrations: None,
) -> None: ) -> None:
"""Test for triggers firing with delay.""" """Test for triggers firing with delay."""
platform = getattr(hass.components, f"test.{DOMAIN}") platform = getattr(hass.components, f"test.{DOMAIN}")
@ -297,7 +340,11 @@ async def test_if_fires_on_state_change_with_for(
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}}) assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
await hass.async_block_till_done() await hass.async_block_till_done()
sensor1 = platform.ENTITIES["battery"] entry = entity_registry.async_get_or_create(
DOMAIN,
"test",
platform.ENTITIES["battery"].unique_id,
)
assert await async_setup_component( assert await async_setup_component(
hass, hass,
@ -309,7 +356,7 @@ async def test_if_fires_on_state_change_with_for(
"platform": "device", "platform": "device",
"domain": DOMAIN, "domain": DOMAIN,
"device_id": "", "device_id": "",
"entity_id": sensor1.entity_id, "entity_id": entry.id,
"type": "turned_off", "type": "turned_off",
"for": {"seconds": 5}, "for": {"seconds": 5},
}, },
@ -333,16 +380,80 @@ async def test_if_fires_on_state_change_with_for(
}, },
) )
await hass.async_block_till_done() await hass.async_block_till_done()
assert hass.states.get(sensor1.entity_id).state == STATE_ON assert hass.states.get(entry.entity_id).state == STATE_ON
assert len(calls) == 0 assert len(calls) == 0
hass.states.async_set(sensor1.entity_id, STATE_OFF) hass.states.async_set(entry.entity_id, STATE_OFF)
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))
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(calls) == 1
await hass.async_block_till_done() await hass.async_block_till_done()
assert calls[0].data["some"] == "turn_off device - {} - on - off - 0:00:05".format( assert (
sensor1.entity_id calls[0].data["some"]
== f"turn_off device - {entry.entity_id} - on - off - 0:00:05"
)
async def test_if_fires_on_state_change_legacy(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
calls,
enable_custom_integrations: None,
) -> None:
"""Test for triggers firing."""
platform = getattr(hass.components, f"test.{DOMAIN}")
platform.init()
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
await hass.async_block_till_done()
entry = entity_registry.async_get_or_create(
DOMAIN,
"test",
platform.ENTITIES["battery"].unique_id,
)
assert await async_setup_component(
hass,
automation.DOMAIN,
{
automation.DOMAIN: [
{
"trigger": {
"platform": "device",
"domain": DOMAIN,
"device_id": "",
"entity_id": entry.entity_id,
"type": "turned_off",
},
"action": {
"service": "test.automation",
"data_template": {
"some": "turn_off {{ trigger.%s }}"
% "}} - {{ trigger.".join(
(
"platform",
"entity_id",
"from_state.state",
"to_state.state",
"for",
)
)
},
},
}
]
},
)
await hass.async_block_till_done()
assert hass.states.get(entry.entity_id).state == STATE_ON
assert len(calls) == 0
hass.states.async_set(entry.entity_id, STATE_OFF)
await hass.async_block_till_done()
assert len(calls) == 1
assert calls[0].data["some"] == "turn_off device - {} - on - off - None".format(
entry.entity_id
) )

View File

@ -26,7 +26,7 @@ from homeassistant.const import (
CONF_TYPE, CONF_TYPE,
) )
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr from homeassistant.helpers import device_registry as dr, entity_registry as er
from homeassistant.helpers.trigger import async_initialize_triggers from homeassistant.helpers.trigger import async_initialize_triggers
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
@ -192,6 +192,9 @@ async def test_get_triggers_for_alarm_event(
device = device_registry.async_get_device( device = device_registry.async_get_device(
identifiers={(DECONZ_DOMAIN, "00:00:00:00:00:00:00:00")} identifiers={(DECONZ_DOMAIN, "00:00:00:00:00:00:00:00")}
) )
entity_registry = er.async_get(hass)
low_bat_entity = entity_registry.async_get("binary_sensor.keypad_low_battery")
tamper_entity = entity_registry.async_get("binary_sensor.keypad_tampered")
triggers = await async_get_device_automations( triggers = await async_get_device_automations(
hass, DeviceAutomationType.TRIGGER, device.id hass, DeviceAutomationType.TRIGGER, device.id
@ -201,7 +204,7 @@ async def test_get_triggers_for_alarm_event(
{ {
CONF_DEVICE_ID: device.id, CONF_DEVICE_ID: device.id,
CONF_DOMAIN: BINARY_SENSOR_DOMAIN, CONF_DOMAIN: BINARY_SENSOR_DOMAIN,
ATTR_ENTITY_ID: "binary_sensor.keypad_low_battery", ATTR_ENTITY_ID: low_bat_entity.id,
CONF_PLATFORM: "device", CONF_PLATFORM: "device",
CONF_TYPE: CONF_BAT_LOW, CONF_TYPE: CONF_BAT_LOW,
"metadata": {"secondary": True}, "metadata": {"secondary": True},
@ -209,7 +212,7 @@ async def test_get_triggers_for_alarm_event(
{ {
CONF_DEVICE_ID: device.id, CONF_DEVICE_ID: device.id,
CONF_DOMAIN: BINARY_SENSOR_DOMAIN, CONF_DOMAIN: BINARY_SENSOR_DOMAIN,
ATTR_ENTITY_ID: "binary_sensor.keypad_low_battery", ATTR_ENTITY_ID: low_bat_entity.id,
CONF_PLATFORM: "device", CONF_PLATFORM: "device",
CONF_TYPE: CONF_NOT_BAT_LOW, CONF_TYPE: CONF_NOT_BAT_LOW,
"metadata": {"secondary": True}, "metadata": {"secondary": True},
@ -217,7 +220,7 @@ async def test_get_triggers_for_alarm_event(
{ {
CONF_DEVICE_ID: device.id, CONF_DEVICE_ID: device.id,
CONF_DOMAIN: BINARY_SENSOR_DOMAIN, CONF_DOMAIN: BINARY_SENSOR_DOMAIN,
ATTR_ENTITY_ID: "binary_sensor.keypad_tampered", ATTR_ENTITY_ID: tamper_entity.id,
CONF_PLATFORM: "device", CONF_PLATFORM: "device",
CONF_TYPE: CONF_TAMPERED, CONF_TYPE: CONF_TAMPERED,
"metadata": {"secondary": True}, "metadata": {"secondary": True},
@ -225,7 +228,7 @@ async def test_get_triggers_for_alarm_event(
{ {
CONF_DEVICE_ID: device.id, CONF_DEVICE_ID: device.id,
CONF_DOMAIN: BINARY_SENSOR_DOMAIN, CONF_DOMAIN: BINARY_SENSOR_DOMAIN,
ATTR_ENTITY_ID: "binary_sensor.keypad_tampered", ATTR_ENTITY_ID: tamper_entity.id,
CONF_PLATFORM: "device", CONF_PLATFORM: "device",
CONF_TYPE: CONF_NOT_TAMPERED, CONF_TYPE: CONF_NOT_TAMPERED,
"metadata": {"secondary": True}, "metadata": {"secondary": True},