2015-09-10 06:37:15 +00:00
|
|
|
"""
|
2016-03-07 17:12:06 +00:00
|
|
|
Support for tracking MQTT enabled devices.
|
2015-09-10 06:37:15 +00:00
|
|
|
|
2015-10-13 18:54:15 +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/device_tracker.mqtt/
|
2015-09-10 06:37:15 +00:00
|
|
|
"""
|
2017-03-03 11:09:10 +00:00
|
|
|
import asyncio
|
2015-09-10 06:37:15 +00:00
|
|
|
import logging
|
2016-02-19 05:27:50 +00:00
|
|
|
|
2016-04-05 05:41:11 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2015-09-10 06:37:15 +00:00
|
|
|
import homeassistant.components.mqtt as mqtt
|
2017-03-03 11:09:10 +00:00
|
|
|
from homeassistant.core import callback
|
2016-09-01 20:04:00 +00:00
|
|
|
from homeassistant.const import CONF_DEVICES
|
2016-04-07 01:35:46 +00:00
|
|
|
from homeassistant.components.mqtt import CONF_QOS
|
2016-10-19 01:10:28 +00:00
|
|
|
from homeassistant.components.device_tracker import PLATFORM_SCHEMA
|
2016-04-05 05:41:11 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2015-09-10 06:37:15 +00:00
|
|
|
|
|
|
|
DEPENDENCIES = ['mqtt']
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2016-10-19 01:10:28 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(mqtt.SCHEMA_BASE).extend({
|
2016-04-05 05:41:11 +00:00
|
|
|
vol.Required(CONF_DEVICES): {cv.string: mqtt.valid_subscribe_topic},
|
|
|
|
})
|
|
|
|
|
2015-09-10 06:37:15 +00:00
|
|
|
|
2017-03-03 11:09:10 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def async_setup_scanner(hass, config, async_see, discovery_info=None):
|
2016-03-07 17:12:06 +00:00
|
|
|
"""Setup the MQTT tracker."""
|
2016-04-05 05:41:11 +00:00
|
|
|
devices = config[CONF_DEVICES]
|
2016-04-07 01:35:46 +00:00
|
|
|
qos = config[CONF_QOS]
|
2015-09-10 06:37:15 +00:00
|
|
|
|
|
|
|
dev_id_lookup = {}
|
|
|
|
|
2017-03-03 11:09:10 +00:00
|
|
|
@callback
|
|
|
|
def async_tracker_message_received(topic, payload, qos):
|
2016-03-07 17:12:06 +00:00
|
|
|
"""MQTT message received."""
|
2017-03-03 11:09:10 +00:00
|
|
|
hass.async_add_job(
|
|
|
|
async_see(dev_id=dev_id_lookup[topic], location_name=payload))
|
2015-09-10 06:37:15 +00:00
|
|
|
|
|
|
|
for dev_id, topic in devices.items():
|
|
|
|
dev_id_lookup[topic] = dev_id
|
2017-03-03 11:09:10 +00:00
|
|
|
yield from mqtt.async_subscribe(
|
|
|
|
hass, topic, async_tracker_message_received, qos)
|
2015-09-10 06:37:15 +00:00
|
|
|
|
|
|
|
return True
|