Skip to content

Commit 46f56c6

Browse files
authored
Remove deprecated yaml import from Telegram bot (home-assistant#158520)
1 parent 060b258 commit 46f56c6

File tree

7 files changed

+7
-370
lines changed

7 files changed

+7
-370
lines changed

homeassistant/components/telegram_bot/__init__.py

Lines changed: 1 addition & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,22 @@
22

33
from __future__ import annotations
44

5-
from ipaddress import IPv4Network, ip_network
65
import logging
76
from types import ModuleType
8-
from typing import Any
97

108
from telegram import Bot
119
from telegram.constants import InputMediaType
1210
from telegram.error import InvalidToken, TelegramError
1311
import voluptuous as vol
1412

1513
from homeassistant.components.script import DOMAIN as SCRIPT_DOMAIN
16-
from homeassistant.config_entries import SOURCE_IMPORT
1714
from homeassistant.const import (
1815
ATTR_DOMAIN,
1916
ATTR_ENTITY_ID,
2017
ATTR_LATITUDE,
2118
ATTR_LONGITUDE,
2219
ATTR_SERVICE,
23-
CONF_API_KEY,
2420
CONF_PLATFORM,
25-
CONF_SOURCE,
26-
CONF_URL,
2721
Platform,
2822
)
2923
from homeassistant.core import (
@@ -89,14 +83,8 @@
8983
CHAT_ACTION_UPLOAD_VIDEO,
9084
CHAT_ACTION_UPLOAD_VIDEO_NOTE,
9185
CHAT_ACTION_UPLOAD_VOICE,
92-
CONF_ALLOWED_CHAT_IDS,
93-
CONF_BOT_COUNT,
9486
CONF_CONFIG_ENTRY_ID,
95-
CONF_PROXY_URL,
96-
CONF_TRUSTED_NETWORKS,
97-
DEFAULT_TRUSTED_NETWORKS,
9887
DOMAIN,
99-
PARSER_MD,
10088
PLATFORM_BROADCAST,
10189
PLATFORM_POLLING,
10290
PLATFORM_WEBHOOKS,
@@ -122,34 +110,7 @@
122110

123111
_LOGGER = logging.getLogger(__name__)
124112

125-
CONFIG_SCHEMA = vol.Schema(
126-
{
127-
DOMAIN: vol.All(
128-
cv.ensure_list,
129-
[
130-
vol.Schema(
131-
{
132-
vol.Required(CONF_PLATFORM): vol.In(
133-
(PLATFORM_BROADCAST, PLATFORM_POLLING, PLATFORM_WEBHOOKS)
134-
),
135-
vol.Required(CONF_API_KEY): cv.string,
136-
vol.Required(CONF_ALLOWED_CHAT_IDS): vol.All(
137-
cv.ensure_list, [vol.Coerce(int)]
138-
),
139-
vol.Optional(ATTR_PARSER, default=PARSER_MD): cv.string,
140-
vol.Optional(CONF_PROXY_URL): cv.string,
141-
# webhooks
142-
vol.Optional(CONF_URL): cv.url,
143-
vol.Optional(
144-
CONF_TRUSTED_NETWORKS, default=DEFAULT_TRUSTED_NETWORKS
145-
): vol.All(cv.ensure_list, [ip_network]),
146-
}
147-
)
148-
],
149-
)
150-
},
151-
extra=vol.ALLOW_EXTRA,
152-
)
113+
CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN)
153114

154115
BASE_SERVICE_SCHEMA = vol.Schema(
155116
{
@@ -385,34 +346,6 @@
385346
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
386347
"""Set up the Telegram bot component."""
387348

388-
# import the last YAML config since existing behavior only works with the last config
389-
domain_config: list[dict[str, Any]] | None = config.get(DOMAIN)
390-
if domain_config:
391-
trusted_networks: list[IPv4Network] = domain_config[-1].get(
392-
CONF_TRUSTED_NETWORKS, []
393-
)
394-
trusted_networks_str: list[str] = (
395-
[str(trusted_network) for trusted_network in trusted_networks]
396-
if trusted_networks
397-
else []
398-
)
399-
hass.async_create_task(
400-
hass.config_entries.flow.async_init(
401-
DOMAIN,
402-
context={CONF_SOURCE: SOURCE_IMPORT},
403-
data={
404-
CONF_PLATFORM: domain_config[-1][CONF_PLATFORM],
405-
CONF_API_KEY: domain_config[-1][CONF_API_KEY],
406-
CONF_ALLOWED_CHAT_IDS: domain_config[-1][CONF_ALLOWED_CHAT_IDS],
407-
ATTR_PARSER: domain_config[-1][ATTR_PARSER],
408-
CONF_PROXY_URL: domain_config[-1].get(CONF_PROXY_URL),
409-
CONF_URL: domain_config[-1].get(CONF_URL),
410-
CONF_TRUSTED_NETWORKS: trusted_networks_str,
411-
CONF_BOT_COUNT: len(domain_config),
412-
},
413-
)
414-
)
415-
416349
async def async_send_telegram_message(service: ServiceCall) -> ServiceResponse:
417350
"""Handle sending Telegram Bot message service calls."""
418351

homeassistant/components/telegram_bot/config_flow.py

Lines changed: 2 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,17 @@
1111
import voluptuous as vol
1212

1313
from homeassistant.config_entries import (
14-
SOURCE_IMPORT,
1514
SOURCE_RECONFIGURE,
1615
ConfigFlow,
1716
ConfigFlowResult,
18-
ConfigSubentryData,
1917
ConfigSubentryFlow,
2018
OptionsFlow,
2119
SubentryFlowResult,
2220
)
2321
from homeassistant.const import CONF_API_KEY, CONF_PLATFORM, CONF_URL
2422
from homeassistant.core import callback
25-
from homeassistant.data_entry_flow import AbortFlow, section
23+
from homeassistant.data_entry_flow import section
2624
from homeassistant.helpers import config_validation as cv
27-
from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue
2825
from homeassistant.helpers.network import NoURLAvailableError, get_url
2926
from homeassistant.helpers.selector import (
3027
SelectSelector,
@@ -39,18 +36,13 @@
3936
from .const import (
4037
ATTR_PARSER,
4138
BOT_NAME,
42-
CONF_ALLOWED_CHAT_IDS,
43-
CONF_BOT_COUNT,
4439
CONF_CHAT_ID,
4540
CONF_PROXY_URL,
4641
CONF_TRUSTED_NETWORKS,
4742
DEFAULT_TRUSTED_NETWORKS,
4843
DOMAIN,
4944
ERROR_FIELD,
5045
ERROR_MESSAGE,
51-
ISSUE_DEPRECATED_YAML,
52-
ISSUE_DEPRECATED_YAML_HAS_MORE_PLATFORMS,
53-
ISSUE_DEPRECATED_YAML_IMPORT_ISSUE_ERROR,
5446
PARSER_HTML,
5547
PARSER_MD,
5648
PARSER_MD2,
@@ -208,111 +200,6 @@ def __init__(self) -> None:
208200
# for passing data between steps
209201
self._step_user_data: dict[str, Any] = {}
210202

211-
# triggered by async_setup() from __init__.py
212-
async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult:
213-
"""Handle import of config entry from configuration.yaml."""
214-
215-
telegram_bot: str = f"{import_data[CONF_PLATFORM]} Telegram bot"
216-
bot_count: int = import_data[CONF_BOT_COUNT]
217-
218-
import_data[CONF_TRUSTED_NETWORKS] = ",".join(
219-
import_data[CONF_TRUSTED_NETWORKS]
220-
)
221-
import_data[SECTION_ADVANCED_SETTINGS] = {
222-
CONF_PROXY_URL: import_data.get(CONF_PROXY_URL)
223-
}
224-
try:
225-
config_flow_result: ConfigFlowResult = await self.async_step_user(
226-
import_data
227-
)
228-
except AbortFlow:
229-
# this happens if the config entry is already imported
230-
self._create_issue(ISSUE_DEPRECATED_YAML, telegram_bot, bot_count)
231-
raise
232-
else:
233-
errors: dict[str, str] | None = config_flow_result.get("errors")
234-
if errors:
235-
error: str = errors.get("base", "unknown")
236-
self._create_issue(
237-
error,
238-
telegram_bot,
239-
bot_count,
240-
config_flow_result["description_placeholders"],
241-
)
242-
return self.async_abort(reason="import_failed")
243-
244-
subentries: list[ConfigSubentryData] = []
245-
allowed_chat_ids: list[int] = import_data[CONF_ALLOWED_CHAT_IDS]
246-
assert self._bot is not None, "Bot should be initialized during import"
247-
for chat_id in allowed_chat_ids:
248-
chat_name: str = await _async_get_chat_name(self._bot, chat_id)
249-
subentry: ConfigSubentryData = ConfigSubentryData(
250-
data={CONF_CHAT_ID: chat_id},
251-
subentry_type=CONF_ALLOWED_CHAT_IDS,
252-
title=f"{chat_name} ({chat_id})",
253-
unique_id=str(chat_id),
254-
)
255-
subentries.append(subentry)
256-
config_flow_result["subentries"] = subentries
257-
258-
self._create_issue(
259-
ISSUE_DEPRECATED_YAML,
260-
telegram_bot,
261-
bot_count,
262-
config_flow_result["description_placeholders"],
263-
)
264-
return config_flow_result
265-
266-
def _create_issue(
267-
self,
268-
issue: str,
269-
telegram_bot_type: str,
270-
bot_count: int,
271-
description_placeholders: Mapping[str, str] | None = None,
272-
) -> None:
273-
translation_key: str = (
274-
ISSUE_DEPRECATED_YAML
275-
if bot_count == 1
276-
else ISSUE_DEPRECATED_YAML_HAS_MORE_PLATFORMS
277-
)
278-
if issue != ISSUE_DEPRECATED_YAML:
279-
translation_key = ISSUE_DEPRECATED_YAML_IMPORT_ISSUE_ERROR
280-
281-
telegram_bot = (
282-
description_placeholders.get(BOT_NAME, telegram_bot_type)
283-
if description_placeholders
284-
else telegram_bot_type
285-
)
286-
error_field = (
287-
description_placeholders.get(ERROR_FIELD, "Unknown error")
288-
if description_placeholders
289-
else "Unknown error"
290-
)
291-
error_message = (
292-
description_placeholders.get(ERROR_MESSAGE, "Unknown error")
293-
if description_placeholders
294-
else "Unknown error"
295-
)
296-
297-
async_create_issue(
298-
self.hass,
299-
DOMAIN,
300-
ISSUE_DEPRECATED_YAML,
301-
breaks_in_ha_version="2025.12.0",
302-
is_fixable=False,
303-
issue_domain=DOMAIN,
304-
severity=IssueSeverity.WARNING,
305-
translation_key=translation_key,
306-
translation_placeholders={
307-
"domain": DOMAIN,
308-
"integration_title": "Telegram Bot",
309-
"telegram_bot": telegram_bot,
310-
ERROR_FIELD: error_field,
311-
ERROR_MESSAGE: error_message,
312-
},
313-
learn_more_url="https://github.com/home-assistant/core/pull/144617",
314-
)
315-
316203
async def async_step_user(
317204
self, user_input: dict[str, Any] | None = None
318205
) -> ConfigFlowResult:
@@ -361,23 +248,13 @@ async def async_step_user(
361248
CONF_PROXY_URL
362249
),
363250
},
364-
options={
365-
# this value may come from yaml import
366-
ATTR_PARSER: user_input.get(ATTR_PARSER, PARSER_MD)
367-
},
251+
options={ATTR_PARSER: PARSER_MD},
368252
description_placeholders=description_placeholders,
369253
)
370254

371255
self._bot_name = bot_name
372256
self._step_user_data.update(user_input)
373257

374-
if self.source == SOURCE_IMPORT:
375-
return await self.async_step_webhooks(
376-
{
377-
CONF_URL: user_input.get(CONF_URL),
378-
CONF_TRUSTED_NETWORKS: user_input[CONF_TRUSTED_NETWORKS],
379-
}
380-
)
381258
return await self.async_step_webhooks()
382259

383260
async def _shutdown_bot(self) -> None:

homeassistant/components/telegram_bot/const.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
SECTION_ADVANCED_SETTINGS = "advanced_settings"
1111
SUBENTRY_TYPE_ALLOWED_CHAT_IDS = "allowed_chat_ids"
1212

13-
CONF_BOT_COUNT = "bot_count"
1413
CONF_ALLOWED_CHAT_IDS = "allowed_chat_ids"
1514
CONF_CONFIG_ENTRY_ID = "config_entry_id"
1615

@@ -24,12 +23,6 @@
2423
ERROR_FIELD = "error_field"
2524
ERROR_MESSAGE = "error_message"
2625

27-
ISSUE_DEPRECATED_YAML = "deprecated_yaml"
28-
ISSUE_DEPRECATED_YAML_HAS_MORE_PLATFORMS = (
29-
"deprecated_yaml_import_issue_has_more_platforms"
30-
)
31-
ISSUE_DEPRECATED_YAML_IMPORT_ISSUE_ERROR = "deprecated_yaml_import_issue_error"
32-
3326
DEFAULT_TRUSTED_NETWORKS = [ip_network("149.154.160.0/20"), ip_network("91.108.4.0/22")]
3427

3528
SERVICE_SEND_CHAT_ACTION = "send_chat_action"

homeassistant/components/telegram_bot/strings.json

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -220,18 +220,6 @@
220220
}
221221
},
222222
"title": "The `timeout` parameter for {integration_title} is being removed"
223-
},
224-
"deprecated_yaml": {
225-
"description": "Configuring {integration_title} using YAML is being removed.\n\nYour existing YAML configuration has been imported into the UI automatically.\n\nRemove the `{domain}` configuration from your configuration.yaml file and restart Home Assistant to fix this issue.",
226-
"title": "The {integration_title} YAML configuration is being removed"
227-
},
228-
"deprecated_yaml_import_issue_error": {
229-
"description": "Configuring {integration_title} using YAML is being removed but there was an error while importing your existing configuration ({telegram_bot}): {error_message}.\nSetup will not proceed.\n\nVerify that your {telegram_bot} is operating correctly and restart Home Assistant to attempt the import again.\n\nAlternatively, you may remove the `{domain}` configuration from your configuration.yaml entirely, restart Home Assistant, and add the {integration_title} integration manually.",
230-
"title": "YAML import failed due to invalid {error_field}"
231-
},
232-
"deprecated_yaml_import_issue_has_more_platforms": {
233-
"description": "Configuring {integration_title} using YAML is being removed.\n\nThe last entry of your existing YAML configuration ({telegram_bot}) has been imported into the UI automatically.\n\nRemove the `{domain}` configuration from your configuration.yaml file and restart Home Assistant to fix this issue. The other Telegram bots will need to be configured manually in the UI.",
234-
"title": "The {integration_title} YAML configuration is being removed"
235223
}
236224
},
237225
"options": {

0 commit comments

Comments
 (0)