Handle accuracy zero correctly in enter/leave events.
This commit is contained in:
parent
6fd0fe05f9
commit
c0b1ff0eaf
2 changed files with 60 additions and 5 deletions
|
@ -29,6 +29,9 @@ LOCK = threading.Lock()
|
||||||
|
|
||||||
CONF_MAX_GPS_ACCURACY = 'max_gps_accuracy'
|
CONF_MAX_GPS_ACCURACY = 'max_gps_accuracy'
|
||||||
|
|
||||||
|
VALIDATE_LOCATION = 'location'
|
||||||
|
VALIDATE_TRANSITION = 'transition'
|
||||||
|
|
||||||
|
|
||||||
def setup_scanner(hass, config, see):
|
def setup_scanner(hass, config, see):
|
||||||
"""Setup an OwnTracks tracker."""
|
"""Setup an OwnTracks tracker."""
|
||||||
|
@ -47,6 +50,8 @@ def setup_scanner(hass, config, see):
|
||||||
'because of missing or malformatted data: %s',
|
'because of missing or malformatted data: %s',
|
||||||
data_type, data)
|
data_type, data)
|
||||||
return None
|
return None
|
||||||
|
if data_type == VALIDATE_TRANSITION:
|
||||||
|
return data
|
||||||
if max_gps_accuracy is not None and \
|
if max_gps_accuracy is not None and \
|
||||||
convert(data.get('acc'), float, 0.0) > max_gps_accuracy:
|
convert(data.get('acc'), float, 0.0) > max_gps_accuracy:
|
||||||
_LOGGER.debug('Skipping %s update because expected GPS '
|
_LOGGER.debug('Skipping %s update because expected GPS '
|
||||||
|
@ -65,7 +70,7 @@ def setup_scanner(hass, config, see):
|
||||||
"""MQTT message received."""
|
"""MQTT message received."""
|
||||||
# Docs on available data:
|
# Docs on available data:
|
||||||
# http://owntracks.org/booklet/tech/json/#_typelocation
|
# http://owntracks.org/booklet/tech/json/#_typelocation
|
||||||
data = validate_payload(payload, 'location')
|
data = validate_payload(payload, VALIDATE_LOCATION)
|
||||||
if not data:
|
if not data:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
@ -86,7 +91,7 @@ def setup_scanner(hass, config, see):
|
||||||
"""MQTT event (geofences) received."""
|
"""MQTT event (geofences) received."""
|
||||||
# Docs on available data:
|
# Docs on available data:
|
||||||
# http://owntracks.org/booklet/tech/json/#_typetransition
|
# http://owntracks.org/booklet/tech/json/#_typetransition
|
||||||
data = validate_payload(payload, 'transition')
|
data = validate_payload(payload, VALIDATE_TRANSITION)
|
||||||
if not data:
|
if not data:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
@ -143,10 +148,17 @@ def setup_scanner(hass, config, see):
|
||||||
else:
|
else:
|
||||||
_LOGGER.info("Exit to GPS")
|
_LOGGER.info("Exit to GPS")
|
||||||
# Check for GPS accuracy
|
# Check for GPS accuracy
|
||||||
if not ('acc' in data and
|
valid_gps = True
|
||||||
max_gps_accuracy is not None and
|
if 'acc' in data:
|
||||||
data['acc'] > max_gps_accuracy):
|
if data['acc'] == 0.0:
|
||||||
|
valid_gps = False
|
||||||
|
_LOGGER.info("Zero GPS reported")
|
||||||
|
if (max_gps_accuracy is not None and
|
||||||
|
data['acc'] > max_gps_accuracy):
|
||||||
|
valid_gps = False
|
||||||
|
_LOGGER.info("Inaccurate GPS reported")
|
||||||
|
|
||||||
|
if valid_gps:
|
||||||
see(**kwargs)
|
see(**kwargs)
|
||||||
see_beacons(dev_id, kwargs)
|
see_beacons(dev_id, kwargs)
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -108,6 +108,31 @@ REGION_LEAVE_INACCURATE_MESSAGE = {
|
||||||
'_type': 'transition'}
|
'_type': 'transition'}
|
||||||
|
|
||||||
|
|
||||||
|
REGION_ENTER_ZERO_MESSAGE = {
|
||||||
|
'lon': 1.0,
|
||||||
|
'event': 'enter',
|
||||||
|
'tid': 'user',
|
||||||
|
'desc': 'inner',
|
||||||
|
'wtst': 1,
|
||||||
|
't': 'b',
|
||||||
|
'acc': 0,
|
||||||
|
'tst': 2,
|
||||||
|
'lat': 2.0,
|
||||||
|
'_type': 'transition'}
|
||||||
|
|
||||||
|
REGION_LEAVE_ZERO_MESSAGE = {
|
||||||
|
'lon': 10.0,
|
||||||
|
'event': 'leave',
|
||||||
|
'tid': 'user',
|
||||||
|
'desc': 'inner',
|
||||||
|
'wtst': 1,
|
||||||
|
't': 'b',
|
||||||
|
'acc': 0,
|
||||||
|
'tst': 2,
|
||||||
|
'lat': 20.0,
|
||||||
|
'_type': 'transition'}
|
||||||
|
|
||||||
|
|
||||||
class TestDeviceTrackerOwnTracks(unittest.TestCase):
|
class TestDeviceTrackerOwnTracks(unittest.TestCase):
|
||||||
"""Test the OwnTrack sensor."""
|
"""Test the OwnTrack sensor."""
|
||||||
|
|
||||||
|
@ -293,6 +318,24 @@ class TestDeviceTrackerOwnTracks(unittest.TestCase):
|
||||||
# But does exit region correctly
|
# But does exit region correctly
|
||||||
self.assertFalse(owntracks.REGIONS_ENTERED[USER])
|
self.assertFalse(owntracks.REGIONS_ENTERED[USER])
|
||||||
|
|
||||||
|
def test_event_entry_exit_zero_accuracy(self):
|
||||||
|
self.send_message(EVENT_TOPIC, REGION_ENTER_ZERO_MESSAGE)
|
||||||
|
|
||||||
|
# Enter uses the zone's gps co-ords
|
||||||
|
self.assert_location_latitude(2.1)
|
||||||
|
self.assert_location_accuracy(10.0)
|
||||||
|
self.assert_location_state('inner')
|
||||||
|
|
||||||
|
self.send_message(EVENT_TOPIC, REGION_LEAVE_ZERO_MESSAGE)
|
||||||
|
|
||||||
|
# Exit doesn't use zero gps
|
||||||
|
self.assert_location_latitude(2.1)
|
||||||
|
self.assert_location_accuracy(10.0)
|
||||||
|
self.assert_location_state('inner')
|
||||||
|
|
||||||
|
# But does exit region correctly
|
||||||
|
self.assertFalse(owntracks.REGIONS_ENTERED[USER])
|
||||||
|
|
||||||
def test_event_exit_outside_zone_sets_away(self):
|
def test_event_exit_outside_zone_sets_away(self):
|
||||||
"""Test the event for exit zone."""
|
"""Test the event for exit zone."""
|
||||||
self.send_message(EVENT_TOPIC, REGION_ENTER_MESSAGE)
|
self.send_message(EVENT_TOPIC, REGION_ENTER_MESSAGE)
|
||||||
|
|
Loading…
Add table
Reference in a new issue