1 """Helpers for device oriented automations."""
3 from __future__
import annotations
5 from typing
import cast
7 import voluptuous
as vol
14 from .
import DeviceAutomationType, async_get_device_automation_platform
15 from .exceptions
import InvalidDeviceAutomationConfig
18 DeviceAutomationType.ACTION:
"async_validate_action_config",
19 DeviceAutomationType.CONDITION:
"async_validate_condition_config",
20 DeviceAutomationType.TRIGGER:
"async_validate_trigger_config",
24 DeviceAutomationType.ACTION:
"ACTION_SCHEMA",
25 DeviceAutomationType.CONDITION:
"CONDITION_SCHEMA",
26 DeviceAutomationType.TRIGGER:
"TRIGGER_SCHEMA",
30 Platform.ALARM_CONTROL_PANEL.value,
31 Platform.BUTTON.value,
32 Platform.CLIMATE.value,
35 Platform.HUMIDIFIER.value,
38 Platform.NUMBER.value,
39 Platform.REMOTE.value,
40 Platform.SELECT.value,
41 Platform.SWITCH.value,
43 Platform.VACUUM.value,
44 Platform.WATER_HEATER.value,
51 automation_schema: vol.Schema,
52 automation_type: DeviceAutomationType,
54 """Validate config."""
55 validated_config: ConfigType = automation_schema(config)
57 hass, validated_config[CONF_DOMAIN], automation_type
61 device_registry = dr.async_get(hass)
62 if not (device := device_registry.async_get(validated_config[CONF_DEVICE_ID])):
65 f
"Unknown device '{validated_config[CONF_DEVICE_ID]}'"
67 if entity_id := validated_config.get(CONF_ENTITY_ID):
69 er.async_validate_entity_id(er.async_get(hass), entity_id)
70 except vol.Invalid
as err:
72 f
"Unknown entity '{entity_id}'"
75 if not hasattr(platform, DYNAMIC_VALIDATOR[automation_type]):
78 ConfigType, getattr(platform, STATIC_VALIDATOR[automation_type])(config)
85 automation_type == DeviceAutomationType.ACTION
86 and validated_config[CONF_DOMAIN]
in ENTITY_PLATFORMS
91 await getattr(platform, DYNAMIC_VALIDATOR[automation_type])(hass, config),
95 device_config_entry =
None
96 for entry_id
in device.config_entries:
98 not (entry := hass.config_entries.async_get_entry(entry_id))
99 or entry.domain != validated_config[CONF_DOMAIN]
102 device_config_entry = entry
105 if not device_config_entry:
108 f
"Device '{validated_config[CONF_DEVICE_ID]}' has no config entry from "
109 f
"domain '{validated_config[CONF_DOMAIN]}'"
112 if not await hass.config_entries.async_wait_component(device_config_entry):
114 return validated_config
119 await getattr(platform, DYNAMIC_VALIDATOR[automation_type])(hass, config),
ConfigType async_validate_device_automation_config(HomeAssistant hass, ConfigType config, vol.Schema automation_schema, DeviceAutomationType automation_type)
DeviceAutomationTriggerProtocol async_get_device_automation_platform(HomeAssistant hass, str domain, Literal[DeviceAutomationType.TRIGGER] automation_type)