mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Fix MQTT light bugs
This commit is contained in:
parent
5b4f607da1
commit
90eab17ea6
@ -18,7 +18,6 @@ DEFAULT_NAME = "MQTT Light"
|
|||||||
DEFAULT_QOS = 0
|
DEFAULT_QOS = 0
|
||||||
DEFAULT_PAYLOAD_ON = "on"
|
DEFAULT_PAYLOAD_ON = "on"
|
||||||
DEFAULT_PAYLOAD_OFF = "off"
|
DEFAULT_PAYLOAD_OFF = "off"
|
||||||
DEFAULT_RGB_PATTERN = "%d,%d,%d"
|
|
||||||
DEFAULT_OPTIMISTIC = False
|
DEFAULT_OPTIMISTIC = False
|
||||||
|
|
||||||
DEPENDENCIES = ['mqtt']
|
DEPENDENCIES = ['mqtt']
|
||||||
@ -137,9 +136,8 @@ class MqttLight(Light):
|
|||||||
|
|
||||||
if ATTR_RGB_COLOR in kwargs and \
|
if ATTR_RGB_COLOR in kwargs and \
|
||||||
self._topic["rgb_command_topic"] is not None:
|
self._topic["rgb_command_topic"] is not None:
|
||||||
rgb = DEFAULT_RGB_PATTERN % tuple(self._rgb)
|
|
||||||
mqtt.publish(self._hass, self._topic["rgb_command_topic"],
|
mqtt.publish(self._hass, self._topic["rgb_command_topic"],
|
||||||
rgb, self._qos)
|
"{},{},{}".format(*kwargs[ATTR_RGB_COLOR]), self._qos)
|
||||||
|
|
||||||
if self._optimistic_rgb:
|
if self._optimistic_rgb:
|
||||||
self._rgb = kwargs[ATTR_RGB_COLOR]
|
self._rgb = kwargs[ATTR_RGB_COLOR]
|
||||||
@ -147,8 +145,10 @@ class MqttLight(Light):
|
|||||||
|
|
||||||
if ATTR_BRIGHTNESS in kwargs and \
|
if ATTR_BRIGHTNESS in kwargs and \
|
||||||
self._topic["brightness_command_topic"] is not None:
|
self._topic["brightness_command_topic"] is not None:
|
||||||
|
|
||||||
mqtt.publish(self._hass, self._topic["brightness_command_topic"],
|
mqtt.publish(self._hass, self._topic["brightness_command_topic"],
|
||||||
self._brightness, self._qos)
|
kwargs[ATTR_BRIGHTNESS], self._qos)
|
||||||
|
|
||||||
if self._optimistic_brightness:
|
if self._optimistic_brightness:
|
||||||
self._brightness = kwargs[ATTR_BRIGHTNESS]
|
self._brightness = kwargs[ATTR_BRIGHTNESS]
|
||||||
should_update = True
|
should_update = True
|
||||||
|
@ -149,9 +149,7 @@ class TestLightMQTT(unittest.TestCase):
|
|||||||
'platform': 'mqtt',
|
'platform': 'mqtt',
|
||||||
'name': 'test',
|
'name': 'test',
|
||||||
'command_topic': 'test_light_rgb/set',
|
'command_topic': 'test_light_rgb/set',
|
||||||
'brightness_state_topic': 'test_light_rgb/brightness/status',
|
|
||||||
'brightness_command_topic': 'test_light_rgb/brightness/set',
|
'brightness_command_topic': 'test_light_rgb/brightness/set',
|
||||||
'rgb_state_topic': 'test_light_rgb/rgb/status',
|
|
||||||
'rgb_command_topic': 'test_light_rgb/rgb/set',
|
'rgb_command_topic': 'test_light_rgb/rgb/set',
|
||||||
'qos': 2,
|
'qos': 2,
|
||||||
'payload_on': 'on',
|
'payload_on': 'on',
|
||||||
@ -177,3 +175,26 @@ class TestLightMQTT(unittest.TestCase):
|
|||||||
self.mock_publish.mock_calls[-1][1])
|
self.mock_publish.mock_calls[-1][1])
|
||||||
state = self.hass.states.get('light.test')
|
state = self.hass.states.get('light.test')
|
||||||
self.assertEqual(STATE_OFF, state.state)
|
self.assertEqual(STATE_OFF, state.state)
|
||||||
|
|
||||||
|
light.turn_on(self.hass, 'light.test', rgb_color=[75, 75, 75],
|
||||||
|
brightness=50)
|
||||||
|
self.hass.pool.block_till_done()
|
||||||
|
|
||||||
|
# Calls are threaded so we need to reorder them
|
||||||
|
bright_call, rgb_call, state_call = \
|
||||||
|
sorted((call[1] for call in self.mock_publish.mock_calls[-3:]),
|
||||||
|
key=lambda call: call[0])
|
||||||
|
|
||||||
|
self.assertEqual(('test_light_rgb/set', 'on', 2, False),
|
||||||
|
state_call)
|
||||||
|
|
||||||
|
self.assertEqual(('test_light_rgb/rgb/set', '75,75,75', 2, False),
|
||||||
|
rgb_call)
|
||||||
|
|
||||||
|
self.assertEqual(('test_light_rgb/brightness/set', 50, 2, False),
|
||||||
|
bright_call)
|
||||||
|
|
||||||
|
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'])
|
||||||
|
Loading…
x
Reference in New Issue
Block a user