2015-08-19 01:25:05 +01:00
|
|
|
"""
|
2016-02-23 06:21:49 +01:00
|
|
|
Support for MQTT sensors.
|
2015-08-19 01:25:05 +01:00
|
|
|
|
2015-10-21 19:36:52 +02:00
|
|
|
For more details about this platform, please refer to the documentation at
|
2015-11-09 13:12:18 +01:00
|
|
|
https://home-assistant.io/components/sensor.mqtt/
|
2015-08-19 01:25:05 +01:00
|
|
|
"""
|
2017-02-22 09:43:22 +01:00
|
|
|
import asyncio
|
2015-08-19 01:25:05 +01:00
|
|
|
import logging
|
2016-02-18 21:27:50 -08:00
|
|
|
|
2016-04-06 20:32:35 -04:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2016-11-17 07:34:46 -08:00
|
|
|
from homeassistant.core import callback
|
2016-09-09 08:37:30 +02:00
|
|
|
from homeassistant.components.mqtt import CONF_STATE_TOPIC, CONF_QOS
|
2016-08-21 00:40:16 +02:00
|
|
|
from homeassistant.const import (
|
|
|
|
CONF_NAME, CONF_VALUE_TEMPLATE, STATE_UNKNOWN, CONF_UNIT_OF_MEASUREMENT)
|
2016-09-09 08:37:30 +02:00
|
|
|
from homeassistant.helpers.entity import Entity
|
|
|
|
import homeassistant.components.mqtt as mqtt
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
2015-08-19 01:25:05 +01:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2016-09-09 08:37:30 +02:00
|
|
|
DEFAULT_NAME = 'MQTT Sensor'
|
2016-04-06 20:32:35 -04:00
|
|
|
DEPENDENCIES = ['mqtt']
|
|
|
|
|
2016-04-06 21:35:46 -04:00
|
|
|
PLATFORM_SCHEMA = mqtt.MQTT_RO_PLATFORM_SCHEMA.extend({
|
2016-04-06 20:32:35 -04:00
|
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
|
|
|
vol.Optional(CONF_UNIT_OF_MEASUREMENT): cv.string,
|
|
|
|
})
|
2015-08-19 01:25:05 +01:00
|
|
|
|
2015-08-20 23:05:51 +01:00
|
|
|
|
2017-02-22 09:43:22 +01:00
|
|
|
@asyncio.coroutine
|
|
|
|
def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
|
2017-02-14 08:54:13 +01:00
|
|
|
"""Set up MQTT Sensor."""
|
|
|
|
if discovery_info is not None:
|
|
|
|
config = PLATFORM_SCHEMA(discovery_info)
|
|
|
|
|
2016-09-27 21:29:55 -07:00
|
|
|
value_template = config.get(CONF_VALUE_TEMPLATE)
|
|
|
|
if value_template is not None:
|
|
|
|
value_template.hass = hass
|
2017-02-22 09:43:22 +01:00
|
|
|
|
|
|
|
yield from async_add_devices([MqttSensor(
|
2016-09-09 08:37:30 +02:00
|
|
|
config.get(CONF_NAME),
|
|
|
|
config.get(CONF_STATE_TOPIC),
|
|
|
|
config.get(CONF_QOS),
|
2016-04-06 20:32:35 -04:00
|
|
|
config.get(CONF_UNIT_OF_MEASUREMENT),
|
2016-09-27 21:29:55 -07:00
|
|
|
value_template,
|
2016-04-06 20:32:35 -04:00
|
|
|
)])
|
2015-08-19 01:25:05 +01:00
|
|
|
|
|
|
|
|
|
|
|
class MqttSensor(Entity):
|
2016-03-08 16:46:34 +01:00
|
|
|
"""Representation of a sensor that can be updated using MQTT."""
|
|
|
|
|
2017-02-22 09:43:22 +01:00
|
|
|
def __init__(self, name, state_topic, qos, unit_of_measurement,
|
2015-12-10 21:39:01 -08:00
|
|
|
value_template):
|
2016-03-08 16:46:34 +01:00
|
|
|
"""Initialize the sensor."""
|
2016-02-03 16:54:01 -07:00
|
|
|
self._state = STATE_UNKNOWN
|
2015-08-19 01:25:05 +01:00
|
|
|
self._name = name
|
|
|
|
self._state_topic = state_topic
|
2015-09-07 00:16:31 +00:00
|
|
|
self._qos = qos
|
2015-08-19 01:25:05 +01:00
|
|
|
self._unit_of_measurement = unit_of_measurement
|
2017-02-22 09:43:22 +01:00
|
|
|
self._template = value_template
|
|
|
|
|
|
|
|
def async_added_to_hass(self):
|
|
|
|
"""Subscribe mqtt events.
|
2015-08-19 01:25:05 +01:00
|
|
|
|
2017-02-22 09:43:22 +01:00
|
|
|
This method must be run in the event loop and returns a coroutine.
|
|
|
|
"""
|
2016-11-17 07:34:46 -08:00
|
|
|
@callback
|
2015-08-19 01:25:05 +01:00
|
|
|
def message_received(topic, payload, qos):
|
2016-02-23 06:21:49 +01:00
|
|
|
"""A new MQTT message has been received."""
|
2017-02-22 09:43:22 +01:00
|
|
|
if self._template is not None:
|
|
|
|
payload = self._template.async_render_with_possible_json_value(
|
2016-10-13 13:39:49 -04:00
|
|
|
payload, self._state)
|
2015-12-10 21:39:01 -08:00
|
|
|
self._state = payload
|
2017-02-22 09:43:22 +01:00
|
|
|
self.hass.async_add_job(self.async_update_ha_state())
|
2015-08-19 01:25:05 +01:00
|
|
|
|
2017-02-22 09:43:22 +01:00
|
|
|
return mqtt.async_subscribe(
|
|
|
|
self.hass, self._state_topic, message_received, self._qos)
|
2015-08-19 01:25:05 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def should_poll(self):
|
2016-02-23 06:21:49 +01:00
|
|
|
"""No polling needed."""
|
2015-08-19 01:25:05 +01:00
|
|
|
return False
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
2016-03-08 16:46:34 +01:00
|
|
|
"""Return the name of the sensor."""
|
2015-08-19 01:25:05 +01:00
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
2016-03-08 16:46:34 +01:00
|
|
|
"""Return the unit this state is expressed in."""
|
2015-08-19 01:25:05 +01:00
|
|
|
return self._unit_of_measurement
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
2016-03-08 16:46:34 +01:00
|
|
|
"""Return the state of the entity."""
|
2015-08-20 23:05:51 +01:00
|
|
|
return self._state
|