def test_methods(self): """ Test if methods call the services as expected. """ # Test is_on self.hass.states.set('light.test', STATE_ON) self.assertTrue(light.is_on(self.hass, 'light.test')) self.hass.states.set('light.test', STATE_OFF) self.assertFalse(light.is_on(self.hass, 'light.test')) self.hass.states.set(light.ENTITY_ID_ALL_LIGHTS, STATE_ON) self.assertTrue(light.is_on(self.hass)) self.hass.states.set(light.ENTITY_ID_ALL_LIGHTS, STATE_OFF) self.assertFalse(light.is_on(self.hass)) # Test turn_on turn_on_calls = mock_service( self.hass, light.DOMAIN, SERVICE_TURN_ON) light.turn_on( self.hass, entity_id='entity_id_val', transition='transition_val', brightness='brightness_val', rgb_color='rgb_color_val', xy_color='xy_color_val', profile='profile_val') self.hass.pool.block_till_done() self.assertEqual(1, len(turn_on_calls)) call = turn_on_calls[-1] self.assertEqual(light.DOMAIN, call.domain) self.assertEqual(SERVICE_TURN_ON, call.service) self.assertEqual('entity_id_val', call.data.get(ATTR_ENTITY_ID)) self.assertEqual( 'transition_val', call.data.get(light.ATTR_TRANSITION)) self.assertEqual( 'brightness_val', call.data.get(light.ATTR_BRIGHTNESS)) self.assertEqual('rgb_color_val', call.data.get(light.ATTR_RGB_COLOR)) self.assertEqual('xy_color_val', call.data.get(light.ATTR_XY_COLOR)) self.assertEqual('profile_val', call.data.get(light.ATTR_PROFILE)) # Test turn_off turn_off_calls = mock_service( self.hass, light.DOMAIN, SERVICE_TURN_OFF) light.turn_off( self.hass, entity_id='entity_id_val', transition='transition_val') self.hass.pool.block_till_done() self.assertEqual(1, len(turn_off_calls)) call = turn_off_calls[-1] self.assertEqual(light.DOMAIN, call.domain) self.assertEqual(SERVICE_TURN_OFF, call.service) self.assertEqual('entity_id_val', call.data[ATTR_ENTITY_ID]) self.assertEqual('transition_val', call.data[light.ATTR_TRANSITION])
def test_services(self): """Test the provided services.""" # Test turn_on turn_on_calls = mock_service( self.hass, remote.DOMAIN, SERVICE_TURN_ON) remote.turn_on( self.hass, entity_id='entity_id_val') self.hass.block_till_done() self.assertEqual(1, len(turn_on_calls)) call = turn_on_calls[-1] self.assertEqual(remote.DOMAIN, call.domain) # Test turn_off turn_off_calls = mock_service( self.hass, remote.DOMAIN, SERVICE_TURN_OFF) remote.turn_off( self.hass, entity_id='entity_id_val') self.hass.block_till_done() self.assertEqual(1, len(turn_off_calls)) call = turn_off_calls[-1] self.assertEqual(remote.DOMAIN, call.domain) self.assertEqual(SERVICE_TURN_OFF, call.service) self.assertEqual('entity_id_val', call.data[ATTR_ENTITY_ID]) # Test send_command send_command_calls = mock_service( self.hass, remote.DOMAIN, SERVICE_SEND_COMMAND) remote.send_command( self.hass, entity_id='entity_id_val', device='test_device', command='test_command') self.hass.block_till_done() self.assertEqual(1, len(send_command_calls)) call = send_command_calls[-1] self.assertEqual(remote.DOMAIN, call.domain) self.assertEqual(SERVICE_SEND_COMMAND, call.service) self.assertEqual('entity_id_val', call.data[ATTR_ENTITY_ID])
def test_flux_before_sunrise(self): """Test the flux switch before sunrise.""" platform = loader.get_component("light.test") platform.init() self.assertTrue(light.setup(self.hass, {light.DOMAIN: {CONF_PLATFORM: "test"}})) dev1 = platform.DEVICES[0] # Verify initial state of light state = self.hass.states.get(dev1.entity_id) self.assertEqual(STATE_ON, state.state) self.assertIsNone(state.attributes.get("xy_color")) self.assertIsNone(state.attributes.get("brightness")) test_time = dt_util.now().replace(hour=2, minute=30, second=0) sunset_time = test_time.replace(hour=17, minute=0, second=0) sunrise_time = test_time.replace(hour=5, minute=0, second=0) + timedelta(days=1) with patch("homeassistant.util.dt.now", return_value=test_time): with patch("homeassistant.components.sun.next_rising", return_value=sunrise_time): with patch("homeassistant.components.sun.next_setting", return_value=sunset_time): assert setup_component( self.hass, switch.DOMAIN, {switch.DOMAIN: {"platform": "flux", "name": "flux", "lights": [dev1.entity_id]}}, ) turn_on_calls = mock_service(self.hass, light.DOMAIN, SERVICE_TURN_ON) switch.turn_on(self.hass, "switch.flux") self.hass.pool.block_till_done() fire_time_changed(self.hass, test_time) self.hass.pool.block_till_done() call = turn_on_calls[-1] self.assertEqual(call.data[light.ATTR_BRIGHTNESS], 119) self.assertEqual(call.data[light.ATTR_XY_COLOR], [0.591, 0.395])
def test_flux_when_switch_is_off(self): """Test the flux switch when it is off.""" platform = loader.get_component("light.test") platform.init() self.assertTrue(light.setup(self.hass, {light.DOMAIN: {CONF_PLATFORM: "test"}})) dev1 = platform.DEVICES[0] # Verify initial state of light state = self.hass.states.get(dev1.entity_id) self.assertEqual(STATE_ON, state.state) self.assertIsNone(state.attributes.get("xy_color")) self.assertIsNone(state.attributes.get("brightness")) test_time = dt_util.now().replace(hour=10, minute=30, second=0) sunset_time = test_time.replace(hour=17, minute=0, second=0) sunrise_time = test_time.replace(hour=5, minute=0, second=0) + timedelta(days=1) with patch("homeassistant.util.dt.now", return_value=test_time): with patch("homeassistant.components.sun.next_rising", return_value=sunrise_time): with patch("homeassistant.components.sun.next_setting", return_value=sunset_time): assert setup_component( self.hass, switch.DOMAIN, {switch.DOMAIN: {"platform": "flux", "name": "flux", "lights": [dev1.entity_id]}}, ) turn_on_calls = mock_service(self.hass, light.DOMAIN, SERVICE_TURN_ON) fire_time_changed(self.hass, test_time) self.hass.pool.block_till_done() self.assertEqual(0, len(turn_on_calls))
def test_service_say_error_msg(self, aioclient_mock): """Test service call say with http error api message.""" calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) aioclient_mock.post( self.url, data=self.form_data, status=200, content=b'The subscription does not support SSML!' ) config = { tts.DOMAIN: { 'platform': 'voicerss', 'api_key': '1234567xx', } } with assert_setup_component(1, tts.DOMAIN): setup_component(self.hass, tts.DOMAIN, config) self.hass.services.call(tts.DOMAIN, 'voicerss_say', { tts.ATTR_MESSAGE: "I person is on front of your door.", }) self.hass.block_till_done() assert len(calls) == 0 assert len(aioclient_mock.mock_calls) == 1 assert aioclient_mock.mock_calls[0][2] == self.form_data
def test_service_say_russian_config(self, aioclient_mock): """Test service call say.""" calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) url = "https://tts.voicetech.yandex.net/generate?format=mp3" \ "&speaker=zahar&key=1234567xx&text=HomeAssistant&lang=ru-RU" aioclient_mock.get( url, status=200, content=b'test') config = { tts.DOMAIN: { 'platform': 'yandextts', 'api_key': '1234567xx', 'language': 'ru-RU', } } with assert_setup_component(1, tts.DOMAIN): setup_component(self.hass, tts.DOMAIN, config) self.hass.services.call(tts.DOMAIN, 'yandextts_say', { tts.ATTR_MESSAGE: "HomeAssistant", }) self.hass.block_till_done() assert len(aioclient_mock.mock_calls) == 1 assert len(calls) == 1
def test_setup_component_and_test_service_with_receive_voice_german(self): """Set up the demo platform and call service and receive voice.""" calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) config = { tts.DOMAIN: { 'platform': 'demo', 'language': 'de', } } with assert_setup_component(1, tts.DOMAIN): setup_component(self.hass, tts.DOMAIN, config) self.hass.start() self.hass.services.call(tts.DOMAIN, 'demo_say', { tts.ATTR_MESSAGE: "I person is on front of your door.", }) self.hass.block_till_done() assert len(calls) == 1 req = requests.get(calls[0].data[ATTR_MEDIA_CONTENT_ID]) _, demo_data = self.demo_provider.get_tts_audio("bla", "de") demo_data = tts.SpeechManager.write_tags( "265944c108cbb00b2a621be5930513e03a0bb2cd_de_-_demo.mp3", demo_data, self.demo_provider, "I person is on front of your door.", 'de', None) assert req.status_code == 200 assert req.content == demo_data
def test_setup_component_test_with_cache_dir(self): """Set up demo platform with cache and call service without cache.""" calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) _, demo_data = self.demo_provider.get_tts_audio("bla", 'en') cache_file = os.path.join( self.default_tts_cache, "265944c108cbb00b2a621be5930513e03a0bb2cd_en_-_demo.mp3") os.mkdir(self.default_tts_cache) with open(cache_file, "wb") as voice_file: voice_file.write(demo_data) config = { tts.DOMAIN: { 'platform': 'demo', 'cache': True, } } with assert_setup_component(1, tts.DOMAIN): setup_component(self.hass, tts.DOMAIN, config) with patch('homeassistant.components.tts.demo.DemoProvider.' 'get_tts_audio', return_value=(None, None)): self.hass.services.call(tts.DOMAIN, 'demo_say', { tts.ATTR_MESSAGE: "I person is on front of your door.", }) self.hass.block_till_done() assert len(calls) == 1 assert calls[0].data[ATTR_MEDIA_CONTENT_ID] == \ "{}/api/tts_proxy/265944c108cbb00b2a621be5930513e03a0bb2cd" \ "_en_-_demo.mp3".format(self.hass.config.api.base_url)
def test_automation_not_trigger_on_bootstrap(hass): """Test if automation is not trigger on bootstrap.""" hass.state = CoreState.not_running calls = mock_service(hass, 'test', 'automation') res = yield from async_setup_component(hass, automation.DOMAIN, { automation.DOMAIN: { 'alias': 'hello', 'trigger': { 'platform': 'event', 'event_type': 'test_event', }, 'action': { 'service': 'test.automation', 'entity_id': 'hello.world' } } }) assert res assert not automation.is_on(hass, 'automation.hello') hass.bus.async_fire('test_event') yield from hass.async_block_till_done() assert len(calls) == 0 hass.bus.async_fire(EVENT_HOMEASSISTANT_START) yield from hass.async_block_till_done() assert automation.is_on(hass, 'automation.hello') hass.bus.async_fire('test_event') yield from hass.async_block_till_done() assert len(calls) == 1 assert ['hello.world'] == calls[0].data.get(ATTR_ENTITY_ID)
def test_setup_component_and_test_service(self): """Set up the demo platform and call service.""" calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) config = { tts.DOMAIN: { 'platform': 'demo', } } with assert_setup_component(1, tts.DOMAIN): setup_component(self.hass, tts.DOMAIN, config) self.hass.services.call(tts.DOMAIN, 'demo_say', { tts.ATTR_MESSAGE: "I person is on front of your door.", }) self.hass.block_till_done() assert len(calls) == 1 assert calls[0].data[ATTR_MEDIA_CONTENT_TYPE] == MEDIA_TYPE_MUSIC assert calls[0].data[ATTR_MEDIA_CONTENT_ID] == \ "{}/api/tts_proxy/265944c108cbb00b2a621be5930513e03a0bb2cd" \ "_en_-_demo.mp3".format(self.hass.config.api.base_url) assert os.path.isfile(os.path.join( self.default_tts_cache, "265944c108cbb00b2a621be5930513e03a0bb2cd_en_-_demo.mp3"))
def test_service_say_german_config(self, mock_calculate, aioclient_mock): """Test service call say with german code in the config.""" calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) self.url_param['tl'] = 'de' aioclient_mock.get( self.url, params=self.url_param, status=200, content=b'test') config = { tts.DOMAIN: { 'platform': 'google', 'language': 'de', } } with assert_setup_component(1, tts.DOMAIN): setup_component(self.hass, tts.DOMAIN, config) self.hass.services.call(tts.DOMAIN, 'google_say', { tts.ATTR_MESSAGE: "90% of I person is on front of your door.", }) self.hass.block_till_done() assert len(calls) == 1 assert len(aioclient_mock.mock_calls) == 1
def test_setup_component_and_test_service_with_receive_voice_german(self): """Setup the demo platform and call service and receive voice.""" calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) config = { tts.DOMAIN: { 'platform': 'demo', } } with assert_setup_component(1, tts.DOMAIN): setup_component(self.hass, tts.DOMAIN, config) self.hass.start() self.hass.services.call(tts.DOMAIN, 'demo_say', { tts.ATTR_MESSAGE: "I person is on front of your door.", }) self.hass.block_till_done() assert len(calls) == 1 req = requests.get(calls[0].data[ATTR_MEDIA_CONTENT_ID]) _, demo_data = self.demo_provider.get_tts_audio("bla", "de") assert req.status_code == 200 assert req.content == demo_data
def test_setup_component_and_test_service_clear_cache(self): """Set up the demo platform and call service clear cache.""" calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) config = { tts.DOMAIN: { 'platform': 'demo', } } with assert_setup_component(1, tts.DOMAIN): setup_component(self.hass, tts.DOMAIN, config) self.hass.services.call(tts.DOMAIN, 'demo_say', { tts.ATTR_MESSAGE: "I person is on front of your door.", }) self.hass.block_till_done() assert len(calls) == 1 assert os.path.isfile(os.path.join( self.default_tts_cache, "265944c108cbb00b2a621be5930513e03a0bb2cd_en_-_demo.mp3")) self.hass.services.call(tts.DOMAIN, tts.SERVICE_CLEAR_CACHE, {}) self.hass.block_till_done() assert not os.path.isfile(os.path.join( self.default_tts_cache, "265944c108cbb00b2a621be5930513e03a0bb2cd_en_-_demo.mp3"))
def test_service_say(self, aioclient_mock): """Test service call say.""" calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) aioclient_mock.post( self.url, data=self.form_data, status=200, content=b'test') config = { tts.DOMAIN: { 'platform': 'voicerss', 'api_key': '1234567xx', } } with assert_setup_component(1, tts.DOMAIN): setup_component(self.hass, tts.DOMAIN, config) self.hass.services.call(tts.DOMAIN, 'voicerss_say', { tts.ATTR_MESSAGE: "I person is on front of your door.", }) self.hass.block_till_done() assert len(calls) == 1 assert len(aioclient_mock.mock_calls) == 1 assert aioclient_mock.mock_calls[0][2] == self.form_data assert calls[0].data[ATTR_MEDIA_CONTENT_ID].find(".mp3") != -1
def test_service_say_russian_config(self, aioclient_mock): """Test service call say.""" calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) url_param = { 'text': 'HomeAssistant', 'lang': 'ru-RU', 'key': '1234567xx', 'speaker': 'zahar', 'format': 'mp3', 'emotion': 'neutral', 'speed': 1 } aioclient_mock.get( self._base_url, status=200, content=b'test', params=url_param) config = { tts.DOMAIN: { 'platform': 'yandextts', 'api_key': '1234567xx', 'language': 'ru-RU', } } with assert_setup_component(1, tts.DOMAIN): setup_component(self.hass, tts.DOMAIN, config) self.hass.services.call(tts.DOMAIN, 'yandextts_say', { tts.ATTR_MESSAGE: "HomeAssistant", }) self.hass.block_till_done() assert len(aioclient_mock.mock_calls) == 1 assert len(calls) == 1
def test_no_initial_value_and_restore_off(hass): """Test initial value off and restored state is turned on.""" calls = mock_service(hass, 'test', 'automation') mock_restore_cache(hass, ( State('automation.hello', STATE_OFF), )) res = yield from async_setup_component(hass, automation.DOMAIN, { automation.DOMAIN: { 'alias': 'hello', 'trigger': { 'platform': 'event', 'event_type': 'test_event', }, 'action': { 'service': 'test.automation', 'entity_id': 'hello.world' } } }) assert res assert not automation.is_on(hass, 'automation.hello') hass.bus.async_fire('test_event') yield from hass.async_block_till_done() assert len(calls) == 0
def test_setup_component_and_test_service_with_service_options_wrong(self): """Set up the demo platform and call service with wrong options.""" calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) config = { tts.DOMAIN: { 'platform': 'demo', } } with assert_setup_component(1, tts.DOMAIN): setup_component(self.hass, tts.DOMAIN, config) self.hass.services.call(tts.DOMAIN, 'demo_say', { tts.ATTR_MESSAGE: "I person is on front of your door.", tts.ATTR_LANGUAGE: "de", tts.ATTR_OPTIONS: { 'speed': 1 } }) self.hass.block_till_done() opt_hash = ctypes.c_size_t(hash(frozenset({'speed': 1}))).value assert len(calls) == 0 assert not os.path.isfile(os.path.join( self.default_tts_cache, "265944c108cbb00b2a621be5930513e03a0bb2cd_de_{0}_demo.mp3".format( opt_hash)))
def test_setup_component_and_test_with_service_options_def(self, def_mock): """Set up the demo platform and call service with default options.""" calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) config = { tts.DOMAIN: { 'platform': 'demo', } } with assert_setup_component(1, tts.DOMAIN): setup_component(self.hass, tts.DOMAIN, config) self.hass.services.call(tts.DOMAIN, 'demo_say', { tts.ATTR_MESSAGE: "I person is on front of your door.", tts.ATTR_LANGUAGE: "de", }) self.hass.block_till_done() opt_hash = ctypes.c_size_t(hash(frozenset({'voice': 'alex'}))).value assert len(calls) == 1 assert calls[0].data[ATTR_MEDIA_CONTENT_TYPE] == MEDIA_TYPE_MUSIC assert calls[0].data[ATTR_MEDIA_CONTENT_ID] == \ "{}/api/tts_proxy/265944c108cbb00b2a621be5930513e03a0bb2cd" \ "_de_{}_demo.mp3".format(self.hass.config.api.base_url, opt_hash) assert os.path.isfile(os.path.join( self.default_tts_cache, "265944c108cbb00b2a621be5930513e03a0bb2cd_de_{0}_demo.mp3".format( opt_hash)))
def test_setup_component_and_test_service_with_base_url_set(self): """Set up the demo platform with ``base_url`` set and call service.""" calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) config = { tts.DOMAIN: { 'platform': 'demo', 'base_url': 'http://fnord', } } with assert_setup_component(1, tts.DOMAIN): setup_component(self.hass, tts.DOMAIN, config) self.hass.services.call(tts.DOMAIN, 'demo_say', { tts.ATTR_MESSAGE: "I person is on front of your door.", }) self.hass.block_till_done() assert len(calls) == 1 assert calls[0].data[ATTR_MEDIA_CONTENT_TYPE] == MEDIA_TYPE_MUSIC assert calls[0].data[ATTR_MEDIA_CONTENT_ID] == \ "http://fnord" \ "/api/tts_proxy/265944c108cbb00b2a621be5930513e03a0bb2cd" \ "_en_-_demo.mp3"
def test_if_fires_on_hass_shutdown(hass): """Test the firing when HASS starts.""" calls = mock_service(hass, 'test', 'automation') hass.state = CoreState.not_running res = yield from async_setup_component(hass, automation.DOMAIN, { automation.DOMAIN: { 'alias': 'hello', 'trigger': { 'platform': 'homeassistant', 'event': 'shutdown', }, 'action': { 'service': 'test.automation', } } }) assert res assert not automation.is_on(hass, 'automation.hello') assert len(calls) == 0 yield from hass.async_start() assert automation.is_on(hass, 'automation.hello') assert len(calls) == 0 with patch.object(hass.loop, 'stop'): yield from hass.async_stop() assert len(calls) == 1
def test_if_fires_on_hass_start(hass): """Test the firing when HASS starts.""" calls = mock_service(hass, 'test', 'automation') hass.state = CoreState.not_running config = { automation.DOMAIN: { 'alias': 'hello', 'trigger': { 'platform': 'homeassistant', 'event': 'start', }, 'action': { 'service': 'test.automation', } } } res = yield from async_setup_component(hass, automation.DOMAIN, config) assert res assert not automation.is_on(hass, 'automation.hello') assert len(calls) == 0 yield from hass.async_start() assert automation.is_on(hass, 'automation.hello') assert len(calls) == 1 with patch('homeassistant.config.async_hass_config_yaml', Mock(return_value=mock_coro(config))): yield from hass.services.async_call( automation.DOMAIN, automation.SERVICE_RELOAD, blocking=True) assert automation.is_on(hass, 'automation.hello') assert len(calls) == 1
def test_service_say_timeout(self, aioclient_mock): """Test service call say with http timeout.""" calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) aioclient_mock.post( self.url, data=self.form_data, exc=asyncio.TimeoutError()) config = { tts.DOMAIN: { 'platform': 'voicerss', 'api_key': '1234567xx', } } with assert_setup_component(1, tts.DOMAIN): setup_component(self.hass, tts.DOMAIN, config) self.hass.services.call(tts.DOMAIN, 'voicerss_say', { tts.ATTR_MESSAGE: "I person is on front of your door.", }) self.hass.block_till_done() assert len(calls) == 0 assert len(aioclient_mock.mock_calls) == 1 assert aioclient_mock.mock_calls[0][2] == self.form_data
def test_service_say_german_config(self, aioclient_mock): """Test service call say with german code in the config.""" calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) self.form_data['hl'] = 'de-de' aioclient_mock.post( self.url, data=self.form_data, status=200, content=b'test') config = { tts.DOMAIN: { 'platform': 'voicerss', 'api_key': '1234567xx', 'language': 'de-de', } } with assert_setup_component(1, tts.DOMAIN): setup_component(self.hass, tts.DOMAIN, config) self.hass.services.call(tts.DOMAIN, 'voicerss_say', { tts.ATTR_MESSAGE: "I person is on front of your door.", }) self.hass.block_till_done() assert len(calls) == 1 assert len(aioclient_mock.mock_calls) == 1 assert aioclient_mock.mock_calls[0][2] == self.form_data
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_turn_on_to_not_block_for_domains_without_service(self, mock_call): """Test if turn_on is blocking domain with no service.""" mock_service(self.hass, 'light', SERVICE_TURN_ON) # We can't test if our service call results in services being called # because by mocking out the call service method, we mock out all # So we mimick how the service registry calls services service_call = ha.ServiceCall('homeassistant', 'turn_on', { 'entity_id': ['light.test', 'sensor.bla', 'light.bla'] }) self.hass.services._services['homeassistant']['turn_on'](service_call) self.assertEqual(2, mock_call.call_count) self.assertEqual( ('light', 'turn_on', {'entity_id': ['light.bla', 'light.test']}, True), mock_call.call_args_list[0][0]) self.assertEqual( ('sensor', 'turn_on', {'entity_id': ['sensor.bla']}, False), mock_call.call_args_list[1][0])
def __init__(self, hass, name): self.hass = hass self._name = name self.entity_id = media_player.ENTITY_ID_FORMAT.format(name) self._state = STATE_OFF self._volume_level = 0 self._is_volume_muted = False self._media_title = None self._supported_media_commands = 0 self.turn_off_service_calls = mock_service(hass, media_player.DOMAIN, media_player.SERVICE_TURN_OFF)
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.pool.block_till_done() self.assertTrue(len(calls) == 0) self.assertEqual('off', self.hass.states.get('light.test').state)
def test_flux_after_sunrise_before_sunset_stop_next_day(self): """ Test the flux switch after sunrise and before sunset. This test has the stop_time on the next day (after midnight). """ platform = loader.get_component('light.test') platform.init() self.assertTrue( setup_component(self.hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: 'test'}})) dev1 = platform.DEVICES[0] # Verify initial state of light state = self.hass.states.get(dev1.entity_id) self.assertEqual(STATE_ON, state.state) self.assertIsNone(state.attributes.get('xy_color')) self.assertIsNone(state.attributes.get('brightness')) test_time = dt_util.now().replace(hour=8, minute=30, second=0) sunset_time = test_time.replace(hour=17, minute=0, second=0) sunrise_time = test_time.replace(hour=5, minute=0, second=0) def event_date(hass, event, now=None): if event == 'sunrise': return sunrise_time else: return sunset_time with patch('homeassistant.util.dt.now', return_value=test_time): with patch('homeassistant.helpers.sun.get_astral_event_date', side_effect=event_date): assert setup_component(self.hass, switch.DOMAIN, { switch.DOMAIN: { 'platform': 'flux', 'name': 'flux', 'lights': [dev1.entity_id], 'stop_time': '01:00' } }) turn_on_calls = mock_service( self.hass, light.DOMAIN, SERVICE_TURN_ON) switch.turn_on(self.hass, 'switch.flux') self.hass.block_till_done() fire_time_changed(self.hass, test_time) self.hass.block_till_done() call = turn_on_calls[-1] self.assertEqual(call.data[light.ATTR_BRIGHTNESS], 173) self.assertEqual(call.data[light.ATTR_XY_COLOR], [0.439, 0.37])
def test_automation_restore_state(hass): """Ensure states are restored on startup.""" time = dt_util.utcnow() mock_restore_cache(hass, ( State('automation.hello', STATE_ON), State('automation.bye', STATE_OFF, {'last_triggered': time}), )) config = {automation.DOMAIN: [{ 'alias': 'hello', 'trigger': { 'platform': 'event', 'event_type': 'test_event_hello', }, 'action': {'service': 'test.automation'} }, { 'alias': 'bye', 'trigger': { 'platform': 'event', 'event_type': 'test_event_bye', }, 'action': {'service': 'test.automation'} }]} assert (yield from async_setup_component(hass, automation.DOMAIN, config)) state = hass.states.get('automation.hello') assert state assert state.state == STATE_ON state = hass.states.get('automation.bye') assert state assert state.state == STATE_OFF assert state.attributes.get('last_triggered') == time calls = mock_service(hass, 'test', 'automation') assert automation.is_on(hass, 'automation.bye') is False hass.bus.async_fire('test_event_bye') yield from hass.async_block_till_done() assert len(calls) == 0 assert automation.is_on(hass, 'automation.hello') hass.bus.async_fire('test_event_hello') yield from hass.async_block_till_done() assert len(calls) == 1
def test_setup_component_test_with_cache_call_service_without_cache(self): """Setup demo platform with cache and call service without cache.""" calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) config = { tts.DOMAIN: { 'platform': 'demo', 'cache': True, } } with assert_setup_component(1, tts.DOMAIN): setup_component(self.hass, tts.DOMAIN, config) self.hass.services.call(tts.DOMAIN, 'demo_say', { tts.ATTR_MESSAGE: "I person is on front of your door.", tts.ATTR_CACHE: False, }) self.hass.block_till_done() assert len(calls) == 1 assert not os.path.isfile(os.path.join( self.default_tts_cache, "265944c108cbb00b2a621be5930513e03a0bb2cd_en_demo.mp3"))
def test_service_say_error(self, aioclient_mock): """Test service call say with http response 400.""" calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) aioclient_mock.post(self.url, data=self.form_data, status=400, content=b"test") config = {tts.DOMAIN: {"platform": "voicerss", "api_key": "1234567xx"}} with assert_setup_component(1, tts.DOMAIN): setup_component(self.hass, tts.DOMAIN, config) self.hass.services.call( tts.DOMAIN, "voicerss_say", {tts.ATTR_MESSAGE: "I person is on front of your door."}, ) self.hass.block_till_done() assert len(calls) == 0 assert len(aioclient_mock.mock_calls) == 1 assert aioclient_mock.mock_calls[0][2] == self.form_data
def test_service_say(self, mock_calculate, aioclient_mock): """Test service call say.""" calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) aioclient_mock.get(self.url, params=self.url_param, status=200, content=b"test") config = {tts.DOMAIN: {"platform": "google_translate"}} with assert_setup_component(1, tts.DOMAIN): setup_component(self.hass, tts.DOMAIN, config) self.hass.services.call( tts.DOMAIN, "google_translate_say", {tts.ATTR_MESSAGE: "90% of I person is on front of your door."}, ) self.hass.block_till_done() assert len(calls) == 1 assert len(aioclient_mock.mock_calls) == 1 assert calls[0].data[ATTR_MEDIA_CONTENT_ID].find(".mp3") != -1
def test_services(self): """ Test if the call service methods convert to correct service calls. """ services = { SERVICE_TURN_ON: media_player.turn_on, SERVICE_TURN_OFF: media_player.turn_off, SERVICE_TOGGLE: media_player.toggle, SERVICE_VOLUME_UP: media_player.volume_up, SERVICE_VOLUME_DOWN: media_player.volume_down, SERVICE_MEDIA_PLAY_PAUSE: media_player.media_play_pause, SERVICE_MEDIA_PLAY: media_player.media_play, SERVICE_MEDIA_PAUSE: media_player.media_pause, SERVICE_MEDIA_NEXT_TRACK: media_player.media_next_track, SERVICE_MEDIA_PREVIOUS_TRACK: media_player.media_previous_track } for service_name, service_method in services.items(): calls = mock_service(self.hass, media_player.DOMAIN, service_name) service_method(self.hass) self.hass.pool.block_till_done() self.assertEqual(1, len(calls)) call = calls[-1] self.assertEqual(media_player.DOMAIN, call.domain) self.assertEqual(service_name, call.service) service_method(self.hass, self.test_entity) self.hass.pool.block_till_done() self.assertEqual(2, len(calls)) call = calls[-1] self.assertEqual(media_player.DOMAIN, call.domain) self.assertEqual(service_name, call.service) self.assertEqual(self.test_entity, call.data.get(ATTR_ENTITY_ID))
def test_setup_component_and_test_with_service_options_def(self, def_mock): """Set up the demo platform and call service with default options.""" calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) config = {tts.DOMAIN: {"platform": "demo"}} with assert_setup_component(1, tts.DOMAIN): setup_component(self.hass, tts.DOMAIN, config) self.hass.services.call( tts.DOMAIN, "demo_say", { tts.ATTR_MESSAGE: "I person is on front of your door.", tts.ATTR_LANGUAGE: "de", }, ) self.hass.block_till_done() opt_hash = ctypes.c_size_t(hash(frozenset({"voice": "alex"}))).value assert len(calls) == 1 assert calls[0].data[ATTR_MEDIA_CONTENT_TYPE] == MEDIA_TYPE_MUSIC assert calls[0].data[ ATTR_MEDIA_CONTENT_ID ] == "{}/api/tts_proxy/265944c108cbb00b2a621be5930513e03a0bb2cd" "_de_{}_demo.mp3".format( self.hass.config.api.base_url, opt_hash ) assert os.path.isfile( os.path.join( self.default_tts_cache, "265944c108cbb00b2a621be5930513e03a0bb2cd_de_{0}_demo.mp3".format( opt_hash ), ) )
def test_service_say_specified_options(self, aioclient_mock): """Test service call say with options.""" calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) url_param = { 'text': 'HomeAssistant', 'lang': 'en-US', 'key': '1234567xx', 'speaker': 'zahar', 'format': 'mp3', 'emotion': 'evil', 'speed': 2 } aioclient_mock.get( self._base_url, status=200, content=b'test', params=url_param) config = { tts.DOMAIN: { 'platform': 'yandextts', 'api_key': '1234567xx', } } with assert_setup_component(1, tts.DOMAIN): setup_component(self.hass, tts.DOMAIN, config) self.hass.services.call(tts.DOMAIN, 'yandextts_say', { tts.ATTR_MESSAGE: "HomeAssistant", 'options': { 'emotion': 'evil', 'speed': 2, } }) self.hass.block_till_done() assert len(aioclient_mock.mock_calls) == 1 assert len(calls) == 1
def test_service_say_with_effect(self): """Test service call say with effects.""" calls = mock_service(self.opp, DOMAIN_MP, SERVICE_PLAY_MEDIA) config = { tts.DOMAIN: { "platform": "marytts", "effect": { "Volume": "amount:2.0;" } } } with assert_setup_component(1, tts.DOMAIN): setup_component(self.opp, tts.DOMAIN, config) with patch( "openpeerpower.components.marytts.tts.MaryTTS.speak", return_value=b"audio", ) as mock_speak: self.opp.services.call( tts.DOMAIN, "marytts_say", { "entity_id": "media_player.something", tts.ATTR_MESSAGE: "OpenPeerPower", }, ) self.opp.block_till_done() mock_speak.assert_called_once() mock_speak.assert_called_with("OpenPeerPower", {"Volume": "amount:2.0;"}) assert len(calls) == 1 assert calls[0].data[ATTR_MEDIA_CONTENT_ID].find(".wav") != -1
def test_setup_component_test_with_cache_dir(self): """Set up demo platform with cache and call service without cache.""" calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) _, demo_data = self.demo_provider.get_tts_audio("bla", "en") cache_file = os.path.join( self.default_tts_cache, "265944c108cbb00b2a621be5930513e03a0bb2cd_en_-_demo.mp3", ) os.mkdir(self.default_tts_cache) with open(cache_file, "wb") as voice_file: voice_file.write(demo_data) config = {tts.DOMAIN: {"platform": "demo", "cache": True}} with assert_setup_component(1, tts.DOMAIN): setup_component(self.hass, tts.DOMAIN, config) with patch( "homeassistant.components.demo.tts.DemoProvider." "get_tts_audio", return_value=(None, None), ): self.hass.services.call( tts.DOMAIN, "demo_say", {tts.ATTR_MESSAGE: "I person is on front of your door."}, ) self.hass.block_till_done() assert len(calls) == 1 assert calls[0].data[ ATTR_MEDIA_CONTENT_ID ] == "{}/api/tts_proxy/265944c108cbb00b2a621be5930513e03a0bb2cd" "_en_-_demo.mp3".format( self.hass.config.api.base_url )
def test_service_say_http_error(self, aioclient_mock): """Test service call say.""" calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) aioclient_mock.get(self.url, params=self.url_param, status=403, content=b'test') config = { tts.DOMAIN: { 'platform': 'marytts', } } with assert_setup_component(1, tts.DOMAIN): setup_component(self.hass, tts.DOMAIN, config) self.hass.services.call(tts.DOMAIN, 'marytts_say', { tts.ATTR_MESSAGE: "HomeAssistant", }) self.hass.block_till_done() assert len(calls) == 0
def test_service_call_to_command(self): """Test service call to command.""" config = copy(self.config_children_only) config['commands'] = { 'turn_off': { 'service': 'test.turn_off', 'data': {} } } config = validate_config(config) service = mock_service(self.hass, 'test', 'turn_off') ump = universal.UniversalMediaPlayer(self.hass, **config) ump.entity_id = media_player.ENTITY_ID_FORMAT.format(config['name']) run_coroutine_threadsafe(ump.async_update(), self.hass.loop).result() self.mock_mp_2._state = STATE_PLAYING self.mock_mp_2.schedule_update_ha_state() self.hass.block_till_done() run_coroutine_threadsafe(ump.async_update(), self.hass.loop).result() run_coroutine_threadsafe(ump.async_turn_off(), self.hass.loop).result() self.assertEqual(1, len(service))
def test_automation_is_on_if_no_initial_state_or_restore(hass): """Test initial value is on when no initial state or restored state.""" calls = mock_service(hass, 'test', 'automation') res = yield from async_setup_component( hass, automation.DOMAIN, { automation.DOMAIN: { 'alias': 'hello', 'trigger': { 'platform': 'event', 'event_type': 'test_event', }, 'action': { 'service': 'test.automation', 'entity_id': 'hello.world' } } }) assert res assert automation.is_on(hass, 'automation.hello') hass.bus.async_fire('test_event') yield from hass.async_block_till_done() assert len(calls) == 1
def test_setup_component_and_test_service_with_config_language(self): """Set up the demo platform and call service.""" calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) config = {tts.DOMAIN: {'platform': 'demo', 'language': 'de'}} with assert_setup_component(1, tts.DOMAIN): setup_component(self.hass, tts.DOMAIN, config) self.hass.services.call( tts.DOMAIN, 'demo_say', { tts.ATTR_MESSAGE: "I person is on front of your door.", }) self.hass.block_till_done() assert len(calls) == 1 assert calls[0].data[ATTR_MEDIA_CONTENT_TYPE] == MEDIA_TYPE_MUSIC assert calls[0].data[ATTR_MEDIA_CONTENT_ID] == \ "{}/api/tts_proxy/265944c108cbb00b2a621be5930513e03a0bb2cd" \ "_de_-_demo.mp3".format(self.hass.config.api.base_url) assert os.path.isfile( os.path.join( self.default_tts_cache, "265944c108cbb00b2a621be5930513e03a0bb2cd_de_-_demo.mp3"))
def test_setup_component_and_test_service_with_service_options(self): """Set up the demo platform and call service with options.""" calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) config = {tts.DOMAIN: {"platform": "demo"}} with assert_setup_component(1, tts.DOMAIN): setup_component(self.hass, tts.DOMAIN, config) self.hass.services.call( tts.DOMAIN, "demo_say", { "entity_id": "media_player.something", tts.ATTR_MESSAGE: "There is someone at the door.", tts.ATTR_LANGUAGE: "de", tts.ATTR_OPTIONS: { "voice": "alex" }, }, ) self.hass.block_till_done() opt_hash = ctypes.c_size_t(hash(frozenset({"voice": "alex"}))).value assert len(calls) == 1 assert calls[0].data[ATTR_MEDIA_CONTENT_TYPE] == MEDIA_TYPE_MUSIC assert calls[0].data[ ATTR_MEDIA_CONTENT_ID] == "{}/api/tts_proxy/42f18378fd4393d18c8dd11d03fa9563c1e54491_de_{}_demo.mp3".format( self.hass.config.api.base_url, opt_hash) assert os.path.isfile( os.path.join( self.default_tts_cache, "42f18378fd4393d18c8dd11d03fa9563c1e54491_de_{0}_demo.mp3". format(opt_hash), ))
def test_setup_component_and_test_service_with_base_url_set(self): """Set up the demo platform with ``base_url`` set and call service.""" calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) config = {tts.DOMAIN: {"platform": "demo", "base_url": "http://fnord"}} with assert_setup_component(1, tts.DOMAIN): setup_component(self.hass, tts.DOMAIN, config) self.hass.services.call( tts.DOMAIN, "demo_say", { "entity_id": "media_player.something", tts.ATTR_MESSAGE: "There is someone at the door.", }, ) self.hass.block_till_done() assert len(calls) == 1 assert calls[0].data[ATTR_MEDIA_CONTENT_TYPE] == MEDIA_TYPE_MUSIC assert (calls[0].data[ATTR_MEDIA_CONTENT_ID] == "http://fnord" "/api/tts_proxy/42f18378fd4393d18c8dd11d03fa9563c1e54491" "_en_-_demo.mp3")
def test_service_call_to_command(self): """Test service call to command.""" config = copy(self.config_children_only) config["commands"] = { "turn_off": { "service": "test.turn_off", "data": {} } } config = validate_config(config) service = mock_service(self.hass, "test", "turn_off") ump = universal.UniversalMediaPlayer(self.hass, **config) ump.entity_id = media_player.ENTITY_ID_FORMAT.format(config["name"]) run_coroutine_threadsafe(ump.async_update(), self.hass.loop).result() self.mock_mp_2._state = STATE_PLAYING self.mock_mp_2.schedule_update_ha_state() self.hass.block_till_done() run_coroutine_threadsafe(ump.async_update(), self.hass.loop).result() run_coroutine_threadsafe(ump.async_turn_off(), self.hass.loop).result() assert 1 == len(service)
def test_flux_with_multiple_lights(self): """Test the flux switch with multiple light entities.""" platform = loader.get_component('light.test') platform.init() self.assertTrue( setup_component(self.hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: 'test'}})) dev1, dev2, dev3 = platform.DEVICES light.turn_on(self.hass, entity_id=dev2.entity_id) self.hass.block_till_done() light.turn_on(self.hass, entity_id=dev3.entity_id) self.hass.block_till_done() state = self.hass.states.get(dev1.entity_id) self.assertEqual(STATE_ON, state.state) self.assertIsNone(state.attributes.get('xy_color')) self.assertIsNone(state.attributes.get('brightness')) state = self.hass.states.get(dev2.entity_id) self.assertEqual(STATE_ON, state.state) self.assertIsNone(state.attributes.get('xy_color')) self.assertIsNone(state.attributes.get('brightness')) state = self.hass.states.get(dev3.entity_id) self.assertEqual(STATE_ON, state.state) self.assertIsNone(state.attributes.get('xy_color')) self.assertIsNone(state.attributes.get('brightness')) test_time = dt_util.now().replace(hour=12, minute=0, second=0) sunset_time = test_time.replace(hour=17, minute=0, second=0) sunrise_time = test_time.replace(hour=5, minute=0, second=0) def event_date(hass, event, now=None): if event == 'sunrise': print('sunrise {}'.format(sunrise_time)) return sunrise_time else: print('sunset {}'.format(sunset_time)) return sunset_time with patch('homeassistant.util.dt.now', return_value=test_time): with patch('homeassistant.helpers.sun.get_astral_event_date', side_effect=event_date): assert setup_component(self.hass, switch.DOMAIN, { switch.DOMAIN: { 'platform': 'flux', 'name': 'flux', 'lights': [dev1.entity_id, dev2.entity_id, dev3.entity_id] } }) turn_on_calls = mock_service( self.hass, light.DOMAIN, SERVICE_TURN_ON) switch.turn_on(self.hass, 'switch.flux') self.hass.block_till_done() fire_time_changed(self.hass, test_time) self.hass.block_till_done() call = turn_on_calls[-1] self.assertEqual(call.data[light.ATTR_BRIGHTNESS], 163) self.assertEqual(call.data[light.ATTR_XY_COLOR], [0.46, 0.376]) call = turn_on_calls[-2] self.assertEqual(call.data[light.ATTR_BRIGHTNESS], 163) self.assertEqual(call.data[light.ATTR_XY_COLOR], [0.46, 0.376]) call = turn_on_calls[-3] self.assertEqual(call.data[light.ATTR_BRIGHTNESS], 163) self.assertEqual(call.data[light.ATTR_XY_COLOR], [0.46, 0.376])
def test_flux_with_multiple_lights(self): """Test the flux switch with multiple light entities.""" platform = loader.get_component('light.test') platform.init() self.assertTrue( light.setup(self.hass, {light.DOMAIN: { CONF_PLATFORM: 'test' }})) dev1, dev2, dev3 = platform.DEVICES light.turn_on(self.hass, entity_id=dev2.entity_id) self.hass.pool.block_till_done() light.turn_on(self.hass, entity_id=dev3.entity_id) self.hass.pool.block_till_done() state = self.hass.states.get(dev1.entity_id) self.assertEqual(STATE_ON, state.state) self.assertIsNone(state.attributes.get('xy_color')) self.assertIsNone(state.attributes.get('brightness')) state = self.hass.states.get(dev2.entity_id) self.assertEqual(STATE_ON, state.state) self.assertIsNone(state.attributes.get('xy_color')) self.assertIsNone(state.attributes.get('brightness')) state = self.hass.states.get(dev3.entity_id) self.assertEqual(STATE_ON, state.state) self.assertIsNone(state.attributes.get('xy_color')) self.assertIsNone(state.attributes.get('brightness')) test_time = dt_util.now().replace(hour=12, minute=0, second=0) sunset_time = test_time.replace(hour=17, minute=0, second=0) sunrise_time = test_time.replace(hour=5, minute=0, second=0) + timedelta(days=1) with patch('homeassistant.util.dt.now', return_value=test_time): with patch('homeassistant.components.sun.next_rising', return_value=sunrise_time): with patch('homeassistant.components.sun.next_setting', return_value=sunset_time): assert setup_component( self.hass, switch.DOMAIN, { switch.DOMAIN: { 'platform': 'flux', 'name': 'flux', 'lights': [ dev1.entity_id, dev2.entity_id, dev3.entity_id ] } }) turn_on_calls = mock_service(self.hass, light.DOMAIN, SERVICE_TURN_ON) switch.turn_on(self.hass, 'switch.flux') self.hass.pool.block_till_done() fire_time_changed(self.hass, test_time) self.hass.pool.block_till_done() call = turn_on_calls[-1] self.assertEqual(call.data[light.ATTR_BRIGHTNESS], 171) self.assertEqual(call.data[light.ATTR_XY_COLOR], [0.452, 0.386]) call = turn_on_calls[-2] self.assertEqual(call.data[light.ATTR_BRIGHTNESS], 171) self.assertEqual(call.data[light.ATTR_XY_COLOR], [0.452, 0.386]) call = turn_on_calls[-3] self.assertEqual(call.data[light.ATTR_BRIGHTNESS], 171) self.assertEqual(call.data[light.ATTR_XY_COLOR], [0.452, 0.386])
def setUp(self): # pylint: disable=invalid-name """Set up things to be run when tests are started.""" self.hass = get_test_home_assistant() self.calls = mock_service(self.hass, "test_domain", "test_service")
def test_toggle(self): """Test toggle method.""" calls = mock_service(self.hass, 'light', SERVICE_TOGGLE) comps.toggle(self.hass, 'light.Bowl') self.hass.block_till_done() self.assertEqual(1, len(calls))
def test_turn_off(self): """Test turn_off method.""" calls = mock_service(self.hass, 'light', SERVICE_TURN_OFF) comps.turn_off(self.hass, 'light.Bowl') self.hass.block_till_done() self.assertEqual(1, len(calls))
def test_turn_on(self): """Test turn_on method.""" calls = mock_service(self.hass, 'light', SERVICE_TURN_ON) comps.turn_on(self.hass, 'light.Ceiling') self.hass.block_till_done() self.assertEqual(1, len(calls))
def test_turn_on_without_entities(self): """Test turn_on method without entities.""" calls = mock_service(self.hass, 'light', SERVICE_TURN_ON) comps.turn_on(self.hass) self.hass.block_till_done() self.assertEqual(0, len(calls))
def __init__(self, hass, name): """Initialize the media player.""" self.hass = hass self._name = name self.entity_id = media_player.ENTITY_ID_FORMAT.format(name) self._state = STATE_OFF self._volume_level = 0 self._is_volume_muted = False self._media_title = None self._supported_features = 0 self._source = None self._tracks = 12 self._media_image_url = None self._shuffle = False self.service_calls = { "turn_on": mock_service(hass, media_player.DOMAIN, media_player.SERVICE_TURN_ON), "turn_off": mock_service(hass, media_player.DOMAIN, media_player.SERVICE_TURN_OFF), "mute_volume": mock_service(hass, media_player.DOMAIN, media_player.SERVICE_VOLUME_MUTE), "set_volume_level": mock_service(hass, media_player.DOMAIN, media_player.SERVICE_VOLUME_SET), "media_play": mock_service(hass, media_player.DOMAIN, media_player.SERVICE_MEDIA_PLAY), "media_pause": mock_service(hass, media_player.DOMAIN, media_player.SERVICE_MEDIA_PAUSE), "media_previous_track": mock_service(hass, media_player.DOMAIN, media_player.SERVICE_MEDIA_PREVIOUS_TRACK), "media_next_track": mock_service(hass, media_player.DOMAIN, media_player.SERVICE_MEDIA_NEXT_TRACK), "media_seek": mock_service(hass, media_player.DOMAIN, media_player.SERVICE_MEDIA_SEEK), "play_media": mock_service(hass, media_player.DOMAIN, media_player.SERVICE_PLAY_MEDIA), "volume_up": mock_service(hass, media_player.DOMAIN, media_player.SERVICE_VOLUME_UP), "volume_down": mock_service(hass, media_player.DOMAIN, media_player.SERVICE_VOLUME_DOWN), "media_play_pause": mock_service(hass, media_player.DOMAIN, media_player.SERVICE_MEDIA_PLAY_PAUSE), "select_source": mock_service(hass, media_player.DOMAIN, media_player.SERVICE_SELECT_SOURCE), "clear_playlist": mock_service(hass, media_player.DOMAIN, media_player.SERVICE_CLEAR_PLAYLIST), "shuffle_set": mock_service(hass, media_player.DOMAIN, media_player.SERVICE_SHUFFLE_SET), }
def test_methods(self): """Test if methods call the services as expected.""" # Test is_on self.hass.states.set("light.test", STATE_ON) assert light.is_on(self.hass, "light.test") self.hass.states.set("light.test", STATE_OFF) assert not light.is_on(self.hass, "light.test") self.hass.states.set(light.ENTITY_ID_ALL_LIGHTS, STATE_ON) assert light.is_on(self.hass) self.hass.states.set(light.ENTITY_ID_ALL_LIGHTS, STATE_OFF) assert not light.is_on(self.hass) # Test turn_on turn_on_calls = mock_service(self.hass, light.DOMAIN, SERVICE_TURN_ON) common.turn_on( self.hass, entity_id="entity_id_val", transition="transition_val", brightness="brightness_val", rgb_color="rgb_color_val", xy_color="xy_color_val", profile="profile_val", color_name="color_name_val", white_value="white_val", ) self.hass.block_till_done() assert 1 == len(turn_on_calls) call = turn_on_calls[-1] assert light.DOMAIN == call.domain assert SERVICE_TURN_ON == call.service assert "entity_id_val" == call.data.get(ATTR_ENTITY_ID) assert "transition_val" == call.data.get(light.ATTR_TRANSITION) assert "brightness_val" == call.data.get(light.ATTR_BRIGHTNESS) assert "rgb_color_val" == call.data.get(light.ATTR_RGB_COLOR) assert "xy_color_val" == call.data.get(light.ATTR_XY_COLOR) assert "profile_val" == call.data.get(light.ATTR_PROFILE) assert "color_name_val" == call.data.get(light.ATTR_COLOR_NAME) assert "white_val" == call.data.get(light.ATTR_WHITE_VALUE) # Test turn_off turn_off_calls = mock_service(self.hass, light.DOMAIN, SERVICE_TURN_OFF) common.turn_off(self.hass, entity_id="entity_id_val", transition="transition_val") self.hass.block_till_done() assert 1 == len(turn_off_calls) call = turn_off_calls[-1] assert light.DOMAIN == call.domain assert SERVICE_TURN_OFF == call.service assert "entity_id_val" == call.data[ATTR_ENTITY_ID] assert "transition_val" == call.data[light.ATTR_TRANSITION] # Test toggle toggle_calls = mock_service(self.hass, light.DOMAIN, SERVICE_TOGGLE) common.toggle(self.hass, entity_id="entity_id_val", transition="transition_val") self.hass.block_till_done() assert 1 == len(toggle_calls) call = toggle_calls[-1] assert light.DOMAIN == call.domain assert SERVICE_TOGGLE == call.service assert "entity_id_val" == call.data[ATTR_ENTITY_ID] assert "transition_val" == call.data[light.ATTR_TRANSITION]
def test_overrides(self): """Test overrides.""" config = copy(self.config_children_and_attr) excmd = {"service": "test.override", "data": {}} config["name"] = "overridden" config["commands"] = { "turn_on": excmd, "turn_off": excmd, "volume_up": excmd, "volume_down": excmd, "volume_mute": excmd, "volume_set": excmd, "select_sound_mode": excmd, "select_source": excmd, "repeat_set": excmd, "shuffle_set": excmd, "media_play": excmd, "media_play_pause": excmd, "media_pause": excmd, "media_stop": excmd, "media_next_track": excmd, "media_previous_track": excmd, "clear_playlist": excmd, "play_media": excmd, "toggle": excmd, } setup_component(self.hass, "media_player", {"media_player": config}) service = mock_service(self.hass, "test", "override") self.hass.services.call( "media_player", "turn_on", service_data={"entity_id": "media_player.overridden"}, blocking=True, ) assert len(service) == 1 self.hass.services.call( "media_player", "turn_off", service_data={"entity_id": "media_player.overridden"}, blocking=True, ) assert len(service) == 2 self.hass.services.call( "media_player", "volume_up", service_data={"entity_id": "media_player.overridden"}, blocking=True, ) assert len(service) == 3 self.hass.services.call( "media_player", "volume_down", service_data={"entity_id": "media_player.overridden"}, blocking=True, ) assert len(service) == 4 self.hass.services.call( "media_player", "volume_mute", service_data={ "entity_id": "media_player.overridden", "is_volume_muted": True, }, blocking=True, ) assert len(service) == 5 self.hass.services.call( "media_player", "volume_set", service_data={ "entity_id": "media_player.overridden", "volume_level": 1 }, blocking=True, ) assert len(service) == 6 self.hass.services.call( "media_player", "select_sound_mode", service_data={ "entity_id": "media_player.overridden", "sound_mode": "music", }, blocking=True, ) assert len(service) == 7 self.hass.services.call( "media_player", "select_source", service_data={ "entity_id": "media_player.overridden", "source": "video1" }, blocking=True, ) assert len(service) == 8 self.hass.services.call( "media_player", "repeat_set", service_data={ "entity_id": "media_player.overridden", "repeat": "all" }, blocking=True, ) assert len(service) == 9 self.hass.services.call( "media_player", "shuffle_set", service_data={ "entity_id": "media_player.overridden", "shuffle": True }, blocking=True, ) assert len(service) == 10 self.hass.services.call( "media_player", "media_play", service_data={"entity_id": "media_player.overridden"}, blocking=True, ) assert len(service) == 11 self.hass.services.call( "media_player", "media_pause", service_data={"entity_id": "media_player.overridden"}, blocking=True, ) assert len(service) == 12 self.hass.services.call( "media_player", "media_stop", service_data={"entity_id": "media_player.overridden"}, blocking=True, ) assert len(service) == 13 self.hass.services.call( "media_player", "media_next_track", service_data={"entity_id": "media_player.overridden"}, blocking=True, ) assert len(service) == 14 self.hass.services.call( "media_player", "media_previous_track", service_data={"entity_id": "media_player.overridden"}, blocking=True, ) assert len(service) == 15 self.hass.services.call( "media_player", "clear_playlist", service_data={"entity_id": "media_player.overridden"}, blocking=True, ) assert len(service) == 16 self.hass.services.call( "media_player", "media_play_pause", service_data={"entity_id": "media_player.overridden"}, blocking=True, ) assert len(service) == 17 self.hass.services.call( "media_player", "play_media", service_data={ "entity_id": "media_player.overridden", "media_content_id": 1, "media_content_type": "channel", }, blocking=True, ) assert len(service) == 18 self.hass.services.call( "media_player", "toggle", service_data={"entity_id": "media_player.overridden"}, blocking=True, ) assert len(service) == 19
def test_methods(self): """Test if methods call the services as expected.""" # Test is_on self.hass.states.set('light.test', STATE_ON) self.assertTrue(light.is_on(self.hass, 'light.test')) self.hass.states.set('light.test', STATE_OFF) self.assertFalse(light.is_on(self.hass, 'light.test')) self.hass.states.set(light.ENTITY_ID_ALL_LIGHTS, STATE_ON) self.assertTrue(light.is_on(self.hass)) self.hass.states.set(light.ENTITY_ID_ALL_LIGHTS, STATE_OFF) self.assertFalse(light.is_on(self.hass)) # Test turn_on turn_on_calls = mock_service(self.hass, light.DOMAIN, SERVICE_TURN_ON) light.turn_on(self.hass, entity_id='entity_id_val', transition='transition_val', brightness='brightness_val', rgb_color='rgb_color_val', xy_color='xy_color_val', profile='profile_val') self.hass.pool.block_till_done() self.assertEqual(1, len(turn_on_calls)) call = turn_on_calls[-1] self.assertEqual(light.DOMAIN, call.domain) self.assertEqual(SERVICE_TURN_ON, call.service) self.assertEqual('entity_id_val', call.data.get(ATTR_ENTITY_ID)) self.assertEqual('transition_val', call.data.get(light.ATTR_TRANSITION)) self.assertEqual('brightness_val', call.data.get(light.ATTR_BRIGHTNESS)) self.assertEqual('rgb_color_val', call.data.get(light.ATTR_RGB_COLOR)) self.assertEqual('xy_color_val', call.data.get(light.ATTR_XY_COLOR)) self.assertEqual('profile_val', call.data.get(light.ATTR_PROFILE)) # Test turn_off turn_off_calls = mock_service(self.hass, light.DOMAIN, SERVICE_TURN_OFF) light.turn_off(self.hass, entity_id='entity_id_val', transition='transition_val') self.hass.pool.block_till_done() self.assertEqual(1, len(turn_off_calls)) call = turn_off_calls[-1] self.assertEqual(light.DOMAIN, call.domain) self.assertEqual(SERVICE_TURN_OFF, call.service) self.assertEqual('entity_id_val', call.data[ATTR_ENTITY_ID]) self.assertEqual('transition_val', call.data[light.ATTR_TRANSITION]) # Test toggle toggle_calls = mock_service(self.hass, light.DOMAIN, SERVICE_TOGGLE) light.toggle(self.hass, entity_id='entity_id_val', transition='transition_val') self.hass.pool.block_till_done() self.assertEqual(1, len(toggle_calls)) call = toggle_calls[-1] self.assertEqual(light.DOMAIN, call.domain) self.assertEqual(SERVICE_TOGGLE, call.service) self.assertEqual('entity_id_val', call.data[ATTR_ENTITY_ID]) self.assertEqual('transition_val', call.data[light.ATTR_TRANSITION])
def test_toggle(self): """Test toggle method.""" calls = mock_service(self.hass, "light", SERVICE_TOGGLE) toggle(self.hass, "light.Bowl") self.hass.block_till_done() assert len(calls) == 1
def test_turn_off(self): """Test turn_off method.""" calls = mock_service(self.hass, "light", SERVICE_TURN_OFF) turn_off(self.hass, "light.Bowl") self.hass.block_till_done() assert len(calls) == 1
def test_turn_on(self): """Test turn_on method.""" calls = mock_service(self.hass, "light", SERVICE_TURN_ON) turn_on(self.hass, "light.Ceiling") self.hass.block_till_done() assert len(calls) == 1
def test_turn_on_without_entities(self): """Test turn_on method without entities.""" calls = mock_service(self.hass, "light", SERVICE_TURN_ON) turn_on(self.hass) self.hass.block_till_done() assert len(calls) == 0