mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 05:07:41 +00:00
Teach select device trigger about entity registry ids (#94981)
This commit is contained in:
parent
7f0be78ebb
commit
af97857c87
@ -40,7 +40,7 @@ from .const import ( # noqa: F401
|
||||
CONF_TURNED_OFF,
|
||||
CONF_TURNED_ON,
|
||||
)
|
||||
from .exceptions import DeviceNotFound, InvalidDeviceAutomationConfig
|
||||
from .exceptions import DeviceNotFound, EntityNotFound, InvalidDeviceAutomationConfig
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .action import DeviceAutomationActionProtocol
|
||||
@ -313,7 +313,7 @@ async def _async_get_device_automation_capabilities(
|
||||
|
||||
try:
|
||||
capabilities = await getattr(platform, function_name)(hass, automation)
|
||||
except InvalidDeviceAutomationConfig:
|
||||
except (EntityNotFound, InvalidDeviceAutomationConfig):
|
||||
return {}
|
||||
|
||||
capabilities = capabilities.copy()
|
||||
@ -328,6 +328,18 @@ async def _async_get_device_automation_capabilities(
|
||||
return capabilities # type: ignore[no-any-return]
|
||||
|
||||
|
||||
@callback
|
||||
def async_get_entity_registry_entry_or_raise(
|
||||
hass: HomeAssistant, entity_registry_id: str
|
||||
) -> er.RegistryEntry:
|
||||
"""Get an entity registry entry from entry ID or raise."""
|
||||
entity_registry = er.async_get(hass)
|
||||
entry = entity_registry.async_get(entity_registry_id)
|
||||
if entry is None:
|
||||
raise EntityNotFound
|
||||
return entry
|
||||
|
||||
|
||||
def handle_device_errors(
|
||||
func: Callable[[HomeAssistant, ActiveConnection, dict[str, Any]], Awaitable[None]]
|
||||
) -> Callable[
|
||||
|
@ -8,3 +8,7 @@ class InvalidDeviceAutomationConfig(HomeAssistantError):
|
||||
|
||||
class DeviceNotFound(HomeAssistantError):
|
||||
"""When referenced device not found."""
|
||||
|
||||
|
||||
class EntityNotFound(HomeAssistantError):
|
||||
"""When referenced entity not found."""
|
||||
|
@ -3,7 +3,10 @@ from __future__ import annotations
|
||||
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components.device_automation import DEVICE_TRIGGER_BASE_SCHEMA
|
||||
from homeassistant.components.device_automation import (
|
||||
DEVICE_TRIGGER_BASE_SCHEMA,
|
||||
async_get_entity_registry_entry_or_raise,
|
||||
)
|
||||
from homeassistant.components.homeassistant.triggers.state import (
|
||||
CONF_FOR,
|
||||
CONF_FROM,
|
||||
@ -31,7 +34,7 @@ TRIGGER_TYPES = {"current_option_changed"}
|
||||
|
||||
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_TO): vol.Any(vol.Coerce(str)),
|
||||
vol.Optional(CONF_FROM): vol.Any(vol.Coerce(str)),
|
||||
@ -50,7 +53,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: "current_option_changed",
|
||||
}
|
||||
for entry in er.async_entries_for_device(registry, device_id)
|
||||
@ -89,8 +92,10 @@ async def async_get_trigger_capabilities(
|
||||
hass: HomeAssistant, config: ConfigType
|
||||
) -> dict[str, vol.Schema]:
|
||||
"""List trigger capabilities."""
|
||||
|
||||
try:
|
||||
options = get_capability(hass, config[CONF_ENTITY_ID], ATTR_OPTIONS) or []
|
||||
entry = async_get_entity_registry_entry_or_raise(hass, config[CONF_ENTITY_ID])
|
||||
options = get_capability(hass, entry.entity_id, ATTR_OPTIONS) or []
|
||||
except HomeAssistantError:
|
||||
options = []
|
||||
|
||||
|
@ -45,7 +45,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 = [
|
||||
@ -54,7 +54,7 @@ async def test_get_triggers(
|
||||
"domain": DOMAIN,
|
||||
"type": "current_option_changed",
|
||||
"device_id": device_entry.id,
|
||||
"entity_id": f"{DOMAIN}.test_5678",
|
||||
"entity_id": entity_entry.id,
|
||||
"metadata": {"secondary": False},
|
||||
}
|
||||
]
|
||||
@ -87,7 +87,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",
|
||||
@ -101,7 +101,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 ["current_option_changed"]
|
||||
@ -112,10 +112,14 @@ 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."""
|
||||
entry = entity_registry.async_get_or_create(DOMAIN, "test", "5678")
|
||||
|
||||
hass.states.async_set(
|
||||
"select.entity", "option1", {"options": ["option1", "option2", "option3"]}
|
||||
entry.entity_id, "option1", {"options": ["option1", "option2", "option3"]}
|
||||
)
|
||||
|
||||
assert await async_setup_component(
|
||||
@ -128,7 +132,7 @@ async def test_if_fires_on_state_change(hass: HomeAssistant, calls) -> None:
|
||||
"platform": "device",
|
||||
"domain": DOMAIN,
|
||||
"device_id": "",
|
||||
"entity_id": "select.entity",
|
||||
"entity_id": entry.id,
|
||||
"type": "current_option_changed",
|
||||
"to": "option2",
|
||||
},
|
||||
@ -149,7 +153,7 @@ async def test_if_fires_on_state_change(hass: HomeAssistant, calls) -> None:
|
||||
"platform": "device",
|
||||
"domain": DOMAIN,
|
||||
"device_id": "",
|
||||
"entity_id": "select.entity",
|
||||
"entity_id": entry.id,
|
||||
"type": "current_option_changed",
|
||||
"from": "option2",
|
||||
},
|
||||
@ -170,7 +174,7 @@ async def test_if_fires_on_state_change(hass: HomeAssistant, calls) -> None:
|
||||
"platform": "device",
|
||||
"domain": DOMAIN,
|
||||
"device_id": "",
|
||||
"entity_id": "select.entity",
|
||||
"entity_id": entry.id,
|
||||
"type": "current_option_changed",
|
||||
"from": "option3",
|
||||
"to": "option1",
|
||||
@ -192,37 +196,94 @@ async def test_if_fires_on_state_change(hass: HomeAssistant, calls) -> None:
|
||||
)
|
||||
|
||||
# Test triggering device trigger with a to state
|
||||
hass.states.async_set("select.entity", "option2")
|
||||
hass.states.async_set(entry.entity_id, "option2")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
assert calls[0].data[
|
||||
"some"
|
||||
] == "to - device - {} - option1 - option2 - None - 0".format("select.entity")
|
||||
assert (
|
||||
calls[0].data["some"]
|
||||
== f"to - device - {entry.entity_id} - option1 - option2 - None - 0"
|
||||
)
|
||||
|
||||
# Test triggering device trigger with a from state
|
||||
hass.states.async_set("select.entity", "option3")
|
||||
hass.states.async_set(entry.entity_id, "option3")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 2
|
||||
assert calls[1].data[
|
||||
"some"
|
||||
] == "from - device - {} - option2 - option3 - None - 0".format("select.entity")
|
||||
assert (
|
||||
calls[1].data["some"]
|
||||
== f"from - device - {entry.entity_id} - option2 - option3 - None - 0"
|
||||
)
|
||||
|
||||
# Test triggering device trigger with both a from and to state
|
||||
hass.states.async_set("select.entity", "option1")
|
||||
hass.states.async_set(entry.entity_id, "option1")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 3
|
||||
assert calls[2].data[
|
||||
"some"
|
||||
] == "from-to - device - {} - option3 - option1 - None - 0".format("select.entity")
|
||||
assert (
|
||||
calls[2].data["some"]
|
||||
== f"from-to - device - {entry.entity_id} - option3 - option1 - None - 0"
|
||||
)
|
||||
|
||||
|
||||
async def test_get_trigger_capabilities(hass: HomeAssistant) -> None:
|
||||
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, "option1", {"options": ["option1", "option2", "option3"]}
|
||||
)
|
||||
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: [
|
||||
{
|
||||
"trigger": {
|
||||
"platform": "device",
|
||||
"domain": DOMAIN,
|
||||
"device_id": "",
|
||||
"entity_id": entry.entity_id,
|
||||
"type": "current_option_changed",
|
||||
"to": "option2",
|
||||
},
|
||||
"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, "option2")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
assert (
|
||||
calls[0].data["some"]
|
||||
== f"to - device - {entry.entity_id} - option1 - option2 - None - 0"
|
||||
)
|
||||
|
||||
|
||||
async def test_get_trigger_capabilities(
|
||||
hass: HomeAssistant, entity_registry: er.EntityRegistry
|
||||
) -> None:
|
||||
"""Test we get the expected capabilities from a select trigger."""
|
||||
entry = entity_registry.async_get_or_create(DOMAIN, "test", "5678")
|
||||
|
||||
config = {
|
||||
"platform": "device",
|
||||
"domain": DOMAIN,
|
||||
"type": "current_option_changed",
|
||||
"entity_id": "select.test",
|
||||
"entity_id": entry.id,
|
||||
"to": "option1",
|
||||
}
|
||||
|
||||
@ -253,7 +314,120 @@ async def test_get_trigger_capabilities(hass: HomeAssistant) -> None:
|
||||
]
|
||||
|
||||
# Mock an entity
|
||||
hass.states.async_set("select.test", "option1", {"options": ["option1", "option2"]})
|
||||
hass.states.async_set(
|
||||
entry.entity_id, "option1", {"options": ["option1", "option2"]}
|
||||
)
|
||||
|
||||
# Test if we get the right capabilities now
|
||||
capabilities = await async_get_trigger_capabilities(hass, config)
|
||||
assert capabilities
|
||||
assert "extra_fields" in capabilities
|
||||
assert voluptuous_serialize.convert(
|
||||
capabilities["extra_fields"], custom_serializer=cv.custom_serializer
|
||||
) == [
|
||||
{
|
||||
"name": "from",
|
||||
"optional": True,
|
||||
"type": "select",
|
||||
"options": [("option1", "option1"), ("option2", "option2")],
|
||||
},
|
||||
{
|
||||
"name": "to",
|
||||
"optional": True,
|
||||
"type": "select",
|
||||
"options": [("option1", "option1"), ("option2", "option2")],
|
||||
},
|
||||
{
|
||||
"name": "for",
|
||||
"optional": True,
|
||||
"type": "positive_time_period_dict",
|
||||
},
|
||||
]
|
||||
|
||||
|
||||
async def test_get_trigger_capabilities_unknown(
|
||||
hass: HomeAssistant, entity_registry: er.EntityRegistry
|
||||
) -> None:
|
||||
"""Test we get the expected capabilities from a select trigger."""
|
||||
config = {
|
||||
"platform": "device",
|
||||
"domain": DOMAIN,
|
||||
"type": "current_option_changed",
|
||||
"entity_id": "12345",
|
||||
"to": "option1",
|
||||
}
|
||||
|
||||
# Test when entity doesn't exists
|
||||
capabilities = await async_get_trigger_capabilities(hass, config)
|
||||
assert capabilities
|
||||
assert "extra_fields" in capabilities
|
||||
assert voluptuous_serialize.convert(
|
||||
capabilities["extra_fields"], custom_serializer=cv.custom_serializer
|
||||
) == [
|
||||
{
|
||||
"name": "from",
|
||||
"optional": True,
|
||||
"type": "select",
|
||||
"options": [],
|
||||
},
|
||||
{
|
||||
"name": "to",
|
||||
"optional": True,
|
||||
"type": "select",
|
||||
"options": [],
|
||||
},
|
||||
{
|
||||
"name": "for",
|
||||
"optional": True,
|
||||
"type": "positive_time_period_dict",
|
||||
},
|
||||
]
|
||||
|
||||
|
||||
async def test_get_trigger_capabilities_legacy(
|
||||
hass: HomeAssistant, entity_registry: er.EntityRegistry
|
||||
) -> None:
|
||||
"""Test we get the expected capabilities from a select trigger."""
|
||||
entry = entity_registry.async_get_or_create(DOMAIN, "test", "5678")
|
||||
|
||||
config = {
|
||||
"platform": "device",
|
||||
"domain": DOMAIN,
|
||||
"type": "current_option_changed",
|
||||
"entity_id": entry.entity_id,
|
||||
"to": "option1",
|
||||
}
|
||||
|
||||
# Test when entity doesn't exists
|
||||
capabilities = await async_get_trigger_capabilities(hass, config)
|
||||
assert capabilities
|
||||
assert "extra_fields" in capabilities
|
||||
assert voluptuous_serialize.convert(
|
||||
capabilities["extra_fields"], custom_serializer=cv.custom_serializer
|
||||
) == [
|
||||
{
|
||||
"name": "from",
|
||||
"optional": True,
|
||||
"type": "select",
|
||||
"options": [],
|
||||
},
|
||||
{
|
||||
"name": "to",
|
||||
"optional": True,
|
||||
"type": "select",
|
||||
"options": [],
|
||||
},
|
||||
{
|
||||
"name": "for",
|
||||
"optional": True,
|
||||
"type": "positive_time_period_dict",
|
||||
},
|
||||
]
|
||||
|
||||
# Mock an entity
|
||||
hass.states.async_set(
|
||||
entry.entity_id, "option1", {"options": ["option1", "option2"]}
|
||||
)
|
||||
|
||||
# Test if we get the right capabilities now
|
||||
capabilities = await async_get_trigger_capabilities(hass, config)
|
||||
|
Loading…
x
Reference in New Issue
Block a user