Home Assistant Unofficial Reference 2024.12.1
action.py
Go to the documentation of this file.
1 """Device action validator."""
2 
3 from __future__ import annotations
4 
5 from typing import Any, Protocol
6 
7 import voluptuous as vol
8 
9 from homeassistant.const import CONF_DOMAIN
10 from homeassistant.core import Context, HomeAssistant
11 from homeassistant.helpers import config_validation as cv
12 from homeassistant.helpers.typing import ConfigType
13 
14 from . import DeviceAutomationType, async_get_device_automation_platform
15 from .helpers import async_validate_device_automation_config
16 
17 
19  """Define the format of device_action modules.
20 
21  Each module must define either ACTION_SCHEMA or async_validate_action_config.
22  """
23 
24  ACTION_SCHEMA: vol.Schema
25 
27  self, hass: HomeAssistant, config: ConfigType
28  ) -> ConfigType:
29  """Validate config."""
30 
32  self,
33  hass: HomeAssistant,
34  config: ConfigType,
35  variables: dict[str, Any],
36  context: Context | None,
37  ) -> None:
38  """Execute a device action."""
39 
41  self, hass: HomeAssistant, config: ConfigType
42  ) -> dict[str, vol.Schema]:
43  """List action capabilities."""
44 
45  async def async_get_actions(
46  self, hass: HomeAssistant, device_id: str
47  ) -> list[dict[str, Any]]:
48  """List actions."""
49 
50 
52  hass: HomeAssistant, config: ConfigType
53 ) -> ConfigType:
54  """Validate config."""
56  hass, config, cv.DEVICE_ACTION_SCHEMA, DeviceAutomationType.ACTION
57  )
58 
59 
61  hass: HomeAssistant,
62  config: ConfigType,
63  variables: dict[str, Any],
64  context: Context | None,
65 ) -> None:
66  """Execute a device action."""
67  platform = await async_get_device_automation_platform(
68  hass,
69  config[CONF_DOMAIN],
70  DeviceAutomationType.ACTION,
71  )
72  await platform.async_call_action_from_config(hass, config, variables, context)
ConfigType async_validate_action_config(self, HomeAssistant hass, ConfigType config)
Definition: action.py:28
dict[str, vol.Schema] async_get_action_capabilities(HomeAssistant hass, ConfigType config)
list[dict[str, str]] async_get_actions(HomeAssistant hass, str device_id)
None async_call_action_from_config(HomeAssistant hass, ConfigType config, dict[str, Any] variables, Context|None context)
Definition: action.py:65
ConfigType async_validate_action_config(HomeAssistant hass, ConfigType config)
Definition: action.py:53
ConfigType async_validate_device_automation_config(HomeAssistant hass, ConfigType config, vol.Schema automation_schema, DeviceAutomationType automation_type)
Definition: helpers.py:53
DeviceAutomationTriggerProtocol async_get_device_automation_platform(HomeAssistant hass, str domain, Literal[DeviceAutomationType.TRIGGER] automation_type)
Definition: __init__.py:137