Increase test coverage MQTT

This commit is contained in:
Paulus Schoutsen 2015-08-10 23:34:58 -07:00
parent 65a4b3c9f8
commit 3fad4d8cda
3 changed files with 45 additions and 8 deletions

View file

@ -85,3 +85,6 @@ exclude_lines =
# Don't complain if tests don't hit defensive assertion code:
raise AssertionError
raise NotImplementedError
# The MQTT client is mocked
class MQTT

View file

@ -628,12 +628,12 @@ class ServiceRegistry(object):
def _execute_service(self, service_and_call):
""" Executes a service and fires a SERVICE_EXECUTED event. """
service, call = service_and_call
service(call)
self._bus.fire(
EVENT_SERVICE_EXECUTED,
{ATTR_SERVICE_CALL_ID: call.data[ATTR_SERVICE_CALL_ID]})
if ATTR_SERVICE_CALL_ID in call.data:
self._bus.fire(
EVENT_SERVICE_EXECUTED,
{ATTR_SERVICE_CALL_ID: call.data[ATTR_SERVICE_CALL_ID]})
def _generate_unique_id(self):
""" Generates a unique service call id. """

View file

@ -5,19 +5,23 @@ tests.test_component_mqtt
Tests MQTT component.
"""
import unittest
from unittest import mock
import socket
import homeassistant as ha
import homeassistant.components.mqtt as mqtt
from homeassistant.const import EVENT_CALL_SERVICE
from homeassistant.const import (
EVENT_CALL_SERVICE, ATTR_DOMAIN, ATTR_SERVICE, EVENT_HOMEASSISTANT_START,
EVENT_HOMEASSISTANT_STOP)
from tests.common import mock_mqtt_component, fire_mqtt_message
from tests.common import (
get_test_home_assistant, mock_mqtt_component, fire_mqtt_message)
class TestDemo(unittest.TestCase):
""" Test the demo module. """
def setUp(self): # pylint: disable=invalid-name
self.hass = ha.HomeAssistant()
self.hass = get_test_home_assistant(1)
mock_mqtt_component(self.hass)
self.calls = []
@ -28,6 +32,28 @@ class TestDemo(unittest.TestCase):
def record_calls(self, *args):
self.calls.append(args)
def test_client_starts_on_home_assistant_start(self):
self.hass.bus.fire(EVENT_HOMEASSISTANT_START)
self.hass.pool.block_till_done()
self.assertTrue(mqtt.MQTT_CLIENT.start.called)
def test_client_stops_on_home_assistant_start(self):
self.hass.bus.fire(EVENT_HOMEASSISTANT_START)
self.hass.pool.block_till_done()
self.hass.bus.fire(EVENT_HOMEASSISTANT_STOP)
self.hass.pool.block_till_done()
self.assertTrue(mqtt.MQTT_CLIENT.stop.called)
def test_setup_fails_if_no_broker_config(self):
self.assertFalse(mqtt.setup(self.hass, {mqtt.DOMAIN: {}}))
def test_setup_fails_if_no_connect_broker(self):
with mock.patch('homeassistant.components.mqtt.MQTT',
side_effect=socket.error()):
self.assertFalse(mqtt.setup(self.hass, {mqtt.DOMAIN: {
mqtt.CONF_BROKER: 'test-broker',
}}))
def test_publish_calls_service(self):
self.hass.bus.listen_once(EVENT_CALL_SERVICE, self.record_calls)
@ -39,6 +65,14 @@ class TestDemo(unittest.TestCase):
self.assertEqual('test-topic', self.calls[0][0].data[mqtt.ATTR_TOPIC])
self.assertEqual('test-payload', self.calls[0][0].data[mqtt.ATTR_PAYLOAD])
def test_service_call_without_topic_does_not_publush(self):
self.hass.bus.fire(EVENT_CALL_SERVICE, {
ATTR_DOMAIN: mqtt.DOMAIN,
ATTR_SERVICE: mqtt.SERVICE_PUBLISH
})
self.hass.pool.block_till_done()
self.assertTrue(not mqtt.MQTT_CLIENT.publish.called)
def test_subscribe_topic(self):
mqtt.subscribe(self.hass, 'test-topic', self.record_calls)