Import YAML config from async_setup_platform

This commit is contained in:
Michael "Chishm" Chisholm
2021-10-01 14:24:12 +10:00
parent 899db1c355
commit 09b02618b1
4 changed files with 71 additions and 88 deletions

View File

@@ -3,39 +3,13 @@ from __future__ import annotations
from homeassistant import config_entries
from homeassistant.components.media_player.const import DOMAIN as MEDIA_PLAYER_DOMAIN
from homeassistant.const import CONF_PLATFORM, CONF_URL
from homeassistant.core import HomeAssistant
from homeassistant.helpers.typing import ConfigType
from .const import DOMAIN, LOGGER
from .const import LOGGER
PLATFORMS = [MEDIA_PLAYER_DOMAIN]
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Set up DLNA component."""
if MEDIA_PLAYER_DOMAIN not in config:
return True
for entry_config in config[MEDIA_PLAYER_DOMAIN]:
if entry_config.get(CONF_PLATFORM) != DOMAIN:
continue
LOGGER.warning(
"Configuring dlna_dmr via yaml is deprecated; the configuration for"
" %s has been migrated to a config entry and can be safely removed",
entry_config.get(CONF_URL),
)
hass.async_create_task(
hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_IMPORT},
data=entry_config,
)
)
return True
async def async_setup_entry(
hass: HomeAssistant, entry: config_entries.ConfigEntry
) -> bool:

View File

@@ -42,6 +42,8 @@ from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry, entity_registry
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from .const import (
CONF_CALLBACK_URL_OVERRIDE,
@@ -98,6 +100,30 @@ def catch_request_errors(func: Func) -> Func:
return cast(Func, wrapper)
async def async_setup_platform(
hass: HomeAssistant,
config: ConfigType,
async_add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Set up DLNA media_player platform."""
del async_add_entities, discovery_info # Unused
hass.async_create_task(
hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_IMPORT},
data=config,
)
)
_LOGGER.warning(
"Configuring dlna_dmr via yaml is deprecated; the configuration for"
" %s has been migrated to a config entry and can be safely removed",
config.get(CONF_URL),
)
async def async_setup_entry(
hass: HomeAssistant,
entry: config_entries.ConfigEntry,

View File

@@ -1,59 +0,0 @@
"""Tests for the DLNA DMR __init__ module."""
from unittest.mock import Mock
from async_upnp_client import UpnpError
from homeassistant.components.dlna_dmr.const import (
CONF_LISTEN_PORT,
DOMAIN as DLNA_DOMAIN,
)
from homeassistant.components.media_player.const import DOMAIN as MEDIA_PLAYER_DOMAIN
from homeassistant.const import CONF_NAME, CONF_PLATFORM, CONF_URL
from homeassistant.core import HomeAssistant
from homeassistant.helpers.typing import ConfigType
from homeassistant.setup import async_setup_component
from .conftest import MOCK_DEVICE_LOCATION
async def test_import_flow_started(hass: HomeAssistant, domain_data_mock: Mock) -> None:
"""Test import flow of YAML config is started if there's config data."""
mock_config: ConfigType = {
MEDIA_PLAYER_DOMAIN: [
{
CONF_PLATFORM: DLNA_DOMAIN,
CONF_URL: MOCK_DEVICE_LOCATION,
CONF_LISTEN_PORT: 1234,
},
{
CONF_PLATFORM: "other_domain",
CONF_URL: MOCK_DEVICE_LOCATION,
CONF_NAME: "another device",
},
]
}
# Device is not available yet
domain_data_mock.upnp_factory.async_create_device.side_effect = UpnpError
# Run the setup
await async_setup_component(hass, DLNA_DOMAIN, mock_config)
await hass.async_block_till_done()
# Check config_flow has completed
assert hass.config_entries.flow.async_progress(include_uninitialized=True) == []
# Check device contact attempt was made
domain_data_mock.upnp_factory.async_create_device.assert_awaited_once_with(
MOCK_DEVICE_LOCATION
)
# Check the device is added to the unmigrated configs
assert domain_data_mock.unmigrated_config == {
MOCK_DEVICE_LOCATION: {
CONF_PLATFORM: DLNA_DOMAIN,
CONF_URL: MOCK_DEVICE_LOCATION,
CONF_LISTEN_PORT: 1234,
}
}

View File

@@ -19,12 +19,13 @@ from homeassistant.components.dlna_dmr.const import (
CONF_CALLBACK_URL_OVERRIDE,
CONF_LISTEN_PORT,
CONF_POLL_AVAILABILITY,
DEFAULT_NAME,
DOMAIN as DLNA_DOMAIN,
)
from homeassistant.components.dlna_dmr.data import EventListenAddr
from homeassistant.components.media_player import ATTR_TO_PROPERTY, const as mp_const
from homeassistant.components.media_player.const import DOMAIN as MP_DOMAIN
from homeassistant.const import ATTR_ENTITY_ID
from homeassistant.const import ATTR_ENTITY_ID, CONF_NAME, CONF_PLATFORM, CONF_URL
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import async_get as async_get_dr
from homeassistant.helpers.entity_component import async_update_entity
@@ -32,6 +33,7 @@ from homeassistant.helpers.entity_registry import (
async_entries_for_config_entry,
async_get as async_get_er,
)
from homeassistant.helpers.typing import ConfigType
from homeassistant.setup import async_setup_component
from .conftest import (
@@ -110,6 +112,46 @@ async def mock_disconnected_entity_id(
}
async def test_setup_platform_import_flow_started(
hass: HomeAssistant, domain_data_mock: Mock
) -> None:
"""Test import flow of YAML config is started if there's config data."""
mock_config: ConfigType = {
MP_DOMAIN: [
{
CONF_PLATFORM: DLNA_DOMAIN,
CONF_URL: MOCK_DEVICE_LOCATION,
CONF_LISTEN_PORT: 1234,
}
]
}
# Device is not available yet
domain_data_mock.upnp_factory.async_create_device.side_effect = UpnpError
# Run the setup
await async_setup_component(hass, MP_DOMAIN, mock_config)
await hass.async_block_till_done()
# Check config_flow has completed
assert hass.config_entries.flow.async_progress(include_uninitialized=True) == []
# Check device contact attempt was made
domain_data_mock.upnp_factory.async_create_device.assert_awaited_once_with(
MOCK_DEVICE_LOCATION
)
# Check the device is added to the unmigrated configs
assert domain_data_mock.unmigrated_config == {
MOCK_DEVICE_LOCATION: {
CONF_PLATFORM: DLNA_DOMAIN,
CONF_URL: MOCK_DEVICE_LOCATION,
CONF_LISTEN_PORT: 1234,
CONF_NAME: DEFAULT_NAME,
}
}
async def test_setup_entry_no_options(
hass: HomeAssistant,
domain_data_mock: Mock,
@@ -799,7 +841,7 @@ async def test_ssdp_byebye(
# Device should be gone
mock_state = hass.states.get(mock_entity_id)
assert mock_state is not None
assert mock_state.state == media_player.STATE_IDLE
assert mock_state.state == ha_const.STATE_UNAVAILABLE
# Second byebye will do nothing
await ssdp_callback(