2016-03-09 09:25:50 +00:00
|
|
|
"""The tests for the MQTT light platform.
|
2015-10-31 18:26:03 +00:00
|
|
|
|
2016-03-09 09:25:50 +00:00
|
|
|
Configuration for RGB Version with brightness:
|
2015-10-31 18:26:03 +00:00
|
|
|
|
|
|
|
light:
|
|
|
|
platform: mqtt
|
|
|
|
name: "Office Light RGB"
|
|
|
|
state_topic: "office/rgb1/light/status"
|
|
|
|
command_topic: "office/rgb1/light/switch"
|
|
|
|
brightness_state_topic: "office/rgb1/brightness/status"
|
|
|
|
brightness_command_topic: "office/rgb1/brightness/set"
|
|
|
|
rgb_state_topic: "office/rgb1/rgb/status"
|
|
|
|
rgb_command_topic: "office/rgb1/rgb/set"
|
|
|
|
qos: 0
|
|
|
|
payload_on: "on"
|
|
|
|
payload_off: "off"
|
|
|
|
|
|
|
|
config without RGB:
|
|
|
|
|
|
|
|
light:
|
|
|
|
platform: mqtt
|
|
|
|
name: "Office Light"
|
|
|
|
state_topic: "office/rgb1/light/status"
|
|
|
|
command_topic: "office/rgb1/light/switch"
|
|
|
|
brightness_state_topic: "office/rgb1/brightness/status"
|
|
|
|
brightness_command_topic: "office/rgb1/brightness/set"
|
|
|
|
qos: 0
|
|
|
|
payload_on: "on"
|
|
|
|
payload_off: "off"
|
|
|
|
|
|
|
|
config without RGB and brightness:
|
|
|
|
|
|
|
|
light:
|
|
|
|
platform: mqtt
|
|
|
|
name: "Office Light"
|
|
|
|
state_topic: "office/rgb1/light/status"
|
|
|
|
command_topic: "office/rgb1/light/switch"
|
|
|
|
qos: 0
|
|
|
|
payload_on: "on"
|
|
|
|
payload_off: "off"
|
2016-02-26 18:33:32 +00:00
|
|
|
|
|
|
|
config for RGB Version with brightness and scale:
|
|
|
|
|
|
|
|
light:
|
|
|
|
platform: mqtt
|
|
|
|
name: "Office Light RGB"
|
|
|
|
state_topic: "office/rgb1/light/status"
|
|
|
|
command_topic: "office/rgb1/light/switch"
|
|
|
|
brightness_state_topic: "office/rgb1/brightness/status"
|
|
|
|
brightness_command_topic: "office/rgb1/brightness/set"
|
|
|
|
brightness_scale: 99
|
|
|
|
rgb_state_topic: "office/rgb1/rgb/status"
|
|
|
|
rgb_command_topic: "office/rgb1/rgb/set"
|
|
|
|
rgb_scale: 99
|
|
|
|
qos: 0
|
|
|
|
payload_on: "on"
|
|
|
|
payload_off: "off"
|
2015-10-31 18:26:03 +00:00
|
|
|
"""
|
|
|
|
import unittest
|
|
|
|
|
2016-04-06 00:55:20 +00:00
|
|
|
from homeassistant.bootstrap import _setup_component
|
2016-02-21 01:17:22 +00:00
|
|
|
from homeassistant.const import STATE_ON, STATE_OFF, ATTR_ASSUMED_STATE
|
2015-10-31 18:26:03 +00:00
|
|
|
import homeassistant.components.light as light
|
2016-02-14 23:08:23 +00:00
|
|
|
from tests.common import (
|
|
|
|
get_test_home_assistant, mock_mqtt_component, fire_mqtt_message)
|
2015-10-31 18:26:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestLightMQTT(unittest.TestCase):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test the MQTT light."""
|
2015-10-31 18:26:03 +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-31 18:26:03 +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-31 18:26:03 +00:00
|
|
|
self.hass.stop()
|
|
|
|
|
2015-12-20 03:36:38 +00:00
|
|
|
def test_fail_setup_if_no_command_topic(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test if command fails with command topic."""
|
2016-04-06 00:55:20 +00:00
|
|
|
self.hass.config.components = ['mqtt']
|
|
|
|
assert not _setup_component(self.hass, light.DOMAIN, {
|
|
|
|
light.DOMAIN: {
|
2015-12-20 03:36:38 +00:00
|
|
|
'platform': 'mqtt',
|
|
|
|
'name': 'test',
|
|
|
|
}
|
2016-04-06 00:55:20 +00:00
|
|
|
})
|
2015-12-20 03:36:38 +00:00
|
|
|
self.assertIsNone(self.hass.states.get('light.test'))
|
|
|
|
|
2015-11-28 20:46:35 +00:00
|
|
|
def test_no_color_or_brightness_if_no_topics(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test if there is no color and brightness if no topic."""
|
2016-04-06 00:55:20 +00:00
|
|
|
self.hass.config.components = ['mqtt']
|
|
|
|
assert _setup_component(self.hass, light.DOMAIN, {
|
|
|
|
light.DOMAIN: {
|
2015-11-28 20:46:35 +00:00
|
|
|
'platform': 'mqtt',
|
|
|
|
'name': 'test',
|
|
|
|
'state_topic': 'test_light_rgb/status',
|
|
|
|
'command_topic': 'test_light_rgb/set',
|
|
|
|
}
|
2016-04-06 00:55:20 +00:00
|
|
|
})
|
2015-11-28 20:46:35 +00:00
|
|
|
|
|
|
|
state = self.hass.states.get('light.test')
|
|
|
|
self.assertEqual(STATE_OFF, state.state)
|
|
|
|
self.assertIsNone(state.attributes.get('rgb_color'))
|
|
|
|
self.assertIsNone(state.attributes.get('brightness'))
|
|
|
|
|
2015-12-20 03:15:05 +00:00
|
|
|
fire_mqtt_message(self.hass, 'test_light_rgb/status', 'ON')
|
2015-11-28 20:46:35 +00:00
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
|
|
|
|
state = self.hass.states.get('light.test')
|
|
|
|
self.assertEqual(STATE_ON, state.state)
|
|
|
|
self.assertIsNone(state.attributes.get('rgb_color'))
|
|
|
|
self.assertIsNone(state.attributes.get('brightness'))
|
|
|
|
|
2015-10-31 18:26:03 +00:00
|
|
|
def test_controlling_state_via_topic(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test the controlling of the state via topic."""
|
2016-04-06 00:55:20 +00:00
|
|
|
self.hass.config.components = ['mqtt']
|
|
|
|
assert _setup_component(self.hass, light.DOMAIN, {
|
|
|
|
light.DOMAIN: {
|
2015-10-31 18:26:03 +00:00
|
|
|
'platform': 'mqtt',
|
|
|
|
'name': 'test',
|
|
|
|
'state_topic': 'test_light_rgb/status',
|
|
|
|
'command_topic': 'test_light_rgb/set',
|
|
|
|
'brightness_state_topic': 'test_light_rgb/brightness/status',
|
|
|
|
'brightness_command_topic': 'test_light_rgb/brightness/set',
|
|
|
|
'rgb_state_topic': 'test_light_rgb/rgb/status',
|
|
|
|
'rgb_command_topic': 'test_light_rgb/rgb/set',
|
2016-03-11 05:42:17 +00:00
|
|
|
'qos': '0',
|
|
|
|
'payload_on': 1,
|
|
|
|
'payload_off': 0
|
2015-10-31 18:26:03 +00:00
|
|
|
}
|
2016-04-06 00:55:20 +00:00
|
|
|
})
|
2015-10-31 18:26:03 +00:00
|
|
|
|
|
|
|
state = self.hass.states.get('light.test')
|
|
|
|
self.assertEqual(STATE_OFF, state.state)
|
2015-11-28 20:46:35 +00:00
|
|
|
self.assertIsNone(state.attributes.get('rgb_color'))
|
|
|
|
self.assertIsNone(state.attributes.get('brightness'))
|
2016-02-21 01:17:22 +00:00
|
|
|
self.assertIsNone(state.attributes.get(ATTR_ASSUMED_STATE))
|
2015-10-31 18:26:03 +00:00
|
|
|
|
2016-03-11 05:42:17 +00:00
|
|
|
fire_mqtt_message(self.hass, 'test_light_rgb/status', '1')
|
2015-10-31 18:26:03 +00:00
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
|
|
|
|
state = self.hass.states.get('light.test')
|
|
|
|
self.assertEqual(STATE_ON, state.state)
|
2015-11-28 20:46:35 +00:00
|
|
|
self.assertEqual([255, 255, 255], state.attributes.get('rgb_color'))
|
|
|
|
self.assertEqual(255, state.attributes.get('brightness'))
|
2015-10-31 18:26:03 +00:00
|
|
|
|
2016-03-11 05:42:17 +00:00
|
|
|
fire_mqtt_message(self.hass, 'test_light_rgb/status', '0')
|
2015-10-31 18:26:03 +00:00
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
|
|
|
|
state = self.hass.states.get('light.test')
|
|
|
|
self.assertEqual(STATE_OFF, state.state)
|
2015-11-13 07:03:56 +00:00
|
|
|
|
2016-03-11 05:42:17 +00:00
|
|
|
fire_mqtt_message(self.hass, 'test_light_rgb/status', '1')
|
2015-11-11 11:32:24 +00:00
|
|
|
self.hass.pool.block_till_done()
|
2015-11-02 19:16:36 +00:00
|
|
|
|
|
|
|
fire_mqtt_message(self.hass, 'test_light_rgb/brightness/status', '100')
|
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
|
|
|
|
light_state = self.hass.states.get('light.test')
|
2015-11-11 19:40:21 +00:00
|
|
|
self.hass.pool.block_till_done()
|
2015-11-02 19:16:36 +00:00
|
|
|
self.assertEqual(100,
|
2015-11-11 11:32:24 +00:00
|
|
|
light_state.attributes['brightness'])
|
2015-11-11 11:38:10 +00:00
|
|
|
|
2016-03-11 05:42:17 +00:00
|
|
|
fire_mqtt_message(self.hass, 'test_light_rgb/status', '1')
|
2015-11-11 19:52:41 +00:00
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
|
2015-11-13 07:03:56 +00:00
|
|
|
fire_mqtt_message(self.hass, 'test_light_rgb/rgb/status',
|
|
|
|
'125,125,125')
|
2015-11-11 11:32:24 +00:00
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
|
|
|
|
light_state = self.hass.states.get('light.test')
|
2015-11-13 07:08:26 +00:00
|
|
|
self.assertEqual([125, 125, 125],
|
|
|
|
light_state.attributes.get('rgb_color'))
|
2015-11-13 07:03:56 +00:00
|
|
|
|
2016-02-26 18:33:32 +00:00
|
|
|
def test_controlling_scale(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test the controlling scale."""
|
2016-04-06 00:55:20 +00:00
|
|
|
self.hass.config.components = ['mqtt']
|
|
|
|
assert _setup_component(self.hass, light.DOMAIN, {
|
|
|
|
light.DOMAIN: {
|
2016-02-26 18:33:32 +00:00
|
|
|
'platform': 'mqtt',
|
|
|
|
'name': 'test',
|
|
|
|
'state_topic': 'test_scale/status',
|
|
|
|
'command_topic': 'test_scale/set',
|
|
|
|
'brightness_state_topic': 'test_scale/brightness/status',
|
|
|
|
'brightness_command_topic': 'test_scale/brightness/set',
|
|
|
|
'brightness_scale': '99',
|
|
|
|
'qos': 0,
|
|
|
|
'payload_on': 'on',
|
|
|
|
'payload_off': 'off'
|
|
|
|
}
|
2016-04-06 00:55:20 +00:00
|
|
|
})
|
2016-02-26 18:33:32 +00:00
|
|
|
|
|
|
|
state = self.hass.states.get('light.test')
|
|
|
|
self.assertEqual(STATE_OFF, state.state)
|
|
|
|
self.assertIsNone(state.attributes.get('brightness'))
|
|
|
|
self.assertIsNone(state.attributes.get(ATTR_ASSUMED_STATE))
|
|
|
|
|
|
|
|
fire_mqtt_message(self.hass, 'test_scale/status', 'on')
|
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
|
|
|
|
state = self.hass.states.get('light.test')
|
|
|
|
self.assertEqual(STATE_ON, state.state)
|
|
|
|
self.assertEqual(255, state.attributes.get('brightness'))
|
|
|
|
|
|
|
|
fire_mqtt_message(self.hass, 'test_scale/status', 'off')
|
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
|
|
|
|
state = self.hass.states.get('light.test')
|
|
|
|
self.assertEqual(STATE_OFF, state.state)
|
|
|
|
|
|
|
|
fire_mqtt_message(self.hass, 'test_scale/status', 'on')
|
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
|
|
|
|
fire_mqtt_message(self.hass, 'test_scale/brightness/status', '99')
|
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
|
|
|
|
light_state = self.hass.states.get('light.test')
|
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
self.assertEqual(255,
|
|
|
|
light_state.attributes['brightness'])
|
|
|
|
|
2015-12-20 03:15:05 +00:00
|
|
|
def test_controlling_state_via_topic_with_templates(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test the setting og the state with a template."""
|
2016-04-06 00:55:20 +00:00
|
|
|
self.hass.config.components = ['mqtt']
|
|
|
|
assert _setup_component(self.hass, light.DOMAIN, {
|
|
|
|
light.DOMAIN: {
|
2015-12-20 03:15:05 +00:00
|
|
|
'platform': 'mqtt',
|
|
|
|
'name': 'test',
|
|
|
|
'state_topic': 'test_light_rgb/status',
|
|
|
|
'command_topic': 'test_light_rgb/set',
|
|
|
|
'brightness_state_topic': 'test_light_rgb/brightness/status',
|
|
|
|
'rgb_state_topic': 'test_light_rgb/rgb/status',
|
|
|
|
'state_value_template': '{{ value_json.hello }}',
|
|
|
|
'brightness_value_template': '{{ value_json.hello }}',
|
|
|
|
'rgb_value_template': '{{ value_json.hello | join(",") }}',
|
|
|
|
}
|
2016-04-06 00:55:20 +00:00
|
|
|
})
|
2015-12-20 03:15:05 +00:00
|
|
|
|
|
|
|
state = self.hass.states.get('light.test')
|
|
|
|
self.assertEqual(STATE_OFF, state.state)
|
|
|
|
self.assertIsNone(state.attributes.get('brightness'))
|
|
|
|
self.assertIsNone(state.attributes.get('rgb_color'))
|
|
|
|
|
2016-02-15 07:16:54 +00:00
|
|
|
fire_mqtt_message(self.hass, 'test_light_rgb/rgb/status',
|
|
|
|
'{"hello": [1, 2, 3]}')
|
2015-12-20 03:15:05 +00:00
|
|
|
fire_mqtt_message(self.hass, 'test_light_rgb/status',
|
|
|
|
'{"hello": "ON"}')
|
|
|
|
fire_mqtt_message(self.hass, 'test_light_rgb/brightness/status',
|
|
|
|
'{"hello": "50"}')
|
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
|
|
|
|
state = self.hass.states.get('light.test')
|
|
|
|
self.assertEqual(STATE_ON, state.state)
|
|
|
|
self.assertEqual(50, state.attributes.get('brightness'))
|
|
|
|
self.assertEqual([1, 2, 3], state.attributes.get('rgb_color'))
|
|
|
|
|
2015-10-31 18:26:03 +00:00
|
|
|
def test_sending_mqtt_commands_and_optimistic(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test the sending of command in optimistic mode."""
|
2016-04-06 00:55:20 +00:00
|
|
|
self.hass.config.components = ['mqtt']
|
|
|
|
assert _setup_component(self.hass, light.DOMAIN, {
|
|
|
|
light.DOMAIN: {
|
2015-10-31 18:26:03 +00:00
|
|
|
'platform': 'mqtt',
|
|
|
|
'name': 'test',
|
|
|
|
'command_topic': 'test_light_rgb/set',
|
|
|
|
'brightness_command_topic': 'test_light_rgb/brightness/set',
|
|
|
|
'rgb_command_topic': 'test_light_rgb/rgb/set',
|
|
|
|
'qos': 2,
|
|
|
|
'payload_on': 'on',
|
|
|
|
'payload_off': 'off'
|
|
|
|
}
|
2016-04-06 00:55:20 +00:00
|
|
|
})
|
2015-10-31 18:26:03 +00:00
|
|
|
|
|
|
|
state = self.hass.states.get('light.test')
|
|
|
|
self.assertEqual(STATE_OFF, state.state)
|
2016-02-21 01:17:22 +00:00
|
|
|
self.assertTrue(state.attributes.get(ATTR_ASSUMED_STATE))
|
2015-10-31 18:26:03 +00:00
|
|
|
|
2015-11-02 16:02:34 +00:00
|
|
|
light.turn_on(self.hass, 'light.test')
|
2015-10-31 18:26:03 +00:00
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
|
2015-11-26 19:55:26 +00:00
|
|
|
self.assertEqual(('test_light_rgb/set', 'on', 2, False),
|
2015-10-31 18:26:03 +00:00
|
|
|
self.mock_publish.mock_calls[-1][1])
|
|
|
|
state = self.hass.states.get('light.test')
|
|
|
|
self.assertEqual(STATE_ON, state.state)
|
|
|
|
|
2015-11-02 16:02:34 +00:00
|
|
|
light.turn_off(self.hass, 'light.test')
|
2015-10-31 18:26:03 +00:00
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
|
2015-11-26 19:55:26 +00:00
|
|
|
self.assertEqual(('test_light_rgb/set', 'off', 2, False),
|
2015-10-31 18:26:03 +00:00
|
|
|
self.mock_publish.mock_calls[-1][1])
|
|
|
|
state = self.hass.states.get('light.test')
|
|
|
|
self.assertEqual(STATE_OFF, state.state)
|
2015-11-30 07:23:27 +00:00
|
|
|
|
|
|
|
light.turn_on(self.hass, 'light.test', rgb_color=[75, 75, 75],
|
|
|
|
brightness=50)
|
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
|
|
|
|
# Calls are threaded so we need to reorder them
|
|
|
|
bright_call, rgb_call, state_call = \
|
|
|
|
sorted((call[1] for call in self.mock_publish.mock_calls[-3:]),
|
|
|
|
key=lambda call: call[0])
|
|
|
|
|
|
|
|
self.assertEqual(('test_light_rgb/set', 'on', 2, False),
|
|
|
|
state_call)
|
|
|
|
|
|
|
|
self.assertEqual(('test_light_rgb/rgb/set', '75,75,75', 2, False),
|
|
|
|
rgb_call)
|
|
|
|
|
|
|
|
self.assertEqual(('test_light_rgb/brightness/set', 50, 2, False),
|
|
|
|
bright_call)
|
|
|
|
|
|
|
|
state = self.hass.states.get('light.test')
|
|
|
|
self.assertEqual(STATE_ON, state.state)
|
2016-04-01 02:59:18 +00:00
|
|
|
self.assertEqual((75, 75, 75), state.attributes['rgb_color'])
|
2015-11-30 07:23:27 +00:00
|
|
|
self.assertEqual(50, state.attributes['brightness'])
|
2016-03-07 06:02:35 +00:00
|
|
|
|
|
|
|
def test_show_brightness_if_only_command_topic(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test the brightness if only a command topic is present."""
|
2016-04-06 00:55:20 +00:00
|
|
|
self.hass.config.components = ['mqtt']
|
|
|
|
assert _setup_component(self.hass, light.DOMAIN, {
|
|
|
|
light.DOMAIN: {
|
2016-03-07 06:02:35 +00:00
|
|
|
'platform': 'mqtt',
|
|
|
|
'name': 'test',
|
|
|
|
'brightness_command_topic': 'test_light_rgb/brightness/set',
|
|
|
|
'command_topic': 'test_light_rgb/set',
|
|
|
|
'state_topic': 'test_light_rgb/status',
|
|
|
|
}
|
2016-04-06 00:55:20 +00:00
|
|
|
})
|
2016-03-07 06:02:35 +00:00
|
|
|
|
|
|
|
state = self.hass.states.get('light.test')
|
|
|
|
self.assertEqual(STATE_OFF, state.state)
|
|
|
|
self.assertIsNone(state.attributes.get('brightness'))
|
|
|
|
|
|
|
|
fire_mqtt_message(self.hass, 'test_light_rgb/status', 'ON')
|
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
|
|
|
|
state = self.hass.states.get('light.test')
|
|
|
|
self.assertEqual(STATE_ON, state.state)
|
|
|
|
self.assertEqual(255, state.attributes.get('brightness'))
|