def test_if_action(self): entity_id = 'domain.test_entity' test_state = 'new_state' automation.setup(self.hass, { automation.DOMAIN: { 'trigger': { 'platform': 'event', 'event_type': 'test_event', }, 'condition': [{ 'platform': 'state', 'entity_id': entity_id, 'state': test_state }], 'action': { 'service': 'test.automation' } } }) self.hass.states.set(entity_id, test_state) self.hass.bus.fire('test_event') self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls)) self.hass.states.set(entity_id, test_state + 'something') self.hass.bus.fire('test_event') self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls))
def test_if_action(self): entity_id = 'domain.test_entity' test_state = 'new_state' automation.setup( self.hass, { automation.DOMAIN: { 'trigger': { 'platform': 'event', 'event_type': 'test_event', }, 'condition': [{ 'platform': 'state', 'entity_id': entity_id, 'state': test_state }], 'action': { 'execute_service': 'test.automation' } } }) self.hass.states.set(entity_id, test_state) self.hass.bus.fire('test_event') self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls)) self.hass.states.set(entity_id, test_state + 'something') self.hass.bus.fire('test_event') self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls))
def test_if_action_one_weekday(self): automation.setup( self.hass, { automation.DOMAIN: { 'trigger': { 'platform': 'event', 'event_type': 'test_event' }, 'condition': { 'platform': 'time', 'weekday': 'mon', }, 'action': { 'service': 'test.automation' } } }) days_past_monday = dt_util.now().weekday() monday = dt_util.now() - timedelta(days=days_past_monday) tuesday = monday + timedelta(days=1) with patch('homeassistant.components.automation.time.dt_util.now', return_value=monday): self.hass.bus.fire('test_event') self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls)) with patch('homeassistant.components.automation.time.dt_util.now', return_value=tuesday): self.hass.bus.fire('test_event') self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls))
def test_if_action(self): automation.setup(self.hass, { automation.DOMAIN: { 'trigger': { 'platform': 'event', 'event_type': 'test_event', }, 'condition': [{ 'platform': 'template', 'value_template': '{{ is_state("test.entity", "world") }}' }], 'action': { 'service': 'test.automation' } } }) # Condition is not true yet self.hass.bus.fire('test_event') self.hass.pool.block_till_done() self.assertEqual(0, len(self.calls)) # Change condition to true, but it shouldn't be triggered yet self.hass.states.set('test.entity', 'world') self.hass.pool.block_till_done() self.assertEqual(0, len(self.calls)) # Condition is true and event is triggered self.hass.bus.fire('test_event') self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls))
def test_old_config_if_action_one_weekday(self): automation.setup( self.hass, { automation.DOMAIN: { CONF_PLATFORM: 'event', event.CONF_EVENT_TYPE: 'test_event', 'execute_service': 'test.automation', 'if': { CONF_PLATFORM: 'time', time.CONF_WEEKDAY: 'mon', } } }) days_past_monday = dt_util.now().weekday() monday = dt_util.now() - timedelta(days=days_past_monday) tuesday = monday + timedelta(days=1) with patch('homeassistant.components.automation.time.dt_util.now', return_value=monday): self.hass.bus.fire('test_event') self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls)) with patch('homeassistant.components.automation.time.dt_util.now', return_value=tuesday): self.hass.bus.fire('test_event') self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls))
def test_old_config_if_action(self): entity_id = 'domain.test_entity' test_state = 'new_state' automation.setup(self.hass, { automation.DOMAIN: { 'platform': 'event', 'event_type': 'test_event', 'execute_service': 'test.automation', 'if': [{ 'platform': 'state', 'entity_id': entity_id, 'state': test_state, }] } }) self.hass.states.set(entity_id, test_state) self.hass.bus.fire('test_event') self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls)) self.hass.states.set(entity_id, test_state + 'something') self.hass.bus.fire('test_event') self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls))
def test_if_action(self): """Test for firing if action.""" automation.setup( self.hass, { automation.DOMAIN: { 'trigger': { 'platform': 'event', 'event_type': 'test_event', }, 'condition': [{ 'platform': 'template', 'value_template': '{{ is_state("test.entity", "world") }}' }], 'action': { 'service': 'test.automation' } } }) # Condition is not true yet self.hass.bus.fire('test_event') self.hass.pool.block_till_done() self.assertEqual(0, len(self.calls)) # Change condition to true, but it shouldn't be triggered yet self.hass.states.set('test.entity', 'world') self.hass.pool.block_till_done() self.assertEqual(0, len(self.calls)) # Condition is true and event is triggered self.hass.bus.fire('test_event') self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls))
def test_if_action_after(self): """Test for if action after.""" automation.setup( self.hass, { automation.DOMAIN: { 'trigger': { 'platform': 'event', 'event_type': 'test_event' }, 'condition': { 'platform': 'time', 'after': '10:00', }, 'action': { 'service': 'test.automation' } } }) before_10 = dt_util.now().replace(hour=8) after_10 = dt_util.now().replace(hour=14) with patch('homeassistant.components.automation.time.dt_util.now', return_value=before_10): self.hass.bus.fire('test_event') self.hass.pool.block_till_done() self.assertEqual(0, len(self.calls)) with patch('homeassistant.components.automation.time.dt_util.now', return_value=after_10): self.hass.bus.fire('test_event') self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls))
def test_using_trigger_as_condition(self): """ """ entity_id = 'test.entity' automation.setup(self.hass, { automation.DOMAIN: { 'trigger': [ { 'platform': 'state', 'entity_id': entity_id, 'state': 100 }, { 'platform': 'numeric_state', 'entity_id': entity_id, 'below': 150 } ], 'condition': 'use_trigger_values', 'action': { 'execute_service': 'test.automation', } } }) self.hass.states.set(entity_id, 100) self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls)) self.hass.states.set(entity_id, 120) self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls)) self.hass.states.set(entity_id, 151) self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls))
def test_old_config_if_action_before(self): """Test for action before.""" automation.setup(self.hass, { automation.DOMAIN: { CONF_PLATFORM: 'event', event.CONF_EVENT_TYPE: 'test_event', 'execute_service': 'test.automation', 'if': { CONF_PLATFORM: 'time', time.CONF_BEFORE: '10:00' } } }) before_10 = dt_util.now().replace(hour=8) after_10 = dt_util.now().replace(hour=14) with patch('homeassistant.components.automation.time.dt_util.now', return_value=before_10): self.hass.bus.fire('test_event') self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls)) with patch('homeassistant.components.automation.time.dt_util.now', return_value=after_10): self.hass.bus.fire('test_event') self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls))
def test_if_action_one_weekday(self): """Test for if action with one weekday.""" automation.setup(self.hass, { automation.DOMAIN: { 'trigger': { 'platform': 'event', 'event_type': 'test_event' }, 'condition': { 'platform': 'time', 'weekday': 'mon', }, 'action': { 'service': 'test.automation' } } }) days_past_monday = dt_util.now().weekday() monday = dt_util.now() - timedelta(days=days_past_monday) tuesday = monday + timedelta(days=1) with patch('homeassistant.components.automation.time.dt_util.now', return_value=monday): self.hass.bus.fire('test_event') self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls)) with patch('homeassistant.components.automation.time.dt_util.now', return_value=tuesday): self.hass.bus.fire('test_event') self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls))
def test_if_action_list_weekday(self): automation.setup( self.hass, { automation.DOMAIN: { "trigger": {"platform": "event", "event_type": "test_event"}, "condition": {"platform": "time", "weekday": ["mon", "tue"]}, "action": {"service": "test.automation"}, } }, ) days_past_monday = dt_util.now().weekday() monday = dt_util.now() - timedelta(days=days_past_monday) tuesday = monday + timedelta(days=1) wednesday = tuesday + timedelta(days=1) with patch("homeassistant.components.automation.time.dt_util.now", return_value=monday): self.hass.bus.fire("test_event") self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls)) with patch("homeassistant.components.automation.time.dt_util.now", return_value=tuesday): self.hass.bus.fire("test_event") self.hass.pool.block_till_done() self.assertEqual(2, len(self.calls)) with patch("homeassistant.components.automation.time.dt_util.now", return_value=wednesday): self.hass.bus.fire("test_event") self.hass.pool.block_till_done() self.assertEqual(2, len(self.calls))
def test_old_config_if_action_before(self): automation.setup( self.hass, { automation.DOMAIN: { CONF_PLATFORM: "event", event.CONF_EVENT_TYPE: "test_event", "execute_service": "test.automation", "if": {CONF_PLATFORM: "time", time.CONF_BEFORE: "10:00"}, } }, ) before_10 = dt_util.now().replace(hour=8) after_10 = dt_util.now().replace(hour=14) with patch("homeassistant.components.automation.time.dt_util.now", return_value=before_10): self.hass.bus.fire("test_event") self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls)) with patch("homeassistant.components.automation.time.dt_util.now", return_value=after_10): self.hass.bus.fire("test_event") self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls))
def test_if_action_after(self): automation.setup( self.hass, { automation.DOMAIN: { "trigger": {"platform": "event", "event_type": "test_event"}, "condition": {"platform": "time", "after": "10:00"}, "action": {"service": "test.automation"}, } }, ) before_10 = dt_util.now().replace(hour=8) after_10 = dt_util.now().replace(hour=14) with patch("homeassistant.components.automation.time.dt_util.now", return_value=before_10): self.hass.bus.fire("test_event") self.hass.pool.block_till_done() self.assertEqual(0, len(self.calls)) with patch("homeassistant.components.automation.time.dt_util.now", return_value=after_10): self.hass.bus.fire("test_event") self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls))
def test_old_config_if_action_list_weekday(self): automation.setup( self.hass, { automation.DOMAIN: { CONF_PLATFORM: "event", event.CONF_EVENT_TYPE: "test_event", "execute_service": "test.automation", "if": {CONF_PLATFORM: "time", time.CONF_WEEKDAY: ["mon", "tue"]}, } }, ) days_past_monday = dt_util.now().weekday() monday = dt_util.now() - timedelta(days=days_past_monday) tuesday = monday + timedelta(days=1) wednesday = tuesday + timedelta(days=1) with patch("homeassistant.components.automation.time.dt_util.now", return_value=monday): self.hass.bus.fire("test_event") self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls)) with patch("homeassistant.components.automation.time.dt_util.now", return_value=tuesday): self.hass.bus.fire("test_event") self.hass.pool.block_till_done() self.assertEqual(2, len(self.calls)) with patch("homeassistant.components.automation.time.dt_util.now", return_value=wednesday): self.hass.bus.fire("test_event") self.hass.pool.block_till_done() self.assertEqual(2, len(self.calls))
def test_if_action_after_with_offset(self): self.hass.states.set(sun.ENTITY_ID, sun.STATE_ABOVE_HORIZON, { sun.STATE_ATTR_NEXT_RISING: '14:00:00 16-09-2015', }) automation.setup(self.hass, { automation.DOMAIN: { 'trigger': { 'platform': 'event', 'event_type': 'test_event', }, 'condition': { 'platform': 'sun', 'after': 'sunrise', 'after_offset': '+1:00:00' }, 'action': { 'service': 'test.automation' } } }) now = datetime(2015, 9, 16, 14, 59, tzinfo=dt_util.UTC) with patch('homeassistant.components.automation.sun.dt_util.now', return_value=now): self.hass.bus.fire('test_event') self.hass.pool.block_till_done() self.assertEqual(0, len(self.calls)) now = datetime(2015, 9, 16, 15, tzinfo=dt_util.UTC) with patch('homeassistant.components.automation.sun.dt_util.now', return_value=now): self.hass.bus.fire('test_event') self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls))
def test_using_trigger_as_condition_with_invalid_condition(self): """ Event is not a valid condition. Will it still work? """ entity_id = 'test.entity' self.hass.states.set(entity_id, 100) automation.setup( self.hass, { automation.DOMAIN: { 'trigger': [{ 'platform': 'event', 'event_type': 'test_event', }, { 'platform': 'numeric_state', 'entity_id': entity_id, 'below': 150 }], 'condition': 'use_trigger_values', 'action': { 'service': 'test.automation', } } }) self.hass.bus.fire('test_event') self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls))
def test_if_action_after(self): """Test for if action after.""" automation.setup(self.hass, { automation.DOMAIN: { 'trigger': { 'platform': 'event', 'event_type': 'test_event' }, 'condition': { 'platform': 'time', 'after': '10:00', }, 'action': { 'service': 'test.automation' } } }) before_10 = dt_util.now().replace(hour=8) after_10 = dt_util.now().replace(hour=14) with patch('homeassistant.components.automation.time.dt_util.now', return_value=before_10): self.hass.bus.fire('test_event') self.hass.pool.block_till_done() self.assertEqual(0, len(self.calls)) with patch('homeassistant.components.automation.time.dt_util.now', return_value=after_10): self.hass.bus.fire('test_event') self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls))
def test_using_trigger_as_condition(self): """ """ entity_id = 'test.entity' automation.setup( self.hass, { automation.DOMAIN: { 'trigger': [{ 'platform': 'state', 'entity_id': entity_id, 'state': 100 }, { 'platform': 'numeric_state', 'entity_id': entity_id, 'below': 150 }], 'condition': 'use_trigger_values', 'action': { 'service': 'test.automation', } } }) self.hass.states.set(entity_id, 100) self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls)) self.hass.states.set(entity_id, 120) self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls)) self.hass.states.set(entity_id, 151) self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls))
def test_old_config_if_action(self): """Test for if action.""" entity_id = 'domain.test_entity' test_state = 'new_state' automation.setup(self.hass, { automation.DOMAIN: { 'platform': 'event', 'event_type': 'test_event', 'execute_service': 'test.automation', 'if': [{ 'platform': 'state', 'entity_id': entity_id, 'state': test_state, }] } }) self.hass.states.set(entity_id, test_state) self.hass.bus.fire('test_event') self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls)) self.hass.states.set(entity_id, test_state + 'something') self.hass.bus.fire('test_event') self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls))
def test_two_triggers(self): automation.setup(self.hass, { automation.DOMAIN: { 'trigger': [ { 'platform': 'event', 'event_type': 'test_event', }, { 'platform': 'state', 'entity_id': 'test.entity', } ], 'action': { 'execute_service': 'test.automation', } } }) self.hass.bus.fire('test_event') self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls)) self.hass.states.set('test.entity', 'hello') self.hass.pool.block_till_done() self.assertEqual(2, len(self.calls))
def test_old_config_if_action_one_weekday(self): """Test for action with one weekday.""" automation.setup(self.hass, { automation.DOMAIN: { CONF_PLATFORM: 'event', event.CONF_EVENT_TYPE: 'test_event', 'execute_service': 'test.automation', 'if': { CONF_PLATFORM: 'time', time.CONF_WEEKDAY: 'mon', } } }) days_past_monday = dt_util.now().weekday() monday = dt_util.now() - timedelta(days=days_past_monday) tuesday = monday + timedelta(days=1) with patch('homeassistant.components.automation.time.dt_util.now', return_value=monday): self.hass.bus.fire('test_event') self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls)) with patch('homeassistant.components.automation.time.dt_util.now', return_value=tuesday): self.hass.bus.fire('test_event') self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls))
def test_using_trigger_as_condition_with_invalid_condition(self): """ Event is not a valid condition. Will it still work? """ entity_id = 'test.entity' self.hass.states.set(entity_id, 100) automation.setup(self.hass, { automation.DOMAIN: { 'trigger': [ { 'platform': 'event', 'event_type': 'test_event', }, { 'platform': 'numeric_state', 'entity_id': entity_id, 'below': 150 } ], 'condition': 'use_trigger_values', 'action': { 'execute_service': 'test.automation', } } }) self.hass.bus.fire('test_event') self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls))
def test_old_config_if_action_after(self): automation.setup( self.hass, { automation.DOMAIN: { CONF_PLATFORM: 'event', event.CONF_EVENT_TYPE: 'test_event', 'execute_service': 'test.automation', 'if': { CONF_PLATFORM: 'time', time.CONF_AFTER: '10:00' } } }) before_10 = dt_util.now().replace(hour=8) after_10 = dt_util.now().replace(hour=14) with patch('homeassistant.components.automation.time.dt_util.now', return_value=before_10): self.hass.bus.fire('test_event') self.hass.pool.block_till_done() self.assertEqual(0, len(self.calls)) with patch('homeassistant.components.automation.time.dt_util.now', return_value=after_10): self.hass.bus.fire('test_event') self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls))
def test_service_data_not_a_dict(self): automation.setup(self.hass, { automation.DOMAIN: { 'platform': 'event', 'event_type': 'test_event', 'execute_service': 'test.automation', 'service_data': 100 } }) self.hass.bus.fire('test_event') self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls))
def test_service_data_not_a_dict(self): automation.setup(self.hass, { automation.DOMAIN: { CONF_PLATFORM: 'event', event.CONF_EVENT_TYPE: 'test_event', automation.CONF_SERVICE: 'test.automation', automation.CONF_SERVICE_DATA: 100 } }) self.hass.bus.fire('test_event') self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls))
def test_service_specify_data(self): automation.setup(self.hass, { automation.DOMAIN: { CONF_PLATFORM: 'event', event.CONF_EVENT_TYPE: 'test_event', automation.CONF_SERVICE: 'test.automation', automation.CONF_SERVICE_DATA: {'some': 'data'} } }) self.hass.bus.fire('test_event') self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls)) self.assertEqual('data', self.calls[0].data['some'])
def test_service_specify_entity_id(self): automation.setup(self.hass, { automation.DOMAIN: { CONF_PLATFORM: 'event', event.CONF_EVENT_TYPE: 'test_event', automation.CONF_SERVICE: 'test.automation', automation.CONF_SERVICE_ENTITY_ID: 'hello.world' } }) self.hass.bus.fire('test_event') self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls)) self.assertEqual(['hello.world'], self.calls[0].data[ATTR_ENTITY_ID])
def test_old_config_service_specify_data(self): automation.setup(self.hass, { automation.DOMAIN: { 'platform': 'event', 'event_type': 'test_event', 'execute_service': 'test.automation', 'service_data': {'some': 'data'} } }) self.hass.bus.fire('test_event') self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls)) self.assertEqual('data', self.calls[0].data['some'])
def test_old_config_service_data_not_a_dict(self): automation.setup( self.hass, { automation.DOMAIN: { 'platform': 'event', 'event_type': 'test_event', 'execute_service': 'test.automation', 'service_data': 100 } }) self.hass.bus.fire('test_event') self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls))
def test_old_config_service_specify_entity_id_list(self): automation.setup(self.hass, { automation.DOMAIN: { 'platform': 'event', 'event_type': 'test_event', 'execute_service': 'test.automation', 'service_entity_id': ['hello.world', 'hello.world2'] } }) self.hass.bus.fire('test_event') self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls)) self.assertEqual(['hello.world', 'hello.world2'], self.calls[0].data.get(ATTR_ENTITY_ID))
def test_if_fires_on_zone_leave(self): """Test for firing on zone leave.""" self.hass.states.set('test.entity', 'hello', { 'latitude': 32.880586, 'longitude': -117.237564 }) self.hass.pool.block_till_done() self.assertTrue(automation.setup(self.hass, { automation.DOMAIN: { 'trigger': { 'platform': 'zone', 'entity_id': 'test.entity', 'zone': 'zone.test', 'event': 'leave', }, 'action': { 'service': 'test.automation', } } })) self.hass.states.set('test.entity', 'hello', { 'latitude': 32.881011, 'longitude': -117.234758 }) self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls))
def test_zone_condition(self): self.hass.states.set('test.entity', 'hello', { 'latitude': 32.880586, 'longitude': -117.237564 }) self.hass.pool.block_till_done() self.assertTrue( automation.setup( self.hass, { automation.DOMAIN: { 'trigger': { 'platform': 'event', 'event_type': 'test_event' }, 'condition': { 'platform': 'zone', 'entity_id': 'test.entity', 'zone': 'zone.test', }, 'action': { 'service': 'test.automation', } } })) self.hass.bus.fire('test_event') self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls))
def test_not_fires_on_attr_change_with_attr_not_below_multiple_attr(self): """"Test if not fired changed attributes.""" self.assertTrue( automation.setup( self.hass, { automation.DOMAIN: { 'trigger': { 'platform': 'numeric_state', 'entity_id': 'test.entity', 'value_template': '{{ state.attributes.test_attribute }}', 'below': 10, }, 'action': { 'service': 'test.automation' } } })) # 11 is not below 10 self.hass.states.set('test.entity', 'entity', { 'test_attribute': 11, 'not_test_attribute': 9 }) self.hass.pool.block_till_done() self.assertEqual(0, len(self.calls))
def test_if_fires_on_entity_change_over_to_below_above_range(self): """"Test the firing with changed entity.""" self.hass.states.set('test.entity', 11) self.hass.pool.block_till_done() self.assertTrue( automation.setup( self.hass, { automation.DOMAIN: { 'trigger': { 'platform': 'numeric_state', 'entity_id': 'test.entity', 'below': 10, 'above': 5, }, 'action': { 'service': 'test.automation' } } })) # 4 is below 5 so it should not fire self.hass.states.set('test.entity', 4) self.hass.pool.block_till_done() self.assertEqual(0, len(self.calls))
def test_if_fires_on_two_change(self): """Test for firing on two changes.""" self.assertTrue( automation.setup( self.hass, { automation.DOMAIN: { 'trigger': { 'platform': 'template', 'value_template': '{{ true }}', }, 'action': { 'service': 'test.automation' } } })) # Trigger once self.hass.states.set('test.entity', 'world') self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls)) # Trigger again self.hass.states.set('test.entity', 'world') self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls))
def test_setup_fails_if_no_topic(self): self.assertFalse(automation.setup(self.hass, { automation.DOMAIN: { CONF_PLATFORM: 'mqtt', automation.CONF_SERVICE: 'test.automation' } }))
def test_automation_list_setting(self): """ Event is not a valid condition. Will it still work? """ self.assertTrue(automation.setup(self.hass, { automation.DOMAIN: [{ 'trigger': { 'platform': 'event', 'event_type': 'test_event', }, 'action': { 'service': 'test.automation', } }, { 'trigger': { 'platform': 'event', 'event_type': 'test_event_2', }, 'action': { 'service': 'test.automation', } }] })) self.hass.bus.fire('test_event') self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls)) self.hass.bus.fire('test_event_2') self.hass.pool.block_till_done() self.assertEqual(2, len(self.calls))
def test_if_not_fires_for_enter_on_zone_leave(self): self.hass.states.set('test.entity', 'hello', { 'latitude': 32.880586, 'longitude': -117.237564 }) self.hass.pool.block_till_done() self.assertTrue( automation.setup( self.hass, { automation.DOMAIN: { 'trigger': { 'platform': 'zone', 'entity_id': 'test.entity', 'zone': 'zone.test', 'event': 'enter', }, 'action': { 'service': 'test.automation', } } })) self.hass.states.set('test.entity', 'hello', { 'latitude': 32.881011, 'longitude': -117.234758 }) self.hass.pool.block_till_done() self.assertEqual(0, len(self.calls))
def test_automation_list_setting(self): """ Event is not a valid condition. Will it still work? """ self.assertTrue( automation.setup( self.hass, { automation.DOMAIN: [{ 'trigger': { 'platform': 'event', 'event_type': 'test_event', }, 'action': { 'service': 'test.automation', } }, { 'trigger': { 'platform': 'event', 'event_type': 'test_event_2', }, 'action': { 'service': 'test.automation', } }] })) self.hass.bus.fire('test_event') self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls)) self.hass.bus.fire('test_event_2') self.hass.pool.block_till_done() self.assertEqual(2, len(self.calls))
def test_sunrise_trigger(self): """Test the sunrise trigger.""" self.hass.states.set(sun.ENTITY_ID, sun.STATE_ABOVE_HORIZON, { sun.STATE_ATTR_NEXT_RISING: '14:00:00 16-09-2015', }) now = datetime(2015, 9, 13, 23, tzinfo=dt_util.UTC) trigger_time = datetime(2015, 9, 16, 14, tzinfo=dt_util.UTC) with patch('homeassistant.components.automation.sun.dt_util.utcnow', return_value=now): self.assertTrue(automation.setup(self.hass, { automation.DOMAIN: { 'trigger': { 'platform': 'sun', 'event': 'sunrise', }, 'action': { 'service': 'test.automation', } } })) fire_time_changed(self.hass, trigger_time) self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls))
def test_if_not_fires_using_wrong_after(self, mock_error): """YAML translates time values to total seconds. This should break the before rule. """ self.assertTrue( automation.setup( self.hass, { automation.DOMAIN: { 'trigger': { 'platform': 'time', 'after': 3605, # Total seconds. Hour = 3600 second }, 'action': { 'service': 'test.automation' } } })) fire_time_changed(self.hass, dt_util.utcnow().replace(hour=1, minute=0, second=5)) self.hass.pool.block_till_done() self.assertEqual(0, len(self.calls)) self.assertEqual(2, mock_error.call_count)
def test_if_fires_on_no_change_with_template_advanced(self): self.assertTrue(automation.setup(self.hass, { automation.DOMAIN: { 'trigger': { 'platform': 'template', 'value_template': '''{%- if is_state("test.entity", "world") -%} true {%- else -%} false {%- endif -%}''', }, 'action': { 'service': 'test.automation' } } })) # Different state self.hass.states.set('test.entity', 'worldz') self.hass.pool.block_till_done() self.assertEqual(0, len(self.calls)) # Different state self.hass.states.set('test.entity', 'hello') self.hass.pool.block_till_done() self.assertEqual(0, len(self.calls))
def test_zone_condition(self): """Test for zone condition.""" self.hass.states.set('test.entity', 'hello', { 'latitude': 32.880586, 'longitude': -117.237564 }) self.hass.pool.block_till_done() self.assertTrue(automation.setup(self.hass, { automation.DOMAIN: { 'trigger': { 'platform': 'event', 'event_type': 'test_event' }, 'condition': { 'platform': 'zone', 'entity_id': 'test.entity', 'zone': 'zone.test', }, 'action': { 'service': 'test.automation', } } })) self.hass.bus.fire('test_event') self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls))
def test_old_config_service_specify_entity_id_list(self): automation.setup( self.hass, { automation.DOMAIN: { 'platform': 'event', 'event_type': 'test_event', 'execute_service': 'test.automation', 'service_entity_id': ['hello.world', 'hello.world2'] } }) self.hass.bus.fire('test_event') self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls)) self.assertEqual(['hello.world', 'hello.world2'], self.calls[0].data.get(ATTR_ENTITY_ID))
def test_setup_fails_if_no_entity_id(self): self.assertFalse(automation.setup(self.hass, { automation.DOMAIN: { CONF_PLATFORM: 'state', automation.CONF_SERVICE: 'test.automation' } }))
def test_if_not_fires_on_entity_change_with_for(self): """Test for not firing on entity change with for.""" self.assertTrue(automation.setup(self.hass, { automation.DOMAIN: { 'trigger': { 'platform': 'state', 'entity_id': 'test.entity', 'to': 'world', 'for': { 'seconds': 5 }, }, 'action': { 'service': 'test.automation' } } })) self.hass.states.set('test.entity', 'world') self.hass.pool.block_till_done() self.hass.states.set('test.entity', 'not_world') self.hass.pool.block_till_done() fire_time_changed(self.hass, dt_util.utcnow() + timedelta(seconds=10)) self.hass.pool.block_till_done() self.assertEqual(0, len(self.calls))
def test_sunset_trigger_with_offset(self): self.hass.states.set(sun.ENTITY_ID, sun.STATE_ABOVE_HORIZON, { sun.STATE_ATTR_NEXT_SETTING: '02:00:00 16-09-2015', }) now = datetime(2015, 9, 15, 23, tzinfo=dt_util.UTC) trigger_time = datetime(2015, 9, 16, 2, 30, tzinfo=dt_util.UTC) with patch('homeassistant.components.automation.sun.dt_util.utcnow', return_value=now): self.assertTrue(automation.setup(self.hass, { automation.DOMAIN: { 'trigger': { 'platform': 'sun', 'event': 'sunset', 'offset': '0:30:00' }, 'action': { 'execute_service': 'test.automation', } } })) fire_time_changed(self.hass, trigger_time) self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls))
def test_if_not_fires_on_entity_change_above_to_above(self): """"Test the firing with changed entity.""" # set initial state self.hass.states.set('test.entity', 11) self.hass.pool.block_till_done() self.assertTrue( automation.setup( self.hass, { automation.DOMAIN: { 'trigger': { 'platform': 'numeric_state', 'entity_id': 'test.entity', 'above': 10, }, 'action': { 'service': 'test.automation' } } })) # 11 is above 10 so this should fire again self.hass.states.set('test.entity', 12) self.hass.pool.block_till_done() self.assertEqual(0, len(self.calls))
def test_if_not_fires_on_entity_change_with_for(self): self.assertTrue( automation.setup( self.hass, { automation.DOMAIN: { 'trigger': { 'platform': 'state', 'entity_id': 'test.entity', 'to': 'world', 'for': { 'seconds': 5 }, }, 'action': { 'service': 'test.automation' } } })) self.hass.states.set('test.entity', 'world') self.hass.pool.block_till_done() self.hass.states.set('test.entity', 'not_world') self.hass.pool.block_till_done() fire_time_changed(self.hass, dt_util.utcnow() + timedelta(seconds=10)) self.hass.pool.block_till_done() self.assertEqual(0, len(self.calls))
def test_if_fires_on_no_change_with_template_advanced(self): """Test for firing on no change with template advanced.""" self.assertTrue( automation.setup( self.hass, { automation.DOMAIN: { 'trigger': { 'platform': 'template', 'value_template': '''{%- if is_state("test.entity", "world") -%} true {%- else -%} false {%- endif -%}''', }, 'action': { 'service': 'test.automation' } } })) # Different state self.hass.states.set('test.entity', 'worldz') self.hass.pool.block_till_done() self.assertEqual(0, len(self.calls)) # Different state self.hass.states.set('test.entity', 'hello') self.hass.pool.block_till_done() self.assertEqual(0, len(self.calls))
def test_if_fires_on_event_with_data(self): """Test the firing of events with data.""" self.assertTrue( automation.setup( self.hass, { automation.DOMAIN: { 'trigger': { 'platform': 'event', 'event_type': 'test_event', 'event_data': { 'some_attr': 'some_value' } }, 'action': { 'service': 'test.automation', } } })) self.hass.bus.fire('test_event', { 'some_attr': 'some_value', 'another': 'value' }) self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls))
def test_old_config_service_specify_data(self): automation.setup( self.hass, { automation.DOMAIN: { 'platform': 'event', 'event_type': 'test_event', 'execute_service': 'test.automation', 'service_data': { 'some': 'data' } } }) self.hass.bus.fire('test_event') self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls)) self.assertEqual('data', self.calls[0].data['some'])
def test_setup_fails_if_no_entity_id(self): self.assertFalse( automation.setup( self.hass, { automation.DOMAIN: { CONF_PLATFORM: 'state', automation.CONF_SERVICE: 'test.automation' } }))