Home Assistant Unofficial Reference 2024.12.1
device_condition.py
Go to the documentation of this file.
1 """Provide the device automations for Humidifier."""
2 
3 from __future__ import annotations
4 
5 import voluptuous as vol
6 
8  async_get_entity_registry_entry_or_raise,
9  toggle_entity,
10 )
11 from homeassistant.const import (
12  ATTR_ENTITY_ID,
13  ATTR_MODE,
14  CONF_CONDITION,
15  CONF_DEVICE_ID,
16  CONF_DOMAIN,
17  CONF_ENTITY_ID,
18  CONF_TYPE,
19 )
20 from homeassistant.core import HomeAssistant, callback
21 from homeassistant.exceptions import HomeAssistantError
22 from homeassistant.helpers import (
23  condition,
24  config_validation as cv,
25  entity_registry as er,
26 )
27 from homeassistant.helpers.config_validation import DEVICE_CONDITION_BASE_SCHEMA
28 from homeassistant.helpers.entity import get_capability, get_supported_features
29 from homeassistant.helpers.typing import ConfigType, TemplateVarsType
30 
31 from . import DOMAIN, const
32 
33 TOGGLE_CONDITION = toggle_entity.CONDITION_SCHEMA.extend(
34  {vol.Required(CONF_DOMAIN): DOMAIN}
35 )
36 
37 MODE_CONDITION = DEVICE_CONDITION_BASE_SCHEMA.extend(
38  {
39  vol.Required(CONF_ENTITY_ID): cv.entity_id_or_uuid,
40  vol.Required(CONF_TYPE): "is_mode",
41  vol.Required(ATTR_MODE): str,
42  }
43 )
44 
45 CONDITION_SCHEMA = vol.Any(TOGGLE_CONDITION, MODE_CONDITION)
46 
47 
49  hass: HomeAssistant, device_id: str
50 ) -> list[dict[str, str]]:
51  """List device conditions for Humidifier devices."""
52  registry = er.async_get(hass)
53  conditions = await toggle_entity.async_get_conditions(hass, device_id, DOMAIN)
54 
55  # Get all the integrations entities for this device
56  for entry in er.async_entries_for_device(registry, device_id):
57  if entry.domain != DOMAIN:
58  continue
59 
60  supported_features = get_supported_features(hass, entry.entity_id)
61 
62  if supported_features & const.HumidifierEntityFeature.MODES:
63  conditions.append(
64  {
65  CONF_CONDITION: "device",
66  CONF_DEVICE_ID: device_id,
67  CONF_DOMAIN: DOMAIN,
68  CONF_ENTITY_ID: entry.id,
69  CONF_TYPE: "is_mode",
70  }
71  )
72 
73  return conditions
74 
75 
76 @callback
78  hass: HomeAssistant, config: ConfigType
79 ) -> condition.ConditionCheckerType:
80  """Create a function to test a device condition."""
81  if config[CONF_TYPE] == "is_mode":
82  attribute = ATTR_MODE
83  else:
84  return toggle_entity.async_condition_from_config(hass, config)
85 
86  registry = er.async_get(hass)
87  entity_id = er.async_resolve_entity_id(registry, config[ATTR_ENTITY_ID])
88 
89  def test_is_state(hass: HomeAssistant, variables: TemplateVarsType) -> bool:
90  """Test if an entity is a certain state."""
91  return (
92  entity_id is not None
93  and (state := hass.states.get(entity_id)) is not None
94  and state.attributes.get(attribute) == config[attribute]
95  )
96 
97  return test_is_state
98 
99 
101  hass: HomeAssistant, config: ConfigType
102 ) -> dict[str, vol.Schema]:
103  """List condition capabilities."""
104  condition_type = config[CONF_TYPE]
105 
106  fields = {}
107 
108  if condition_type == "is_mode":
109  try:
111  hass, config[CONF_ENTITY_ID]
112  )
113  modes = (
114  get_capability(hass, entry.entity_id, const.ATTR_AVAILABLE_MODES) or []
115  )
116  except HomeAssistantError:
117  modes = []
118 
119  fields[vol.Required(ATTR_MODE)] = vol.In(modes)
120 
121  return {"extra_fields": vol.Schema(fields)}
122 
123  return await toggle_entity.async_get_condition_capabilities(hass, config)
er.RegistryEntry async_get_entity_registry_entry_or_raise(HomeAssistant hass, str entity_registry_id)
Definition: __init__.py:332
dict[str, vol.Schema] async_get_condition_capabilities(HomeAssistant hass, ConfigType config)
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
Any|None get_capability(HomeAssistant hass, str entity_id, str capability)
Definition: entity.py:139