mirror of
https://github.com/home-assistant/core.git
synced 2025-07-30 08:47:09 +00:00
Better error handling when setting up config entry for Telegram bot (#149444)
This commit is contained in:
parent
48c4240a5d
commit
46d810b9f9
@ -7,7 +7,7 @@ from types import MappingProxyType
|
|||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from telegram import Bot, ChatFullInfo
|
from telegram import Bot, ChatFullInfo
|
||||||
from telegram.error import BadRequest, InvalidToken, NetworkError
|
from telegram.error import BadRequest, InvalidToken, TelegramError
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.config_entries import (
|
from homeassistant.config_entries import (
|
||||||
@ -399,13 +399,17 @@ class TelgramBotConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||||||
placeholders[ERROR_FIELD] = "API key"
|
placeholders[ERROR_FIELD] = "API key"
|
||||||
placeholders[ERROR_MESSAGE] = str(err)
|
placeholders[ERROR_MESSAGE] = str(err)
|
||||||
return "Unknown bot"
|
return "Unknown bot"
|
||||||
except (ValueError, NetworkError) as err:
|
except ValueError as err:
|
||||||
_LOGGER.warning("Invalid proxy")
|
_LOGGER.warning("Invalid proxy")
|
||||||
errors["base"] = "invalid_proxy_url"
|
errors["base"] = "invalid_proxy_url"
|
||||||
placeholders["proxy_url_error"] = str(err)
|
placeholders["proxy_url_error"] = str(err)
|
||||||
placeholders[ERROR_FIELD] = "proxy url"
|
placeholders[ERROR_FIELD] = "proxy url"
|
||||||
placeholders[ERROR_MESSAGE] = str(err)
|
placeholders[ERROR_MESSAGE] = str(err)
|
||||||
return "Unknown bot"
|
return "Unknown bot"
|
||||||
|
except TelegramError as err:
|
||||||
|
errors["base"] = "telegram_error"
|
||||||
|
placeholders[ERROR_MESSAGE] = str(err)
|
||||||
|
return "Unknown bot"
|
||||||
else:
|
else:
|
||||||
return user.full_name
|
return user.full_name
|
||||||
|
|
||||||
|
@ -66,6 +66,7 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"error": {
|
"error": {
|
||||||
|
"telegram_error": "Error from Telegram: {error_message}",
|
||||||
"invalid_api_key": "[%key:common::config_flow::error::invalid_api_key%]",
|
"invalid_api_key": "[%key:common::config_flow::error::invalid_api_key%]",
|
||||||
"invalid_proxy_url": "{proxy_url_error}",
|
"invalid_proxy_url": "{proxy_url_error}",
|
||||||
"no_url_available": "URL is required since you have not configured an external URL in Home Assistant",
|
"no_url_available": "URL is required since you have not configured an external URL in Home Assistant",
|
||||||
|
@ -221,11 +221,6 @@ async def test_create_entry(hass: HomeAssistant) -> None:
|
|||||||
|
|
||||||
# test: invalid proxy url
|
# test: invalid proxy url
|
||||||
|
|
||||||
with patch(
|
|
||||||
"homeassistant.components.telegram_bot.config_flow.Bot.get_me",
|
|
||||||
) as mock_bot:
|
|
||||||
mock_bot.side_effect = NetworkError("mock invalid proxy")
|
|
||||||
|
|
||||||
result = await hass.config_entries.flow.async_configure(
|
result = await hass.config_entries.flow.async_configure(
|
||||||
result["flow_id"],
|
result["flow_id"],
|
||||||
{
|
{
|
||||||
@ -241,6 +236,31 @@ async def test_create_entry(hass: HomeAssistant) -> None:
|
|||||||
assert result["step_id"] == "user"
|
assert result["step_id"] == "user"
|
||||||
assert result["type"] is FlowResultType.FORM
|
assert result["type"] is FlowResultType.FORM
|
||||||
assert result["errors"]["base"] == "invalid_proxy_url"
|
assert result["errors"]["base"] == "invalid_proxy_url"
|
||||||
|
assert result["description_placeholders"]["error_field"] == "proxy url"
|
||||||
|
|
||||||
|
# test: telegram error
|
||||||
|
|
||||||
|
with patch(
|
||||||
|
"homeassistant.components.telegram_bot.config_flow.Bot.get_me",
|
||||||
|
) as mock_bot:
|
||||||
|
mock_bot.side_effect = NetworkError("mock network error")
|
||||||
|
|
||||||
|
result = await hass.config_entries.flow.async_configure(
|
||||||
|
result["flow_id"],
|
||||||
|
{
|
||||||
|
CONF_PLATFORM: PLATFORM_WEBHOOKS,
|
||||||
|
CONF_API_KEY: "mock api key",
|
||||||
|
SECTION_ADVANCED_SETTINGS: {
|
||||||
|
CONF_PROXY_URL: "https://proxy",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
assert result["step_id"] == "user"
|
||||||
|
assert result["type"] is FlowResultType.FORM
|
||||||
|
assert result["errors"]["base"] == "telegram_error"
|
||||||
|
assert result["description_placeholders"]["error_message"] == "mock network error"
|
||||||
|
|
||||||
# test: valid input, to continue with webhooks step
|
# test: valid input, to continue with webhooks step
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user