Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use config entry setup in cast tests #93595

Merged
merged 4 commits into from
May 28, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 1 addition & 33 deletions homeassistant/components/cast/__init__.py
Original file line number Diff line number Diff line change
@@ -1,59 +1,27 @@
"""Component to embed Google Cast."""
from __future__ import annotations

import logging
from typing import Protocol

from pychromecast import Chromecast
import voluptuous as vol

from homeassistant.components.media_player import BrowseMedia, MediaType
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import config_validation as cv, device_registry as dr
from homeassistant.helpers.integration_platform import (
async_process_integration_platforms,
)
from homeassistant.helpers.typing import ConfigType

from . import home_assistant_cast
from .const import DOMAIN
from .media_player import ENTITY_SCHEMA

CONFIG_SCHEMA = cv.removed(DOMAIN, raise_if_present=False)

_LOGGER = logging.getLogger(__name__)

PLATFORMS = [Platform.MEDIA_PLAYER]


async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Set up the Cast component."""
if (conf := config.get(DOMAIN)) is not None:
media_player_config_validated = []
media_player_config = conf.get("media_player", {})
if not isinstance(media_player_config, list):
media_player_config = [media_player_config]
for cfg in media_player_config:
try:
cfg = ENTITY_SCHEMA(cfg)
media_player_config_validated.append(cfg)
except vol.Error as ex:
_LOGGER.warning("Invalid config '%s': %s", cfg, ex)

hass.async_create_task(
hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_IMPORT},
data=media_player_config_validated,
)
)

return True


async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up Cast from a config entry."""
await home_assistant_cast.async_setup_ha_cast(hass, entry)
Expand Down
11 changes: 0 additions & 11 deletions homeassistant/components/cast/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
CONNECTION_STATUS_CONNECTED,
CONNECTION_STATUS_DISCONNECTED,
)
import voluptuous as vol
import yarl

from homeassistant.components import media_source, zeroconf
Expand All @@ -47,7 +46,6 @@
)
from homeassistant.core import CALLBACK_TYPE, Event, HomeAssistant, callback
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
Expand Down Expand Up @@ -83,15 +81,6 @@

CAST_SPLASH = "https://www.home-assistant.io/images/cast/splash.png"

ENTITY_SCHEMA = vol.All(
vol.Schema(
{
vol.Optional(CONF_UUID): cv.string,
vol.Optional(CONF_IGNORE_CEC): vol.All(cv.ensure_list, [cv.string]),
}
),
)


@callback
def _async_create_cast_device(hass: HomeAssistant, info: ChromecastInfo):
Expand Down
55 changes: 5 additions & 50 deletions tests/components/cast/test_media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ def remove_chromecast(service_name: str, info: ChromecastInfo) -> None:


async def async_setup_media_player_cast(hass: HomeAssistant, info: ChromecastInfo):
"""Set up the cast platform with async_setup_component."""
"""Set up a cast config entry."""
browser = MagicMock(devices={}, zc={})
chromecast = get_fake_chromecast(info)
zconf = get_fake_zconf(host=info.cast_info.host, port=info.cast_info.port)
Expand All @@ -196,9 +196,10 @@ async def async_setup_media_player_cast(hass: HomeAssistant, info: ChromecastInf
"homeassistant.components.cast.discovery.ChromeCastZeroconf.get_zeroconf",
return_value=zconf,
):
await async_setup_component(
hass, "cast", {"cast": {"media_player": {"uuid": info.uuid}}}
)
data = {"ignore_cec": [], "known_hosts": [], "uuid": [str(info.uuid)]}
entry = MockConfigEntry(data=data, domain="cast")
entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
await hass.async_block_till_done()

Expand Down Expand Up @@ -2014,52 +2015,6 @@ async def test_entry_setup_no_config(hass: HomeAssistant) -> None:
assert not hass.config_entries.async_entries("cast")


async def test_entry_setup_empty_config(hass: HomeAssistant) -> None:
"""Test deprecated empty yaml config.."""
await async_setup_component(hass, "cast", {"cast": {}})
await hass.async_block_till_done()

config_entry = hass.config_entries.async_entries("cast")[0]
assert config_entry.data["uuid"] == []
assert config_entry.data["ignore_cec"] == []


async def test_entry_setup_single_config(hass: HomeAssistant) -> None:
"""Test deprecated yaml config with a single config media_player."""
await async_setup_component(
hass, "cast", {"cast": {"media_player": {"uuid": "bla", "ignore_cec": "cast1"}}}
)
await hass.async_block_till_done()

config_entry = hass.config_entries.async_entries("cast")[0]
assert config_entry.data["uuid"] == ["bla"]
assert config_entry.data["ignore_cec"] == ["cast1"]

assert ["cast1"] == pychromecast.IGNORE_CEC


async def test_entry_setup_list_config(hass: HomeAssistant) -> None:
"""Test deprecated yaml config with multiple media_players."""
await async_setup_component(
hass,
"cast",
{
"cast": {
"media_player": [
{"uuid": "bla", "ignore_cec": "cast1"},
{"uuid": "blu", "ignore_cec": ["cast2", "cast3"]},
]
}
},
)
await hass.async_block_till_done()

config_entry = hass.config_entries.async_entries("cast")[0]
assert set(config_entry.data["uuid"]) == {"bla", "blu"}
assert set(config_entry.data["ignore_cec"]) == {"cast1", "cast2", "cast3"}
assert set(pychromecast.IGNORE_CEC) == {"cast1", "cast2", "cast3"}


async def test_invalid_cast_platform(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
) -> None:
Expand Down