Use entity registry id in text device actions (#95398)

This commit is contained in:
Erik Montnemery 2023-06-27 20:24:40 +02:00 committed by GitHub
parent d7b7deb95f
commit 179e1da164
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 88 additions and 14 deletions

View File

@ -38,6 +38,7 @@ ENTITY_PLATFORMS = {
Platform.REMOTE.value, Platform.REMOTE.value,
Platform.SELECT.value, Platform.SELECT.value,
Platform.SWITCH.value, Platform.SWITCH.value,
Platform.TEXT.value,
Platform.VACUUM.value, Platform.VACUUM.value,
Platform.WATER_HEATER.value, Platform.WATER_HEATER.value,
} }

View File

@ -3,6 +3,7 @@ from __future__ import annotations
import voluptuous as vol import voluptuous as vol
from homeassistant.components.device_automation import async_validate_entity_schema
from homeassistant.const import ( from homeassistant.const import (
ATTR_ENTITY_ID, ATTR_ENTITY_ID,
CONF_DEVICE_ID, CONF_DEVICE_ID,
@ -19,15 +20,22 @@ from .const import ATTR_VALUE, DOMAIN, SERVICE_SET_VALUE
ATYP_SET_VALUE = "set_value" ATYP_SET_VALUE = "set_value"
ACTION_SCHEMA = cv.DEVICE_ACTION_BASE_SCHEMA.extend( _ACTION_SCHEMA = cv.DEVICE_ACTION_BASE_SCHEMA.extend(
{ {
vol.Required(CONF_TYPE): ATYP_SET_VALUE, vol.Required(CONF_TYPE): ATYP_SET_VALUE,
vol.Required(CONF_ENTITY_ID): cv.entity_domain(DOMAIN), vol.Required(CONF_ENTITY_ID): cv.entity_id_or_uuid,
vol.Required(ATTR_VALUE): cv.string, vol.Required(ATTR_VALUE): cv.string,
} }
) )
async def async_validate_action_config(
hass: HomeAssistant, config: ConfigType
) -> ConfigType:
"""Validate config."""
return async_validate_entity_schema(hass, config, _ACTION_SCHEMA)
async def async_get_actions( async def async_get_actions(
hass: HomeAssistant, device_id: str hass: HomeAssistant, device_id: str
) -> list[dict[str, str]]: ) -> list[dict[str, str]]:
@ -44,7 +52,7 @@ async def async_get_actions(
{ {
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: ATYP_SET_VALUE, CONF_TYPE: ATYP_SET_VALUE,
} }
) )

View File

@ -40,7 +40,7 @@ async def test_get_actions(
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
) )
hass.states.async_set("text.test_5678", 0.5, {"min_value": 0.0, "max_value": 1.0}) hass.states.async_set("text.test_5678", 0.5, {"min_value": 0.0, "max_value": 1.0})
@ -49,7 +49,7 @@ async def test_get_actions(
"domain": DOMAIN, "domain": DOMAIN,
"type": "set_value", "type": "set_value",
"device_id": device_entry.id, "device_id": device_entry.id,
"entity_id": "text.test_5678", "entity_id": entity_entry.id,
"metadata": {"secondary": False}, "metadata": {"secondary": False},
}, },
] ]
@ -82,7 +82,7 @@ async def test_get_actions_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",
@ -96,7 +96,7 @@ async def test_get_actions_hidden_auxiliary(
"domain": DOMAIN, "domain": DOMAIN,
"type": action, "type": action,
"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 action in ["set_value"] for action in ["set_value"]
@ -119,7 +119,7 @@ async def test_get_action_no_state(
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_actions = [ expected_actions = [
@ -127,7 +127,7 @@ async def test_get_action_no_state(
"domain": DOMAIN, "domain": DOMAIN,
"type": "set_value", "type": "set_value",
"device_id": device_entry.id, "device_id": device_entry.id,
"entity_id": "text.test_5678", "entity_id": entity_entry.id,
"metadata": {"secondary": False}, "metadata": {"secondary": False},
}, },
] ]
@ -137,9 +137,10 @@ async def test_get_action_no_state(
assert actions == unordered(expected_actions) assert actions == unordered(expected_actions)
async def test_action(hass: HomeAssistant) -> None: async def test_action(hass: HomeAssistant, entity_registry: er.EntityRegistry) -> None:
"""Test for actions.""" """Test for actions."""
hass.states.async_set("text.entity", 0.5, {"min_value": 0.0, "max_value": 1.0}) entry = entity_registry.async_get_or_create(DOMAIN, "test", "5678")
hass.states.async_set(entry.entity_id, 0.5, {"min_value": 0.0, "max_value": 1.0})
assert await async_setup_component( assert await async_setup_component(
hass, hass,
@ -154,7 +155,7 @@ async def test_action(hass: HomeAssistant) -> None:
"action": { "action": {
"domain": DOMAIN, "domain": DOMAIN,
"device_id": "abcdefgh", "device_id": "abcdefgh",
"entity_id": "text.entity", "entity_id": entry.id,
"type": "set_value", "type": "set_value",
"value": 0.3, "value": 0.3,
}, },
@ -173,14 +174,78 @@ async def test_action(hass: HomeAssistant) -> None:
assert len(calls) == 1 assert len(calls) == 1
async def test_capabilities(hass: HomeAssistant) -> None: async def test_action_legacy(
hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> None:
"""Test for actions."""
entry = entity_registry.async_get_or_create(DOMAIN, "test", "5678")
hass.states.async_set(entry.entity_id, 0.5, {"min_value": 0.0, "max_value": 1.0})
assert await async_setup_component(
hass,
automation.DOMAIN,
{
automation.DOMAIN: [
{
"trigger": {
"platform": "event",
"event_type": "test_event_set_value",
},
"action": {
"domain": DOMAIN,
"device_id": "abcdefgh",
"entity_id": entry.entity_id,
"type": "set_value",
"value": 0.3,
},
},
]
},
)
calls = async_mock_service(hass, DOMAIN, "set_value")
assert len(calls) == 0
hass.bus.async_fire("test_event_set_value")
await hass.async_block_till_done()
assert len(calls) == 1
async def test_capabilities(
hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> None:
"""Test getting capabilities.""" """Test getting capabilities."""
entry = entity_registry.async_get_or_create(DOMAIN, "test", "5678")
capabilities = await device_action.async_get_action_capabilities( capabilities = await device_action.async_get_action_capabilities(
hass, hass,
{ {
"domain": DOMAIN, "domain": DOMAIN,
"device_id": "abcdefgh", "device_id": "abcdefgh",
"entity_id": "text.entity", "entity_id": entry.id,
"type": "set_value",
},
)
assert capabilities and "extra_fields" in capabilities
assert voluptuous_serialize.convert(
capabilities["extra_fields"], custom_serializer=cv.custom_serializer
) == [{"name": "value", "required": True, "type": "string"}]
async def test_capabilities_legacy(
hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> None:
"""Test getting capabilities."""
entry = entity_registry.async_get_or_create(DOMAIN, "test", "5678")
capabilities = await device_action.async_get_action_capabilities(
hass,
{
"domain": DOMAIN,
"device_id": "abcdefgh",
"entity_id": entry.entity_id,
"type": "set_value", "type": "set_value",
}, },
) )