示例#1
0
    def if_in_zone(hass: HomeAssistant, variables: TemplateVarsType = None) -> bool:
        """Test if condition."""
        errors = []

        all_ok = True
        for entity_id in entity_ids:
            entity_ok = False
            for zone_entity_id in zone_entity_ids:
                try:
                    if zone(hass, zone_entity_id, entity_id):
                        entity_ok = True
                except ConditionErrorMessage as ex:
                    errors.append(
                        ConditionErrorMessage(
                            "zone",
                            f"error matching {entity_id} with {zone_entity_id}: {ex.message}",
                        )
                    )

            if not entity_ok:
                all_ok = False

        # Raise the errors only if no definitive result was found
        if errors and not all_ok:
            raise ConditionErrorContainer("zone", errors=errors)

        return all_ok
示例#2
0
    def if_state(hass: HomeAssistant,
                 variables: TemplateVarsType = None) -> bool:
        """Test if condition."""
        errors = []
        result: bool = match != ENTITY_MATCH_ANY
        for index, entity_id in enumerate(entity_ids):
            try:
                with trace_path(["entity_id",
                                 str(index)]), trace_condition(variables):
                    if state(hass, entity_id, req_states, for_period,
                             attribute):
                        result = True
                    elif match == ENTITY_MATCH_ALL:
                        return False
            except ConditionError as ex:
                errors.append(
                    ConditionErrorIndex("state",
                                        index=index,
                                        total=len(entity_ids),
                                        error=ex))

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

        return result
示例#3
0
    def if_numeric_state(
        hass: HomeAssistant, variables: TemplateVarsType = None
    ) -> bool:
        """Test numeric state condition."""
        if value_template is not None:
            value_template.hass = hass

        errors = []
        for index, entity_id in enumerate(entity_ids):
            try:
                with trace_path(["entity_id", str(index)]), trace_condition(variables):
                    if not async_numeric_state(
                        hass,
                        entity_id,
                        below,
                        above,
                        value_template,
                        variables,
                        attribute,
                    ):
                        return False
            except ConditionError as ex:
                errors.append(
                    ConditionErrorIndex(
                        "numeric_state", index=index, total=len(entity_ids), error=ex
                    )
                )

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

        return True
示例#4
0
def test_conditionerror_format():
    """Test ConditionError stringifiers."""
    error1 = ConditionErrorMessage("test", "A test error")
    assert str(error1) == "In 'test' condition: A test error"

    error2 = ConditionErrorMessage("test", "Another error")
    assert str(error2) == "In 'test' condition: Another error"

    error_pos1 = ConditionErrorIndex("box", index=0, total=2, error=error1)
    assert (str(error_pos1) == """In 'box' (item 1 of 2):
  In 'test' condition: A test error""")

    error_pos2 = ConditionErrorIndex("box", index=1, total=2, error=error2)
    assert (str(error_pos2) == """In 'box' (item 2 of 2):
  In 'test' condition: Another error""")

    error_container1 = ConditionErrorContainer("box",
                                               errors=[error_pos1, error_pos2])
    assert (str(error_container1) == """In 'box' (item 1 of 2):
  In 'test' condition: A test error
In 'box' (item 2 of 2):
  In 'test' condition: Another error""")

    error_pos3 = ConditionErrorIndex("box", index=0, total=1, error=error1)
    assert (str(error_pos3) == """In 'box':
  In 'test' condition: A test error""")
示例#5
0
    def if_not_condition(
        hass: HomeAssistant, variables: TemplateVarsType = None
    ) -> bool:
        """Test not condition."""
        errors = []
        for index, check in enumerate(checks):
            try:
                with trace_path(["conditions", str(index)]):
                    if check(hass, variables):
                        return False
            except ConditionError as ex:
                errors.append(
                    ConditionErrorIndex("not", index=index, total=len(checks), error=ex)
                )

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

        return True
示例#6
0
文件: condition.py 项目: vog31/core
    def if_and_condition(hass: HomeAssistant,
                         variables: TemplateVarsType = None) -> bool:
        """Test and condition."""
        errors = []
        for index, check in enumerate(checks):
            try:
                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
示例#7
0
文件: condition.py 项目: vog31/core
    def if_state(hass: HomeAssistant,
                 variables: TemplateVarsType = None) -> bool:
        """Test if condition."""
        errors = []
        for index, entity_id in enumerate(entity_ids):
            try:
                if not state(hass, entity_id, req_states, for_period,
                             attribute):
                    return False
            except ConditionError as ex:
                errors.append(
                    ConditionErrorIndex("state",
                                        index=index,
                                        total=len(entity_ids),
                                        error=ex))

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

        return True
示例#8
0
    def if_action(variables=None):
        """AND all conditions."""
        errors = []
        for index, check in enumerate(checks):
            try:
                if not check(hass, variables):
                    return False
            except ConditionError as ex:
                errors.append(
                    ConditionErrorIndex("condition",
                                        index=index,
                                        total=len(checks),
                                        error=ex))

        if errors:
            LOGGER.warning(
                "Error evaluating condition in '%s':\n%s",
                name,
                ConditionErrorContainer("condition", errors=errors),
            )
            return False

        return True