Home Assistant Unofficial Reference 2024.12.1
device_action.py
Go to the documentation of this file.
1 """Provides device automations for Lock."""
2 
3 from __future__ import annotations
4 
5 import voluptuous as vol
6 
7 from homeassistant.components.device_automation import async_validate_entity_schema
8 from homeassistant.const import (
9  ATTR_ENTITY_ID,
10  CONF_DEVICE_ID,
11  CONF_DOMAIN,
12  CONF_ENTITY_ID,
13  CONF_TYPE,
14  SERVICE_LOCK,
15  SERVICE_OPEN,
16  SERVICE_UNLOCK,
17 )
18 from homeassistant.core import Context, HomeAssistant
19 from homeassistant.helpers import entity_registry as er
21 from homeassistant.helpers.entity import get_supported_features
22 from homeassistant.helpers.typing import ConfigType, TemplateVarsType
23 
24 from . import DOMAIN, LockEntityFeature
25 
26 ACTION_TYPES = {"lock", "unlock", "open"}
27 
28 _ACTION_SCHEMA = cv.DEVICE_ACTION_BASE_SCHEMA.extend(
29  {
30  vol.Required(CONF_TYPE): vol.In(ACTION_TYPES),
31  vol.Required(CONF_ENTITY_ID): cv.entity_id_or_uuid,
32  }
33 )
34 
35 
37  hass: HomeAssistant, config: ConfigType
38 ) -> ConfigType:
39  """Validate config."""
40  return async_validate_entity_schema(hass, config, _ACTION_SCHEMA)
41 
42 
44  hass: HomeAssistant, device_id: str
45 ) -> list[dict[str, str]]:
46  """List device actions for Lock devices."""
47  registry = er.async_get(hass)
48  actions = []
49 
50  # Get all the integrations entities for this device
51  for entry in er.async_entries_for_device(registry, device_id):
52  if entry.domain != DOMAIN:
53  continue
54 
55  supported_features = get_supported_features(hass, entry.entity_id)
56 
57  # Add actions for each entity that belongs to this integration
58  base_action = {
59  CONF_DEVICE_ID: device_id,
60  CONF_DOMAIN: DOMAIN,
61  CONF_ENTITY_ID: entry.id,
62  }
63 
64  actions.append({**base_action, CONF_TYPE: "lock"})
65  actions.append({**base_action, CONF_TYPE: "unlock"})
66 
67  if supported_features & (LockEntityFeature.OPEN):
68  actions.append({**base_action, CONF_TYPE: "open"})
69 
70  return actions
71 
72 
74  hass: HomeAssistant,
75  config: ConfigType,
76  variables: TemplateVarsType,
77  context: Context | None,
78 ) -> None:
79  """Execute a device action."""
80  service_data = {ATTR_ENTITY_ID: config[CONF_ENTITY_ID]}
81 
82  if config[CONF_TYPE] == "lock":
83  service = SERVICE_LOCK
84  elif config[CONF_TYPE] == "unlock":
85  service = SERVICE_UNLOCK
86  elif config[CONF_TYPE] == "open":
87  service = SERVICE_OPEN
88 
89  await hass.services.async_call(
90  DOMAIN, service, service_data, blocking=True, context=context
91  )
ConfigType async_validate_entity_schema(HomeAssistant hass, ConfigType config, VolSchemaType schema)
Definition: __init__.py:344
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)
int get_supported_features(HomeAssistant hass, str entity_id)
Definition: entity.py:169