2019-06-10 22:36:11 +00:00
|
|
|
"""Offer device oriented automation."""
|
|
|
|
import voluptuous as vol
|
|
|
|
|
2019-09-27 15:48:48 +00:00
|
|
|
from homeassistant.components.device_automation import (
|
2021-06-10 17:11:38 +00:00
|
|
|
DEVICE_TRIGGER_BASE_SCHEMA,
|
2019-09-27 15:48:48 +00:00
|
|
|
async_get_device_automation_platform,
|
|
|
|
)
|
2019-10-02 20:14:52 +00:00
|
|
|
from homeassistant.const import CONF_DOMAIN
|
2019-06-10 22:36:11 +00:00
|
|
|
|
2021-09-03 17:15:57 +00:00
|
|
|
from .exceptions import InvalidDeviceAutomationConfig
|
|
|
|
|
2019-08-12 03:38:18 +00:00
|
|
|
# mypy: allow-untyped-defs, no-check-untyped-defs
|
|
|
|
|
2021-06-10 17:11:38 +00:00
|
|
|
TRIGGER_SCHEMA = DEVICE_TRIGGER_BASE_SCHEMA.extend({}, extra=vol.ALLOW_EXTRA)
|
2019-09-27 15:48:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def async_validate_trigger_config(hass, config):
|
|
|
|
"""Validate config."""
|
2019-10-02 20:14:52 +00:00
|
|
|
platform = await async_get_device_automation_platform(
|
|
|
|
hass, config[CONF_DOMAIN], "trigger"
|
|
|
|
)
|
2021-09-03 17:15:57 +00:00
|
|
|
if not hasattr(platform, "async_validate_trigger_config"):
|
|
|
|
return platform.TRIGGER_SCHEMA(config)
|
2019-10-09 19:04:11 +00:00
|
|
|
|
2021-09-03 17:15:57 +00:00
|
|
|
try:
|
|
|
|
return await getattr(platform, "async_validate_trigger_config")(hass, config)
|
|
|
|
except InvalidDeviceAutomationConfig as err:
|
|
|
|
raise vol.Invalid(str(err) or "Invalid trigger configuration") from err
|
2019-06-10 22:36:11 +00:00
|
|
|
|
|
|
|
|
2019-09-24 21:57:05 +00:00
|
|
|
async def async_attach_trigger(hass, config, action, automation_info):
|
2019-06-10 22:36:11 +00:00
|
|
|
"""Listen for trigger."""
|
2019-10-02 20:14:52 +00:00
|
|
|
platform = await async_get_device_automation_platform(
|
|
|
|
hass, config[CONF_DOMAIN], "trigger"
|
|
|
|
)
|
2019-09-24 21:57:05 +00:00
|
|
|
return await platform.async_attach_trigger(hass, config, action, automation_info)
|