New features for Owntracks device_tracker (#11480)

* New features for Owntracks device_tracker

- Supporting a mapping of region names in OT to zones in HA, allowing
  separate namespaces in both applications. This is especially helpful
  if using one OT instance to update geofences for multiple homes.
- Creating a setting to ignore all location updates, allowing users to
  rely completely on enter and leave events. I have personally always
  used OT integrations with home automation this way and find it the
  most reliable.
- Allowing the OT topic to be overridden in configuration

* Fixing configuration of MQTT topic, related tests

* Tests for Owntracks events_only feature

* Tests for customizing mqtt topic, region mapping

* Fixing _parse and http for owntracks custom topic

* Making tests more thorough and cleaning up lint
This commit is contained in:
Chris Cowart authored and Martin Hjelmare committed 2018-01-08 08:16:45 +01:00
1 parent c53fc94e84
commit 8c0035c5b3
3 files changed
+139 -17

No files matched your search

@@ -35,6 +35,9 @@ CONF_MAX_GPS_ACCURACY = 'max_gps_accuracy'
CONF_WAYPOINT_IMPORT = owntracks.CONF_WAYPOINT_IMPORT
CONF_WAYPOINT_WHITELIST = owntracks.CONF_WAYPOINT_WHITELIST
CONF_SECRET = owntracks.CONF_SECRET
CONF_MQTT_TOPIC = owntracks.CONF_MQTT_TOPIC
CONF_EVENTS_ONLY = owntracks.CONF_EVENTS_ONLY
CONF_REGION_MAPPING = owntracks.CONF_REGION_MAPPING
TEST_ZONE_LAT = 45.0
TEST_ZONE_LON = 90.0
@@ -179,6 +182,13 @@ REGION_GPS_LEAVE_MESSAGE_OUTER = build_message(
'event': 'leave'},
DEFAULT_TRANSITION_MESSAGE)
REGION_GPS_ENTER_MESSAGE_OUTER = build_message(
{'lon': OUTER_ZONE['longitude'],
'lat': OUTER_ZONE['latitude'],
'desc': 'outer',
'event': 'enter'},
DEFAULT_TRANSITION_MESSAGE)
# Region Beacon messages
REGION_BEACON_ENTER_MESSAGE = DEFAULT_BEACON_TRANSITION_MESSAGE
@@ -616,6 +626,46 @@ class TestDeviceTrackerOwnTracks(BaseMQTT):
self.send_message(EVENT_TOPIC, message)
self.assert_location_state('inner')
def test_events_only_on(self):
"""Test events_only config suppresses location updates."""
# Sending a location message that is not home
self.send_message(LOCATION_TOPIC, LOCATION_MESSAGE_NOT_HOME)
self.assert_location_state(STATE_NOT_HOME)
self.context.events_only = True
# Enter and Leave messages
self.send_message(EVENT_TOPIC, REGION_GPS_ENTER_MESSAGE_OUTER)
self.assert_location_state('outer')
self.send_message(EVENT_TOPIC, REGION_GPS_LEAVE_MESSAGE_OUTER)
self.assert_location_state(STATE_NOT_HOME)
# Sending a location message that is inside outer zone
self.send_message(LOCATION_TOPIC, LOCATION_MESSAGE)
# Ignored location update. Location remains at previous.
self.assert_location_state(STATE_NOT_HOME)
def test_events_only_off(self):
"""Test when events_only is False."""
# Sending a location message that is not home
self.send_message(LOCATION_TOPIC, LOCATION_MESSAGE_NOT_HOME)
self.assert_location_state(STATE_NOT_HOME)
self.context.events_only = False
# Enter and Leave messages
self.send_message(EVENT_TOPIC, REGION_GPS_ENTER_MESSAGE_OUTER)
self.assert_location_state('outer')
self.send_message(EVENT_TOPIC, REGION_GPS_LEAVE_MESSAGE_OUTER)
self.assert_location_state(STATE_NOT_HOME)
# Sending a location message that is inside outer zone
self.send_message(LOCATION_TOPIC, LOCATION_MESSAGE)
# Location update processed
self.assert_location_state('outer')
# Region Beacon based event entry / exit testing
def test_event_region_entry_exit(self):
@@ -1111,7 +1161,8 @@ class TestDeviceTrackerOwnTracks(BaseMQTT):
test_config = {
CONF_PLATFORM: 'owntracks',
CONF_MAX_GPS_ACCURACY: 200,
CONF_WAYPOINT_IMPORT: True
CONF_WAYPOINT_IMPORT: True,
CONF_MQTT_TOPIC: 'owntracks/#',
}
run_coroutine_threadsafe(owntracks.async_setup_scanner(
self.hass, test_config, mock_see), self.hass.loop).result()
@@ -1353,3 +1404,37 @@ class TestDeviceTrackerOwnTrackConfigs(BaseMQTT):
self.send_message(LOCATION_TOPIC, ENCRYPTED_LOCATION_MESSAGE)
self.assert_location_latitude(LOCATION_MESSAGE['lat'])
def test_customized_mqtt_topic(self):
"""Test subscribing to a custom mqtt topic."""
with assert_setup_component(1, device_tracker.DOMAIN):
assert setup_component(self.hass, device_tracker.DOMAIN, {
device_tracker.DOMAIN: {
CONF_PLATFORM: 'owntracks',
CONF_MQTT_TOPIC: 'mytracks/#',
}})
topic = 'mytracks/{}/{}'.format(USER, DEVICE)
self.send_message(topic, LOCATION_MESSAGE)
self.assert_location_latitude(LOCATION_MESSAGE['lat'])
def test_region_mapping(self):
"""Test region to zone mapping."""
with assert_setup_component(1, device_tracker.DOMAIN):
assert setup_component(self.hass, device_tracker.DOMAIN, {
device_tracker.DOMAIN: {
CONF_PLATFORM: 'owntracks',
CONF_REGION_MAPPING: {
'foo': 'inner'
},
}})
self.hass.states.set(
'zone.inner', 'zoning', INNER_ZONE)
message = build_message({'desc': 'foo'}, REGION_GPS_ENTER_MESSAGE)
self.assertEqual(message['desc'], 'foo')
self.send_message(EVENT_TOPIC, message)
self.assert_location_state('inner')