Skip to content

Commit

Permalink
Add type annotations to quick_play.py (#817)
Browse files Browse the repository at this point in the history
* Add type annotations to quick_play.py

* Fix lint error
  • Loading branch information
emontnemery committed Feb 6, 2024
1 parent d8548ed commit c66d959
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 23 deletions.
1 change: 0 additions & 1 deletion mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,4 @@ warn_unreachable = true
exclude = (?x)(
^pychromecast/generated/ # The protobuf autogenerated files are not annotated
| ^pychromecast/controllers/plex\.py$
| ^pychromecast/quick_play\.py$
)
8 changes: 8 additions & 0 deletions pychromecast/controllers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,11 @@ def tear_down(self) -> None:
"""Called when we are shutting down."""
self._socket_client = None
self._message_func = None


class QuickPlayController(BaseController, abc.ABC):
"""ABC for controller which supports quick play."""

@abc.abstractmethod
def quick_play(self, *, media_id: str, **kwargs: Any) -> None:
"""Quick Play support for a controller."""
5 changes: 3 additions & 2 deletions pychromecast/controllers/bbciplayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ def __init__(self) -> None:
# pylint: disable-next=arguments-differ
def quick_play(
self,
media_id: str | None = None,
*,
media_id: str,
is_live: bool = False,
metadata: dict[str, Any] | None = None,
**kwargs: Any
Expand All @@ -39,7 +40,7 @@ def quick_play(
subtitle = metadata.pop("subtitle", "")

super().quick_play(
media_id,
media_id=media_id,
media_type=None,
stream_type=stream_type,
metadata=metadata,
Expand Down
5 changes: 3 additions & 2 deletions pychromecast/controllers/bbcsounds.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ def __init__(self) -> None:
# pylint: disable-next=arguments-differ
def quick_play(
self,
media_id: str | None = None,
*,
media_id: str,
is_live: bool = False,
metadata: dict[str, Any] | None = None,
**kwargs: Any
Expand All @@ -34,7 +35,7 @@ def quick_play(
if metadata is None:
metadata = metadata_default
super().quick_play(
media_id,
media_id=media_id,
media_type=None,
stream_type=stream_type,
metadata=metadata,
Expand Down
8 changes: 4 additions & 4 deletions pychromecast/controllers/media.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
CastMessage,
)
from ..response_handler import WaitResponse
from . import BaseController, CallbackType
from . import CallbackType, QuickPlayController

STREAM_TYPE_UNKNOWN = "UNKNOWN"
STREAM_TYPE_BUFFERED = "BUFFERED"
Expand Down Expand Up @@ -371,7 +371,7 @@ def load_media_failed(self, item: int, error_code: int) -> None:
"""Called when load media failed."""


class BaseMediaPlayer(BaseController):
class BaseMediaPlayer(QuickPlayController):
"""Mixin class for apps which can play media using the default media namespace."""

def __init__(self, supporting_app_id: str, app_must_match: bool = True) -> None:
Expand Down Expand Up @@ -541,14 +541,14 @@ def _send_start_play_media( # pylint: disable=too-many-locals

self.send_message(msg, inc_session_id=True, callback_function=callback_function)

def quick_play(self, media_id: str | None = None, **kwargs: Any) -> None:
def quick_play(self, *, media_id: str, **kwargs: Any) -> None:
"""Quick Play"""

media_type = kwargs.pop("media_type", "video/mp4")

response_handler = WaitResponse(30)
self.play_media(
media_id, media_type, **kwargs, callback_function=response_handler.callback # type: ignore[arg-type]
media_id, media_type, **kwargs, callback_function=response_handler.callback
)
request_completed = response_handler.wait_response()

Expand Down
8 changes: 3 additions & 5 deletions pychromecast/controllers/supla.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import logging
from typing import Any

from . import BaseController, CallbackType
from . import CallbackType, QuickPlayController
from ..config import APP_SUPLA
from ..error import PyChromecastError
from ..response_handler import WaitResponse
Expand All @@ -13,7 +13,7 @@


# pylint: disable=too-many-instance-attributes
class SuplaController(BaseController):
class SuplaController(QuickPlayController):
"""Controller to interact with Supla namespace."""

def __init__(self) -> None:
Expand Down Expand Up @@ -56,11 +56,9 @@ def play_media(
)

def quick_play(
self, media_id: str | None = None, is_live: bool = False, **kwargs: Any
self, *, media_id: str, is_live: bool = False, **kwargs: Any
) -> None:
"""Quick Play"""
if media_id is None:
raise ValueError("media_id must be specified")
response_handler = WaitResponse(10)
self.play_media(
media_id,
Expand Down
5 changes: 2 additions & 3 deletions pychromecast/controllers/yleareena.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,13 @@ def play_areena_media( # pylint: disable=too-many-locals
# pylint: disable-next=arguments-differ
def quick_play(
self,
media_id: str | None = None,
*,
media_id: str,
audio_lang: str = "",
text_lang: str = "off",
**kwargs: Any,
) -> None:
"""Quick Play"""
if media_id is None:
raise ValueError("media_id must be specified")
response_handler = WaitResponse(10)
self.play_areena_media(
media_id,
Expand Down
9 changes: 4 additions & 5 deletions pychromecast/controllers/youtube.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from casttube import YouTubeSession # type: ignore[import-untyped]

from . import BaseController
from . import QuickPlayController
from ..const import MESSAGE_TYPE
from ..generated.cast_channel_pb2 import ( # pylint: disable=no-name-in-module
CastMessage,
Expand All @@ -22,7 +22,7 @@
_LOGGER = logging.getLogger(__name__)


class YouTubeController(BaseController):
class YouTubeController(QuickPlayController):
"""Controller to interact with Youtube."""

_session: YouTubeSession
Expand Down Expand Up @@ -110,14 +110,13 @@ def _process_status(self, status: dict) -> None:

def quick_play(
self,
media_id: str | None = None,
*,
media_id: str,
playlist_id: str | None = None,
enqueue: bool = False,
**kwargs: Any
) -> None:
"""Quick Play"""
if media_id is None:
raise ValueError("media_id must be specified")
if enqueue:
self.add_to_queue(media_id, **kwargs)
else:
Expand Down
9 changes: 8 additions & 1 deletion pychromecast/quick_play.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
""" Choose a controller and quick play """

from typing import Any

from . import Chromecast
from .controllers import QuickPlayController
from .controllers.bbciplayer import BbcIplayerController
from .controllers.bbcsounds import BbcSoundsController
from .controllers.bubbleupnp import BubbleUPNPController
Expand All @@ -13,7 +17,9 @@
from .controllers.nrkradio import NrkRadioController


def quick_play(cast, app_name, data): # pylint:disable=too-many-branches
def quick_play( # pylint:disable=too-many-branches
cast: Chromecast, app_name: str, data: dict[str, Any]
) -> None:
"""
Given a Chromecast connection, launch the app `app_name` and start playing media
based on parameters defined in `data`.
Expand Down Expand Up @@ -56,6 +62,7 @@ def quick_play(cast, app_name, data): # pylint:disable=too-many-branches
"BUFFERED" or "LIVE"
"""

controller: QuickPlayController
if app_name == "bbciplayer":
controller = BbcIplayerController()
elif app_name == "bbcsounds":
Expand Down

0 comments on commit c66d959

Please sign in to comment.