Merge pull request #1703 from jaharkes/config-validation-mqtt

Config validation for MQTT
This commit is contained in:
Paulus Schoutsen 2016-04-07 08:43:01 -07:00
commit 5ff9479f0b
25 changed files with 512 additions and 299 deletions

View file

@ -1,7 +1,7 @@
"""The tests the MQTT alarm control panel component."""
import unittest
from unittest.mock import patch
from homeassistant.bootstrap import _setup_component
from homeassistant.const import (
STATE_ALARM_DISARMED, STATE_ALARM_ARMED_HOME, STATE_ALARM_ARMED_AWAY,
STATE_ALARM_PENDING, STATE_ALARM_TRIGGERED, STATE_UNKNOWN)
@ -25,37 +25,37 @@ class TestAlarmControlPanelMQTT(unittest.TestCase):
"""Stop down stuff we started."""
self.hass.stop()
@patch('homeassistant.components.alarm_control_panel.mqtt._LOGGER.error')
def test_fail_setup_without_state_topic(self, mock_error):
def test_fail_setup_without_state_topic(self):
"""Test for failing with no state topic."""
self.assertTrue(alarm_control_panel.setup(self.hass, {
'alarm_control_panel': {
self.hass.config.components = ['mqtt']
assert not _setup_component(self.hass, alarm_control_panel.DOMAIN, {
alarm_control_panel.DOMAIN: {
'platform': 'mqtt',
'command_topic': 'alarm/command'
}}))
}
})
self.assertEqual(1, mock_error.call_count)
@patch('homeassistant.components.alarm_control_panel.mqtt._LOGGER.error')
def test_fail_setup_without_command_topic(self, mock_error):
def test_fail_setup_without_command_topic(self):
"""Test failing with no command topic."""
self.assertTrue(alarm_control_panel.setup(self.hass, {
'alarm_control_panel': {
self.hass.config.components = ['mqtt']
assert not _setup_component(self.hass, alarm_control_panel.DOMAIN, {
alarm_control_panel.DOMAIN: {
'platform': 'mqtt',
'state_topic': 'alarm/state'
}}))
self.assertEqual(1, mock_error.call_count)
}
})
def test_update_state_via_state_topic(self):
"""Test updating with via state topic."""
self.assertTrue(alarm_control_panel.setup(self.hass, {
'alarm_control_panel': {
self.hass.config.components = ['mqtt']
assert _setup_component(self.hass, alarm_control_panel.DOMAIN, {
alarm_control_panel.DOMAIN: {
'platform': 'mqtt',
'name': 'test',
'state_topic': 'alarm/state',
'command_topic': 'alarm/command',
}}))
}
})
entity_id = 'alarm_control_panel.test'
@ -71,13 +71,15 @@ class TestAlarmControlPanelMQTT(unittest.TestCase):
def test_ignore_update_state_if_unknown_via_state_topic(self):
"""Test ignoring updates via state topic."""
self.assertTrue(alarm_control_panel.setup(self.hass, {
'alarm_control_panel': {
self.hass.config.components = ['mqtt']
assert _setup_component(self.hass, alarm_control_panel.DOMAIN, {
alarm_control_panel.DOMAIN: {
'platform': 'mqtt',
'name': 'test',
'state_topic': 'alarm/state',
'command_topic': 'alarm/command',
}}))
}
})
entity_id = 'alarm_control_panel.test'
@ -90,13 +92,15 @@ class TestAlarmControlPanelMQTT(unittest.TestCase):
def test_arm_home_publishes_mqtt(self):
"""Test publishing of MQTT messages while armed."""
self.assertTrue(alarm_control_panel.setup(self.hass, {
'alarm_control_panel': {
self.hass.config.components = ['mqtt']
assert _setup_component(self.hass, alarm_control_panel.DOMAIN, {
alarm_control_panel.DOMAIN: {
'platform': 'mqtt',
'name': 'test',
'state_topic': 'alarm/state',
'command_topic': 'alarm/command',
}}))
}
})
alarm_control_panel.alarm_arm_home(self.hass)
self.hass.pool.block_till_done()
@ -105,14 +109,16 @@ class TestAlarmControlPanelMQTT(unittest.TestCase):
def test_arm_home_not_publishes_mqtt_with_invalid_code(self):
"""Test not publishing of MQTT messages with invalid code."""
self.assertTrue(alarm_control_panel.setup(self.hass, {
'alarm_control_panel': {
self.hass.config.components = ['mqtt']
assert _setup_component(self.hass, alarm_control_panel.DOMAIN, {
alarm_control_panel.DOMAIN: {
'platform': 'mqtt',
'name': 'test',
'state_topic': 'alarm/state',
'command_topic': 'alarm/command',
'code': '1234'
}}))
}
})
call_count = self.mock_publish.call_count
alarm_control_panel.alarm_arm_home(self.hass, 'abcd')
@ -121,13 +127,15 @@ class TestAlarmControlPanelMQTT(unittest.TestCase):
def test_arm_away_publishes_mqtt(self):
"""Test publishing of MQTT messages while armed."""
self.assertTrue(alarm_control_panel.setup(self.hass, {
'alarm_control_panel': {
self.hass.config.components = ['mqtt']
assert _setup_component(self.hass, alarm_control_panel.DOMAIN, {
alarm_control_panel.DOMAIN: {
'platform': 'mqtt',
'name': 'test',
'state_topic': 'alarm/state',
'command_topic': 'alarm/command',
}}))
}
})
alarm_control_panel.alarm_arm_away(self.hass)
self.hass.pool.block_till_done()
@ -136,14 +144,16 @@ class TestAlarmControlPanelMQTT(unittest.TestCase):
def test_arm_away_not_publishes_mqtt_with_invalid_code(self):
"""Test not publishing of MQTT messages with invalid code."""
self.assertTrue(alarm_control_panel.setup(self.hass, {
'alarm_control_panel': {
self.hass.config.components = ['mqtt']
assert _setup_component(self.hass, alarm_control_panel.DOMAIN, {
alarm_control_panel.DOMAIN: {
'platform': 'mqtt',
'name': 'test',
'state_topic': 'alarm/state',
'command_topic': 'alarm/command',
'code': '1234'
}}))
}
})
call_count = self.mock_publish.call_count
alarm_control_panel.alarm_arm_away(self.hass, 'abcd')
@ -152,13 +162,15 @@ class TestAlarmControlPanelMQTT(unittest.TestCase):
def test_disarm_publishes_mqtt(self):
"""Test publishing of MQTT messages while disarmed."""
self.assertTrue(alarm_control_panel.setup(self.hass, {
'alarm_control_panel': {
self.hass.config.components = ['mqtt']
assert _setup_component(self.hass, alarm_control_panel.DOMAIN, {
alarm_control_panel.DOMAIN: {
'platform': 'mqtt',
'name': 'test',
'state_topic': 'alarm/state',
'command_topic': 'alarm/command',
}}))
}
})
alarm_control_panel.alarm_disarm(self.hass)
self.hass.pool.block_till_done()
@ -167,14 +179,16 @@ class TestAlarmControlPanelMQTT(unittest.TestCase):
def test_disarm_not_publishes_mqtt_with_invalid_code(self):
"""Test not publishing of MQTT messages with invalid code."""
self.assertTrue(alarm_control_panel.setup(self.hass, {
'alarm_control_panel': {
self.hass.config.components = ['mqtt']
assert _setup_component(self.hass, alarm_control_panel.DOMAIN, {
alarm_control_panel.DOMAIN: {
'platform': 'mqtt',
'name': 'test',
'state_topic': 'alarm/state',
'command_topic': 'alarm/command',
'code': '1234'
}}))
}
})
call_count = self.mock_publish.call_count
alarm_control_panel.alarm_disarm(self.hass, 'abcd')

