mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Fix Telegram bot yaml import for webhooks containing None value for URL (#147586)
This commit is contained in:
parent
78060e4833
commit
3879f6d2ef
@ -412,12 +412,20 @@ class TelgramBotConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||||||
"""Handle config flow for webhook Telegram bot."""
|
"""Handle config flow for webhook Telegram bot."""
|
||||||
|
|
||||||
if not user_input:
|
if not user_input:
|
||||||
|
default_trusted_networks = ",".join(
|
||||||
|
[str(network) for network in DEFAULT_TRUSTED_NETWORKS]
|
||||||
|
)
|
||||||
|
|
||||||
if self.source == SOURCE_RECONFIGURE:
|
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(
|
return self.async_show_form(
|
||||||
step_id="webhooks",
|
step_id="webhooks",
|
||||||
data_schema=self.add_suggested_values_to_schema(
|
data_schema=self.add_suggested_values_to_schema(
|
||||||
STEP_WEBHOOKS_DATA_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(
|
data_schema=self.add_suggested_values_to_schema(
|
||||||
STEP_WEBHOOKS_DATA_SCHEMA,
|
STEP_WEBHOOKS_DATA_SCHEMA,
|
||||||
{
|
{
|
||||||
CONF_TRUSTED_NETWORKS: ",".join(
|
CONF_TRUSTED_NETWORKS: default_trusted_networks,
|
||||||
[str(network) for network in DEFAULT_TRUSTED_NETWORKS]
|
|
||||||
),
|
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
@ -479,12 +485,8 @@ class TelgramBotConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||||||
description_placeholders: dict[str, str],
|
description_placeholders: dict[str, str],
|
||||||
) -> None:
|
) -> None:
|
||||||
# validate URL
|
# validate URL
|
||||||
if CONF_URL in user_input and not user_input[CONF_URL].startswith("https"):
|
url: str | None = user_input.get(CONF_URL)
|
||||||
errors["base"] = "invalid_url"
|
if url is None:
|
||||||
description_placeholders[ERROR_FIELD] = "URL"
|
|
||||||
description_placeholders[ERROR_MESSAGE] = "URL must start with https"
|
|
||||||
return
|
|
||||||
if CONF_URL not in user_input:
|
|
||||||
try:
|
try:
|
||||||
get_url(self.hass, require_ssl=True, allow_internal=False)
|
get_url(self.hass, require_ssl=True, allow_internal=False)
|
||||||
except NoURLAvailableError:
|
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"
|
"URL is required since you have not configured an external URL in Home Assistant"
|
||||||
)
|
)
|
||||||
return
|
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
|
# validate trusted networks
|
||||||
csv_trusted_networks: list[str] = []
|
csv_trusted_networks: list[str] = []
|
||||||
|
@ -121,13 +121,13 @@ async def test_reconfigure_flow_broadcast(
|
|||||||
|
|
||||||
async def test_reconfigure_flow_webhooks(
|
async def test_reconfigure_flow_webhooks(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
mock_webhooks_config_entry: MockConfigEntry,
|
mock_broadcast_config_entry: MockConfigEntry,
|
||||||
mock_external_calls: None,
|
mock_external_calls: None,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test reconfigure flow for webhook."""
|
"""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["step_id"] == "reconfigure"
|
||||||
assert result["type"] is FlowResultType.FORM
|
assert result["type"] is FlowResultType.FORM
|
||||||
assert result["errors"] is None
|
assert result["errors"] is None
|
||||||
@ -198,8 +198,8 @@ async def test_reconfigure_flow_webhooks(
|
|||||||
|
|
||||||
assert result["type"] is FlowResultType.ABORT
|
assert result["type"] is FlowResultType.ABORT
|
||||||
assert result["reason"] == "reconfigure_successful"
|
assert result["reason"] == "reconfigure_successful"
|
||||||
assert mock_webhooks_config_entry.data[CONF_URL] == "https://reconfigure"
|
assert mock_broadcast_config_entry.data[CONF_URL] == "https://reconfigure"
|
||||||
assert mock_webhooks_config_entry.data[CONF_TRUSTED_NETWORKS] == [
|
assert mock_broadcast_config_entry.data[CONF_TRUSTED_NETWORKS] == [
|
||||||
"149.154.160.0/20"
|
"149.154.160.0/20"
|
||||||
]
|
]
|
||||||
|
|
||||||
@ -499,9 +499,22 @@ async def test_import_multiple(
|
|||||||
CONF_BOT_COUNT: 2,
|
CONF_BOT_COUNT: 2,
|
||||||
}
|
}
|
||||||
|
|
||||||
with patch(
|
with (
|
||||||
"homeassistant.components.telegram_bot.config_flow.Bot.get_me",
|
patch(
|
||||||
return_value=User(123456, "Testbot", True),
|
"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
|
# test: import first entry success
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user