Add cast app media (#35114)

* Extend media_play for media_type cast
This commit is contained in:
Eerovil 2020-05-11 20:22:26 +03:00 committed by GitHub
parent 132bb4e890
commit 3de4bc56b5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 4 deletions

View File

@ -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"]

View File

@ -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

View File

@ -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

View File

@ -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