diff --git a/homeassistant/components/telegram_bot/config_flow.py b/homeassistant/components/telegram_bot/config_flow.py index 1a77a5b9a81..7b441889b8c 100644 --- a/homeassistant/components/telegram_bot/config_flow.py +++ b/homeassistant/components/telegram_bot/config_flow.py @@ -412,12 +412,20 @@ class TelgramBotConfigFlow(ConfigFlow, domain=DOMAIN): """Handle config flow for webhook Telegram bot.""" if not user_input: + default_trusted_networks = ",".join( + [str(network) for network in DEFAULT_TRUSTED_NETWORKS] + ) + if self.source == SOURCE_RECONFIGURE: + suggested_values = dict(self._get_reconfigure_entry().data) + if CONF_TRUSTED_NETWORKS not in self._get_reconfigure_entry().data: + suggested_values[CONF_TRUSTED_NETWORKS] = default_trusted_networks + return self.async_show_form( step_id="webhooks", data_schema=self.add_suggested_values_to_schema( STEP_WEBHOOKS_DATA_SCHEMA, - self._get_reconfigure_entry().data, + suggested_values, ), ) @@ -426,9 +434,7 @@ class TelgramBotConfigFlow(ConfigFlow, domain=DOMAIN): data_schema=self.add_suggested_values_to_schema( STEP_WEBHOOKS_DATA_SCHEMA, { - CONF_TRUSTED_NETWORKS: ",".join( - [str(network) for network in DEFAULT_TRUSTED_NETWORKS] - ), + CONF_TRUSTED_NETWORKS: default_trusted_networks, }, ), ) @@ -479,12 +485,8 @@ class TelgramBotConfigFlow(ConfigFlow, domain=DOMAIN): description_placeholders: dict[str, str], ) -> None: # validate URL - if CONF_URL in user_input and not user_input[CONF_URL].startswith("https"): - errors["base"] = "invalid_url" - description_placeholders[ERROR_FIELD] = "URL" - description_placeholders[ERROR_MESSAGE] = "URL must start with https" - return - if CONF_URL not in user_input: + url: str | None = user_input.get(CONF_URL) + if url is None: try: get_url(self.hass, require_ssl=True, allow_internal=False) except NoURLAvailableError: @@ -494,6 +496,11 @@ class TelgramBotConfigFlow(ConfigFlow, domain=DOMAIN): "URL is required since you have not configured an external URL in Home Assistant" ) return + elif not url.startswith("https"): + errors["base"] = "invalid_url" + description_placeholders[ERROR_FIELD] = "URL" + description_placeholders[ERROR_MESSAGE] = "URL must start with https" + return # validate trusted networks csv_trusted_networks: list[str] = [] diff --git a/tests/components/telegram_bot/test_config_flow.py b/tests/components/telegram_bot/test_config_flow.py index e13fab8f28b..2af90b9f7ef 100644 --- a/tests/components/telegram_bot/test_config_flow.py +++ b/tests/components/telegram_bot/test_config_flow.py @@ -121,13 +121,13 @@ async def test_reconfigure_flow_broadcast( async def test_reconfigure_flow_webhooks( hass: HomeAssistant, - mock_webhooks_config_entry: MockConfigEntry, + mock_broadcast_config_entry: MockConfigEntry, mock_external_calls: None, ) -> None: """Test reconfigure flow for webhook.""" - mock_webhooks_config_entry.add_to_hass(hass) + mock_broadcast_config_entry.add_to_hass(hass) - result = await mock_webhooks_config_entry.start_reconfigure_flow(hass) + result = await mock_broadcast_config_entry.start_reconfigure_flow(hass) assert result["step_id"] == "reconfigure" assert result["type"] is FlowResultType.FORM assert result["errors"] is None @@ -198,8 +198,8 @@ async def test_reconfigure_flow_webhooks( assert result["type"] is FlowResultType.ABORT assert result["reason"] == "reconfigure_successful" - assert mock_webhooks_config_entry.data[CONF_URL] == "https://reconfigure" - assert mock_webhooks_config_entry.data[CONF_TRUSTED_NETWORKS] == [ + assert mock_broadcast_config_entry.data[CONF_URL] == "https://reconfigure" + assert mock_broadcast_config_entry.data[CONF_TRUSTED_NETWORKS] == [ "149.154.160.0/20" ] @@ -499,9 +499,22 @@ async def test_import_multiple( CONF_BOT_COUNT: 2, } - with patch( - "homeassistant.components.telegram_bot.config_flow.Bot.get_me", - return_value=User(123456, "Testbot", True), + with ( + patch( + "homeassistant.components.telegram_bot.config_flow.Bot.get_me", + return_value=User(123456, "Testbot", True), + ), + patch( + "homeassistant.components.telegram_bot.config_flow.Bot.get_chat", + return_value=ChatFullInfo( + id=987654321, + title="mock title", + first_name="mock first_name", + type="PRIVATE", + max_reaction_count=100, + accent_color_id=AccentColor.COLOR_000, + ), + ), ): # test: import first entry success