Merge pull request #1495 from balloob/mqtt-light
MQTT Light: fix brightness issues
This commit is contained in:
commit
cfd65e48cb
2 changed files with 29 additions and 4 deletions
|
@ -26,7 +26,6 @@ DEPENDENCIES = ['mqtt']
|
||||||
|
|
||||||
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
|
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
|
||||||
"""Add MQTT Light."""
|
"""Add MQTT Light."""
|
||||||
|
|
||||||
if config.get('command_topic') is None:
|
if config.get('command_topic') is None:
|
||||||
_LOGGER.error("Missing required variable: command_topic")
|
_LOGGER.error("Missing required variable: command_topic")
|
||||||
return False
|
return False
|
||||||
|
@ -51,12 +50,12 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
|
||||||
|
|
||||||
|
|
||||||
class MqttLight(Light):
|
class MqttLight(Light):
|
||||||
"""Provides a MQTT light."""
|
"""MQTT light."""
|
||||||
|
|
||||||
# pylint: disable=too-many-arguments,too-many-instance-attributes
|
# pylint: disable=too-many-arguments,too-many-instance-attributes
|
||||||
def __init__(self, hass, name, topic, templates, qos, payload, optimistic,
|
def __init__(self, hass, name, topic, templates, qos, payload, optimistic,
|
||||||
brightness_scale):
|
brightness_scale):
|
||||||
|
"""Initialize MQTT light."""
|
||||||
self._hass = hass
|
self._hass = hass
|
||||||
self._name = name
|
self._name = name
|
||||||
self._topic = topic
|
self._topic = topic
|
||||||
|
@ -98,6 +97,8 @@ class MqttLight(Light):
|
||||||
mqtt.subscribe(self._hass, self._topic["brightness_state_topic"],
|
mqtt.subscribe(self._hass, self._topic["brightness_state_topic"],
|
||||||
brightness_received, self._qos)
|
brightness_received, self._qos)
|
||||||
self._brightness = 255
|
self._brightness = 255
|
||||||
|
elif self._topic["brightness_command_topic"] is not None:
|
||||||
|
self._brightness = 255
|
||||||
else:
|
else:
|
||||||
self._brightness = None
|
self._brightness = None
|
||||||
|
|
||||||
|
@ -111,6 +112,8 @@ class MqttLight(Light):
|
||||||
mqtt.subscribe(self._hass, self._topic["rgb_state_topic"],
|
mqtt.subscribe(self._hass, self._topic["rgb_state_topic"],
|
||||||
rgb_received, self._qos)
|
rgb_received, self._qos)
|
||||||
self._rgb = [255, 255, 255]
|
self._rgb = [255, 255, 255]
|
||||||
|
if self._topic["rgb_command_topic"] is not None:
|
||||||
|
self._rgb = [255, 255, 255]
|
||||||
else:
|
else:
|
||||||
self._rgb = None
|
self._rgb = None
|
||||||
|
|
||||||
|
@ -131,7 +134,7 @@ class MqttLight(Light):
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
"""Returns the name of the device if any."""
|
"""Name of the device if any."""
|
||||||
return self._name
|
return self._name
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
|
|
@ -305,3 +305,25 @@ class TestLightMQTT(unittest.TestCase):
|
||||||
self.assertEqual(STATE_ON, state.state)
|
self.assertEqual(STATE_ON, state.state)
|
||||||
self.assertEqual([75, 75, 75], state.attributes['rgb_color'])
|
self.assertEqual([75, 75, 75], state.attributes['rgb_color'])
|
||||||
self.assertEqual(50, state.attributes['brightness'])
|
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'))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue