Ignore smartthings storage on fresh install (#98418)

* Ignore smartthings storage on fresh install

* Also unload existing things when going for clean install

* Rename param

* Fix tests
This commit is contained in:
Paulus Schoutsen 2023-08-14 22:39:05 -04:00 committed by GitHub
parent e3438baf49
commit ced4af1e22
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 7 deletions

View file

@ -58,7 +58,7 @@ CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN)
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Initialize the SmartThings platform."""
await setup_smartapp_endpoint(hass)
await setup_smartapp_endpoint(hass, False)
return True

View file

@ -50,6 +50,7 @@ class SmartThingsFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
self.installed_app_id = None
self.refresh_token = None
self.location_id = None
self.endpoints_initialized = False
async def async_step_import(self, user_input=None):
"""Occurs when a previously entry setup fails and is re-initiated."""
@ -57,7 +58,11 @@ class SmartThingsFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
async def async_step_user(self, user_input=None):
"""Validate and confirm webhook setup."""
await setup_smartapp_endpoint(self.hass)
if not self.endpoints_initialized:
self.endpoints_initialized = True
await setup_smartapp_endpoint(
self.hass, len(self._async_current_entries()) == 0
)
webhook_url = get_webhook_url(self.hass)
# Abort if the webhook is invalid

View file

@ -197,7 +197,7 @@ def setup_smartapp(hass, app):
return smartapp
async def setup_smartapp_endpoint(hass: HomeAssistant):
async def setup_smartapp_endpoint(hass: HomeAssistant, fresh_install: bool):
"""Configure the SmartApp webhook in hass.
SmartApps are an extension point within the SmartThings ecosystem and
@ -205,11 +205,16 @@ async def setup_smartapp_endpoint(hass: HomeAssistant):
"""
if hass.data.get(DOMAIN):
# already setup
return
if not fresh_install:
return
# We're doing a fresh install, clean up
await unload_smartapp_endpoint(hass)
# Get/create config to store a unique id for this hass instance.
store = Store[dict[str, Any]](hass, STORAGE_VERSION, STORAGE_KEY)
if not (config := await store.async_load()):
if fresh_install or not (config := await store.async_load()):
# Create config
config = {
CONF_INSTANCE_ID: str(uuid4()),

View file

@ -187,6 +187,7 @@ async def test_entry_created_existing_app_new_oauth_client(
smartthings_mock.apps.return_value = [app]
smartthings_mock.generate_app_oauth.return_value = app_oauth_client
smartthings_mock.locations.return_value = [location]
smartthings_mock.create_app = AsyncMock(return_value=(app, app_oauth_client))
request = Mock()
request.installed_app_id = installed_app_id
request.auth_token = token
@ -366,7 +367,7 @@ async def test_entry_created_with_cloudhook(
"async_create_cloudhook",
AsyncMock(return_value="http://cloud.test"),
) as mock_create_cloudhook:
await smartapp.setup_smartapp_endpoint(hass)
await smartapp.setup_smartapp_endpoint(hass, True)
# Webhook confirmation shown
result = await hass.config_entries.flow.async_init(
@ -377,7 +378,8 @@ async def test_entry_created_with_cloudhook(
assert result["description_placeholders"][
"webhook_url"
] == smartapp.get_webhook_url(hass)
assert mock_create_cloudhook.call_count == 1
# One is done by app fixture, one done by new config entry
assert mock_create_cloudhook.call_count == 2
# Advance to PAT screen
result = await hass.config_entries.flow.async_configure(result["flow_id"], {})