1
0
mirror of https://github.com/home-assistant/core.git synced 2025-08-21 19:30:02 +00:00
Files
.github
docs
homeassistant
components
alarm_control_panel
alexa
automation
binary_sensor
calendar
camera
climate
cloud
config
cover
device_tracker
__init__.py
actiontec.py
aruba.py
asuswrt.py
automatic.py
bbox.py
bluetooth_le_tracker.py
bluetooth_tracker.py
bt_home_hub_5.py
cisco_ios.py
ddwrt.py
demo.py
fritz.py
geofency.py
gpslogger.py
huawei_router.py
icloud.py
keenetic_ndms2.py
linksys_ap.py
linksys_smart.py
locative.py
luci.py
mikrotik.py
mqtt.py
mqtt_json.py
mysensors.py
netgear.py
nmap_tracker.py
owntracks.py
owntracks_http.py
ping.py
services.yaml
sky_hub.py
snmp.py
swisscom.py
tado.py
tesla.py
thomson.py
tomato.py
tplink.py
trackr.py
ubus.py
unifi.py
upc_connect.py
volvooncall.py
xiaomi.py
emulated_hue
fan
frontend
group
http
image_processing
light
lock
mailbox
media_player
mqtt
notify
persistent_notification
recorder
remote
scene
sensor
switch
telegram_bot
tts
vacuum
weather
zha
zwave
__init__.py
abode.py
alarmdecoder.py
alert.py
amcrest.py
android_ip_webcam.py
apcupsd.py
api.py
apiai.py
apple_tv.py
arduino.py
arlo.py
asterisk_mbox.py
axis.py
bbb_gpio.py
blink.py
bloomsky.py
browser.py
comfoconnect.py
configurator.py
conversation.py
counter.py
datadog.py
demo.py
device_sun_light_trigger.py
digital_ocean.py
discovery.py
doorbird.py
downloader.py
duckdns.py
dweet.py
dyson.py
ecobee.py
eight_sleep.py
emoncms_history.py
enocean.py
envisalink.py
feedreader.py
ffmpeg.py
foursquare.py
google.py
graphite.py
hassio.py
hdmi_cec.py
history.py
history_graph.py
homematic.py
ifttt.py
influxdb.py
input_boolean.py
input_datetime.py
input_number.py
input_select.py
input_text.py
insteon_local.py
insteon_plm.py
intent_script.py
introduction.py
ios.py
isy994.py
joaoapps_join.py
juicenet.py
keyboard.py
keyboard_remote.py
kira.py
knx.py
lametric.py
lirc.py
litejet.py
logbook.py
logentries.py
logger.py
lutron.py
lutron_caseta.py
mailgun.py
maxcube.py
media_extractor.py
microsoft_face.py
mochad.py
modbus.py
mqtt_eventstream.py
mqtt_statestream.py
mycroft.py
mysensors.py
neato.py
nest.py
netatmo.py
nuimo_controller.py
octoprint.py
panel_custom.py
panel_iframe.py
pilight.py
plant.py
prometheus.py
proximity.py
python_script.py
qwikswitch.py
raincloud.py
raspihats.py
rest_command.py
rflink.py
rfxtrx.py
ring.py
rpi_gpio.py
rpi_pfio.py
rss_feed_template.py
satel_integra.py
script.py
scsgate.py
services.yaml
shell_command.py
shiftr.py
shopping_list.py
sleepiq.py
snips.py
spc.py
splunk.py
statsd.py
sun.py
tado.py
tellduslive.py
tellstick.py
tesla.py
thethingsnetwork.py
thingspeak.py
tradfri.py
twilio.py
updater.py
upnp.py
usps.py
velbus.py
velux.py
vera.py
verisure.py
volvooncall.py
wake_on_lan.py
weblink.py
websocket_api.py
wemo.py
wink.py
xiaomi_aqara.py
zabbix.py
zeroconf.py
zigbee.py
zone.py
zoneminder.py
helpers
scripts
util
__init__.py
__main__.py
bootstrap.py
config.py
const.py
core.py
exceptions.py
loader.py
monkey_patch.py
package_constraints.txt
remote.py
setup.py
script
tests
virtualization
.coveragerc
.dockerignore
.gitignore
.gitmodules
.hound.yml
.ignore
.travis.yml
CLA.md
CODEOWNERS
CODE_OF_CONDUCT.md
CONTRIBUTING.md
Dockerfile
LICENSE.md
MANIFEST.in
README.rst
pylintrc
requirements_all.txt
requirements_docs.txt
requirements_test.txt
requirements_test_all.txt
setup.cfg
setup.py
tox.ini
core/homeassistant/components/device_tracker/mqtt_json.py
Fabian Affolter 3ee4d1060f Update docstrings ()
* Update docstrings