View file

@ -1,6 +1,7 @@
"""The tests for the MQTT binary sensor platform."""
import unittest
from homeassistant.bootstrap import _setup_component
import homeassistant.components.binary_sensor as binary_sensor
from tests.common import mock_mqtt_component, fire_mqtt_message
from homeassistant.const import (STATE_OFF, STATE_ON)
@ -22,15 +23,16 @@ class TestSensorMQTT(unittest.TestCase):
def test_setting_sensor_value_via_mqtt_message(self):
"""Test the setting of the value via MQTT."""
self.assertTrue(binary_sensor.setup(self.hass, {
'binary_sensor': {
self.hass.config.components = ['mqtt']
assert _setup_component(self.hass, binary_sensor.DOMAIN, {
binary_sensor.DOMAIN: {
'platform': 'mqtt',
'name': 'test',
'state_topic': 'test-topic',
'payload_on': 'ON',
'payload_off': 'OFF',
}
}))
})
state = self.hass.states.get('binary_sensor.test')
self.assertEqual(STATE_OFF, state.state)
@ -47,28 +49,30 @@ class TestSensorMQTT(unittest.TestCase):
def test_valid_sensor_class(self):
"""Test the setting of a valid sensor class."""
self.assertTrue(binary_sensor.setup(self.hass, {
'binary_sensor': {
self.hass.config.components = ['mqtt']
assert _setup_component(self.hass, binary_sensor.DOMAIN, {
binary_sensor.DOMAIN: {
'platform': 'mqtt',
'name': 'test',
'sensor_class': 'motion',
'state_topic': 'test-topic',
}
}))
})
state = self.hass.states.get('binary_sensor.test')
self.assertEqual('motion', state.attributes.get('sensor_class'))
def test_invalid_sensor_class(self):
"""Test the setting of an invalid sensor class."""
self.assertTrue(binary_sensor.setup(self.hass, {
'binary_sensor': {
self.hass.config.components = ['mqtt']
assert _setup_component(self.hass, binary_sensor.DOMAIN, {
binary_sensor.DOMAIN: {
'platform': 'mqtt',
'name': 'test',
'sensor_class': 'abc123',
'state_topic': 'test-topic',
}
}))
})
state = self.hass.states.get('binary_sensor.test')
self.assertIsNone(state.attributes.get('sensor_class'))

View file

@ -2,6 +2,7 @@
import unittest
import os
from homeassistant.bootstrap import _setup_component
from homeassistant.components import device_tracker
from homeassistant.const import CONF_PLATFORM
@ -31,11 +32,13 @@ class TestComponentsDeviceTrackerMQTT(unittest.TestCase):
topic = '/location/paulus'
location = 'work'
self.assertTrue(device_tracker.setup(self.hass, {
self.hass.config.components = ['mqtt', 'zone']
assert _setup_component(self.hass, device_tracker.DOMAIN, {
device_tracker.DOMAIN: {
CONF_PLATFORM: 'mqtt',
'devices': {dev_id: topic}
}}))
}
})
fire_mqtt_message(self.hass, topic, location)
self.hass.pool.block_till_done()
self.assertEqual(location, self.hass.states.get(enttiy_id).state)

View file

@ -58,6 +58,7 @@ light:
"""
import unittest
from homeassistant.bootstrap import _setup_component
from homeassistant.const import STATE_ON, STATE_OFF, ATTR_ASSUMED_STATE
import homeassistant.components.light as light
from tests.common import (
@ -78,24 +79,26 @@ class TestLightMQTT(unittest.TestCase):
def test_fail_setup_if_no_command_topic(self):
"""Test if command fails with command topic."""
self.assertTrue(light.setup(self.hass, {
'light': {
self.hass.config.components = ['mqtt']
assert not _setup_component(self.hass, light.DOMAIN, {
light.DOMAIN: {
'platform': 'mqtt',
'name': 'test',
}
}))
})
self.assertIsNone(self.hass.states.get('light.test'))
def test_no_color_or_brightness_if_no_topics(self):
"""Test if there is no color and brightness if no topic."""
self.assertTrue(light.setup(self.hass, {
'light': {
self.hass.config.components = ['mqtt']
assert _setup_component(self.hass, light.DOMAIN, {
light.DOMAIN: {
'platform': 'mqtt',
'name': 'test',
'state_topic': 'test_light_rgb/status',
'command_topic': 'test_light_rgb/set',
}
}))
})
state = self.hass.states.get('light.test')
self.assertEqual(STATE_OFF, state.state)
@ -112,8 +115,9 @@ class TestLightMQTT(unittest.TestCase):
def test_controlling_state_via_topic(self):
"""Test the controlling of the state via topic."""
self.assertTrue(light.setup(self.hass, {
'light': {
self.hass.config.components = ['mqtt']
assert _setup_component(self.hass, light.DOMAIN, {
light.DOMAIN: {
'platform': 'mqtt',
'name': 'test',
'state_topic': 'test_light_rgb/status',
@ -126,7 +130,7 @@ class TestLightMQTT(unittest.TestCase):
'payload_on': 1,
'payload_off': 0
}
}))
})
state = self.hass.states.get('light.test')
self.assertEqual(STATE_OFF, state.state)
@ -172,8 +176,9 @@ class TestLightMQTT(unittest.TestCase):
def test_controlling_scale(self):
"""Test the controlling scale."""
self.assertTrue(light.setup(self.hass, {
'light': {
self.hass.config.components = ['mqtt']
assert _setup_component(self.hass, light.DOMAIN, {
light.DOMAIN: {
'platform': 'mqtt',
'name': 'test',
'state_topic': 'test_scale/status',
@ -185,7 +190,7 @@ class TestLightMQTT(unittest.TestCase):
'payload_on': 'on',
'payload_off': 'off'
}
}))
})
state = self.hass.states.get('light.test')
self.assertEqual(STATE_OFF, state.state)
@ -218,8 +223,9 @@ class TestLightMQTT(unittest.TestCase):
def test_controlling_state_via_topic_with_templates(self):
"""Test the setting og the state with a template."""
self.assertTrue(light.setup(self.hass, {
'light': {
self.hass.config.components = ['mqtt']
assert _setup_component(self.hass, light.DOMAIN, {
light.DOMAIN: {
'platform': 'mqtt',
'name': 'test',
'state_topic': 'test_light_rgb/status',
@ -230,7 +236,7 @@ class TestLightMQTT(unittest.TestCase):
'brightness_value_template': '{{ value_json.hello }}',
'rgb_value_template': '{{ value_json.hello | join(",") }}',
}
}))
})
state = self.hass.states.get('light.test')
self.assertEqual(STATE_OFF, state.state)
@ -252,8 +258,9 @@ class TestLightMQTT(unittest.TestCase):
def test_sending_mqtt_commands_and_optimistic(self):
"""Test the sending of command in optimistic mode."""
self.assertTrue(light.setup(self.hass, {
'light': {
self.hass.config.components = ['mqtt']
assert _setup_component(self.hass, light.DOMAIN, {
light.DOMAIN: {
'platform': 'mqtt',
'name': 'test',
'command_topic': 'test_light_rgb/set',
@ -263,7 +270,7 @@ class TestLightMQTT(unittest.TestCase):
'payload_on': 'on',
'payload_off': 'off'
}
}))
})
state = self.hass.states.get('light.test')
self.assertEqual(STATE_OFF, state.state)
@ -310,15 +317,16 @@ class TestLightMQTT(unittest.TestCase):
def test_show_brightness_if_only_command_topic(self):
"""Test the brightness if only a command topic is present."""
self.assertTrue(light.setup(self.hass, {
'light': {
self.hass.config.components = ['mqtt']
assert _setup_component(self.hass, light.DOMAIN, {
light.DOMAIN: {
'platform': 'mqtt',
'name': 'test',
'brightness_command_topic': 'test_light_rgb/brightness/set',
'command_topic': 'test_light_rgb/set',
'state_topic': 'test_light_rgb/status',
}
}))
})
state = self.hass.states.get('light.test')
self.assertEqual(STATE_OFF, state.state)

View file

@ -1,6 +1,7 @@
"""The tests for the MQTT lock platform."""
import unittest
from homeassistant.bootstrap import _setup_component
from homeassistant.const import (STATE_LOCKED, STATE_UNLOCKED,
ATTR_ASSUMED_STATE)
import homeassistant.components.lock as lock
@ -22,8 +23,9 @@ class TestLockMQTT(unittest.TestCase):
def test_controlling_state_via_topic(self):
"""Test the controlling state via topic."""
self.assertTrue(lock.setup(self.hass, {
'lock': {
self.hass.config.components = ['mqtt']
assert _setup_component(self.hass, lock.DOMAIN, {
lock.DOMAIN: {
'platform': 'mqtt',
'name': 'test',
'state_topic': 'state-topic',
@ -31,7 +33,7 @@ class TestLockMQTT(unittest.TestCase):
'payload_lock': 'LOCK',
'payload_unlock': 'UNLOCK'
}
}))
})
state = self.hass.states.get('lock.test')
self.assertEqual(STATE_UNLOCKED, state.state)
@ -51,8 +53,9 @@ class TestLockMQTT(unittest.TestCase):
def test_sending_mqtt_commands_and_optimistic(self):
"""Test the sending MQTT commands in optimistic mode."""
self.assertTrue(lock.setup(self.hass, {
'lock': {
self.hass.config.components = ['mqtt']
assert _setup_component(self.hass, lock.DOMAIN, {
lock.DOMAIN: {
'platform': 'mqtt',
'name': 'test',
'command_topic': 'command-topic',
@ -60,7 +63,7 @@ class TestLockMQTT(unittest.TestCase):
'payload_unlock': 'UNLOCK',
'qos': 2
}
}))
})
state = self.hass.states.get('lock.test')
self.assertEqual(STATE_UNLOCKED, state.state)
@ -84,8 +87,9 @@ class TestLockMQTT(unittest.TestCase):
def test_controlling_state_via_topic_and_json_message(self):
"""Test the controlling state via topic and JSON message."""
self.assertTrue(lock.setup(self.hass, {
'lock': {
self.hass.config.components = ['mqtt']
assert _setup_component(self.hass, lock.DOMAIN, {
lock.DOMAIN: {
'platform': 'mqtt',
'name': 'test',
'state_topic': 'state-topic',
@ -94,7 +98,7 @@ class TestLockMQTT(unittest.TestCase):
'payload_unlock': 'UNLOCK',
'value_template': '{{ value_json.val }}'
}
}))
})
state = self.hass.states.get('lock.test')
self.assertEqual(STATE_UNLOCKED, state.state)

View file

@ -4,6 +4,7 @@ import unittest
from unittest import mock
import socket
from homeassistant.bootstrap import _setup_component
import homeassistant.components.mqtt as mqtt
from homeassistant.const import (
EVENT_CALL_SERVICE, ATTR_DOMAIN, ATTR_SERVICE, EVENT_HOMEASSISTANT_START,
@ -48,9 +49,12 @@ class TestMQTT(unittest.TestCase):
"""Test for setup failure if connection to broker is missing."""
with mock.patch('homeassistant.components.mqtt.MQTT',
side_effect=socket.error()):
self.assertFalse(mqtt.setup(self.hass, {mqtt.DOMAIN: {
mqtt.CONF_BROKER: 'test-broker',
}}))
self.hass.config.components = []
assert not _setup_component(self.hass, mqtt.DOMAIN, {
mqtt.DOMAIN: {
mqtt.CONF_BROKER: 'test-broker',
}
})
def test_publish_calls_service(self):
"""Test the publishing of call to services."""
@ -211,12 +215,12 @@ class TestMQTTCallbacks(unittest.TestCase):
# mock_mqtt_component(self.hass)
with mock.patch('paho.mqtt.client.Client'):
mqtt.setup(self.hass, {
self.hass.config.components = []
assert _setup_component(self.hass, mqtt.DOMAIN, {
mqtt.DOMAIN: {
mqtt.CONF_BROKER: 'mock-broker',
}
})
self.hass.config.components.append(mqtt.DOMAIN)
def tearDown(self): # pylint: disable=invalid-name
"""Stop everything that was started."""

View file

@ -1,6 +1,7 @@
"""The tests for the MQTT component embedded server."""
from unittest.mock import MagicMock, patch
from homeassistant.bootstrap import _setup_component
import homeassistant.components.mqtt as mqtt
from tests.common import get_test_home_assistant
@ -27,15 +28,16 @@ class TestMQTT:
password = 'super_secret'
self.hass.config.api = MagicMock(api_password=password)
assert mqtt.setup(self.hass, {})
assert _setup_component(self.hass, mqtt.DOMAIN, {})
assert mock_mqtt.called
assert mock_mqtt.mock_calls[0][1][5] == 'homeassistant'
assert mock_mqtt.mock_calls[0][1][6] == password
mock_mqtt.reset_mock()
self.hass.config.components = ['http']
self.hass.config.api = MagicMock(api_password=None)
assert mqtt.setup(self.hass, {})
assert _setup_component(self.hass, mqtt.DOMAIN, {})
assert mock_mqtt.called
assert mock_mqtt.mock_calls[0][1][5] is None
assert mock_mqtt.mock_calls[0][1][6] is None
@ -50,6 +52,6 @@ class TestMQTT:
mock_gather.side_effect = BrokerException
self.hass.config.api = MagicMock(api_password=None)
assert not mqtt.setup(self.hass, {
'mqtt': {'embedded': {}}
assert not _setup_component(self.hass, mqtt.DOMAIN, {
mqtt.DOMAIN: {mqtt.CONF_EMBEDDED: {}}
})

View file

@ -1,6 +1,7 @@
"""The tests for the MQTT roller shutter platform."""
import unittest
from homeassistant.bootstrap import _setup_component
from homeassistant.const import STATE_OPEN, STATE_CLOSED, STATE_UNKNOWN
import homeassistant.components.rollershutter as rollershutter
from tests.common import mock_mqtt_component, fire_mqtt_message
@ -22,8 +23,9 @@ class TestRollershutterMQTT(unittest.TestCase):
def test_controlling_state_via_topic(self):
"""Test the controlling state via topic."""
self.assertTrue(rollershutter.setup(self.hass, {
'rollershutter': {
self.hass.config.components = ['mqtt']
assert _setup_component(self.hass, rollershutter.DOMAIN, {
rollershutter.DOMAIN: {
'platform': 'mqtt',
'name': 'test',
'state_topic': 'state-topic',
@ -33,7 +35,7 @@ class TestRollershutterMQTT(unittest.TestCase):
'payload_down': 'DOWN',
'payload_stop': 'STOP'
}
}))
})
state = self.hass.states.get('rollershutter.test')
self.assertEqual(STATE_UNKNOWN, state.state)
@ -58,15 +60,16 @@ class TestRollershutterMQTT(unittest.TestCase):
def test_send_move_up_command(self):
"""Test the sending of move_up."""
self.assertTrue(rollershutter.setup(self.hass, {
'rollershutter': {
self.hass.config.components = ['mqtt']
assert _setup_component(self.hass, rollershutter.DOMAIN, {
rollershutter.DOMAIN: {
'platform': 'mqtt',
'name': 'test',
'state_topic': 'state-topic',
'command_topic': 'command-topic',
'qos': 2
}
}))
})
state = self.hass.states.get('rollershutter.test')
self.assertEqual(STATE_UNKNOWN, state.state)
@ -81,15 +84,16 @@ class TestRollershutterMQTT(unittest.TestCase):
def test_send_move_down_command(self):
"""Test the sending of move_down."""
self.assertTrue(rollershutter.setup(self.hass, {
'rollershutter': {
self.hass.config.components = ['mqtt']
assert _setup_component(self.hass, rollershutter.DOMAIN, {
rollershutter.DOMAIN: {
'platform': 'mqtt',
'name': 'test',
'state_topic': 'state-topic',
'command_topic': 'command-topic',
'qos': 2
}
}))
})
state = self.hass.states.get('rollershutter.test')
self.assertEqual(STATE_UNKNOWN, state.state)
@ -104,15 +108,16 @@ class TestRollershutterMQTT(unittest.TestCase):
def test_send_stop_command(self):
"""Test the sending of stop."""
self.assertTrue(rollershutter.setup(self.hass, {
'rollershutter': {
self.hass.config.components = ['mqtt']
assert _setup_component(self.hass, rollershutter.DOMAIN, {
rollershutter.DOMAIN: {
'platform': 'mqtt',
'name': 'test',
'state_topic': 'state-topic',
'command_topic': 'command-topic',
'qos': 2
}
}))
})
state = self.hass.states.get('rollershutter.test')
self.assertEqual(STATE_UNKNOWN, state.state)
@ -127,8 +132,9 @@ class TestRollershutterMQTT(unittest.TestCase):
def test_state_attributes_current_position(self):
"""Test the current position."""
self.assertTrue(rollershutter.setup(self.hass, {
'rollershutter': {
self.hass.config.components = ['mqtt']
assert _setup_component(self.hass, rollershutter.DOMAIN, {
rollershutter.DOMAIN: {
'platform': 'mqtt',
'name': 'test',
'state_topic': 'state-topic',
@ -137,7 +143,7 @@ class TestRollershutterMQTT(unittest.TestCase):
'payload_down': 'DOWN',
'payload_stop': 'STOP'
}
}))
})
state_attributes_dict = self.hass.states.get(
'rollershutter.test').attributes

View file

@ -1,6 +1,7 @@
"""The tests for the MQTT sensor platform."""
import unittest
from homeassistant.bootstrap import _setup_component
import homeassistant.components.sensor as sensor
from tests.common import mock_mqtt_component, fire_mqtt_message
@ -21,14 +22,15 @@ class TestSensorMQTT(unittest.TestCase):
def test_setting_sensor_value_via_mqtt_message(self):
"""Test the setting of the value via MQTT."""
self.assertTrue(sensor.setup(self.hass, {
'sensor': {
self.hass.config.components = ['mqtt']
assert _setup_component(self.hass, sensor.DOMAIN, {
sensor.DOMAIN: {
'platform': 'mqtt',
'name': 'test',
'state_topic': 'test-topic',
'unit_of_measurement': 'fav unit'
}
}))
})
fire_mqtt_message(self.hass, 'test-topic', '100')
self.hass.pool.block_till_done()
@ -40,15 +42,16 @@ class TestSensorMQTT(unittest.TestCase):
def test_setting_sensor_value_via_mqtt_json_message(self):
"""Test the setting of the value via MQTT with JSON playload."""
self.assertTrue(sensor.setup(self.hass, {
'sensor': {
self.hass.config.components = ['mqtt']
assert _setup_component(self.hass, sensor.DOMAIN, {
sensor.DOMAIN: {
'platform': 'mqtt',
'name': 'test',
'state_topic': 'test-topic',
'unit_of_measurement': 'fav unit',
'value_template': '{{ value_json.val }}'
}
}))
})
fire_mqtt_message(self.hass, 'test-topic', '{ "val": "100" }')
self.hass.pool.block_till_done()

View file

@ -1,6 +1,7 @@
"""The tests for the MQTT switch platform."""
import unittest
from homeassistant.bootstrap import _setup_component
from homeassistant.const import STATE_ON, STATE_OFF, ATTR_ASSUMED_STATE
import homeassistant.components.switch as switch
from tests.common import (
@ -21,8 +22,9 @@ class TestSensorMQTT(unittest.TestCase):
def test_controlling_state_via_topic(self):
"""Test the controlling state via topic."""
self.assertTrue(switch.setup(self.hass, {
'switch': {
self.hass.config.components = ['mqtt']
assert _setup_component(self.hass, switch.DOMAIN, {
switch.DOMAIN: {
'platform': 'mqtt',
'name': 'test',
'state_topic': 'state-topic',
@ -30,7 +32,7 @@ class TestSensorMQTT(unittest.TestCase):
'payload_on': 1,
'payload_off': 0
}
}))
})
state = self.hass.states.get('switch.test')
self.assertEqual(STATE_OFF, state.state)
@ -50,8 +52,9 @@ class TestSensorMQTT(unittest.TestCase):
def test_sending_mqtt_commands_and_optimistic(self):
"""Test the sending MQTT commands in optimistic mode."""
self.assertTrue(switch.setup(self.hass, {
'switch': {
self.hass.config.components = ['mqtt']
assert _setup_component(self.hass, switch.DOMAIN, {
switch.DOMAIN: {
'platform': 'mqtt',
'name': 'test',
'command_topic': 'command-topic',
@ -59,7 +62,7 @@ class TestSensorMQTT(unittest.TestCase):
'payload_off': 'beer off',
'qos': '2'
}
}))
})
state = self.hass.states.get('switch.test')
self.assertEqual(STATE_OFF, state.state)
@ -83,8 +86,9 @@ class TestSensorMQTT(unittest.TestCase):
def test_controlling_state_via_topic_and_json_message(self):
"""Test the controlling state via topic and JSON message."""
self.assertTrue(switch.setup(self.hass, {
'switch': {
self.hass.config.components = ['mqtt']
assert _setup_component(self.hass, switch.DOMAIN, {
switch.DOMAIN: {
'platform': 'mqtt',
'name': 'test',
'state_topic': 'state-topic',
@ -93,7 +97,7 @@ class TestSensorMQTT(unittest.TestCase):
'payload_off': 'beer off',
'value_template': '{{ value_json.val }}'
}
}))
})
state = self.hass.states.get('switch.test')
self.assertEqual(STATE_OFF, state.state)