2019-04-03 15:40:03 +00:00
|
|
|
"""Support for tracking MQTT enabled devices."""
|
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
|
|
|
|
|
2018-07-18 09:54:27 +00:00
|
|
|
from homeassistant.components import mqtt
|
2016-10-19 01:10:28 +00:00
|
|
|
from homeassistant.components.device_tracker import PLATFORM_SCHEMA
|
2019-03-21 05:56:46 +00:00
|
|
|
from homeassistant.const import CONF_DEVICES
|
|
|
|
from homeassistant.core import callback
|
2016-04-05 05:41:11 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2015-09-10 06:37:15 +00:00
|
|
|
|
2019-03-21 05:56:46 +00:00
|
|
|
from . import CONF_QOS
|
|
|
|
|
2015-09-10 06:37:15 +00:00
|
|
|
_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
|
|
|
|
2018-10-01 06:59:45 +00:00
|
|
|
async def async_setup_scanner(hass, config, async_see, discovery_info=None):
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Set up 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
|
|
|
|
|
|
|
for dev_id, topic in devices.items():
|
2018-02-09 23:22:50 +00:00
|
|
|
@callback
|
2019-03-13 19:58:20 +00:00
|
|
|
def async_message_received(msg, dev_id=dev_id):
|
2018-02-09 23:22:50 +00:00
|
|
|
"""Handle received MQTT message."""
|
2018-10-02 09:03:09 +00:00
|
|
|
hass.async_create_task(
|
2019-03-13 19:58:20 +00:00
|
|
|
async_see(dev_id=dev_id, location_name=msg.payload))
|
2018-02-09 23:22:50 +00:00
|
|
|
|
2018-10-01 06:59:45 +00:00
|
|
|
await mqtt.async_subscribe(
|
2018-02-09 23:22:50 +00:00
|
|
|
hass, topic, async_message_received, qos)
|
2015-09-10 06:37:15 +00:00
|
|
|
|
|
|
|
return True
|