From b47f1c3a964320feb7b31c4137d23104225d0f0f Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Sun, 6 Mar 2016 22:02:35 -0800 Subject: [PATCH] MQTT Light: fix brightness issues --- homeassistant/components/light/mqtt.py | 11 +++++++---- tests/components/light/test_mqtt.py | 22 ++++++++++++++++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/light/mqtt.py b/homeassistant/components/light/mqtt.py index a1ad6ea7e52..c71ccd6dff7 100644 --- a/homeassistant/components/light/mqtt.py +++ b/homeassistant/components/light/mqtt.py @@ -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 diff --git a/tests/components/light/test_mqtt.py b/tests/components/light/test_mqtt.py index 0c7703eaa78..624f5ed94f7 100644 --- a/tests/components/light/test_mqtt.py +++ b/tests/components/light/test_mqtt.py @@ -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'))