diff --git a/homeassistant/components/smartthings/__init__.py b/homeassistant/components/smartthings/__init__.py index 4e694556598..22856bdb05b 100644 --- a/homeassistant/components/smartthings/__init__.py +++ b/homeassistant/components/smartthings/__init__.py @@ -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 diff --git a/homeassistant/components/smartthings/config_flow.py b/homeassistant/components/smartthings/config_flow.py index 0328c3a7f8e..5e3451dfbce 100644 --- a/homeassistant/components/smartthings/config_flow.py +++ b/homeassistant/components/smartthings/config_flow.py @@ -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 diff --git a/homeassistant/components/smartthings/smartapp.py b/homeassistant/components/smartthings/smartapp.py index 9b17034ab3b..78c0bfa86b1 100644 --- a/homeassistant/components/smartthings/smartapp.py +++ b/homeassistant/components/smartthings/smartapp.py @@ -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()), diff --git a/tests/components/smartthings/test_config_flow.py b/tests/components/smartthings/test_config_flow.py index 5c44a5af2e9..168756b0dfe 100644 --- a/tests/components/smartthings/test_config_flow.py +++ b/tests/components/smartthings/test_config_flow.py @@ -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"], {})