2019-04-03 15:40:03 +00:00
|
|
|
"""Support for MQTT room presence detection."""
|
2016-08-21 03:49:38 +00:00
|
|
|
import logging
|
|
|
|
import json
|
2016-09-14 03:34:59 +00:00
|
|
|
from datetime import timedelta
|
2016-08-21 03:49:38 +00:00
|
|
|
|
|
|
|
import voluptuous as vol
|
|
|
|
|
2018-07-18 09:54:27 +00:00
|
|
|
from homeassistant.components import mqtt
|
2017-10-29 16:28:07 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
from homeassistant.components.mqtt import CONF_STATE_TOPIC
|
2016-08-21 03:49:38 +00:00
|
|
|
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
2019-07-31 19:25:30 +00:00
|
|
|
from homeassistant.const import CONF_NAME, CONF_TIMEOUT, STATE_NOT_HOME, ATTR_ID
|
2017-10-29 16:28:07 +00:00
|
|
|
from homeassistant.core import callback
|
2016-08-21 03:49:38 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
|
|
|
from homeassistant.util import dt, slugify
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_DEVICE_ID = "device_id"
|
|
|
|
ATTR_DISTANCE = "distance"
|
|
|
|
ATTR_ROOM = "room"
|
2016-09-09 06:37:30 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_DEVICE_ID = "device_id"
|
|
|
|
CONF_AWAY_TIMEOUT = "away_timeout"
|
2016-08-21 03:49:38 +00:00
|
|
|
|
2017-10-29 16:28:07 +00:00
|
|
|
DEFAULT_AWAY_TIMEOUT = 0
|
2019-07-31 19:25:30 +00:00
|
|
|
DEFAULT_NAME = "Room Sensor"
|
2016-09-09 06:37:30 +00:00
|
|
|
DEFAULT_TIMEOUT = 5
|
2019-07-31 19:25:30 +00:00
|
|
|
DEFAULT_TOPIC = "room_presence"
|
|
|
|
|
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_DEVICE_ID): cv.string,
|
|
|
|
vol.Required(CONF_TIMEOUT, default=DEFAULT_TIMEOUT): cv.positive_int,
|
|
|
|
vol.Optional(CONF_AWAY_TIMEOUT, default=DEFAULT_AWAY_TIMEOUT): cv.positive_int,
|
|
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
|
|
|
}
|
|
|
|
).extend(mqtt.MQTT_RO_PLATFORM_SCHEMA.schema)
|
|
|
|
|
|
|
|
MQTT_PAYLOAD = vol.Schema(
|
|
|
|
vol.All(
|
|
|
|
json.loads,
|
|
|
|
vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(ATTR_ID): cv.string,
|
|
|
|
vol.Required(ATTR_DISTANCE): vol.Coerce(float),
|
|
|
|
},
|
|
|
|
extra=vol.ALLOW_EXTRA,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Set up MQTT room Sensor."""
|
2019-07-31 19:25:30 +00:00
|
|
|
async_add_entities(
|
|
|
|
[
|
|
|
|
MQTTRoomSensor(
|
|
|
|
config.get(CONF_NAME),
|
|
|
|
config.get(CONF_STATE_TOPIC),
|
|
|
|
config.get(CONF_DEVICE_ID),
|
|
|
|
config.get(CONF_TIMEOUT),
|
|
|
|
config.get(CONF_AWAY_TIMEOUT),
|
|
|
|
)
|
|
|
|
]
|
|
|
|
)
|
2016-08-21 03:49:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
class MQTTRoomSensor(Entity):
|
|
|
|
"""Representation of a room sensor that is updated via MQTT."""
|
|
|
|
|
2017-02-22 08:43:22 +00:00
|
|
|
def __init__(self, name, state_topic, device_id, timeout, consider_home):
|
2016-08-21 03:49:38 +00:00
|
|
|
"""Initialize the sensor."""
|
2017-10-18 09:19:09 +00:00
|
|
|
self._state = STATE_NOT_HOME
|
2016-08-21 03:49:38 +00:00
|
|
|
self._name = name
|
2019-07-31 19:25:30 +00:00
|
|
|
self._state_topic = "{}{}".format(state_topic, "/+")
|
2016-08-21 03:49:38 +00:00
|
|
|
self._device_id = slugify(device_id).upper()
|
|
|
|
self._timeout = timeout
|
2019-07-31 19:25:30 +00:00
|
|
|
self._consider_home = (
|
|
|
|
timedelta(seconds=consider_home) if consider_home else None
|
|
|
|
)
|
2016-08-21 03:49:38 +00:00
|
|
|
self._distance = None
|
|
|
|
self._updated = None
|
|
|
|
|
2018-10-01 12:44:11 +00:00
|
|
|
async def async_added_to_hass(self):
|
2018-01-02 02:32:29 +00:00
|
|
|
"""Subscribe to MQTT events."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2017-02-22 08:43:22 +00:00
|
|
|
@callback
|
2016-08-21 03:49:38 +00:00
|
|
|
def update_state(device_id, room, distance):
|
|
|
|
"""Update the sensor state."""
|
|
|
|
self._state = room
|
|
|
|
self._distance = distance
|
|
|
|
self._updated = dt.utcnow()
|
|
|
|
|
2017-09-12 08:01:03 +00:00
|
|
|
self.async_schedule_update_ha_state()
|
2016-08-21 03:49:38 +00:00
|
|
|
|
2017-04-06 06:23:02 +00:00
|
|
|
@callback
|
2019-03-14 17:58:32 +00:00
|
|
|
def message_received(msg):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Handle new MQTT messages."""
|
2016-08-21 03:49:38 +00:00
|
|
|
try:
|
2019-03-14 17:58:32 +00:00
|
|
|
data = MQTT_PAYLOAD(msg.payload)
|
2016-08-21 03:49:38 +00:00
|
|
|
except vol.MultipleInvalid as error:
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.debug("Skipping update because of malformatted data: %s", error)
|
2016-08-21 03:49:38 +00:00
|
|
|
return
|
|
|
|
|
2019-03-14 17:58:32 +00:00
|
|
|
device = _parse_update_data(msg.topic, data)
|
2016-09-09 06:37:30 +00:00
|
|
|
if device.get(CONF_DEVICE_ID) == self._device_id:
|
2016-08-21 03:49:38 +00:00
|
|
|
if self._distance is None or self._updated is None:
|
|
|
|
update_state(**device)
|
|
|
|
else:
|
|
|
|
# update if:
|
|
|
|
# device is in the same room OR
|
|
|
|
# device is closer to another room OR
|
|
|
|
# last update from other room was too long ago
|
|
|
|
timediff = dt.utcnow() - self._updated
|
2019-07-31 19:25:30 +00:00
|
|
|
if (
|
|
|
|
device.get(ATTR_ROOM) == self._state
|
|
|
|
or device.get(ATTR_DISTANCE) < self._distance
|
|
|
|
or timediff.seconds >= self._timeout
|
|
|
|
):
|
2016-08-21 03:49:38 +00:00
|
|
|
update_state(**device)
|
|
|
|
|
2018-10-01 12:44:11 +00:00
|
|
|
return await mqtt.async_subscribe(
|
2019-07-31 19:25:30 +00:00
|
|
|
self.hass, self._state_topic, message_received, 1
|
|
|
|
)
|
2016-08-21 03:49:38 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the sensor."""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return the state attributes."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return {ATTR_DISTANCE: self._distance}
|
2016-08-21 03:49:38 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the current room of the entity."""
|
|
|
|
return self._state
|
|
|
|
|
2016-09-14 03:34:59 +00:00
|
|
|
def update(self):
|
|
|
|
"""Update the state for absent devices."""
|
2019-07-31 19:25:30 +00:00
|
|
|
if (
|
|
|
|
self._updated
|
|
|
|
and self._consider_home
|
|
|
|
and dt.utcnow() - self._updated > self._consider_home
|
|
|
|
):
|
2017-10-18 09:19:09 +00:00
|
|
|
self._state = STATE_NOT_HOME
|
2016-09-14 03:34:59 +00:00
|
|
|
|
2016-08-21 03:49:38 +00:00
|
|
|
|
|
|
|
def _parse_update_data(topic, data):
|
|
|
|
"""Parse the room presence update."""
|
2019-07-31 19:25:30 +00:00
|
|
|
parts = topic.split("/")
|
2016-08-21 03:49:38 +00:00
|
|
|
room = parts[-1]
|
2016-09-09 06:37:30 +00:00
|
|
|
device_id = slugify(data.get(ATTR_ID)).upper()
|
2019-07-31 19:25:30 +00:00
|
|
|
distance = data.get("distance")
|
|
|
|
parsed_data = {ATTR_DEVICE_ID: device_id, ATTR_ROOM: room, ATTR_DISTANCE: distance}
|
2016-08-21 03:49:38 +00:00
|
|
|
return parsed_data
|