From 536424b0c84a7684c0bb54a59a6b87d6a0639105 Mon Sep 17 00:00:00 2001 From: Conrad Juhl Andersen Date: Thu, 18 Jan 2018 23:00:20 +0100 Subject: [PATCH] Owntracks: Use bluetooth_le as source_type if beacon was used for location change. (#11615) * Use bluetooth_le source_type, if location was changed by beacon * No reason to do nested ifs * Added tests for source_type on owntracks * Fixed The Hound * Added test and fixed bug surfaced by test --- .../components/device_tracker/owntracks.py | 10 +++++- .../device_tracker/test_owntracks.py | 31 +++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/device_tracker/owntracks.py b/homeassistant/components/device_tracker/owntracks.py index 32d677a59db..1742a0aed95 100644 --- a/homeassistant/components/device_tracker/owntracks.py +++ b/homeassistant/components/device_tracker/owntracks.py @@ -15,7 +15,10 @@ import voluptuous as vol import homeassistant.components.mqtt as mqtt import homeassistant.helpers.config_validation as cv from homeassistant.components import zone as zone_comp -from homeassistant.components.device_tracker import PLATFORM_SCHEMA +from homeassistant.components.device_tracker import ( + PLATFORM_SCHEMA, ATTR_SOURCE_TYPE, SOURCE_TYPE_BLUETOOTH_LE, + SOURCE_TYPE_GPS +) from homeassistant.const import STATE_HOME from homeassistant.core import callback from homeassistant.util import slugify, decorator @@ -140,6 +143,11 @@ def _parse_see_args(message, subscribe_topic): kwargs['attributes']['tid'] = message['tid'] if 'addr' in message: kwargs['attributes']['address'] = message['addr'] + if 't' in message: + if message['t'] == 'c': + kwargs['attributes'][ATTR_SOURCE_TYPE] = SOURCE_TYPE_GPS + if message['t'] == 'b': + kwargs['attributes'][ATTR_SOURCE_TYPE] = SOURCE_TYPE_BLUETOOTH_LE return dev_id, kwargs diff --git a/tests/components/device_tracker/test_owntracks.py b/tests/components/device_tracker/test_owntracks.py index 5f1f29e7697..44c0e0c6295 100644 --- a/tests/components/device_tracker/test_owntracks.py +++ b/tests/components/device_tracker/test_owntracks.py @@ -316,6 +316,11 @@ class BaseMQTT(unittest.TestCase): state = self.hass.states.get(DEVICE_TRACKER_STATE) self.assertEqual(state.attributes.get('gps_accuracy'), accuracy) + def assert_location_source_type(self, source_type): + """Test the assertion of source_type.""" + state = self.hass.states.get(DEVICE_TRACKER_STATE) + self.assertEqual(state.attributes.get('source_type'), source_type) + class TestDeviceTrackerOwnTracks(BaseMQTT): """Test the OwnTrack sensor.""" @@ -666,6 +671,32 @@ class TestDeviceTrackerOwnTracks(BaseMQTT): # Location update processed self.assert_location_state('outer') + def test_event_source_type_entry_exit(self): + """Test the entry and exit events of source type.""" + # Entering the owntrack circular region named "inner" + self.send_message(EVENT_TOPIC, REGION_GPS_ENTER_MESSAGE) + + # source_type should be gps when enterings using gps. + self.assert_location_source_type('gps') + + # owntracks shouldn't send beacon events with acc = 0 + self.send_message(EVENT_TOPIC, build_message( + {'acc': 1}, REGION_BEACON_ENTER_MESSAGE)) + + # We should be able to enter a beacon zone even inside a gps zone + self.assert_location_source_type('bluetooth_le') + + self.send_message(EVENT_TOPIC, REGION_GPS_LEAVE_MESSAGE) + + # source_type should be gps when leaving using gps. + self.assert_location_source_type('gps') + + # owntracks shouldn't send beacon events with acc = 0 + self.send_message(EVENT_TOPIC, build_message( + {'acc': 1}, REGION_BEACON_LEAVE_MESSAGE)) + + self.assert_location_source_type('bluetooth_le') + # Region Beacon based event entry / exit testing def test_event_region_entry_exit(self):