Improve device automation validation (#86143)

This commit is contained in:
Erik Montnemery 2023-01-21 00:44:17 +01:00 committed by GitHub
parent 0c8b6c13fc
commit 1e2f00e186
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 255 additions and 116 deletions

View file

@ -1,16 +1,17 @@
"""Device action validator."""
from __future__ import annotations
from typing import Any, Protocol, cast
from typing import Any, Protocol
import voluptuous as vol
from homeassistant.const import CONF_DOMAIN
from homeassistant.core import Context, HomeAssistant
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.typing import ConfigType
from . import DeviceAutomationType, async_get_device_automation_platform
from .exceptions import InvalidDeviceAutomationConfig
from .helpers import async_validate_device_automation_config
class DeviceAutomationActionProtocol(Protocol):
@ -50,15 +51,9 @@ async def async_validate_action_config(
hass: HomeAssistant, config: ConfigType
) -> ConfigType:
"""Validate config."""
try:
platform = await async_get_device_automation_platform(
hass, config[CONF_DOMAIN], DeviceAutomationType.ACTION
)
if hasattr(platform, "async_validate_action_config"):
return await platform.async_validate_action_config(hass, config)
return cast(ConfigType, platform.ACTION_SCHEMA(config))
except InvalidDeviceAutomationConfig as err:
raise vol.Invalid(str(err) or "Invalid action configuration") from err
return await async_validate_device_automation_config(
hass, config, cv.DEVICE_ACTION_SCHEMA, DeviceAutomationType.ACTION
)
async def async_call_action_from_config(