Merge pull request #1495 from balloob/mqtt-light

MQTT Light: fix brightness issues
This commit is contained in:
Paulus Schoutsen 2016-03-07 10:20:36 -08:00
commit cfd65e48cb
2 changed files with 29 additions and 4 deletions

View File

@ -26,7 +26,6 @@ DEPENDENCIES = ['mqtt']
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
"""Add MQTT Light."""
if config.get('command_topic') is None:
_LOGGER.error("Missing required variable: command_topic")
return False
@ -51,12 +50,12 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
class MqttLight(Light):
"""Provides a MQTT light."""
"""MQTT light."""
# pylint: disable=too-many-arguments,too-many-instance-attributes
def __init__(self, hass, name, topic, templates, qos, payload, optimistic,
brightness_scale):
"""Initialize MQTT light."""
self._hass = hass
self._name = name
self._topic = topic
@ -98,6 +97,8 @@ class MqttLight(Light):
mqtt.subscribe(self._hass, self._topic["brightness_state_topic"],
brightness_received, self._qos)
self._brightness = 255
elif self._topic["brightness_command_topic"] is not None:
self._brightness = 255
else:
self._brightness = None
@ -111,6 +112,8 @@ class MqttLight(Light):
mqtt.subscribe(self._hass, self._topic["rgb_state_topic"],
rgb_received, self._qos)
self._rgb = [255, 255, 255]
if self._topic["rgb_command_topic"] is not None:
self._rgb = [255, 255, 255]
else:
self._rgb = None
@ -131,7 +134,7 @@ class MqttLight(Light):
@property
def name(self):
"""Returns the name of the device if any."""
"""Name of the device if any."""
return self._name
@property

View File

@ -305,3 +305,25 @@ class TestLightMQTT(unittest.TestCase):
self.assertEqual(STATE_ON, state.state)
self.assertEqual([75, 75, 75], state.attributes['rgb_color'])
self.assertEqual(50, state.attributes['brightness'])
def test_show_brightness_if_only_command_topic(self):
self.assertTrue(light.setup(self.hass, {
'light': {
'platform': 'mqtt',
'name': 'test',
'brightness_command_topic': 'test_light_rgb/brightness/set',
'command_topic': 'test_light_rgb/set',
'state_topic': 'test_light_rgb/status',
}
}))
state = self.hass.states.get('light.test')
self.assertEqual(STATE_OFF, state.state)
self.assertIsNone(state.attributes.get('brightness'))
fire_mqtt_message(self.hass, 'test_light_rgb/status', 'ON')
self.hass.pool.block_till_done()
state = self.hass.states.get('light.test')
self.assertEqual(STATE_ON, state.state)
self.assertEqual(255, state.attributes.get('brightness'))