* Update docstrings

* Update docstrings

* Update docstrings

* update docstrings

* Update docstrings

* Update docstrings

* Update docstrings

* Update docstrings

* Update docstrings

* Update tomato.py

* Update isy994.py

* Lint + fix tests

* Lint
2017-04-29 22:04:49 -07:00

86 lines
2.6 KiB
Python

"""
Support for GPS tracking MQTT enabled devices.
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/device_tracker.mqtt_json/
"""
import asyncio
import json
import logging
import voluptuous as vol
import homeassistant.components.mqtt as mqtt
from homeassistant.core import callback
from homeassistant.components.mqtt import CONF_QOS
from homeassistant.components.device_tracker import PLATFORM_SCHEMA
import homeassistant.helpers.config_validation as cv
from homeassistant.const import (
CONF_DEVICES, ATTR_GPS_ACCURACY, ATTR_LATITUDE,
ATTR_LONGITUDE, ATTR_BATTERY_LEVEL)
DEPENDENCIES = ['mqtt']
_LOGGER = logging.getLogger(__name__)
GPS_JSON_PAYLOAD_SCHEMA = vol.Schema({
vol.Required(ATTR_LATITUDE): vol.Coerce(float),
vol.Required(ATTR_LONGITUDE): vol.Coerce(float),
vol.Optional(ATTR_GPS_ACCURACY, default=None): vol.Coerce(int),
vol.Optional(ATTR_BATTERY_LEVEL, default=None): vol.Coerce(str),
}, extra=vol.ALLOW_EXTRA)
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(mqtt.SCHEMA_BASE).extend({
vol.Required(CONF_DEVICES): {cv.string: mqtt.valid_subscribe_topic},
})
@asyncio.coroutine
def async_setup_scanner(hass, config, async_see, discovery_info=None):
"""Set up the MQTT JSON tracker."""
devices = config[CONF_DEVICES]
qos = config[CONF_QOS]
dev_id_lookup = {}
@callback
def async_tracker_message_received(topic, payload, qos):
"""Handle received MQTT message."""
dev_id = dev_id_lookup[topic]
try:
data = GPS_JSON_PAYLOAD_SCHEMA(json.loads(payload))
except vol.MultipleInvalid:
_LOGGER.error("Skipping update for following data "
"because of missing or malformatted data: %s",
payload)
return
except ValueError:
_LOGGER.error("Error parsing JSON payload: %s", payload)
return
kwargs = _parse_see_args(dev_id, data)
hass.async_add_job(
async_see(**kwargs))
for dev_id, topic in devices.items():
dev_id_lookup[topic] = dev_id
yield from mqtt.async_subscribe(
hass, topic, async_tracker_message_received, qos)
return True
def _parse_see_args(dev_id, data):
"""Parse the payload location parameters, into the format see expects."""
kwargs = {
'gps': (data[ATTR_LATITUDE], data[ATTR_LONGITUDE]),
'dev_id': dev_id
}
if ATTR_GPS_ACCURACY in data:
kwargs[ATTR_GPS_ACCURACY] = data[ATTR_GPS_ACCURACY]
if ATTR_BATTERY_LEVEL in data:
kwargs['battery'] = data[ATTR_BATTERY_LEVEL]
return kwargs