2022-02-22 21:15:16 +00:00
|
|
|
"""Device action validator."""
|
|
|
|
from __future__ import annotations
|
|
|
|
|
2022-05-19 16:23:23 +00:00
|
|
|
from collections.abc import Awaitable
|
2022-02-22 21:15:16 +00:00
|
|
|
from typing import Any, Protocol, cast
|
|
|
|
|
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant.const import CONF_DOMAIN
|
|
|
|
from homeassistant.core import Context, HomeAssistant
|
|
|
|
from homeassistant.helpers.typing import ConfigType
|
|
|
|
|
2022-05-23 10:18:17 +00:00
|
|
|
from . import DeviceAutomationType, async_get_device_automation_platform
|
2022-02-22 21:15:16 +00:00
|
|
|
from .exceptions import InvalidDeviceAutomationConfig
|
|
|
|
|
|
|
|
|
|
|
|
class DeviceAutomationActionProtocol(Protocol):
|
|
|
|
"""Define the format of device_action modules.
|
|
|
|
|
|
|
|
Each module must define either ACTION_SCHEMA or async_validate_action_config.
|
|
|
|
"""
|
|
|
|
|
|
|
|
ACTION_SCHEMA: vol.Schema
|
|
|
|
|
|
|
|
async def async_validate_action_config(
|
|
|
|
self, hass: HomeAssistant, config: ConfigType
|
|
|
|
) -> ConfigType:
|
|
|
|
"""Validate config."""
|
|
|
|
|
|
|
|
async def async_call_action_from_config(
|
|
|
|
self,
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigType,
|
|
|
|
variables: dict[str, Any],
|
|
|
|
context: Context | None,
|
|
|
|
) -> None:
|
|
|
|
"""Execute a device action."""
|
2022-05-19 16:23:23 +00:00
|
|
|
|
|
|
|
def async_get_action_capabilities(
|
|
|
|
self, hass: HomeAssistant, config: ConfigType
|
2022-05-23 10:18:17 +00:00
|
|
|
) -> dict[str, vol.Schema] | Awaitable[dict[str, vol.Schema]]:
|
2022-05-19 16:23:23 +00:00
|
|
|
"""List action capabilities."""
|
|
|
|
|
|
|
|
def async_get_actions(
|
|
|
|
self, hass: HomeAssistant, device_id: str
|
2022-05-23 10:18:17 +00:00
|
|
|
) -> list[dict[str, Any]] | Awaitable[list[dict[str, Any]]]:
|
2022-05-19 16:23:23 +00:00
|
|
|
"""List actions."""
|
2022-02-22 21:15:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def async_validate_action_config(
|
|
|
|
hass: HomeAssistant, config: ConfigType
|
|
|
|
) -> ConfigType:
|
|
|
|
"""Validate config."""
|
|
|
|
try:
|
|
|
|
platform = await async_get_device_automation_platform(
|
|
|
|
hass, config[CONF_DOMAIN], DeviceAutomationType.ACTION
|
|
|
|
)
|
|
|
|
if hasattr(platform, "async_validate_action_config"):
|
|
|
|
return await platform.async_validate_action_config(hass, config)
|
|
|
|
return cast(ConfigType, platform.ACTION_SCHEMA(config))
|
|
|
|
except InvalidDeviceAutomationConfig as err:
|
|
|
|
raise vol.Invalid(str(err) or "Invalid action configuration") from err
|
|
|
|
|
|
|
|
|
|
|
|
async def async_call_action_from_config(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigType,
|
|
|
|
variables: dict[str, Any],
|
|
|
|
context: Context | None,
|
|
|
|
) -> None:
|
|
|
|
"""Execute a device action."""
|
|
|
|
platform = await async_get_device_automation_platform(
|
|
|
|
hass,
|
|
|
|
config[CONF_DOMAIN],
|
|
|
|
DeviceAutomationType.ACTION,
|
|
|
|
)
|
|
|
|
await platform.async_call_action_from_config(hass, config, variables, context)
|