Simplify logic

This commit is contained in:
Philip Lundrigan 2015-12-31 12:02:50 -07:00
parent 5d953061e8
commit ce152e9c94
2 changed files with 20 additions and 74 deletions

View File

@ -11,10 +11,11 @@ from functools import partial
from homeassistant.const import (
HTTP_UNPROCESSABLE_ENTITY, STATE_NOT_HOME)
from homeassistant.components.device_tracker import DOMAIN
_LOGGER = logging.getLogger(__name__)
DEPENDENCIES = ['http', 'zone']
DEPENDENCIES = ['http']
URL_API_LOCATIVE_ENDPOINT = "/api/locative"
@ -43,31 +44,15 @@ def _handle_get_api_locative(hass, see, handler, path_match, data):
location_name = data['id'].lower()
direction = data['trigger']
try:
gps_coords = (float(data['latitude']), float(data['longitude']))
except ValueError:
handler.write_text("Invalid latitude / longitude format.",
HTTP_UNPROCESSABLE_ENTITY)
_LOGGER.error("Received invalid latitude / longitude format.")
return
if direction == 'enter':
zones = [state for state in hass.states.entity_ids('zone')]
if "zone.{}".format(location_name) in zones:
see(dev_id=device, location_name=location_name)
handler.write_text(
"Setting location to {}".format(location_name))
else:
see(dev_id=device, gps=gps_coords)
handler.write_text(
"Setting location to {}".format(gps_coords))
see(dev_id=device, location_name=location_name)
handler.write_text("Setting location to {}".format(location_name))
elif direction == 'exit':
current_zone = hass.states.get(
"{}.{}".format("device_tracker", device)).state
current_state = hass.states.get(
"{}.{}".format(DOMAIN, device)).state
if current_zone == location_name:
if current_state == location_name:
see(dev_id=device, location_name=STATE_NOT_HOME)
handler.write_text("Setting location to not home")
else:
@ -77,8 +62,7 @@ def _handle_get_api_locative(hass, see, handler, path_match, data):
# first, then the exit message will be sent second.
handler.write_text(
'Ignoring exit from {} (already in {})'.format(
location_name,
current_zone.split('.')[-1]))
location_name, current_state))
elif direction == 'test':
# In the app, a test message can be sent. Just return something to

View File

@ -36,24 +36,6 @@ def setUpModule(mock_get_local_ip): # pylint: disable=invalid-name
hass = ha.HomeAssistant()
# Set up a couple of zones
bootstrap.setup_component(hass, zone.DOMAIN, {
zone.DOMAIN: [
{
'name': 'Home',
'latitude': 41.7855,
'longitude': -110.7367,
'radius': 200
},
{
'name': 'Work',
'latitude': 41.5855,
'longitude': -110.9367,
'radius': 100
}
]
})
# Set up server
bootstrap.setup_component(hass, http.DOMAIN, {
http.DOMAIN: {
@ -120,12 +102,6 @@ class TestLocative(unittest.TestCase):
req = requests.get(_url(copy))
self.assertEqual(422, req.status_code)
# Bad longitude
copy = data.copy()
copy['longitude'] = 'hello world'
req = requests.get(_url(copy))
self.assertEqual(422, req.status_code)
# Test message
copy = data.copy()
copy['trigger'] = 'test'
@ -139,7 +115,7 @@ class TestLocative(unittest.TestCase):
self.assertEqual(422, req.status_code)
def test_known_zone(self, update_config):
def test_enter_and_exit(self, update_config):
""" Test when there is a known zone """
data = {
'latitude': 40.7855,
@ -173,36 +149,22 @@ class TestLocative(unittest.TestCase):
state_name = hass.states.get('{}.{}'.format('device_tracker', data['device'])).state
self.assertEqual(state_name, 'home')
def test_unknown_zone(self, update_config):
""" Test when there is no known zone """
data = {
'latitude': 40.7855,
'longitude': -111.7367,
'device': '123',
'id': 'Foobar',
'trigger': 'enter'
}
# Enter Foobar
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')
self.assertEqual(state.attributes['latitude'], data['latitude'])
self.assertEqual(state.attributes['longitude'], data['longitude'])
data['trigger'] = 'exit'
# Exit Foobar
# Exit Home
req = requests.get(_url(data))
self.assertEqual(200, req.status_code)
state_name = hass.states.get('{}.{}'.format('device_tracker', data['device'])).state
self.assertEqual(state_name, 'not_home')
state = hass.states.get('{}.{}'.format('device_tracker', data['device']))
self.assertEqual(state.state, 'not_home')
self.assertEqual(state.attributes['latitude'], data['latitude'])
self.assertEqual(state.attributes['longitude'], data['longitude'])
data['id'] = 'work'
data['trigger'] = 'enter'
# Enter Work
req = requests.get(_url(data))
self.assertEqual(200, req.status_code)
state_name = hass.states.get('{}.{}'.format('device_tracker', data['device'])).state
self.assertEqual(state_name, 'work')
def test_exit_after_enter(self, update_config):