core/homeassistant/components/mqtt/device_tracker.py

59 lines
1.9 KiB
Python
Raw Normal View History

"""Support for tracking MQTT enabled devices."""
2015-09-10 06:37:15 +00:00
import logging
2016-02-19 05:27:50 +00:00
import voluptuous as vol
from homeassistant.components import mqtt
from homeassistant.components.device_tracker import PLATFORM_SCHEMA, SOURCE_TYPES
from homeassistant.const import CONF_DEVICES, STATE_HOME, STATE_NOT_HOME
from homeassistant.core import callback
import homeassistant.helpers.config_validation as cv
2015-09-10 06:37:15 +00:00
from . import CONF_QOS
2015-09-10 06:37:15 +00:00
_LOGGER = logging.getLogger(__name__)
CONF_PAYLOAD_HOME = "payload_home"
CONF_PAYLOAD_NOT_HOME = "payload_not_home"
CONF_SOURCE_TYPE = "source_type"
2019-07-31 19:25:30 +00:00
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(mqtt.SCHEMA_BASE).extend(
{
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,
vol.Optional(CONF_SOURCE_TYPE): vol.In(SOURCE_TYPES),
}
2019-07-31 19:25:30 +00:00
)
2015-09-10 06:37:15 +00:00
async def async_setup_scanner(hass, config, async_see, discovery_info=None):
"""Set up the MQTT tracker."""
devices = config[CONF_DEVICES]
qos = config[CONF_QOS]
payload_home = config[CONF_PAYLOAD_HOME]
payload_not_home = config[CONF_PAYLOAD_NOT_HOME]
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
@callback
def async_message_received(msg, dev_id=dev_id):
"""Handle received MQTT message."""
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
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))
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