"""
homeassistant.components.device_tracker.owntracks
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

OwnTracks platform for the device tracker.

device_tracker:
  platform: owntracks
"""
import json

import homeassistant.components.mqtt as mqtt

DEPENDENCIES = ['mqtt']

LOCATION_TOPIC = 'owntracks/+/+'


def setup_scanner(hass, config, see):
    """ Set up a OwnTracksks tracker. """

    def owntracks_location_update(topic, payload, qos):
        """ MQTT message received. """

        # Docs on available data:
        # http://owntracks.org/booklet/tech/json/#_typelocation

        parts = topic.split('/')
        try:
            data = json.loads(payload)
        except ValueError:
            # If invalid JSON
            return
        if data.get('_type') != 'location':
            return
        dev_id = '{}_{}'.format(parts[1], parts[2])
        see(dev_id=dev_id, host_name=parts[1], gps=(data['lat'], data['lon']),
            gps_accuracy=data['acc'], battery=data['batt'])

    mqtt.subscribe(hass, LOCATION_TOPIC, owntracks_location_update, 1)

    return True

@@ -195,16 +195,31 @@
 ort json
+%0Aimport logging
 %0A%0Aimport
@@ -593,17 +593,16 @@
 ocation%0A
-%0A
         
@@ -606,30 +606,28 @@
    p
-arts = topic.split('/'
+rint(%22BIER%22, payload
 )%0A  
@@ -733,16 +733,127 @@
 id JSON%0A
+            logging.getLogger(__name__).error(%0A                'Unable to parse payload as JSON: %25s', payload)%0A
         
@@ -855,32 +855,33 @@
          return%0A
+%0A
         if data.
@@ -927,24 +927,81 @@
  return%0A
+%0A        parts = topic.split('/')%0A        kwargs = %7B%0A    
         
 dev_id =
@@ -996,16 +996,17 @@
     
+'
 dev_id
- =
+':
  '%7B%7D
@@ -1036,16 +1036,17 @@
 arts%5B2%5D)
+,
 %0A       
@@ -1050,27 +1050,13 @@
     
-see(dev_id=dev_id, 
+    '
 host
@@ -1060,17 +1060,19 @@
 ost_name
-=
+': 
 parts%5B1%5D
@@ -1076,13 +1076,28 @@
 %5B1%5D,
- gps=
+%0A            'gps': 
 (dat
@@ -1128,20 +1128,64 @@
         
+%7D%0A
     
+    if 'acc' in data:%0A            kwargs%5B'
 gps_accu
@@ -1188,17 +1188,21 @@
 accuracy
-=
+'%5D = 
 data%5B'ac
@@ -1208,18 +1208,68 @@
 cc'%5D
-, 
+%0A        if 'batt' in data:%0A            kwargs%5B'
 battery
-=
+'%5D = 
 data
@@ -1276,16 +1276,38 @@
 %5B'batt'%5D
+%0A%0A        see(**kwargs
 )%0A%0A    m

Make owntracks more robust