diff --git a/homeassistant/components/cast/manifest.json b/homeassistant/components/cast/manifest.json index b8ad1fe67cc..ddb6697d370 100644 --- a/homeassistant/components/cast/manifest.json +++ b/homeassistant/components/cast/manifest.json @@ -3,7 +3,7 @@ "name": "Google Cast", "config_flow": true, "documentation": "https://www.home-assistant.io/integrations/cast", - "requirements": ["pychromecast==5.0.0"], + "requirements": ["pychromecast==5.1.0"], "after_dependencies": ["cloud"], "zeroconf": ["_googlecast._tcp.local."], "codeowners": ["@emontnemery"] diff --git a/homeassistant/components/cast/media_player.py b/homeassistant/components/cast/media_player.py index 9b6e78f156a..fd7443d1c26 100644 --- a/homeassistant/components/cast/media_player.py +++ b/homeassistant/components/cast/media_player.py @@ -1,11 +1,13 @@ """Provide functionality to interact with Cast devices on the network.""" import asyncio +import json import logging from typing import Optional import pychromecast from pychromecast.controllers.homeassistant import HomeAssistantController from pychromecast.controllers.multizone import MultizoneManager +from pychromecast.quick_play import quick_play from pychromecast.socket_client import ( CONNECTION_STATUS_CONNECTED, CONNECTION_STATUS_DISCONNECTED, @@ -477,7 +479,33 @@ class CastDevice(MediaPlayerEntity): def play_media(self, media_type, media_id, **kwargs): """Play media from a URL.""" # We do not want this to be forwarded to a group - self._chromecast.media_controller.play_media(media_id, media_type) + if media_type == CAST_DOMAIN: + try: + app_data = json.loads(media_id) + except json.JSONDecodeError: + _LOGGER.error("Invalid JSON in media_content_id") + raise + + # Special handling for passed `app_id` parameter. This will only launch + # an arbitrary cast app, generally for UX. + if "app_id" in app_data: + app_id = app_data.pop("app_id") + _LOGGER.info("Starting Cast app by ID %s", app_id) + self._chromecast.start_app(app_id) + if app_data: + _LOGGER.warning( + "Extra keys %s were ignored. Please use app_name to cast media.", + app_data.keys(), + ) + return + + app_name = app_data.pop("app_name") + try: + quick_play(self._chromecast, app_name, app_data) + except NotImplementedError: + _LOGGER.error("App %s not supported", app_name) + else: + self._chromecast.media_controller.play_media(media_id, media_type) # ========== Properties ========== @property diff --git a/requirements_all.txt b/requirements_all.txt index 66807333a11..9f4be8a8c7d 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -1245,7 +1245,7 @@ pycfdns==0.0.1 pychannels==1.0.0 # homeassistant.components.cast -pychromecast==5.0.0 +pychromecast==5.1.0 # homeassistant.components.cmus pycmus==0.1.1 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 2e704f2e38e..1cb7ad868cc 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -527,7 +527,7 @@ pyblackbird==0.5 pybotvac==0.0.17 # homeassistant.components.cast -pychromecast==5.0.0 +pychromecast==5.1.0 # homeassistant.components.coolmaster pycoolmasternet==0.0.4