From 02b76be0ba4f69e8bbee70870e477bb193a2e806 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Sun, 28 May 2023 03:07:54 +0200 Subject: [PATCH] Use config entry setup in cast tests (#93595) * Use config entry setup in cast tests * Remove import step from config flow * Remove import tests * Fix tests --- homeassistant/components/cast/__init__.py | 34 +----------- homeassistant/components/cast/config_flow.py | 15 ----- homeassistant/components/cast/media_player.py | 11 ---- tests/components/cast/conftest.py | 3 +- tests/components/cast/test_config_flow.py | 1 - tests/components/cast/test_init.py | 50 ----------------- tests/components/cast/test_media_player.py | 55 ++----------------- 7 files changed, 8 insertions(+), 161 deletions(-) delete mode 100644 tests/components/cast/test_init.py diff --git a/homeassistant/components/cast/__init__.py b/homeassistant/components/cast/__init__.py index 4d1c00f967b..aa0bdfa8118 100644 --- a/homeassistant/components/cast/__init__.py +++ b/homeassistant/components/cast/__init__.py @@ -1,14 +1,12 @@ """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 @@ -16,44 +14,14 @@ 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) diff --git a/homeassistant/components/cast/config_flow.py b/homeassistant/components/cast/config_flow.py index a5fc4360097..e58bcb71b28 100644 --- a/homeassistant/components/cast/config_flow.py +++ b/homeassistant/components/cast/config_flow.py @@ -38,21 +38,6 @@ class FlowHandler(config_entries.ConfigFlow, domain=DOMAIN): """Get the options flow for this handler.""" return CastOptionsFlowHandler(config_entry) - async def async_step_import(self, import_data=None): - """Import data.""" - if self._async_current_entries(): - return self.async_abort(reason="single_instance_allowed") - - media_player_config = import_data or [] - for cfg in media_player_config: - if CONF_IGNORE_CEC in cfg: - self._ignore_cec.update(set(cfg[CONF_IGNORE_CEC])) - if CONF_UUID in cfg: - self._wanted_uuid.add(cfg[CONF_UUID]) - - data = self._get_data() - return self.async_create_entry(title="Google Cast", data=data) - async def async_step_user(self, user_input=None): """Handle a flow initialized by the user.""" if self._async_current_entries(): diff --git a/homeassistant/components/cast/media_player.py b/homeassistant/components/cast/media_player.py index b701890d85d..3031eb8365b 100644 --- a/homeassistant/components/cast/media_player.py +++ b/homeassistant/components/cast/media_player.py @@ -23,7 +23,6 @@ from pychromecast.socket_client import ( CONNECTION_STATUS_CONNECTED, CONNECTION_STATUS_DISCONNECTED, ) -import voluptuous as vol import yarl from homeassistant.components import media_source, zeroconf @@ -47,7 +46,6 @@ from homeassistant.const import ( ) 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 @@ -83,15 +81,6 @@ APP_IDS_UNRELIABLE_MEDIA_INFO = ("Netflix",) 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): diff --git a/tests/components/cast/conftest.py b/tests/components/cast/conftest.py index dc50e42e458..bd1dc00659e 100644 --- a/tests/components/cast/conftest.py +++ b/tests/components/cast/conftest.py @@ -3,6 +3,7 @@ from unittest.mock import AsyncMock, MagicMock, patch import pychromecast +from pychromecast.controllers import multizone import pytest @@ -30,7 +31,7 @@ def castbrowser_mock(): @pytest.fixture def mz_mock(): """Mock pychromecast MultizoneManager.""" - return MagicMock(spec_set=pychromecast.controllers.multizone.MultizoneManager) + return MagicMock(spec_set=multizone.MultizoneManager) @pytest.fixture diff --git a/tests/components/cast/test_config_flow.py b/tests/components/cast/test_config_flow.py index c1ecedcf551..2d688489d39 100644 --- a/tests/components/cast/test_config_flow.py +++ b/tests/components/cast/test_config_flow.py @@ -39,7 +39,6 @@ async def test_creating_entry_sets_up_media_player(hass: HomeAssistant) -> None: @pytest.mark.parametrize( "source", [ - config_entries.SOURCE_IMPORT, config_entries.SOURCE_USER, config_entries.SOURCE_ZEROCONF, ], diff --git a/tests/components/cast/test_init.py b/tests/components/cast/test_init.py deleted file mode 100644 index 69640e29cef..00000000000 --- a/tests/components/cast/test_init.py +++ /dev/null @@ -1,50 +0,0 @@ -"""Tests for the Cast integration.""" -from unittest.mock import patch - -import pytest - -from homeassistant.components import cast -from homeassistant.core import HomeAssistant -from homeassistant.setup import async_setup_component - - -async def test_import(hass: HomeAssistant, caplog: pytest.LogCaptureFixture) -> None: - """Test that specifying config will create an entry.""" - with patch( - "homeassistant.components.cast.async_setup_entry", return_value=True - ) as mock_setup: - await async_setup_component( - hass, - cast.DOMAIN, - { - "cast": { - "media_player": [ - {"uuid": "abcd"}, - {"uuid": "abcd", "ignore_cec": "milk"}, - {"uuid": "efgh", "ignore_cec": "beer"}, - {"incorrect": "config"}, - ] - } - }, - ) - await hass.async_block_till_done() - - assert len(mock_setup.mock_calls) == 1 - - assert len(hass.config_entries.async_entries("cast")) == 1 - entry = hass.config_entries.async_entries("cast")[0] - assert set(entry.data["ignore_cec"]) == {"milk", "beer"} - assert set(entry.data["uuid"]) == {"abcd", "efgh"} - - assert "Invalid config '{'incorrect': 'config'}'" in caplog.text - - -async def test_not_configuring_cast_not_creates_entry(hass: HomeAssistant) -> None: - """Test that an empty config does not create an entry.""" - with patch( - "homeassistant.components.cast.async_setup_entry", return_value=True - ) as mock_setup: - await async_setup_component(hass, cast.DOMAIN, {}) - await hass.async_block_till_done() - - assert len(mock_setup.mock_calls) == 0 diff --git a/tests/components/cast/test_media_player.py b/tests/components/cast/test_media_player.py index 46a778f5e31..f290754d6fa 100644 --- a/tests/components/cast/test_media_player.py +++ b/tests/components/cast/test_media_player.py @@ -181,7 +181,7 @@ async def async_setup_cast_internal_discovery(hass, config=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) @@ -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() @@ -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: