Exemplo n.º 1
0
async def async_or_from_config(
        hass: HomeAssistant,
        config: ConfigType,
        config_validation: bool = True) -> ConditionCheckerType:
    """Create multi condition matcher using 'OR'."""
    if config_validation:
        config = cv.OR_CONDITION_SCHEMA(config)
    checks = [
        await async_from_config(hass, entry, False)
        for entry in config["conditions"]
    ]

    def if_or_condition(hass: HomeAssistant,
                        variables: TemplateVarsType = None) -> bool:
        """Test and condition."""
        try:
            for check in checks:
                if check(hass, variables):
                    return True
        except Exception as ex:  # pylint: disable=broad-except
            _LOGGER.warning("Error during or-condition: %s", ex)

        return False

    return if_or_condition
Exemplo n.º 2
0
def async_or_from_config(config: ConfigType, config_validation: bool = True):
    """Create multi condition matcher using 'OR'."""
    if config_validation:
        config = cv.OR_CONDITION_SCHEMA(config)
    checks = None

    def if_or_condition(hass: HomeAssistant, variables=None) -> bool:
        """Test and condition."""
        nonlocal checks

        if checks is None:
            checks = [
                async_from_config(entry, False)
                for entry in config['conditions']
            ]

        try:
            for check in checks:
                if check(hass, variables):
                    return True
        except Exception as ex:  # pylint: disable=broad-except
            _LOGGER.warning("Error during or-condition: %s", ex)

        return False

    return if_or_condition
Exemplo n.º 3
0
async def async_or_from_config(
    hass: HomeAssistant, config: ConfigType, config_validation: bool = True
) -> ConditionCheckerType:
    """Create multi condition matcher using 'OR'."""
    if config_validation:
        config = cv.OR_CONDITION_SCHEMA(config)
    checks = [
        await async_from_config(hass, entry, False) for entry in config["conditions"]
    ]

    @trace_condition_function
    def if_or_condition(
        hass: HomeAssistant, variables: TemplateVarsType = None
    ) -> bool:
        """Test or condition."""
        errors = []
        for index, check in enumerate(checks):
            try:
                with trace_path(["conditions", str(index)]):
                    if check(hass, variables):
                        return True
            except ConditionError as ex:
                errors.append(
                    ConditionErrorIndex("or", index=index, total=len(checks), error=ex)
                )

        # Raise the errors if no check was true
        if errors:
            raise ConditionErrorContainer("or", errors=errors)

        return False

    return if_or_condition
Exemplo n.º 4
0
def or_from_config(config, config_validation=True):
    """Create multi condition matcher using 'OR'."""
    if config_validation:
        config = cv.OR_CONDITION_SCHEMA(config)
    checks = [from_config(entry) for entry in config['conditions']]

    def if_or_condition(hass, variables=None):
        """Test and condition."""
        for check in checks:
            try:
                if check(hass, variables):
                    return True
            except Exception as ex:  # pylint: disable=broad-except
                _LOGGER.warning('Error during or-condition: %s', ex)

        return False

    return if_or_condition