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
|
2019-10-23 05:26:29 +00:00
|
|
|
from homeassistant.components.device_tracker import PLATFORM_SCHEMA, SOURCE_TYPES
|
2019-12-08 17:15:26 +00:00
|
|
|
from homeassistant.const import CONF_DEVICES, STATE_HOME, STATE_NOT_HOME
|
2019-03-21 05:56:46 +00:00
|
|
|
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__)
|
|
|
|
|
2019-10-18 00:51:27 +00:00
|
|
|
CONF_PAYLOAD_HOME = "payload_home"
|
|
|
|
CONF_PAYLOAD_NOT_HOME = "payload_not_home"
|
2019-10-23 05:26:29 +00:00
|
|
|
CONF_SOURCE_TYPE = "source_type"
|
2019-10-18 00:51:27 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(mqtt.SCHEMA_BASE).extend(
|
2019-10-18 00:51:27 +00:00
|
|
|
{
|
|
|
|
vol.Required(CONF_DEVICES): {cv.string: mqtt.valid_subscribe_topic},
|
|
|
|
vol.Optional(CONF_PAYLOAD_HOME, default=STATE_HOME): cv.string,
|
|
|
|
vol.Optional(CONF_PAYLOAD_NOT_HOME, default=STATE_NOT_HOME): cv.string,
|
2019-10-23 05:26:29 +00:00
|
|
|
vol.Optional(CONF_SOURCE_TYPE): vol.In(SOURCE_TYPES),
|
2019-10-18 00:51:27 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2016-04-05 05:41:11 +00:00
|
|
|
|
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]
|
2019-10-18 00:51:27 +00:00
|
|
|
payload_home = config[CONF_PAYLOAD_HOME]
|
|
|
|
payload_not_home = config[CONF_PAYLOAD_NOT_HOME]
|
2019-10-23 05:26:29 +00:00
|
|
|
source_type = config.get(CONF_SOURCE_TYPE)
|
2015-09-10 06:37:15 +00:00
|
|
|
|
|
|
|
for dev_id, topic in devices.items():
|
2019-07-31 19:25:30 +00:00
|
|
|
|
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."""
|
2019-10-18 00:51:27 +00:00
|
|
|
if msg.payload == payload_home:
|
|
|
|
location_name = STATE_HOME
|
|
|
|
elif msg.payload == payload_not_home:
|
|
|
|
location_name = STATE_NOT_HOME
|
|
|
|
else:
|
|
|
|
location_name = msg.payload
|
|
|
|
|
2019-10-23 05:26:29 +00:00
|
|
|
see_args = {"dev_id": dev_id, "location_name": location_name}
|
|
|
|
if source_type:
|
|
|
|
see_args["source_type"] = source_type
|
|
|
|
|
|
|
|
hass.async_create_task(async_see(**see_args))
|
2018-02-09 23:22:50 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
await mqtt.async_subscribe(hass, topic, async_message_received, qos)
|
2015-09-10 06:37:15 +00:00
|
|
|
|
|
|
|
return True
|