def _reproduce_state(self, states): """ Wraps reproduce state with Scence specific logic. """ self.ignore_updates = True reproduce_state(self.hass, states, True) self.ignore_updates = False self.update_ha_state(True)
def test_reproduce_with_no_entity(self): """Test reproduce_state with no entity.""" calls = mock_service(self.hass, 'light', SERVICE_TURN_ON) state.reproduce_state(self.hass, ha.State('light.test', 'on')) self.hass.block_till_done() assert len(calls) == 0 assert self.hass.states.get('light.test') is None
def test_reproduce_with_no_entity(self): """Test reproduce_state with no entity.""" calls = mock_service(self.hass, 'light', SERVICE_TURN_ON) state.reproduce_state(self.hass, ha.State('light.test', 'on')) self.hass.block_till_done() self.assertTrue(len(calls) == 0) self.assertEqual(None, self.hass.states.get('light.test'))
def test_reproduce_bad_state(self): """Test reproduce_state with bad state.""" calls = mock_service(self.hass, 'light', SERVICE_TURN_ON) self.hass.states.set('light.test', 'off') state.reproduce_state(self.hass, ha.State('light.test', 'bad')) self.hass.block_till_done() assert len(calls) == 0 assert 'off' == self.hass.states.get('light.test').state
def test_reproduce_bad_state(self): """Test reproduce_state with bad state.""" calls = mock_service(self.hass, 'light', SERVICE_TURN_ON) self.hass.states.set('light.test', 'off') state.reproduce_state(self.hass, ha.State('light.test', 'bad')) self.hass.block_till_done() self.assertTrue(len(calls) == 0) self.assertEqual('off', self.hass.states.get('light.test').state)
def test_reproduce_bad_state(self): """Test reproduce_state with bad state.""" calls = mock_service(self.hass, 'light', SERVICE_TURN_ON) self.hass.states.set('light.test', 'off') state.reproduce_state(self.hass, ha.State('light.test', 'bad')) self.hass.pool.block_till_done() self.assertTrue(len(calls) == 0) self.assertEqual('off', self.hass.states.get('light.test').state)
def test_reproduce_state_with_turn_on(self): calls = mock_service(self.hass, 'light', SERVICE_TURN_ON) self.hass.states.set('light.test', 'off') state.reproduce_state(self.hass, ha.State('light.test', 'on')) self.hass.pool.block_till_done() self.assertTrue(len(calls) > 0) last_call = calls[-1] self.assertEqual('light', last_call.domain) self.assertEqual(SERVICE_TURN_ON, last_call.service) self.assertEqual(['light.test'], last_call.data.get('entity_id'))
def test_reproduce_turn_off(self): """Test reproduce_state with SERVICE_TURN_OFF.""" calls = mock_service(self.hass, 'light', SERVICE_TURN_OFF) self.hass.states.set('light.test', 'on') state.reproduce_state(self.hass, ha.State('light.test', 'off')) self.hass.block_till_done() assert len(calls) > 0 last_call = calls[-1] assert 'light' == last_call.domain assert SERVICE_TURN_OFF == last_call.service assert ['light.test'] == last_call.data.get('entity_id')
def test_reproduce_turn_on(self): """Test reproduce_state with SERVICE_TURN_ON.""" calls = mock_service(self.hass, 'light', SERVICE_TURN_ON) self.hass.states.set('light.test', 'off') state.reproduce_state(self.hass, ha.State('light.test', 'on')) self.hass.block_till_done() assert len(calls) > 0 last_call = calls[-1] assert 'light' == last_call.domain assert SERVICE_TURN_ON == last_call.service assert ['light.test'] == last_call.data.get('entity_id')
def test_reproduce_state_with_group(self): light_calls = mock_service(self.hass, 'light', SERVICE_TURN_ON) self.hass.states.set('group.test', 'off', {'entity_id': ['light.test1', 'light.test2']}) state.reproduce_state(self.hass, ha.State('group.test', 'on')) self.hass.pool.block_till_done() self.assertEqual(1, len(light_calls)) last_call = light_calls[-1] self.assertEqual('light', last_call.domain) self.assertEqual(SERVICE_TURN_ON, last_call.service) self.assertEqual(['light.test1', 'light.test2'], last_call.data.get('entity_id'))
def test_reproduce_state_with_group(self): light_calls = mock_service(self.hass, 'light', SERVICE_TURN_ON) self.hass.states.set('group.test', 'off', { 'entity_id': ['light.test1', 'light.test2']}) state.reproduce_state(self.hass, ha.State('group.test', 'on')) self.hass.pool.block_till_done() self.assertEqual(1, len(light_calls)) last_call = light_calls[-1] self.assertEqual('light', last_call.domain) self.assertEqual(SERVICE_TURN_ON, last_call.service) self.assertEqual(['light.test1', 'light.test2'], last_call.data.get('entity_id'))
def test_reproduce_media_pause(self): """Test reproduce_state with SERVICE_MEDIA_PAUSE.""" calls = mock_service(self.hass, 'media_player', SERVICE_MEDIA_PAUSE) self.hass.states.set('media_player.test', 'playing') state.reproduce_state( self.hass, ha.State('media_player.test', 'paused')) self.hass.block_till_done() assert len(calls) > 0 last_call = calls[-1] assert 'media_player' == last_call.domain assert SERVICE_MEDIA_PAUSE == last_call.service assert ['media_player.test'] == \ last_call.data.get('entity_id')
def test_reproduce_group(self): """Test reproduce_state with group.""" light_calls = mock_service(self.hass, 'light', SERVICE_TURN_ON) self.hass.states.set('group.test', 'off', { 'entity_id': ['light.test1', 'light.test2']}) state.reproduce_state(self.hass, ha.State('group.test', 'on')) self.hass.block_till_done() assert 1 == len(light_calls) last_call = light_calls[-1] assert 'light' == last_call.domain assert SERVICE_TURN_ON == last_call.service assert ['light.test1', 'light.test2'] == \ last_call.data.get('entity_id')
def test_reproduce_media_pause(self): """Test reproduce_state with SERVICE_MEDIA_PAUSE.""" calls = mock_service(self.hass, 'media_player', SERVICE_MEDIA_PAUSE) self.hass.states.set('media_player.test', 'playing') state.reproduce_state( self.hass, ha.State('media_player.test', 'paused')) self.hass.block_till_done() self.assertTrue(len(calls) > 0) last_call = calls[-1] self.assertEqual('media_player', last_call.domain) self.assertEqual(SERVICE_MEDIA_PAUSE, last_call.service) self.assertEqual(['media_player.test'], last_call.data.get('entity_id'))
def test_reproduce_media_pause(self): """Test reproduce_state with SERVICE_MEDIA_PAUSE.""" calls = mock_service(self.hass, 'media_player', SERVICE_MEDIA_PAUSE) self.hass.states.set('media_player.test', 'playing') state.reproduce_state(self.hass, ha.State('media_player.test', 'paused')) self.hass.block_till_done() assert len(calls) > 0 last_call = calls[-1] assert 'media_player' == last_call.domain assert SERVICE_MEDIA_PAUSE == last_call.service assert ['media_player.test'] == \ last_call.data.get('entity_id')
def test_reproduce_media_pause(self): """Test reproduce_state with SERVICE_MEDIA_PAUSE.""" calls = mock_service(self.hass, 'media_player', SERVICE_MEDIA_PAUSE) self.hass.states.set('media_player.test', 'playing') state.reproduce_state(self.hass, ha.State('media_player.test', 'paused')) self.hass.block_till_done() self.assertTrue(len(calls) > 0) last_call = calls[-1] self.assertEqual('media_player', last_call.domain) self.assertEqual(SERVICE_MEDIA_PAUSE, last_call.service) self.assertEqual(['media_player.test'], last_call.data.get('entity_id'))
def test_reproduce_group(self): """Test reproduce_state with group.""" light_calls = mock_service(self.hass, 'light', SERVICE_TURN_ON) self.hass.states.set('group.test', 'off', {'entity_id': ['light.test1', 'light.test2']}) state.reproduce_state(self.hass, ha.State('group.test', 'on')) self.hass.block_till_done() assert 1 == len(light_calls) last_call = light_calls[-1] assert 'light' == last_call.domain assert SERVICE_TURN_ON == last_call.service assert ['light.test1', 'light.test2'] == \ last_call.data.get('entity_id')
def test_reproduce_state_with_complex_service_data(self): calls = mock_service(self.hass, 'light', SERVICE_TURN_ON) self.hass.states.set('light.test', 'off') complex_data = ['hello', {'11': '22'}] state.reproduce_state( self.hass, ha.State('light.test', 'on', {'complex': complex_data})) self.hass.pool.block_till_done() self.assertTrue(len(calls) > 0) last_call = calls[-1] self.assertEqual('light', last_call.domain) self.assertEqual(SERVICE_TURN_ON, last_call.service) self.assertEqual(complex_data, last_call.data.get('complex'))
def test_reproduce_state_with_complex_service_data(self): calls = mock_service(self.hass, 'light', SERVICE_TURN_ON) self.hass.states.set('light.test', 'off') complex_data = ['hello', {'11': '22'}] state.reproduce_state(self.hass, ha.State('light.test', 'on', { 'complex': complex_data })) self.hass.pool.block_till_done() self.assertTrue(len(calls) > 0) last_call = calls[-1] self.assertEqual('light', last_call.domain) self.assertEqual(SERVICE_TURN_ON, last_call.service) self.assertEqual(complex_data, last_call.data.get('complex'))
def test_reproduce_complex_data(self): """Test reproduce_state with complex service data.""" calls = mock_service(self.hass, 'light', SERVICE_TURN_ON) self.hass.states.set('light.test', 'off') complex_data = ['hello', {'11': '22'}] state.reproduce_state( self.hass, ha.State('light.test', 'on', {'complex': complex_data})) self.hass.block_till_done() assert len(calls) > 0 last_call = calls[-1] assert 'light' == last_call.domain assert SERVICE_TURN_ON == last_call.service assert complex_data == last_call.data.get('complex')
def test_reproduce_complex_data(self): """Test reproduce_state with complex service data.""" calls = mock_service(self.hass, 'light', SERVICE_TURN_ON) self.hass.states.set('light.test', 'off') complex_data = ['hello', {'11': '22'}] state.reproduce_state(self.hass, ha.State('light.test', 'on', { 'complex': complex_data })) self.hass.block_till_done() assert len(calls) > 0 last_call = calls[-1] assert 'light' == last_call.domain assert SERVICE_TURN_ON == last_call.service assert complex_data == last_call.data.get('complex')
def test_reproduce_state_group_states_with_same_domain_and_data(self): light_calls = mock_service(self.hass, 'light', SERVICE_TURN_ON) self.hass.states.set('light.test1', 'off') self.hass.states.set('light.test2', 'off') state.reproduce_state(self.hass, [ ha.State('light.test1', 'on', {'brightness': 95}), ha.State('light.test2', 'on', {'brightness': 95})]) self.hass.pool.block_till_done() self.assertEqual(1, len(light_calls)) last_call = light_calls[-1] self.assertEqual('light', last_call.domain) self.assertEqual(SERVICE_TURN_ON, last_call.service) self.assertEqual(['light.test1', 'light.test2'], last_call.data.get('entity_id')) self.assertEqual(95, last_call.data.get('brightness'))
def test_reproduce_media_data(self): """Test reproduce_state with SERVICE_PLAY_MEDIA.""" calls = mock_service(self.hass, 'media_player', SERVICE_PLAY_MEDIA) self.hass.states.set('media_player.test', 'off') media_attributes = {'media_content_type': 'movie', 'media_content_id': 'batman'} state.reproduce_state(self.hass, ha.State('media_player.test', 'None', media_attributes)) self.hass.pool.block_till_done() self.assertTrue(len(calls) > 0) last_call = calls[-1] self.assertEqual('media_player', last_call.domain) self.assertEqual(SERVICE_PLAY_MEDIA, last_call.service) self.assertEqual('movie', last_call.data.get('media_content_type')) self.assertEqual('batman', last_call.data.get('media_content_id'))
def test_reproduce_state_group_states_with_same_domain_and_data(self): light_calls = mock_service(self.hass, 'light', SERVICE_TURN_ON) self.hass.states.set('light.test1', 'off') self.hass.states.set('light.test2', 'off') state.reproduce_state(self.hass, [ ha.State('light.test1', 'on', {'brightness': 95}), ha.State('light.test2', 'on', {'brightness': 95}) ]) self.hass.pool.block_till_done() self.assertEqual(1, len(light_calls)) last_call = light_calls[-1] self.assertEqual('light', last_call.domain) self.assertEqual(SERVICE_TURN_ON, last_call.service) self.assertEqual(['light.test1', 'light.test2'], last_call.data.get('entity_id')) self.assertEqual(95, last_call.data.get('brightness'))
def test_reproduce_media_data(self): """Test reproduce_state with SERVICE_PLAY_MEDIA.""" calls = mock_service(self.hass, 'media_player', SERVICE_PLAY_MEDIA) self.hass.states.set('media_player.test', 'off') media_attributes = {'media_content_type': 'movie', 'media_content_id': 'batman'} state.reproduce_state(self.hass, ha.State('media_player.test', 'None', media_attributes)) self.hass.block_till_done() assert len(calls) > 0 last_call = calls[-1] assert 'media_player' == last_call.domain assert SERVICE_PLAY_MEDIA == last_call.service assert 'movie' == last_call.data.get('media_content_type') assert 'batman' == last_call.data.get('media_content_id')
def test_reproduce_group_same_data(self): """Test reproduce_state with group with same domain and data.""" light_calls = mock_service(self.hass, 'light', SERVICE_TURN_ON) self.hass.states.set('light.test1', 'off') self.hass.states.set('light.test2', 'off') state.reproduce_state(self.hass, [ ha.State('light.test1', 'on', {'brightness': 95}), ha.State('light.test2', 'on', {'brightness': 95})]) self.hass.block_till_done() assert 1 == len(light_calls) last_call = light_calls[-1] assert 'light' == last_call.domain assert SERVICE_TURN_ON == last_call.service assert ['light.test1', 'light.test2'] == \ last_call.data.get('entity_id') assert 95 == last_call.data.get('brightness')
def test_reproduce_group_same_data(self): """Test reproduce_state with group with same domain and data.""" light_calls = mock_service(self.hass, 'light', SERVICE_TURN_ON) self.hass.states.set('light.test1', 'off') self.hass.states.set('light.test2', 'off') state.reproduce_state(self.hass, [ ha.State('light.test1', 'on', {'brightness': 95}), ha.State('light.test2', 'on', {'brightness': 95}) ]) self.hass.block_till_done() assert 1 == len(light_calls) last_call = light_calls[-1] assert 'light' == last_call.domain assert SERVICE_TURN_ON == last_call.service assert ['light.test1', 'light.test2'] == \ last_call.data.get('entity_id') assert 95 == last_call.data.get('brightness')
def test_reproduce_media_data(self): """Test reproduce_state with SERVICE_PLAY_MEDIA.""" calls = mock_service(self.hass, 'media_player', SERVICE_PLAY_MEDIA) self.hass.states.set('media_player.test', 'off') media_attributes = { 'media_content_type': 'movie', 'media_content_id': 'batman' } state.reproduce_state( self.hass, ha.State('media_player.test', 'None', media_attributes)) self.hass.block_till_done() assert len(calls) > 0 last_call = calls[-1] assert 'media_player' == last_call.domain assert SERVICE_PLAY_MEDIA == last_call.service assert 'movie' == last_call.data.get('media_content_type') assert 'batman' == last_call.data.get('media_content_id')
def activate(self): """ Activates scene. Tries to get entities into requested state. """ reproduce_state(self.hass, self.scene_config.states.values(), True)