* Add effect support to MQTT Light
* Use effect state topic for supported_features
* Dont use rainbow as default color
* Add color_temp support to MQTT JSON Light
* Add effect to MQTT JSON light
* Support lights in MQTT discovery
* Allow discovered devices to set their platform
* Add white value support to MQTT Light
* Add white value support to MQTT JSON Light
* Remove blank line
* Add color_temp support to MQTT Template light
* Add white value support to MQTT Template Light
* Remove unused SUPPORT_MQTT_TEMPLATE and stale unused flash and transition code from MQTT Template
* Add XY Color to MQTT Light Platform
* Fix syntax
* Fix more syntax errors
* Revert "Remove unused SUPPORT_MQTT_TEMPLATE and stale unused flash and transition code from MQTT Template"
This reverts commit c03798cb63
.
* MQTT Template supports flash and transition but doesnt allow templating of the values
* Add XY color support to MQTT JSON
* Proper variable names
* Only allow whitelisted MQTT platforms to be loaded via MQTT Discovery
* Minor tweaks.
74 lines
2.2 KiB
Python
74 lines
2.2 KiB
Python
"""
|
|
Support for MQTT discovery.
|
|
|
|
For more details about this component, please refer to the documentation at
|
|
https://home-assistant.io/components/mqtt/#discovery
|
|
"""
|
|
import asyncio
|
|
import json
|
|
import logging
|
|
import re
|
|
|
|
import homeassistant.components.mqtt as mqtt
|
|
from homeassistant.components.mqtt import DOMAIN
|
|
from homeassistant.helpers.discovery import async_load_platform
|
|
from homeassistant.const import CONF_PLATFORM
|
|
from homeassistant.components.mqtt import CONF_STATE_TOPIC
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
TOPIC_MATCHER = re.compile(
|
|
r'(?P<prefix_topic>\w+)/(?P<component>\w+)/(?P<object_id>\w+)/config')
|
|
|
|
SUPPORTED_COMPONENTS = ['binary_sensor', 'light', 'sensor']
|
|
|
|
ALLOWED_PLATFORMS = {
|
|
'binary_sensor': ['mqtt'],
|
|
'light': ['mqtt', 'mqtt_json', 'mqtt_template'],
|
|
'sensor': ['mqtt']
|
|
}
|
|
|
|
|
|
@asyncio.coroutine
|
|
def async_start(hass, discovery_topic, hass_config):
|
|
"""Initialization of MQTT Discovery."""
|
|
# pylint: disable=unused-variable
|
|
@asyncio.coroutine
|
|
def async_device_message_received(topic, payload, qos):
|
|
"""Process the received message."""
|
|
match = TOPIC_MATCHER.match(topic)
|
|
|
|
if not match:
|
|
return
|
|
|
|
prefix_topic, component, object_id = match.groups()
|
|
|
|
try:
|
|
payload = json.loads(payload)
|
|
except ValueError:
|
|
_LOGGER.warning("Unable to parse JSON %s: %s", object_id, payload)
|
|
return
|
|
|
|
if component not in SUPPORTED_COMPONENTS:
|
|
_LOGGER.warning("Component %s is not supported", component)
|
|
return
|
|
|
|
payload = dict(payload)
|
|
platform = payload.get(CONF_PLATFORM, 'mqtt')
|
|
if platform not in ALLOWED_PLATFORMS.get(component, []):
|
|
_LOGGER.warning("Platform %s (component %s) is not allowed",
|
|
platform, component)
|
|
return
|
|
|
|
payload[CONF_PLATFORM] = platform
|
|
if CONF_STATE_TOPIC not in payload:
|
|
payload[CONF_STATE_TOPIC] = '{}/{}/{}/state'.format(
|
|
discovery_topic, component, object_id)
|
|
|
|
yield from async_load_platform(
|
|
hass, component, DOMAIN, payload, hass_config)
|
|
|
|
yield from mqtt.async_subscribe(
|
|
hass, discovery_topic + '/#', async_device_message_received, 0)
|
|
|
|
return True
|