mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Fix mqtt_json color commands (#13617)
This commit is contained in:
parent
4ad0152a44
commit
be43c3bcfe
@ -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]
|
||||
|
@ -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."""
|
||||
|
Loading…
x
Reference in New Issue
Block a user