2015-11-23 00:22:43 +00:00
|
|
|
"""
|
2015-11-25 18:13:39 +00:00
|
|
|
homeassistant.components.motor.mqtt
|
2015-11-29 14:52:44 +00:00
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2015-11-25 18:13:39 +00:00
|
|
|
Allows to configure a MQTT motor.
|
2015-11-29 14:52:44 +00:00
|
|
|
|
2015-11-23 00:22:43 +00:00
|
|
|
For more details about this platform, please refer to the documentation at
|
2015-11-25 18:13:39 +00:00
|
|
|
https://home-assistant.io/components/motor.mqtt/
|
2015-11-23 00:22:43 +00:00
|
|
|
"""
|
|
|
|
import logging
|
|
|
|
import homeassistant.components.mqtt as mqtt
|
2015-11-25 18:13:39 +00:00
|
|
|
from homeassistant.components.motor import MotorDevice
|
2015-11-23 00:22:43 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
DEPENDENCIES = ['mqtt']
|
|
|
|
|
2015-11-25 18:13:39 +00:00
|
|
|
DEFAULT_NAME = "MQTT Motor"
|
2015-11-23 00:22:43 +00:00
|
|
|
DEFAULT_QOS = 0
|
2015-11-25 18:13:39 +00:00
|
|
|
DEFAULT_PAYLOAD_OPEN = "OPEN"
|
|
|
|
DEFAULT_PAYLOAD_CLOSE = "CLOSE"
|
2015-11-23 00:22:43 +00:00
|
|
|
DEFAULT_PAYLOAD_STOP = "STOP"
|
|
|
|
|
|
|
|
|
|
|
|
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
|
2015-11-25 18:13:39 +00:00
|
|
|
""" Add MQTT Motor """
|
2015-11-23 00:22:43 +00:00
|
|
|
|
|
|
|
if config.get('command_topic') is None:
|
|
|
|
_LOGGER.error("Missing required variable: command_topic")
|
|
|
|
return False
|
|
|
|
|
2015-11-25 18:13:39 +00:00
|
|
|
add_devices_callback([MqttMotor(
|
2015-11-23 00:22:43 +00:00
|
|
|
hass,
|
|
|
|
config.get('name', DEFAULT_NAME),
|
|
|
|
config.get('state_topic'),
|
|
|
|
config.get('command_topic'),
|
|
|
|
config.get('qos', DEFAULT_QOS),
|
2015-11-25 18:13:39 +00:00
|
|
|
config.get('payload_open', DEFAULT_PAYLOAD_OPEN),
|
|
|
|
config.get('payload_close', DEFAULT_PAYLOAD_CLOSE),
|
2015-11-23 00:22:43 +00:00
|
|
|
config.get('payload_stop', DEFAULT_PAYLOAD_STOP),
|
|
|
|
config.get('state_format'))])
|
|
|
|
|
|
|
|
|
|
|
|
# pylint: disable=too-many-arguments, too-many-instance-attributes
|
2015-11-25 18:13:39 +00:00
|
|
|
class MqttMotor(MotorDevice):
|
2015-11-29 14:52:44 +00:00
|
|
|
""" Represents a motor that can be controlled using MQTT. """
|
2015-11-23 00:22:43 +00:00
|
|
|
def __init__(self, hass, name, state_topic, command_topic, qos,
|
2015-11-25 18:13:39 +00:00
|
|
|
payload_open, payload_close, payload_stop, state_format):
|
2015-11-24 10:41:39 +00:00
|
|
|
self._state = None
|
2015-11-23 00:22:43 +00:00
|
|
|
self._hass = hass
|
|
|
|
self._name = name
|
|
|
|
self._state_topic = state_topic
|
|
|
|
self._command_topic = command_topic
|
|
|
|
self._qos = qos
|
2015-11-25 18:13:39 +00:00
|
|
|
self._payload_open = payload_open
|
|
|
|
self._payload_close = payload_close
|
2015-11-23 00:22:43 +00:00
|
|
|
self._payload_stop = payload_stop
|
|
|
|
self._parse = mqtt.FmtParser(state_format)
|
|
|
|
|
2015-11-24 10:41:39 +00:00
|
|
|
if self._state_topic is None:
|
|
|
|
return
|
2015-11-23 00:22:43 +00:00
|
|
|
|
2015-11-24 10:41:39 +00:00
|
|
|
def message_received(topic, payload, qos):
|
|
|
|
""" A new MQTT message has been received. """
|
|
|
|
value = self._parse(payload)
|
|
|
|
if value.isnumeric() and 0 <= int(value) <= 100:
|
|
|
|
self._state = int(value)
|
|
|
|
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)
|
2015-11-23 00:22:43 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def should_poll(self):
|
|
|
|
""" No polling needed """
|
|
|
|
return False
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
2015-11-29 14:52:44 +00:00
|
|
|
""" The name of the motor. """
|
2015-11-23 00:22:43 +00:00
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
2015-11-24 10:41:39 +00:00
|
|
|
def current_position(self):
|
2015-11-29 14:52:44 +00:00
|
|
|
"""
|
|
|
|
Return current position of motor.
|
|
|
|
None is unknown, 0 is closed, 100 is fully open.
|
|
|
|
"""
|
2015-11-24 10:41:39 +00:00
|
|
|
return self._state
|
2015-11-23 00:22:43 +00:00
|
|
|
|
2015-11-25 18:13:39 +00:00
|
|
|
def open(self, **kwargs):
|
2015-11-26 09:38:25 +00:00
|
|
|
""" Open the device. """
|
2015-11-25 18:13:39 +00:00
|
|
|
mqtt.publish(self.hass, self._command_topic, self._payload_open,
|
2015-11-23 00:22:43 +00:00
|
|
|
self._qos)
|
|
|
|
|
2015-11-25 18:13:39 +00:00
|
|
|
def close(self, **kwargs):
|
2015-11-26 09:38:25 +00:00
|
|
|
""" Close the device. """
|
2015-11-25 18:13:39 +00:00
|
|
|
mqtt.publish(self.hass, self._command_topic, self._payload_close,
|
2015-11-23 00:22:43 +00:00
|
|
|
self._qos)
|
|
|
|
|
2015-11-25 18:13:39 +00:00
|
|
|
def stop(self, **kwargs):
|
|
|
|
""" Stop the device. """
|
2015-11-23 00:22:43 +00:00
|
|
|
mqtt.publish(self.hass, self._command_topic, self._payload_stop,
|
|
|
|
self._qos)
|