core/tests/components/test_conversation.py

107 lines
4.3 KiB
Python
Raw Normal View History

2016-03-09 09:25:50 +00:00
"""The tests for the Conversation component."""
# pylint: disable=too-many-public-methods,protected-access
import unittest
2015-09-01 07:18:26 +00:00
from unittest.mock import patch
import homeassistant.components as core_components
2015-09-01 07:18:26 +00:00
from homeassistant.components import conversation
from homeassistant.const import ATTR_ENTITY_ID
2015-09-01 07:18:26 +00:00
from tests.common import get_test_home_assistant
class TestConversation(unittest.TestCase):
2016-03-09 09:25:50 +00:00
"""Test the conversation component."""
def setUp(self): # pylint: disable=invalid-name
2016-03-09 09:25:50 +00:00
"""Setup things to be run when tests are started."""
2015-09-01 07:18:26 +00:00
self.ent_id = 'light.kitchen_lights'
self.hass = get_test_home_assistant(3)
2015-09-01 07:18:26 +00:00
self.hass.states.set(self.ent_id, 'on')
self.assertTrue(core_components.setup(self.hass, {}))
self.assertTrue(
conversation.setup(self.hass, {conversation.DOMAIN: {}}))
def tearDown(self): # pylint: disable=invalid-name
2016-03-09 09:25:50 +00:00
"""Stop everything that was started."""
self.hass.stop()
2015-09-01 07:18:26 +00:00
def test_turn_on(self):
2016-03-09 09:25:50 +00:00
"""Setup and perform good turn on requests."""
2015-09-01 07:18:26 +00:00
calls = []
2015-09-01 07:18:26 +00:00
def record_call(service):
calls.append(service)
self.hass.services.register('light', 'turn_on', record_call)
2015-08-31 07:54:00 +00:00
event_data = {conversation.ATTR_TEXT: 'turn kitchen lights on'}
2015-09-01 07:18:26 +00:00
self.assertTrue(self.hass.services.call(
conversation.DOMAIN, 'process', event_data, True))
2015-09-01 07:18:26 +00:00
call = calls[-1]
self.assertEqual('light', call.domain)
self.assertEqual('turn_on', call.service)
self.assertEqual([self.ent_id], call.data[ATTR_ENTITY_ID])
2015-09-01 07:18:26 +00:00
def test_turn_off(self):
2016-03-09 09:25:50 +00:00
"""Setup and perform good turn off requests."""
2015-09-01 07:18:26 +00:00
calls = []
2015-08-30 09:04:04 +00:00
2015-09-01 07:18:26 +00:00
def record_call(service):
calls.append(service)
self.hass.services.register('light', 'turn_off', record_call)
2015-08-30 09:04:04 +00:00
2015-08-31 07:54:00 +00:00
event_data = {conversation.ATTR_TEXT: 'turn kitchen lights off'}
2015-09-01 07:18:26 +00:00
self.assertTrue(self.hass.services.call(
conversation.DOMAIN, 'process', event_data, True))
2015-09-01 07:18:26 +00:00
call = calls[-1]
self.assertEqual('light', call.domain)
self.assertEqual('turn_off', call.service)
self.assertEqual([self.ent_id], call.data[ATTR_ENTITY_ID])
2015-09-01 07:18:26 +00:00
@patch('homeassistant.components.conversation.logging.Logger.error')
@patch('homeassistant.core.ServiceRegistry.call')
def test_bad_request_format(self, mock_logger, mock_call):
2016-03-09 09:25:50 +00:00
"""Setup and perform a badly formatted request."""
2015-08-31 07:54:00 +00:00
event_data = {
conversation.ATTR_TEXT:
'what is the answer to the ultimate question of life, ' +
'the universe and everything'}
self.assertTrue(self.hass.services.call(
conversation.DOMAIN, 'process', event_data, True))
2015-09-01 07:18:26 +00:00
self.assertTrue(mock_logger.called)
self.assertFalse(mock_call.called)
2015-09-01 07:18:26 +00:00
@patch('homeassistant.components.conversation.logging.Logger.error')
@patch('homeassistant.core.ServiceRegistry.call')
def test_bad_request_entity(self, mock_logger, mock_call):
2016-03-09 09:25:50 +00:00
"""Setup and perform requests with bad entity id."""
event_data = {conversation.ATTR_TEXT: 'turn something off'}
self.assertTrue(self.hass.services.call(
conversation.DOMAIN, 'process', event_data, True))
2015-09-01 07:18:26 +00:00
self.assertTrue(mock_logger.called)
self.assertFalse(mock_call.called)
2015-09-01 07:18:26 +00:00
@patch('homeassistant.components.conversation.logging.Logger.error')
@patch('homeassistant.core.ServiceRegistry.call')
def test_bad_request_command(self, mock_logger, mock_call):
2016-03-09 09:25:50 +00:00
"""Setup and perform requests with bad command."""
2015-09-01 07:18:26 +00:00
event_data = {conversation.ATTR_TEXT: 'turn kitchen lights over'}
self.assertTrue(self.hass.services.call(
conversation.DOMAIN, 'process', event_data, True))
2015-09-01 07:18:26 +00:00
self.assertTrue(mock_logger.called)
self.assertFalse(mock_call.called)
2015-08-30 08:30:19 +00:00
2015-09-01 07:18:26 +00:00
@patch('homeassistant.components.conversation.logging.Logger.error')
@patch('homeassistant.core.ServiceRegistry.call')
def test_bad_request_notext(self, mock_logger, mock_call):
2016-03-09 09:25:50 +00:00
"""Setup and perform requests with bad command with no text."""
2015-08-30 08:30:19 +00:00
event_data = {}
self.assertTrue(self.hass.services.call(
conversation.DOMAIN, 'process', event_data, True))
2015-09-01 07:18:26 +00:00
self.assertTrue(mock_logger.called)
self.assertFalse(mock_call.called)