core/homeassistant/components/rollershutter/mqtt.py

122 lines
3.9 KiB
Python
Raw Normal View History

"""
2016-02-23 20:06:50 +00:00
Support for MQTT roller shutters.
2015-11-29 14:52:44 +00:00
For more details about this platform, please refer to the documentation at
2015-12-02 12:18:49 +00:00
https://home-assistant.io/components/rollershutter.mqtt/
"""
import logging
2016-02-19 05:27:50 +00:00
import voluptuous as vol
import homeassistant.components.mqtt as mqtt
2015-12-02 12:18:49 +00:00
from homeassistant.components.rollershutter import RollershutterDevice
from homeassistant.const import CONF_NAME, CONF_VALUE_TEMPLATE
from homeassistant.components.mqtt import (
CONF_STATE_TOPIC, CONF_COMMAND_TOPIC, CONF_QOS)
2016-02-23 20:06:50 +00:00
from homeassistant.helpers import template
import homeassistant.helpers.config_validation as cv
_LOGGER = logging.getLogger(__name__)
DEPENDENCIES = ['mqtt']
CONF_PAYLOAD_UP = 'payload_up'
CONF_PAYLOAD_DOWN = 'payload_down'
CONF_PAYLOAD_STOP = 'payload_stop'
2015-12-02 12:18:49 +00:00
DEFAULT_NAME = "MQTT Rollershutter"
DEFAULT_PAYLOAD_UP = "UP"
DEFAULT_PAYLOAD_DOWN = "DOWN"
DEFAULT_PAYLOAD_STOP = "STOP"
PLATFORM_SCHEMA = mqtt.MQTT_RW_PLATFORM_SCHEMA.extend({
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Optional(CONF_PAYLOAD_UP, default=DEFAULT_PAYLOAD_UP): cv.string,
vol.Optional(CONF_PAYLOAD_DOWN, default=DEFAULT_PAYLOAD_DOWN): cv.string,
vol.Optional(CONF_PAYLOAD_STOP, default=DEFAULT_PAYLOAD_STOP): cv.string,
})
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
2016-02-23 20:06:50 +00:00
"""Add MQTT Rollershutter."""
2015-12-02 12:18:49 +00:00
add_devices_callback([MqttRollershutter(
hass,
config[CONF_NAME],
config.get(CONF_STATE_TOPIC),
config[CONF_COMMAND_TOPIC],
config[CONF_QOS],
config[CONF_PAYLOAD_UP],
config[CONF_PAYLOAD_DOWN],
config[CONF_PAYLOAD_STOP],
config.get(CONF_VALUE_TEMPLATE)
)])
# pylint: disable=abstract-method
# pylint: disable=too-many-arguments, too-many-instance-attributes
2015-12-02 12:18:49 +00:00
class MqttRollershutter(RollershutterDevice):
2016-03-07 22:01:34 +00:00
"""Representation of a roller shutter that can be controlled using MQTT."""
def __init__(self, hass, name, state_topic, command_topic, qos,
payload_up, payload_down, payload_stop, value_template):
2016-03-07 22:01:34 +00:00
"""Initialize the roller shutter."""
self._state = None
self._hass = hass
self._name = name
self._state_topic = state_topic
self._command_topic = command_topic
self._qos = qos
2015-12-02 12:18:49 +00:00
self._payload_up = payload_up
self._payload_down = payload_down
self._payload_stop = payload_stop
if self._state_topic is None:
return
def message_received(topic, payload, qos):
2016-02-23 20:06:50 +00:00
"""A new MQTT message has been received."""
if value_template is not None:
payload = template.render_with_possible_json_value(
hass, value_template, payload)
if payload.isnumeric() and 0 <= int(payload) <= 100:
self._state = int(payload)
self.update_ha_state()
else:
_LOGGER.warning(
"Payload is expected to be an integer between 0 and 100")
mqtt.subscribe(hass, self._state_topic, message_received, self._qos)
@property
def should_poll(self):
2016-02-23 20:06:50 +00:00
"""No polling needed."""
return False
@property
def name(self):
2016-03-07 22:01:34 +00:00
"""Return the name of the roller shutter."""
return self._name
@property
def current_position(self):
2016-03-07 22:01:34 +00:00
"""Return current position of roller shutter.
2015-11-29 14:52:44 +00:00
None is unknown, 0 is closed, 100 is fully open.
"""
return self._state
2015-12-02 12:18:49 +00:00
def move_up(self, **kwargs):
2016-03-07 22:01:34 +00:00
"""Move the roller shutter up."""
2015-12-02 12:18:49 +00:00
mqtt.publish(self.hass, self._command_topic, self._payload_up,
self._qos)
2015-12-02 12:18:49 +00:00
def move_down(self, **kwargs):
2016-03-07 22:01:34 +00:00
"""Move the roller shutter down."""
2015-12-02 12:18:49 +00:00
mqtt.publish(self.hass, self._command_topic, self._payload_down,
self._qos)
def stop(self, **kwargs):
2016-02-23 20:06:50 +00:00
"""Stop the device."""
mqtt.publish(self.hass, self._command_topic, self._payload_stop,
self._qos)