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