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
This commit is contained in:
Erik Montnemery
2023-05-28 03:07:54 +02:00
committed by GitHub
parent fad3a4e168
commit 02b76be0ba
7 changed files with 8 additions and 161 deletions
+2 -1
View File
@@ -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
@@ -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,
],
-50
View File
@@ -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
+5 -50
View File
@@ -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: