Teach button device trigger about entity registry ids (#94965)
* Teach button device trigger about entity registry ids * Update homekit_controller tests
This commit is contained in:
parent
4414f06ed2
commit
49ec806046
3 changed files with 81 additions and 26 deletions
|
@ -26,7 +26,7 @@ TRIGGER_TYPES = {"pressed"}
|
||||||
|
|
||||||
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(TRIGGER_TYPES),
|
vol.Required(CONF_TYPE): vol.In(TRIGGER_TYPES),
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
@ -42,7 +42,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: "pressed",
|
CONF_TYPE: "pressed",
|
||||||
}
|
}
|
||||||
for entry in er.async_entries_for_device(registry, device_id)
|
for entry in er.async_entries_for_device(registry, device_id)
|
||||||
|
|
|
@ -37,7 +37,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 = [
|
||||||
|
@ -46,7 +46,7 @@ async def test_get_triggers(
|
||||||
"domain": DOMAIN,
|
"domain": DOMAIN,
|
||||||
"type": "pressed",
|
"type": "pressed",
|
||||||
"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},
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
@ -79,7 +79,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",
|
||||||
|
@ -93,7 +93,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 ["pressed"]
|
for trigger in ["pressed"]
|
||||||
|
@ -104,9 +104,13 @@ async def test_get_triggers_hidden_auxiliary(
|
||||||
assert triggers == unordered(expected_triggers)
|
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."""
|
"""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(
|
assert await async_setup_component(
|
||||||
hass,
|
hass,
|
||||||
|
@ -118,7 +122,7 @@ async def test_if_fires_on_state_change(hass: HomeAssistant, calls) -> None:
|
||||||
"platform": "device",
|
"platform": "device",
|
||||||
"domain": DOMAIN,
|
"domain": DOMAIN,
|
||||||
"device_id": "",
|
"device_id": "",
|
||||||
"entity_id": "button.entity",
|
"entity_id": entry.id,
|
||||||
"type": "pressed",
|
"type": "pressed",
|
||||||
},
|
},
|
||||||
"action": {
|
"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
|
# 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()
|
await hass.async_block_till_done()
|
||||||
assert len(calls) == 1
|
assert len(calls) == 1
|
||||||
assert calls[0].data[
|
assert (
|
||||||
"some"
|
calls[0].data["some"]
|
||||||
] == "to - device - {} - unknown - 2021-01-01T23:59:59+00:00 - None - 0".format(
|
== f"to - device - {entry.entity_id} - unknown - 2021-01-01T23:59:59+00:00 - None - 0"
|
||||||
"button.entity"
|
)
|
||||||
|
|
||||||
|
|
||||||
|
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"
|
||||||
)
|
)
|
||||||
|
|
|
@ -91,16 +91,17 @@ async def test_enumerate_remote(hass: HomeAssistant, utcnow) -> None:
|
||||||
await setup_test_component(hass, create_remote)
|
await setup_test_component(hass, create_remote)
|
||||||
|
|
||||||
entity_registry = er.async_get(hass)
|
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_registry = dr.async_get(hass)
|
||||||
device = device_registry.async_get(entry.device_id)
|
device = device_registry.async_get(bat_sensor.device_id)
|
||||||
|
|
||||||
expected = [
|
expected = [
|
||||||
{
|
{
|
||||||
"device_id": device.id,
|
"device_id": device.id,
|
||||||
"domain": "sensor",
|
"domain": "sensor",
|
||||||
"entity_id": "sensor.testdevice_battery",
|
"entity_id": bat_sensor.entity_id,
|
||||||
"platform": "device",
|
"platform": "device",
|
||||||
"type": "battery_level",
|
"type": "battery_level",
|
||||||
"metadata": {"secondary": True},
|
"metadata": {"secondary": True},
|
||||||
|
@ -108,7 +109,7 @@ async def test_enumerate_remote(hass: HomeAssistant, utcnow) -> None:
|
||||||
{
|
{
|
||||||
"device_id": device.id,
|
"device_id": device.id,
|
||||||
"domain": "button",
|
"domain": "button",
|
||||||
"entity_id": "button.testdevice_identify",
|
"entity_id": identify_button.id,
|
||||||
"platform": "device",
|
"platform": "device",
|
||||||
"type": "pressed",
|
"type": "pressed",
|
||||||
"metadata": {"secondary": True},
|
"metadata": {"secondary": True},
|
||||||
|
@ -139,16 +140,17 @@ async def test_enumerate_button(hass: HomeAssistant, utcnow) -> None:
|
||||||
await setup_test_component(hass, create_button)
|
await setup_test_component(hass, create_button)
|
||||||
|
|
||||||
entity_registry = er.async_get(hass)
|
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_registry = dr.async_get(hass)
|
||||||
device = device_registry.async_get(entry.device_id)
|
device = device_registry.async_get(bat_sensor.device_id)
|
||||||
|
|
||||||
expected = [
|
expected = [
|
||||||
{
|
{
|
||||||
"device_id": device.id,
|
"device_id": device.id,
|
||||||
"domain": "sensor",
|
"domain": "sensor",
|
||||||
"entity_id": "sensor.testdevice_battery",
|
"entity_id": bat_sensor.entity_id,
|
||||||
"platform": "device",
|
"platform": "device",
|
||||||
"type": "battery_level",
|
"type": "battery_level",
|
||||||
"metadata": {"secondary": True},
|
"metadata": {"secondary": True},
|
||||||
|
@ -156,7 +158,7 @@ async def test_enumerate_button(hass: HomeAssistant, utcnow) -> None:
|
||||||
{
|
{
|
||||||
"device_id": device.id,
|
"device_id": device.id,
|
||||||
"domain": "button",
|
"domain": "button",
|
||||||
"entity_id": "button.testdevice_identify",
|
"entity_id": identify_button.id,
|
||||||
"platform": "device",
|
"platform": "device",
|
||||||
"type": "pressed",
|
"type": "pressed",
|
||||||
"metadata": {"secondary": True},
|
"metadata": {"secondary": True},
|
||||||
|
@ -186,16 +188,17 @@ async def test_enumerate_doorbell(hass: HomeAssistant, utcnow) -> None:
|
||||||
await setup_test_component(hass, create_doorbell)
|
await setup_test_component(hass, create_doorbell)
|
||||||
|
|
||||||
entity_registry = er.async_get(hass)
|
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_registry = dr.async_get(hass)
|
||||||
device = device_registry.async_get(entry.device_id)
|
device = device_registry.async_get(bat_sensor.device_id)
|
||||||
|
|
||||||
expected = [
|
expected = [
|
||||||
{
|
{
|
||||||
"device_id": device.id,
|
"device_id": device.id,
|
||||||
"domain": "sensor",
|
"domain": "sensor",
|
||||||
"entity_id": "sensor.testdevice_battery",
|
"entity_id": bat_sensor.entity_id,
|
||||||
"platform": "device",
|
"platform": "device",
|
||||||
"type": "battery_level",
|
"type": "battery_level",
|
||||||
"metadata": {"secondary": True},
|
"metadata": {"secondary": True},
|
||||||
|
@ -203,7 +206,7 @@ async def test_enumerate_doorbell(hass: HomeAssistant, utcnow) -> None:
|
||||||
{
|
{
|
||||||
"device_id": device.id,
|
"device_id": device.id,
|
||||||
"domain": "button",
|
"domain": "button",
|
||||||
"entity_id": "button.testdevice_identify",
|
"entity_id": identify_button.id,
|
||||||
"platform": "device",
|
"platform": "device",
|
||||||
"type": "pressed",
|
"type": "pressed",
|
||||||
"metadata": {"secondary": True},
|
"metadata": {"secondary": True},
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue