Expand MQTT lights (#6481)

* Add effect support to MQTT Light

* Use effect state topic for supported_features

* Dont use rainbow as default color

* Add color_temp support to MQTT JSON Light

* Add effect to MQTT JSON light

* Support lights in MQTT discovery

* Allow discovered devices to set their platform

* Add white value support to MQTT Light

* Add white value support to MQTT JSON Light

* Remove blank line

* Add color_temp support to MQTT Template light

* Add white value support to MQTT Template Light

* Remove unused SUPPORT_MQTT_TEMPLATE and stale unused flash and transition code from MQTT Template

* Add XY Color to MQTT Light Platform

* Fix syntax

* Fix more syntax errors

* Revert "Remove unused SUPPORT_MQTT_TEMPLATE and stale unused flash and transition code from MQTT Template"

This reverts commit c03798cb63.

* MQTT Template supports flash and transition but doesnt allow templating of the values

* Add XY color support to MQTT JSON

* Proper variable names

* Only allow whitelisted MQTT platforms to be loaded via MQTT Discovery

* Minor tweaks.
This commit is contained in:
Robbie Trencheny
2017-03-07 23:01:36 -08:00
committed by Paulus Schoutsen
parent c937a7bcb0
commit 2b97449d98
8 changed files with 945 additions and 136 deletions
+239 -8
View File
@@ -15,6 +15,21 @@ light:
payload_on: "on"
payload_off: "off"
Configuration for XY Version with brightness:
light:
platform: mqtt
name: "Office Light XY"
state_topic: "office/xy1/light/status"
command_topic: "office/xy1/light/switch"
brightness_state_topic: "office/xy1/brightness/status"
brightness_command_topic: "office/xy1/brightness/set"
xy_state_topic: "office/xy1/xy/status"
xy_command_topic: "office/xy1/xy/set"
qos: 0
payload_on: "on"
payload_off: "off"
config without RGB:
light:
@@ -72,6 +87,42 @@ light:
payload_on: "on"
payload_off: "off"
config with brightness and effect
light:
platform: mqtt
name: "Office Light Color Temp"
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
effect_state_topic: "office/rgb1/effect/status"
effect_command_topic: "office/rgb1/effect/set"
effect_list:
- rainbow
- colorloop
qos: 0
payload_on: "on"
payload_off: "off"
config for RGB Version with white value and scale:
light:
platform: mqtt
name: "Office Light RGB"
state_topic: "office/rgb1/light/status"
command_topic: "office/rgb1/light/switch"
white_value_state_topic: "office/rgb1/white_value/status"
white_value_command_topic: "office/rgb1/white_value/set"
white_value_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"
"""
import unittest
from unittest import mock
@@ -109,7 +160,7 @@ class TestLightMQTT(unittest.TestCase):
})
self.assertIsNone(self.hass.states.get('light.test'))
def test_no_color_or_brightness_or_color_temp_if_no_topics(self): \
def test_no_color_brightness_color_temp_white_xy_if_no_topics(self): \
# pylint: disable=invalid-name
"""Test if there is no color and brightness if no topic."""
with assert_setup_component(1):
@@ -127,6 +178,8 @@ class TestLightMQTT(unittest.TestCase):
self.assertIsNone(state.attributes.get('rgb_color'))
self.assertIsNone(state.attributes.get('brightness'))
self.assertIsNone(state.attributes.get('color_temp'))
self.assertIsNone(state.attributes.get('white_value'))
self.assertIsNone(state.attributes.get('xy_color'))
fire_mqtt_message(self.hass, 'test_light_rgb/status', 'ON')
self.hass.block_till_done()
@@ -136,6 +189,8 @@ class TestLightMQTT(unittest.TestCase):
self.assertIsNone(state.attributes.get('rgb_color'))
self.assertIsNone(state.attributes.get('brightness'))
self.assertIsNone(state.attributes.get('color_temp'))
self.assertIsNone(state.attributes.get('white_value'))
self.assertIsNone(state.attributes.get('xy_color'))
def test_controlling_state_via_topic(self): \
# pylint: disable=invalid-name
@@ -151,6 +206,12 @@ class TestLightMQTT(unittest.TestCase):
'rgb_command_topic': 'test_light_rgb/rgb/set',
'color_temp_state_topic': 'test_light_rgb/color_temp/status',
'color_temp_command_topic': 'test_light_rgb/color_temp/set',
'effect_state_topic': 'test_light_rgb/effect/status',
'effect_command_topic': 'test_light_rgb/effect/set',
'white_value_state_topic': 'test_light_rgb/white_value/status',
'white_value_command_topic': 'test_light_rgb/white_value/set',
'xy_state_topic': 'test_light_rgb/xy/status',
'xy_command_topic': 'test_light_rgb/xy/set',
'qos': '0',
'payload_on': 1,
'payload_off': 0
@@ -164,6 +225,9 @@ class TestLightMQTT(unittest.TestCase):
self.assertIsNone(state.attributes.get('rgb_color'))
self.assertIsNone(state.attributes.get('brightness'))
self.assertIsNone(state.attributes.get('color_temp'))
self.assertIsNone(state.attributes.get('effect'))
self.assertIsNone(state.attributes.get('white_value'))
self.assertIsNone(state.attributes.get('xy_color'))
self.assertIsNone(state.attributes.get(ATTR_ASSUMED_STATE))
fire_mqtt_message(self.hass, 'test_light_rgb/status', '1')
@@ -174,6 +238,9 @@ class TestLightMQTT(unittest.TestCase):
self.assertEqual([255, 255, 255], state.attributes.get('rgb_color'))
self.assertEqual(255, state.attributes.get('brightness'))
self.assertEqual(150, state.attributes.get('color_temp'))
self.assertEqual('none', state.attributes.get('effect'))
self.assertEqual(255, state.attributes.get('white_value'))
self.assertEqual([1, 1], state.attributes.get('xy_color'))
fire_mqtt_message(self.hass, 'test_light_rgb/status', '0')
self.hass.block_till_done()
@@ -198,6 +265,21 @@ class TestLightMQTT(unittest.TestCase):
self.hass.block_till_done()
self.assertEqual(300, light_state.attributes['color_temp'])
fire_mqtt_message(self.hass, 'test_light_rgb/effect/status', 'rainbow')
self.hass.block_till_done()
light_state = self.hass.states.get('light.test')
self.hass.block_till_done()
self.assertEqual('rainbow', light_state.attributes['effect'])
fire_mqtt_message(self.hass, 'test_light_rgb/white_value/status',
'100')
self.hass.block_till_done()
light_state = self.hass.states.get('light.test')
self.hass.block_till_done()
self.assertEqual(100,
light_state.attributes['white_value'])
fire_mqtt_message(self.hass, 'test_light_rgb/status', '1')
self.hass.block_till_done()
@@ -209,8 +291,16 @@ class TestLightMQTT(unittest.TestCase):
self.assertEqual([125, 125, 125],
light_state.attributes.get('rgb_color'))
def test_controlling_scale(self):
"""Test the controlling scale."""
fire_mqtt_message(self.hass, 'test_light_rgb/xy/status',
'0.675,0.322')
self.hass.block_till_done()
light_state = self.hass.states.get('light.test')
self.assertEqual([0.675, 0.322],
light_state.attributes.get('xy_color'))
def test_brightness_controlling_scale(self):
"""Test the brightness controlling scale."""
with assert_setup_component(1):
assert setup_component(self.hass, light.DOMAIN, {
light.DOMAIN: {
@@ -256,6 +346,53 @@ class TestLightMQTT(unittest.TestCase):
self.assertEqual(255,
light_state.attributes['brightness'])
def test_white_value_controlling_scale(self):
"""Test the white_value controlling scale."""
with assert_setup_component(1):
assert setup_component(self.hass, light.DOMAIN, {
light.DOMAIN: {
'platform': 'mqtt',
'name': 'test',
'state_topic': 'test_scale/status',
'command_topic': 'test_scale/set',
'white_value_state_topic': 'test_scale/white_value/status',
'white_value_command_topic': 'test_scale/white_value/set',
'white_value_scale': '99',
'qos': 0,
'payload_on': 'on',
'payload_off': 'off'
}
})
state = self.hass.states.get('light.test')
self.assertEqual(STATE_OFF, state.state)
self.assertIsNone(state.attributes.get('white_value'))
self.assertIsNone(state.attributes.get(ATTR_ASSUMED_STATE))
fire_mqtt_message(self.hass, 'test_scale/status', 'on')
self.hass.block_till_done()
state = self.hass.states.get('light.test')
self.assertEqual(STATE_ON, state.state)
self.assertEqual(255, state.attributes.get('white_value'))
fire_mqtt_message(self.hass, 'test_scale/status', 'off')
self.hass.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.block_till_done()
fire_mqtt_message(self.hass, 'test_scale/white_value/status', '99')
self.hass.block_till_done()
light_state = self.hass.states.get('light.test')
self.hass.block_till_done()
self.assertEqual(255,
light_state.attributes['white_value'])
def test_controlling_state_via_topic_with_templates(self): \
# pylint: disable=invalid-name
"""Test the setting og the state with a template."""
@@ -266,11 +403,17 @@ class TestLightMQTT(unittest.TestCase):
'command_topic': 'test_light_rgb/set',
'brightness_state_topic': 'test_light_rgb/brightness/status',
'color_temp_state_topic': 'test_light_rgb/color_temp/status',
'effect_state_topic': 'test_light_rgb/effect/status',
'rgb_state_topic': 'test_light_rgb/rgb/status',
'white_value_state_topic': 'test_light_rgb/white_value/status',
'xy_state_topic': 'test_light_rgb/xy/status',
'state_value_template': '{{ value_json.hello }}',
'brightness_value_template': '{{ value_json.hello }}',
'color_temp_value_template': '{{ value_json.hello }}',
'effect_value_template': '{{ value_json.hello }}',
'rgb_value_template': '{{ value_json.hello | join(",") }}',
'white_value_template': '{{ value_json.hello }}',
'xy_value_template': '{{ value_json.hello | join(",") }}',
}}
with assert_setup_component(1):
@@ -289,6 +432,12 @@ class TestLightMQTT(unittest.TestCase):
'{"hello": "50"}')
fire_mqtt_message(self.hass, 'test_light_rgb/color_temp/status',
'{"hello": "300"}')
fire_mqtt_message(self.hass, 'test_light_rgb/effect/status',
'{"hello": "rainbow"}')
fire_mqtt_message(self.hass, 'test_light_rgb/white_value/status',
'{"hello": "75"}')
fire_mqtt_message(self.hass, 'test_light_rgb/xy/status',
'{"hello": [0.123,0.123]}')
self.hass.block_till_done()
state = self.hass.states.get('light.test')
@@ -296,6 +445,9 @@ class TestLightMQTT(unittest.TestCase):
self.assertEqual(50, state.attributes.get('brightness'))
self.assertEqual([1, 2, 3], state.attributes.get('rgb_color'))
self.assertEqual(300, state.attributes.get('color_temp'))
self.assertEqual('rainbow', state.attributes.get('effect'))
self.assertEqual(75, state.attributes.get('white_value'))
self.assertEqual([0.123, 0.123], state.attributes.get('xy_color'))
def test_sending_mqtt_commands_and_optimistic(self): \
# pylint: disable=invalid-name
@@ -307,6 +459,9 @@ class TestLightMQTT(unittest.TestCase):
'brightness_command_topic': 'test_light_rgb/brightness/set',
'rgb_command_topic': 'test_light_rgb/rgb/set',
'color_temp_command_topic': 'test_light_rgb/color_temp/set',
'effect_command_topic': 'test_light_rgb/effect/set',
'white_value_command_topic': 'test_light_rgb/white_value/set',
'xy_command_topic': 'test_light_rgb/xy/set',
'qos': 2,
'payload_on': 'on',
'payload_off': 'off'
@@ -337,19 +492,23 @@ class TestLightMQTT(unittest.TestCase):
self.mock_publish.reset_mock()
light.turn_on(self.hass, 'light.test', rgb_color=[75, 75, 75],
brightness=50)
brightness=50, white_value=80, xy_color=[0.123, 0.123])
self.hass.block_till_done()
self.mock_publish().async_publish.assert_has_calls([
mock.call('test_light_rgb/set', 'on', 2, False),
mock.call('test_light_rgb/rgb/set', '75,75,75', 2, False),
mock.call('test_light_rgb/brightness/set', 50, 2, False),
], any_order=True)
mock.call('test_light_rgb/set', 'on', 2, False),
mock.call('test_light_rgb/rgb/set', '75,75,75', 2, False),
mock.call('test_light_rgb/brightness/set', 50, 2, False),
mock.call('test_light_rgb/white_value/set', 80, 2, False),
mock.call('test_light_rgb/xy/set', '0.123,0.123', 2, False),
], any_order=True)
state = self.hass.states.get('light.test')
self.assertEqual(STATE_ON, state.state)
self.assertEqual((75, 75, 75), state.attributes['rgb_color'])
self.assertEqual(50, state.attributes['brightness'])
self.assertEqual(80, state.attributes['white_value'])
self.assertEqual((0.123, 0.123), state.attributes['xy_color'])
def test_show_brightness_if_only_command_topic(self):
"""Test the brightness if only a command topic is present."""
@@ -398,3 +557,75 @@ class TestLightMQTT(unittest.TestCase):
state = self.hass.states.get('light.test')
self.assertEqual(STATE_ON, state.state)
self.assertEqual(150, state.attributes.get('color_temp'))
def test_show_effect_only_if_command_topic(self):
"""Test the color temp only if a command topic is present."""
config = {light.DOMAIN: {
'platform': 'mqtt',
'name': 'test',
'effect_command_topic': 'test_light_rgb/effect/set',
'command_topic': 'test_light_rgb/set',
'state_topic': 'test_light_rgb/status'
}}
with assert_setup_component(1):
assert setup_component(self.hass, light.DOMAIN, config)
state = self.hass.states.get('light.test')
self.assertEqual(STATE_OFF, state.state)
self.assertIsNone(state.attributes.get('effect'))
fire_mqtt_message(self.hass, 'test_light_rgb/status', 'ON')
self.hass.block_till_done()
state = self.hass.states.get('light.test')
self.assertEqual(STATE_ON, state.state)
self.assertEqual('none', state.attributes.get('effect'))
def test_show_white_value_if_only_command_topic(self):
"""Test the white_value if only a command topic is present."""
config = {light.DOMAIN: {
'platform': 'mqtt',
'name': 'test',
'white_value_command_topic': 'test_light_rgb/white_value/set',
'command_topic': 'test_light_rgb/set',
'state_topic': 'test_light_rgb/status',
}}
with assert_setup_component(1):
assert setup_component(self.hass, light.DOMAIN, config)
state = self.hass.states.get('light.test')
self.assertEqual(STATE_OFF, state.state)
self.assertIsNone(state.attributes.get('white_value'))
fire_mqtt_message(self.hass, 'test_light_rgb/status', 'ON')
self.hass.block_till_done()
state = self.hass.states.get('light.test')
self.assertEqual(STATE_ON, state.state)
self.assertEqual(255, state.attributes.get('white_value'))
def test_show_xy_if_only_command_topic(self):
"""Test the xy if only a command topic is present."""
config = {light.DOMAIN: {
'platform': 'mqtt',
'name': 'test',
'xy_command_topic': 'test_light_rgb/xy/set',
'command_topic': 'test_light_rgb/set',
'state_topic': 'test_light_rgb/status',
}}
with assert_setup_component(1):
assert setup_component(self.hass, light.DOMAIN, config)
state = self.hass.states.get('light.test')
self.assertEqual(STATE_OFF, state.state)
self.assertIsNone(state.attributes.get('xy_color'))
fire_mqtt_message(self.hass, 'test_light_rgb/status', 'ON')
self.hass.block_till_done()
state = self.hass.states.get('light.test')
self.assertEqual(STATE_ON, state.state)
self.assertEqual([1, 1], state.attributes.get('xy_color'))
+141 -9
View File
@@ -1,6 +1,56 @@
"""The tests for the MQTT JSON light platform.
Configuration for RGB Version with brightness:
Configuration with RGB, brightness, color temp, effect, white value and XY:
light:
platform: mqtt_json
name: mqtt_json_light_1
state_topic: "home/rgb1"
command_topic: "home/rgb1/set"
brightness: true
color_temp: true
effect: true
rgb: true
white_value: true
xy: true
Configuration with RGB, brightness, color temp, effect, white value:
light:
platform: mqtt_json
name: mqtt_json_light_1
state_topic: "home/rgb1"
command_topic: "home/rgb1/set"
brightness: true
color_temp: true
effect: true
rgb: true
white_value: true
Configuration with RGB, brightness, color temp and effect:
light:
platform: mqtt_json
name: mqtt_json_light_1
state_topic: "home/rgb1"
command_topic: "home/rgb1/set"
brightness: true
color_temp: true
effect: true
rgb: true
Configuration with RGB, brightness and color temp:
light:
platform: mqtt_json
name: mqtt_json_light_1
state_topic: "home/rgb1"
command_topic: "home/rgb1/set"
brightness: true
rgb: true
color_temp: true
Configuration with RGB, brightness:
light:
platform: mqtt_json
@@ -62,9 +112,9 @@ class TestLightMQTTJSON(unittest.TestCase):
})
self.assertIsNone(self.hass.states.get('light.test'))
def test_no_color_or_brightness_if_no_config(self): \
def test_no_color_brightness_color_temp_white_val_if_no_topics(self): \
# pylint: disable=invalid-name
"""Test if there is no color and brightness if they aren't defined."""
"""Test for no RGB, brightness, color temp, effect, white val or XY."""
assert setup_component(self.hass, light.DOMAIN, {
light.DOMAIN: {
'platform': 'mqtt_json',
@@ -78,6 +128,10 @@ class TestLightMQTTJSON(unittest.TestCase):
self.assertEqual(STATE_OFF, state.state)
self.assertIsNone(state.attributes.get('rgb_color'))
self.assertIsNone(state.attributes.get('brightness'))
self.assertIsNone(state.attributes.get('color_temp'))
self.assertIsNone(state.attributes.get('effect'))
self.assertIsNone(state.attributes.get('white_value'))
self.assertIsNone(state.attributes.get('xy_color'))
fire_mqtt_message(self.hass, 'test_light_rgb', '{"state":"ON"}')
self.hass.block_till_done()
@@ -86,6 +140,10 @@ class TestLightMQTTJSON(unittest.TestCase):
self.assertEqual(STATE_ON, state.state)
self.assertIsNone(state.attributes.get('rgb_color'))
self.assertIsNone(state.attributes.get('brightness'))
self.assertIsNone(state.attributes.get('color_temp'))
self.assertIsNone(state.attributes.get('effect'))
self.assertIsNone(state.attributes.get('white_value'))
self.assertIsNone(state.attributes.get('xy_color'))
def test_controlling_state_via_topic(self): \
# pylint: disable=invalid-name
@@ -97,7 +155,11 @@ class TestLightMQTTJSON(unittest.TestCase):
'state_topic': 'test_light_rgb',
'command_topic': 'test_light_rgb/set',
'brightness': True,
'color_temp': True,
'effect': True,
'rgb': True,
'white_value': True,
'xy': True,
'qos': '0'
}
})
@@ -106,19 +168,31 @@ class TestLightMQTTJSON(unittest.TestCase):
self.assertEqual(STATE_OFF, state.state)
self.assertIsNone(state.attributes.get('rgb_color'))
self.assertIsNone(state.attributes.get('brightness'))
self.assertIsNone(state.attributes.get('color_temp'))
self.assertIsNone(state.attributes.get('effect'))
self.assertIsNone(state.attributes.get('white_value'))
self.assertIsNone(state.attributes.get('xy_color'))
self.assertIsNone(state.attributes.get(ATTR_ASSUMED_STATE))
# Turn on the light, full white
fire_mqtt_message(self.hass, 'test_light_rgb',
'{"state":"ON",'
'"color":{"r":255,"g":255,"b":255},'
'"brightness":255}')
'"color":{"r":255,"g":255,"b":255,'
'"x":0.123,"y":0.123},'
'"brightness":255,'
'"color_temp":155,'
'"effect":"colorloop",'
'"white_value":150}')
self.hass.block_till_done()
state = self.hass.states.get('light.test')
self.assertEqual(STATE_ON, state.state)
self.assertEqual([255, 255, 255], state.attributes.get('rgb_color'))
self.assertEqual(255, state.attributes.get('brightness'))
self.assertEqual(155, state.attributes.get('color_temp'))
self.assertEqual('colorloop', state.attributes.get('effect'))
self.assertEqual(150, state.attributes.get('white_value'))
self.assertEqual([0.123, 0.123], state.attributes.get('xy_color'))
# Turn the light off
fire_mqtt_message(self.hass, 'test_light_rgb', '{"state":"OFF"}')
@@ -146,6 +220,39 @@ class TestLightMQTTJSON(unittest.TestCase):
self.assertEqual([125, 125, 125],
light_state.attributes.get('rgb_color'))
fire_mqtt_message(self.hass, 'test_light_rgb',
'{"state":"ON",'
'"color":{"x":0.135,"y":0.135}}')
self.hass.block_till_done()
light_state = self.hass.states.get('light.test')
self.assertEqual([0.135, 0.135],
light_state.attributes.get('xy_color'))
fire_mqtt_message(self.hass, 'test_light_rgb',
'{"state":"ON",'
'"color_temp":155}')
self.hass.block_till_done()
light_state = self.hass.states.get('light.test')
self.assertEqual(155, light_state.attributes.get('color_temp'))
fire_mqtt_message(self.hass, 'test_light_rgb',
'{"state":"ON",'
'"effect":"colorloop"}')
self.hass.block_till_done()
light_state = self.hass.states.get('light.test')
self.assertEqual('colorloop', light_state.attributes.get('effect'))
fire_mqtt_message(self.hass, 'test_light_rgb',
'{"state":"ON",'
'"white_value":155}')
self.hass.block_till_done()
light_state = self.hass.states.get('light.test')
self.assertEqual(155, light_state.attributes.get('white_value'))
def test_sending_mqtt_commands_and_optimistic(self): \
# pylint: disable=invalid-name
"""Test the sending of command in optimistic mode."""
@@ -155,7 +262,10 @@ class TestLightMQTTJSON(unittest.TestCase):
'name': 'test',
'command_topic': 'test_light_rgb/set',
'brightness': True,
'color_temp': True,
'effect': True,
'rgb': True,
'white_value': True,
'qos': 2
}
})
@@ -181,7 +291,8 @@ class TestLightMQTTJSON(unittest.TestCase):
self.assertEqual(STATE_OFF, state.state)
light.turn_on(self.hass, 'light.test', rgb_color=[75, 75, 75],
brightness=50)
brightness=50, color_temp=155, effect='colorloop',
white_value=170)
self.hass.block_till_done()
self.assertEqual('test_light_rgb/set',
@@ -191,15 +302,21 @@ class TestLightMQTTJSON(unittest.TestCase):
# Get the sent message
message_json = json.loads(self.mock_publish.mock_calls[-2][1][1])
self.assertEqual(50, message_json["brightness"])
self.assertEqual(155, message_json["color_temp"])
self.assertEqual('colorloop', message_json["effect"])
self.assertEqual(75, message_json["color"]["r"])
self.assertEqual(75, message_json["color"]["g"])
self.assertEqual(75, message_json["color"]["b"])
self.assertEqual(170, message_json["white_value"])
self.assertEqual("ON", message_json["state"])
state = self.hass.states.get('light.test')
self.assertEqual(STATE_ON, state.state)
self.assertEqual((75, 75, 75), state.attributes['rgb_color'])
self.assertEqual(50, state.attributes['brightness'])
self.assertEqual(155, state.attributes['color_temp'])
self.assertEqual('colorloop', state.attributes['effect'])
self.assertEqual(170, state.attributes['white_value'])
def test_flash_short_and_long(self): \
# pylint: disable=invalid-name
@@ -283,9 +400,9 @@ class TestLightMQTTJSON(unittest.TestCase):
self.assertEqual(10, message_json["transition"])
self.assertEqual("OFF", message_json["state"])
def test_invalid_color_and_brightness_values(self): \
def test_invalid_color_brightness_and_white_values(self): \
# pylint: disable=invalid-name
"""Test that invalid color/brightness values are ignored."""
"""Test that invalid color/brightness/white values are ignored."""
assert setup_component(self.hass, light.DOMAIN, {
light.DOMAIN: {
'platform': 'mqtt_json',
@@ -294,6 +411,7 @@ class TestLightMQTTJSON(unittest.TestCase):
'command_topic': 'test_light_rgb/set',
'brightness': True,
'rgb': True,
'white_value': True,
'qos': '0'
}
})
@@ -302,19 +420,22 @@ class TestLightMQTTJSON(unittest.TestCase):
self.assertEqual(STATE_OFF, state.state)
self.assertIsNone(state.attributes.get('rgb_color'))
self.assertIsNone(state.attributes.get('brightness'))
self.assertIsNone(state.attributes.get('white_value'))
self.assertIsNone(state.attributes.get(ATTR_ASSUMED_STATE))
# Turn on the light
fire_mqtt_message(self.hass, 'test_light_rgb',
'{"state":"ON",'
'"color":{"r":255,"g":255,"b":255},'
'"brightness": 255}')
'"brightness": 255,'
'"white_value": 255}')
self.hass.block_till_done()
state = self.hass.states.get('light.test')
self.assertEqual(STATE_ON, state.state)
self.assertEqual([255, 255, 255], state.attributes.get('rgb_color'))
self.assertEqual(255, state.attributes.get('brightness'))
self.assertEqual(255, state.attributes.get('white_value'))
# Bad color values
fire_mqtt_message(self.hass, 'test_light_rgb',
@@ -337,3 +458,14 @@ class TestLightMQTTJSON(unittest.TestCase):
state = self.hass.states.get('light.test')
self.assertEqual(STATE_ON, state.state)
self.assertEqual(255, state.attributes.get('brightness'))
# Bad white value
fire_mqtt_message(self.hass, 'test_light_rgb',
'{"state":"ON",'
'"white_value": "badValue"}')
self.hass.block_till_done()
# White value should not have changed
state = self.hass.states.get('light.test')
self.assertEqual(STATE_ON, state.state)
self.assertEqual(255, state.attributes.get('white_value'))
+87 -22
View File
@@ -12,13 +12,19 @@ light:
command_off_template: 'off'
state_template: '{{ value.split(",")[0] }}'
brightness_template: '{{ value.split(",")[1] }}'
red_template: '{{ value.split(",")[2].split("-")[0] }}'
green_template: '{{ value.split(",")[2].split("-")[1] }}'
blue_template: '{{ value.split(",")[2].split("-")[2] }}'
color_temp_template: '{{ value.split(",")[2] }}'
white_value_template: '{{ value.split(",")[3] }}'
red_template: '{{ value.split(",")[4].split("-")[0] }}'
green_template: '{{ value.split(",")[4].split("-")[1] }}'
blue_template: '{{ value.split(",")[4].split("-")[2] }}'
If your light doesn't support brightness feature, omit `brightness_template`.
If your light doesn't support rgb feature, omit `(red|green|blue)_template`.
If your light doesn't support color temp feature, omit `color_temp_template`.
If your light doesn't support white value feature, omit `white_value_template`.
If your light doesn't support RGB feature, omit `(red|green|blue)_template`.
"""
import unittest
@@ -66,6 +72,8 @@ class TestLightMQTTTemplate(unittest.TestCase):
'command_topic': 'test_light_rgb/set',
'command_on_template': 'on,'
'{{ brightness|d }},'
'{{ color_temp|d }},'
'{{ white_value|d }},'
'{{ red|d }}-'
'{{ green|d }}-'
'{{ blue|d }}',
@@ -78,6 +86,8 @@ class TestLightMQTTTemplate(unittest.TestCase):
self.assertEqual(STATE_OFF, state.state)
self.assertIsNone(state.attributes.get('rgb_color'))
self.assertIsNone(state.attributes.get('brightness'))
self.assertIsNone(state.attributes.get('color_temp'))
self.assertIsNone(state.attributes.get('white_value'))
self.assertIsNone(state.attributes.get(ATTR_ASSUMED_STATE))
fire_mqtt_message(self.hass, 'test_light_rgb', 'on')
@@ -87,10 +97,12 @@ class TestLightMQTTTemplate(unittest.TestCase):
self.assertEqual(STATE_ON, state.state)
self.assertIsNone(state.attributes.get('rgb_color'))
self.assertIsNone(state.attributes.get('brightness'))
self.assertIsNone(state.attributes.get('color_temp'))
self.assertIsNone(state.attributes.get('white_value'))
def test_state_brightness_color_effect_change_via_topic(self): \
def test_state_brightness_color_effect_temp_white_change_via_topic(self): \
# pylint: disable=invalid-name
"""Test state, brightness, color and effect change via topic."""
"""Test state, bri, color, effect, color temp, white val change."""
with assert_setup_component(1):
assert setup_component(self.hass, light.DOMAIN, {
light.DOMAIN: {
@@ -101,6 +113,8 @@ class TestLightMQTTTemplate(unittest.TestCase):
'command_topic': 'test_light_rgb/set',
'command_on_template': 'on,'
'{{ brightness|d }},'
'{{ color_temp|d }},'
'{{ white_value|d }},'
'{{ red|d }}-'
'{{ green|d }}-'
'{{ blue|d }},'
@@ -108,13 +122,15 @@ class TestLightMQTTTemplate(unittest.TestCase):
'command_off_template': 'off',
'state_template': '{{ value.split(",")[0] }}',
'brightness_template': '{{ value.split(",")[1] }}',
'red_template': '{{ value.split(",")[2].'
'color_temp_template': '{{ value.split(",")[2] }}',
'white_value_template': '{{ value.split(",")[3] }}',
'red_template': '{{ value.split(",")[4].'
'split("-")[0] }}',
'green_template': '{{ value.split(",")[2].'
'green_template': '{{ value.split(",")[4].'
'split("-")[1] }}',
'blue_template': '{{ value.split(",")[2].'
'blue_template': '{{ value.split(",")[4].'
'split("-")[2] }}',
'effect_template': '{{ value.split(",")[3] }}'
'effect_template': '{{ value.split(",")[5] }}'
}
})
@@ -123,16 +139,21 @@ class TestLightMQTTTemplate(unittest.TestCase):
self.assertIsNone(state.attributes.get('rgb_color'))
self.assertIsNone(state.attributes.get('brightness'))
self.assertIsNone(state.attributes.get('effect'))
self.assertIsNone(state.attributes.get('color_temp'))
self.assertIsNone(state.attributes.get('white_value'))
self.assertIsNone(state.attributes.get(ATTR_ASSUMED_STATE))
# turn on the light, full white
fire_mqtt_message(self.hass, 'test_light_rgb', 'on,255,255-255-255,')
fire_mqtt_message(self.hass, 'test_light_rgb',
'on,255,145,123,255-255-255,')
self.hass.block_till_done()
state = self.hass.states.get('light.test')
self.assertEqual(STATE_ON, state.state)
self.assertEqual([255, 255, 255], state.attributes.get('rgb_color'))
self.assertEqual(255, state.attributes.get('brightness'))
self.assertEqual(145, state.attributes.get('color_temp'))
self.assertEqual(123, state.attributes.get('white_value'))
self.assertIsNone(state.attributes.get('effect'))
# turn the light off
@@ -150,15 +171,32 @@ class TestLightMQTTTemplate(unittest.TestCase):
self.hass.block_till_done()
self.assertEqual(100, light_state.attributes['brightness'])
# change the color temp
fire_mqtt_message(self.hass, 'test_light_rgb', 'on,,195')
self.hass.block_till_done()
light_state = self.hass.states.get('light.test')
self.hass.block_till_done()
self.assertEqual(195, light_state.attributes['color_temp'])
# change the color
fire_mqtt_message(self.hass, 'test_light_rgb', 'on,,41-42-43')
fire_mqtt_message(self.hass, 'test_light_rgb', 'on,,,,41-42-43')
self.hass.block_till_done()
light_state = self.hass.states.get('light.test')
self.assertEqual([41, 42, 43], light_state.attributes.get('rgb_color'))
# change the white value
fire_mqtt_message(self.hass, 'test_light_rgb', 'on,,,134')
self.hass.block_till_done()
light_state = self.hass.states.get('light.test')
self.hass.block_till_done()
self.assertEqual(134, light_state.attributes['white_value'])
# change the effect
fire_mqtt_message(self.hass, 'test_light_rgb', 'on,,41-42-43,rainbow')
fire_mqtt_message(self.hass, 'test_light_rgb',
'on,,,,41-42-43,rainbow')
self.hass.block_till_done()
light_state = self.hass.states.get('light.test')
@@ -175,6 +213,8 @@ class TestLightMQTTTemplate(unittest.TestCase):
'command_topic': 'test_light_rgb/set',
'command_on_template': 'on,'
'{{ brightness|d }},'
'{{ color_temp|d }},'
'{{ white_value|d }},'
'{{ red|d }}-'
'{{ green|d }}-'
'{{ blue|d }}',
@@ -191,7 +231,7 @@ class TestLightMQTTTemplate(unittest.TestCase):
light.turn_on(self.hass, 'light.test')
self.hass.block_till_done()
self.assertEqual(('test_light_rgb/set', 'on,,--', 2, False),
self.assertEqual(('test_light_rgb/set', 'on,,,,--', 2, False),
self.mock_publish.mock_calls[-2][1])
state = self.hass.states.get('light.test')
self.assertEqual(STATE_ON, state.state)
@@ -205,9 +245,9 @@ class TestLightMQTTTemplate(unittest.TestCase):
state = self.hass.states.get('light.test')
self.assertEqual(STATE_OFF, state.state)
# turn on the light with brightness and color
# turn on the light with brightness, color, color temp and white val
light.turn_on(self.hass, 'light.test', brightness=50,
rgb_color=[75, 75, 75])
rgb_color=[75, 75, 75], color_temp=200, white_value=139)
self.hass.block_till_done()
self.assertEqual('test_light_rgb/set',
@@ -217,13 +257,15 @@ class TestLightMQTTTemplate(unittest.TestCase):
# check the payload
payload = self.mock_publish.mock_calls[-2][1][1]
self.assertEqual('on,50,75-75-75', payload)
self.assertEqual('on,50,200,139,75-75-75', payload)
# check the state
state = self.hass.states.get('light.test')
self.assertEqual(STATE_ON, state.state)
self.assertEqual((75, 75, 75), state.attributes['rgb_color'])
self.assertEqual(50, state.attributes['brightness'])
self.assertEqual(200, state.attributes['color_temp'])
self.assertEqual(139, state.attributes['white_value'])
def test_flash(self): \
# pylint: disable=invalid-name
@@ -324,6 +366,7 @@ class TestLightMQTTTemplate(unittest.TestCase):
'command_topic': 'test_light_rgb/set',
'command_on_template': 'on,'
'{{ brightness|d }},'
'{{ color_temp|d }},'
'{{ red|d }}-'
'{{ green|d }}-'
'{{ blue|d }},'
@@ -331,13 +374,15 @@ class TestLightMQTTTemplate(unittest.TestCase):
'command_off_template': 'off',
'state_template': '{{ value.split(",")[0] }}',
'brightness_template': '{{ value.split(",")[1] }}',
'red_template': '{{ value.split(",")[2].'
'color_temp_template': '{{ value.split(",")[2] }}',
'white_value_template': '{{ value.split(",")[3] }}',
'red_template': '{{ value.split(",")[4].'
'split("-")[0] }}',
'green_template': '{{ value.split(",")[2].'
'green_template': '{{ value.split(",")[4].'
'split("-")[1] }}',
'blue_template': '{{ value.split(",")[2].'
'blue_template': '{{ value.split(",")[4].'
'split("-")[2] }}',
'effect_template': '{{ value.split(",")[3] }}',
'effect_template': '{{ value.split(",")[5] }}',
}
})
@@ -345,18 +390,22 @@ class TestLightMQTTTemplate(unittest.TestCase):
self.assertEqual(STATE_OFF, state.state)
self.assertIsNone(state.attributes.get('rgb_color'))
self.assertIsNone(state.attributes.get('brightness'))
self.assertIsNone(state.attributes.get('color_temp'))
self.assertIsNone(state.attributes.get('effect'))
self.assertIsNone(state.attributes.get('white_value'))
self.assertIsNone(state.attributes.get(ATTR_ASSUMED_STATE))
# turn on the light, full white
fire_mqtt_message(self.hass, 'test_light_rgb',
'on,255,255-255-255,rainbow')
'on,255,215,222,255-255-255,rainbow')
self.hass.block_till_done()
state = self.hass.states.get('light.test')
self.assertEqual(STATE_ON, state.state)
self.assertEqual(255, state.attributes.get('brightness'))
self.assertEqual(215, state.attributes.get('color_temp'))
self.assertEqual([255, 255, 255], state.attributes.get('rgb_color'))
self.assertEqual(222, state.attributes.get('white_value'))
self.assertEqual('rainbow', state.attributes.get('effect'))
# bad state value
@@ -375,6 +424,14 @@ class TestLightMQTTTemplate(unittest.TestCase):
state = self.hass.states.get('light.test')
self.assertEqual(255, state.attributes.get('brightness'))
# bad color temp values
fire_mqtt_message(self.hass, 'test_light_rgb', 'on,,off,255-255-255')
self.hass.block_till_done()
# color temp should not have changed
state = self.hass.states.get('light.test')
self.assertEqual(215, state.attributes.get('color_temp'))
# bad color values
fire_mqtt_message(self.hass, 'test_light_rgb', 'on,255,a-b-c')
self.hass.block_till_done()
@@ -383,6 +440,14 @@ class TestLightMQTTTemplate(unittest.TestCase):
state = self.hass.states.get('light.test')
self.assertEqual([255, 255, 255], state.attributes.get('rgb_color'))
# bad white value values
fire_mqtt_message(self.hass, 'test_light_rgb', 'on,,,off,255-255-255')
self.hass.block_till_done()
# white value should not have changed
state = self.hass.states.get('light.test')
self.assertEqual(222, state.attributes.get('white_value'))
# bad effect value
fire_mqtt_message(self.hass, 'test_light_rgb', 'on,255,a-b-c,white')
self.hass.block_till_done()