mirror of
https://github.com/home-assistant/core.git
synced 2025-04-23 16:57:53 +00:00
Teach vacuum device trigger about entity registry ids (#94989)
This commit is contained in:
parent
90f5b1c323
commit
e204e80528
@ -24,7 +24,7 @@ TRIGGER_TYPES = {"cleaning", "docked"}
|
||||
|
||||
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_FOR): cv.positive_time_period_dict,
|
||||
}
|
||||
@ -48,7 +48,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,
|
||||
}
|
||||
for trigger in TRIGGER_TYPES
|
||||
|
@ -46,7 +46,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 = [
|
||||
@ -55,7 +55,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 ["cleaning", "docked"]
|
||||
@ -89,7 +89,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",
|
||||
@ -103,7 +103,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 ["cleaning", "docked"]
|
||||
@ -145,9 +145,45 @@ async def test_get_trigger_capabilities(
|
||||
}
|
||||
|
||||
|
||||
async def test_if_fires_on_state_change(hass: HomeAssistant, calls) -> None:
|
||||
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 vacuum device."""
|
||||
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
|
||||
)
|
||||
|
||||
triggers = await async_get_device_automations(
|
||||
hass, DeviceAutomationType.TRIGGER, device_entry.id
|
||||
)
|
||||
assert len(triggers) == 2
|
||||
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 == {
|
||||
"extra_fields": [
|
||||
{"name": "for", "optional": True, "type": "positive_time_period_dict"}
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
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("vacuum.entity", STATE_DOCKED)
|
||||
entry = entity_registry.async_get_or_create(DOMAIN, "test", "5678")
|
||||
|
||||
hass.states.async_set(entry.entity_id, STATE_DOCKED)
|
||||
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
@ -159,7 +195,7 @@ async def test_if_fires_on_state_change(hass: HomeAssistant, calls) -> None:
|
||||
"platform": "device",
|
||||
"domain": DOMAIN,
|
||||
"device_id": "",
|
||||
"entity_id": "vacuum.entity",
|
||||
"entity_id": entry.id,
|
||||
"type": "cleaning",
|
||||
},
|
||||
"action": {
|
||||
@ -178,7 +214,7 @@ async def test_if_fires_on_state_change(hass: HomeAssistant, calls) -> None:
|
||||
"platform": "device",
|
||||
"domain": DOMAIN,
|
||||
"device_id": "",
|
||||
"entity_id": "vacuum.entity",
|
||||
"entity_id": entry.id,
|
||||
"type": "docked",
|
||||
},
|
||||
"action": {
|
||||
@ -197,26 +233,31 @@ async def test_if_fires_on_state_change(hass: HomeAssistant, calls) -> None:
|
||||
)
|
||||
|
||||
# Fake that the entity is cleaning
|
||||
hass.states.async_set("vacuum.entity", STATE_CLEANING)
|
||||
hass.states.async_set(entry.entity_id, STATE_CLEANING)
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
assert (
|
||||
calls[0].data["some"] == "cleaning - device - vacuum.entity - docked - cleaning"
|
||||
calls[0].data["some"]
|
||||
== f"cleaning - device - {entry.entity_id} - docked - cleaning"
|
||||
)
|
||||
|
||||
# Fake that the entity is docked
|
||||
hass.states.async_set("vacuum.entity", STATE_DOCKED)
|
||||
hass.states.async_set(entry.entity_id, STATE_DOCKED)
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 2
|
||||
assert (
|
||||
calls[1].data["some"] == "docked - device - vacuum.entity - cleaning - docked"
|
||||
calls[1].data["some"]
|
||||
== f"docked - device - {entry.entity_id} - cleaning - docked"
|
||||
)
|
||||
|
||||
|
||||
async def test_if_fires_on_state_change_with_for(hass: HomeAssistant, calls) -> None:
|
||||
"""Test for triggers firing with delay."""
|
||||
entity_id = f"{DOMAIN}.entity"
|
||||
hass.states.async_set(entity_id, STATE_DOCKED)
|
||||
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, STATE_DOCKED)
|
||||
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
@ -228,7 +269,53 @@ async def test_if_fires_on_state_change_with_for(hass: HomeAssistant, calls) ->
|
||||
"platform": "device",
|
||||
"domain": DOMAIN,
|
||||
"device_id": "",
|
||||
"entity_id": entity_id,
|
||||
"entity_id": entry.entity_id,
|
||||
"type": "cleaning",
|
||||
},
|
||||
"action": {
|
||||
"service": "test.automation",
|
||||
"data_template": {
|
||||
"some": (
|
||||
"cleaning - {{ trigger.platform}} - "
|
||||
"{{ trigger.entity_id}} - {{ trigger.from_state.state}} - "
|
||||
"{{ trigger.to_state.state}}"
|
||||
)
|
||||
},
|
||||
},
|
||||
},
|
||||
]
|
||||
},
|
||||
)
|
||||
|
||||
# Fake that the entity is cleaning
|
||||
hass.states.async_set(entry.entity_id, STATE_CLEANING)
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
assert (
|
||||
calls[0].data["some"]
|
||||
== f"cleaning - device - {entry.entity_id} - docked - cleaning"
|
||||
)
|
||||
|
||||
|
||||
async def test_if_fires_on_state_change_with_for(
|
||||
hass: HomeAssistant, entity_registry: er.EntityRegistry, calls
|
||||
) -> None:
|
||||
"""Test for triggers firing with delay."""
|
||||
entry = entity_registry.async_get_or_create(DOMAIN, "test", "5678")
|
||||
|
||||
hass.states.async_set(entry.entity_id, STATE_DOCKED)
|
||||
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: [
|
||||
{
|
||||
"trigger": {
|
||||
"platform": "device",
|
||||
"domain": DOMAIN,
|
||||
"device_id": "",
|
||||
"entity_id": entry.id,
|
||||
"type": "cleaning",
|
||||
"for": {"seconds": 5},
|
||||
},
|
||||
@ -252,10 +339,9 @@ async def test_if_fires_on_state_change_with_for(hass: HomeAssistant, calls) ->
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert hass.states.get(entity_id).state == STATE_DOCKED
|
||||
assert len(calls) == 0
|
||||
|
||||
hass.states.async_set(entity_id, STATE_CLEANING)
|
||||
hass.states.async_set(entry.entity_id, STATE_CLEANING)
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 0
|
||||
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=10))
|
||||
@ -264,5 +350,5 @@ async def test_if_fires_on_state_change_with_for(hass: HomeAssistant, calls) ->
|
||||
await hass.async_block_till_done()
|
||||
assert (
|
||||
calls[0].data["some"]
|
||||
== f"turn_off device - {entity_id} - docked - cleaning - 0:00:05"
|
||||
== f"turn_off device - {entry.entity_id} - docked - cleaning - 0:00:05"
|
||||
)
|
||||
|
Loading…
x
Reference in New Issue
Block a user