diff --git a/homeassistant/components/device_automation/helpers.py b/homeassistant/components/device_automation/helpers.py index 2734f7c58aa..038ded07e8a 100644 --- a/homeassistant/components/device_automation/helpers.py +++ b/homeassistant/components/device_automation/helpers.py @@ -38,6 +38,7 @@ ENTITY_PLATFORMS = { Platform.REMOTE.value, Platform.SELECT.value, Platform.SWITCH.value, + Platform.TEXT.value, Platform.VACUUM.value, Platform.WATER_HEATER.value, } diff --git a/homeassistant/components/text/device_action.py b/homeassistant/components/text/device_action.py index 89fbbc7fbc7..9d06d4b7441 100644 --- a/homeassistant/components/text/device_action.py +++ b/homeassistant/components/text/device_action.py @@ -3,6 +3,7 @@ from __future__ import annotations import voluptuous as vol +from homeassistant.components.device_automation import async_validate_entity_schema from homeassistant.const import ( ATTR_ENTITY_ID, CONF_DEVICE_ID, @@ -19,15 +20,22 @@ from .const import ATTR_VALUE, DOMAIN, SERVICE_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_ENTITY_ID): cv.entity_domain(DOMAIN), + vol.Required(CONF_ENTITY_ID): cv.entity_id_or_uuid, 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( hass: HomeAssistant, device_id: str ) -> list[dict[str, str]]: @@ -44,7 +52,7 @@ async def async_get_actions( { CONF_DEVICE_ID: device_id, CONF_DOMAIN: DOMAIN, - CONF_ENTITY_ID: entry.entity_id, + CONF_ENTITY_ID: entry.id, CONF_TYPE: ATYP_SET_VALUE, } ) diff --git a/tests/components/text/test_device_action.py b/tests/components/text/test_device_action.py index 09a1a3176f5..88bf692b711 100644 --- a/tests/components/text/test_device_action.py +++ b/tests/components/text/test_device_action.py @@ -40,7 +40,7 @@ async def test_get_actions( 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 ) 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, "type": "set_value", "device_id": device_entry.id, - "entity_id": "text.test_5678", + "entity_id": entity_entry.id, "metadata": {"secondary": False}, }, ] @@ -82,7 +82,7 @@ async def test_get_actions_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", @@ -96,7 +96,7 @@ async def test_get_actions_hidden_auxiliary( "domain": DOMAIN, "type": action, "device_id": device_entry.id, - "entity_id": f"{DOMAIN}.test_5678", + "entity_id": entity_entry.id, "metadata": {"secondary": True}, } for action in ["set_value"] @@ -119,7 +119,7 @@ async def test_get_action_no_state( 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_actions = [ @@ -127,7 +127,7 @@ async def test_get_action_no_state( "domain": DOMAIN, "type": "set_value", "device_id": device_entry.id, - "entity_id": "text.test_5678", + "entity_id": entity_entry.id, "metadata": {"secondary": False}, }, ] @@ -137,9 +137,10 @@ async def test_get_action_no_state( 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.""" - 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( hass, @@ -154,7 +155,7 @@ async def test_action(hass: HomeAssistant) -> None: "action": { "domain": DOMAIN, "device_id": "abcdefgh", - "entity_id": "text.entity", + "entity_id": entry.id, "type": "set_value", "value": 0.3, }, @@ -173,14 +174,78 @@ async def test_action(hass: HomeAssistant) -> None: 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.""" + 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": "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", }, )