mirror of
https://github.com/elisspace/core.git
synced 2026-10-05 16:56:19 +00:00
Mobile app to use device tracker config entry (#24238)
* Mobile app to use device tracker config entry * Lint * Re-use device_info * Lint
This commit is contained in:
@@ -1,74 +1 @@
|
||||
"""Tests for mobile_app component."""
|
||||
# pylint: disable=redefined-outer-name,unused-import
|
||||
import pytest
|
||||
|
||||
from tests.common import mock_device_registry
|
||||
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from homeassistant.components.mobile_app.const import (DATA_BINARY_SENSOR,
|
||||
DATA_DELETED_IDS,
|
||||
DATA_SENSOR,
|
||||
DOMAIN,
|
||||
STORAGE_KEY,
|
||||
STORAGE_VERSION)
|
||||
|
||||
from .const import REGISTER, REGISTER_CLEARTEXT
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def registry(hass):
|
||||
"""Return a configured device registry."""
|
||||
return mock_device_registry(hass)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def create_registrations(authed_api_client):
|
||||
"""Return two new registrations."""
|
||||
enc_reg = await authed_api_client.post(
|
||||
'/api/mobile_app/registrations', json=REGISTER
|
||||
)
|
||||
|
||||
assert enc_reg.status == 201
|
||||
enc_reg_json = await enc_reg.json()
|
||||
|
||||
clear_reg = await authed_api_client.post(
|
||||
'/api/mobile_app/registrations', json=REGISTER_CLEARTEXT
|
||||
)
|
||||
|
||||
assert clear_reg.status == 201
|
||||
clear_reg_json = await clear_reg.json()
|
||||
|
||||
return (enc_reg_json, clear_reg_json)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def webhook_client(hass, aiohttp_client, hass_storage, hass_admin_user):
|
||||
"""mobile_app mock client."""
|
||||
hass_storage[STORAGE_KEY] = {
|
||||
'version': STORAGE_VERSION,
|
||||
'data': {
|
||||
DATA_BINARY_SENSOR: {},
|
||||
DATA_DELETED_IDS: [],
|
||||
DATA_SENSOR: {}
|
||||
}
|
||||
}
|
||||
|
||||
await async_setup_component(hass, DOMAIN, {DOMAIN: {}})
|
||||
await hass.async_block_till_done()
|
||||
return await aiohttp_client(hass.http.app)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def authed_api_client(hass, hass_client):
|
||||
"""Provide an authenticated client for mobile_app to use."""
|
||||
await async_setup_component(hass, DOMAIN, {DOMAIN: {}})
|
||||
await hass.async_block_till_done()
|
||||
return await hass_client()
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
async def setup_ws(hass):
|
||||
"""Configure the websocket_api component."""
|
||||
assert await async_setup_component(hass, 'websocket_api', {})
|
||||
await hass.async_block_till_done()
|
||||
"""Tests for the mobile app integration."""
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
"""Tests for mobile_app component."""
|
||||
# pylint: disable=redefined-outer-name,unused-import
|
||||
import pytest
|
||||
|
||||
from tests.common import mock_device_registry
|
||||
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from homeassistant.components.mobile_app.const import DOMAIN
|
||||
|
||||
from .const import REGISTER, REGISTER_CLEARTEXT
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def registry(hass):
|
||||
"""Return a configured device registry."""
|
||||
return mock_device_registry(hass)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def create_registrations(authed_api_client):
|
||||
"""Return two new registrations."""
|
||||
enc_reg = await authed_api_client.post(
|
||||
'/api/mobile_app/registrations', json=REGISTER
|
||||
)
|
||||
|
||||
assert enc_reg.status == 201
|
||||
enc_reg_json = await enc_reg.json()
|
||||
|
||||
clear_reg = await authed_api_client.post(
|
||||
'/api/mobile_app/registrations', json=REGISTER_CLEARTEXT
|
||||
)
|
||||
|
||||
assert clear_reg.status == 201
|
||||
clear_reg_json = await clear_reg.json()
|
||||
|
||||
return (enc_reg_json, clear_reg_json)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def webhook_client(hass, aiohttp_client):
|
||||
"""mobile_app mock client."""
|
||||
await async_setup_component(hass, DOMAIN, {DOMAIN: {}})
|
||||
await hass.async_block_till_done()
|
||||
return await aiohttp_client(hass.http.app)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def authed_api_client(hass, hass_client):
|
||||
"""Provide an authenticated client for mobile_app to use."""
|
||||
await async_setup_component(hass, DOMAIN, {DOMAIN: {}})
|
||||
await hass.async_block_till_done()
|
||||
return await hass_client()
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
async def setup_ws(hass):
|
||||
"""Configure the websocket_api component."""
|
||||
assert await async_setup_component(hass, 'websocket_api', {})
|
||||
await hass.async_block_till_done()
|
||||
@@ -0,0 +1,68 @@
|
||||
"""Test mobile app device tracker."""
|
||||
|
||||
|
||||
async def test_sending_location(hass, create_registrations, webhook_client):
|
||||
"""Test sending a location via a webhook."""
|
||||
resp = await webhook_client.post(
|
||||
'/api/webhook/{}'.format(create_registrations[1]['webhook_id']),
|
||||
json={
|
||||
'type': 'update_location',
|
||||
'data': {
|
||||
'gps': [10, 20],
|
||||
'gps_accuracy': 30,
|
||||
'battery': 40,
|
||||
'altitude': 50,
|
||||
'course': 60,
|
||||
'speed': 70,
|
||||
'vertical_accuracy': 80,
|
||||
'location_name': 'bar',
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
assert resp.status == 200
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get('device_tracker.test_1')
|
||||
assert state is not None
|
||||
assert state.name == 'Test 1'
|
||||
assert state.state == 'bar'
|
||||
assert state.attributes['source_type'] == 'gps'
|
||||
assert state.attributes['latitude'] == 10
|
||||
assert state.attributes['longitude'] == 20
|
||||
assert state.attributes['gps_accuracy'] == 30
|
||||
assert state.attributes['battery_level'] == 40
|
||||
assert state.attributes['altitude'] == 50
|
||||
assert state.attributes['course'] == 60
|
||||
assert state.attributes['speed'] == 70
|
||||
assert state.attributes['vertical_accuracy'] == 80
|
||||
|
||||
resp = await webhook_client.post(
|
||||
'/api/webhook/{}'.format(create_registrations[1]['webhook_id']),
|
||||
json={
|
||||
'type': 'update_location',
|
||||
'data': {
|
||||
'gps': [1, 2],
|
||||
'gps_accuracy': 3,
|
||||
'battery': 4,
|
||||
'altitude': 5,
|
||||
'course': 6,
|
||||
'speed': 7,
|
||||
'vertical_accuracy': 8,
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
assert resp.status == 200
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get('device_tracker.test_1')
|
||||
assert state is not None
|
||||
assert state.state == 'not_home'
|
||||
assert state.attributes['source_type'] == 'gps'
|
||||
assert state.attributes['latitude'] == 1
|
||||
assert state.attributes['longitude'] == 2
|
||||
assert state.attributes['gps_accuracy'] == 3
|
||||
assert state.attributes['battery_level'] == 4
|
||||
assert state.attributes['altitude'] == 5
|
||||
assert state.attributes['course'] == 6
|
||||
assert state.attributes['speed'] == 7
|
||||
assert state.attributes['vertical_accuracy'] == 8
|
||||
@@ -2,9 +2,6 @@
|
||||
# pylint: disable=redefined-outer-name,unused-import
|
||||
import logging
|
||||
|
||||
from . import (authed_api_client, create_registrations, # noqa: F401
|
||||
webhook_client) # noqa: F401
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
|
||||
@@ -7,10 +7,9 @@ from homeassistant.const import CONF_WEBHOOK_ID
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from .const import REGISTER, RENDER_TEMPLATE
|
||||
from . import authed_api_client # noqa: F401
|
||||
|
||||
|
||||
async def test_registration(hass, hass_client): # noqa: F811
|
||||
async def test_registration(hass, hass_client):
|
||||
"""Test that registrations happen."""
|
||||
try:
|
||||
# pylint: disable=unused-import
|
||||
|
||||
@@ -11,17 +11,14 @@ from homeassistant.setup import async_setup_component
|
||||
|
||||
from tests.common import async_mock_service
|
||||
|
||||
from . import (authed_api_client, create_registrations, # noqa: F401
|
||||
webhook_client) # noqa: F401
|
||||
|
||||
from .const import (CALL_SERVICE, FIRE_EVENT, REGISTER_CLEARTEXT,
|
||||
RENDER_TEMPLATE, UPDATE)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
async def test_webhook_handle_render_template(create_registrations, # noqa: F401, F811, E501
|
||||
webhook_client): # noqa: F811
|
||||
async def test_webhook_handle_render_template(create_registrations,
|
||||
webhook_client):
|
||||
"""Test that we render templates properly."""
|
||||
resp = await webhook_client.post(
|
||||
'/api/webhook/{}'.format(create_registrations[1]['webhook_id']),
|
||||
@@ -34,7 +31,7 @@ async def test_webhook_handle_render_template(create_registrations, # noqa: F40
|
||||
assert json == {'one': 'Hello world'}
|
||||
|
||||
|
||||
async def test_webhook_handle_call_services(hass, create_registrations, # noqa: F401, F811, E501
|
||||
async def test_webhook_handle_call_services(hass, create_registrations,
|
||||
webhook_client): # noqa: E501 F811
|
||||
"""Test that we call services properly."""
|
||||
calls = async_mock_service(hass, 'test', 'mobile_app')
|
||||
@@ -49,8 +46,8 @@ async def test_webhook_handle_call_services(hass, create_registrations, # noqa:
|
||||
assert len(calls) == 1
|
||||
|
||||
|
||||
async def test_webhook_handle_fire_event(hass, create_registrations, # noqa: F401, F811, E501
|
||||
webhook_client): # noqa: F811
|
||||
async def test_webhook_handle_fire_event(hass, create_registrations,
|
||||
webhook_client):
|
||||
"""Test that we can fire events."""
|
||||
events = []
|
||||
|
||||
@@ -76,7 +73,7 @@ async def test_webhook_handle_fire_event(hass, create_registrations, # noqa: F4
|
||||
|
||||
async def test_webhook_update_registration(webhook_client, hass_client): # noqa: E501 F811
|
||||
"""Test that a we can update an existing registration via webhook."""
|
||||
authed_api_client = await hass_client() # noqa: F811
|
||||
authed_api_client = await hass_client()
|
||||
register_resp = await authed_api_client.post(
|
||||
'/api/mobile_app/registrations', json=REGISTER_CLEARTEXT
|
||||
)
|
||||
@@ -102,8 +99,8 @@ async def test_webhook_update_registration(webhook_client, hass_client): # noqa
|
||||
assert CONF_SECRET not in update_json
|
||||
|
||||
|
||||
async def test_webhook_handle_get_zones(hass, create_registrations, # noqa: F401, F811, E501
|
||||
webhook_client): # noqa: F811
|
||||
async def test_webhook_handle_get_zones(hass, create_registrations,
|
||||
webhook_client):
|
||||
"""Test that we can get zones properly."""
|
||||
await async_setup_component(hass, ZONE_DOMAIN, {
|
||||
ZONE_DOMAIN: {
|
||||
@@ -126,8 +123,8 @@ async def test_webhook_handle_get_zones(hass, create_registrations, # noqa: F40
|
||||
assert json[0]['entity_id'] == 'zone.home'
|
||||
|
||||
|
||||
async def test_webhook_handle_get_config(hass, create_registrations, # noqa: F401, F811, E501
|
||||
webhook_client): # noqa: F811
|
||||
async def test_webhook_handle_get_config(hass, create_registrations,
|
||||
webhook_client):
|
||||
"""Test that we can get config properly."""
|
||||
resp = await webhook_client.post(
|
||||
'/api/webhook/{}'.format(create_registrations[1]['webhook_id']),
|
||||
@@ -160,8 +157,8 @@ async def test_webhook_handle_get_config(hass, create_registrations, # noqa: F4
|
||||
assert expected_dict == json
|
||||
|
||||
|
||||
async def test_webhook_returns_error_incorrect_json(webhook_client, # noqa: F401, F811, E501
|
||||
create_registrations, # noqa: F401, F811, E501
|
||||
async def test_webhook_returns_error_incorrect_json(webhook_client,
|
||||
create_registrations,
|
||||
caplog): # noqa: E501 F811
|
||||
"""Test that an error is returned when JSON is invalid."""
|
||||
resp = await webhook_client.post(
|
||||
@@ -175,8 +172,8 @@ async def test_webhook_returns_error_incorrect_json(webhook_client, # noqa: F40
|
||||
assert 'invalid JSON' in caplog.text
|
||||
|
||||
|
||||
async def test_webhook_handle_decryption(webhook_client, # noqa: F811
|
||||
create_registrations): # noqa: F401, F811, E501
|
||||
async def test_webhook_handle_decryption(webhook_client,
|
||||
create_registrations):
|
||||
"""Test that we can encrypt/decrypt properly."""
|
||||
try:
|
||||
# pylint: disable=unused-import
|
||||
@@ -221,8 +218,8 @@ async def test_webhook_handle_decryption(webhook_client, # noqa: F811
|
||||
assert json.loads(decrypted_data) == {'one': 'Hello world'}
|
||||
|
||||
|
||||
async def test_webhook_requires_encryption(webhook_client, # noqa: F811
|
||||
create_registrations): # noqa: F401, F811, E501
|
||||
async def test_webhook_requires_encryption(webhook_client,
|
||||
create_registrations):
|
||||
"""Test that encrypted registrations only accept encrypted data."""
|
||||
resp = await webhook_client.post(
|
||||
'/api/webhook/{}'.format(create_registrations[0]['webhook_id']),
|
||||
|
||||
@@ -5,7 +5,6 @@ from homeassistant.components.websocket_api.const import TYPE_RESULT
|
||||
from homeassistant.const import CONF_WEBHOOK_ID
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from . import authed_api_client, setup_ws, webhook_client # noqa: F401
|
||||
from .const import (CALL_SERVICE, REGISTER)
|
||||
|
||||
|
||||
@@ -45,7 +44,7 @@ async def test_webocket_get_user_registrations(hass, aiohttp_client,
|
||||
|
||||
|
||||
async def test_webocket_delete_registration(hass, hass_client,
|
||||
hass_ws_client, webhook_client): # noqa: E501 F811
|
||||
hass_ws_client, webhook_client):
|
||||
"""Test delete_registration websocket command."""
|
||||
authed_api_client = await hass_client() # noqa: F811
|
||||
register_resp = await authed_api_client.post(
|
||||
|
||||
Reference in New Issue
Block a user