2015-11-20 22:43:59 +00:00
|
|
|
"""
|
2016-03-07 19:21:08 +00:00
|
|
|
Support for MQTT binary sensors.
|
2015-11-20 22:43:59 +00:00
|
|
|
|
|
|
|
For more details about this platform, please refer to the documentation at
|
|
|
|
https://home-assistant.io/components/binary_sensor.mqtt/
|
|
|
|
"""
|
2017-02-22 08:43:22 +00:00
|
|
|
import asyncio
|
2015-11-20 22:43:59 +00:00
|
|
|
import logging
|
2018-06-13 14:20:38 +00:00
|
|
|
from typing import Optional
|
2015-12-14 20:38:56 +00:00
|
|
|
|
2016-04-05 04:53:58 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2016-11-17 15:34:46 +00:00
|
|
|
from homeassistant.core import callback
|
2016-02-19 05:27:50 +00:00
|
|
|
import homeassistant.components.mqtt as mqtt
|
2016-09-09 00:32:32 +00:00
|
|
|
from homeassistant.components.binary_sensor import (
|
2017-02-11 04:46:15 +00:00
|
|
|
BinarySensorDevice, DEVICE_CLASSES_SCHEMA)
|
2016-09-09 00:32:32 +00:00
|
|
|
from homeassistant.const import (
|
2018-02-08 11:28:12 +00:00
|
|
|
CONF_FORCE_UPDATE, CONF_NAME, CONF_VALUE_TEMPLATE, CONF_PAYLOAD_ON,
|
|
|
|
CONF_PAYLOAD_OFF, CONF_DEVICE_CLASS)
|
2017-09-21 15:02:11 +00:00
|
|
|
from homeassistant.components.mqtt import (
|
2018-01-02 02:32:29 +00:00
|
|
|
CONF_STATE_TOPIC, CONF_AVAILABILITY_TOPIC, CONF_PAYLOAD_AVAILABLE,
|
|
|
|
CONF_PAYLOAD_NOT_AVAILABLE, CONF_QOS, MqttAvailability)
|
2016-04-05 04:53:58 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2015-11-20 22:43:59 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
DEFAULT_NAME = 'MQTT Binary sensor'
|
2018-06-13 14:20:38 +00:00
|
|
|
CONF_UNIQUE_ID = 'unique_id'
|
2015-11-20 22:43:59 +00:00
|
|
|
DEFAULT_PAYLOAD_OFF = 'OFF'
|
2016-09-09 00:32:32 +00:00
|
|
|
DEFAULT_PAYLOAD_ON = 'ON'
|
2018-02-08 11:28:12 +00:00
|
|
|
DEFAULT_FORCE_UPDATE = False
|
2017-09-21 15:02:11 +00:00
|
|
|
|
2016-09-09 00:32:32 +00:00
|
|
|
DEPENDENCIES = ['mqtt']
|
2015-11-20 22:43:59 +00:00
|
|
|
|
2016-04-07 01:35:46 +00:00
|
|
|
PLATFORM_SCHEMA = mqtt.MQTT_RO_PLATFORM_SCHEMA.extend({
|
2016-04-05 04:53:58 +00:00
|
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
2016-09-09 00:32:32 +00:00
|
|
|
vol.Optional(CONF_PAYLOAD_OFF, default=DEFAULT_PAYLOAD_OFF): cv.string,
|
|
|
|
vol.Optional(CONF_PAYLOAD_ON, default=DEFAULT_PAYLOAD_ON): cv.string,
|
2017-02-11 04:46:15 +00:00
|
|
|
vol.Optional(CONF_DEVICE_CLASS): DEVICE_CLASSES_SCHEMA,
|
2018-02-08 11:28:12 +00:00
|
|
|
vol.Optional(CONF_FORCE_UPDATE, default=DEFAULT_FORCE_UPDATE): cv.boolean,
|
2018-06-13 14:20:38 +00:00
|
|
|
# Integrations shouldn't never expose unique_id through configuration
|
|
|
|
# this here is an exception because MQTT is a msg transport, not a protocol
|
|
|
|
vol.Optional(CONF_UNIQUE_ID): cv.string,
|
2018-01-02 02:32:29 +00:00
|
|
|
}).extend(mqtt.MQTT_AVAILABILITY_SCHEMA.schema)
|
2015-11-20 22:43:59 +00:00
|
|
|
|
|
|
|
|
2017-02-22 08:43:22 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
|
2017-02-07 17:13:24 +00:00
|
|
|
"""Set up the MQTT binary sensor."""
|
|
|
|
if discovery_info is not None:
|
|
|
|
config = PLATFORM_SCHEMA(discovery_info)
|
|
|
|
|
2016-09-25 20:33:01 +00:00
|
|
|
value_template = config.get(CONF_VALUE_TEMPLATE)
|
|
|
|
if value_template is not None:
|
2016-09-28 04:29:55 +00:00
|
|
|
value_template.hass = hass
|
2017-02-22 08:43:22 +00:00
|
|
|
|
2017-03-01 04:33:19 +00:00
|
|
|
async_add_devices([MqttBinarySensor(
|
2016-09-09 00:32:32 +00:00
|
|
|
config.get(CONF_NAME),
|
|
|
|
config.get(CONF_STATE_TOPIC),
|
2017-09-21 15:02:11 +00:00
|
|
|
config.get(CONF_AVAILABILITY_TOPIC),
|
2017-07-29 23:46:27 +00:00
|
|
|
config.get(CONF_DEVICE_CLASS),
|
2016-09-09 00:32:32 +00:00
|
|
|
config.get(CONF_QOS),
|
2018-02-08 11:28:12 +00:00
|
|
|
config.get(CONF_FORCE_UPDATE),
|
2016-09-09 00:32:32 +00:00
|
|
|
config.get(CONF_PAYLOAD_ON),
|
|
|
|
config.get(CONF_PAYLOAD_OFF),
|
2017-09-21 15:02:11 +00:00
|
|
|
config.get(CONF_PAYLOAD_AVAILABLE),
|
|
|
|
config.get(CONF_PAYLOAD_NOT_AVAILABLE),
|
2018-06-13 14:20:38 +00:00
|
|
|
value_template,
|
|
|
|
config.get(CONF_UNIQUE_ID),
|
2016-04-05 04:53:58 +00:00
|
|
|
)])
|
2015-11-20 22:43:59 +00:00
|
|
|
|
|
|
|
|
2018-01-02 02:32:29 +00:00
|
|
|
class MqttBinarySensor(MqttAvailability, BinarySensorDevice):
|
2016-03-10 19:51:58 +00:00
|
|
|
"""Representation a binary sensor that is updated by MQTT."""
|
2016-03-07 19:21:08 +00:00
|
|
|
|
2017-09-21 15:02:11 +00:00
|
|
|
def __init__(self, name, state_topic, availability_topic, device_class,
|
2018-02-08 11:28:12 +00:00
|
|
|
qos, force_update, payload_on, payload_off, payload_available,
|
2018-06-13 14:20:38 +00:00
|
|
|
payload_not_available, value_template,
|
|
|
|
unique_id: Optional[str]):
|
2016-03-07 19:21:08 +00:00
|
|
|
"""Initialize the MQTT binary sensor."""
|
2018-01-02 02:32:29 +00:00
|
|
|
super().__init__(availability_topic, qos, payload_available,
|
|
|
|
payload_not_available)
|
2015-11-20 22:43:59 +00:00
|
|
|
self._name = name
|
2017-09-21 15:02:11 +00:00
|
|
|
self._state = None
|
2015-11-20 22:43:59 +00:00
|
|
|
self._state_topic = state_topic
|
2017-02-11 04:46:15 +00:00
|
|
|
self._device_class = device_class
|
2015-11-20 22:43:59 +00:00
|
|
|
self._payload_on = payload_on
|
|
|
|
self._payload_off = payload_off
|
|
|
|
self._qos = qos
|
2018-02-08 11:28:12 +00:00
|
|
|
self._force_update = force_update
|
2017-02-22 08:43:22 +00:00
|
|
|
self._template = value_template
|
2018-06-13 14:20:38 +00:00
|
|
|
self._unique_id = unique_id
|
2017-02-22 08:43:22 +00:00
|
|
|
|
2018-01-02 02:32:29 +00:00
|
|
|
@asyncio.coroutine
|
2017-02-22 08:43:22 +00:00
|
|
|
def async_added_to_hass(self):
|
2018-01-02 02:32:29 +00:00
|
|
|
"""Subscribe mqtt events."""
|
|
|
|
yield from super().async_added_to_hass()
|
2015-11-20 22:43:59 +00:00
|
|
|
|
2016-11-17 15:34:46 +00:00
|
|
|
@callback
|
2017-09-21 15:02:11 +00:00
|
|
|
def state_message_received(topic, payload, qos):
|
|
|
|
"""Handle a new received MQTT state message."""
|
2017-02-22 08:43:22 +00:00
|
|
|
if self._template is not None:
|
|
|
|
payload = self._template.async_render_with_possible_json_value(
|
2016-09-28 04:29:55 +00:00
|
|
|
payload)
|
2015-11-20 22:43:59 +00:00
|
|
|
if payload == self._payload_on:
|
|
|
|
self._state = True
|
|
|
|
elif payload == self._payload_off:
|
|
|
|
self._state = False
|
2018-01-30 09:18:45 +00:00
|
|
|
else: # Payload is not for this entity
|
|
|
|
_LOGGER.warning('No matching payload found'
|
|
|
|
' for entity: %s with state_topic: %s',
|
|
|
|
self._name, self._state_topic)
|
|
|
|
return
|
2015-11-20 22:43:59 +00:00
|
|
|
|
2017-09-12 08:01:03 +00:00
|
|
|
self.async_schedule_update_ha_state()
|
2017-02-22 08:43:22 +00:00
|
|
|
|
2017-09-21 15:02:11 +00:00
|
|
|
yield from mqtt.async_subscribe(
|
|
|
|
self.hass, self._state_topic, state_message_received, self._qos)
|
|
|
|
|
2015-11-20 22:43:59 +00:00
|
|
|
@property
|
|
|
|
def should_poll(self):
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Return the polling state."""
|
2015-11-20 22:43:59 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
2016-03-07 19:21:08 +00:00
|
|
|
"""Return the name of the binary sensor."""
|
2015-11-20 22:43:59 +00:00
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
2016-03-07 19:21:08 +00:00
|
|
|
"""Return true if the binary sensor is on."""
|
2015-11-20 22:43:59 +00:00
|
|
|
return self._state
|
2016-03-10 19:51:58 +00:00
|
|
|
|
|
|
|
@property
|
2017-02-11 04:46:15 +00:00
|
|
|
def device_class(self):
|
2016-03-10 19:51:58 +00:00
|
|
|
"""Return the class of this sensor."""
|
2017-02-11 04:46:15 +00:00
|
|
|
return self._device_class
|
2018-02-08 11:28:12 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def force_update(self):
|
|
|
|
"""Force update."""
|
|
|
|
return self._force_update
|
2018-06-13 14:20:38 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return a unique ID."""
|
|
|
|
return self._unique_id
|