def test_or_condition(self): """Test the 'or' condition.""" test = condition.from_config({ 'condition': 'or', 'conditions': [ { 'condition': 'state', 'entity_id': 'sensor.temperature', 'state': '100', }, { 'condition': 'numeric_state', 'entity_id': 'sensor.temperature', 'below': 110, } ] }) self.hass.states.set('sensor.temperature', 120) assert not test(self.hass) self.hass.states.set('sensor.temperature', 105) assert test(self.hass) self.hass.states.set('sensor.temperature', 100) assert test(self.hass)
def test_or_condition_with_template(self): """Test the 'or' condition.""" test = condition.from_config({ 'condition': 'or', 'conditions': [ { 'condition': 'template', 'value_template': '{{ states.sensor.temperature.state == "100" }}', }, { 'condition': 'numeric_state', 'entity_id': 'sensor.temperature', 'below': 110, } ] }) self.hass.states.set('sensor.temperature', 120) assert not test(self.hass) self.hass.states.set('sensor.temperature', 105) assert test(self.hass) self.hass.states.set('sensor.temperature', 100) assert test(self.hass)
def test_or_condition(self): """Test the 'or' condition.""" test = condition.from_config({ "condition": "or", "conditions": [ { "condition": "state", "entity_id": "sensor.temperature", "state": "100", }, { "condition": "numeric_state", "entity_id": "sensor.temperature", "below": 110, }, ], }) self.hass.states.set("sensor.temperature", 120) assert not test(self.hass) self.hass.states.set("sensor.temperature", 105) assert test(self.hass) self.hass.states.set("sensor.temperature", 100) assert test(self.hass)
def test_and_condition_with_template(self): """Test the 'and' condition.""" test = condition.from_config({ "condition": "and", "conditions": [ { "condition": "template", "value_template": '{{ states.sensor.temperature.state == "100" }}', }, { "condition": "numeric_state", "entity_id": "sensor.temperature", "below": 110, }, ], }) self.hass.states.set("sensor.temperature", 120) assert not test(self.hass) self.hass.states.set("sensor.temperature", 105) assert not test(self.hass) self.hass.states.set("sensor.temperature", 100) assert test(self.hass)
def _process_if(hass, config, p_config, action): """Process if checks.""" cond_type = p_config.get(CONF_CONDITION_TYPE, DEFAULT_CONDITION_TYPE).lower() # Deprecated since 0.19 - 5/5/2016 if cond_type != DEFAULT_CONDITION_TYPE: _LOGGER.warning('Using condition_type: "or" is deprecated. Please use ' '"condition: or" instead.') if_configs = p_config.get(CONF_CONDITION) use_trigger = if_configs == CONDITION_USE_TRIGGER_VALUES if use_trigger: if_configs = p_config[CONF_TRIGGER] checks = [] for if_config in if_configs: # Deprecated except for used by use_trigger_values # since 0.19 - 5/5/2016 if CONF_PLATFORM in if_config: if not use_trigger: _LOGGER.warning("Please switch your condition configuration " "to use 'condition' instead of 'platform'.") if_config = dict(if_config) if_config[CONF_CONDITION] = if_config.pop(CONF_PLATFORM) # To support use_trigger_values with state trigger accepting # multiple entity_ids to monitor. if_entity_id = if_config.get(ATTR_ENTITY_ID) if isinstance(if_entity_id, list) and len(if_entity_id) == 1: if_config[ATTR_ENTITY_ID] = if_entity_id[0] try: checks.append(condition.from_config(if_config)) except HomeAssistantError as ex: # Invalid conditions are allowed if we base it on trigger if use_trigger: _LOGGER.warning('Ignoring invalid condition: %s', ex) else: _LOGGER.warning('Invalid condition: %s', ex) return None if cond_type == CONDITION_TYPE_AND: def if_action(variables=None): """AND all conditions.""" if all(check(hass, variables) for check in checks): action(variables) else: def if_action(variables=None): """OR all conditions.""" if any(check(hass, variables) for check in checks): action(variables) return if_action
def test_if_numeric_state_not_raise_on_unavailable(self): """Test numeric_state doesn't raise on unavailable/unknown state.""" test = condition.from_config({ 'condition': 'numeric_state', 'entity_id': 'sensor.temperature', 'below': 42 }) with patch('homeassistant.helpers.condition._LOGGER.warning') \ as logwarn: self.hass.states.set('sensor.temperature', 'unavailable') assert not test(self.hass) assert len(logwarn.mock_calls) == 0 self.hass.states.set('sensor.temperature', 'unknown') assert not test(self.hass) assert len(logwarn.mock_calls) == 0
def test_if_numeric_state_not_raise_on_unavailable(self): """Test numeric_state doesn't raise on unavailable/unknown state.""" test = condition.from_config({ "condition": "numeric_state", "entity_id": "sensor.temperature", "below": 42, }) with patch( "homeassistant.helpers.condition._LOGGER.warning") as logwarn: self.hass.states.set("sensor.temperature", "unavailable") assert not test(self.hass) assert len(logwarn.mock_calls) == 0 self.hass.states.set("sensor.temperature", "unknown") assert not test(self.hass) assert len(logwarn.mock_calls) == 0
def _check_condition(self, action, variables): """Test if condition is matching.""" self.last_action = action.get(CONF_ALIAS, action[CONF_CONDITION]) check = condition.from_config(action, False)(self.hass, variables) self._log("Test condition {}: {}".format(self.last_action, check)) return check
def _check_condition(self, action, variables): """Test if condition is matching.""" self.last_action = action.get(CONF_ALIAS, action[CONF_CONDITION]) check = condition.from_config(action)(self.hass, variables) self._log("Test condition {}: {}".format(self.last_action, check)) return check