Fix Telegram bot yaml import for webhooks containing None value for URL (#147586)

This commit is contained in:
hanwg 2025-06-27 16:03:03 +08:00 committed by GitHub
parent 78060e4833
commit 3879f6d2ef
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 38 additions and 18 deletions

View File

@ -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] = []

View File

@ -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