Bump pychromecast to 9.0.0 (#47086)

* Adapt to Pychromecast 9.0.0

* Bump pychromecast to 9.0.0

* Fix lint issues
This commit is contained in:
Erik Montnemery 2021-02-26 13:43:53 +01:00 committed by GitHub
parent d5ee49cd4e
commit dfbb653107
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 134 additions and 146 deletions

View file

@ -6,6 +6,7 @@ from unittest.mock import ANY, AsyncMock, MagicMock, Mock, patch
from uuid import UUID
import attr
import pychromecast
import pytest
from homeassistant.components import tts
@ -47,6 +48,12 @@ def dial_mock():
return dial_mock
@pytest.fixture()
def castbrowser_mock():
"""Mock pychromecast CastBrowser."""
return MagicMock()
@pytest.fixture()
def mz_mock():
"""Mock pychromecast MultizoneManager."""
@ -54,10 +61,13 @@ def mz_mock():
@pytest.fixture()
def pycast_mock():
def pycast_mock(castbrowser_mock):
"""Mock pychromecast."""
pycast_mock = MagicMock()
pycast_mock.start_discovery.return_value = (None, Mock())
pycast_mock.discovery.CastBrowser.return_value = castbrowser_mock
pycast_mock.discovery.AbstractCastListener = (
pychromecast.discovery.AbstractCastListener
)
return pycast_mock
@ -97,7 +107,7 @@ FakeGroupUUID = UUID("57355bce-9364-4aa6-ac1e-eb849dccf9e3")
def get_fake_chromecast(info: ChromecastInfo):
"""Generate a Fake Chromecast object with the specified arguments."""
mock = MagicMock(host=info.host, port=info.port, uuid=info.uuid)
mock = MagicMock(uuid=info.uuid)
mock.media_controller.status = None
return mock
@ -106,12 +116,35 @@ def get_fake_chromecast_info(
host="192.168.178.42", port=8009, uuid: Optional[UUID] = FakeUUID
):
"""Generate a Fake ChromecastInfo with the specified arguments."""
return ChromecastInfo(
@attr.s(slots=True, frozen=True, eq=False)
class ExtendedChromecastInfo(ChromecastInfo):
host: Optional[str] = attr.ib(default=None)
port: Optional[int] = attr.ib(default=0)
def __eq__(self, other):
if isinstance(other, ChromecastInfo):
return (
ChromecastInfo(
services=self.services,
uuid=self.uuid,
manufacturer=self.manufacturer,
model_name=self.model_name,
friendly_name=self.friendly_name,
is_audio_group=self.is_audio_group,
is_dynamic_group=self.is_dynamic_group,
)
== other
)
return super().__eq__(other)
return ExtendedChromecastInfo(
host=host,
port=port,
uuid=uuid,
friendly_name="Speaker",
services={"the-service"},
is_audio_group=port != 8009,
)
@ -141,32 +174,30 @@ async def async_setup_cast(hass, config=None):
async def async_setup_cast_internal_discovery(hass, config=None):
"""Set up the cast platform and the discovery."""
listener = MagicMock(services={})
browser = MagicMock(zc={})
browser = MagicMock(devices={}, zc={})
with patch(
"homeassistant.components.cast.discovery.pychromecast.CastListener",
return_value=listener,
) as cast_listener, patch(
"homeassistant.components.cast.discovery.pychromecast.start_discovery",
"homeassistant.components.cast.discovery.pychromecast.discovery.CastBrowser",
return_value=browser,
) as start_discovery:
) as cast_browser:
add_entities = await async_setup_cast(hass, config)
await hass.async_block_till_done()
await hass.async_block_till_done()
assert start_discovery.call_count == 1
assert browser.start_discovery.call_count == 1
discovery_callback = cast_listener.call_args[0][0]
remove_callback = cast_listener.call_args[0][1]
discovery_callback = cast_browser.call_args[0][0].add_cast
remove_callback = cast_browser.call_args[0][0].remove_cast
def discover_chromecast(service_name: str, info: ChromecastInfo) -> None:
"""Discover a chromecast device."""
listener.services[info.uuid] = (
browser.devices[info.uuid] = pychromecast.discovery.CastInfo(
{service_name},
info.uuid,
info.model_name,
info.friendly_name,
info.host,
info.port,
)
discovery_callback(info.uuid, service_name)
@ -175,7 +206,14 @@ async def async_setup_cast_internal_discovery(hass, config=None):
remove_callback(
info.uuid,
service_name,
(set(), info.uuid, info.model_name, info.friendly_name),
pychromecast.discovery.CastInfo(
set(),
info.uuid,
info.model_name,
info.friendly_name,
info.host,
info.port,
),
)
return discover_chromecast, remove_chromecast, add_entities
@ -183,21 +221,17 @@ async def async_setup_cast_internal_discovery(hass, config=None):
async def async_setup_media_player_cast(hass: HomeAssistantType, info: ChromecastInfo):
"""Set up the cast platform with async_setup_component."""
listener = MagicMock(services={})
browser = MagicMock(zc={})
browser = MagicMock(devices={}, zc={})
chromecast = get_fake_chromecast(info)
zconf = get_fake_zconf(host=info.host, port=info.port)
with patch(
"homeassistant.components.cast.discovery.pychromecast.get_chromecast_from_service",
"homeassistant.components.cast.discovery.pychromecast.get_chromecast_from_cast_info",
return_value=chromecast,
) as get_chromecast, patch(
"homeassistant.components.cast.discovery.pychromecast.CastListener",
return_value=listener,
) as cast_listener, patch(
"homeassistant.components.cast.discovery.pychromecast.start_discovery",
"homeassistant.components.cast.discovery.pychromecast.discovery.CastBrowser",
return_value=browser,
), patch(
) as cast_browser, patch(
"homeassistant.components.cast.discovery.ChromeCastZeroconf.get_zeroconf",
return_value=zconf,
):
@ -205,15 +239,18 @@ async def async_setup_media_player_cast(hass: HomeAssistantType, info: Chromecas
hass, "cast", {"cast": {"media_player": {"uuid": info.uuid}}}
)
await hass.async_block_till_done()
await hass.async_block_till_done()
discovery_callback = cast_listener.call_args[0][0]
discovery_callback = cast_browser.call_args[0][0].add_cast
service_name = "the-service"
listener.services[info.uuid] = (
browser.devices[info.uuid] = pychromecast.discovery.CastInfo(
{service_name},
info.uuid,
info.model_name,
info.friendly_name,
info.host,
info.port,
)
discovery_callback(info.uuid, service_name)
@ -223,11 +260,13 @@ async def async_setup_media_player_cast(hass: HomeAssistantType, info: Chromecas
def discover_chromecast(service_name: str, info: ChromecastInfo) -> None:
"""Discover a chromecast device."""
listener.services[info.uuid] = (
browser.devices[info.uuid] = pychromecast.discovery.CastInfo(
{service_name},
info.uuid,
info.model_name,
info.friendly_name,
info.host,
info.port,
)
discovery_callback(info.uuid, service_name)
@ -253,18 +292,13 @@ def get_status_callbacks(chromecast_mock, mz_mock=None):
return cast_status_cb, conn_status_cb, media_status_cb, group_media_status_cb
async def test_start_discovery_called_once(hass):
async def test_start_discovery_called_once(hass, castbrowser_mock):
"""Test pychromecast.start_discovery called exactly once."""
with patch(
"homeassistant.components.cast.discovery.pychromecast.start_discovery",
return_value=Mock(),
) as start_discovery:
await async_setup_cast(hass)
await async_setup_cast(hass)
assert castbrowser_mock.start_discovery.call_count == 1
assert start_discovery.call_count == 1
await async_setup_cast(hass)
assert start_discovery.call_count == 1
await async_setup_cast(hass)
assert castbrowser_mock.start_discovery.call_count == 1
async def test_internal_discovery_callback_fill_out(hass):
@ -350,7 +384,6 @@ async def test_internal_discovery_callback_fill_out_fail(hass):
# when called with incomplete info, it should use HTTP to get missing
discover = signal.mock_calls[0][1][0]
assert discover == full_info
# assert 1 == 2
async def test_internal_discovery_callback_fill_out_group(hass):
@ -384,27 +417,16 @@ async def test_internal_discovery_callback_fill_out_group(hass):
assert discover == full_info
async def test_stop_discovery_called_on_stop(hass):
async def test_stop_discovery_called_on_stop(hass, castbrowser_mock):
"""Test pychromecast.stop_discovery called on shutdown."""
browser = MagicMock(zc={})
# start_discovery should be called with empty config
await async_setup_cast(hass, {})
assert castbrowser_mock.start_discovery.call_count == 1
with patch(
"homeassistant.components.cast.discovery.pychromecast.start_discovery",
return_value=browser,
) as start_discovery:
# start_discovery should be called with empty config
await async_setup_cast(hass, {})
assert start_discovery.call_count == 1
with patch(
"homeassistant.components.cast.discovery.pychromecast.discovery.stop_discovery"
) as stop_discovery:
# stop discovery should be called on shutdown
hass.bus.async_fire(EVENT_HOMEASSISTANT_STOP)
await hass.async_block_till_done()
stop_discovery.assert_called_once_with(browser)
# stop discovery should be called on shutdown
hass.bus.async_fire(EVENT_HOMEASSISTANT_STOP)
await hass.async_block_till_done()
assert castbrowser_mock.stop_discovery.call_count == 1
async def test_create_cast_device_without_uuid(hass):
@ -539,7 +561,7 @@ async def test_discover_dynamic_group(hass, dial_mock, pycast_mock, caplog):
tmp2.uuid = FakeUUID2
dial_mock.get_multizone_status.return_value.dynamic_groups = [tmp1, tmp2]
pycast_mock.get_chromecast_from_service.assert_not_called()
pycast_mock.get_chromecast_from_cast_info.assert_not_called()
discover_cast, remove_cast, add_dev1 = await async_setup_cast_internal_discovery(
hass
)
@ -552,8 +574,8 @@ async def test_discover_dynamic_group(hass, dial_mock, pycast_mock, caplog):
discover_cast("service", cast_1)
await hass.async_block_till_done()
await hass.async_block_till_done() # having tasks that add jobs
pycast_mock.get_chromecast_from_service.assert_called()
pycast_mock.get_chromecast_from_service.reset_mock()
pycast_mock.get_chromecast_from_cast_info.assert_called()
pycast_mock.get_chromecast_from_cast_info.reset_mock()
assert add_dev1.call_count == 0
assert reg.async_get_entity_id("media_player", "cast", cast_1.uuid) is None
@ -565,8 +587,8 @@ async def test_discover_dynamic_group(hass, dial_mock, pycast_mock, caplog):
discover_cast("service", cast_2)
await hass.async_block_till_done()
await hass.async_block_till_done() # having tasks that add jobs
pycast_mock.get_chromecast_from_service.assert_called()
pycast_mock.get_chromecast_from_service.reset_mock()
pycast_mock.get_chromecast_from_cast_info.assert_called()
pycast_mock.get_chromecast_from_cast_info.reset_mock()
assert add_dev1.call_count == 0
assert reg.async_get_entity_id("media_player", "cast", cast_1.uuid) is None
@ -578,7 +600,7 @@ async def test_discover_dynamic_group(hass, dial_mock, pycast_mock, caplog):
discover_cast("service", cast_1)
await hass.async_block_till_done()
await hass.async_block_till_done() # having tasks that add jobs
pycast_mock.get_chromecast_from_service.assert_not_called()
pycast_mock.get_chromecast_from_cast_info.assert_not_called()
assert add_dev1.call_count == 0
assert reg.async_get_entity_id("media_player", "cast", cast_1.uuid) is None