* Upgrade pytest-aiohttp * Make sure executors, tasks and timers are closed Some test will trigger warnings on garbage collect, these warnings spills over into next test. Some test trigger tasks that raise errors on shutdown, these spill over into next test. This is to mimic older pytest-aiohttp and it's behaviour on test cleanup. Discussions on similar changes for pytest-aiohttp are here: https://github.com/pytest-dev/pytest-asyncio/pull/309 * Replace loop with event_loop * Make sure time is frozen for tests * Make sure the ConditionType is not async /home-assistant/homeassistant/helpers/template.py:2082: RuntimeWarning: coroutine 'AsyncMockMixin._execute_mock_call' was never awaited def wrapper(*args, **kwargs): Enable tracemalloc to get traceback where the object was allocated. See https://docs.pytest.org/en/stable/how-to/capture-warnings.html#resource-warnings for more info. * Increase litejet press tests with a factor 10 The times are simulated anyway, and we can't stop the normal event from occuring. * Use async handlers for aiohttp tests/components/motioneye/test_camera.py::test_get_still_image_from_camera tests/components/motioneye/test_camera.py::test_get_still_image_from_camera tests/components/motioneye/test_camera.py::test_get_stream_from_camera tests/components/motioneye/test_camera.py::test_get_stream_from_camera tests/components/motioneye/test_camera.py::test_camera_option_stream_url_template tests/components/motioneye/test_camera.py::test_camera_option_stream_url_template /Users/joakim/src/hass/home-assistant/venv/lib/python3.9/site-packages/aiohttp/web_urldispatcher.py:189: DeprecationWarning: Bare functions are deprecated, use async ones warnings.warn( * Switch to freezegun in modbus tests The tests allowed clock to tick in between steps * Make sure skybell object are fully mocked Old tests would trigger attempts to post to could services: ``` DEBUG:aioskybell:HTTP post https://cloud.myskybell.com/api/v3/login/ Request with headers: {'content-type': 'application/json', 'accept': '*/*', 'x-skybell-app-id': 'd2b542c7-a7e4-4e1e-b77d-2b76911c7c46', 'x-skybell-client-id': '1f36a3c0-6dee-4997-a6db-4e1c67338e57'} ``` * Fix sorting that broke after rebase
242 lines
7.8 KiB
Python
242 lines
7.8 KiB
Python
"""The tests the for Traccar device tracker platform."""
|
|
from http import HTTPStatus
|
|
from unittest.mock import patch
|
|
|
|
import pytest
|
|
|
|
from homeassistant import config_entries, data_entry_flow
|
|
from homeassistant.components import traccar, zone
|
|
from homeassistant.components.device_tracker import DOMAIN as DEVICE_TRACKER_DOMAIN
|
|
from homeassistant.components.traccar import DOMAIN, TRACKER_UPDATE
|
|
from homeassistant.config import async_process_ha_core_config
|
|
from homeassistant.const import STATE_HOME, STATE_NOT_HOME
|
|
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
|
from homeassistant.helpers.dispatcher import DATA_DISPATCHER
|
|
from homeassistant.setup import async_setup_component
|
|
|
|
HOME_LATITUDE = 37.239622
|
|
HOME_LONGITUDE = -115.815811
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def mock_dev_track(mock_device_tracker_conf):
|
|
"""Mock device tracker config loading."""
|
|
|
|
|
|
@pytest.fixture(name="client")
|
|
async def traccar_client(event_loop, hass, hass_client_no_auth):
|
|
"""Mock client for Traccar (unauthenticated)."""
|
|
|
|
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {}})
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
with patch("homeassistant.components.device_tracker.legacy.update_config"):
|
|
return await hass_client_no_auth()
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
async def setup_zones(event_loop, hass):
|
|
"""Set up Zone config in HA."""
|
|
assert await async_setup_component(
|
|
hass,
|
|
zone.DOMAIN,
|
|
{
|
|
"zone": {
|
|
"name": "Home",
|
|
"latitude": HOME_LATITUDE,
|
|
"longitude": HOME_LONGITUDE,
|
|
"radius": 100,
|
|
}
|
|
},
|
|
)
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
@pytest.fixture(name="webhook_id")
|
|
async def webhook_id_fixture(hass, client):
|
|
"""Initialize the Traccar component and get the webhook_id."""
|
|
await async_process_ha_core_config(
|
|
hass,
|
|
{"external_url": "http://example.com"},
|
|
)
|
|
result = await hass.config_entries.flow.async_init(
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
)
|
|
assert result["type"] == data_entry_flow.FlowResultType.FORM, result
|
|
|
|
result = await hass.config_entries.flow.async_configure(result["flow_id"], {})
|
|
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
|
|
|
|
await hass.async_block_till_done()
|
|
return result["result"].data["webhook_id"]
|
|
|
|
|
|
async def test_missing_data(hass, client, webhook_id):
|
|
"""Test missing data."""
|
|
url = f"/api/webhook/{webhook_id}"
|
|
data = {"lat": "1.0", "lon": "1.1", "id": "123"}
|
|
|
|
# No data
|
|
req = await client.post(url)
|
|
await hass.async_block_till_done()
|
|
assert req.status == HTTPStatus.UNPROCESSABLE_ENTITY
|
|
|
|
# No latitude
|
|
copy = data.copy()
|
|
del copy["lat"]
|
|
req = await client.post(url, params=copy)
|
|
await hass.async_block_till_done()
|
|
assert req.status == HTTPStatus.UNPROCESSABLE_ENTITY
|
|
|
|
# No device
|
|
copy = data.copy()
|
|
del copy["id"]
|
|
req = await client.post(url, params=copy)
|
|
await hass.async_block_till_done()
|
|
assert req.status == HTTPStatus.UNPROCESSABLE_ENTITY
|
|
|
|
|
|
async def test_enter_and_exit(hass, client, webhook_id):
|
|
"""Test when there is a known zone."""
|
|
url = f"/api/webhook/{webhook_id}"
|
|
data = {"lat": str(HOME_LATITUDE), "lon": str(HOME_LONGITUDE), "id": "123"}
|
|
|
|
# Enter the Home
|
|
req = await client.post(url, params=data)
|
|
await hass.async_block_till_done()
|
|
assert req.status == HTTPStatus.OK
|
|
state_name = hass.states.get(
|
|
"{}.{}".format(DEVICE_TRACKER_DOMAIN, data["id"])
|
|
).state
|
|
assert state_name == STATE_HOME
|
|
|
|
# Enter Home again
|
|
req = await client.post(url, params=data)
|
|
await hass.async_block_till_done()
|
|
assert req.status == HTTPStatus.OK
|
|
state_name = hass.states.get(
|
|
"{}.{}".format(DEVICE_TRACKER_DOMAIN, data["id"])
|
|
).state
|
|
assert state_name == STATE_HOME
|
|
|
|
data["lon"] = 0
|
|
data["lat"] = 0
|
|
|
|
# Enter Somewhere else
|
|
req = await client.post(url, params=data)
|
|
await hass.async_block_till_done()
|
|
assert req.status == HTTPStatus.OK
|
|
state_name = hass.states.get(
|
|
"{}.{}".format(DEVICE_TRACKER_DOMAIN, data["id"])
|
|
).state
|
|
assert state_name == STATE_NOT_HOME
|
|
|
|
dev_reg = dr.async_get(hass)
|
|
assert len(dev_reg.devices) == 1
|
|
|
|
ent_reg = er.async_get(hass)
|
|
assert len(ent_reg.entities) == 1
|
|
|
|
|
|
async def test_enter_with_attrs(hass, client, webhook_id):
|
|
"""Test when additional attributes are present."""
|
|
url = f"/api/webhook/{webhook_id}"
|
|
data = {
|
|
"timestamp": 123456789,
|
|
"lat": "1.0",
|
|
"lon": "1.1",
|
|
"id": "123",
|
|
"accuracy": "10.5",
|
|
"batt": 10,
|
|
"speed": 100,
|
|
"bearing": "105.32",
|
|
"altitude": 102,
|
|
}
|
|
|
|
req = await client.post(url, params=data)
|
|
await hass.async_block_till_done()
|
|
assert req.status == HTTPStatus.OK
|
|
state = hass.states.get("{}.{}".format(DEVICE_TRACKER_DOMAIN, data["id"]))
|
|
assert state.state == STATE_NOT_HOME
|
|
assert state.attributes["gps_accuracy"] == 10.5
|
|
assert state.attributes["battery_level"] == 10.0
|
|
assert state.attributes["speed"] == 100.0
|
|
assert state.attributes["bearing"] == 105.32
|
|
assert state.attributes["altitude"] == 102.0
|
|
|
|
data = {
|
|
"lat": str(HOME_LATITUDE),
|
|
"lon": str(HOME_LONGITUDE),
|
|
"id": "123",
|
|
"accuracy": 123,
|
|
"batt": 23,
|
|
"speed": 23,
|
|
"bearing": 123,
|
|
"altitude": 123,
|
|
}
|
|
|
|
req = await client.post(url, params=data)
|
|
await hass.async_block_till_done()
|
|
assert req.status == HTTPStatus.OK
|
|
state = hass.states.get("{}.{}".format(DEVICE_TRACKER_DOMAIN, data["id"]))
|
|
assert state.state == STATE_HOME
|
|
assert state.attributes["gps_accuracy"] == 123
|
|
assert state.attributes["battery_level"] == 23
|
|
assert state.attributes["speed"] == 23
|
|
assert state.attributes["bearing"] == 123
|
|
assert state.attributes["altitude"] == 123
|
|
|
|
|
|
async def test_two_devices(hass, client, webhook_id):
|
|
"""Test updating two different devices."""
|
|
url = f"/api/webhook/{webhook_id}"
|
|
|
|
data_device_1 = {"lat": "1.0", "lon": "1.1", "id": "device_1"}
|
|
|
|
# Exit Home
|
|
req = await client.post(url, params=data_device_1)
|
|
await hass.async_block_till_done()
|
|
assert req.status == HTTPStatus.OK
|
|
|
|
state = hass.states.get("{}.{}".format(DEVICE_TRACKER_DOMAIN, data_device_1["id"]))
|
|
assert state.state == "not_home"
|
|
|
|
# Enter Home
|
|
data_device_2 = dict(data_device_1)
|
|
data_device_2["lat"] = str(HOME_LATITUDE)
|
|
data_device_2["lon"] = str(HOME_LONGITUDE)
|
|
data_device_2["id"] = "device_2"
|
|
req = await client.post(url, params=data_device_2)
|
|
await hass.async_block_till_done()
|
|
assert req.status == HTTPStatus.OK
|
|
|
|
state = hass.states.get("{}.{}".format(DEVICE_TRACKER_DOMAIN, data_device_2["id"]))
|
|
assert state.state == "home"
|
|
state = hass.states.get("{}.{}".format(DEVICE_TRACKER_DOMAIN, data_device_1["id"]))
|
|
assert state.state == "not_home"
|
|
|
|
|
|
@pytest.mark.xfail(
|
|
reason="The device_tracker component does not support unloading yet."
|
|
)
|
|
async def test_load_unload_entry(hass, client, webhook_id):
|
|
"""Test that the appropriate dispatch signals are added and removed."""
|
|
url = f"/api/webhook/{webhook_id}"
|
|
data = {"lat": str(HOME_LATITUDE), "lon": str(HOME_LONGITUDE), "id": "123"}
|
|
|
|
# Enter the Home
|
|
req = await client.post(url, params=data)
|
|
await hass.async_block_till_done()
|
|
assert req.status == HTTPStatus.OK
|
|
state_name = hass.states.get(
|
|
"{}.{}".format(DEVICE_TRACKER_DOMAIN, data["id"])
|
|
).state
|
|
assert state_name == STATE_HOME
|
|
assert len(hass.data[DATA_DISPATCHER][TRACKER_UPDATE]) == 1
|
|
|
|
entry = hass.config_entries.async_entries(DOMAIN)[0]
|
|
|
|
assert await traccar.async_unload_entry(hass, entry)
|
|
await hass.async_block_till_done()
|
|
assert not hass.data[DATA_DISPATCHER][TRACKER_UPDATE]
|