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

Update SmartThings config flow to be entirely UI based (#34163)

* bump pysmartthings 0.7.1

* Update config flow to use UI

* Code review comments and fix for resetting oauth client

* Replace html with markdown
This commit is contained in:
Andrew Sayre
2020-04-14 17:26:53 -05:00
committed by GitHub
parent bf33169627
commit 075030f15a
10 changed files with 543 additions and 382 deletions

View File

@@ -7,7 +7,6 @@ from pysmartthings import AppEntity, Capability
from homeassistant.components.smartthings import smartapp
from homeassistant.components.smartthings.const import (
CONF_INSTALLED_APP_ID,
CONF_INSTALLED_APPS,
CONF_LOCATION_ID,
CONF_REFRESH_TOKEN,
DATA_MANAGER,
@@ -40,11 +39,11 @@ async def test_update_app_updated_needed(hass, app):
assert mock_app.classifications == app.classifications
async def test_smartapp_install_store_if_no_other(
hass, smartthings_mock, device_factory
):
"""Test aborts if no other app was configured already."""
async def test_smartapp_install_configures_flow(hass):
"""Test install event continues an existing flow."""
# Arrange
flow_id = str(uuid4())
flows = [{"flow_id": flow_id, "handler": DOMAIN}]
app = Mock()
app.app_id = uuid4()
request = Mock()
@@ -52,50 +51,22 @@ async def test_smartapp_install_store_if_no_other(
request.auth_token = str(uuid4())
request.location_id = str(uuid4())
request.refresh_token = str(uuid4())
# Act
await smartapp.smartapp_install(hass, request, None, app)
# Assert
entries = hass.config_entries.async_entries("smartthings")
assert not entries
data = hass.data[DOMAIN][CONF_INSTALLED_APPS][0]
assert data[CONF_REFRESH_TOKEN] == request.refresh_token
assert data[CONF_LOCATION_ID] == request.location_id
assert data[CONF_INSTALLED_APP_ID] == request.installed_app_id
async def test_smartapp_install_creates_flow(
hass, smartthings_mock, config_entry, location, device_factory
):
"""Test installation creates flow."""
# Arrange
config_entry.add_to_hass(hass)
app = Mock()
app.app_id = config_entry.data["app_id"]
request = Mock()
request.installed_app_id = str(uuid4())
request.auth_token = str(uuid4())
request.refresh_token = str(uuid4())
request.location_id = location.location_id
devices = [
device_factory("", [Capability.battery, "ping"]),
device_factory("", [Capability.switch, Capability.switch_level]),
device_factory("", [Capability.switch]),
]
smartthings_mock.devices.return_value = devices
# Act
await smartapp.smartapp_install(hass, request, None, app)
# Assert
await hass.async_block_till_done()
entries = hass.config_entries.async_entries("smartthings")
assert len(entries) == 2
assert entries[1].data["app_id"] == app.app_id
assert entries[1].data["installed_app_id"] == request.installed_app_id
assert entries[1].data["location_id"] == request.location_id
assert entries[1].data["access_token"] == config_entry.data["access_token"]
assert entries[1].data["refresh_token"] == request.refresh_token
assert entries[1].data["client_secret"] == config_entry.data["client_secret"]
assert entries[1].data["client_id"] == config_entry.data["client_id"]
assert entries[1].title == location.name
with patch.object(
hass.config_entries.flow, "async_progress", return_value=flows
), patch.object(hass.config_entries.flow, "async_configure") as configure_mock:
await smartapp.smartapp_install(hass, request, None, app)
configure_mock.assert_called_once_with(
flow_id,
{
CONF_INSTALLED_APP_ID: request.installed_app_id,
CONF_LOCATION_ID: request.location_id,
CONF_REFRESH_TOKEN: request.refresh_token,
},
)
async def test_smartapp_update_saves_token(
@@ -121,6 +92,36 @@ async def test_smartapp_update_saves_token(
assert entry.data[CONF_REFRESH_TOKEN] == request.refresh_token
async def test_smartapp_update_configures_flow(hass):
"""Test update event continues an existing flow."""
# Arrange
flow_id = str(uuid4())
flows = [{"flow_id": flow_id, "handler": DOMAIN}]
app = Mock()
app.app_id = uuid4()
request = Mock()
request.installed_app_id = str(uuid4())
request.auth_token = str(uuid4())
request.location_id = str(uuid4())
request.refresh_token = str(uuid4())
# Act
with patch.object(
hass.config_entries.flow, "async_progress", return_value=flows
), patch.object(hass.config_entries.flow, "async_configure") as configure_mock:
await smartapp.smartapp_update(hass, request, None, app)
configure_mock.assert_called_once_with(
flow_id,
{
CONF_INSTALLED_APP_ID: request.installed_app_id,
CONF_LOCATION_ID: request.location_id,
CONF_REFRESH_TOKEN: request.refresh_token,
},
)
async def test_smartapp_uninstall(hass, config_entry):
"""Test the config entry is unloaded when the app is uninstalled."""
config_entry.add_to_hass(hass)