def test_sunset_trigger(self): """Test the sunset trigger.""" now = datetime(2015, 9, 15, 23, tzinfo=dt_util.UTC) trigger_time = datetime(2015, 9, 16, 2, tzinfo=dt_util.UTC) with patch('homeassistant.util.dt.utcnow', return_value=now): setup_component(self.hass, automation.DOMAIN, { automation.DOMAIN: { 'trigger': { 'platform': 'sun', 'event': 'sunset', }, 'action': { 'service': 'test.automation', } } }) automation.turn_off(self.hass) self.hass.block_till_done() fire_time_changed(self.hass, trigger_time) self.hass.block_till_done() self.assertEqual(0, len(self.calls)) with patch('homeassistant.util.dt.utcnow', return_value=now): automation.turn_on(self.hass) self.hass.block_till_done() fire_time_changed(self.hass, trigger_time) self.hass.block_till_done() self.assertEqual(1, len(self.calls))
def test_services(self): """Test the automation services for turning entities on/off.""" entity_id = 'automation.hello' assert self.hass.states.get(entity_id) is None assert not automation.is_on(self.hass, entity_id) assert _setup_component( self.hass, automation.DOMAIN, { automation.DOMAIN: { 'alias': 'hello', 'trigger': { 'platform': 'event', 'event_type': 'test_event', }, 'action': { 'service': 'test.automation', } } }) assert self.hass.states.get(entity_id) is not None assert automation.is_on(self.hass, entity_id) self.hass.bus.fire('test_event') self.hass.pool.block_till_done() assert len(self.calls) == 1 automation.turn_off(self.hass, entity_id) self.hass.pool.block_till_done() assert not automation.is_on(self.hass, entity_id) self.hass.bus.fire('test_event') self.hass.pool.block_till_done() assert len(self.calls) == 1 automation.toggle(self.hass, entity_id) self.hass.pool.block_till_done() assert automation.is_on(self.hass, entity_id) self.hass.bus.fire('test_event') self.hass.pool.block_till_done() assert len(self.calls) == 2 automation.trigger(self.hass, entity_id) self.hass.pool.block_till_done() assert len(self.calls) == 3 automation.turn_off(self.hass, entity_id) self.hass.pool.block_till_done() automation.trigger(self.hass, entity_id) self.hass.pool.block_till_done() assert len(self.calls) == 4 automation.turn_on(self.hass, entity_id) self.hass.pool.block_till_done() assert automation.is_on(self.hass, entity_id)
def test_services(self): """Test the automation services for turning entities on/off.""" entity_id = 'automation.hello' assert self.hass.states.get(entity_id) is None assert not automation.is_on(self.hass, entity_id) assert setup_component(self.hass, automation.DOMAIN, { automation.DOMAIN: { 'alias': 'hello', 'trigger': { 'platform': 'event', 'event_type': 'test_event', }, 'action': { 'service': 'test.automation', } } }) assert self.hass.states.get(entity_id) is not None assert automation.is_on(self.hass, entity_id) self.hass.bus.fire('test_event') self.hass.block_till_done() assert len(self.calls) == 1 automation.turn_off(self.hass, entity_id) self.hass.block_till_done() assert not automation.is_on(self.hass, entity_id) self.hass.bus.fire('test_event') self.hass.block_till_done() assert len(self.calls) == 1 automation.toggle(self.hass, entity_id) self.hass.block_till_done() assert automation.is_on(self.hass, entity_id) self.hass.bus.fire('test_event') self.hass.block_till_done() assert len(self.calls) == 2 automation.trigger(self.hass, entity_id) self.hass.block_till_done() assert len(self.calls) == 3 automation.turn_off(self.hass, entity_id) self.hass.block_till_done() automation.trigger(self.hass, entity_id) self.hass.block_till_done() assert len(self.calls) == 4 automation.turn_on(self.hass, entity_id) self.hass.block_till_done() assert automation.is_on(self.hass, entity_id)
def test_sunset_trigger(self): """Test the sunset trigger.""" self.hass.states.set(sun.ENTITY_ID, sun.STATE_ABOVE_HORIZON, { sun.STATE_ATTR_NEXT_SETTING: '2015-09-16T02:00:00Z', }) now = datetime(2015, 9, 15, 23, tzinfo=dt_util.UTC) trigger_time = datetime(2015, 9, 16, 2, tzinfo=dt_util.UTC) with patch('homeassistant.util.dt.utcnow', return_value=now): setup_component(self.hass, automation.DOMAIN, { automation.DOMAIN: { 'trigger': { 'platform': 'sun', 'event': 'sunset', }, 'action': { 'service': 'test.automation', } } }) automation.turn_off(self.hass) self.hass.block_till_done() fire_time_changed(self.hass, trigger_time) self.hass.block_till_done() self.assertEqual(0, len(self.calls)) with patch('homeassistant.util.dt.utcnow', return_value=now): automation.turn_on(self.hass) self.hass.block_till_done() fire_time_changed(self.hass, trigger_time) self.hass.block_till_done() self.assertEqual(1, len(self.calls))