Home Assistant Unofficial Reference 2024.12.1
device_action.py
Go to the documentation of this file.
1 """Provides device automations for RFXCOM RFXtrx."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 
7 import voluptuous as vol
8 
9 from homeassistant.components.device_automation import InvalidDeviceAutomationConfig
10 from homeassistant.const import CONF_DEVICE_ID, CONF_DOMAIN, CONF_TYPE
11 from homeassistant.core import Context, HomeAssistant
13 from homeassistant.helpers.typing import ConfigType, TemplateVarsType
14 
15 from . import DATA_RFXOBJECT, DOMAIN
16 from .helpers import async_get_device_object
17 
18 CONF_DATA = "data"
19 CONF_SUBTYPE = "subtype"
20 
21 ACTION_TYPE_COMMAND = "send_command"
22 ACTION_TYPE_STATUS = "send_status"
23 
24 ACTION_TYPES = {
25  ACTION_TYPE_COMMAND,
26  ACTION_TYPE_STATUS,
27 }
28 
29 ACTION_SELECTION = {
30  ACTION_TYPE_COMMAND: "COMMANDS",
31  ACTION_TYPE_STATUS: "STATUS",
32 }
33 
34 ACTION_SCHEMA = cv.DEVICE_ACTION_BASE_SCHEMA.extend(
35  {
36  vol.Required(CONF_TYPE): vol.In(ACTION_TYPES),
37  vol.Required(CONF_SUBTYPE): str,
38  }
39 )
40 
41 
43  hass: HomeAssistant, device_id: str
44 ) -> list[dict[str, str]]:
45  """List device actions for RFXCOM RFXtrx devices."""
46 
47  try:
48  device = async_get_device_object(hass, device_id)
49  except ValueError:
50  return []
51 
52  return [
53  {
54  CONF_DEVICE_ID: device_id,
55  CONF_DOMAIN: DOMAIN,
56  CONF_TYPE: action_type,
57  CONF_SUBTYPE: value,
58  }
59  for action_type in ACTION_TYPES
60  if hasattr(device, action_type)
61  for value in getattr(device, ACTION_SELECTION[action_type], {}).values()
62  ]
63 
64 
66  hass: HomeAssistant, device_id: str, action_type: str
67 ) -> tuple[dict[str, str], Callable[..., None]]:
68  device = async_get_device_object(hass, device_id)
69  send_fun = getattr(device, action_type)
70  commands = getattr(device, ACTION_SELECTION[action_type], {})
71  return commands, send_fun
72 
73 
75  hass: HomeAssistant, config: ConfigType
76 ) -> ConfigType:
77  """Validate config."""
78  config = ACTION_SCHEMA(config)
79  commands, _ = _get_commands(hass, config[CONF_DEVICE_ID], config[CONF_TYPE])
80  sub_type = config[CONF_SUBTYPE]
81 
82  if sub_type not in commands.values():
84  f"Subtype {sub_type} not found in device commands {commands}"
85  )
86 
87  return config
88 
89 
91  hass: HomeAssistant,
92  config: ConfigType,
93  variables: TemplateVarsType,
94  context: Context | None,
95 ) -> None:
96  """Execute a device action."""
97  config = ACTION_SCHEMA(config)
98 
99  rfx = hass.data[DOMAIN][DATA_RFXOBJECT]
100  commands, send_fun = _get_commands(hass, config[CONF_DEVICE_ID], config[CONF_TYPE])
101  sub_type = config[CONF_SUBTYPE]
102 
103  for key, value in commands.items():
104  if value == sub_type:
105  await hass.async_add_executor_job(send_fun, rfx.transport, key)
106  return
ConfigType async_validate_action_config(HomeAssistant hass, ConfigType config)
None async_call_action_from_config(HomeAssistant hass, ConfigType config, TemplateVarsType variables, Context|None context)
list[dict[str, str]] async_get_actions(HomeAssistant hass, str device_id)
tuple[dict[str, str], Callable[..., None]] _get_commands(HomeAssistant hass, str device_id, str action_type)
RFXtrxDevice async_get_device_object(HomeAssistant hass, str device_id)
Definition: helpers.py:12