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 c03798cb636e6b3258fce6f676fc6ca34c9b7d37. * 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:
1 parent
c937a7bcb0
commit
2b97449d98
8 files changed
+945
-136
No files matched your search
@@ -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()
|
||||
|
||||
Reference in new issue
Block a user