From ff72c5e4568bbb5ac8f4b12a1a2102617fbb53fc Mon Sep 17 00:00:00 2001
From: Adam Mills <adam@armills.info>
Date: Sun, 1 Apr 2018 14:12:55 -0400
Subject: [PATCH] Fix mqtt_json color commands (#13617)

---
 homeassistant/components/light/mqtt_json.py | 30 ++++++++++++---------
 tests/components/light/test_mqtt_json.py    | 27 +++++++++++++++++++
 2 files changed, 44 insertions(+), 13 deletions(-)

diff --git a/homeassistant/components/light/mqtt_json.py b/homeassistant/components/light/mqtt_json.py
index 25212e45c60..20e49e40bae 100644
--- a/homeassistant/components/light/mqtt_json.py
+++ b/homeassistant/components/light/mqtt_json.py
@@ -129,6 +129,8 @@ class MqttJson(MqttAvailability, Light):
         self._retain = retain
         self._optimistic = optimistic or topic[CONF_STATE_TOPIC] is None
         self._state = False
+        self._rgb = rgb
+        self._xy = xy
         if brightness:
             self._brightness = 255
         else:
@@ -307,20 +309,22 @@ class MqttJson(MqttAvailability, Light):
 
         message = {'state': 'ON'}
 
-        if ATTR_HS_COLOR in kwargs:
+        if ATTR_HS_COLOR in kwargs and (self._rgb or self._xy):
             hs_color = kwargs[ATTR_HS_COLOR]
-            brightness = kwargs.get(
-                ATTR_BRIGHTNESS, self._brightness if self._brightness else 255)
-            rgb = color_util.color_hsv_to_RGB(
-                hs_color[0], hs_color[1], brightness / 255 * 100)
-            xy_color = color_util.color_hs_to_xy(*kwargs[ATTR_HS_COLOR])
-            message['color'] = {
-                'r': rgb[0],
-                'g': rgb[1],
-                'b': rgb[2],
-                'x': xy_color[0],
-                'y': xy_color[1],
-            }
+            message['color'] = {}
+            if self._rgb:
+                brightness = kwargs.get(
+                    ATTR_BRIGHTNESS,
+                    self._brightness if self._brightness else 255)
+                rgb = color_util.color_hsv_to_RGB(
+                    hs_color[0], hs_color[1], brightness / 255 * 100)
+                message['color']['r'] = rgb[0]
+                message['color']['g'] = rgb[1]
+                message['color']['b'] = rgb[2]
+            if self._xy:
+                xy_color = color_util.color_hs_to_xy(*kwargs[ATTR_HS_COLOR])
+                message['color']['x'] = xy_color[0]
+                message['color']['y'] = xy_color[1]
 
             if self._optimistic:
                 self._hs = kwargs[ATTR_HS_COLOR]
diff --git a/tests/components/light/test_mqtt_json.py b/tests/components/light/test_mqtt_json.py
index cfeffc93108..a183355fbb3 100644
--- a/tests/components/light/test_mqtt_json.py
+++ b/tests/components/light/test_mqtt_json.py
@@ -334,6 +334,33 @@ class TestLightMQTTJSON(unittest.TestCase):
         self.assertEqual('colorloop', state.attributes['effect'])
         self.assertEqual(170, state.attributes['white_value'])
 
+        # Test a color command
+        light.turn_on(self.hass, 'light.test',
+                      brightness=50, hs_color=(125, 100))
+        self.hass.block_till_done()
+
+        self.assertEqual('test_light_rgb/set',
+                         self.mock_publish.async_publish.mock_calls[0][1][0])
+        self.assertEqual(2,
+                         self.mock_publish.async_publish.mock_calls[0][1][2])
+        self.assertEqual(False,
+                         self.mock_publish.async_publish.mock_calls[0][1][3])
+        # Get the sent message
+        message_json = json.loads(
+            self.mock_publish.async_publish.mock_calls[1][1][1])
+        self.assertEqual(50, message_json["brightness"])
+        self.assertEqual({
+            'r': 0,
+            'g': 50,
+            'b': 4,
+        }, message_json["color"])
+        self.assertEqual("ON", message_json["state"])
+
+        state = self.hass.states.get('light.test')
+        self.assertEqual(STATE_ON, state.state)
+        self.assertEqual(50, state.attributes['brightness'])
+        self.assertEqual((125, 100), state.attributes['hs_color'])
+
     def test_flash_short_and_long(self): \
             # pylint: disable=invalid-name
         """Test for flash length being sent when included."""
-- 
GitLab