diff --git a/tests/components/youtube/__init__.py b/tests/components/youtube/__init__.py index 31125d3a71e..c8e4f2b5f8e 100644 --- a/tests/components/youtube/__init__.py +++ b/tests/components/youtube/__init__.py @@ -6,7 +6,10 @@ import json from youtubeaio.models import YouTubeChannel, YouTubePlaylistItem, YouTubeSubscription from youtubeaio.types import AuthScope -from tests.common import load_fixture +from homeassistant.components.youtube import DOMAIN +from homeassistant.core import HomeAssistant + +from tests.common import async_load_fixture class MockYouTube: @@ -16,11 +19,13 @@ class MockYouTube: def __init__( self, - channel_fixture: str = "youtube/get_channel.json", - playlist_items_fixture: str = "youtube/get_playlist_items.json", - subscriptions_fixture: str = "youtube/get_subscriptions.json", + hass: HomeAssistant, + channel_fixture: str = "get_channel.json", + playlist_items_fixture: str = "get_playlist_items.json", + subscriptions_fixture: str = "get_subscriptions.json", ) -> None: """Initialize mock service.""" + self.hass = hass self._channel_fixture = channel_fixture self._playlist_items_fixture = playlist_items_fixture self._subscriptions_fixture = subscriptions_fixture @@ -32,7 +37,9 @@ class MockYouTube: async def get_user_channels(self) -> AsyncGenerator[YouTubeChannel]: """Get channels for authenticated user.""" - channels = json.loads(load_fixture(self._channel_fixture)) + channels = json.loads( + await async_load_fixture(self.hass, self._channel_fixture, DOMAIN) + ) for item in channels["items"]: yield YouTubeChannel(**item) @@ -42,7 +49,9 @@ class MockYouTube: """Get channels.""" if self._thrown_error is not None: raise self._thrown_error - channels = json.loads(load_fixture(self._channel_fixture)) + channels = json.loads( + await async_load_fixture(self.hass, self._channel_fixture, DOMAIN) + ) for item in channels["items"]: yield YouTubeChannel(**item) @@ -50,13 +59,17 @@ class MockYouTube: self, playlist_id: str, amount: int ) -> AsyncGenerator[YouTubePlaylistItem]: """Get channels.""" - channels = json.loads(load_fixture(self._playlist_items_fixture)) + channels = json.loads( + await async_load_fixture(self.hass, self._playlist_items_fixture, DOMAIN) + ) for item in channels["items"]: yield YouTubePlaylistItem(**item) async def get_user_subscriptions(self) -> AsyncGenerator[YouTubeSubscription]: """Get channels for authenticated user.""" - channels = json.loads(load_fixture(self._subscriptions_fixture)) + channels = json.loads( + await async_load_fixture(self.hass, self._subscriptions_fixture, DOMAIN) + ) for item in channels["items"]: yield YouTubeSubscription(**item) diff --git a/tests/components/youtube/conftest.py b/tests/components/youtube/conftest.py index 7f1caef47b5..7cc9bd2435b 100644 --- a/tests/components/youtube/conftest.py +++ b/tests/components/youtube/conftest.py @@ -107,7 +107,7 @@ async def mock_setup_integration( ) async def func() -> MockYouTube: - mock = MockYouTube() + mock = MockYouTube(hass) with patch("homeassistant.components.youtube.api.YouTube", return_value=mock): assert await async_setup_component(hass, DOMAIN, {}) await hass.async_block_till_done() diff --git a/tests/components/youtube/test_config_flow.py b/tests/components/youtube/test_config_flow.py index 2cfb970928d..b52978368c0 100644 --- a/tests/components/youtube/test_config_flow.py +++ b/tests/components/youtube/test_config_flow.py @@ -61,7 +61,7 @@ async def test_full_flow( ) as mock_setup, patch( "homeassistant.components.youtube.config_flow.YouTube", - return_value=MockYouTube(), + return_value=MockYouTube(hass), ), ): result = await hass.config_entries.flow.async_configure(result["flow_id"]) @@ -114,7 +114,7 @@ async def test_flow_abort_without_channel( assert resp.status == 200 assert resp.headers["content-type"] == "text/html; charset=utf-8" - service = MockYouTube(channel_fixture="youtube/get_no_channel.json") + service = MockYouTube(hass, channel_fixture="get_no_channel.json") with ( patch("homeassistant.components.youtube.async_setup_entry", return_value=True), patch( @@ -156,8 +156,9 @@ async def test_flow_abort_without_subscriptions( assert resp.headers["content-type"] == "text/html; charset=utf-8" service = MockYouTube( - channel_fixture="youtube/get_no_channel.json", - subscriptions_fixture="youtube/get_no_subscriptions.json", + hass, + channel_fixture="get_no_channel.json", + subscriptions_fixture="get_no_subscriptions.json", ) with ( patch("homeassistant.components.youtube.async_setup_entry", return_value=True), @@ -199,7 +200,7 @@ async def test_flow_without_subscriptions( assert resp.status == 200 assert resp.headers["content-type"] == "text/html; charset=utf-8" - service = MockYouTube(subscriptions_fixture="youtube/get_no_subscriptions.json") + service = MockYouTube(hass, subscriptions_fixture="get_no_subscriptions.json") with ( patch("homeassistant.components.youtube.async_setup_entry", return_value=True), patch( @@ -352,7 +353,7 @@ async def test_reauth( }, ) - youtube = MockYouTube(channel_fixture=f"youtube/{fixture}.json") + youtube = MockYouTube(hass, channel_fixture=f"{fixture}.json") with ( patch( "homeassistant.components.youtube.async_setup_entry", return_value=True @@ -422,7 +423,7 @@ async def test_options_flow( await setup_integration() with patch( "homeassistant.components.youtube.config_flow.YouTube", - return_value=MockYouTube(), + return_value=MockYouTube(hass), ): entry = hass.config_entries.async_entries(DOMAIN)[0] result = await hass.config_entries.options.async_init(entry.entry_id) @@ -476,7 +477,7 @@ async def test_own_channel_included( ) as mock_setup, patch( "homeassistant.components.youtube.config_flow.YouTube", - return_value=MockYouTube(), + return_value=MockYouTube(hass), ), ): result = await hass.config_entries.flow.async_configure(result["flow_id"]) @@ -522,7 +523,7 @@ async def test_options_flow_own_channel( await setup_integration() with patch( "homeassistant.components.youtube.config_flow.YouTube", - return_value=MockYouTube(), + return_value=MockYouTube(hass), ): entry = hass.config_entries.async_entries(DOMAIN)[0] result = await hass.config_entries.options.async_init(entry.entry_id) diff --git a/tests/components/youtube/test_sensor.py b/tests/components/youtube/test_sensor.py index 1090b8c391a..6b3fb55ef42 100644 --- a/tests/components/youtube/test_sensor.py +++ b/tests/components/youtube/test_sensor.py @@ -1,5 +1,6 @@ """Sensor tests for the YouTube integration.""" +import asyncio from datetime import timedelta from unittest.mock import patch @@ -42,12 +43,13 @@ async def test_sensor_without_uploaded_video( with patch( "homeassistant.components.youtube.api.AsyncConfigEntryAuth.get_resource", return_value=MockYouTube( - playlist_items_fixture="youtube/get_no_playlist_items.json" + hass, playlist_items_fixture="get_no_playlist_items.json" ), ): future = dt_util.utcnow() + timedelta(minutes=15) async_fire_time_changed(hass, future) await hass.async_block_till_done() + await asyncio.sleep(0.1) state = hass.states.get("sensor.google_for_developers_latest_upload") assert state == snapshot @@ -72,12 +74,13 @@ async def test_sensor_updating( with patch( "homeassistant.components.youtube.api.AsyncConfigEntryAuth.get_resource", return_value=MockYouTube( - playlist_items_fixture="youtube/get_playlist_items_2.json" + hass, playlist_items_fixture="get_playlist_items_2.json" ), ): future = dt_util.utcnow() + timedelta(minutes=15) async_fire_time_changed(hass, future) await hass.async_block_till_done() + await asyncio.sleep(0.1) state = hass.states.get("sensor.google_for_developers_latest_upload") assert state assert state.name == "Google for Developers Latest upload"