Convert mqtt platforms to async (#6145)

* Convert mqtt platforms to async

* fix lint

* add more platforms

* convert mqtt_eventstream

* fix lint / add mqtt_room

* fix lint

* fix test part 1

* fix test part 2

* fix out of memory bug

* address comments
This commit is contained in:
Pascal Vizeli authored and GitHub committed 2017-02-22 09:43:22 +01:00
1 parent 65ed85c6eb
commit b0d3bbed79
15 files changed
+509 -291

No files matched your search

+69 -40
View File
@@ -4,10 +4,12 @@ Support for MQTT lights.
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/light.mqtt/
"""
import asyncio
import logging
import voluptuous as vol
from homeassistant.core import callback
import homeassistant.components.mqtt as mqtt
from homeassistant.components.light import (
ATTR_BRIGHTNESS, ATTR_RGB_COLOR, ATTR_COLOR_TEMP, SUPPORT_BRIGHTNESS,
@@ -62,12 +64,13 @@ PLATFORM_SCHEMA = mqtt.MQTT_RW_PLATFORM_SCHEMA.extend({
})
def setup_platform(hass, config, add_devices, discovery_info=None):
@asyncio.coroutine
def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
"""Add MQTT Light."""
config.setdefault(CONF_STATE_VALUE_TEMPLATE,
config.get(CONF_VALUE_TEMPLATE))
add_devices([MqttLight(
hass,
config.setdefault(
CONF_STATE_VALUE_TEMPLATE, config.get(CONF_VALUE_TEMPLATE))
yield from async_add_devices([MqttLight(
config.get(CONF_NAME),
{
key: config.get(key) for key in (
@@ -101,15 +104,15 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
class MqttLight(Light):
"""MQTT light."""
def __init__(self, hass, name, topic, templates, qos, retain, payload,
def __init__(self, name, topic, templates, qos, retain, payload,
optimistic, brightness_scale):
"""Initialize MQTT light."""
self._hass = hass
self._name = name
self._topic = topic
self._qos = qos
self._retain = retain
self._payload = payload
self._templates = templates
self._optimistic = optimistic or topic[CONF_STATE_TOPIC] is None
self._optimistic_rgb = \
optimistic or topic[CONF_RGB_STATE_TOPIC] is None
@@ -119,6 +122,9 @@ class MqttLight(Light):
optimistic or topic[CONF_COLOR_TEMP_STATE_TOPIC] is None)
self._brightness_scale = brightness_scale
self._state = False
self._brightness = None
self._rgb = None
self._color_temp = None
self._supported_features = 0
self._supported_features |= (
topic[CONF_RGB_COMMAND_TOPIC] is not None and SUPPORT_RGB_COLOR)
@@ -129,13 +135,21 @@ class MqttLight(Light):
topic[CONF_COLOR_TEMP_COMMAND_TOPIC] is not None and
SUPPORT_COLOR_TEMP)
for key, tpl in list(templates.items()):
@asyncio.coroutine
def async_added_to_hass(self):
"""Subscribe mqtt events.
This method is a coroutine.
"""
templates = {}
for key, tpl in list(self._templates.items()):
if tpl is None:
templates[key] = lambda value: value
else:
tpl.hass = hass
templates[key] = tpl.render_with_possible_json_value
tpl.hass = self.hass
templates[key] = tpl.async_render_with_possible_json_value
@callback
def state_received(topic, payload, qos):
"""A new MQTT message has been received."""
payload = templates[CONF_STATE](payload)
@@ -143,23 +157,24 @@ class MqttLight(Light):
self._state = True
elif payload == self._payload['off']:
self._state = False
self.update_ha_state()
self.hass.async_add_job(self.async_update_ha_state())
if self._topic[CONF_STATE_TOPIC] is not None:
mqtt.subscribe(self._hass, self._topic[CONF_STATE_TOPIC],
state_received, self._qos)
yield from mqtt.async_subscribe(
self.hass, self._topic[CONF_STATE_TOPIC], state_received,
self._qos)
@callback
def brightness_received(topic, payload, qos):
"""A new MQTT message for the brightness has been received."""
device_value = float(templates[CONF_BRIGHTNESS](payload))
percent_bright = device_value / self._brightness_scale
self._brightness = int(percent_bright * 255)
self.update_ha_state()
self.hass.async_add_job(self.async_update_ha_state())
if self._topic[CONF_BRIGHTNESS_STATE_TOPIC] is not None:
mqtt.subscribe(
self._hass, self._topic[CONF_BRIGHTNESS_STATE_TOPIC],
yield from mqtt.async_subscribe(
self.hass, self._topic[CONF_BRIGHTNESS_STATE_TOPIC],
brightness_received, self._qos)
self._brightness = 255
elif self._topic[CONF_BRIGHTNESS_COMMAND_TOPIC] is not None:
@@ -167,29 +182,32 @@ class MqttLight(Light):
else:
self._brightness = None
@callback
def rgb_received(topic, payload, qos):
"""A new MQTT message has been received."""
self._rgb = [int(val) for val in
templates[CONF_RGB](payload).split(',')]
self.update_ha_state()
self.hass.async_add_job(self.async_update_ha_state())
if self._topic[CONF_RGB_STATE_TOPIC] is not None:
mqtt.subscribe(self._hass, self._topic[CONF_RGB_STATE_TOPIC],
rgb_received, self._qos)
yield from mqtt.async_subscribe(
self.hass, self._topic[CONF_RGB_STATE_TOPIC], rgb_received,
self._qos)
self._rgb = [255, 255, 255]
if self._topic[CONF_RGB_COMMAND_TOPIC] is not None:
self._rgb = [255, 255, 255]
else:
self._rgb = None
@callback
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()
self.hass.async_add_job(self.async_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],
yield from mqtt.async_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:
@@ -237,16 +255,21 @@ class MqttLight(Light):
"""Flag supported features."""
return self._supported_features
def turn_on(self, **kwargs):
"""Turn the device on."""
@asyncio.coroutine
def async_turn_on(self, **kwargs):
"""Turn the device on.
This method is a coroutine.
"""
should_update = False
if ATTR_RGB_COLOR in kwargs and \
self._topic[CONF_RGB_COMMAND_TOPIC] is not None:
mqtt.publish(self._hass, self._topic[CONF_RGB_COMMAND_TOPIC],
'{},{},{}'.format(*kwargs[ATTR_RGB_COLOR]),
self._qos, self._retain)
mqtt.async_publish(
self.hass, self._topic[CONF_RGB_COMMAND_TOPIC],
'{},{},{}'.format(*kwargs[ATTR_RGB_COLOR]), self._qos,
self._retain)
if self._optimistic_rgb:
self._rgb = kwargs[ATTR_RGB_COLOR]
@@ -256,8 +279,8 @@ class MqttLight(Light):
self._topic[CONF_BRIGHTNESS_COMMAND_TOPIC] is not None:
percent_bright = float(kwargs[ATTR_BRIGHTNESS]) / 255
device_brightness = int(percent_bright * self._brightness_scale)
mqtt.publish(
self._hass, self._topic[CONF_BRIGHTNESS_COMMAND_TOPIC],
mqtt.async_publish(
self.hass, self._topic[CONF_BRIGHTNESS_COMMAND_TOPIC],
device_brightness, self._qos, self._retain)
if self._optimistic_brightness:
@@ -267,15 +290,16 @@ class MqttLight(Light):
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],
mqtt.async_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],
self._payload['on'], self._qos, self._retain)
mqtt.async_publish(
self.hass, self._topic[CONF_COMMAND_TOPIC], self._payload['on'],
self._qos, self._retain)
if self._optimistic:
# Optimistically assume that switch has changed state.
@@ -283,14 +307,19 @@ class MqttLight(Light):
should_update = True
if should_update:
self.schedule_update_ha_state()
self.hass.async_add_job(self.async_update_ha_state())
def turn_off(self, **kwargs):
"""Turn the device off."""
mqtt.publish(self._hass, self._topic[CONF_COMMAND_TOPIC],
self._payload['off'], self._qos, self._retain)
@asyncio.coroutine
def async_turn_off(self, **kwargs):
"""Turn the device off.
This method is a coroutine.
"""
mqtt.async_publish(
self.hass, self._topic[CONF_COMMAND_TOPIC], self._payload['off'],
self._qos, self._retain)
if self._optimistic:
# Optimistically assume that switch has changed state.
self._state = False
self.schedule_update_ha_state()
self.hass.async_add_job(self.async_update_ha_state())
+38 -20
View File
@@ -4,11 +4,12 @@ Support for MQTT JSON lights.
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/light.mqtt_json/
"""
import asyncio
import logging
import json
import voluptuous as vol
from homeassistant.core import callback
import homeassistant.components.mqtt as mqtt
from homeassistant.components.light import (
ATTR_BRIGHTNESS, ATTR_RGB_COLOR, ATTR_TRANSITION, PLATFORM_SCHEMA,
@@ -57,10 +58,10 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
})
def setup_platform(hass, config, add_devices, discovery_info=None):
@asyncio.coroutine
def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
"""Setup a MQTT JSON Light."""
add_devices([MqttJson(
hass,
yield from async_add_devices([MqttJson(
config.get(CONF_NAME),
{
key: config.get(key) for key in (
@@ -85,10 +86,9 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
class MqttJson(Light):
"""Representation of a MQTT JSON light."""
def __init__(self, hass, name, topic, qos, retain,
optimistic, brightness, rgb, flash_times):
def __init__(self, name, topic, qos, retain, optimistic, brightness, rgb,
flash_times):
"""Initialize MQTT JSON light."""
self._hass = hass
self._name = name
self._topic = topic
self._qos = qos
@@ -107,6 +107,13 @@ class MqttJson(Light):
self._flash_times = flash_times
@asyncio.coroutine
def async_added_to_hass(self):
"""Subscribe mqtt events.
This method is a coroutine.
"""
@callback
def state_received(topic, payload, qos):
"""A new MQTT message has been received."""
values = json.loads(payload)
@@ -136,11 +143,12 @@ class MqttJson(Light):
except ValueError:
_LOGGER.warning('Invalid brightness value received')
self.update_ha_state()
self.hass.async_add_job(self.async_update_ha_state())
if self._topic[CONF_STATE_TOPIC] is not None:
mqtt.subscribe(self._hass, self._topic[CONF_STATE_TOPIC],
state_received, self._qos)
yield from mqtt.async_subscribe(
self.hass, self._topic[CONF_STATE_TOPIC], state_received,
self._qos)
@property
def brightness(self):
@@ -177,8 +185,12 @@ class MqttJson(Light):
"""Flag supported features."""
return SUPPORT_MQTT_JSON
def turn_on(self, **kwargs):
"""Turn the device on."""
@asyncio.coroutine
def async_turn_on(self, **kwargs):
"""Turn the device on.
This method is a coroutine.
"""
should_update = False
message = {'state': 'ON'}
@@ -212,8 +224,9 @@ class MqttJson(Light):
self._brightness = kwargs[ATTR_BRIGHTNESS]
should_update = True
mqtt.publish(self._hass, self._topic[CONF_COMMAND_TOPIC],
json.dumps(message), self._qos, self._retain)
mqtt.async_publish(
self.hass, self._topic[CONF_COMMAND_TOPIC], json.dumps(message),
self._qos, self._retain)
if self._optimistic:
# Optimistically assume that the light has changed state.
@@ -221,19 +234,24 @@ class MqttJson(Light):
should_update = True
if should_update:
self.schedule_update_ha_state()
self.hass.async_add_job(self.async_update_ha_state())
def turn_off(self, **kwargs):
"""Turn the device off."""
@asyncio.coroutine
def async_turn_off(self, **kwargs):
"""Turn the device off.
This method is a coroutine.
"""
message = {'state': 'OFF'}
if ATTR_TRANSITION in kwargs:
message['transition'] = kwargs[ATTR_TRANSITION]
mqtt.publish(self._hass, self._topic[CONF_COMMAND_TOPIC],
json.dumps(message), self._qos, self._retain)
mqtt.async_publish(
self.hass, self._topic[CONF_COMMAND_TOPIC], json.dumps(message),
self._qos, self._retain)
if self._optimistic:
# Optimistically assume that the light has changed state.
self._state = False
self.schedule_update_ha_state()
self.hass.async_add_job(self.async_update_ha_state())
+47 -28
View File
@@ -4,10 +4,11 @@ Support for MQTT Template lights.
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/light.mqtt_template/
"""
import asyncio
import logging
import voluptuous as vol
from homeassistant.core import callback
import homeassistant.components.mqtt as mqtt
from homeassistant.components.light import (
ATTR_BRIGHTNESS, ATTR_EFFECT, ATTR_FLASH, ATTR_RGB_COLOR, ATTR_TRANSITION,
@@ -60,9 +61,10 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
})
def setup_platform(hass, config, add_devices, discovery_info=None):
@asyncio.coroutine
def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
"""Setup a MQTT Template light."""
add_devices([MqttTemplate(
yield from async_add_devices([MqttTemplate(
hass,
config.get(CONF_NAME),
config.get(CONF_EFFECT_LIST),
@@ -96,14 +98,10 @@ class MqttTemplate(Light):
def __init__(self, hass, name, effect_list, topics, templates, optimistic,
qos, retain):
"""Initialize MQTT Template light."""
self._hass = hass
self._name = name
self._effect_list = effect_list
self._topics = topics
self._templates = templates
for tpl in self._templates.values():
if tpl is not None:
tpl.hass = hass
self._optimistic = optimistic or topics[CONF_STATE_TOPIC] is None \
or templates[CONF_STATE_TEMPLATE] is None
self._qos = qos
@@ -124,11 +122,23 @@ class MqttTemplate(Light):
self._rgb = None
self._effect = None
# init hass to template
for tpl in self._templates.values():
if tpl is not None:
tpl.hass = hass
@asyncio.coroutine
def async_added_to_hass(self):
"""Subscribe mqtt events.
This method is a coroutine.
"""
@callback
def state_received(topic, payload, qos):
"""A new MQTT message has been received."""
# read state
state = self._templates[CONF_STATE_TEMPLATE].\
render_with_possible_json_value(payload)
async_render_with_possible_json_value(payload)
if state == STATE_ON:
self._state = True
elif state == STATE_OFF:
@@ -141,7 +151,7 @@ class MqttTemplate(Light):
try:
self._brightness = int(
self._templates[CONF_BRIGHTNESS_TEMPLATE].
render_with_possible_json_value(payload)
async_render_with_possible_json_value(payload)
)
except ValueError:
_LOGGER.warning('Invalid brightness value received')
@@ -151,20 +161,20 @@ class MqttTemplate(Light):
try:
self._rgb[0] = int(
self._templates[CONF_RED_TEMPLATE].
render_with_possible_json_value(payload))
async_render_with_possible_json_value(payload))
self._rgb[1] = int(
self._templates[CONF_GREEN_TEMPLATE].
render_with_possible_json_value(payload))
async_render_with_possible_json_value(payload))
self._rgb[2] = int(
self._templates[CONF_BLUE_TEMPLATE].
render_with_possible_json_value(payload))
async_render_with_possible_json_value(payload))
except ValueError:
_LOGGER.warning('Invalid color value received')
# read effect
if self._templates[CONF_EFFECT_TEMPLATE] is not None:
effect = self._templates[CONF_EFFECT_TEMPLATE].\
render_with_possible_json_value(payload)
async_render_with_possible_json_value(payload)
# validate effect value
if effect in self._effect_list:
@@ -172,11 +182,12 @@ class MqttTemplate(Light):
else:
_LOGGER.warning('Unsupported effect value received')
self.update_ha_state()
self.hass.async_add_job(self.async_update_ha_state())
if self._topics[CONF_STATE_TOPIC] is not None:
mqtt.subscribe(self._hass, self._topics[CONF_STATE_TOPIC],
state_received, self._qos)
yield from mqtt.async_subscribe(
self.hass, self._topics[CONF_STATE_TOPIC], state_received,
self._qos)
@property
def brightness(self):
@@ -221,8 +232,12 @@ class MqttTemplate(Light):
"""Return the current effect."""
return self._effect
def turn_on(self, **kwargs):
"""Turn the entity on."""
@asyncio.coroutine
def async_turn_on(self, **kwargs):
"""Turn the entity on.
This method is a coroutine.
"""
# state
values = {'state': True}
if self._optimistic:
@@ -256,17 +271,21 @@ class MqttTemplate(Light):
if ATTR_TRANSITION in kwargs:
values['transition'] = kwargs[ATTR_TRANSITION]
mqtt.publish(
self._hass, self._topics[CONF_COMMAND_TOPIC],
self._templates[CONF_COMMAND_ON_TEMPLATE].render(**values),
mqtt.async_publish(
self.hass, self._topics[CONF_COMMAND_TOPIC],
self._templates[CONF_COMMAND_ON_TEMPLATE].async_render(**values),
self._qos, self._retain
)
if self._optimistic:
self.schedule_update_ha_state()
self.hass.async_add_job(self.async_update_ha_state())
def turn_off(self, **kwargs):
"""Turn the entity off."""
@asyncio.coroutine
def async_turn_off(self, **kwargs):
"""Turn the entity off.
This method is a coroutine.
"""
# state
values = {'state': False}
if self._optimistic:
@@ -276,14 +295,14 @@ class MqttTemplate(Light):
if ATTR_TRANSITION in kwargs:
values['transition'] = kwargs[ATTR_TRANSITION]
mqtt.publish(
self._hass, self._topics[CONF_COMMAND_TOPIC],
self._templates[CONF_COMMAND_OFF_TEMPLATE].render(**values),
mqtt.async_publish(
self.hass, self._topics[CONF_COMMAND_TOPIC],
self._templates[CONF_COMMAND_OFF_TEMPLATE].async_render(**values),
self._qos, self._retain
)
if self._optimistic:
self.schedule_update_ha_state()
self.hass.async_add_job(self.async_update_ha_state())
@property
def supported_features(self):