Skip to content

Commit

Permalink
Fix mqtt_json color commands (#13617)
Browse files Browse the repository at this point in the history
  • Loading branch information
emlove authored and balloob committed Apr 1, 2018
1 parent 9f0f739 commit ff72c5e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 13 deletions.
30 changes: 17 additions & 13 deletions homeassistant/components/light/mqtt_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ def __init__(self, name, effect_list, topic, qos, retain, optimistic,
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:
Expand Down Expand Up @@ -307,20 +309,22 @@ def async_turn_on(self, **kwargs):

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]
Expand Down
27 changes: 27 additions & 0 deletions tests/components/light/test_mqtt_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,33 @@ def test_sending_mqtt_commands_and_optimistic(self): \
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."""
Expand Down

0 comments on commit ff72c5e

Please sign in to comment.