Mobile App: Register devices into the registry (#21856)

* Register devices into the registry

* Switch to device ID instead of webhook ID

* Rearchitect mobile_app to support config entries

* Kill DATA_REGISTRATIONS by migrating registrations into config entries

* Fix tests

* Improve how we get the config_entry_id

* Remove single_instance_allowed

* Simplify setup_registration

* Move webhook registering functions into __init__.py since they are only ever used once

* Kill get_registration websocket command

* Support description_placeholders in async_abort

* Add link to mobile_app implementing apps in abort dialog

* Store config entry and device registry entry in hass.data instead of looking it up

* Add testing to ensure that the config entry is created at registration

* Fix busted async_abort test

* Remove unnecessary check for entry is None
This commit is contained in:
Robbie Trencheny authored and Paulus Schoutsen committed 2019-03-15 10:57:00 -07:00
1 parent 3fd1e8d382
commit f7dcfe28b6
15 files changed
+254 -212

No files matched your search

@@ -226,6 +226,7 @@ def test_abort(hass, client):
data = yield from resp.json()
data.pop('flow_id')
assert data == {
'description_placeholders': None,
'handler': 'test',
'reason': 'bla',
'type': 'abort'
+38 -27
View File
@@ -2,48 +2,59 @@
# 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_DELETED_IDS,
DATA_REGISTRATIONS,
CONF_SECRET,
CONF_USER_ID, DOMAIN,
from homeassistant.components.mobile_app.const import (DATA_CONFIG_ENTRIES,
DATA_DELETED_IDS,
DATA_DEVICES,
DOMAIN,
STORAGE_KEY,
STORAGE_VERSION)
from homeassistant.const import CONF_WEBHOOK_ID
from .const import REGISTER, REGISTER_CLEARTEXT
@pytest.fixture
def webhook_client(hass, aiohttp_client, hass_storage, hass_admin_user):
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_REGISTRATIONS: {
'mobile_app_test': {
CONF_SECRET: '58eb127991594dad934d1584bdee5f27',
'supports_encryption': True,
CONF_WEBHOOK_ID: 'mobile_app_test',
'device_name': 'Test Device',
CONF_USER_ID: hass_admin_user.id,
},
'mobile_app_test_cleartext': {
'supports_encryption': False,
CONF_WEBHOOK_ID: 'mobile_app_test_cleartext',
'device_name': 'Test Device (Cleartext)',
CONF_USER_ID: hass_admin_user.id,
}
},
DATA_DELETED_IDS: [],
DATA_CONFIG_ENTRIES: {}, DATA_DELETED_IDS: [], DATA_DEVICES: {},
}
}
assert hass.loop.run_until_complete(async_setup_component(
hass, DOMAIN, {
DOMAIN: {}
}))
await async_setup_component(hass, DOMAIN, {DOMAIN: {}})
return hass.loop.run_until_complete(aiohttp_client(hass.http.app))
return await aiohttp_client(hass.http.app)
@pytest.fixture
+23 -6
View File
@@ -2,14 +2,15 @@
# pylint: disable=redefined-outer-name,unused-import
import pytest
from homeassistant.components.mobile_app.const import CONF_SECRET
from homeassistant.components.mobile_app.const import CONF_SECRET, DOMAIN
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_client, authed_api_client): # noqa: F811
async def test_registration(hass, hass_client): # noqa: F811
"""Test that registrations happen."""
try:
# pylint: disable=unused-import
@@ -21,7 +22,11 @@ async def test_registration(hass_client, authed_api_client): # noqa: F811
import json
resp = await authed_api_client.post(
await async_setup_component(hass, DOMAIN, {DOMAIN: {}})
api_client = await hass_client()
resp = await api_client.post(
'/api/mobile_app/registrations', json=REGISTER
)
@@ -30,6 +35,20 @@ async def test_registration(hass_client, authed_api_client): # noqa: F811
assert CONF_WEBHOOK_ID in register_json
assert CONF_SECRET in register_json
entries = hass.config_entries.async_entries(DOMAIN)
assert entries[0].data['app_data'] == REGISTER['app_data']
assert entries[0].data['app_id'] == REGISTER['app_id']
assert entries[0].data['app_name'] == REGISTER['app_name']
assert entries[0].data['app_version'] == REGISTER['app_version']
assert entries[0].data['device_name'] == REGISTER['device_name']
assert entries[0].data['manufacturer'] == REGISTER['manufacturer']
assert entries[0].data['model'] == REGISTER['model']
assert entries[0].data['os_name'] == REGISTER['os_name']
assert entries[0].data['os_version'] == REGISTER['os_version']
assert entries[0].data['supports_encryption'] == \
REGISTER['supports_encryption']
keylen = SecretBox.KEY_SIZE
key = register_json[CONF_SECRET].encode("utf-8")
key = key[:keylen]
@@ -46,9 +65,7 @@ async def test_registration(hass_client, authed_api_client): # noqa: F811
'encrypted_data': data,
}
webhook_client = await hass_client()
resp = await webhook_client.post(
resp = await api_client.post(
'/api/webhook/{}'.format(register_json[CONF_WEBHOOK_ID]),
json=container
)
+25 -14
View File
@@ -1,5 +1,6 @@
"""Webhook tests for mobile_app."""
# pylint: disable=redefined-outer-name,unused-import
import logging
import pytest
from homeassistant.components.mobile_app.const import CONF_SECRET
@@ -8,16 +9,20 @@ from homeassistant.core import callback
from tests.common import async_mock_service
from . import authed_api_client, webhook_client # noqa: F401
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(webhook_client): # noqa: F811
async def test_webhook_handle_render_template(create_registrations, # noqa: F401, F811, E501
webhook_client): # noqa: F811
"""Test that we render templates properly."""
resp = await webhook_client.post(
'/api/webhook/mobile_app_test_cleartext',
'/api/webhook/{}'.format(create_registrations[1]['webhook_id']),
json=RENDER_TEMPLATE
)
@@ -27,12 +32,13 @@ async def test_webhook_handle_render_template(webhook_client): # noqa: F811
assert json == {'one': 'Hello world'}
async def test_webhook_handle_call_services(hass, webhook_client): # noqa: E501 F811
async def test_webhook_handle_call_services(hass, create_registrations, # noqa: F401, F811, E501
webhook_client): # noqa: E501 F811
"""Test that we call services properly."""
calls = async_mock_service(hass, 'test', 'mobile_app')
resp = await webhook_client.post(
'/api/webhook/mobile_app_test_cleartext',
'/api/webhook/{}'.format(create_registrations[1]['webhook_id']),
json=CALL_SERVICE
)
@@ -41,7 +47,8 @@ async def test_webhook_handle_call_services(hass, webhook_client): # noqa: E501
assert len(calls) == 1
async def test_webhook_handle_fire_event(hass, webhook_client): # noqa: F811
async def test_webhook_handle_fire_event(hass, create_registrations, # noqa: F401, F811, E501
webhook_client): # noqa: F811
"""Test that we can fire events."""
events = []
@@ -53,7 +60,7 @@ async def test_webhook_handle_fire_event(hass, webhook_client): # noqa: F811
hass.bus.async_listen('test_event', store_event)
resp = await webhook_client.post(
'/api/webhook/mobile_app_test_cleartext',
'/api/webhook/{}'.format(create_registrations[1]['webhook_id']),
json=FIRE_EVENT
)
@@ -93,10 +100,12 @@ async def test_webhook_update_registration(webhook_client, hass_client): # noqa
assert CONF_SECRET not in update_json
async def test_webhook_returns_error_incorrect_json(webhook_client, caplog): # noqa: E501 F811
async def test_webhook_returns_error_incorrect_json(webhook_client, # noqa: F401, F811, E501
create_registrations, # noqa: F401, F811, E501
caplog): # noqa: E501 F811
"""Test that an error is returned when JSON is invalid."""
resp = await webhook_client.post(
'/api/webhook/mobile_app_test_cleartext',
'/api/webhook/{}'.format(create_registrations[1]['webhook_id']),
data='not json'
)
@@ -106,7 +115,8 @@ async def test_webhook_returns_error_incorrect_json(webhook_client, caplog): #
assert 'invalid JSON' in caplog.text
async def test_webhook_handle_decryption(webhook_client): # noqa: F811
async def test_webhook_handle_decryption(webhook_client, # noqa: F811
create_registrations): # noqa: F401, F811, E501
"""Test that we can encrypt/decrypt properly."""
try:
# pylint: disable=unused-import
@@ -119,7 +129,7 @@ async def test_webhook_handle_decryption(webhook_client): # noqa: F811
import json
keylen = SecretBox.KEY_SIZE
key = "58eb127991594dad934d1584bdee5f27".encode("utf-8")
key = create_registrations[0]['secret'].encode("utf-8")
key = key[:keylen]
key = key.ljust(keylen, b'\0')
@@ -135,7 +145,7 @@ async def test_webhook_handle_decryption(webhook_client): # noqa: F811
}
resp = await webhook_client.post(
'/api/webhook/mobile_app_test',
'/api/webhook/{}'.format(create_registrations[0]['webhook_id']),
json=container
)
@@ -151,10 +161,11 @@ 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
async def test_webhook_requires_encryption(webhook_client, # noqa: F811
create_registrations): # noqa: F401, F811, E501
"""Test that encrypted registrations only accept encrypted data."""
resp = await webhook_client.post(
'/api/webhook/mobile_app_test',
'/api/webhook/{}'.format(create_registrations[0]['webhook_id']),
json=RENDER_TEMPLATE
)
@@ -9,33 +9,6 @@ from . import authed_api_client, setup_ws, webhook_client # noqa: F401
from .const import (CALL_SERVICE, REGISTER)
async def test_webocket_get_registration(hass, setup_ws, authed_api_client, # noqa: E501 F811
hass_ws_client):
"""Test get_registration websocket command."""
register_resp = await authed_api_client.post(
'/api/mobile_app/registrations', json=REGISTER
)
assert register_resp.status == 201
register_json = await register_resp.json()
assert CONF_WEBHOOK_ID in register_json
assert CONF_SECRET in register_json
client = await hass_ws_client(hass)
await client.send_json({
'id': 5,
'type': 'mobile_app/get_registration',
CONF_WEBHOOK_ID: register_json[CONF_WEBHOOK_ID],
})
msg = await client.receive_json()
assert msg['id'] == 5
assert msg['type'] == TYPE_RESULT
assert msg['success']
assert msg['result']['app_id'] == 'io.homeassistant.mobile_app_test'
async def test_webocket_get_user_registrations(hass, aiohttp_client,
hass_ws_client,
hass_read_only_access_token):