Use entity registry id in number device actions (#95273)
parent
c2457b8574
commit
9dc586bd98
|
@ -33,6 +33,7 @@ ENTITY_PLATFORMS = {
|
|||
Platform.HUMIDIFIER.value,
|
||||
Platform.LIGHT.value,
|
||||
Platform.LOCK.value,
|
||||
Platform.NUMBER.value,
|
||||
Platform.REMOTE.value,
|
||||
Platform.SELECT.value,
|
||||
Platform.SWITCH.value,
|
||||
|
|
|
@ -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): vol.Coerce(float),
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
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,
|
||||
}
|
||||
)
|
||||
|
|
|
@ -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("number.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": "number.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": "number.test_5678",
|
||||
"entity_id": entity_entry.id,
|
||||
"metadata": {"secondary": False},
|
||||
},
|
||||
]
|
||||
|
@ -137,9 +137,11 @@ 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("number.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 +156,7 @@ async def test_action(hass: HomeAssistant) -> None:
|
|||
"action": {
|
||||
"domain": DOMAIN,
|
||||
"device_id": "abcdefgh",
|
||||
"entity_id": "number.entity",
|
||||
"entity_id": entry.id,
|
||||
"type": "set_value",
|
||||
"value": 0.3,
|
||||
},
|
||||
|
@ -164,23 +166,96 @@ async def test_action(hass: HomeAssistant) -> None:
|
|||
)
|
||||
|
||||
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
|
||||
assert calls[0].domain == DOMAIN
|
||||
assert calls[0].service == "set_value"
|
||||
assert calls[0].data == {"entity_id": entry.entity_id, "value": 0.3}
|
||||
|
||||
|
||||
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
|
||||
assert calls[0].domain == DOMAIN
|
||||
assert calls[0].service == "set_value"
|
||||
assert calls[0].data == {"entity_id": entry.entity_id, "value": 0.3}
|
||||
|
||||
|
||||
async def test_capabilities(
|
||||
hass: HomeAssistant, entity_registry: er.EntityRegistry
|
||||
) -> None:
|
||||
"""Test getting capabilities."""
|
||||
entry = entity_registry.async_get_or_create(
|
||||
DOMAIN, "test", "5678", device_id="abcdefgh"
|
||||
)
|
||||
capabilities = await device_action.async_get_action_capabilities(
|
||||
hass,
|
||||
{
|
||||
"domain": DOMAIN,
|
||||
"device_id": "abcdefgh",
|
||||
"entity_id": "number.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": "float"}]
|
||||
|
||||
|
||||
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", device_id="abcdefgh"
|
||||
)
|
||||
capabilities = await device_action.async_get_action_capabilities(
|
||||
hass,
|
||||
{
|
||||
"domain": DOMAIN,
|
||||
"device_id": "abcdefgh",
|
||||
"entity_id": entry.entity_id,
|
||||
"type": "set_value",
|
||||
},
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue