Add color temp support to mqtt lights.
This commit is contained in:
parent
9a87e5e336
commit
702defb932
2 changed files with 50 additions and 4 deletions
|
@ -11,11 +11,12 @@ import voluptuous as vol
|
||||||
|
|
||||||
import homeassistant.components.mqtt as mqtt
|
import homeassistant.components.mqtt as mqtt
|
||||||
from homeassistant.components.light import (
|
from homeassistant.components.light import (
|
||||||
ATTR_BRIGHTNESS, ATTR_RGB_COLOR, SUPPORT_BRIGHTNESS, SUPPORT_RGB_COLOR,
|
ATTR_BRIGHTNESS, ATTR_RGB_COLOR, ATTR_COLOR_TEMP, SUPPORT_BRIGHTNESS,
|
||||||
Light)
|
SUPPORT_RGB_COLOR, SUPPORT_COLOR_TEMP, Light)
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
CONF_NAME, CONF_OPTIMISTIC, CONF_VALUE_TEMPLATE, CONF_PAYLOAD_OFF,
|
CONF_NAME, CONF_OPTIMISTIC, CONF_VALUE_TEMPLATE, CONF_PAYLOAD_OFF,
|
||||||
CONF_PAYLOAD_ON, CONF_STATE, CONF_BRIGHTNESS, CONF_RGB)
|
CONF_PAYLOAD_ON, CONF_STATE, CONF_BRIGHTNESS, CONF_RGB,
|
||||||
|
CONF_COLOR_TEMP)
|
||||||
from homeassistant.components.mqtt import (
|
from homeassistant.components.mqtt import (
|
||||||
CONF_STATE_TOPIC, CONF_COMMAND_TOPIC, CONF_QOS, CONF_RETAIN)
|
CONF_STATE_TOPIC, CONF_COMMAND_TOPIC, CONF_QOS, CONF_RETAIN)
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
@ -33,6 +34,9 @@ CONF_RGB_STATE_TOPIC = 'rgb_state_topic'
|
||||||
CONF_RGB_COMMAND_TOPIC = 'rgb_command_topic'
|
CONF_RGB_COMMAND_TOPIC = 'rgb_command_topic'
|
||||||
CONF_RGB_VALUE_TEMPLATE = 'rgb_value_template'
|
CONF_RGB_VALUE_TEMPLATE = 'rgb_value_template'
|
||||||
CONF_BRIGHTNESS_SCALE = 'brightness_scale'
|
CONF_BRIGHTNESS_SCALE = 'brightness_scale'
|
||||||
|
CONF_COLOR_TEMP_STATE_TOPIC = 'color_temp_state_topic'
|
||||||
|
CONF_COLOR_TEMP_COMMAND_TOPIC = 'color_temp_command_topic'
|
||||||
|
CONF_COLOR_TEMP_VALUE_TEMPLATE = 'color_temp_value_template'
|
||||||
|
|
||||||
DEFAULT_NAME = 'MQTT Light'
|
DEFAULT_NAME = 'MQTT Light'
|
||||||
DEFAULT_PAYLOAD_ON = 'ON'
|
DEFAULT_PAYLOAD_ON = 'ON'
|
||||||
|
@ -46,6 +50,9 @@ PLATFORM_SCHEMA = mqtt.MQTT_RW_PLATFORM_SCHEMA.extend({
|
||||||
vol.Optional(CONF_BRIGHTNESS_STATE_TOPIC): mqtt.valid_subscribe_topic,
|
vol.Optional(CONF_BRIGHTNESS_STATE_TOPIC): mqtt.valid_subscribe_topic,
|
||||||
vol.Optional(CONF_BRIGHTNESS_COMMAND_TOPIC): mqtt.valid_publish_topic,
|
vol.Optional(CONF_BRIGHTNESS_COMMAND_TOPIC): mqtt.valid_publish_topic,
|
||||||
vol.Optional(CONF_BRIGHTNESS_VALUE_TEMPLATE): cv.template,
|
vol.Optional(CONF_BRIGHTNESS_VALUE_TEMPLATE): cv.template,
|
||||||
|
vol.Optional(CONF_COLOR_TEMP_STATE_TOPIC): mqtt.valid_subscribe_topic,
|
||||||
|
vol.Optional(CONF_COLOR_TEMP_COMMAND_TOPIC): mqtt.valid_publish_topic,
|
||||||
|
vol.Optional(CONF_COLOR_TEMP_VALUE_TEMPLATE): cv.template,
|
||||||
vol.Optional(CONF_RGB_STATE_TOPIC): mqtt.valid_subscribe_topic,
|
vol.Optional(CONF_RGB_STATE_TOPIC): mqtt.valid_subscribe_topic,
|
||||||
vol.Optional(CONF_RGB_COMMAND_TOPIC): mqtt.valid_publish_topic,
|
vol.Optional(CONF_RGB_COMMAND_TOPIC): mqtt.valid_publish_topic,
|
||||||
vol.Optional(CONF_RGB_VALUE_TEMPLATE): cv.template,
|
vol.Optional(CONF_RGB_VALUE_TEMPLATE): cv.template,
|
||||||
|
@ -72,12 +79,15 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||||
CONF_BRIGHTNESS_COMMAND_TOPIC,
|
CONF_BRIGHTNESS_COMMAND_TOPIC,
|
||||||
CONF_RGB_STATE_TOPIC,
|
CONF_RGB_STATE_TOPIC,
|
||||||
CONF_RGB_COMMAND_TOPIC,
|
CONF_RGB_COMMAND_TOPIC,
|
||||||
|
CONF_COLOR_TEMP_STATE_TOPIC,
|
||||||
|
CONF_COLOR_TEMP_COMMAND_TOPIC
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
CONF_STATE: config.get(CONF_STATE_VALUE_TEMPLATE),
|
CONF_STATE: config.get(CONF_STATE_VALUE_TEMPLATE),
|
||||||
CONF_BRIGHTNESS: config.get(CONF_BRIGHTNESS_VALUE_TEMPLATE),
|
CONF_BRIGHTNESS: config.get(CONF_BRIGHTNESS_VALUE_TEMPLATE),
|
||||||
CONF_RGB: config.get(CONF_RGB_VALUE_TEMPLATE)
|
CONF_RGB: config.get(CONF_RGB_VALUE_TEMPLATE),
|
||||||
|
CONF_COLOR_TEMP: config.get(CONF_COLOR_TEMP_VALUE_TEMPLATE)
|
||||||
},
|
},
|
||||||
config.get(CONF_QOS),
|
config.get(CONF_QOS),
|
||||||
config.get(CONF_RETAIN),
|
config.get(CONF_RETAIN),
|
||||||
|
@ -108,6 +118,8 @@ class MqttLight(Light):
|
||||||
optimistic or topic[CONF_RGB_STATE_TOPIC] is None
|
optimistic or topic[CONF_RGB_STATE_TOPIC] is None
|
||||||
self._optimistic_brightness = (
|
self._optimistic_brightness = (
|
||||||
optimistic or topic[CONF_BRIGHTNESS_STATE_TOPIC] is None)
|
optimistic or topic[CONF_BRIGHTNESS_STATE_TOPIC] is None)
|
||||||
|
self._optimistic_color_temp = (
|
||||||
|
optimistic or topic[CONF_COLOR_TEMP_STATE_TOPIC] is None)
|
||||||
self._brightness_scale = brightness_scale
|
self._brightness_scale = brightness_scale
|
||||||
self._state = False
|
self._state = False
|
||||||
self._supported_features = 0
|
self._supported_features = 0
|
||||||
|
@ -116,6 +128,9 @@ class MqttLight(Light):
|
||||||
self._supported_features |= (
|
self._supported_features |= (
|
||||||
topic[CONF_BRIGHTNESS_STATE_TOPIC] is not None
|
topic[CONF_BRIGHTNESS_STATE_TOPIC] is not None
|
||||||
and SUPPORT_BRIGHTNESS)
|
and SUPPORT_BRIGHTNESS)
|
||||||
|
self._supported_features |= (
|
||||||
|
topic[CONF_COLOR_TEMP_STATE_TOPIC] is not None
|
||||||
|
and SUPPORT_COLOR_TEMP)
|
||||||
|
|
||||||
templates = {key: ((lambda value: value) if tpl is None else
|
templates = {key: ((lambda value: value) if tpl is None else
|
||||||
partial(render_with_possible_json_value, hass, tpl))
|
partial(render_with_possible_json_value, hass, tpl))
|
||||||
|
@ -167,6 +182,21 @@ class MqttLight(Light):
|
||||||
else:
|
else:
|
||||||
self._rgb = None
|
self._rgb = None
|
||||||
|
|
||||||
|
def color_temp_received(topic, payload, qos):
|
||||||
|
"""A new MQTT message for color temp has been received."""
|
||||||
|
self._color_temp = int(templates[CONF_COLOR_TEMP](payload))
|
||||||
|
self.update_ha_state()
|
||||||
|
|
||||||
|
if self._topic[CONF_COLOR_TEMP_STATE_TOPIC] is not None:
|
||||||
|
mqtt.subscribe(
|
||||||
|
self._hass, self._topic[CONF_COLOR_TEMP_STATE_TOPIC],
|
||||||
|
color_temp_received, self._qos)
|
||||||
|
self._color_temp = 150
|
||||||
|
if self._topic[CONF_COLOR_TEMP_COMMAND_TOPIC] is not None:
|
||||||
|
self._color_temp = 150
|
||||||
|
else:
|
||||||
|
self._color_temp = None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def brightness(self):
|
def brightness(self):
|
||||||
"""Return the brightness of this light between 0..255."""
|
"""Return the brightness of this light between 0..255."""
|
||||||
|
@ -177,6 +207,11 @@ class MqttLight(Light):
|
||||||
"""Return the RGB color value."""
|
"""Return the RGB color value."""
|
||||||
return self._rgb
|
return self._rgb
|
||||||
|
|
||||||
|
@property
|
||||||
|
def color_temp(self):
|
||||||
|
return self._color_temp
|
||||||
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def should_poll(self):
|
def should_poll(self):
|
||||||
"""No polling needed for a MQTT light."""
|
"""No polling needed for a MQTT light."""
|
||||||
|
@ -229,6 +264,16 @@ class MqttLight(Light):
|
||||||
self._brightness = kwargs[ATTR_BRIGHTNESS]
|
self._brightness = kwargs[ATTR_BRIGHTNESS]
|
||||||
should_update = True
|
should_update = True
|
||||||
|
|
||||||
|
if ATTR_COLOR_TEMP in kwargs and \
|
||||||
|
self._topic[CONF_COLOR_TEMP_COMMAND_TOPIC] is not None:
|
||||||
|
color_temp = int(kwargs[ATTR_COLOR_TEMP])
|
||||||
|
mqtt.publish(
|
||||||
|
self._hass, self._topic[CONF_COLOR_TEMP_COMMAND_TOPIC],
|
||||||
|
color_temp, self._qos, self._retain)
|
||||||
|
if self._optimistic_color_temp:
|
||||||
|
self._color_temp = kwargs[ATTR_COLOR_TEMP]
|
||||||
|
should_update = True
|
||||||
|
|
||||||
mqtt.publish(self._hass, self._topic[CONF_COMMAND_TOPIC],
|
mqtt.publish(self._hass, self._topic[CONF_COMMAND_TOPIC],
|
||||||
self._payload['on'], self._qos, self._retain)
|
self._payload['on'], self._qos, self._retain)
|
||||||
|
|
||||||
|
|
|
@ -63,6 +63,7 @@ CONF_BELOW = 'below'
|
||||||
CONF_BLACKLIST = 'blacklist'
|
CONF_BLACKLIST = 'blacklist'
|
||||||
CONF_BRIGHTNESS = 'brightness'
|
CONF_BRIGHTNESS = 'brightness'
|
||||||
CONF_CODE = 'code'
|
CONF_CODE = 'code'
|
||||||
|
CONF_COLOR_TEMP = 'color_temp'
|
||||||
CONF_COMMAND = 'command'
|
CONF_COMMAND = 'command'
|
||||||
CONF_COMMAND_CLOSE = 'command_close'
|
CONF_COMMAND_CLOSE = 'command_close'
|
||||||
CONF_COMMAND_OFF = 'command_off'
|
CONF_COMMAND_OFF = 'command_off'
|
||||||
|
|
Loading…
Add table
Reference in a new issue