2015-10-25 20:48:01 +00:00
|
|
|
"""
|
2016-02-23 20:06:50 +00:00
|
|
|
Support for MQTT lights.
|
2015-10-26 14:05:01 +00:00
|
|
|
|
2015-11-13 07:29:54 +00:00
|
|
|
For more details about this platform, please refer to the documentation at
|
|
|
|
https://home-assistant.io/components/light.mqtt/
|
2015-10-25 20:48:01 +00:00
|
|
|
"""
|
|
|
|
import logging
|
2015-10-31 18:26:03 +00:00
|
|
|
|
2016-04-06 00:55:20 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2017-02-22 08:43:22 +00:00
|
|
|
from homeassistant.core import callback
|
2018-11-27 13:00:05 +00:00
|
|
|
from homeassistant.components import mqtt
|
2016-02-19 05:27:50 +00:00
|
|
|
from homeassistant.components.light import (
|
2018-03-18 22:00:29 +00:00
|
|
|
ATTR_BRIGHTNESS, ATTR_COLOR_TEMP, ATTR_EFFECT, ATTR_HS_COLOR,
|
|
|
|
ATTR_WHITE_VALUE, Light, SUPPORT_BRIGHTNESS, SUPPORT_COLOR_TEMP,
|
|
|
|
SUPPORT_EFFECT, SUPPORT_COLOR, SUPPORT_WHITE_VALUE)
|
2016-09-11 07:25:19 +00:00
|
|
|
from homeassistant.const import (
|
2018-12-13 14:51:50 +00:00
|
|
|
CONF_BRIGHTNESS, CONF_COLOR_TEMP, CONF_DEVICE, CONF_EFFECT, CONF_HS,
|
|
|
|
CONF_NAME, CONF_OPTIMISTIC, CONF_PAYLOAD_OFF, CONF_PAYLOAD_ON, STATE_ON,
|
2017-03-08 07:01:36 +00:00
|
|
|
CONF_RGB, CONF_STATE, CONF_VALUE_TEMPLATE, CONF_WHITE_VALUE, CONF_XY)
|
2016-04-07 01:35:46 +00:00
|
|
|
from homeassistant.components.mqtt import (
|
2019-01-15 22:26:37 +00:00
|
|
|
CONF_COMMAND_TOPIC, CONF_QOS, CONF_RETAIN, CONF_STATE_TOPIC,
|
2019-01-27 17:54:52 +00:00
|
|
|
CONF_UNIQUE_ID, MqttAttributes, MqttAvailability, MqttDiscoveryUpdate,
|
2019-01-17 18:54:22 +00:00
|
|
|
MqttEntityDeviceInfo, subscription)
|
2018-11-28 12:16:43 +00:00
|
|
|
from homeassistant.helpers.restore_state import RestoreEntity
|
2016-04-06 00:55:20 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2018-03-18 22:00:29 +00:00
|
|
|
import homeassistant.util.color as color_util
|
2015-10-25 20:48:01 +00:00
|
|
|
|
2019-01-29 17:29:02 +00:00
|
|
|
from . import MQTT_LIGHT_SCHEMA_SCHEMA
|
|
|
|
|
2015-10-25 20:48:01 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2016-04-06 00:55:20 +00:00
|
|
|
DEPENDENCIES = ['mqtt']
|
|
|
|
|
|
|
|
CONF_BRIGHTNESS_COMMAND_TOPIC = 'brightness_command_topic'
|
|
|
|
CONF_BRIGHTNESS_SCALE = 'brightness_scale'
|
2017-03-08 07:01:36 +00:00
|
|
|
CONF_BRIGHTNESS_STATE_TOPIC = 'brightness_state_topic'
|
|
|
|
CONF_BRIGHTNESS_VALUE_TEMPLATE = 'brightness_value_template'
|
2019-01-01 14:42:41 +00:00
|
|
|
CONF_COLOR_TEMP_COMMAND_TEMPLATE = 'color_temp_command_template'
|
2016-10-03 01:04:00 +00:00
|
|
|
CONF_COLOR_TEMP_COMMAND_TOPIC = 'color_temp_command_topic'
|
2017-03-08 07:01:36 +00:00
|
|
|
CONF_COLOR_TEMP_STATE_TOPIC = 'color_temp_state_topic'
|
2016-10-03 01:04:00 +00:00
|
|
|
CONF_COLOR_TEMP_VALUE_TEMPLATE = 'color_temp_value_template'
|
2017-03-08 07:01:36 +00:00
|
|
|
CONF_EFFECT_COMMAND_TOPIC = 'effect_command_topic'
|
|
|
|
CONF_EFFECT_LIST = 'effect_list'
|
|
|
|
CONF_EFFECT_STATE_TOPIC = 'effect_state_topic'
|
|
|
|
CONF_EFFECT_VALUE_TEMPLATE = 'effect_value_template'
|
2018-10-08 13:36:57 +00:00
|
|
|
CONF_HS_COMMAND_TOPIC = 'hs_command_topic'
|
|
|
|
CONF_HS_STATE_TOPIC = 'hs_state_topic'
|
|
|
|
CONF_HS_VALUE_TEMPLATE = 'hs_value_template'
|
2017-09-05 23:01:03 +00:00
|
|
|
CONF_RGB_COMMAND_TEMPLATE = 'rgb_command_template'
|
2017-03-08 07:01:36 +00:00
|
|
|
CONF_RGB_COMMAND_TOPIC = 'rgb_command_topic'
|
|
|
|
CONF_RGB_STATE_TOPIC = 'rgb_state_topic'
|
|
|
|
CONF_RGB_VALUE_TEMPLATE = 'rgb_value_template'
|
|
|
|
CONF_STATE_VALUE_TEMPLATE = 'state_value_template'
|
|
|
|
CONF_XY_COMMAND_TOPIC = 'xy_command_topic'
|
|
|
|
CONF_XY_STATE_TOPIC = 'xy_state_topic'
|
|
|
|
CONF_XY_VALUE_TEMPLATE = 'xy_value_template'
|
|
|
|
CONF_WHITE_VALUE_COMMAND_TOPIC = 'white_value_command_topic'
|
|
|
|
CONF_WHITE_VALUE_SCALE = 'white_value_scale'
|
|
|
|
CONF_WHITE_VALUE_STATE_TOPIC = 'white_value_state_topic'
|
|
|
|
CONF_WHITE_VALUE_TEMPLATE = 'white_value_template'
|
2017-10-31 22:18:49 +00:00
|
|
|
CONF_ON_COMMAND_TYPE = 'on_command_type'
|
2016-04-06 00:55:20 +00:00
|
|
|
|
2017-03-08 07:01:36 +00:00
|
|
|
DEFAULT_BRIGHTNESS_SCALE = 255
|
2015-12-20 03:15:05 +00:00
|
|
|
DEFAULT_NAME = 'MQTT Light'
|
2015-10-26 14:05:01 +00:00
|
|
|
DEFAULT_OPTIMISTIC = False
|
2017-03-08 07:01:36 +00:00
|
|
|
DEFAULT_PAYLOAD_OFF = 'OFF'
|
|
|
|
DEFAULT_PAYLOAD_ON = 'ON'
|
|
|
|
DEFAULT_WHITE_VALUE_SCALE = 255
|
2017-10-31 22:18:49 +00:00
|
|
|
DEFAULT_ON_COMMAND_TYPE = 'last'
|
|
|
|
|
|
|
|
VALUES_ON_COMMAND_TYPE = ['first', 'last', 'brightness']
|
2015-10-25 20:48:01 +00:00
|
|
|
|
2018-11-27 13:00:05 +00:00
|
|
|
PLATFORM_SCHEMA_BASIC = mqtt.MQTT_RW_PLATFORM_SCHEMA.extend({
|
2016-04-06 00:55:20 +00:00
|
|
|
vol.Optional(CONF_BRIGHTNESS_COMMAND_TOPIC): mqtt.valid_publish_topic,
|
2017-03-08 07:01:36 +00:00
|
|
|
vol.Optional(CONF_BRIGHTNESS_SCALE, default=DEFAULT_BRIGHTNESS_SCALE):
|
|
|
|
vol.All(vol.Coerce(int), vol.Range(min=1)),
|
|
|
|
vol.Optional(CONF_BRIGHTNESS_STATE_TOPIC): mqtt.valid_subscribe_topic,
|
2016-04-06 00:55:20 +00:00
|
|
|
vol.Optional(CONF_BRIGHTNESS_VALUE_TEMPLATE): cv.template,
|
2019-01-01 14:42:41 +00:00
|
|
|
vol.Optional(CONF_COLOR_TEMP_COMMAND_TEMPLATE): cv.template,
|
2016-10-03 01:04:00 +00:00
|
|
|
vol.Optional(CONF_COLOR_TEMP_COMMAND_TOPIC): mqtt.valid_publish_topic,
|
2017-03-08 07:01:36 +00:00
|
|
|
vol.Optional(CONF_COLOR_TEMP_STATE_TOPIC): mqtt.valid_subscribe_topic,
|
2016-10-03 01:04:00 +00:00
|
|
|
vol.Optional(CONF_COLOR_TEMP_VALUE_TEMPLATE): cv.template,
|
2017-03-08 07:01:36 +00:00
|
|
|
vol.Optional(CONF_EFFECT_COMMAND_TOPIC): mqtt.valid_publish_topic,
|
|
|
|
vol.Optional(CONF_EFFECT_LIST): vol.All(cv.ensure_list, [cv.string]),
|
|
|
|
vol.Optional(CONF_EFFECT_STATE_TOPIC): mqtt.valid_subscribe_topic,
|
|
|
|
vol.Optional(CONF_EFFECT_VALUE_TEMPLATE): cv.template,
|
2018-10-08 13:36:57 +00:00
|
|
|
vol.Optional(CONF_HS_COMMAND_TOPIC): mqtt.valid_publish_topic,
|
|
|
|
vol.Optional(CONF_HS_STATE_TOPIC): mqtt.valid_subscribe_topic,
|
|
|
|
vol.Optional(CONF_HS_VALUE_TEMPLATE): cv.template,
|
2017-03-08 07:01:36 +00:00
|
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
2018-09-01 21:37:03 +00:00
|
|
|
vol.Optional(CONF_UNIQUE_ID): cv.string,
|
2017-03-08 07:01:36 +00:00
|
|
|
vol.Optional(CONF_OPTIMISTIC, default=DEFAULT_OPTIMISTIC): cv.boolean,
|
|
|
|
vol.Optional(CONF_PAYLOAD_OFF, default=DEFAULT_PAYLOAD_OFF): cv.string,
|
|
|
|
vol.Optional(CONF_PAYLOAD_ON, default=DEFAULT_PAYLOAD_ON): cv.string,
|
2017-09-05 23:01:03 +00:00
|
|
|
vol.Optional(CONF_RGB_COMMAND_TEMPLATE): cv.template,
|
2016-04-06 00:55:20 +00:00
|
|
|
vol.Optional(CONF_RGB_COMMAND_TOPIC): mqtt.valid_publish_topic,
|
2017-03-08 07:01:36 +00:00
|
|
|
vol.Optional(CONF_RGB_STATE_TOPIC): mqtt.valid_subscribe_topic,
|
2016-04-06 00:55:20 +00:00
|
|
|
vol.Optional(CONF_RGB_VALUE_TEMPLATE): cv.template,
|
2017-03-08 07:01:36 +00:00
|
|
|
vol.Optional(CONF_STATE_VALUE_TEMPLATE): cv.template,
|
|
|
|
vol.Optional(CONF_WHITE_VALUE_COMMAND_TOPIC): mqtt.valid_publish_topic,
|
|
|
|
vol.Optional(CONF_WHITE_VALUE_SCALE, default=DEFAULT_WHITE_VALUE_SCALE):
|
2016-04-06 00:55:20 +00:00
|
|
|
vol.All(vol.Coerce(int), vol.Range(min=1)),
|
2017-03-08 07:01:36 +00:00
|
|
|
vol.Optional(CONF_WHITE_VALUE_STATE_TOPIC): mqtt.valid_subscribe_topic,
|
|
|
|
vol.Optional(CONF_WHITE_VALUE_TEMPLATE): cv.template,
|
|
|
|
vol.Optional(CONF_XY_COMMAND_TOPIC): mqtt.valid_publish_topic,
|
|
|
|
vol.Optional(CONF_XY_STATE_TOPIC): mqtt.valid_subscribe_topic,
|
|
|
|
vol.Optional(CONF_XY_VALUE_TEMPLATE): cv.template,
|
2017-10-31 22:18:49 +00:00
|
|
|
vol.Optional(CONF_ON_COMMAND_TYPE, default=DEFAULT_ON_COMMAND_TYPE):
|
|
|
|
vol.In(VALUES_ON_COMMAND_TYPE),
|
2018-12-13 14:51:50 +00:00
|
|
|
vol.Optional(CONF_DEVICE): mqtt.MQTT_ENTITY_DEVICE_INFO_SCHEMA,
|
2019-01-17 18:54:22 +00:00
|
|
|
}).extend(mqtt.MQTT_AVAILABILITY_SCHEMA.schema).extend(
|
2019-01-29 17:29:02 +00:00
|
|
|
mqtt.MQTT_JSON_ATTRS_SCHEMA.schema).extend(MQTT_LIGHT_SCHEMA_SCHEMA.schema)
|
2015-10-25 20:48:01 +00:00
|
|
|
|
2015-10-25 22:38:24 +00:00
|
|
|
|
2019-01-29 03:45:34 +00:00
|
|
|
async def async_setup_entity_basic(config, async_add_entities, config_entry,
|
2018-11-27 13:00:05 +00:00
|
|
|
discovery_hash=None):
|
2018-09-28 14:57:17 +00:00
|
|
|
"""Set up a MQTT Light."""
|
2017-02-22 08:43:22 +00:00
|
|
|
config.setdefault(
|
|
|
|
CONF_STATE_VALUE_TEMPLATE, config.get(CONF_VALUE_TEMPLATE))
|
|
|
|
|
2019-01-29 03:45:34 +00:00
|
|
|
async_add_entities([MqttLight(config, config_entry, discovery_hash)])
|
2018-12-02 15:16:36 +00:00
|
|
|
|
|
|
|
|
2018-12-13 14:51:50 +00:00
|
|
|
# pylint: disable=too-many-ancestors
|
2019-01-17 18:54:22 +00:00
|
|
|
class MqttLight(MqttAttributes, MqttAvailability, MqttDiscoveryUpdate,
|
|
|
|
MqttEntityDeviceInfo, Light, RestoreEntity):
|
2018-12-02 15:16:36 +00:00
|
|
|
"""Representation of a MQTT light."""
|
|
|
|
|
2019-01-29 03:45:34 +00:00
|
|
|
def __init__(self, config, config_entry, discovery_hash):
|
2018-12-02 15:16:36 +00:00
|
|
|
"""Initialize MQTT light."""
|
|
|
|
self._state = False
|
|
|
|
self._sub_state = None
|
|
|
|
self._brightness = None
|
|
|
|
self._hs = None
|
|
|
|
self._color_temp = None
|
|
|
|
self._effect = None
|
|
|
|
self._white_value = None
|
|
|
|
|
|
|
|
self._topic = None
|
|
|
|
self._payload = None
|
|
|
|
self._templates = None
|
|
|
|
self._optimistic = False
|
|
|
|
self._optimistic_rgb = False
|
|
|
|
self._optimistic_brightness = False
|
|
|
|
self._optimistic_color_temp = False
|
|
|
|
self._optimistic_effect = False
|
|
|
|
self._optimistic_hs = False
|
|
|
|
self._optimistic_white_value = False
|
|
|
|
self._optimistic_xy = False
|
|
|
|
self._unique_id = config.get(CONF_UNIQUE_ID)
|
|
|
|
|
|
|
|
# Load config
|
|
|
|
self._setup_from_config(config)
|
|
|
|
|
2018-12-13 14:51:50 +00:00
|
|
|
device_config = config.get(CONF_DEVICE)
|
2018-12-02 15:16:36 +00:00
|
|
|
|
2019-01-17 18:54:22 +00:00
|
|
|
MqttAttributes.__init__(self, config)
|
2019-01-15 22:26:37 +00:00
|
|
|
MqttAvailability.__init__(self, config)
|
2018-12-02 15:16:36 +00:00
|
|
|
MqttDiscoveryUpdate.__init__(self, discovery_hash,
|
|
|
|
self.discovery_update)
|
2019-01-29 03:45:34 +00:00
|
|
|
MqttEntityDeviceInfo.__init__(self, device_config, config_entry)
|
2018-12-02 15:16:36 +00:00
|
|
|
|
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""Subscribe to MQTT events."""
|
|
|
|
await super().async_added_to_hass()
|
|
|
|
await self._subscribe_topics()
|
|
|
|
|
|
|
|
async def discovery_update(self, discovery_payload):
|
|
|
|
"""Handle updated discovery message."""
|
|
|
|
config = PLATFORM_SCHEMA_BASIC(discovery_payload)
|
|
|
|
self._setup_from_config(config)
|
2019-01-17 18:54:22 +00:00
|
|
|
await self.attributes_discovery_update(config)
|
2018-12-02 15:16:36 +00:00
|
|
|
await self.availability_discovery_update(config)
|
2019-01-29 03:45:34 +00:00
|
|
|
await self.device_info_discovery_update(config)
|
2018-12-02 15:16:36 +00:00
|
|
|
await self._subscribe_topics()
|
|
|
|
self.async_schedule_update_ha_state()
|
|
|
|
|
|
|
|
def _setup_from_config(self, config):
|
|
|
|
"""(Re)Setup the entity."""
|
2018-12-05 13:46:37 +00:00
|
|
|
self._config = config
|
|
|
|
|
2018-12-02 15:16:36 +00:00
|
|
|
topic = {
|
2016-04-06 00:55:20 +00:00
|
|
|
key: config.get(key) for key in (
|
|
|
|
CONF_BRIGHTNESS_COMMAND_TOPIC,
|
2017-03-08 07:01:36 +00:00
|
|
|
CONF_BRIGHTNESS_STATE_TOPIC,
|
|
|
|
CONF_COLOR_TEMP_COMMAND_TOPIC,
|
2016-10-03 01:04:00 +00:00
|
|
|
CONF_COLOR_TEMP_STATE_TOPIC,
|
2017-03-08 07:01:36 +00:00
|
|
|
CONF_COMMAND_TOPIC,
|
|
|
|
CONF_EFFECT_COMMAND_TOPIC,
|
|
|
|
CONF_EFFECT_STATE_TOPIC,
|
2018-10-08 13:36:57 +00:00
|
|
|
CONF_HS_COMMAND_TOPIC,
|
|
|
|
CONF_HS_STATE_TOPIC,
|
2017-03-08 07:01:36 +00:00
|
|
|
CONF_RGB_COMMAND_TOPIC,
|
|
|
|
CONF_RGB_STATE_TOPIC,
|
|
|
|
CONF_STATE_TOPIC,
|
|
|
|
CONF_WHITE_VALUE_COMMAND_TOPIC,
|
|
|
|
CONF_WHITE_VALUE_STATE_TOPIC,
|
|
|
|
CONF_XY_COMMAND_TOPIC,
|
|
|
|
CONF_XY_STATE_TOPIC,
|
2016-04-06 00:55:20 +00:00
|
|
|
)
|
2018-12-02 15:16:36 +00:00
|
|
|
}
|
|
|
|
self._topic = topic
|
|
|
|
self._payload = {
|
|
|
|
'on': config.get(CONF_PAYLOAD_ON),
|
|
|
|
'off': config.get(CONF_PAYLOAD_OFF),
|
|
|
|
}
|
|
|
|
self._templates = {
|
2016-09-11 07:25:19 +00:00
|
|
|
CONF_BRIGHTNESS: config.get(CONF_BRIGHTNESS_VALUE_TEMPLATE),
|
2017-03-08 07:01:36 +00:00
|
|
|
CONF_COLOR_TEMP: config.get(CONF_COLOR_TEMP_VALUE_TEMPLATE),
|
2019-01-01 14:42:41 +00:00
|
|
|
CONF_COLOR_TEMP_COMMAND_TEMPLATE:
|
|
|
|
config.get(CONF_COLOR_TEMP_COMMAND_TEMPLATE),
|
2017-03-08 07:01:36 +00:00
|
|
|
CONF_EFFECT: config.get(CONF_EFFECT_VALUE_TEMPLATE),
|
2018-10-08 13:36:57 +00:00
|
|
|
CONF_HS: config.get(CONF_HS_VALUE_TEMPLATE),
|
2016-10-03 01:04:00 +00:00
|
|
|
CONF_RGB: config.get(CONF_RGB_VALUE_TEMPLATE),
|
2017-09-05 23:01:03 +00:00
|
|
|
CONF_RGB_COMMAND_TEMPLATE: config.get(CONF_RGB_COMMAND_TEMPLATE),
|
2017-03-08 07:01:36 +00:00
|
|
|
CONF_STATE: config.get(CONF_STATE_VALUE_TEMPLATE),
|
|
|
|
CONF_WHITE_VALUE: config.get(CONF_WHITE_VALUE_TEMPLATE),
|
|
|
|
CONF_XY: config.get(CONF_XY_VALUE_TEMPLATE),
|
2018-12-02 15:16:36 +00:00
|
|
|
}
|
2015-10-25 21:58:07 +00:00
|
|
|
|
2018-12-02 15:16:36 +00:00
|
|
|
optimistic = config.get(CONF_OPTIMISTIC)
|
2016-09-11 07:25:19 +00:00
|
|
|
self._optimistic = optimistic or topic[CONF_STATE_TOPIC] is None
|
|
|
|
self._optimistic_rgb = \
|
|
|
|
optimistic or topic[CONF_RGB_STATE_TOPIC] is None
|
|
|
|
self._optimistic_brightness = (
|
2018-10-20 16:37:25 +00:00
|
|
|
optimistic or
|
|
|
|
(topic[CONF_BRIGHTNESS_COMMAND_TOPIC] is not None and
|
|
|
|
topic[CONF_BRIGHTNESS_STATE_TOPIC] is None) or
|
|
|
|
(topic[CONF_BRIGHTNESS_COMMAND_TOPIC] is None and
|
|
|
|
topic[CONF_RGB_STATE_TOPIC] is None))
|
2016-10-03 01:04:00 +00:00
|
|
|
self._optimistic_color_temp = (
|
|
|
|
optimistic or topic[CONF_COLOR_TEMP_STATE_TOPIC] is None)
|
2017-03-08 07:01:36 +00:00
|
|
|
self._optimistic_effect = (
|
|
|
|
optimistic or topic[CONF_EFFECT_STATE_TOPIC] is None)
|
2018-10-08 13:36:57 +00:00
|
|
|
self._optimistic_hs = \
|
|
|
|
optimistic or topic[CONF_HS_STATE_TOPIC] is None
|
2017-03-08 07:01:36 +00:00
|
|
|
self._optimistic_white_value = (
|
|
|
|
optimistic or topic[CONF_WHITE_VALUE_STATE_TOPIC] is None)
|
|
|
|
self._optimistic_xy = \
|
|
|
|
optimistic or topic[CONF_XY_STATE_TOPIC] is None
|
2018-12-02 15:16:36 +00:00
|
|
|
|
|
|
|
async def _subscribe_topics(self):
|
|
|
|
"""(Re)Subscribe to topics."""
|
|
|
|
topics = {}
|
2017-02-22 08:43:22 +00:00
|
|
|
templates = {}
|
|
|
|
for key, tpl in list(self._templates.items()):
|
2016-09-28 04:29:55 +00:00
|
|
|
if tpl is None:
|
|
|
|
templates[key] = lambda value: value
|
|
|
|
else:
|
2017-02-22 08:43:22 +00:00
|
|
|
tpl.hass = self.hass
|
|
|
|
templates[key] = tpl.async_render_with_possible_json_value
|
2015-12-20 03:15:05 +00:00
|
|
|
|
2018-11-28 12:16:43 +00:00
|
|
|
last_state = await self.async_get_last_state()
|
2018-05-15 10:25:50 +00:00
|
|
|
|
2017-02-22 08:43:22 +00:00
|
|
|
@callback
|
2015-11-28 20:46:35 +00:00
|
|
|
def state_received(topic, payload, qos):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Handle new MQTT messages."""
|
2016-09-11 07:25:19 +00:00
|
|
|
payload = templates[CONF_STATE](payload)
|
2018-09-27 09:48:52 +00:00
|
|
|
if not payload:
|
|
|
|
_LOGGER.debug("Ignoring empty state message from '%s'", topic)
|
|
|
|
return
|
|
|
|
|
2016-09-11 07:25:19 +00:00
|
|
|
if payload == self._payload['on']:
|
2015-10-25 20:48:01 +00:00
|
|
|
self._state = True
|
2016-09-11 07:25:19 +00:00
|
|
|
elif payload == self._payload['off']:
|
2015-10-25 20:48:01 +00:00
|
|
|
self._state = False
|
2017-09-12 08:01:03 +00:00
|
|
|
self.async_schedule_update_ha_state()
|
2015-10-25 20:48:01 +00:00
|
|
|
|
2016-09-11 07:25:19 +00:00
|
|
|
if self._topic[CONF_STATE_TOPIC] is not None:
|
2018-12-02 15:16:36 +00:00
|
|
|
topics[CONF_STATE_TOPIC] = {
|
|
|
|
'topic': self._topic[CONF_STATE_TOPIC],
|
|
|
|
'msg_callback': state_received,
|
2018-12-05 13:46:37 +00:00
|
|
|
'qos': self._config.get(CONF_QOS)}
|
2018-05-15 10:25:50 +00:00
|
|
|
elif self._optimistic and last_state:
|
|
|
|
self._state = last_state.state == STATE_ON
|
2015-10-25 20:48:01 +00:00
|
|
|
|
2017-02-22 08:43:22 +00:00
|
|
|
@callback
|
2015-10-26 14:05:01 +00:00
|
|
|
def brightness_received(topic, payload, qos):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Handle new MQTT messages for the brightness."""
|
2018-09-27 09:48:52 +00:00
|
|
|
payload = templates[CONF_BRIGHTNESS](payload)
|
|
|
|
if not payload:
|
|
|
|
_LOGGER.debug("Ignoring empty brightness message from '%s'",
|
|
|
|
topic)
|
|
|
|
return
|
|
|
|
|
|
|
|
device_value = float(payload)
|
2018-12-05 13:46:37 +00:00
|
|
|
percent_bright = \
|
|
|
|
device_value / self._config.get(CONF_BRIGHTNESS_SCALE)
|
2018-12-27 17:18:12 +00:00
|
|
|
self._brightness = percent_bright * 255
|
2017-09-12 08:01:03 +00:00
|
|
|
self.async_schedule_update_ha_state()
|
2015-10-26 14:05:01 +00:00
|
|
|
|
2016-09-11 07:25:19 +00:00
|
|
|
if self._topic[CONF_BRIGHTNESS_STATE_TOPIC] is not None:
|
2018-12-02 15:16:36 +00:00
|
|
|
topics[CONF_BRIGHTNESS_STATE_TOPIC] = {
|
|
|
|
'topic': self._topic[CONF_BRIGHTNESS_STATE_TOPIC],
|
|
|
|
'msg_callback': brightness_received,
|
2018-12-05 13:46:37 +00:00
|
|
|
'qos': self._config.get(CONF_QOS)}
|
2015-10-31 18:26:03 +00:00
|
|
|
self._brightness = 255
|
2018-05-15 10:25:50 +00:00
|
|
|
elif self._optimistic_brightness and last_state\
|
|
|
|
and last_state.attributes.get(ATTR_BRIGHTNESS):
|
|
|
|
self._brightness = last_state.attributes.get(ATTR_BRIGHTNESS)
|
2016-09-11 07:25:19 +00:00
|
|
|
elif self._topic[CONF_BRIGHTNESS_COMMAND_TOPIC] is not None:
|
2016-03-07 06:02:35 +00:00
|
|
|
self._brightness = 255
|
2015-10-31 18:26:03 +00:00
|
|
|
else:
|
|
|
|
self._brightness = None
|
2015-10-26 14:05:01 +00:00
|
|
|
|
2017-02-22 08:43:22 +00:00
|
|
|
@callback
|
2015-11-28 20:46:35 +00:00
|
|
|
def rgb_received(topic, payload, qos):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Handle new MQTT messages for RGB."""
|
2018-09-27 09:48:52 +00:00
|
|
|
payload = templates[CONF_RGB](payload)
|
|
|
|
if not payload:
|
|
|
|
_LOGGER.debug("Ignoring empty rgb message from '%s'", topic)
|
|
|
|
return
|
|
|
|
|
|
|
|
rgb = [int(val) for val in payload.split(',')]
|
2018-03-18 22:00:29 +00:00
|
|
|
self._hs = color_util.color_RGB_to_hs(*rgb)
|
2018-10-20 16:37:25 +00:00
|
|
|
if self._topic[CONF_BRIGHTNESS_STATE_TOPIC] is None:
|
|
|
|
percent_bright = \
|
|
|
|
float(color_util.color_RGB_to_hsv(*rgb)[2]) / 100.0
|
2018-12-27 17:18:12 +00:00
|
|
|
self._brightness = percent_bright * 255
|
2017-09-12 08:01:03 +00:00
|
|
|
self.async_schedule_update_ha_state()
|
2015-11-28 20:46:35 +00:00
|
|
|
|
2016-09-11 07:25:19 +00:00
|
|
|
if self._topic[CONF_RGB_STATE_TOPIC] is not None:
|
2018-12-02 15:16:36 +00:00
|
|
|
topics[CONF_RGB_STATE_TOPIC] = {
|
|
|
|
'topic': self._topic[CONF_RGB_STATE_TOPIC],
|
|
|
|
'msg_callback': rgb_received,
|
2018-12-05 13:46:37 +00:00
|
|
|
'qos': self._config.get(CONF_QOS)}
|
2018-03-18 22:00:29 +00:00
|
|
|
self._hs = (0, 0)
|
2018-05-15 10:25:50 +00:00
|
|
|
if self._optimistic_rgb and last_state\
|
|
|
|
and last_state.attributes.get(ATTR_HS_COLOR):
|
|
|
|
self._hs = last_state.attributes.get(ATTR_HS_COLOR)
|
|
|
|
elif self._topic[CONF_RGB_COMMAND_TOPIC] is not None:
|
2018-03-18 22:00:29 +00:00
|
|
|
self._hs = (0, 0)
|
2015-10-26 14:05:01 +00:00
|
|
|
|
2017-02-22 08:43:22 +00:00
|
|
|
@callback
|
2016-10-03 01:04:00 +00:00
|
|
|
def color_temp_received(topic, payload, qos):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Handle new MQTT messages for color temperature."""
|
2018-09-27 09:48:52 +00:00
|
|
|
payload = templates[CONF_COLOR_TEMP](payload)
|
|
|
|
if not payload:
|
|
|
|
_LOGGER.debug("Ignoring empty color temp message from '%s'",
|
|
|
|
topic)
|
|
|
|
return
|
|
|
|
|
|
|
|
self._color_temp = int(payload)
|
2017-09-12 08:01:03 +00:00
|
|
|
self.async_schedule_update_ha_state()
|
2016-10-03 01:04:00 +00:00
|
|
|
|
|
|
|
if self._topic[CONF_COLOR_TEMP_STATE_TOPIC] is not None:
|
2018-12-02 15:16:36 +00:00
|
|
|
topics[CONF_COLOR_TEMP_STATE_TOPIC] = {
|
|
|
|
'topic': self._topic[CONF_COLOR_TEMP_STATE_TOPIC],
|
|
|
|
'msg_callback': color_temp_received,
|
2018-12-05 13:46:37 +00:00
|
|
|
'qos': self._config.get(CONF_QOS)}
|
2016-10-03 01:04:00 +00:00
|
|
|
self._color_temp = 150
|
2018-05-15 10:25:50 +00:00
|
|
|
if self._optimistic_color_temp and last_state\
|
|
|
|
and last_state.attributes.get(ATTR_COLOR_TEMP):
|
|
|
|
self._color_temp = last_state.attributes.get(ATTR_COLOR_TEMP)
|
|
|
|
elif self._topic[CONF_COLOR_TEMP_COMMAND_TOPIC] is not None:
|
2016-10-03 01:04:00 +00:00
|
|
|
self._color_temp = 150
|
|
|
|
else:
|
|
|
|
self._color_temp = None
|
|
|
|
|
2017-03-08 07:01:36 +00:00
|
|
|
@callback
|
|
|
|
def effect_received(topic, payload, qos):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Handle new MQTT messages for effect."""
|
2018-09-27 09:48:52 +00:00
|
|
|
payload = templates[CONF_EFFECT](payload)
|
|
|
|
if not payload:
|
|
|
|
_LOGGER.debug("Ignoring empty effect message from '%s'", topic)
|
|
|
|
return
|
|
|
|
|
|
|
|
self._effect = payload
|
2017-09-12 08:01:03 +00:00
|
|
|
self.async_schedule_update_ha_state()
|
2017-03-08 07:01:36 +00:00
|
|
|
|
|
|
|
if self._topic[CONF_EFFECT_STATE_TOPIC] is not None:
|
2018-12-02 15:16:36 +00:00
|
|
|
topics[CONF_EFFECT_STATE_TOPIC] = {
|
|
|
|
'topic': self._topic[CONF_EFFECT_STATE_TOPIC],
|
|
|
|
'msg_callback': effect_received,
|
2018-12-05 13:46:37 +00:00
|
|
|
'qos': self._config.get(CONF_QOS)}
|
2017-03-08 07:01:36 +00:00
|
|
|
self._effect = 'none'
|
2018-05-15 10:25:50 +00:00
|
|
|
if self._optimistic_effect and last_state\
|
|
|
|
and last_state.attributes.get(ATTR_EFFECT):
|
|
|
|
self._effect = last_state.attributes.get(ATTR_EFFECT)
|
|
|
|
elif self._topic[CONF_EFFECT_COMMAND_TOPIC] is not None:
|
2017-03-08 07:01:36 +00:00
|
|
|
self._effect = 'none'
|
|
|
|
else:
|
|
|
|
self._effect = None
|
|
|
|
|
2018-10-08 13:36:57 +00:00
|
|
|
@callback
|
|
|
|
def hs_received(topic, payload, qos):
|
|
|
|
"""Handle new MQTT messages for hs color."""
|
|
|
|
payload = templates[CONF_HS](payload)
|
|
|
|
if not payload:
|
|
|
|
_LOGGER.debug("Ignoring empty hs message from '%s'", topic)
|
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
|
|
|
hs_color = [float(val) for val in payload.split(',', 2)]
|
|
|
|
self._hs = hs_color
|
|
|
|
self.async_schedule_update_ha_state()
|
|
|
|
except ValueError:
|
|
|
|
_LOGGER.debug("Failed to parse hs state update: '%s'",
|
|
|
|
payload)
|
|
|
|
|
|
|
|
if self._topic[CONF_HS_STATE_TOPIC] is not None:
|
2018-12-02 15:16:36 +00:00
|
|
|
topics[CONF_HS_STATE_TOPIC] = {
|
|
|
|
'topic': self._topic[CONF_HS_STATE_TOPIC],
|
|
|
|
'msg_callback': hs_received,
|
2018-12-05 13:46:37 +00:00
|
|
|
'qos': self._config.get(CONF_QOS)}
|
2018-10-08 13:36:57 +00:00
|
|
|
self._hs = (0, 0)
|
|
|
|
if self._optimistic_hs and last_state\
|
|
|
|
and last_state.attributes.get(ATTR_HS_COLOR):
|
|
|
|
self._hs = last_state.attributes.get(ATTR_HS_COLOR)
|
|
|
|
elif self._topic[CONF_HS_COMMAND_TOPIC] is not None:
|
|
|
|
self._hs = (0, 0)
|
|
|
|
|
2017-03-08 07:01:36 +00:00
|
|
|
@callback
|
|
|
|
def white_value_received(topic, payload, qos):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Handle new MQTT messages for white value."""
|
2018-09-27 09:48:52 +00:00
|
|
|
payload = templates[CONF_WHITE_VALUE](payload)
|
|
|
|
if not payload:
|
|
|
|
_LOGGER.debug("Ignoring empty white value message from '%s'",
|
|
|
|
topic)
|
|
|
|
return
|
|
|
|
|
|
|
|
device_value = float(payload)
|
2018-12-05 13:46:37 +00:00
|
|
|
percent_white = \
|
|
|
|
device_value / self._config.get(CONF_WHITE_VALUE_SCALE)
|
2018-12-27 17:18:12 +00:00
|
|
|
self._white_value = percent_white * 255
|
2017-09-12 08:01:03 +00:00
|
|
|
self.async_schedule_update_ha_state()
|
2017-03-08 07:01:36 +00:00
|
|
|
|
|
|
|
if self._topic[CONF_WHITE_VALUE_STATE_TOPIC] is not None:
|
2018-12-02 15:16:36 +00:00
|
|
|
topics[CONF_WHITE_VALUE_STATE_TOPIC] = {
|
|
|
|
'topic': self._topic[CONF_WHITE_VALUE_STATE_TOPIC],
|
|
|
|
'msg_callback': white_value_received,
|
2018-12-05 13:46:37 +00:00
|
|
|
'qos': self._config.get(CONF_QOS)}
|
2017-03-08 07:01:36 +00:00
|
|
|
self._white_value = 255
|
2018-05-15 10:25:50 +00:00
|
|
|
elif self._optimistic_white_value and last_state\
|
|
|
|
and last_state.attributes.get(ATTR_WHITE_VALUE):
|
|
|
|
self._white_value = last_state.attributes.get(ATTR_WHITE_VALUE)
|
2017-03-08 07:01:36 +00:00
|
|
|
elif self._topic[CONF_WHITE_VALUE_COMMAND_TOPIC] is not None:
|
|
|
|
self._white_value = 255
|
|
|
|
else:
|
|
|
|
self._white_value = None
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def xy_received(topic, payload, qos):
|
2018-10-08 13:36:57 +00:00
|
|
|
"""Handle new MQTT messages for xy color."""
|
2018-09-27 09:48:52 +00:00
|
|
|
payload = templates[CONF_XY](payload)
|
|
|
|
if not payload:
|
|
|
|
_LOGGER.debug("Ignoring empty xy-color message from '%s'",
|
|
|
|
topic)
|
|
|
|
return
|
|
|
|
|
|
|
|
xy_color = [float(val) for val in payload.split(',')]
|
2018-03-18 22:00:29 +00:00
|
|
|
self._hs = color_util.color_xy_to_hs(*xy_color)
|
2017-09-12 08:01:03 +00:00
|
|
|
self.async_schedule_update_ha_state()
|
2017-03-08 07:01:36 +00:00
|
|
|
|
|
|
|
if self._topic[CONF_XY_STATE_TOPIC] is not None:
|
2018-12-02 15:16:36 +00:00
|
|
|
topics[CONF_XY_STATE_TOPIC] = {
|
|
|
|
'topic': self._topic[CONF_XY_STATE_TOPIC],
|
|
|
|
'msg_callback': xy_received,
|
2018-12-05 13:46:37 +00:00
|
|
|
'qos': self._config.get(CONF_QOS)}
|
2018-03-18 22:00:29 +00:00
|
|
|
self._hs = (0, 0)
|
2018-05-15 10:25:50 +00:00
|
|
|
if self._optimistic_xy and last_state\
|
|
|
|
and last_state.attributes.get(ATTR_HS_COLOR):
|
|
|
|
self._hs = last_state.attributes.get(ATTR_HS_COLOR)
|
|
|
|
elif self._topic[CONF_XY_COMMAND_TOPIC] is not None:
|
2018-03-18 22:00:29 +00:00
|
|
|
self._hs = (0, 0)
|
2017-03-08 07:01:36 +00:00
|
|
|
|
2018-12-02 15:16:36 +00:00
|
|
|
self._sub_state = await subscription.async_subscribe_topics(
|
|
|
|
self.hass, self._sub_state,
|
|
|
|
topics)
|
|
|
|
|
|
|
|
async def async_will_remove_from_hass(self):
|
|
|
|
"""Unsubscribe when removed."""
|
2018-12-19 13:05:24 +00:00
|
|
|
self._sub_state = await subscription.async_unsubscribe_topics(
|
|
|
|
self.hass, self._sub_state)
|
2019-01-17 18:54:22 +00:00
|
|
|
await MqttAttributes.async_will_remove_from_hass(self)
|
2018-12-02 15:16:36 +00:00
|
|
|
await MqttAvailability.async_will_remove_from_hass(self)
|
|
|
|
|
2015-10-25 20:48:01 +00:00
|
|
|
@property
|
|
|
|
def brightness(self):
|
2016-03-07 21:08:21 +00:00
|
|
|
"""Return the brightness of this light between 0..255."""
|
2018-12-27 17:18:12 +00:00
|
|
|
brightness = self._brightness
|
|
|
|
if brightness:
|
|
|
|
brightness = min(round(brightness), 255)
|
|
|
|
return brightness
|
2015-10-25 20:48:01 +00:00
|
|
|
|
|
|
|
@property
|
2018-03-18 22:00:29 +00:00
|
|
|
def hs_color(self):
|
|
|
|
"""Return the hs color value."""
|
|
|
|
return self._hs
|
2015-10-25 21:58:07 +00:00
|
|
|
|
2016-10-03 01:04:00 +00:00
|
|
|
@property
|
|
|
|
def color_temp(self):
|
|
|
|
"""Return the color temperature in mired."""
|
|
|
|
return self._color_temp
|
|
|
|
|
2017-03-08 07:01:36 +00:00
|
|
|
@property
|
|
|
|
def white_value(self):
|
|
|
|
"""Return the white property."""
|
2018-12-27 17:18:12 +00:00
|
|
|
white_value = self._white_value
|
|
|
|
if white_value:
|
|
|
|
white_value = min(round(white_value), 255)
|
|
|
|
return white_value
|
2017-03-08 07:01:36 +00:00
|
|
|
|
2015-10-31 18:26:03 +00:00
|
|
|
@property
|
|
|
|
def should_poll(self):
|
2016-02-23 20:06:50 +00:00
|
|
|
"""No polling needed for a MQTT light."""
|
2015-10-31 18:26:03 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
2016-03-07 21:08:21 +00:00
|
|
|
"""Return the name of the device if any."""
|
2018-12-05 13:46:37 +00:00
|
|
|
return self._config.get(CONF_NAME)
|
2015-10-31 18:26:03 +00:00
|
|
|
|
2018-09-01 21:37:03 +00:00
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return a unique ID."""
|
|
|
|
return self._unique_id
|
|
|
|
|
2015-10-31 18:26:03 +00:00
|
|
|
@property
|
|
|
|
def is_on(self):
|
2016-03-07 21:08:21 +00:00
|
|
|
"""Return true if device is on."""
|
2015-10-31 18:26:03 +00:00
|
|
|
return self._state
|
|
|
|
|
2016-02-21 01:17:22 +00:00
|
|
|
@property
|
|
|
|
def assumed_state(self):
|
2016-03-07 21:08:21 +00:00
|
|
|
"""Return true if we do optimistic updates."""
|
2016-02-21 01:17:22 +00:00
|
|
|
return self._optimistic
|
|
|
|
|
2017-03-08 07:01:36 +00:00
|
|
|
@property
|
|
|
|
def effect_list(self):
|
|
|
|
"""Return the list of supported effects."""
|
2018-12-05 13:46:37 +00:00
|
|
|
return self._config.get(CONF_EFFECT_LIST)
|
2017-03-08 07:01:36 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def effect(self):
|
|
|
|
"""Return the current effect."""
|
|
|
|
return self._effect
|
|
|
|
|
2016-08-16 06:07:07 +00:00
|
|
|
@property
|
|
|
|
def supported_features(self):
|
|
|
|
"""Flag supported features."""
|
2019-01-08 06:21:26 +00:00
|
|
|
supported_features = 0
|
|
|
|
supported_features |= (
|
|
|
|
self._topic[CONF_RGB_COMMAND_TOPIC] is not None and
|
|
|
|
(SUPPORT_COLOR | SUPPORT_BRIGHTNESS))
|
|
|
|
supported_features |= (
|
|
|
|
self._topic[CONF_BRIGHTNESS_COMMAND_TOPIC] is not None and
|
|
|
|
SUPPORT_BRIGHTNESS)
|
|
|
|
supported_features |= (
|
|
|
|
self._topic[CONF_COLOR_TEMP_COMMAND_TOPIC] is not None and
|
|
|
|
SUPPORT_COLOR_TEMP)
|
|
|
|
supported_features |= (
|
|
|
|
self._topic[CONF_EFFECT_COMMAND_TOPIC] is not None and
|
|
|
|
SUPPORT_EFFECT)
|
|
|
|
supported_features |= (
|
|
|
|
self._topic[CONF_HS_COMMAND_TOPIC] is not None and SUPPORT_COLOR)
|
|
|
|
supported_features |= (
|
|
|
|
self._topic[CONF_WHITE_VALUE_COMMAND_TOPIC] is not None and
|
|
|
|
SUPPORT_WHITE_VALUE)
|
|
|
|
supported_features |= (
|
|
|
|
self._topic[CONF_XY_COMMAND_TOPIC] is not None and SUPPORT_COLOR)
|
|
|
|
|
|
|
|
return supported_features
|
2016-08-16 06:07:07 +00:00
|
|
|
|
2018-05-15 10:25:50 +00:00
|
|
|
async def async_turn_on(self, **kwargs):
|
2017-02-22 08:43:22 +00:00
|
|
|
"""Turn the device on.
|
|
|
|
|
|
|
|
This method is a coroutine.
|
|
|
|
"""
|
2015-11-28 20:46:35 +00:00
|
|
|
should_update = False
|
2018-12-05 13:46:37 +00:00
|
|
|
on_command_type = self._config.get(CONF_ON_COMMAND_TYPE)
|
2015-10-25 20:48:01 +00:00
|
|
|
|
2018-12-05 13:46:37 +00:00
|
|
|
if on_command_type == 'first':
|
2017-10-31 22:18:49 +00:00
|
|
|
mqtt.async_publish(
|
|
|
|
self.hass, self._topic[CONF_COMMAND_TOPIC],
|
2018-12-05 13:46:37 +00:00
|
|
|
self._payload['on'], self._config.get(CONF_QOS),
|
|
|
|
self._config.get(CONF_RETAIN))
|
2017-10-31 22:18:49 +00:00
|
|
|
should_update = True
|
|
|
|
|
|
|
|
# If brightness is being used instead of an on command, make sure
|
|
|
|
# there is a brightness input. Either set the brightness to our
|
|
|
|
# saved value or the maximum value if this is the first call
|
2018-12-05 13:46:37 +00:00
|
|
|
elif on_command_type == 'brightness':
|
2017-10-31 22:18:49 +00:00
|
|
|
if ATTR_BRIGHTNESS not in kwargs:
|
|
|
|
kwargs[ATTR_BRIGHTNESS] = self._brightness if \
|
|
|
|
self._brightness else 255
|
|
|
|
|
2018-03-18 22:00:29 +00:00
|
|
|
if ATTR_HS_COLOR in kwargs and \
|
2016-09-11 07:25:19 +00:00
|
|
|
self._topic[CONF_RGB_COMMAND_TOPIC] is not None:
|
2016-02-26 18:33:32 +00:00
|
|
|
|
2018-03-18 22:00:29 +00:00
|
|
|
hs_color = kwargs[ATTR_HS_COLOR]
|
2018-06-21 18:59:03 +00:00
|
|
|
|
|
|
|
# If there's a brightness topic set, we don't want to scale the RGB
|
|
|
|
# values given using the brightness.
|
|
|
|
if self._topic[CONF_BRIGHTNESS_COMMAND_TOPIC] is not None:
|
|
|
|
brightness = 255
|
|
|
|
else:
|
|
|
|
brightness = kwargs.get(
|
|
|
|
ATTR_BRIGHTNESS, self._brightness if self._brightness else
|
|
|
|
255)
|
2018-03-18 22:00:29 +00:00
|
|
|
rgb = color_util.color_hsv_to_RGB(
|
|
|
|
hs_color[0], hs_color[1], brightness / 255 * 100)
|
2017-09-05 23:01:03 +00:00
|
|
|
tpl = self._templates[CONF_RGB_COMMAND_TEMPLATE]
|
|
|
|
if tpl:
|
2018-03-18 22:00:29 +00:00
|
|
|
rgb_color_str = tpl.async_render({
|
|
|
|
'red': rgb[0],
|
|
|
|
'green': rgb[1],
|
|
|
|
'blue': rgb[2],
|
|
|
|
})
|
2017-09-05 23:01:03 +00:00
|
|
|
else:
|
2018-03-18 22:00:29 +00:00
|
|
|
rgb_color_str = '{},{},{}'.format(*rgb)
|
2017-10-31 22:18:49 +00:00
|
|
|
|
2017-02-22 08:43:22 +00:00
|
|
|
mqtt.async_publish(
|
|
|
|
self.hass, self._topic[CONF_RGB_COMMAND_TOPIC],
|
2018-12-05 13:46:37 +00:00
|
|
|
rgb_color_str, self._config.get(CONF_QOS),
|
|
|
|
self._config.get(CONF_RETAIN))
|
2015-10-26 14:05:01 +00:00
|
|
|
|
2015-11-28 20:46:35 +00:00
|
|
|
if self._optimistic_rgb:
|
2018-03-18 22:00:29 +00:00
|
|
|
self._hs = kwargs[ATTR_HS_COLOR]
|
|
|
|
should_update = True
|
|
|
|
|
2018-10-08 13:36:57 +00:00
|
|
|
if ATTR_HS_COLOR in kwargs and \
|
|
|
|
self._topic[CONF_HS_COMMAND_TOPIC] is not None:
|
|
|
|
|
|
|
|
hs_color = kwargs[ATTR_HS_COLOR]
|
|
|
|
mqtt.async_publish(
|
|
|
|
self.hass, self._topic[CONF_HS_COMMAND_TOPIC],
|
2018-12-05 13:46:37 +00:00
|
|
|
'{},{}'.format(*hs_color), self._config.get(CONF_QOS),
|
|
|
|
self._config.get(CONF_RETAIN))
|
2018-10-08 13:36:57 +00:00
|
|
|
|
|
|
|
if self._optimistic_hs:
|
|
|
|
self._hs = kwargs[ATTR_HS_COLOR]
|
|
|
|
should_update = True
|
|
|
|
|
2018-03-18 22:00:29 +00:00
|
|
|
if ATTR_HS_COLOR in kwargs and \
|
|
|
|
self._topic[CONF_XY_COMMAND_TOPIC] is not None:
|
|
|
|
|
|
|
|
xy_color = color_util.color_hs_to_xy(*kwargs[ATTR_HS_COLOR])
|
|
|
|
mqtt.async_publish(
|
|
|
|
self.hass, self._topic[CONF_XY_COMMAND_TOPIC],
|
2018-12-05 13:46:37 +00:00
|
|
|
'{},{}'.format(*xy_color), self._config.get(CONF_QOS),
|
|
|
|
self._config.get(CONF_RETAIN))
|
2018-03-18 22:00:29 +00:00
|
|
|
|
|
|
|
if self._optimistic_xy:
|
|
|
|
self._hs = kwargs[ATTR_HS_COLOR]
|
2015-11-28 20:46:35 +00:00
|
|
|
should_update = True
|
|
|
|
|
2015-10-26 14:05:01 +00:00
|
|
|
if ATTR_BRIGHTNESS in kwargs and \
|
2016-09-11 07:25:19 +00:00
|
|
|
self._topic[CONF_BRIGHTNESS_COMMAND_TOPIC] is not None:
|
2016-02-26 18:33:32 +00:00
|
|
|
percent_bright = float(kwargs[ATTR_BRIGHTNESS]) / 255
|
2018-12-27 17:18:12 +00:00
|
|
|
brightness_scale = self._config.get(CONF_BRIGHTNESS_SCALE)
|
2018-12-05 13:46:37 +00:00
|
|
|
device_brightness = \
|
2018-12-27 17:18:12 +00:00
|
|
|
min(round(percent_bright * brightness_scale), brightness_scale)
|
2017-02-22 08:43:22 +00:00
|
|
|
mqtt.async_publish(
|
|
|
|
self.hass, self._topic[CONF_BRIGHTNESS_COMMAND_TOPIC],
|
2018-12-05 13:46:37 +00:00
|
|
|
device_brightness, self._config.get(CONF_QOS),
|
|
|
|
self._config.get(CONF_RETAIN))
|
2015-11-30 07:23:27 +00:00
|
|
|
|
2015-11-28 20:46:35 +00:00
|
|
|
if self._optimistic_brightness:
|
|
|
|
self._brightness = kwargs[ATTR_BRIGHTNESS]
|
|
|
|
should_update = True
|
2018-10-20 16:37:25 +00:00
|
|
|
elif ATTR_BRIGHTNESS in kwargs and ATTR_HS_COLOR not in kwargs and\
|
|
|
|
self._topic[CONF_RGB_COMMAND_TOPIC] is not None:
|
|
|
|
rgb = color_util.color_hsv_to_RGB(
|
|
|
|
self._hs[0], self._hs[1], kwargs[ATTR_BRIGHTNESS] / 255 * 100)
|
|
|
|
tpl = self._templates[CONF_RGB_COMMAND_TEMPLATE]
|
|
|
|
if tpl:
|
|
|
|
rgb_color_str = tpl.async_render({
|
|
|
|
'red': rgb[0],
|
|
|
|
'green': rgb[1],
|
|
|
|
'blue': rgb[2],
|
|
|
|
})
|
|
|
|
else:
|
|
|
|
rgb_color_str = '{},{},{}'.format(*rgb)
|
|
|
|
|
|
|
|
mqtt.async_publish(
|
|
|
|
self.hass, self._topic[CONF_RGB_COMMAND_TOPIC],
|
2018-12-05 13:46:37 +00:00
|
|
|
rgb_color_str, self._config.get(CONF_QOS),
|
|
|
|
self._config.get(CONF_RETAIN))
|
2018-10-20 16:37:25 +00:00
|
|
|
|
|
|
|
if self._optimistic_brightness:
|
|
|
|
self._brightness = kwargs[ATTR_BRIGHTNESS]
|
|
|
|
should_update = True
|
2015-10-25 20:48:01 +00:00
|
|
|
|
2016-10-03 01:04:00 +00:00
|
|
|
if ATTR_COLOR_TEMP in kwargs and \
|
|
|
|
self._topic[CONF_COLOR_TEMP_COMMAND_TOPIC] is not None:
|
|
|
|
color_temp = int(kwargs[ATTR_COLOR_TEMP])
|
2019-01-01 14:42:41 +00:00
|
|
|
tpl = self._templates[CONF_COLOR_TEMP_COMMAND_TEMPLATE]
|
|
|
|
|
|
|
|
if tpl:
|
|
|
|
color_temp = tpl.async_render({
|
|
|
|
'value': color_temp,
|
|
|
|
})
|
|
|
|
|
2017-02-22 08:43:22 +00:00
|
|
|
mqtt.async_publish(
|
|
|
|
self.hass, self._topic[CONF_COLOR_TEMP_COMMAND_TOPIC],
|
2018-12-05 13:46:37 +00:00
|
|
|
color_temp, self._config.get(CONF_QOS),
|
|
|
|
self._config.get(CONF_RETAIN))
|
2017-10-31 22:18:49 +00:00
|
|
|
|
2016-10-03 01:04:00 +00:00
|
|
|
if self._optimistic_color_temp:
|
|
|
|
self._color_temp = kwargs[ATTR_COLOR_TEMP]
|
|
|
|
should_update = True
|
|
|
|
|
2017-03-08 07:01:36 +00:00
|
|
|
if ATTR_EFFECT in kwargs and \
|
|
|
|
self._topic[CONF_EFFECT_COMMAND_TOPIC] is not None:
|
|
|
|
effect = kwargs[ATTR_EFFECT]
|
2018-12-05 13:46:37 +00:00
|
|
|
if effect in self._config.get(CONF_EFFECT_LIST):
|
2017-03-08 07:01:36 +00:00
|
|
|
mqtt.async_publish(
|
|
|
|
self.hass, self._topic[CONF_EFFECT_COMMAND_TOPIC],
|
2018-12-05 13:46:37 +00:00
|
|
|
effect, self._config.get(CONF_QOS),
|
|
|
|
self._config.get(CONF_RETAIN))
|
2017-10-31 22:18:49 +00:00
|
|
|
|
2017-03-08 07:01:36 +00:00
|
|
|
if self._optimistic_effect:
|
|
|
|
self._effect = kwargs[ATTR_EFFECT]
|
|
|
|
should_update = True
|
|
|
|
|
|
|
|
if ATTR_WHITE_VALUE in kwargs and \
|
|
|
|
self._topic[CONF_WHITE_VALUE_COMMAND_TOPIC] is not None:
|
|
|
|
percent_white = float(kwargs[ATTR_WHITE_VALUE]) / 255
|
2018-12-27 17:18:12 +00:00
|
|
|
white_scale = self._config.get(CONF_WHITE_VALUE_SCALE)
|
2018-12-05 13:46:37 +00:00
|
|
|
device_white_value = \
|
2018-12-27 17:18:12 +00:00
|
|
|
min(round(percent_white * white_scale), white_scale)
|
2017-03-08 07:01:36 +00:00
|
|
|
mqtt.async_publish(
|
|
|
|
self.hass, self._topic[CONF_WHITE_VALUE_COMMAND_TOPIC],
|
2018-12-05 13:46:37 +00:00
|
|
|
device_white_value, self._config.get(CONF_QOS),
|
|
|
|
self._config.get(CONF_RETAIN))
|
2017-03-08 07:01:36 +00:00
|
|
|
|
|
|
|
if self._optimistic_white_value:
|
|
|
|
self._white_value = kwargs[ATTR_WHITE_VALUE]
|
|
|
|
should_update = True
|
|
|
|
|
2018-12-05 13:46:37 +00:00
|
|
|
if on_command_type == 'last':
|
2017-10-31 22:18:49 +00:00
|
|
|
mqtt.async_publish(self.hass, self._topic[CONF_COMMAND_TOPIC],
|
2018-12-05 13:46:37 +00:00
|
|
|
self._payload['on'], self._config.get(CONF_QOS),
|
|
|
|
self._config.get(CONF_RETAIN))
|
2017-10-31 22:18:49 +00:00
|
|
|
should_update = True
|
2015-10-26 14:05:01 +00:00
|
|
|
|
|
|
|
if self._optimistic:
|
2018-11-27 13:00:05 +00:00
|
|
|
# Optimistically assume that the light has changed state.
|
2015-10-25 20:48:01 +00:00
|
|
|
self._state = True
|
2015-11-28 20:46:35 +00:00
|
|
|
should_update = True
|
|
|
|
|
|
|
|
if should_update:
|
2017-09-12 08:01:03 +00:00
|
|
|
self.async_schedule_update_ha_state()
|
2017-02-22 08:43:22 +00:00
|
|
|
|
2018-05-15 10:25:50 +00:00
|
|
|
async def async_turn_off(self, **kwargs):
|
2017-02-22 08:43:22 +00:00
|
|
|
"""Turn the device off.
|
2015-10-31 18:26:03 +00:00
|
|
|
|
2017-02-22 08:43:22 +00:00
|
|
|
This method is a coroutine.
|
|
|
|
"""
|
|
|
|
mqtt.async_publish(
|
|
|
|
self.hass, self._topic[CONF_COMMAND_TOPIC], self._payload['off'],
|
2018-12-05 13:46:37 +00:00
|
|
|
self._config.get(CONF_QOS), self._config.get(CONF_RETAIN))
|
2015-10-31 18:26:03 +00:00
|
|
|
|
|
|
|
if self._optimistic:
|
2018-11-27 13:00:05 +00:00
|
|
|
# Optimistically assume that the light has changed state.
|
2015-10-31 18:26:03 +00:00
|
|
|
self._state = False
|
2017-09-12 08:01:03 +00:00
|
|
|
self.async_schedule_update_ha_state()
|