Пример #1
0
    def state_automation_listener(entity, from_s, to_s):
        """Listen for state changes and calls action."""
        if to_s is None:
            return

        variables = {
            'trigger': {
                'platform': 'numeric_state',
                'entity_id': entity,
                'below': below,
                'above': above,
            }
        }

        # If new one doesn't match, nothing to do
        if not condition.async_numeric_state(
                hass, to_s, below, above, value_template, variables):
            return

        # Only match if old didn't exist or existed but didn't match
        # Written as: skip if old one did exist and matched
        if from_s is not None and condition.async_numeric_state(
                hass, from_s, below, above, value_template, variables):
            return

        variables['trigger']['from_state'] = from_s
        variables['trigger']['to_state'] = to_s

        hass.async_run_job(action, variables)
Пример #2
0
    def state_automation_listener(entity, from_s, to_s):
        """Listen for state changes and calls action."""
        if to_s is None:
            return

        variables = {
            'trigger': {
                'platform': 'numeric_state',
                'entity_id': entity,
                'below': below,
                'above': above,
            }
        }

        # If new one doesn't match, nothing to do
        if not condition.async_numeric_state(hass, to_s, below, above,
                                             value_template, variables):
            return

        # Only match if old didn't exist or existed but didn't match
        # Written as: skip if old one did exist and matched
        if from_s is not None and condition.async_numeric_state(
                hass, from_s, below, above, value_template, variables):
            return

        variables['trigger']['from_state'] = from_s
        variables['trigger']['to_state'] = to_s

        hass.async_run_job(action, variables)
Пример #3
0
async def test_numeric_state_using_input_number(hass):
    """Test numeric_state conditions using input_number entities."""
    await async_setup_component(
        hass,
        "input_number",
        {
            "input_number": {
                "low": {"min": 0, "max": 255, "initial": 10},
                "high": {"min": 0, "max": 255, "initial": 100},
            }
        },
    )

    test = await condition.async_from_config(
        hass,
        {
            "condition": "and",
            "conditions": [
                {
                    "condition": "numeric_state",
                    "entity_id": "sensor.temperature",
                    "below": "input_number.high",
                    "above": "input_number.low",
                },
            ],
        },
    )

    hass.states.async_set("sensor.temperature", 42)
    assert test(hass)

    hass.states.async_set("sensor.temperature", 10)
    assert not test(hass)

    hass.states.async_set("sensor.temperature", 100)
    assert not test(hass)

    await hass.services.async_call(
        "input_number",
        "set_value",
        {
            "entity_id": "input_number.high",
            "value": 101,
        },
        blocking=True,
    )
    assert test(hass)

    with pytest.raises(ConditionError):
        condition.async_numeric_state(
            hass, entity="sensor.temperature", below="input_number.not_exist"
        )
    with pytest.raises(ConditionError):
        condition.async_numeric_state(
            hass, entity="sensor.temperature", above="input_number.not_exist"
        )
Пример #4
0
    def template_if(hass: HomeAssistant, variables: TemplateVarsType = None) -> bool:
        """Validate template based if-condition."""
        value_template.hass = hass

        return condition.async_numeric_state(
            hass, config[ATTR_ENTITY_ID], max_pos, min_pos, value_template
        )
Пример #5
0
    def check_numeric_state(entity_id, from_s, to_s):
        """Return True if criteria are now met."""
        if to_s is None:
            return False

        return condition.async_numeric_state(hass, to_s, below, above,
                                             value_template,
                                             variables(entity_id), attribute)
 def check_numeric_state(hass: HomeAssistant,
                         variables: TemplateVarsType = None) -> bool:
     """Return whether the criteria are met."""
     return condition.async_numeric_state(hass,
                                          config[ATTR_ENTITY_ID],
                                          max_pos,
                                          min_pos,
                                          attribute=position_attr)
Пример #7
0
    def _process_numeric_state(self, entity_observation):
        """Add entity to current_obs if numeric state conditions are met."""
        entity = entity_observation['entity_id']

        should_trigger = condition.async_numeric_state(
            self.hass, entity, entity_observation.get('below'),
            entity_observation.get('above'), None, entity_observation)

        self._update_current_obs(entity_observation, should_trigger)
    def _process_numeric_state(self, entity_observation):
        """Add entity to current_obs if numeric state conditions are met."""
        entity = entity_observation['entity_id']

        should_trigger = condition.async_numeric_state(
            self.hass, entity,
            entity_observation.get('below'),
            entity_observation.get('above'), None, entity_observation)

        self._update_current_obs(entity_observation, should_trigger)
Пример #9
0
    def _process_numeric_state(self, entity_observation):
        """Return True if numeric condition is met."""
        entity = entity_observation["entity_id"]

        return condition.async_numeric_state(
            self.hass,
            entity,
            entity_observation.get("below"),
            entity_observation.get("above"),
            None,
            entity_observation,
        )
Пример #10
0
    def check_numeric_state(entity, from_s, to_s):
        """Return True if criteria are now met."""
        if to_s is None:
            return False

        variables = {
            "trigger": {
                "platform": "numeric_state",
                "entity_id": entity,
                "below": below,
                "above": above,
            }
        }
        return condition.async_numeric_state(hass, to_s, below, above,
                                             value_template, variables)
Пример #11
0
    def check_numeric_state(entity, from_s, to_s):
        """Return True if criteria are now met."""
        if to_s is None:
            return False

        variables = {
            'trigger': {
                'platform': 'numeric_state',
                'entity_id': entity,
                'below': below,
                'above': above,
            }
        }
        return condition.async_numeric_state(
            hass, to_s, below, above, value_template, variables)
    def check_numeric_state(entity, from_s, to_s):
        """Return True if they should trigger."""
        if to_s is None:
            return False

        variables = {
            'trigger': {
                'platform': 'numeric_state',
                'entity_id': entity,
                'below': below,
                'above': above,
            }
        }

        # If new one doesn't match, nothing to do
        if not condition.async_numeric_state(
                hass, to_s, below, above, value_template, variables):
            return False

        return True
    def state_automation_listener(entity, from_s, to_s):
        """Listen for state changes and calls action."""
        nonlocal async_remove_track_same

        if not check_numeric_state(entity, from_s, to_s):
            return

        variables = {
            'trigger': {
                'platform': 'numeric_state',
                'entity_id': entity,
                'below': below,
                'above': above,
                'from_state': from_s,
                'to_state': to_s,
            }
        }

        # Only match if old didn't exist or existed but didn't match
        # Written as: skip if old one did exist and matched
        if from_s is not None and condition.async_numeric_state(
                hass, from_s, below, above, value_template, variables):
            return

        @callback
        def call_action():
            """Call action with right context."""
            hass.async_run_job(action, variables)

        if not time_delta:
            call_action()
            return

        async_remove_track_same = async_track_same_state(
            hass, True, time_delta, call_action, entity_ids=entity_id,
            async_check_func=check_numeric_state)
Пример #14
0
 def check_numeric_state(entity_id, from_s, to_s):
     """Return whether the criteria are met, raise ConditionError if unknown."""
     return condition.async_numeric_state(
         hass, to_s, below, above, value_template, variables(entity_id), attribute
     )