Merge pull request #2833 from home-assistant/fix_owntracks_beacon_accuracy_bug3
Handle accuracy zero correctly in enter/leave events.pull/2797/head
commit
91e24de3d5
|
@ -29,6 +29,9 @@ LOCK = threading.Lock()
|
|||
|
||||
CONF_MAX_GPS_ACCURACY = 'max_gps_accuracy'
|
||||
|
||||
VALIDATE_LOCATION = 'location'
|
||||
VALIDATE_TRANSITION = 'transition'
|
||||
|
||||
|
||||
def setup_scanner(hass, config, see):
|
||||
"""Setup an OwnTracks tracker."""
|
||||
|
@ -47,16 +50,18 @@ def setup_scanner(hass, config, see):
|
|||
'because of missing or malformatted data: %s',
|
||||
data_type, data)
|
||||
return None
|
||||
if data_type == VALIDATE_TRANSITION:
|
||||
return data
|
||||
if max_gps_accuracy is not None and \
|
||||
convert(data.get('acc'), float, 0.0) > max_gps_accuracy:
|
||||
_LOGGER.debug('Skipping %s update because expected GPS '
|
||||
'accuracy %s is not met: %s',
|
||||
data_type, max_gps_accuracy, data)
|
||||
_LOGGER.warning('Ignoring %s update because expected GPS '
|
||||
'accuracy %s is not met: %s',
|
||||
data_type, max_gps_accuracy, payload)
|
||||
return None
|
||||
if convert(data.get('acc'), float, 1.0) == 0.0:
|
||||
_LOGGER.debug('Skipping %s update because GPS accuracy'
|
||||
'is zero',
|
||||
data_type)
|
||||
_LOGGER.warning('Ignoring %s update because GPS accuracy'
|
||||
'is zero: %s',
|
||||
data_type, payload)
|
||||
return None
|
||||
|
||||
return data
|
||||
|
@ -65,7 +70,7 @@ def setup_scanner(hass, config, see):
|
|||
"""MQTT message received."""
|
||||
# Docs on available data:
|
||||
# http://owntracks.org/booklet/tech/json/#_typelocation
|
||||
data = validate_payload(payload, 'location')
|
||||
data = validate_payload(payload, VALIDATE_LOCATION)
|
||||
if not data:
|
||||
return
|
||||
|
||||
|
@ -86,7 +91,7 @@ def setup_scanner(hass, config, see):
|
|||
"""MQTT event (geofences) received."""
|
||||
# Docs on available data:
|
||||
# http://owntracks.org/booklet/tech/json/#_typetransition
|
||||
data = validate_payload(payload, 'transition')
|
||||
data = validate_payload(payload, VALIDATE_TRANSITION)
|
||||
if not data:
|
||||
return
|
||||
|
||||
|
@ -143,14 +148,24 @@ def setup_scanner(hass, config, see):
|
|||
else:
|
||||
_LOGGER.info("Exit to GPS")
|
||||
# Check for GPS accuracy
|
||||
if not ('acc' in data and
|
||||
max_gps_accuracy is not None and
|
||||
data['acc'] > max_gps_accuracy):
|
||||
|
||||
valid_gps = True
|
||||
if 'acc' in data:
|
||||
if data['acc'] == 0.0:
|
||||
valid_gps = False
|
||||
_LOGGER.warning(
|
||||
'Ignoring GPS in region exit because accuracy'
|
||||
'is zero: %s',
|
||||
payload)
|
||||
if (max_gps_accuracy is not None and
|
||||
data['acc'] > max_gps_accuracy):
|
||||
valid_gps = False
|
||||
_LOGGER.warning(
|
||||
'Ignoring GPS in region exit because expected '
|
||||
'GPS accuracy %s is not met: %s',
|
||||
max_gps_accuracy, payload)
|
||||
if valid_gps:
|
||||
see(**kwargs)
|
||||
see_beacons(dev_id, kwargs)
|
||||
else:
|
||||
_LOGGER.info("Inaccurate GPS reported")
|
||||
|
||||
beacons = MOBILE_BEACONS_ACTIVE[dev_id]
|
||||
if location in beacons:
|
||||
|
|
|
@ -108,6 +108,31 @@ REGION_LEAVE_INACCURATE_MESSAGE = {
|
|||
'_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):
|
||||
"""Test the OwnTrack sensor."""
|
||||
|
||||
|
@ -293,6 +318,25 @@ class TestDeviceTrackerOwnTracks(unittest.TestCase):
|
|||
# But does exit region correctly
|
||||
self.assertFalse(owntracks.REGIONS_ENTERED[USER])
|
||||
|
||||
def test_event_entry_exit_zero_accuracy(self):
|
||||
"""Test entry/exit events with accuracy zero."""
|
||||
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):
|
||||
"""Test the event for exit zone."""
|
||||
self.send_message(EVENT_TOPIC, REGION_ENTER_MESSAGE)
|
||||
|
|
Loading…
Reference in New Issue