diff --git a/homeassistant/components/withings/config_flow.py b/homeassistant/components/withings/config_flow.py index 7bbf869069f..f25ef95210c 100644 --- a/homeassistant/components/withings/config_flow.py +++ b/homeassistant/components/withings/config_flow.py @@ -5,10 +5,12 @@ from collections.abc import Mapping import logging from typing import Any +import voluptuous as vol from withings_api.common import AuthScope -from homeassistant.config_entries import ConfigEntry +from homeassistant.config_entries import ConfigEntry, OptionsFlowWithConfigEntry from homeassistant.const import CONF_TOKEN +from homeassistant.core import callback from homeassistant.data_entry_flow import FlowResult from homeassistant.helpers import config_entry_oauth2_flow @@ -24,6 +26,14 @@ class WithingsFlowHandler( reauth_entry: ConfigEntry | None = None + @staticmethod + @callback + def async_get_options_flow( + config_entry: ConfigEntry, + ) -> WithingsOptionsFlowHandler: + """Get the options flow for this handler.""" + return WithingsOptionsFlowHandler(config_entry) + @property def logger(self) -> logging.Logger: """Return logger.""" @@ -77,3 +87,23 @@ class WithingsFlowHandler( return self.async_abort(reason="reauth_successful") return self.async_abort(reason="wrong_account") + + +class WithingsOptionsFlowHandler(OptionsFlowWithConfigEntry): + """Withings Options flow handler.""" + + async def async_step_init( + self, user_input: dict[str, Any] | None = None + ) -> FlowResult: + """Initialize form.""" + if user_input is not None: + return self.async_create_entry( + data=user_input, + ) + return self.async_show_form( + step_id="init", + data_schema=self.add_suggested_values_to_schema( + vol.Schema({vol.Required(CONF_USE_WEBHOOK): bool}), + self.options, + ), + ) diff --git a/homeassistant/components/withings/strings.json b/homeassistant/components/withings/strings.json index 424a0edadce..5fa155a1c1c 100644 --- a/homeassistant/components/withings/strings.json +++ b/homeassistant/components/withings/strings.json @@ -28,6 +28,15 @@ "default": "Successfully authenticated with Withings." } }, + "options": { + "step": { + "init": { + "data": { + "use_webhook": "Use webhooks" + } + } + } + }, "entity": { "binary_sensor": { "in_bed": { diff --git a/tests/components/withings/test_config_flow.py b/tests/components/withings/test_config_flow.py index 768f6fed16d..52a584e2513 100644 --- a/tests/components/withings/test_config_flow.py +++ b/tests/components/withings/test_config_flow.py @@ -1,7 +1,7 @@ """Tests for config flow.""" from unittest.mock import AsyncMock, patch -from homeassistant.components.withings.const import DOMAIN +from homeassistant.components.withings.const import CONF_USE_WEBHOOK, DOMAIN from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_USER from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType @@ -255,3 +255,31 @@ async def test_config_reauth_wrong_account( assert result assert result["type"] == FlowResultType.ABORT assert result["reason"] == "wrong_account" + + +async def test_options_flow( + hass: HomeAssistant, + hass_client_no_auth: ClientSessionGenerator, + aioclient_mock: AiohttpClientMocker, + config_entry: MockConfigEntry, + withings: AsyncMock, + disable_webhook_delay, + current_request_with_host, +) -> None: + """Test options flow.""" + await setup_integration(hass, config_entry) + + result = await hass.config_entries.options.async_init(config_entry.entry_id) + await hass.async_block_till_done() + + assert result["type"] == FlowResultType.FORM + assert result["step_id"] == "init" + + result = await hass.config_entries.options.async_configure( + result["flow_id"], + user_input={CONF_USE_WEBHOOK: True}, + ) + await hass.async_block_till_done() + + assert result["type"] == FlowResultType.CREATE_ENTRY + assert result["data"] == {CONF_USE_WEBHOOK: True}