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
|
|
|
|
|
2016-10-27 07:16:23 +00:00
|
|
|
from homeassistant.bootstrap import setup_component
|
2016-02-21 01:17:30 +00:00
|
|
|
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."""
|
2016-04-07 00:47:47 +00:00
|
|
|
self.hass.config.components = ['mqtt']
|
2016-10-27 07:16:23 +00:00
|
|
|
assert setup_component(self.hass, switch.DOMAIN, {
|
2016-04-07 00:47:47 +00:00
|
|
|
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
|
|
|
}
|
2016-04-07 00:47:47 +00:00
|
|
|
})
|
2015-10-03 06:57:26 +00:00
|
|
|
|
|
|
|
state = self.hass.states.get('switch.test')
|
|
|
|
self.assertEqual(STATE_OFF, state.state)
|
2016-02-21 01:17:30 +00:00
|
|
|
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')
|
2016-09-13 02:16:14 +00:00
|
|
|
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')
|
2016-09-13 02:16:14 +00:00
|
|
|
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."""
|
2016-04-07 00:47:47 +00:00
|
|
|
self.hass.config.components = ['mqtt']
|
2016-10-27 07:16:23 +00:00
|
|
|
assert setup_component(self.hass, switch.DOMAIN, {
|
2016-04-07 00:47:47 +00:00
|
|
|
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
|
|
|
}
|
2016-04-07 00:47:47 +00:00
|
|
|
})
|
2015-10-03 06:57:26 +00:00
|
|
|
|
|
|
|
state = self.hass.states.get('switch.test')
|
|
|
|
self.assertEqual(STATE_OFF, state.state)
|
2016-02-21 01:17:30 +00:00
|
|
|
self.assertTrue(state.attributes.get(ATTR_ASSUMED_STATE))
|
2015-10-03 06:57:26 +00:00
|
|
|
|
|
|
|
switch.turn_on(self.hass, 'switch.test')
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2015-10-03 06:57:26 +00:00
|
|
|
|
2015-11-26 19:55: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')
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2015-10-03 06:57:26 +00:00
|
|
|
|
2015-11-26 19:55: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."""
|
2016-04-07 00:47:47 +00:00
|
|
|
self.hass.config.components = ['mqtt']
|
2016-10-27 07:16:23 +00:00
|
|
|
assert setup_component(self.hass, switch.DOMAIN, {
|
2016-04-07 00:47:47 +00:00
|
|
|
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',
|
2015-12-11 05:39:01 +00:00
|
|
|
'value_template': '{{ value_json.val }}'
|
2015-11-20 22:50:46 +00:00
|
|
|
}
|
2016-04-07 00:47:47 +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"}')
|
2016-09-13 02:16:14 +00:00
|
|
|
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"}')
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2015-11-20 22:50:46 +00:00
|
|
|
|
|
|
|
state = self.hass.states.get('switch.test')
|
2016-02-13 13:19:11 +00:00
|
|
|
self.assertEqual(STATE_OFF, state.state)
|