fix import issue when there are no notifiers in configuration.yaml

This commit is contained in:
hanwg
2025-05-10 14:37:59 +00:00
parent 30c2bb4ae4
commit b5b83e2a9a
5 changed files with 41 additions and 38 deletions

View File

@ -14,6 +14,11 @@ CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN)
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Pass notify platform config from configuration.yaml to config flow for import."""
# Handle scenario where there's no notifiers in configuration.yaml
if Platform.NOTIFY not in config:
return True
for notify in config[Platform.NOTIFY]:
if notify[CONF_PLATFORM] == DOMAIN: # Only import Telegram notifiers
hass.async_create_task(

View File

@ -1 +1 @@
"""Tests for telegram component."""
"""Tests for Telegram integration."""

View File

@ -0,0 +1,27 @@
"""Fixtures for Telegram integration tests."""
from unittest.mock import patch
from homeassistant.components import notify
from homeassistant.components.telegram.const import DOMAIN
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
async def setup_dependencies(hass: HomeAssistant) -> None:
"""Set up the dependencies for the Telegram component."""
with patch("homeassistant.components.telegram_bot.async_setup", return_value=True):
assert await async_setup_component(
hass,
notify.DOMAIN,
{
notify.DOMAIN: [
{
"name": DOMAIN,
"platform": DOMAIN,
"chat_id": 1,
},
]
},
)
await hass.async_block_till_done()

View File

@ -1,33 +1,17 @@
"""Test the Telegram config flow."""
from unittest.mock import patch
from homeassistant.components import notify
from homeassistant.components.telegram.const import DOMAIN
from homeassistant.components.telegram import DOMAIN
from homeassistant.config_entries import SOURCE_USER
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
from homeassistant.setup import async_setup_component
from .conftest import setup_dependencies
async def test_async_step_user(hass: HomeAssistant) -> None:
"""Test config flow."""
"""Test config flow form display."""
with patch("homeassistant.components.telegram_bot.async_setup", return_value=True):
assert await async_setup_component(
hass,
notify.DOMAIN,
{
notify.DOMAIN: [
{
"name": DOMAIN,
"platform": DOMAIN,
"chat_id": 1,
},
]
},
)
await hass.async_block_till_done()
await setup_dependencies(hass)
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}

View File

@ -7,7 +7,8 @@ from homeassistant.components import notify
from homeassistant.components.telegram import DOMAIN
from homeassistant.const import SERVICE_RELOAD
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
from .conftest import setup_dependencies
from tests.common import get_fixture_path
@ -15,21 +16,7 @@ from tests.common import get_fixture_path
async def test_reload_notify(hass: HomeAssistant) -> None:
"""Verify we can reload the notify service."""
with patch("homeassistant.components.telegram_bot.async_setup", return_value=True):
assert await async_setup_component(
hass,
notify.DOMAIN,
{
notify.DOMAIN: [
{
"name": DOMAIN,
"platform": DOMAIN,
"chat_id": 1,
},
]
},
)
await hass.async_block_till_done()
await setup_dependencies(hass)
assert hass.services.has_service(notify.DOMAIN, DOMAIN)