Beispiel #1
0
async def async_and_from_config(
        hass: HomeAssistant,
        config: ConfigType,
        config_validation: bool = True) -> ConditionCheckerType:
    """Create multi condition matcher using 'AND'."""
    if config_validation:
        config = cv.AND_CONDITION_SCHEMA(config)
    checks = [
        await async_from_config(hass, entry, False)
        for entry in config["conditions"]
    ]

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

        return True

    return if_and_condition
Beispiel #2
0
def async_and_from_config(config: ConfigType, config_validation: bool = True):
    """Create multi condition matcher using 'AND'."""
    if config_validation:
        config = cv.AND_CONDITION_SCHEMA(config)
    checks = None

    def if_and_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 not check(hass, variables):
                    return False
        except Exception as ex:  # pylint: disable=broad-except
            _LOGGER.warning("Error during and-condition: %s", ex)
            return False

        return True

    return if_and_condition
Beispiel #3
0
async def async_and_from_config(
    hass: HomeAssistant, config: ConfigType, config_validation: bool = True
) -> ConditionCheckerType:
    """Create multi condition matcher using 'AND'."""
    if config_validation:
        config = cv.AND_CONDITION_SCHEMA(config)
    checks = [
        await async_from_config(hass, entry, False) for entry in config["conditions"]
    ]

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

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

        return True

    return if_and_condition
Beispiel #4
0
def and_from_config(config, config_validation=True):
    """Create multi condition matcher using 'AND'."""
    if config_validation:
        config = cv.AND_CONDITION_SCHEMA(config)
    checks = [from_config(entry) for entry in config['conditions']]

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

        return True

    return if_and_condition