1 """Provide the device automations for Climate."""
3 from __future__
import annotations
5 import voluptuous
as vol
8 async_get_entity_registry_entry_or_raise,
22 config_validation
as cv,
23 entity_registry
as er,
29 from .
import DOMAIN, const
31 CONDITION_TYPES = {
"is_hvac_mode",
"is_preset_mode"}
33 HVAC_MODE_CONDITION = DEVICE_CONDITION_BASE_SCHEMA.extend(
35 vol.Required(CONF_ENTITY_ID): cv.entity_id_or_uuid,
36 vol.Required(CONF_TYPE):
"is_hvac_mode",
37 vol.Required(const.ATTR_HVAC_MODE): vol.In(const.HVAC_MODES),
41 PRESET_MODE_CONDITION = DEVICE_CONDITION_BASE_SCHEMA.extend(
43 vol.Required(CONF_ENTITY_ID): cv.entity_id_or_uuid,
44 vol.Required(CONF_TYPE):
"is_preset_mode",
45 vol.Required(const.ATTR_PRESET_MODE): str,
49 CONDITION_SCHEMA = vol.Any(HVAC_MODE_CONDITION, PRESET_MODE_CONDITION)
53 hass: HomeAssistant, device_id: str
54 ) -> list[dict[str, str]]:
55 """List device conditions for Climate devices."""
56 registry = er.async_get(hass)
60 for entry
in er.async_entries_for_device(registry, device_id):
61 if entry.domain != DOMAIN:
67 CONF_CONDITION:
"device",
68 CONF_DEVICE_ID: device_id,
70 CONF_ENTITY_ID: entry.id,
73 conditions.append({**base_condition, CONF_TYPE:
"is_hvac_mode"})
75 if supported_features & const.ClimateEntityFeature.PRESET_MODE:
76 conditions.append({**base_condition, CONF_TYPE:
"is_preset_mode"})
83 hass: HomeAssistant, config: ConfigType
84 ) -> condition.ConditionCheckerType:
85 """Create a function to test a device condition."""
87 registry = er.async_get(hass)
88 entity_id = er.async_resolve_entity_id(registry, config[ATTR_ENTITY_ID])
90 def test_is_state(hass: HomeAssistant, variables: TemplateVarsType) -> bool:
91 """Test if an entity is a certain state."""
92 if not entity_id
or (state := hass.states.get(entity_id))
is None:
95 if config[CONF_TYPE] ==
"is_hvac_mode":
96 return bool(state.state == config[const.ATTR_HVAC_MODE])
99 state.attributes.get(const.ATTR_PRESET_MODE)
100 == config[const.ATTR_PRESET_MODE]
107 hass: HomeAssistant, config: ConfigType
108 ) -> dict[str, vol.Schema]:
109 """List condition capabilities."""
110 condition_type = config[CONF_TYPE]
114 if condition_type ==
"is_hvac_mode":
117 hass, config[CONF_ENTITY_ID]
120 get_capability(hass, entry.entity_id, const.ATTR_HVAC_MODES)
or []
122 except HomeAssistantError:
124 fields[vol.Required(const.ATTR_HVAC_MODE)] = vol.In(hvac_modes)
126 elif condition_type ==
"is_preset_mode":
129 hass, config[CONF_ENTITY_ID]
132 get_capability(hass, entry.entity_id, const.ATTR_PRESET_MODES)
or []
134 except HomeAssistantError:
136 fields[vol.Required(const.ATTR_PRESET_MODE)] = vol.In(preset_modes)
138 return {
"extra_fields": vol.Schema(fields)}
list[dict[str, str]] async_get_conditions(HomeAssistant hass, str device_id)
condition.ConditionCheckerType async_condition_from_config(HomeAssistant hass, ConfigType config)
dict[str, vol.Schema] async_get_condition_capabilities(HomeAssistant hass, ConfigType config)
er.RegistryEntry async_get_entity_registry_entry_or_raise(HomeAssistant hass, str entity_registry_id)
int get_supported_features(HomeAssistant hass, str entity_id)
Any|None get_capability(HomeAssistant hass, str entity_id, str capability)