OwnTracks Config Entry (#18759)
* OwnTracks Config Entry * Fix test * Fix headers * Lint * Username for android only * Update translations * Tweak translation * Create config entry if not there * Update reqs * Types * Lint
This commit is contained in:
1 parent
58e0ff0b1b
commit
f860cac4ea
15 files changed
+554
-355
No files matched your search
@@ -4,12 +4,11 @@ from asynctest import patch
|
||||
import pytest
|
||||
|
||||
from tests.common import (
|
||||
assert_setup_component, async_fire_mqtt_message, mock_coro, mock_component,
|
||||
async_mock_mqtt_component)
|
||||
import homeassistant.components.device_tracker.owntracks as owntracks
|
||||
async_fire_mqtt_message, mock_coro, mock_component,
|
||||
async_mock_mqtt_component, MockConfigEntry)
|
||||
from homeassistant.components import owntracks
|
||||
from homeassistant.setup import async_setup_component
|
||||
from homeassistant.components import device_tracker
|
||||
from homeassistant.const import CONF_PLATFORM, STATE_NOT_HOME
|
||||
from homeassistant.const import STATE_NOT_HOME
|
||||
|
||||
USER = 'greg'
|
||||
DEVICE = 'phone'
|
||||
@@ -290,6 +289,25 @@ def setup_comp(hass):
|
||||
'zone.outer', 'zoning', OUTER_ZONE)
|
||||
|
||||
|
||||
async def setup_owntracks(hass, config,
|
||||
ctx_cls=owntracks.OwnTracksContext):
|
||||
"""Set up OwnTracks."""
|
||||
await async_mock_mqtt_component(hass)
|
||||
|
||||
MockConfigEntry(domain='owntracks', data={
|
||||
'webhook_id': 'owntracks_test',
|
||||
'secret': 'abcd',
|
||||
}).add_to_hass(hass)
|
||||
|
||||
with patch('homeassistant.components.device_tracker.async_load_config',
|
||||
return_value=mock_coro([])), \
|
||||
patch('homeassistant.components.device_tracker.'
|
||||
'load_yaml_config_file', return_value=mock_coro({})), \
|
||||
patch.object(owntracks, 'OwnTracksContext', ctx_cls):
|
||||
assert await async_setup_component(
|
||||
hass, 'owntracks', {'owntracks': config})
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def context(hass, setup_comp):
|
||||
"""Set up the mocked context."""
|
||||
@@ -306,20 +324,11 @@ def context(hass, setup_comp):
|
||||
context = orig_context(*args)
|
||||
return context
|
||||
|
||||
with patch('homeassistant.components.device_tracker.async_load_config',
|
||||
return_value=mock_coro([])), \
|
||||
patch('homeassistant.components.device_tracker.'
|
||||
'load_yaml_config_file', return_value=mock_coro({})), \
|
||||
patch.object(owntracks, 'OwnTracksContext', store_context), \
|
||||
assert_setup_component(1, device_tracker.DOMAIN):
|
||||
assert hass.loop.run_until_complete(async_setup_component(
|
||||
hass, device_tracker.DOMAIN, {
|
||||
device_tracker.DOMAIN: {
|
||||
CONF_PLATFORM: 'owntracks',
|
||||
CONF_MAX_GPS_ACCURACY: 200,
|
||||
CONF_WAYPOINT_IMPORT: True,
|
||||
CONF_WAYPOINT_WHITELIST: ['jon', 'greg']
|
||||
}}))
|
||||
hass.loop.run_until_complete(setup_owntracks(hass, {
|
||||
CONF_MAX_GPS_ACCURACY: 200,
|
||||
CONF_WAYPOINT_IMPORT: True,
|
||||
CONF_WAYPOINT_WHITELIST: ['jon', 'greg']
|
||||
}, store_context))
|
||||
|
||||
def get_context():
|
||||
"""Get the current context."""
|
||||
@@ -1211,19 +1220,14 @@ async def test_waypoint_import_blacklist(hass, context):
|
||||
assert wayp is None
|
||||
|
||||
|
||||
async def test_waypoint_import_no_whitelist(hass, context):
|
||||
async def test_waypoint_import_no_whitelist(hass, config_context):
|
||||
"""Test import of list of waypoints with no whitelist set."""
|
||||
async def mock_see(**kwargs):
|
||||
"""Fake see method for owntracks."""
|
||||
return
|
||||
|
||||
test_config = {
|
||||
CONF_PLATFORM: 'owntracks',
|
||||
await setup_owntracks(hass, {
|
||||
CONF_MAX_GPS_ACCURACY: 200,
|
||||
CONF_WAYPOINT_IMPORT: True,
|
||||
CONF_MQTT_TOPIC: 'owntracks/#',
|
||||
}
|
||||
await owntracks.async_setup_scanner(hass, test_config, mock_see)
|
||||
})
|
||||
|
||||
waypoints_message = WAYPOINTS_EXPORTED_MESSAGE.copy()
|
||||
await send_message(hass, WAYPOINTS_TOPIC_BLOCKED, waypoints_message)
|
||||
# Check if it made it into states
|
||||
@@ -1364,12 +1368,9 @@ def config_context(hass, setup_comp):
|
||||
mock_cipher)
|
||||
async def test_encrypted_payload(hass, config_context):
|
||||
"""Test encrypted payload."""
|
||||
with assert_setup_component(1, device_tracker.DOMAIN):
|
||||
assert await async_setup_component(hass, device_tracker.DOMAIN, {
|
||||
device_tracker.DOMAIN: {
|
||||
CONF_PLATFORM: 'owntracks',
|
||||
CONF_SECRET: TEST_SECRET_KEY,
|
||||
}})
|
||||
await setup_owntracks(hass, {
|
||||
CONF_SECRET: TEST_SECRET_KEY,
|
||||
})
|
||||
await send_message(hass, LOCATION_TOPIC, MOCK_ENCRYPTED_LOCATION_MESSAGE)
|
||||
assert_location_latitude(hass, LOCATION_MESSAGE['lat'])
|
||||
|
||||
@@ -1378,13 +1379,11 @@ async def test_encrypted_payload(hass, config_context):
|
||||
mock_cipher)
|
||||
async def test_encrypted_payload_topic_key(hass, config_context):
|
||||
"""Test encrypted payload with a topic key."""
|
||||
with assert_setup_component(1, device_tracker.DOMAIN):
|
||||
assert await async_setup_component(hass, device_tracker.DOMAIN, {
|
||||
device_tracker.DOMAIN: {
|
||||
CONF_PLATFORM: 'owntracks',
|
||||
CONF_SECRET: {
|
||||
LOCATION_TOPIC: TEST_SECRET_KEY,
|
||||
}}})
|
||||
await setup_owntracks(hass, {
|
||||
CONF_SECRET: {
|
||||
LOCATION_TOPIC: TEST_SECRET_KEY,
|
||||
}
|
||||
})
|
||||
await send_message(hass, LOCATION_TOPIC, MOCK_ENCRYPTED_LOCATION_MESSAGE)
|
||||
assert_location_latitude(hass, LOCATION_MESSAGE['lat'])
|
||||
|
||||
@@ -1394,12 +1393,10 @@ async def test_encrypted_payload_topic_key(hass, config_context):
|
||||
async def test_encrypted_payload_no_key(hass, config_context):
|
||||
"""Test encrypted payload with no key, ."""
|
||||
assert hass.states.get(DEVICE_TRACKER_STATE) is None
|
||||
with assert_setup_component(1, device_tracker.DOMAIN):
|
||||
assert await async_setup_component(hass, device_tracker.DOMAIN, {
|
||||
device_tracker.DOMAIN: {
|
||||
CONF_PLATFORM: 'owntracks',
|
||||
# key missing
|
||||
}})
|
||||
await setup_owntracks(hass, {
|
||||
CONF_SECRET: {
|
||||
}
|
||||
})
|
||||
await send_message(hass, LOCATION_TOPIC, MOCK_ENCRYPTED_LOCATION_MESSAGE)
|
||||
assert hass.states.get(DEVICE_TRACKER_STATE) is None
|
||||
|
||||
@@ -1408,12 +1405,9 @@ async def test_encrypted_payload_no_key(hass, config_context):
|
||||
mock_cipher)
|
||||
async def test_encrypted_payload_wrong_key(hass, config_context):
|
||||
"""Test encrypted payload with wrong key."""
|
||||
with assert_setup_component(1, device_tracker.DOMAIN):
|
||||
assert await async_setup_component(hass, device_tracker.DOMAIN, {
|
||||
device_tracker.DOMAIN: {
|
||||
CONF_PLATFORM: 'owntracks',
|
||||
CONF_SECRET: 'wrong key',
|
||||
}})
|
||||
await setup_owntracks(hass, {
|
||||
CONF_SECRET: 'wrong key',
|
||||
})
|
||||
await send_message(hass, LOCATION_TOPIC, MOCK_ENCRYPTED_LOCATION_MESSAGE)
|
||||
assert hass.states.get(DEVICE_TRACKER_STATE) is None
|
||||
|
||||
@@ -1422,13 +1416,11 @@ async def test_encrypted_payload_wrong_key(hass, config_context):
|
||||
mock_cipher)
|
||||
async def test_encrypted_payload_wrong_topic_key(hass, config_context):
|
||||
"""Test encrypted payload with wrong topic key."""
|
||||
with assert_setup_component(1, device_tracker.DOMAIN):
|
||||
assert await async_setup_component(hass, device_tracker.DOMAIN, {
|
||||
device_tracker.DOMAIN: {
|
||||
CONF_PLATFORM: 'owntracks',
|
||||
CONF_SECRET: {
|
||||
LOCATION_TOPIC: 'wrong key'
|
||||
}}})
|
||||
await setup_owntracks(hass, {
|
||||
CONF_SECRET: {
|
||||
LOCATION_TOPIC: 'wrong key'
|
||||
},
|
||||
})
|
||||
await send_message(hass, LOCATION_TOPIC, MOCK_ENCRYPTED_LOCATION_MESSAGE)
|
||||
assert hass.states.get(DEVICE_TRACKER_STATE) is None
|
||||
|
||||
@@ -1437,13 +1429,10 @@ async def test_encrypted_payload_wrong_topic_key(hass, config_context):
|
||||
mock_cipher)
|
||||
async def test_encrypted_payload_no_topic_key(hass, config_context):
|
||||
"""Test encrypted payload with no topic key."""
|
||||
with assert_setup_component(1, device_tracker.DOMAIN):
|
||||
assert await async_setup_component(hass, device_tracker.DOMAIN, {
|
||||
device_tracker.DOMAIN: {
|
||||
CONF_PLATFORM: 'owntracks',
|
||||
CONF_SECRET: {
|
||||
'owntracks/{}/{}'.format(USER, 'otherdevice'): 'foobar'
|
||||
}}})
|
||||
await setup_owntracks(hass, {
|
||||
CONF_SECRET: {
|
||||
'owntracks/{}/{}'.format(USER, 'otherdevice'): 'foobar'
|
||||
}})
|
||||
await send_message(hass, LOCATION_TOPIC, MOCK_ENCRYPTED_LOCATION_MESSAGE)
|
||||
assert hass.states.get(DEVICE_TRACKER_STATE) is None
|
||||
|
||||
@@ -1456,12 +1445,9 @@ async def test_encrypted_payload_libsodium(hass, config_context):
|
||||
pytest.skip("libnacl/libsodium is not installed")
|
||||
return
|
||||
|
||||
with assert_setup_component(1, device_tracker.DOMAIN):
|
||||
assert await async_setup_component(hass, device_tracker.DOMAIN, {
|
||||
device_tracker.DOMAIN: {
|
||||
CONF_PLATFORM: 'owntracks',
|
||||
CONF_SECRET: TEST_SECRET_KEY,
|
||||
}})
|
||||
await setup_owntracks(hass, {
|
||||
CONF_SECRET: TEST_SECRET_KEY,
|
||||
})
|
||||
|
||||
await send_message(hass, LOCATION_TOPIC, ENCRYPTED_LOCATION_MESSAGE)
|
||||
assert_location_latitude(hass, LOCATION_MESSAGE['lat'])
|
||||
@@ -1469,12 +1455,9 @@ async def test_encrypted_payload_libsodium(hass, config_context):
|
||||
|
||||
async def test_customized_mqtt_topic(hass, config_context):
|
||||
"""Test subscribing to a custom mqtt topic."""
|
||||
with assert_setup_component(1, device_tracker.DOMAIN):
|
||||
assert await async_setup_component(hass, device_tracker.DOMAIN, {
|
||||
device_tracker.DOMAIN: {
|
||||
CONF_PLATFORM: 'owntracks',
|
||||
CONF_MQTT_TOPIC: 'mytracks/#',
|
||||
}})
|
||||
await setup_owntracks(hass, {
|
||||
CONF_MQTT_TOPIC: 'mytracks/#',
|
||||
})
|
||||
|
||||
topic = 'mytracks/{}/{}'.format(USER, DEVICE)
|
||||
|
||||
@@ -1484,14 +1467,11 @@ async def test_customized_mqtt_topic(hass, config_context):
|
||||
|
||||
async def test_region_mapping(hass, config_context):
|
||||
"""Test region to zone mapping."""
|
||||
with assert_setup_component(1, device_tracker.DOMAIN):
|
||||
assert await async_setup_component(hass, device_tracker.DOMAIN, {
|
||||
device_tracker.DOMAIN: {
|
||||
CONF_PLATFORM: 'owntracks',
|
||||
CONF_REGION_MAPPING: {
|
||||
'foo': 'inner'
|
||||
},
|
||||
}})
|
||||
await setup_owntracks(hass, {
|
||||
CONF_REGION_MAPPING: {
|
||||
'foo': 'inner'
|
||||
},
|
||||
})
|
||||
|
||||
hass.states.async_set(
|
||||
'zone.inner', 'zoning', INNER_ZONE)
|
||||
|
||||
@@ -1,124 +0,0 @@
|
||||
"""Test the owntracks_http platform."""
|
||||
import asyncio
|
||||
from unittest.mock import patch
|
||||
|
||||
import os
|
||||
import pytest
|
||||
|
||||
from homeassistant.components import device_tracker
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from tests.common import mock_component, mock_coro
|
||||
|
||||
MINIMAL_LOCATION_MESSAGE = {
|
||||
'_type': 'location',
|
||||
'lon': 45,
|
||||
'lat': 90,
|
||||
'p': 101.3977584838867,
|
||||
'tid': 'test',
|
||||
'tst': 1,
|
||||
}
|
||||
|
||||
LOCATION_MESSAGE = {
|
||||
'_type': 'location',
|
||||
'acc': 60,
|
||||
'alt': 27,
|
||||
'batt': 92,
|
||||
'cog': 248,
|
||||
'lon': 45,
|
||||
'lat': 90,
|
||||
'p': 101.3977584838867,
|
||||
'tid': 'test',
|
||||
't': 'u',
|
||||
'tst': 1,
|
||||
'vac': 4,
|
||||
'vel': 0
|
||||
}
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def owntracks_http_cleanup(hass):
|
||||
"""Remove known_devices.yaml."""
|
||||
try:
|
||||
os.remove(hass.config.path(device_tracker.YAML_DEVICES))
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_client(hass, aiohttp_client):
|
||||
"""Start the Hass HTTP component."""
|
||||
mock_component(hass, 'group')
|
||||
mock_component(hass, 'zone')
|
||||
with patch('homeassistant.components.device_tracker.async_load_config',
|
||||
return_value=mock_coro([])):
|
||||
hass.loop.run_until_complete(
|
||||
async_setup_component(hass, 'device_tracker', {
|
||||
'device_tracker': {
|
||||
'platform': 'owntracks_http',
|
||||
'webhook_id': 'owntracks_test'
|
||||
}
|
||||
}))
|
||||
return hass.loop.run_until_complete(aiohttp_client(hass.http.app))
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_handle_valid_message(mock_client):
|
||||
"""Test that we forward messages correctly to OwnTracks."""
|
||||
resp = yield from mock_client.post('/api/webhook/owntracks_test?'
|
||||
'u=test&d=test',
|
||||
json=LOCATION_MESSAGE)
|
||||
|
||||
assert resp.status == 200
|
||||
|
||||
json = yield from resp.json()
|
||||
assert json == []
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_handle_valid_minimal_message(mock_client):
|
||||
"""Test that we forward messages correctly to OwnTracks."""
|
||||
resp = yield from mock_client.post('/api/webhook/owntracks_test?'
|
||||
'u=test&d=test',
|
||||
json=MINIMAL_LOCATION_MESSAGE)
|
||||
|
||||
assert resp.status == 200
|
||||
|
||||
json = yield from resp.json()
|
||||
assert json == []
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_handle_value_error(mock_client):
|
||||
"""Test we don't disclose that this is a valid webhook."""
|
||||
resp = yield from mock_client.post('/api/webhook/owntracks_test'
|
||||
'?u=test&d=test', json='')
|
||||
|
||||
assert resp.status == 200
|
||||
|
||||
json = yield from resp.text()
|
||||
assert json == ""
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_returns_error_missing_username(mock_client):
|
||||
"""Test that an error is returned when username is missing."""
|
||||
resp = yield from mock_client.post('/api/webhook/owntracks_test?d=test',
|
||||
json=LOCATION_MESSAGE)
|
||||
|
||||
assert resp.status == 200
|
||||
|
||||
json = yield from resp.json()
|
||||
assert json == {'error': 'You need to supply username.'}
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_returns_error_missing_device(mock_client):
|
||||
"""Test that an error is returned when device name is missing."""
|
||||
resp = yield from mock_client.post('/api/webhook/owntracks_test?u=test',
|
||||
json=LOCATION_MESSAGE)
|
||||
|
||||
assert resp.status == 200
|
||||
|
||||
json = yield from resp.json()
|
||||
assert json == {'error': 'You need to supply device name.'}
|
||||
Reference in new issue
Block a user