core/homeassistant/components/switch/mqtt.py

131 lines
4.2 KiB
Python
Raw Normal View History

2015-08-19 00:24:40 +00:00
"""
2016-02-23 20:06:50 +00:00
Support for MQTT switches.
2015-08-19 00:24:40 +00:00
2015-10-21 17:43:24 +00:00
For more details about this platform, please refer to the documentation at
2015-11-09 12:12:18 +00:00
https://home-assistant.io/components/switch.mqtt/
2015-08-19 00:24:40 +00:00
"""
import logging
2016-02-19 05:27:50 +00:00
import voluptuous as vol
from homeassistant.components.mqtt import (
CONF_STATE_TOPIC, CONF_COMMAND_TOPIC, CONF_QOS, CONF_RETAIN)
from homeassistant.components.switch import SwitchDevice
from homeassistant.const import (
CONF_NAME, CONF_OPTIMISTIC, CONF_VALUE_TEMPLATE, CONF_PAYLOAD_OFF,
CONF_PAYLOAD_ON)
import homeassistant.components.mqtt as mqtt
import homeassistant.helpers.config_validation as cv
2015-08-19 00:24:40 +00:00
_LOGGER = logging.getLogger(__name__)
DEPENDENCIES = ['mqtt']
DEFAULT_NAME = 'MQTT Switch'
DEFAULT_PAYLOAD_ON = 'ON'
DEFAULT_PAYLOAD_OFF = 'OFF'
2015-08-19 00:24:40 +00:00
DEFAULT_OPTIMISTIC = False
PLATFORM_SCHEMA = mqtt.MQTT_RW_PLATFORM_SCHEMA.extend({
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Optional(CONF_PAYLOAD_ON, default=DEFAULT_PAYLOAD_ON): cv.string,
vol.Optional(CONF_PAYLOAD_OFF, default=DEFAULT_PAYLOAD_OFF): cv.string,
vol.Optional(CONF_OPTIMISTIC, default=DEFAULT_OPTIMISTIC): cv.boolean,
})
2015-08-19 00:24:40 +00:00
2015-08-22 14:10:49 +00:00
2015-08-19 00:24:40 +00:00
# pylint: disable=unused-argument
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Setup the MQTT switch."""
value_template = config.get(CONF_VALUE_TEMPLATE)
if value_template is not None:
value_template.hass = hass
add_devices([MqttSwitch(
2015-08-19 00:24:40 +00:00
hass,
config.get(CONF_NAME),
config.get(CONF_STATE_TOPIC),
config.get(CONF_COMMAND_TOPIC),
config.get(CONF_QOS),
config.get(CONF_RETAIN),
config.get(CONF_PAYLOAD_ON),
config.get(CONF_PAYLOAD_OFF),
config.get(CONF_OPTIMISTIC),
value_template,
)])
2015-08-19 00:24:40 +00:00
2015-08-22 14:10:49 +00:00
2015-08-22 15:10:29 +00:00
# pylint: disable=too-many-arguments, too-many-instance-attributes
2015-08-19 00:24:40 +00:00
class MqttSwitch(SwitchDevice):
2016-03-08 12:35:39 +00:00
"""Representation of a switch that can be toggled using MQTT."""
def __init__(self, hass, name, state_topic, command_topic, qos, retain,
payload_on, payload_off, optimistic, value_template):
2016-03-08 12:35:39 +00:00
"""Initialize the MQTT switch."""
2015-08-22 14:10:49 +00:00
self._state = False
2015-08-19 00:24:40 +00:00
self._hass = hass
self._name = name
self._state_topic = state_topic
self._command_topic = command_topic
self._qos = qos
self._retain = retain
2015-08-19 00:24:40 +00:00
self._payload_on = payload_on
self._payload_off = payload_off
self._optimistic = optimistic
2015-08-22 14:10:49 +00:00
2015-08-19 00:24:40 +00:00
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 = value_template.render_with_possible_json_value(
payload)
2015-08-22 14:10:49 +00:00
if payload == self._payload_on:
2015-08-19 00:24:40 +00:00
self._state = True
self.update_ha_state()
2015-08-22 14:10:49 +00:00
elif payload == self._payload_off:
2015-08-19 00:24:40 +00:00
self._state = False
self.update_ha_state()
if self._state_topic is None:
2016-02-23 20:06:50 +00:00
# Force into optimistic mode.
2015-08-19 00:24:40 +00:00
self._optimistic = True
else:
mqtt.subscribe(
hass, self._state_topic, message_received, self._qos)
2015-08-19 00:24:40 +00:00
@property
def should_poll(self):
2016-02-23 20:06:50 +00:00
"""No polling needed."""
2015-08-19 00:24:40 +00:00
return False
@property
def name(self):
2016-03-08 12:35:39 +00:00
"""Return the name of the switch."""
2015-08-19 00:24:40 +00:00
return self._name
@property
def is_on(self):
2016-03-08 12:35:39 +00:00
"""Return true if device is on."""
2015-08-19 00:24:40 +00:00
return self._state
@property
def assumed_state(self):
2016-03-08 12:35:39 +00:00
"""Return true if we do optimistic updates."""
return self._optimistic
2015-08-19 00:24:40 +00:00
def turn_on(self, **kwargs):
2016-02-23 20:06:50 +00:00
"""Turn the device on."""
mqtt.publish(self.hass, self._command_topic, self._payload_on,
self._qos, self._retain)
2015-08-19 00:24:40 +00:00
if self._optimistic:
2016-02-23 20:06:50 +00:00
# Optimistically assume that switch has changed state.
2015-08-19 00:24:40 +00:00
self._state = True
self.update_ha_state()
def turn_off(self, **kwargs):
2016-02-23 20:06:50 +00:00
"""Turn the device off."""
mqtt.publish(self.hass, self._command_topic, self._payload_off,
self._qos, self._retain)
2015-08-19 00:24:40 +00:00
if self._optimistic:
2016-02-23 20:06:50 +00:00
# Optimistically assume that switch has changed state.
2015-08-19 00:24:40 +00:00
self._state = False
self.update_ha_state()