Ejemplo n.º 1
0
async def async_validate_condition_config(
        hass: HomeAssistant,
        config: ConfigType | Template) -> ConfigType | Template:
    """Validate config."""
    if isinstance(config, Template):
        return config

    condition = config[CONF_CONDITION]
    if condition in ("and", "not", "or"):
        conditions = []
        for sub_cond in config["conditions"]:
            sub_cond = await async_validate_condition_config(hass, sub_cond)
            conditions.append(sub_cond)
        config["conditions"] = conditions

    if condition == "device":
        config = cv.DEVICE_CONDITION_SCHEMA(config)
        assert not isinstance(config, Template)
        platform = await async_get_device_automation_platform(
            hass, config[CONF_DOMAIN], "condition")
        if hasattr(platform, "async_validate_condition_config"):
            return await platform.async_validate_condition_config(
                hass, config)  # type: ignore
        return cast(ConfigType,
                    platform.CONDITION_SCHEMA(config))  # type: ignore

    return config
Ejemplo n.º 2
0
async def async_validate_condition_config(hass: HomeAssistant,
                                          config: ConfigType) -> ConfigType:
    """Validate device condition config."""
    try:
        config = cv.DEVICE_CONDITION_SCHEMA(config)
        platform = await async_get_device_automation_platform(
            hass, config[CONF_DOMAIN], DeviceAutomationType.CONDITION)
        if hasattr(platform, "async_validate_condition_config"):
            return await platform.async_validate_condition_config(hass, config)
        return cast(ConfigType, platform.CONDITION_SCHEMA(config))
    except InvalidDeviceAutomationConfig as err:
        raise vol.Invalid(str(err)
                          or "Invalid condition configuration") from err
Ejemplo n.º 3
0
async def async_device_from_config(
    hass: HomeAssistant, config: ConfigType, config_validation: bool = True
) -> ConditionCheckerType:
    """Test a device condition."""
    if config_validation:
        config = cv.DEVICE_CONDITION_SCHEMA(config)
    platform = await async_get_device_automation_platform(
        hass, config[CONF_DOMAIN], "condition"
    )
    return cast(
        ConditionCheckerType,
        platform.async_condition_from_config(config, config_validation),  # type: ignore
    )
Ejemplo n.º 4
0
async def async_device_condition_from_config(
        hass: HomeAssistant,
        config: ConfigType,
        config_validation: bool = True) -> Callable[..., bool]:
    """Wrap action method with state based condition."""
    if config_validation:
        config = cv.DEVICE_CONDITION_SCHEMA(config)
    integration = await async_get_integration(hass, config[CONF_DOMAIN])
    platform = integration.get_platform("device_automation")
    return cast(
        Callable[..., bool],
        platform.async_condition_from_config(
            config, config_validation),  # type: ignore
    )
Ejemplo n.º 5
0
async def async_validate_condition_config(hass: HomeAssistant,
                                          config: ConfigType) -> ConfigType:
    """Validate config."""
    condition = config[CONF_CONDITION]
    if condition in ("and", "or"):
        conditions = []
        for sub_cond in config["conditions"]:
            sub_cond = await async_validate_condition_config(hass, sub_cond)
            conditions.append(sub_cond)
        config["conditions"] = conditions

    if condition == "device":
        config = cv.DEVICE_CONDITION_SCHEMA(config)
        platform = await async_get_device_automation_platform(
            hass, config[CONF_DOMAIN], "condition")
        return cast(ConfigType,
                    platform.CONDITION_SCHEMA(config))  # type: ignore

    return config