Use entity registry id in toggle_entity device automations (#94995)
* Use entity registry id in toggle_entity device automations * Update tests --------- Co-authored-by: J. Nick Koston <nick@koston.org>
This commit is contained in:
parent
408c613731
commit
a338e7e242
27 changed files with 1352 additions and 480 deletions
|
@ -7,7 +7,7 @@ from pytest_unordered import unordered
|
|||
import homeassistant.components.automation as automation
|
||||
from homeassistant.components.device_automation import DeviceAutomationType
|
||||
from homeassistant.components.switch import DOMAIN
|
||||
from homeassistant.const import CONF_PLATFORM, STATE_OFF, STATE_ON, EntityCategory
|
||||
from homeassistant.const import STATE_OFF, STATE_ON, EntityCategory
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
||||
from homeassistant.helpers.entity_registry import RegistryEntryHider
|
||||
|
@ -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 ["changed_states", "turned_off", "turned_on"]
|
||||
|
@ -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 ["changed_states", "turned_off", "turned_on"]
|
||||
|
@ -144,17 +144,47 @@ async def test_get_trigger_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 switch 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(
|
||||
hass: HomeAssistant, calls, enable_custom_integrations: None
|
||||
hass: HomeAssistant,
|
||||
entity_registry: er.EntityRegistry,
|
||||
calls,
|
||||
enable_custom_integrations: None,
|
||||
) -> None:
|
||||
"""Test for turn_on and turn_off triggers firing."""
|
||||
platform = getattr(hass.components, f"test.{DOMAIN}")
|
||||
entry = entity_registry.async_get_or_create(DOMAIN, "test", "5678")
|
||||
|
||||
platform.init()
|
||||
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
ent1, ent2, ent3 = platform.ENTITIES
|
||||
hass.states.async_set(entry.entity_id, STATE_ON)
|
||||
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
|
@ -166,7 +196,7 @@ async def test_if_fires_on_state_change(
|
|||
"platform": "device",
|
||||
"domain": DOMAIN,
|
||||
"device_id": "",
|
||||
"entity_id": ent1.entity_id,
|
||||
"entity_id": entry.id,
|
||||
"type": "turned_on",
|
||||
},
|
||||
"action": {
|
||||
|
@ -190,7 +220,7 @@ async def test_if_fires_on_state_change(
|
|||
"platform": "device",
|
||||
"domain": DOMAIN,
|
||||
"device_id": "",
|
||||
"entity_id": ent1.entity_id,
|
||||
"entity_id": entry.id,
|
||||
"type": "turned_off",
|
||||
},
|
||||
"action": {
|
||||
|
@ -214,7 +244,7 @@ async def test_if_fires_on_state_change(
|
|||
"platform": "device",
|
||||
"domain": DOMAIN,
|
||||
"device_id": "",
|
||||
"entity_id": ent1.entity_id,
|
||||
"entity_id": entry.id,
|
||||
"type": "changed_states",
|
||||
},
|
||||
"action": {
|
||||
|
@ -237,37 +267,35 @@ async def test_if_fires_on_state_change(
|
|||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert hass.states.get(ent1.entity_id).state == STATE_ON
|
||||
assert len(calls) == 0
|
||||
|
||||
hass.states.async_set(ent1.entity_id, STATE_OFF)
|
||||
hass.states.async_set(entry.entity_id, STATE_OFF)
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 2
|
||||
assert {calls[0].data["some"], calls[1].data["some"]} == {
|
||||
f"turn_off device - {ent1.entity_id} - on - off - None",
|
||||
f"turn_on_or_off device - {ent1.entity_id} - on - off - None",
|
||||
f"turn_off device - {entry.entity_id} - on - off - None",
|
||||
f"turn_on_or_off device - {entry.entity_id} - on - off - None",
|
||||
}
|
||||
|
||||
hass.states.async_set(ent1.entity_id, STATE_ON)
|
||||
hass.states.async_set(entry.entity_id, STATE_ON)
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 4
|
||||
assert {calls[2].data["some"], calls[3].data["some"]} == {
|
||||
f"turn_on device - {ent1.entity_id} - off - on - None",
|
||||
f"turn_on_or_off device - {ent1.entity_id} - off - on - None",
|
||||
f"turn_on device - {entry.entity_id} - off - on - None",
|
||||
f"turn_on_or_off device - {entry.entity_id} - off - on - None",
|
||||
}
|
||||
|
||||
|
||||
async def test_if_fires_on_state_change_with_for(
|
||||
hass: HomeAssistant, calls, enable_custom_integrations: None
|
||||
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 with delay."""
|
||||
platform = getattr(hass.components, f"test.{DOMAIN}")
|
||||
"""Test for turn_on and turn_off triggers firing."""
|
||||
entry = entity_registry.async_get_or_create(DOMAIN, "test", "5678")
|
||||
|
||||
platform.init()
|
||||
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
ent1, ent2, ent3 = platform.ENTITIES
|
||||
hass.states.async_set(entry.entity_id, STATE_ON)
|
||||
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
|
@ -279,7 +307,62 @@ async def test_if_fires_on_state_change_with_for(
|
|||
"platform": "device",
|
||||
"domain": DOMAIN,
|
||||
"device_id": "",
|
||||
"entity_id": ent1.entity_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 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"]
|
||||
== f"turn_off device - {entry.entity_id} - on - off - None"
|
||||
)
|
||||
|
||||
|
||||
async def test_if_fires_on_state_change_with_for(
|
||||
hass: HomeAssistant,
|
||||
entity_registry: er.EntityRegistry,
|
||||
calls,
|
||||
enable_custom_integrations: None,
|
||||
) -> 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_ON)
|
||||
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: [
|
||||
{
|
||||
"trigger": {
|
||||
"platform": "device",
|
||||
"domain": DOMAIN,
|
||||
"device_id": "",
|
||||
"entity_id": entry.id,
|
||||
"type": "turned_off",
|
||||
"for": {"seconds": 5},
|
||||
},
|
||||
|
@ -303,16 +386,16 @@ async def test_if_fires_on_state_change_with_for(
|
|||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert hass.states.get(ent1.entity_id).state == STATE_ON
|
||||
assert len(calls) == 0
|
||||
|
||||
hass.states.async_set(ent1.entity_id, STATE_OFF)
|
||||
hass.states.async_set(entry.entity_id, STATE_OFF)
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 0
|
||||
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=10))
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
await hass.async_block_till_done()
|
||||
assert calls[0].data["some"] == "turn_off device - {} - on - off - 0:00:05".format(
|
||||
ent1.entity_id
|
||||
assert (
|
||||
calls[0].data["some"]
|
||||
== f"turn_off device - {entry.entity_id} - on - off - 0:00:05"
|
||||
)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue