Home Assistant Unofficial Reference 2024.12.1
device_condition.py
Go to the documentation of this file.
1 """Provide the device automations for Alarm control panel."""
2 
3 from __future__ import annotations
4 
5 from typing import Final
6 
7 import voluptuous as vol
8 
9 from homeassistant.const import (
10  ATTR_ENTITY_ID,
11  CONF_CONDITION,
12  CONF_DEVICE_ID,
13  CONF_DOMAIN,
14  CONF_ENTITY_ID,
15  CONF_TYPE,
16 )
17 from homeassistant.core import HomeAssistant, callback
18 from homeassistant.helpers import (
19  condition,
20  config_validation as cv,
21  entity_registry as er,
22 )
23 from homeassistant.helpers.config_validation import DEVICE_CONDITION_BASE_SCHEMA
24 from homeassistant.helpers.entity import get_supported_features
25 from homeassistant.helpers.typing import ConfigType, TemplateVarsType
26 
27 from . import DOMAIN, AlarmControlPanelState
28 from .const import (
29  CONDITION_ARMED_AWAY,
30  CONDITION_ARMED_CUSTOM_BYPASS,
31  CONDITION_ARMED_HOME,
32  CONDITION_ARMED_NIGHT,
33  CONDITION_ARMED_VACATION,
34  CONDITION_DISARMED,
35  CONDITION_TRIGGERED,
36  AlarmControlPanelEntityFeature,
37 )
38 
39 CONDITION_TYPES: Final[set[str]] = {
40  CONDITION_TRIGGERED,
41  CONDITION_DISARMED,
42  CONDITION_ARMED_HOME,
43  CONDITION_ARMED_AWAY,
44  CONDITION_ARMED_NIGHT,
45  CONDITION_ARMED_VACATION,
46  CONDITION_ARMED_CUSTOM_BYPASS,
47 }
48 
49 CONDITION_SCHEMA: Final = DEVICE_CONDITION_BASE_SCHEMA.extend(
50  {
51  vol.Required(CONF_ENTITY_ID): cv.entity_id_or_uuid,
52  vol.Required(CONF_TYPE): vol.In(CONDITION_TYPES),
53  }
54 )
55 
56 
58  hass: HomeAssistant, device_id: str
59 ) -> list[dict[str, str]]:
60  """List device conditions for Alarm control panel devices."""
61  registry = er.async_get(hass)
62  conditions = []
63 
64  # Get all the integrations entities for this device
65  for entry in er.async_entries_for_device(registry, device_id):
66  if entry.domain != DOMAIN:
67  continue
68 
69  supported_features = get_supported_features(hass, entry.entity_id)
70 
71  # Add conditions for each entity that belongs to this integration
72  base_condition = {
73  CONF_CONDITION: "device",
74  CONF_DEVICE_ID: device_id,
75  CONF_DOMAIN: DOMAIN,
76  CONF_ENTITY_ID: entry.id,
77  }
78 
79  conditions += [
80  {**base_condition, CONF_TYPE: CONDITION_DISARMED},
81  {**base_condition, CONF_TYPE: CONDITION_TRIGGERED},
82  ]
83  if supported_features & AlarmControlPanelEntityFeature.ARM_HOME:
84  conditions.append({**base_condition, CONF_TYPE: CONDITION_ARMED_HOME})
85  if supported_features & AlarmControlPanelEntityFeature.ARM_AWAY:
86  conditions.append({**base_condition, CONF_TYPE: CONDITION_ARMED_AWAY})
87  if supported_features & AlarmControlPanelEntityFeature.ARM_NIGHT:
88  conditions.append({**base_condition, CONF_TYPE: CONDITION_ARMED_NIGHT})
89  if supported_features & AlarmControlPanelEntityFeature.ARM_VACATION:
90  conditions.append({**base_condition, CONF_TYPE: CONDITION_ARMED_VACATION})
91  if supported_features & AlarmControlPanelEntityFeature.ARM_CUSTOM_BYPASS:
92  conditions.append(
93  {**base_condition, CONF_TYPE: CONDITION_ARMED_CUSTOM_BYPASS}
94  )
95 
96  return conditions
97 
98 
99 @callback
101  hass: HomeAssistant, config: ConfigType
102 ) -> condition.ConditionCheckerType:
103  """Create a function to test a device condition."""
104  if config[CONF_TYPE] == CONDITION_TRIGGERED:
105  state = AlarmControlPanelState.TRIGGERED
106  elif config[CONF_TYPE] == CONDITION_DISARMED:
107  state = AlarmControlPanelState.DISARMED
108  elif config[CONF_TYPE] == CONDITION_ARMED_HOME:
109  state = AlarmControlPanelState.ARMED_HOME
110  elif config[CONF_TYPE] == CONDITION_ARMED_AWAY:
111  state = AlarmControlPanelState.ARMED_AWAY
112  elif config[CONF_TYPE] == CONDITION_ARMED_NIGHT:
113  state = AlarmControlPanelState.ARMED_NIGHT
114  elif config[CONF_TYPE] == CONDITION_ARMED_VACATION:
115  state = AlarmControlPanelState.ARMED_VACATION
116  elif config[CONF_TYPE] == CONDITION_ARMED_CUSTOM_BYPASS:
117  state = AlarmControlPanelState.ARMED_CUSTOM_BYPASS
118 
119  registry = er.async_get(hass)
120  entity_id = er.async_resolve_entity_id(registry, config[ATTR_ENTITY_ID])
121 
122  def test_is_state(hass: HomeAssistant, variables: TemplateVarsType) -> bool:
123  """Test if an entity is a certain state."""
124  return condition.state(hass, entity_id, state)
125 
126  return test_is_state
condition.ConditionCheckerType async_condition_from_config(HomeAssistant hass, ConfigType config)
list[dict[str, str]] async_get_conditions(HomeAssistant hass, str device_id)
int get_supported_features(HomeAssistant hass, str entity_id)
Definition: entity.py:169