desc
stringlengths 3
26.7k
| decl
stringlengths 11
7.89k
| bodies
stringlengths 8
553k
|
---|---|---|
'Test request with a valid username.'
| def test_valid_username_request(self):
| request_json = {'invalid_key': 'my_device'}
result = requests.post(BRIDGE_URL_BASE.format('/api'), data=json.dumps(request_json), timeout=5)
self.assertEqual(result.status_code, 400)
|
'Setup things to be run when tests are started.'
| def setUp(self):
| self.hass = get_test_home_assistant()
self.mock_publish = mock_mqtt_component(self.hass)
|
'Stop everything that was started.'
| def tearDown(self):
| self.hass.stop()
|
'Test the controlling state via topic.'
| def test_controlling_state_via_topic(self):
| assert setup_component(self.hass, lock.DOMAIN, {lock.DOMAIN: {'platform': 'mqtt', 'name': 'test', 'state_topic': 'state-topic', 'command_topic': 'command-topic', 'payload_lock': 'LOCK', 'payload_unlock': 'UNLOCK'}})
state = self.hass.states.get('lock.test')
self.assertEqual(STATE_UNLOCKED, state.state)
self.assertFalse(state.attributes.get(ATTR_ASSUMED_STATE))
fire_mqtt_message(self.hass, 'state-topic', 'LOCK')
self.hass.block_till_done()
state = self.hass.states.get('lock.test')
self.assertEqual(STATE_LOCKED, state.state)
fire_mqtt_message(self.hass, 'state-topic', 'UNLOCK')
self.hass.block_till_done()
state = self.hass.states.get('lock.test')
self.assertEqual(STATE_UNLOCKED, state.state)
|
'Test the sending MQTT commands in optimistic mode.'
| def test_sending_mqtt_commands_and_optimistic(self):
| assert setup_component(self.hass, lock.DOMAIN, {lock.DOMAIN: {'platform': 'mqtt', 'name': 'test', 'command_topic': 'command-topic', 'payload_lock': 'LOCK', 'payload_unlock': 'UNLOCK', 'qos': 2}})
state = self.hass.states.get('lock.test')
self.assertEqual(STATE_UNLOCKED, state.state)
self.assertTrue(state.attributes.get(ATTR_ASSUMED_STATE))
lock.lock(self.hass, 'lock.test')
self.hass.block_till_done()
self.assertEqual(('command-topic', 'LOCK', 2, False), self.mock_publish.mock_calls[(-2)][1])
state = self.hass.states.get('lock.test')
self.assertEqual(STATE_LOCKED, state.state)
lock.unlock(self.hass, 'lock.test')
self.hass.block_till_done()
self.assertEqual(('command-topic', 'UNLOCK', 2, False), self.mock_publish.mock_calls[(-2)][1])
state = self.hass.states.get('lock.test')
self.assertEqual(STATE_UNLOCKED, state.state)
|
'Test the controlling state via topic and JSON message.'
| def test_controlling_state_via_topic_and_json_message(self):
| assert setup_component(self.hass, lock.DOMAIN, {lock.DOMAIN: {'platform': 'mqtt', 'name': 'test', 'state_topic': 'state-topic', 'command_topic': 'command-topic', 'payload_lock': 'LOCK', 'payload_unlock': 'UNLOCK', 'value_template': '{{ value_json.val }}'}})
state = self.hass.states.get('lock.test')
self.assertEqual(STATE_UNLOCKED, state.state)
fire_mqtt_message(self.hass, 'state-topic', '{"val":"LOCK"}')
self.hass.block_till_done()
state = self.hass.states.get('lock.test')
self.assertEqual(STATE_LOCKED, state.state)
fire_mqtt_message(self.hass, 'state-topic', '{"val":"UNLOCK"}')
self.hass.block_till_done()
state = self.hass.states.get('lock.test')
self.assertEqual(STATE_UNLOCKED, state.state)
|
'Setup things to be run when tests are started.'
| def setUp(self):
| self.hass = get_test_home_assistant()
self.assertTrue(setup_component(self.hass, lock.DOMAIN, {'lock': {'platform': 'demo'}}))
|
'Stop everything that was started.'
| def tearDown(self):
| self.hass.stop()
|
'Test if lock is locked.'
| def test_is_locked(self):
| self.assertTrue(lock.is_locked(self.hass, FRONT))
self.hass.states.is_state(FRONT, 'locked')
self.assertFalse(lock.is_locked(self.hass, KITCHEN))
self.hass.states.is_state(KITCHEN, 'unlocked')
|
'Test the locking of a lock.'
| def test_locking(self):
| lock.lock(self.hass, KITCHEN)
self.hass.block_till_done()
self.assertTrue(lock.is_locked(self.hass, KITCHEN))
|
'Test the unlocking of a lock.'
| def test_unlocking(self):
| lock.unlock(self.hass, FRONT)
self.hass.block_till_done()
self.assertFalse(lock.is_locked(self.hass, FRONT))
|
'Setup things to be run when tests are started.'
| def setup_method(self):
| self.hass = get_test_home_assistant()
self._base_url = 'https://tts.voicetech.yandex.net/generate?'
|
'Stop everything that was started.'
| def teardown_method(self):
| default_tts = self.hass.config.path(tts.DEFAULT_CACHE_DIR)
if os.path.isdir(default_tts):
shutil.rmtree(default_tts)
self.hass.stop()
|
'Test setup component.'
| def test_setup_component(self):
| config = {tts.DOMAIN: {'platform': 'yandextts', 'api_key': '1234567xx'}}
with assert_setup_component(1, tts.DOMAIN):
setup_component(self.hass, tts.DOMAIN, config)
|
'Test setup component without api key.'
| def test_setup_component_without_api_key(self):
| config = {tts.DOMAIN: {'platform': 'yandextts'}}
with assert_setup_component(0, tts.DOMAIN):
setup_component(self.hass, tts.DOMAIN, config)
|
'Test service call say.'
| def test_service_say(self, aioclient_mock):
| calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA)
url_param = {'text': 'HomeAssistant', 'lang': 'en-US', 'key': '1234567xx', 'speaker': 'zahar', 'format': 'mp3', 'emotion': 'neutral', 'speed': 1}
aioclient_mock.get(self._base_url, status=200, content='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'})
self.hass.block_till_done()
assert (len(aioclient_mock.mock_calls) == 1)
assert (len(calls) == 1)
|
'Test service call say.'
| def test_service_say_russian_config(self, aioclient_mock):
| 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='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)
|
'Test service call say.'
| def test_service_say_russian_service(self, aioclient_mock):
| 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='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', tts.ATTR_LANGUAGE: 'ru-RU'})
self.hass.block_till_done()
assert (len(aioclient_mock.mock_calls) == 1)
assert (len(calls) == 1)
|
'Test service call say.'
| def test_service_say_timeout(self, aioclient_mock):
| calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA)
url_param = {'text': 'HomeAssistant', 'lang': 'en-US', 'key': '1234567xx', 'speaker': 'zahar', 'format': 'mp3', 'emotion': 'neutral', 'speed': 1}
aioclient_mock.get(self._base_url, status=200, exc=asyncio.TimeoutError(), 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'})
self.hass.block_till_done()
assert (len(calls) == 0)
assert (len(aioclient_mock.mock_calls) == 1)
|
'Test service call say.'
| def test_service_say_http_error(self, aioclient_mock):
| calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA)
url_param = {'text': 'HomeAssistant', 'lang': 'en-US', 'key': '1234567xx', 'speaker': 'zahar', 'format': 'mp3', 'emotion': 'neutral', 'speed': 1}
aioclient_mock.get(self._base_url, status=403, content='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'})
self.hass.block_till_done()
assert (len(calls) == 0)
|
'Test service call say.'
| def test_service_say_specifed_speaker(self, aioclient_mock):
| calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA)
url_param = {'text': 'HomeAssistant', 'lang': 'en-US', 'key': '1234567xx', 'speaker': 'alyss', 'format': 'mp3', 'emotion': 'neutral', 'speed': 1}
aioclient_mock.get(self._base_url, status=200, content='test', params=url_param)
config = {tts.DOMAIN: {'platform': 'yandextts', 'api_key': '1234567xx', 'voice': 'alyss'}}
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)
|
'Test service call say.'
| def test_service_say_specifed_emotion(self, aioclient_mock):
| 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': 1}
aioclient_mock.get(self._base_url, status=200, content='test', params=url_param)
config = {tts.DOMAIN: {'platform': 'yandextts', 'api_key': '1234567xx', 'emotion': 'evil'}}
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)
|
'Test service call say.'
| def test_service_say_specified_low_speed(self, aioclient_mock):
| calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA)
url_param = {'text': 'HomeAssistant', 'lang': 'en-US', 'key': '1234567xx', 'speaker': 'zahar', 'format': 'mp3', 'emotion': 'neutral', 'speed': '0.1'}
aioclient_mock.get(self._base_url, status=200, content='test', params=url_param)
config = {tts.DOMAIN: {'platform': 'yandextts', 'api_key': '1234567xx', 'speed': 0.1}}
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)
|
'Test service call say.'
| def test_service_say_specified_speed(self, aioclient_mock):
| calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA)
url_param = {'text': 'HomeAssistant', 'lang': 'en-US', 'key': '1234567xx', 'speaker': 'zahar', 'format': 'mp3', 'emotion': 'neutral', 'speed': 2}
aioclient_mock.get(self._base_url, status=200, content='test', params=url_param)
config = {tts.DOMAIN: {'platform': 'yandextts', 'api_key': '1234567xx', 'speed': 2}}
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)
|
'Setup things to be run when tests are started.'
| def setup_method(self):
| self.hass = get_test_home_assistant()
self.url = 'https://api.voicerss.org/'
self.form_data = {'key': '1234567xx', 'hl': 'en-us', 'c': 'MP3', 'f': '8khz_8bit_mono', 'src': 'I person is on front of your door.'}
|
'Stop everything that was started.'
| def teardown_method(self):
| default_tts = self.hass.config.path(tts.DEFAULT_CACHE_DIR)
if os.path.isdir(default_tts):
shutil.rmtree(default_tts)
self.hass.stop()
|
'Test setup component.'
| def test_setup_component(self):
| config = {tts.DOMAIN: {'platform': 'voicerss', 'api_key': '1234567xx'}}
with assert_setup_component(1, tts.DOMAIN):
setup_component(self.hass, tts.DOMAIN, config)
|
'Test setup component without api key.'
| def test_setup_component_without_api_key(self):
| config = {tts.DOMAIN: {'platform': 'voicerss'}}
with assert_setup_component(0, tts.DOMAIN):
setup_component(self.hass, tts.DOMAIN, config)
|
'Test service call say.'
| def test_service_say(self, aioclient_mock):
| calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA)
aioclient_mock.post(self.url, data=self.form_data, status=200, content='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))
|
'Test service call say with german code in the config.'
| def test_service_say_german_config(self, aioclient_mock):
| 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='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)
|
'Test service call say with german code in the service.'
| def test_service_say_german_service(self, aioclient_mock):
| 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='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.', tts.ATTR_LANGUAGE: 'de-de'})
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)
|
'Test service call say with http response 400.'
| def test_service_say_error(self, aioclient_mock):
| calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA)
aioclient_mock.post(self.url, data=self.form_data, status=400, content='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)
|
'Test service call say with http timeout.'
| def test_service_say_timeout(self, aioclient_mock):
| 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)
|
'Test service call say with http error api message.'
| def test_service_say_error_msg(self, aioclient_mock):
| calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA)
aioclient_mock.post(self.url, data=self.form_data, status=200, content='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)
|
'Setup things to be run when tests are started.'
| def setup_method(self):
| self.hass = get_test_home_assistant()
self.demo_provider = DemoProvider('en')
self.default_tts_cache = self.hass.config.path(tts.DEFAULT_CACHE_DIR)
setup_component(self.hass, http.DOMAIN, {http.DOMAIN: {http.CONF_SERVER_PORT: get_test_instance_port()}})
|
'Stop everything that was started.'
| def teardown_method(self):
| if os.path.isdir(self.default_tts_cache):
shutil.rmtree(self.default_tts_cache)
self.hass.stop()
|
'Setup the demo platform with defaults.'
| def test_setup_component_demo(self):
| config = {tts.DOMAIN: {'platform': 'demo'}}
with assert_setup_component(1, tts.DOMAIN):
setup_component(self.hass, tts.DOMAIN, config)
assert self.hass.services.has_service(tts.DOMAIN, 'demo_say')
assert self.hass.services.has_service(tts.DOMAIN, 'clear_cache')
|
'Setup the demo platform with defaults.'
| @patch('os.mkdir', side_effect=OSError(2, 'No access'))
def test_setup_component_demo_no_access_cache_folder(self, mock_mkdir):
| config = {tts.DOMAIN: {'platform': 'demo'}}
assert (not setup_component(self.hass, tts.DOMAIN, config))
assert (not self.hass.services.has_service(tts.DOMAIN, 'demo_say'))
assert (not self.hass.services.has_service(tts.DOMAIN, 'clear_cache'))
|
'Setup the demo platform and call service.'
| def test_setup_component_and_test_service(self):
| 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].find('/api/tts_proxy/265944c108cbb00b2a621be5930513e03a0bb2cd_en_-_demo.mp3') != (-1))
assert os.path.isfile(os.path.join(self.default_tts_cache, '265944c108cbb00b2a621be5930513e03a0bb2cd_en_-_demo.mp3'))
|
'Setup the demo platform and call service.'
| def test_setup_component_and_test_service_with_config_language(self):
| 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].find('/api/tts_proxy/265944c108cbb00b2a621be5930513e03a0bb2cd_de_-_demo.mp3') != (-1))
assert os.path.isfile(os.path.join(self.default_tts_cache, '265944c108cbb00b2a621be5930513e03a0bb2cd_de_-_demo.mp3'))
|
'Setup the demo platform and call service with wrong config.'
| def test_setup_component_and_test_service_with_wrong_conf_language(self):
| config = {tts.DOMAIN: {'platform': 'demo', 'language': 'ru'}}
with assert_setup_component(0, tts.DOMAIN):
setup_component(self.hass, tts.DOMAIN, config)
|
'Setup the demo platform and call service.'
| def test_setup_component_and_test_service_with_service_language(self):
| 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()
assert (len(calls) == 1)
assert (calls[0].data[ATTR_MEDIA_CONTENT_TYPE] == MEDIA_TYPE_MUSIC)
assert (calls[0].data[ATTR_MEDIA_CONTENT_ID].find('/api/tts_proxy/265944c108cbb00b2a621be5930513e03a0bb2cd_de_-_demo.mp3') != (-1))
assert os.path.isfile(os.path.join(self.default_tts_cache, '265944c108cbb00b2a621be5930513e03a0bb2cd_de_-_demo.mp3'))
|
'Setup the demo platform and call service.'
| def test_setup_component_test_service_with_wrong_service_language(self):
| 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: 'lang'})
self.hass.block_till_done()
assert (len(calls) == 0)
assert (not os.path.isfile(os.path.join(self.default_tts_cache, '265944c108cbb00b2a621be5930513e03a0bb2cd_lang_-_demo.mp3')))
|
'Setup the demo platform and call service with options.'
| def test_setup_component_and_test_service_with_service_options(self):
| 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: {'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].find('/api/tts_proxy/265944c108cbb00b2a621be5930513e03a0bb2cd_de_{0}_demo.mp3'.format(opt_hash)) != (-1))
assert os.path.isfile(os.path.join(self.default_tts_cache, '265944c108cbb00b2a621be5930513e03a0bb2cd_de_{0}_demo.mp3'.format(opt_hash)))
|
'Setup the demo platform and call service with default options.'
| @patch('homeassistant.components.tts.demo.DemoProvider.default_options', new_callable=PropertyMock(return_value={'voice': 'alex'}))
def test_setup_component_and_test_with_service_options_def(self, def_mock):
| 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].find('/api/tts_proxy/265944c108cbb00b2a621be5930513e03a0bb2cd_de_{0}_demo.mp3'.format(opt_hash)) != (-1))
assert os.path.isfile(os.path.join(self.default_tts_cache, '265944c108cbb00b2a621be5930513e03a0bb2cd_de_{0}_demo.mp3'.format(opt_hash)))
|
'Setup the demo platform and call service with wrong options.'
| def test_setup_component_and_test_service_with_service_options_wrong(self):
| 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))))
|
'Setup the demo platform and call service clear cache.'
| def test_setup_component_and_test_service_clear_cache(self):
| 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')))
|
'Setup the demo platform and call service and receive voice.'
| def test_setup_component_and_test_service_with_receive_voice(self):
| 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', 'en')
demo_data = tts.SpeechManager.write_tags('265944c108cbb00b2a621be5930513e03a0bb2cd_en_-_demo.mp3', demo_data, self.demo_provider, 'I person is on front of your door.', 'en', None)
assert (req.status_code == 200)
assert (req.content == demo_data)
|
'Setup the demo platform and call service and receive voice.'
| def test_setup_component_and_test_service_with_receive_voice_german(self):
| 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)
|
'Setup the demo platform and receive wrong file from web.'
| def test_setup_component_and_web_view_wrong_file(self):
| config = {tts.DOMAIN: {'platform': 'demo'}}
with assert_setup_component(1, tts.DOMAIN):
setup_component(self.hass, tts.DOMAIN, config)
self.hass.start()
url = '{}/api/tts_proxy/265944c108cbb00b2a621be5930513e03a0bb2cd_en_-_demo.mp3'.format(self.hass.config.api.base_url)
req = requests.get(url)
assert (req.status_code == 404)
|
'Setup the demo platform and receive wrong filename from web.'
| def test_setup_component_and_web_view_wrong_filename(self):
| config = {tts.DOMAIN: {'platform': 'demo'}}
with assert_setup_component(1, tts.DOMAIN):
setup_component(self.hass, tts.DOMAIN, config)
self.hass.start()
url = '{}/api/tts_proxy/265944dsk32c1b2a621be5930510bb2cd_en_-_demo.mp3'.format(self.hass.config.api.base_url)
req = requests.get(url)
assert (req.status_code == 404)
|
'Setup demo platform without cache.'
| def test_setup_component_test_without_cache(self):
| calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA)
config = {tts.DOMAIN: {'platform': 'demo', 'cache': False}}
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 (not os.path.isfile(os.path.join(self.default_tts_cache, '265944c108cbb00b2a621be5930513e03a0bb2cd_en_-_demo.mp3')))
|
'Setup demo platform with cache and call service without cache.'
| def test_setup_component_test_with_cache_call_service_without_cache(self):
| 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')))
|
'Setup demo platform with cache and call service without cache.'
| def test_setup_component_test_with_cache_dir(self):
| 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].find('/api/tts_proxy/265944c108cbb00b2a621be5930513e03a0bb2cd_en_-_demo.mp3') != (-1))
|
'Setup demo platform with wrong get_tts_audio.'
| @patch('homeassistant.components.tts.demo.DemoProvider.get_tts_audio', return_value=(None, None))
def test_setup_component_test_with_error_on_get_tts(self, tts_mock):
| 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) == 0)
|
'Setup component and load cache and get without mem cache.'
| def test_setup_component_load_cache_retrieve_without_mem_cache(self):
| (_, 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)
self.hass.start()
url = '{}/api/tts_proxy/265944c108cbb00b2a621be5930513e03a0bb2cd_en_-_demo.mp3'.format(self.hass.config.api.base_url)
req = requests.get(url)
assert (req.status_code == 200)
assert (req.content == demo_data)
|
'Setup things to be run when tests are started.'
| def setup_method(self):
| self.hass = get_test_home_assistant()
self.url = 'http://localhost:59125/process?'
self.url_param = {'INPUT_TEXT': 'HomeAssistant', 'INPUT_TYPE': 'TEXT', 'AUDIO': 'WAVE', 'VOICE': 'cmu-slt-hsmm', 'OUTPUT_TYPE': 'AUDIO', 'LOCALE': 'en_US'}
|
'Stop everything that was started.'
| def teardown_method(self):
| default_tts = self.hass.config.path(tts.DEFAULT_CACHE_DIR)
if os.path.isdir(default_tts):
shutil.rmtree(default_tts)
self.hass.stop()
|
'Test setup component.'
| def test_setup_component(self):
| config = {tts.DOMAIN: {'platform': 'marytts'}}
with assert_setup_component(1, tts.DOMAIN):
setup_component(self.hass, tts.DOMAIN, config)
|
'Test service call say.'
| def test_service_say(self, aioclient_mock):
| calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA)
aioclient_mock.get(self.url, params=self.url_param, status=200, content='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(aioclient_mock.mock_calls) == 1)
assert (len(calls) == 1)
|
'Test service call say.'
| def test_service_say_timeout(self, aioclient_mock):
| calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA)
aioclient_mock.get(self.url, params=self.url_param, status=200, exc=asyncio.TimeoutError())
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)
assert (len(aioclient_mock.mock_calls) == 1)
|
'Test service call say.'
| def test_service_say_http_error(self, aioclient_mock):
| calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA)
aioclient_mock.get(self.url, params=self.url_param, status=403, content='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)
|
'Setup things to be run when tests are started.'
| def setup_method(self):
| self.hass = get_test_home_assistant()
self.url = 'http://translate.google.com/translate_tts'
self.url_param = {'tl': 'en', 'q': '90%25%20of%20I%20person%20is%20on%20front%20of%20your%20door.', 'tk': 5, 'client': 'tw-ob', 'textlen': 41, 'total': 1, 'idx': 0, 'ie': 'UTF-8'}
|
'Stop everything that was started.'
| def teardown_method(self):
| default_tts = self.hass.config.path(tts.DEFAULT_CACHE_DIR)
if os.path.isdir(default_tts):
shutil.rmtree(default_tts)
self.hass.stop()
|
'Test setup component.'
| def test_setup_component(self):
| config = {tts.DOMAIN: {'platform': 'google'}}
with assert_setup_component(1, tts.DOMAIN):
setup_component(self.hass, tts.DOMAIN, config)
|
'Test service call say.'
| @patch('gtts_token.gtts_token.Token.calculate_token', autospec=True, return_value=5)
def test_service_say(self, mock_calculate, aioclient_mock):
| calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA)
aioclient_mock.get(self.url, params=self.url_param, status=200, content='test')
config = {tts.DOMAIN: {'platform': 'google'}}
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)
assert (calls[0].data[ATTR_MEDIA_CONTENT_ID].find('.mp3') != (-1))
|
'Test service call say with german code in the config.'
| @patch('gtts_token.gtts_token.Token.calculate_token', autospec=True, return_value=5)
def test_service_say_german_config(self, mock_calculate, aioclient_mock):
| 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='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)
|
'Test service call say with german code in the service.'
| @patch('gtts_token.gtts_token.Token.calculate_token', autospec=True, return_value=5)
def test_service_say_german_service(self, mock_calculate, aioclient_mock):
| 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='test')
config = {tts.DOMAIN: {'platform': 'google'}}
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.', tts.ATTR_LANGUAGE: 'de'})
self.hass.block_till_done()
assert (len(calls) == 1)
assert (len(aioclient_mock.mock_calls) == 1)
|
'Test service call say with http response 400.'
| @patch('gtts_token.gtts_token.Token.calculate_token', autospec=True, return_value=5)
def test_service_say_error(self, mock_calculate, aioclient_mock):
| calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA)
aioclient_mock.get(self.url, params=self.url_param, status=400, content='test')
config = {tts.DOMAIN: {'platform': 'google'}}
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) == 0)
assert (len(aioclient_mock.mock_calls) == 1)
|
'Test service call say with http timeout.'
| @patch('gtts_token.gtts_token.Token.calculate_token', autospec=True, return_value=5)
def test_service_say_timeout(self, mock_calculate, aioclient_mock):
| calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA)
aioclient_mock.get(self.url, params=self.url_param, exc=asyncio.TimeoutError())
config = {tts.DOMAIN: {'platform': 'google'}}
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) == 0)
assert (len(aioclient_mock.mock_calls) == 1)
|
'Test service call say with a lot of text.'
| @patch('gtts_token.gtts_token.Token.calculate_token', autospec=True, return_value=5)
def test_service_say_long_size(self, mock_calculate, aioclient_mock):
| calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA)
self.url_param['total'] = 9
self.url_param['q'] = 'I%20person%20is%20on%20front%20of%20your%20door'
self.url_param['textlen'] = 33
for idx in range(0, 9):
self.url_param['idx'] = idx
aioclient_mock.get(self.url, params=self.url_param, status=200, content='test')
config = {tts.DOMAIN: {'platform': 'google'}}
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: 'I person is on front of your door.I person is on front of your door.I person is on front of your door.I person is on front of your door.I person is on front of your door.I person is on front of your door.I person is on front of your door.I person is on front of your door.I person is on front of your door.'})
self.hass.block_till_done()
assert (len(calls) == 1)
assert (len(aioclient_mock.mock_calls) == 9)
assert (calls[0].data[ATTR_MEDIA_CONTENT_ID].find('.mp3') != (-1))
|
'Setup things to be run when tests are started.'
| def setUp(self):
| self.hass = get_test_home_assistant()
self.log_filter = None
|
'Stop everything that was started.'
| def tearDown(self):
| del logging.root.handlers[(-1)]
self.hass.stop()
|
'Setup logger and save log filter.'
| def setup_logger(self, config):
| setup_component(self.hass, logger.DOMAIN, config)
self.log_filter = logging.root.handlers[(-1)].filters[0]
|
'Assert that a certain record was logged.'
| def assert_logged(self, name, level):
| self.assertTrue(self.log_filter.filter(RECORD(name, level)))
|
'Assert that a certain record was not logged.'
| def assert_not_logged(self, name, level):
| self.assertFalse(self.log_filter.filter(RECORD(name, level)))
|
'Use logger to create a logging filter.'
| def test_logger_setup(self):
| self.setup_logger(TEST_CONFIG)
self.assertTrue((len(logging.root.handlers) > 0))
handler = logging.root.handlers[(-1)]
self.assertEqual(len(handler.filters), 1)
log_filter = handler.filters[0].logfilter
self.assertEqual(log_filter['default'], logging.WARNING)
self.assertEqual(log_filter['logs']['test'], logging.INFO)
|
'Test resulting filter operation.'
| def test_logger_test_filters(self):
| self.setup_logger(TEST_CONFIG)
self.assert_not_logged('asdf', logging.DEBUG)
self.assert_logged('asdf', logging.WARNING)
self.assert_not_logged('test', logging.DEBUG)
self.assert_logged('test', logging.INFO)
|
'Test change log level from empty configuration.'
| def test_set_filter_empty_config(self):
| self.setup_logger(NO_LOGS_CONFIG)
self.assert_not_logged('test', logging.DEBUG)
self.hass.services.call(logger.DOMAIN, 'set_level', {'test': 'debug'})
self.hass.block_till_done()
self.assert_logged('test', logging.DEBUG)
|
'Test change log level of existing filter.'
| def test_set_filter(self):
| self.setup_logger(TEST_CONFIG)
self.assert_not_logged('asdf', logging.DEBUG)
self.assert_logged('dummy', logging.WARNING)
self.hass.services.call(logger.DOMAIN, 'set_level', {'asdf': 'debug', 'dummy': 'info'})
self.hass.block_till_done()
self.assert_logged('asdf', logging.DEBUG)
self.assert_logged('dummy', logging.WARNING)
|
'Init pilight client, ignore parameters.'
| def __init__(self, host, port):
| pass
|
'Called pilight.send service is called.'
| def send_code(self, call):
| _LOGGER.error(('PilightDaemonSim payload: ' + str(call)))
|
'Called homeassistant.start is called.
Also sends one test message after start up'
| def start(self):
| _LOGGER.error('PilightDaemonSim start')
if (not self.called):
self.callback(self.test_message)
self.called = True
|
'Called homeassistant.stop is called.'
| def stop(self):
| _LOGGER.error('PilightDaemonSim stop')
|
'Callback called on event pilight.pilight_received.'
| def set_callback(self, function):
| self.callback = function
_LOGGER.error(('PilightDaemonSim callback: ' + str(function)))
|
'Setup things to be run when tests are started.'
| def setUp(self):
| self.hass = get_test_home_assistant()
self.skip_teardown_stop = False
|
'Stop everything that was started.'
| def tearDown(self):
| if (not self.skip_teardown_stop):
self.hass.stop()
|
'Try to connect at 127.0.0.1:5000 with socket error.'
| @patch('homeassistant.components.pilight._LOGGER.error')
def test_connection_failed_error(self, mock_error):
| with assert_setup_component(4):
with patch('pilight.pilight.Client', side_effect=socket.error) as mock_client:
self.assertFalse(setup_component(self.hass, pilight.DOMAIN, {pilight.DOMAIN: {}}))
mock_client.assert_called_once_with(host=pilight.DEFAULT_HOST, port=pilight.DEFAULT_PORT)
self.assertEqual(1, mock_error.call_count)
|
'Try to connect at 127.0.0.1:5000 with socket timeout.'
| @patch('homeassistant.components.pilight._LOGGER.error')
def test_connection_timeout_error(self, mock_error):
| with assert_setup_component(4):
with patch('pilight.pilight.Client', side_effect=socket.timeout) as mock_client:
self.assertFalse(setup_component(self.hass, pilight.DOMAIN, {pilight.DOMAIN: {}}))
mock_client.assert_called_once_with(host=pilight.DEFAULT_HOST, port=pilight.DEFAULT_PORT)
self.assertEqual(1, mock_error.call_count)
|
'Try to send data without protocol information, should give error.'
| @patch('pilight.pilight.Client', PilightDaemonSim)
@patch('homeassistant.core._LOGGER.error')
@patch('tests.components.test_pilight._LOGGER.error')
def test_send_code_no_protocol(self, mock_pilight_error, mock_error):
| with assert_setup_component(4):
self.assertTrue(setup_component(self.hass, pilight.DOMAIN, {pilight.DOMAIN: {}}))
self.hass.services.call(pilight.DOMAIN, pilight.SERVICE_NAME, service_data={'noprotocol': 'test', 'value': 42}, blocking=True)
self.hass.block_till_done()
error_log_call = mock_error.call_args_list[(-1)]
self.assertTrue(("required key not provided @ data['protocol']" in str(error_log_call)))
|
'Try to send proper data.'
| @patch('pilight.pilight.Client', PilightDaemonSim)
@patch('tests.components.test_pilight._LOGGER.error')
def test_send_code(self, mock_pilight_error):
| with assert_setup_component(4):
self.assertTrue(setup_component(self.hass, pilight.DOMAIN, {pilight.DOMAIN: {}}))
service_data = {'protocol': 'test', 'value': 42}
self.hass.services.call(pilight.DOMAIN, pilight.SERVICE_NAME, service_data=service_data, blocking=True)
self.hass.block_till_done()
error_log_call = mock_pilight_error.call_args_list[(-1)]
service_data['protocol'] = [service_data['protocol']]
self.assertTrue((str(service_data) in str(error_log_call)))
|
'Check IOError exception error message.'
| @patch('pilight.pilight.Client', PilightDaemonSim)
@patch('homeassistant.components.pilight._LOGGER.error')
def test_send_code_fail(self, mock_pilight_error):
| with assert_setup_component(4):
with patch('pilight.pilight.Client.send_code', side_effect=IOError):
self.assertTrue(setup_component(self.hass, pilight.DOMAIN, {pilight.DOMAIN: {}}))
service_data = {'protocol': 'test', 'value': 42}
self.hass.services.call(pilight.DOMAIN, pilight.SERVICE_NAME, service_data=service_data, blocking=True)
self.hass.block_till_done()
error_log_call = mock_pilight_error.call_args_list[(-1)]
self.assertTrue(('Pilight send failed' in str(error_log_call)))
|
'Try to send proper data with delay afterwards.'
| @patch('pilight.pilight.Client', PilightDaemonSim)
@patch('tests.components.test_pilight._LOGGER.error')
def test_send_code_delay(self, mock_pilight_error):
| with assert_setup_component(4):
self.assertTrue(setup_component(self.hass, pilight.DOMAIN, {pilight.DOMAIN: {pilight.CONF_SEND_DELAY: 5.0}}))
service_data1 = {'protocol': 'test11', 'value': 42}
service_data2 = {'protocol': 'test22', 'value': 42}
self.hass.services.call(pilight.DOMAIN, pilight.SERVICE_NAME, service_data=service_data1, blocking=True)
self.hass.services.call(pilight.DOMAIN, pilight.SERVICE_NAME, service_data=service_data2, blocking=True)
service_data1['protocol'] = [service_data1['protocol']]
service_data2['protocol'] = [service_data2['protocol']]
self.hass.bus.fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: dt_util.utcnow()})
self.hass.block_till_done()
error_log_call = mock_pilight_error.call_args_list[(-1)]
self.assertTrue((str(service_data1) in str(error_log_call)))
new_time = (dt_util.utcnow() + timedelta(seconds=5))
self.hass.bus.fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: new_time})
self.hass.block_till_done()
error_log_call = mock_pilight_error.call_args_list[(-1)]
self.assertTrue((str(service_data2) in str(error_log_call)))
|
'Check correct startup and stop of pilight daemon.'
| @patch('pilight.pilight.Client', PilightDaemonSim)
@patch('tests.components.test_pilight._LOGGER.error')
def test_start_stop(self, mock_pilight_error):
| with assert_setup_component(4):
self.assertTrue(setup_component(self.hass, pilight.DOMAIN, {pilight.DOMAIN: {}}))
self.hass.start()
self.hass.block_till_done()
error_log_call = mock_pilight_error.call_args_list[(-2)]
self.assertTrue(('PilightDaemonSim callback' in str(error_log_call)))
error_log_call = mock_pilight_error.call_args_list[(-1)]
self.assertTrue(('PilightDaemonSim start' in str(error_log_call)))
self.skip_teardown_stop = True
self.hass.stop()
error_log_call = mock_pilight_error.call_args_list[(-1)]
self.assertTrue(('PilightDaemonSim stop' in str(error_log_call)))
|
'Check if code receiving via pilight daemon works.'
| @patch('pilight.pilight.Client', PilightDaemonSim)
@patch('homeassistant.core._LOGGER.info')
def test_receive_code(self, mock_info):
| with assert_setup_component(4):
self.assertTrue(setup_component(self.hass, pilight.DOMAIN, {pilight.DOMAIN: {}}))
self.hass.start()
self.hass.block_till_done()
expected_message = dict({'protocol': PilightDaemonSim.test_message['protocol'], 'uuid': PilightDaemonSim.test_message['uuid']}, **PilightDaemonSim.test_message['message'])
error_log_call = mock_info.call_args_list[(-1)]
for (key, value) in expected_message.items():
self.assertTrue((str(key) in str(error_log_call)))
self.assertTrue((str(value) in str(error_log_call)))
|
'Check whitelist filter with matched data.'
| @patch('pilight.pilight.Client', PilightDaemonSim)
@patch('homeassistant.core._LOGGER.info')
def test_whitelist_exact_match(self, mock_info):
| with assert_setup_component(4):
whitelist = {'protocol': [PilightDaemonSim.test_message['protocol']], 'uuid': [PilightDaemonSim.test_message['uuid']], 'id': [PilightDaemonSim.test_message['message']['id']], 'unit': [PilightDaemonSim.test_message['message']['unit']]}
self.assertTrue(setup_component(self.hass, pilight.DOMAIN, {pilight.DOMAIN: {'whitelist': whitelist}}))
self.hass.start()
self.hass.block_till_done()
expected_message = dict({'protocol': PilightDaemonSim.test_message['protocol'], 'uuid': PilightDaemonSim.test_message['uuid']}, **PilightDaemonSim.test_message['message'])
info_log_call = mock_info.call_args_list[(-1)]
for (key, value) in expected_message.items():
self.assertTrue((str(key) in str(info_log_call)))
self.assertTrue((str(value) in str(info_log_call)))
|
'Check whitelist filter with partially matched data, should work.'
| @patch('pilight.pilight.Client', PilightDaemonSim)
@patch('homeassistant.core._LOGGER.info')
def test_whitelist_partial_match(self, mock_info):
| with assert_setup_component(4):
whitelist = {'protocol': [PilightDaemonSim.test_message['protocol']], 'id': [PilightDaemonSim.test_message['message']['id']]}
self.assertTrue(setup_component(self.hass, pilight.DOMAIN, {pilight.DOMAIN: {'whitelist': whitelist}}))
self.hass.start()
self.hass.block_till_done()
expected_message = dict({'protocol': PilightDaemonSim.test_message['protocol'], 'uuid': PilightDaemonSim.test_message['uuid']}, **PilightDaemonSim.test_message['message'])
info_log_call = mock_info.call_args_list[(-1)]
for (key, value) in expected_message.items():
self.assertTrue((str(key) in str(info_log_call)))
self.assertTrue((str(value) in str(info_log_call)))
|
'Check whitelist filter with several subsection, should work.'
| @patch('pilight.pilight.Client', PilightDaemonSim)
@patch('homeassistant.core._LOGGER.info')
def test_whitelist_or_match(self, mock_info):
| with assert_setup_component(4):
whitelist = {'protocol': [PilightDaemonSim.test_message['protocol'], 'other_protocoll'], 'id': [PilightDaemonSim.test_message['message']['id']]}
self.assertTrue(setup_component(self.hass, pilight.DOMAIN, {pilight.DOMAIN: {'whitelist': whitelist}}))
self.hass.start()
self.hass.block_till_done()
expected_message = dict({'protocol': PilightDaemonSim.test_message['protocol'], 'uuid': PilightDaemonSim.test_message['uuid']}, **PilightDaemonSim.test_message['message'])
info_log_call = mock_info.call_args_list[(-1)]
for (key, value) in expected_message.items():
self.assertTrue((str(key) in str(info_log_call)))
self.assertTrue((str(value) in str(info_log_call)))
|
'Check whitelist filter with unmatched data, should not work.'
| @patch('pilight.pilight.Client', PilightDaemonSim)
@patch('homeassistant.core._LOGGER.info')
def test_whitelist_no_match(self, mock_info):
| with assert_setup_component(4):
whitelist = {'protocol': ['wrong_protocoll'], 'id': [PilightDaemonSim.test_message['message']['id']]}
self.assertTrue(setup_component(self.hass, pilight.DOMAIN, {pilight.DOMAIN: {'whitelist': whitelist}}))
self.hass.start()
self.hass.block_till_done()
info_log_call = mock_info.call_args_list[(-1)]
self.assertFalse(('Event pilight_received' in info_log_call))
|
'Setup things to be run when tests are started.'
| def setUp(self):
| self.hass = get_test_home_assistant()
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.