1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 12:59:34 +00:00

Add cloudhook support to SmartThings component (#21905)

* Add support for Nabu Casa cloudhooks

* Added tests to cover cloudhook creation and removal

* Remove cloud dependency
This commit is contained in:
Andrew Sayre
2019-03-11 07:33:25 -05:00
committed by Charles Garwood
parent 3fd6aa0ba9
commit c401f35a43
9 changed files with 176 additions and 33 deletions

View File

@@ -85,7 +85,8 @@ def app_fixture(hass, config_file):
'appType': 'WEBHOOK_SMART_APP',
'classifications': [CLASSIFICATION_AUTOMATION],
'displayName': 'Home Assistant',
'description': "Home Assistant at " + hass.config.api.base_url,
'description':
hass.config.location_name + " at " + hass.config.api.base_url,
'singleInstance': True,
'webhookSmartApp': {
'targetUrl': webhook.async_generate_url(

View File

@@ -6,6 +6,8 @@ from aiohttp import ClientResponseError
from pysmartthings import APIResponseError
from homeassistant import data_entry_flow
from homeassistant.components import cloud
from homeassistant.components.smartthings import smartapp
from homeassistant.components.smartthings.config_flow import (
SmartThingsFlowHandler)
from homeassistant.components.smartthings.const import (
@@ -41,7 +43,7 @@ async def test_base_url_not_https(hass):
hass.config.api.base_url = 'http://0.0.0.0'
flow = SmartThingsFlowHandler()
flow.hass = hass
result = await flow.async_step_import()
result = await flow.async_step_user({'access_token': str(uuid4())})
assert result['type'] == data_entry_flow.RESULT_TYPE_FORM
assert result['step_id'] == 'user'
@@ -193,6 +195,38 @@ async def test_app_created_then_show_wait_form(
assert result['step_id'] == 'wait_install'
async def test_cloudhook_app_created_then_show_wait_form(
hass, app, app_oauth_client, smartthings_mock):
"""Test SmartApp is created with a cloudhoko and shows wait form."""
# Unload the endpoint so we can reload it under the cloud.
await smartapp.unload_smartapp_endpoint(hass)
mock_async_active_subscription = Mock(return_value=True)
mock_create_cloudhook = Mock(return_value=mock_coro(
return_value="http://cloud.test"))
with patch.object(cloud, 'async_active_subscription',
new=mock_async_active_subscription), \
patch.object(cloud, 'async_create_cloudhook',
new=mock_create_cloudhook):
await smartapp.setup_smartapp_endpoint(hass)
flow = SmartThingsFlowHandler()
flow.hass = hass
smartthings = smartthings_mock.return_value
smartthings.apps.return_value = mock_coro(return_value=[])
smartthings.create_app.return_value = \
mock_coro(return_value=(app, app_oauth_client))
smartthings.update_app_settings.return_value = mock_coro()
smartthings.update_app_oauth.return_value = mock_coro()
result = await flow.async_step_user({'access_token': str(uuid4())})
assert result['type'] == data_entry_flow.RESULT_TYPE_FORM
assert result['step_id'] == 'wait_install'
assert mock_create_cloudhook.call_count == 1
async def test_app_updated_then_show_wait_form(
hass, app, app_oauth_client, smartthings_mock):
"""Test SmartApp is updated when an existing is already created."""

View File

@@ -6,10 +6,11 @@ from aiohttp import ClientConnectionError, ClientResponseError
from pysmartthings import InstalledAppStatus
import pytest
from homeassistant.components import smartthings
from homeassistant.components import cloud, smartthings
from homeassistant.components.smartthings.const import (
CONF_INSTALLED_APP_ID, CONF_REFRESH_TOKEN, DATA_BROKERS, DOMAIN,
EVENT_BUTTON, SIGNAL_SMARTTHINGS_UPDATE, SUPPORTED_PLATFORMS)
CONF_CLOUDHOOK_URL, CONF_INSTALLED_APP_ID, CONF_REFRESH_TOKEN,
DATA_BROKERS, DOMAIN, EVENT_BUTTON, SIGNAL_SMARTTHINGS_UPDATE,
SUPPORTED_PLATFORMS)
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers.dispatcher import async_dispatcher_connect
@@ -224,6 +225,29 @@ async def test_remove_entry(hass, config_entry, smartthings_mock):
assert api.delete_app.call_count == 1
async def test_remove_entry_cloudhook(hass, config_entry, smartthings_mock):
"""Test that the installed app, app, and cloudhook are removed up."""
# Arrange
setattr(hass.config_entries, '_entries', [config_entry])
hass.data[DOMAIN][CONF_CLOUDHOOK_URL] = "https://test.cloud"
api = smartthings_mock.return_value
api.delete_installed_app.side_effect = lambda _: mock_coro()
api.delete_app.side_effect = lambda _: mock_coro()
mock_async_is_logged_in = Mock(return_value=True)
mock_async_delete_cloudhook = Mock(return_value=mock_coro())
# Act
with patch.object(cloud, 'async_is_logged_in',
new=mock_async_is_logged_in), \
patch.object(cloud, 'async_delete_cloudhook',
new=mock_async_delete_cloudhook):
await smartthings.async_remove_entry(hass, config_entry)
# Assert
assert api.delete_installed_app.call_count == 1
assert api.delete_app.call_count == 1
assert mock_async_is_logged_in.call_count == 1
assert mock_async_delete_cloudhook.call_count == 1
async def test_remove_entry_app_in_use(hass, config_entry, smartthings_mock):
"""Test app is not removed if in use by another config entry."""
# Arrange