core/tests/components/switch/test_mqtt.py

116 lines
4.1 KiB
Python
Raw Normal View History

2016-03-09 09:25:50 +00:00
"""The tests for the MQTT switch platform."""
2015-10-03 06:57:26 +00:00
import unittest
from homeassistant.bootstrap import setup_component
from homeassistant.const import STATE_ON, STATE_OFF, ATTR_ASSUMED_STATE
2015-10-03 06:57:26 +00:00
import homeassistant.components.switch as switch
2016-02-14 23:08:23 +00:00
from tests.common import (
mock_mqtt_component, fire_mqtt_message, get_test_home_assistant)
2015-10-03 06:57:26 +00:00
class TestSensorMQTT(unittest.TestCase):
2016-03-09 09:25:50 +00:00
"""Test the MQTT switch."""
2015-10-03 06:57:26 +00:00
def setUp(self): # pylint: disable=invalid-name
2016-03-09 09:25:50 +00:00
"""Setup things to be run when tests are started."""
2016-02-14 23:08:23 +00:00
self.hass = get_test_home_assistant()
2015-10-03 06:57:26 +00:00
self.mock_publish = mock_mqtt_component(self.hass)
def tearDown(self): # pylint: disable=invalid-name
2016-03-09 09:25:50 +00:00
""""Stop everything that was started."""
2015-10-03 06:57:26 +00:00
self.hass.stop()
def test_controlling_state_via_topic(self):
2016-03-09 09:25:50 +00:00
"""Test the controlling state via topic."""
self.hass.config.components = ['mqtt']
assert setup_component(self.hass, switch.DOMAIN, {
switch.DOMAIN: {
2015-10-03 06:57:26 +00:00
'platform': 'mqtt',
'name': 'test',
'state_topic': 'state-topic',
'command_topic': 'command-topic',
2016-03-11 05:42:17 +00:00
'payload_on': 1,
'payload_off': 0
2015-10-03 06:57:26 +00:00
}
})
2015-10-03 06:57:26 +00:00
state = self.hass.states.get('switch.test')
self.assertEqual(STATE_OFF, state.state)
self.assertIsNone(state.attributes.get(ATTR_ASSUMED_STATE))
2015-10-03 06:57:26 +00:00
2016-03-11 05:42:17 +00:00
fire_mqtt_message(self.hass, 'state-topic', '1')
self.hass.block_till_done()
2015-10-03 06:57:26 +00:00
state = self.hass.states.get('switch.test')
self.assertEqual(STATE_ON, state.state)
2016-03-11 05:42:17 +00:00
fire_mqtt_message(self.hass, 'state-topic', '0')
self.hass.block_till_done()
2015-10-03 06:57:26 +00:00
state = self.hass.states.get('switch.test')
self.assertEqual(STATE_OFF, state.state)
def test_sending_mqtt_commands_and_optimistic(self):
2016-03-09 09:25:50 +00:00
"""Test the sending MQTT commands in optimistic mode."""
self.hass.config.components = ['mqtt']
assert setup_component(self.hass, switch.DOMAIN, {
switch.DOMAIN: {
2015-10-03 06:57:26 +00:00
'platform': 'mqtt',
'name': 'test',
'command_topic': 'command-topic',
'payload_on': 'beer on',
'payload_off': 'beer off',
2016-03-11 05:42:17 +00:00
'qos': '2'
2015-10-03 06:57:26 +00:00
}
})
2015-10-03 06:57:26 +00:00
state = self.hass.states.get('switch.test')
self.assertEqual(STATE_OFF, state.state)
self.assertTrue(state.attributes.get(ATTR_ASSUMED_STATE))
2015-10-03 06:57:26 +00:00
switch.turn_on(self.hass, 'switch.test')
self.hass.block_till_done()
2015-10-03 06:57:26 +00:00
self.assertEqual(('command-topic', 'beer on', 2, False),
2015-10-03 06:57:26 +00:00
self.mock_publish.mock_calls[-1][1])
state = self.hass.states.get('switch.test')
self.assertEqual(STATE_ON, state.state)
switch.turn_off(self.hass, 'switch.test')
self.hass.block_till_done()
2015-10-03 06:57:26 +00:00
self.assertEqual(('command-topic', 'beer off', 2, False),
2015-10-03 06:57:26 +00:00
self.mock_publish.mock_calls[-1][1])
state = self.hass.states.get('switch.test')
self.assertEqual(STATE_OFF, state.state)
2015-11-20 22:50:46 +00:00
def test_controlling_state_via_topic_and_json_message(self):
2016-03-09 09:25:50 +00:00
"""Test the controlling state via topic and JSON message."""
self.hass.config.components = ['mqtt']
assert setup_component(self.hass, switch.DOMAIN, {
switch.DOMAIN: {
2015-11-20 22:50:46 +00:00
'platform': 'mqtt',
'name': 'test',
'state_topic': 'state-topic',
'command_topic': 'command-topic',
'payload_on': 'beer on',
'payload_off': 'beer off',
'value_template': '{{ value_json.val }}'
2015-11-20 22:50:46 +00:00
}
})
2015-11-20 22:50:46 +00:00
state = self.hass.states.get('switch.test')
self.assertEqual(STATE_OFF, state.state)
fire_mqtt_message(self.hass, 'state-topic', '{"val":"beer on"}')
self.hass.block_till_done()
2015-11-20 22:50:46 +00:00
state = self.hass.states.get('switch.test')
self.assertEqual(STATE_ON, state.state)
fire_mqtt_message(self.hass, 'state-topic', '{"val":"beer off"}')
self.hass.block_till_done()
2015-11-20 22:50:46 +00:00
state = self.hass.states.get('switch.test')
self.assertEqual(STATE_OFF, state.state)