From f96c5aa62f91ba6301e45adbeec55db4fde1870d Mon Sep 17 00:00:00 2001 From: Philip Lundrigan Date: Fri, 15 Jan 2016 13:07:26 -0700 Subject: [PATCH] Fix bug in locative logic --- .../components/device_tracker/locative.py | 5 ++--- .../components/device_tracker/test_locative.py | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/device_tracker/locative.py b/homeassistant/components/device_tracker/locative.py index e7532d1075d..11884829600 100644 --- a/homeassistant/components/device_tracker/locative.py +++ b/homeassistant/components/device_tracker/locative.py @@ -49,10 +49,9 @@ def _handle_get_api_locative(hass, see, handler, path_match, data): handler.write_text("Setting location to {}".format(location_name)) elif direction == 'exit': - current_state = hass.states.get( - "{}.{}".format(DOMAIN, device)).state + current_state = hass.states.get("{}.{}".format(DOMAIN, device)) - if current_state == location_name: + if current_state is None or current_state.state == location_name: see(dev_id=device, location_name=STATE_NOT_HOME) handler.write_text("Setting location to not home") else: diff --git a/tests/components/device_tracker/test_locative.py b/tests/components/device_tracker/test_locative.py index 619fe929ac7..cd02380d324 100644 --- a/tests/components/device_tracker/test_locative.py +++ b/tests/components/device_tracker/test_locative.py @@ -203,3 +203,21 @@ class TestLocative(unittest.TestCase): state = hass.states.get('{}.{}'.format('device_tracker', data['device'])) self.assertEqual(state.state, 'work') + + def test_exit_first(self, update_config): + """ Test when an exit message is sent first on a new device """ + + data = { + 'latitude': 40.7855, + 'longitude': -111.7367, + 'device': 'new_device', + 'id': 'Home', + 'trigger': 'exit' + } + + # Exit Home + req = requests.get(_url(data)) + self.assertEqual(200, req.status_code) + + state = hass.states.get('{}.{}'.format('device_tracker', data['device'])) + self.assertEqual(state.state, 'not_home')