Add device action support to Select entity (#51990)
This commit is contained in:
parent
06edc731c5
commit
87a43eacb7
5 changed files with 215 additions and 0 deletions
|
@ -5,4 +5,6 @@ DOMAIN = "select"
|
||||||
ATTR_OPTIONS = "options"
|
ATTR_OPTIONS = "options"
|
||||||
ATTR_OPTION = "option"
|
ATTR_OPTION = "option"
|
||||||
|
|
||||||
|
CONF_OPTION = "option"
|
||||||
|
|
||||||
SERVICE_SELECT_OPTION = "select_option"
|
SERVICE_SELECT_OPTION = "select_option"
|
||||||
|
|
80
homeassistant/components/select/device_action.py
Normal file
80
homeassistant/components/select/device_action.py
Normal file
|
@ -0,0 +1,80 @@
|
||||||
|
"""Provides device actions for Select."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
import voluptuous as vol
|
||||||
|
|
||||||
|
from homeassistant.const import (
|
||||||
|
ATTR_ENTITY_ID,
|
||||||
|
CONF_DEVICE_ID,
|
||||||
|
CONF_DOMAIN,
|
||||||
|
CONF_ENTITY_ID,
|
||||||
|
CONF_TYPE,
|
||||||
|
)
|
||||||
|
from homeassistant.core import Context, HomeAssistant
|
||||||
|
from homeassistant.helpers import entity_registry
|
||||||
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
from homeassistant.helpers.typing import ConfigType
|
||||||
|
|
||||||
|
from .const import ATTR_OPTION, ATTR_OPTIONS, CONF_OPTION, DOMAIN, SERVICE_SELECT_OPTION
|
||||||
|
|
||||||
|
ACTION_TYPES = {"select_option"}
|
||||||
|
|
||||||
|
ACTION_SCHEMA = cv.DEVICE_ACTION_BASE_SCHEMA.extend(
|
||||||
|
{
|
||||||
|
vol.Required(CONF_TYPE): vol.In(ACTION_TYPES),
|
||||||
|
vol.Required(CONF_ENTITY_ID): cv.entity_domain(DOMAIN),
|
||||||
|
vol.Required(CONF_OPTION): str,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def async_get_actions(hass: HomeAssistant, device_id: str) -> list[dict]:
|
||||||
|
"""List device actions for Select devices."""
|
||||||
|
registry = await entity_registry.async_get_registry(hass)
|
||||||
|
return [
|
||||||
|
{
|
||||||
|
CONF_DEVICE_ID: device_id,
|
||||||
|
CONF_DOMAIN: DOMAIN,
|
||||||
|
CONF_ENTITY_ID: entry.entity_id,
|
||||||
|
CONF_TYPE: "select_option",
|
||||||
|
}
|
||||||
|
for entry in entity_registry.async_entries_for_device(registry, device_id)
|
||||||
|
if entry.domain == DOMAIN
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
async def async_call_action_from_config(
|
||||||
|
hass: HomeAssistant, config: dict, variables: dict, context: Context | None
|
||||||
|
) -> None:
|
||||||
|
"""Execute a device action."""
|
||||||
|
await hass.services.async_call(
|
||||||
|
DOMAIN,
|
||||||
|
SERVICE_SELECT_OPTION,
|
||||||
|
{
|
||||||
|
ATTR_ENTITY_ID: config[CONF_ENTITY_ID],
|
||||||
|
ATTR_OPTION: config[CONF_OPTION],
|
||||||
|
},
|
||||||
|
blocking=True,
|
||||||
|
context=context,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def async_get_action_capabilities(
|
||||||
|
hass: HomeAssistant, config: ConfigType
|
||||||
|
) -> dict[str, Any]:
|
||||||
|
"""List action capabilities."""
|
||||||
|
state = hass.states.get(config[CONF_ENTITY_ID])
|
||||||
|
if state is None:
|
||||||
|
return {}
|
||||||
|
|
||||||
|
return {
|
||||||
|
"extra_fields": vol.Schema(
|
||||||
|
{
|
||||||
|
vol.Required(CONF_OPTION): vol.In(
|
||||||
|
state.attributes.get(ATTR_OPTIONS, [])
|
||||||
|
),
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
|
@ -3,6 +3,9 @@
|
||||||
"device_automation": {
|
"device_automation": {
|
||||||
"trigger_type": {
|
"trigger_type": {
|
||||||
"current_option_changed": "{entity_name} option changed"
|
"current_option_changed": "{entity_name} option changed"
|
||||||
|
},
|
||||||
|
"action_type": {
|
||||||
|
"select_option": "Change {entity_name} option"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,8 @@
|
||||||
{
|
{
|
||||||
"device_automation": {
|
"device_automation": {
|
||||||
|
"action_type": {
|
||||||
|
"select_option": "Change {entity_name} option"
|
||||||
|
},
|
||||||
"trigger_type": {
|
"trigger_type": {
|
||||||
"current_option_changed": "{entity_name} option changed"
|
"current_option_changed": "{entity_name} option changed"
|
||||||
}
|
}
|
||||||
|
|
127
tests/components/select/test_device_action.py
Normal file
127
tests/components/select/test_device_action.py
Normal file
|
@ -0,0 +1,127 @@
|
||||||
|
"""The tests for Select device actions."""
|
||||||
|
import pytest
|
||||||
|
import voluptuous_serialize
|
||||||
|
|
||||||
|
from homeassistant.components import automation
|
||||||
|
from homeassistant.components.select import DOMAIN
|
||||||
|
from homeassistant.components.select.device_action import async_get_action_capabilities
|
||||||
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.helpers import (
|
||||||
|
config_validation as cv,
|
||||||
|
device_registry,
|
||||||
|
entity_registry,
|
||||||
|
)
|
||||||
|
from homeassistant.setup import async_setup_component
|
||||||
|
|
||||||
|
from tests.common import (
|
||||||
|
MockConfigEntry,
|
||||||
|
assert_lists_same,
|
||||||
|
async_get_device_automations,
|
||||||
|
async_mock_service,
|
||||||
|
mock_device_registry,
|
||||||
|
mock_registry,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def device_reg(hass: HomeAssistant) -> device_registry.DeviceRegistry:
|
||||||
|
"""Return an empty, loaded, registry."""
|
||||||
|
return mock_device_registry(hass)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def entity_reg(hass: HomeAssistant) -> entity_registry.EntityRegistry:
|
||||||
|
"""Return an empty, loaded, registry."""
|
||||||
|
return mock_registry(hass)
|
||||||
|
|
||||||
|
|
||||||
|
async def test_get_actions(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
device_reg: device_registry.DeviceRegistry,
|
||||||
|
entity_reg: entity_registry.EntityRegistry,
|
||||||
|
) -> None:
|
||||||
|
"""Test we get the expected actions from a select."""
|
||||||
|
config_entry = MockConfigEntry(domain="test", data={})
|
||||||
|
config_entry.add_to_hass(hass)
|
||||||
|
device_entry = device_reg.async_get_or_create(
|
||||||
|
config_entry_id=config_entry.entry_id,
|
||||||
|
connections={(device_registry.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
|
||||||
|
)
|
||||||
|
entity_reg.async_get_or_create(DOMAIN, "test", "5678", device_id=device_entry.id)
|
||||||
|
expected_actions = [
|
||||||
|
{
|
||||||
|
"domain": DOMAIN,
|
||||||
|
"type": "select_option",
|
||||||
|
"device_id": device_entry.id,
|
||||||
|
"entity_id": "select.test_5678",
|
||||||
|
}
|
||||||
|
]
|
||||||
|
actions = await async_get_device_automations(hass, "action", device_entry.id)
|
||||||
|
assert_lists_same(actions, expected_actions)
|
||||||
|
|
||||||
|
|
||||||
|
async def test_action(hass: HomeAssistant) -> None:
|
||||||
|
"""Test for select_option action."""
|
||||||
|
assert await async_setup_component(
|
||||||
|
hass,
|
||||||
|
automation.DOMAIN,
|
||||||
|
{
|
||||||
|
automation.DOMAIN: [
|
||||||
|
{
|
||||||
|
"trigger": {
|
||||||
|
"platform": "event",
|
||||||
|
"event_type": "test_event",
|
||||||
|
},
|
||||||
|
"action": {
|
||||||
|
"domain": DOMAIN,
|
||||||
|
"device_id": "abcdefgh",
|
||||||
|
"entity_id": "select.entity",
|
||||||
|
"type": "select_option",
|
||||||
|
"option": "option1",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
]
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
select_calls = async_mock_service(hass, DOMAIN, "select_option")
|
||||||
|
|
||||||
|
hass.bus.async_fire("test_event")
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
assert len(select_calls) == 1
|
||||||
|
assert select_calls[0].domain == DOMAIN
|
||||||
|
assert select_calls[0].service == "select_option"
|
||||||
|
assert select_calls[0].data == {"entity_id": "select.entity", "option": "option1"}
|
||||||
|
|
||||||
|
|
||||||
|
async def test_get_trigger_capabilities(hass: HomeAssistant) -> None:
|
||||||
|
"""Test we get the expected capabilities from a select action."""
|
||||||
|
config = {
|
||||||
|
"platform": "device",
|
||||||
|
"domain": DOMAIN,
|
||||||
|
"type": "select_option",
|
||||||
|
"entity_id": "select.test",
|
||||||
|
"option": "option1",
|
||||||
|
}
|
||||||
|
|
||||||
|
# Test when entity doesn't exists
|
||||||
|
capabilities = await async_get_action_capabilities(hass, config)
|
||||||
|
assert capabilities == {}
|
||||||
|
|
||||||
|
# Mock an entity
|
||||||
|
hass.states.async_set("select.test", "option1", {"options": ["option1", "option2"]})
|
||||||
|
|
||||||
|
# Test if we get the right capabilities now
|
||||||
|
capabilities = await async_get_action_capabilities(hass, config)
|
||||||
|
assert capabilities
|
||||||
|
assert "extra_fields" in capabilities
|
||||||
|
assert voluptuous_serialize.convert(
|
||||||
|
capabilities["extra_fields"], custom_serializer=cv.custom_serializer
|
||||||
|
) == [
|
||||||
|
{
|
||||||
|
"name": "option",
|
||||||
|
"required": True,
|
||||||
|
"type": "select",
|
||||||
|
"options": [("option1", "option1"), ("option2", "option2")],
|
||||||
|
},
|
||||||
|
]
|
Loading…
Add table
Add a link
Reference in a new issue