Migrate mqtt tracker and arwn sensor to async / cleanup owntrack (#6373)

* Migrate mqtt tracker and arwn sensor to async / cleanup owntrack

* Fix tests / lint
This commit is contained in:
Pascal Vizeli 2017-03-03 12:09:10 +01:00 committed by GitHub
parent 55f8ec8866
commit ed9e93c29f
4 changed files with 35 additions and 25 deletions

View file

@ -4,11 +4,13 @@ Support for tracking MQTT enabled devices.
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/device_tracker.mqtt/
"""
import asyncio
import logging
import voluptuous as vol
import homeassistant.components.mqtt as mqtt
from homeassistant.core import callback
from homeassistant.const import CONF_DEVICES
from homeassistant.components.mqtt import CONF_QOS
from homeassistant.components.device_tracker import PLATFORM_SCHEMA
@ -23,19 +25,23 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(mqtt.SCHEMA_BASE).extend({
})
def setup_scanner(hass, config, see, discovery_info=None):
@asyncio.coroutine
def async_setup_scanner(hass, config, async_see, discovery_info=None):
"""Setup the MQTT tracker."""
devices = config[CONF_DEVICES]
qos = config[CONF_QOS]
dev_id_lookup = {}
def device_tracker_message_received(topic, payload, qos):
@callback
def async_tracker_message_received(topic, payload, qos):
"""MQTT message received."""
see(dev_id=dev_id_lookup[topic], location_name=payload)
hass.async_add_job(
async_see(dev_id=dev_id_lookup[topic], location_name=payload))
for dev_id, topic in devices.items():
dev_id_lookup[topic] = dev_id
mqtt.subscribe(hass, topic, device_tracker_message_received, qos)
yield from mqtt.async_subscribe(
hass, topic, async_tracker_message_received, qos)
return True