core/homeassistant/components/number/device_action.py

78 lines
2.1 KiB
Python
Raw Normal View History

2021-01-27 12:40:13 +00:00
"""Provides device actions for Number."""
2021-03-18 12:21:46 +00:00
from __future__ import annotations
2021-01-27 12:40:13 +00:00
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
2021-01-27 12:40:13 +00:00
from .const import ATTR_VALUE, DOMAIN, SERVICE_SET_VALUE
2021-01-27 12:40:13 +00:00
ATYP_SET_VALUE = "set_value"
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(ATTR_VALUE): vol.Coerce(float),
2021-01-27 12:40:13 +00:00
}
)
async def async_get_actions(
hass: HomeAssistant, device_id: str
) -> list[dict[str, str]]:
2021-01-27 12:40:13 +00:00
"""List device actions for Number."""
registry = await entity_registry.async_get_registry(hass)
actions: list[dict[str, str]] = []
2021-01-27 12:40:13 +00:00
# Get all the integrations entities for this device
for entry in entity_registry.async_entries_for_device(registry, device_id):
if entry.domain != DOMAIN:
continue
actions.append(
{
CONF_DEVICE_ID: device_id,
CONF_DOMAIN: DOMAIN,
CONF_ENTITY_ID: entry.entity_id,
CONF_TYPE: ATYP_SET_VALUE,
}
)
return actions
async def async_call_action_from_config(
2021-03-18 12:21:46 +00:00
hass: HomeAssistant, config: dict, variables: dict, context: Context | None
2021-01-27 12:40:13 +00:00
) -> None:
"""Execute a device action."""
await hass.services.async_call(
DOMAIN,
SERVICE_SET_VALUE,
2021-01-27 12:40:13 +00:00
{
ATTR_ENTITY_ID: config[CONF_ENTITY_ID],
ATTR_VALUE: config[ATTR_VALUE],
2021-01-27 12:40:13 +00:00
},
blocking=True,
context=context,
)
async def async_get_action_capabilities(
hass: HomeAssistant, config: ConfigType
) -> dict[str, vol.Schema]:
2021-01-27 12:40:13 +00:00
"""List action capabilities."""
fields = {vol.Required(ATTR_VALUE): vol.Coerce(float)}
2021-01-27 12:40:13 +00:00
return {"extra_fields": vol.Schema(fields)}