Hide Telegram bot proxy URL behind section (#147613)

Co-authored-by: Manu <4445816+tr4nt0r@users.noreply.github.com>
This commit is contained in:
Joost Lekkerkerker 2025-06-26 19:55:38 +02:00 committed by Franck Nijhof
parent 4df1f702bf
commit 26521f8cc0
No known key found for this signature in database
GPG Key ID: AB33ADACE7101952
5 changed files with 91 additions and 26 deletions

View File

@ -22,7 +22,7 @@ from homeassistant.config_entries import (
)
from homeassistant.const import CONF_API_KEY, CONF_PLATFORM, CONF_URL
from homeassistant.core import callback
from homeassistant.data_entry_flow import AbortFlow
from homeassistant.data_entry_flow import AbortFlow, section
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue
from homeassistant.helpers.network import NoURLAvailableError, get_url
@ -58,6 +58,7 @@ from .const import (
PLATFORM_BROADCAST,
PLATFORM_POLLING,
PLATFORM_WEBHOOKS,
SECTION_ADVANCED_SETTINGS,
SUBENTRY_TYPE_ALLOWED_CHAT_IDS,
)
@ -81,8 +82,15 @@ STEP_USER_DATA_SCHEMA: vol.Schema = vol.Schema(
autocomplete="current-password",
)
),
vol.Optional(CONF_PROXY_URL): TextSelector(
config=TextSelectorConfig(type=TextSelectorType.URL)
vol.Required(SECTION_ADVANCED_SETTINGS): section(
vol.Schema(
{
vol.Optional(CONF_PROXY_URL): TextSelector(
config=TextSelectorConfig(type=TextSelectorType.URL)
),
},
),
{"collapsed": True},
),
}
)
@ -98,8 +106,15 @@ STEP_RECONFIGURE_USER_DATA_SCHEMA: vol.Schema = vol.Schema(
translation_key="platforms",
)
),
vol.Optional(CONF_PROXY_URL): TextSelector(
config=TextSelectorConfig(type=TextSelectorType.URL)
vol.Required(SECTION_ADVANCED_SETTINGS): section(
vol.Schema(
{
vol.Optional(CONF_PROXY_URL): TextSelector(
config=TextSelectorConfig(type=TextSelectorType.URL)
),
},
),
{"collapsed": True},
),
}
)
@ -197,6 +212,9 @@ class TelgramBotConfigFlow(ConfigFlow, domain=DOMAIN):
import_data[CONF_TRUSTED_NETWORKS] = ",".join(
import_data[CONF_TRUSTED_NETWORKS]
)
import_data[SECTION_ADVANCED_SETTINGS] = {
CONF_PROXY_URL: import_data.get(CONF_PROXY_URL)
}
try:
config_flow_result: ConfigFlowResult = await self.async_step_user(
import_data
@ -332,7 +350,7 @@ class TelgramBotConfigFlow(ConfigFlow, domain=DOMAIN):
data={
CONF_PLATFORM: user_input[CONF_PLATFORM],
CONF_API_KEY: user_input[CONF_API_KEY],
CONF_PROXY_URL: user_input.get(CONF_PROXY_URL),
CONF_PROXY_URL: user_input["advanced_settings"].get(CONF_PROXY_URL),
},
options={
# this value may come from yaml import
@ -444,7 +462,9 @@ class TelgramBotConfigFlow(ConfigFlow, domain=DOMAIN):
data={
CONF_PLATFORM: self._step_user_data[CONF_PLATFORM],
CONF_API_KEY: self._step_user_data[CONF_API_KEY],
CONF_PROXY_URL: self._step_user_data.get(CONF_PROXY_URL),
CONF_PROXY_URL: self._step_user_data[SECTION_ADVANCED_SETTINGS].get(
CONF_PROXY_URL
),
CONF_URL: user_input.get(CONF_URL),
CONF_TRUSTED_NETWORKS: user_input[CONF_TRUSTED_NETWORKS],
},
@ -509,9 +529,19 @@ class TelgramBotConfigFlow(ConfigFlow, domain=DOMAIN):
step_id="reconfigure",
data_schema=self.add_suggested_values_to_schema(
STEP_RECONFIGURE_USER_DATA_SCHEMA,
self._get_reconfigure_entry().data,
{
**self._get_reconfigure_entry().data,
SECTION_ADVANCED_SETTINGS: {
CONF_PROXY_URL: self._get_reconfigure_entry().data.get(
CONF_PROXY_URL
),
},
},
),
)
user_input[CONF_PROXY_URL] = user_input[SECTION_ADVANCED_SETTINGS].get(
CONF_PROXY_URL
)
errors: dict[str, str] = {}
description_placeholders: dict[str, str] = {}
@ -527,7 +557,12 @@ class TelgramBotConfigFlow(ConfigFlow, domain=DOMAIN):
step_id="reconfigure",
data_schema=self.add_suggested_values_to_schema(
STEP_RECONFIGURE_USER_DATA_SCHEMA,
user_input,
{
**user_input,
SECTION_ADVANCED_SETTINGS: {
CONF_PROXY_URL: user_input.get(CONF_PROXY_URL),
},
},
),
errors=errors,
description_placeholders=description_placeholders,

View File

@ -7,7 +7,7 @@ DOMAIN = "telegram_bot"
PLATFORM_BROADCAST = "broadcast"
PLATFORM_POLLING = "polling"
PLATFORM_WEBHOOKS = "webhooks"
SECTION_ADVANCED_SETTINGS = "advanced_settings"
SUBENTRY_TYPE_ALLOWED_CHAT_IDS = "allowed_chat_ids"
CONF_BOT_COUNT = "bot_count"

View File

@ -5,13 +5,22 @@
"description": "To create a Telegram bot, follow these steps:\n\n1. Open Telegram and start a chat with [{botfather_username}]({botfather_url}).\n1. Send the command `/newbot`.\n1. Follow the instructions to create your bot and get your API token.",
"data": {
"platform": "Platform",
"api_key": "[%key:common::config_flow::data::api_token%]",
"proxy_url": "Proxy URL"
"api_key": "[%key:common::config_flow::data::api_token%]"
},
"data_description": {
"platform": "Telegram bot implementation",
"api_key": "The API token of your bot.",
"proxy_url": "Proxy URL if working behind one, optionally including username and password.\n(socks5://username:password@proxy_ip:proxy_port)"
"api_key": "The API token of your bot."
},
"sections": {
"advanced_settings": {
"name": "Advanced settings",
"data": {
"proxy_url": "Proxy URL"
},
"data_description": {
"proxy_url": "Proxy URL if working behind one, optionally including username and password.\n(socks5://username:password@proxy_ip:proxy_port)"
}
}
}
},
"webhooks": {
@ -29,12 +38,21 @@
"title": "Telegram bot setup",
"description": "Reconfigure Telegram bot",
"data": {
"platform": "[%key:component::telegram_bot::config::step::user::data::platform%]",
"proxy_url": "[%key:component::telegram_bot::config::step::user::data::proxy_url%]"
"platform": "[%key:component::telegram_bot::config::step::user::data::platform%]"
},
"data_description": {
"platform": "[%key:component::telegram_bot::config::step::user::data_description::platform%]",
"proxy_url": "[%key:component::telegram_bot::config::step::user::data_description::proxy_url%]"
"platform": "[%key:component::telegram_bot::config::step::user::data_description::platform%]"
},
"sections": {
"advanced_settings": {
"name": "[%key:component::telegram_bot::config::step::user::sections::advanced_settings::name%]",
"data": {
"proxy_url": "[%key:component::telegram_bot::config::step::user::sections::advanced_settings::data::proxy_url%]"
},
"data_description": {
"proxy_url": "[%key:component::telegram_bot::config::step::user::sections::advanced_settings::data_description::proxy_url%]"
}
}
}
},
"reauth_confirm": {

View File

@ -23,6 +23,7 @@ from homeassistant.components.telegram_bot.const import (
PARSER_PLAIN_TEXT,
PLATFORM_BROADCAST,
PLATFORM_WEBHOOKS,
SECTION_ADVANCED_SETTINGS,
SUBENTRY_TYPE_ALLOWED_CHAT_IDS,
)
from homeassistant.config_entries import SOURCE_IMPORT, SOURCE_USER, ConfigSubentry
@ -89,7 +90,9 @@ async def test_reconfigure_flow_broadcast(
result["flow_id"],
{
CONF_PLATFORM: PLATFORM_BROADCAST,
CONF_PROXY_URL: "invalid",
SECTION_ADVANCED_SETTINGS: {
CONF_PROXY_URL: "invalid",
},
},
)
await hass.async_block_till_done()
@ -104,7 +107,9 @@ async def test_reconfigure_flow_broadcast(
result["flow_id"],
{
CONF_PLATFORM: PLATFORM_BROADCAST,
CONF_PROXY_URL: "https://test",
SECTION_ADVANCED_SETTINGS: {
CONF_PROXY_URL: "https://test",
},
},
)
await hass.async_block_till_done()
@ -131,7 +136,9 @@ async def test_reconfigure_flow_webhooks(
result["flow_id"],
{
CONF_PLATFORM: PLATFORM_WEBHOOKS,
CONF_PROXY_URL: "https://test",
SECTION_ADVANCED_SETTINGS: {
CONF_PROXY_URL: "https://test",
},
},
)
await hass.async_block_till_done()
@ -197,9 +204,7 @@ async def test_reconfigure_flow_webhooks(
]
async def test_create_entry(
hass: HomeAssistant,
) -> None:
async def test_create_entry(hass: HomeAssistant) -> None:
"""Test user flow."""
# test: no input
@ -225,7 +230,9 @@ async def test_create_entry(
{
CONF_PLATFORM: PLATFORM_WEBHOOKS,
CONF_API_KEY: "mock api key",
CONF_PROXY_URL: "invalid",
SECTION_ADVANCED_SETTINGS: {
CONF_PROXY_URL: "invalid",
},
},
)
await hass.async_block_till_done()
@ -245,7 +252,9 @@ async def test_create_entry(
{
CONF_PLATFORM: PLATFORM_WEBHOOKS,
CONF_API_KEY: "mock api key",
CONF_PROXY_URL: "https://proxy",
SECTION_ADVANCED_SETTINGS: {
CONF_PROXY_URL: "https://proxy",
},
},
)
await hass.async_block_till_done()
@ -535,6 +544,7 @@ async def test_duplicate_entry(hass: HomeAssistant) -> None:
data = {
CONF_PLATFORM: PLATFORM_BROADCAST,
CONF_API_KEY: "mock api key",
SECTION_ADVANCED_SETTINGS: {},
}
with patch(

View File

@ -51,6 +51,7 @@ from homeassistant.components.telegram_bot.const import (
CONF_CONFIG_ENTRY_ID,
DOMAIN,
PLATFORM_BROADCAST,
SECTION_ADVANCED_SETTINGS,
SERVICE_ANSWER_CALLBACK_QUERY,
SERVICE_DELETE_MESSAGE,
SERVICE_EDIT_CAPTION,
@ -722,6 +723,7 @@ async def test_send_message_no_chat_id_error(
data = {
CONF_PLATFORM: PLATFORM_BROADCAST,
CONF_API_KEY: "mock api key",
SECTION_ADVANCED_SETTINGS: {},
}
with patch("homeassistant.components.telegram_bot.config_flow.Bot.get_me"):