mirror of
https://github.com/home-assistant/core.git
synced 2026-12-03 04:08:19 -04:00
Use enums for media_extractor action names and arguments (#188173)
This commit is contained in:
@@ -2,8 +2,3 @@
|
||||
|
||||
DEFAULT_STREAM_QUERY = "best"
|
||||
DOMAIN = "media_extractor"
|
||||
|
||||
ATTR_URL = "url"
|
||||
ATTR_FORMAT_QUERY = "format_query"
|
||||
|
||||
SERVICE_EXTRACT_MEDIA_URL = "extract_media_url"
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
"""Services for the Media extractor integration."""
|
||||
|
||||
from collections.abc import Callable
|
||||
from enum import StrEnum
|
||||
import logging
|
||||
from pathlib import Path
|
||||
from typing import Any, cast
|
||||
@@ -10,10 +11,10 @@ from yt_dlp import YoutubeDL
|
||||
from yt_dlp.utils import DownloadError, ExtractorError
|
||||
|
||||
from homeassistant.components.media_player import (
|
||||
ATTR_MEDIA_CONTENT_ID,
|
||||
DOMAIN as MEDIA_PLAYER_DOMAIN,
|
||||
MEDIA_PLAYER_PLAY_MEDIA_SCHEMA,
|
||||
SERVICE_PLAY_MEDIA,
|
||||
MediaPlayerService,
|
||||
MediaPlayerServiceArgument,
|
||||
)
|
||||
from homeassistant.const import ATTR_ENTITY_ID
|
||||
from homeassistant.core import (
|
||||
@@ -26,17 +27,27 @@ from homeassistant.core import (
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
from homeassistant.helpers import config_validation as cv
|
||||
|
||||
from .const import (
|
||||
ATTR_FORMAT_QUERY,
|
||||
ATTR_URL,
|
||||
DEFAULT_STREAM_QUERY,
|
||||
DOMAIN,
|
||||
SERVICE_EXTRACT_MEDIA_URL,
|
||||
)
|
||||
from .const import DEFAULT_STREAM_QUERY, DOMAIN
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class MediaExtractorService(StrEnum):
|
||||
"""Store keys for media extractor services."""
|
||||
|
||||
EXTRACT_MEDIA_URL = "extract_media_url"
|
||||
PLAY_MEDIA = "play_media"
|
||||
|
||||
|
||||
class MediaExtractorServiceArgument(StrEnum):
|
||||
"""Store keys for media extractor service arguments."""
|
||||
|
||||
FORMAT_QUERY = "format_query"
|
||||
MEDIA_CONTENT_ID = "media_content_id"
|
||||
MEDIA_CONTENT_TYPE = "media_content_type"
|
||||
URL = "url"
|
||||
|
||||
|
||||
class MEDownloadException(Exception):
|
||||
"""Media extractor download exception."""
|
||||
|
||||
@@ -59,7 +70,7 @@ class MediaExtractor:
|
||||
|
||||
def get_media_url(self) -> str:
|
||||
"""Return media content url."""
|
||||
return cast(str, self.call_data[ATTR_MEDIA_CONTENT_ID])
|
||||
return cast(str, self.call_data[MediaExtractorServiceArgument.MEDIA_CONTENT_ID])
|
||||
|
||||
def get_entities(self) -> list[str]:
|
||||
"""Return list of entities."""
|
||||
@@ -144,13 +155,15 @@ class MediaExtractor:
|
||||
return
|
||||
_LOGGER.debug("Selected the following stream: %s", stream_url)
|
||||
data = {k: v for k, v in self.call_data.items() if k != ATTR_ENTITY_ID}
|
||||
data[ATTR_MEDIA_CONTENT_ID] = stream_url
|
||||
data[MediaPlayerServiceArgument.MEDIA_CONTENT_ID] = stream_url
|
||||
|
||||
if entity_id:
|
||||
data[ATTR_ENTITY_ID] = entity_id
|
||||
|
||||
self.hass.create_task(
|
||||
self.hass.services.async_call(MEDIA_PLAYER_DOMAIN, SERVICE_PLAY_MEDIA, data)
|
||||
self.hass.services.async_call(
|
||||
MEDIA_PLAYER_DOMAIN, MediaPlayerService.PLAY_MEDIA, data
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
@@ -189,12 +202,16 @@ async def _extract_media_url(call: ServiceCall) -> ServiceResponse:
|
||||
{
|
||||
"quiet": True,
|
||||
"logger": _LOGGER,
|
||||
"format": call.data[ATTR_FORMAT_QUERY],
|
||||
"format": call.data[MediaExtractorServiceArgument.FORMAT_QUERY],
|
||||
}
|
||||
)
|
||||
return cast(
|
||||
dict[str, Any],
|
||||
youtube_dl.extract_info(call.data[ATTR_URL], download=False, process=False),
|
||||
youtube_dl.extract_info(
|
||||
call.data[MediaExtractorServiceArgument.URL],
|
||||
download=False,
|
||||
process=False,
|
||||
),
|
||||
)
|
||||
|
||||
result = await hass.async_add_executor_job(extract_info)
|
||||
@@ -232,13 +249,14 @@ def async_setup_services(hass: HomeAssistant) -> None:
|
||||
|
||||
hass.services.async_register(
|
||||
DOMAIN,
|
||||
SERVICE_EXTRACT_MEDIA_URL,
|
||||
MediaExtractorService.EXTRACT_MEDIA_URL,
|
||||
_extract_media_url,
|
||||
schema=probatio.Schema(
|
||||
{
|
||||
probatio.Required(ATTR_URL): cv.string,
|
||||
probatio.Required(MediaExtractorServiceArgument.URL): cv.string,
|
||||
probatio.Optional(
|
||||
ATTR_FORMAT_QUERY, default=DEFAULT_STREAM_QUERY
|
||||
MediaExtractorServiceArgument.FORMAT_QUERY,
|
||||
default=DEFAULT_STREAM_QUERY,
|
||||
): cv.string,
|
||||
}
|
||||
),
|
||||
@@ -247,7 +265,7 @@ def async_setup_services(hass: HomeAssistant) -> None:
|
||||
|
||||
hass.services.async_register(
|
||||
DOMAIN,
|
||||
SERVICE_PLAY_MEDIA,
|
||||
MediaExtractorService.PLAY_MEDIA,
|
||||
_play_media,
|
||||
schema=cv.make_entity_service_schema(MEDIA_PLAYER_PLAY_MEDIA_SCHEMA),
|
||||
)
|
||||
|
||||
@@ -10,12 +10,11 @@ from pytest_homeassistant.common import MockConfigEntry, async_load_json_object_
|
||||
from syrupy.assertion import SnapshotAssertion
|
||||
from yt_dlp import DownloadError
|
||||
|
||||
from homeassistant.components.media_extractor.const import (
|
||||
ATTR_URL,
|
||||
DOMAIN,
|
||||
SERVICE_EXTRACT_MEDIA_URL,
|
||||
from homeassistant.components.media_extractor.const import DOMAIN
|
||||
from homeassistant.components.media_extractor.services import (
|
||||
MediaExtractorService,
|
||||
MediaExtractorServiceArgument,
|
||||
)
|
||||
from homeassistant.components.media_player import SERVICE_PLAY_MEDIA
|
||||
from homeassistant.core import HomeAssistant, ServiceCall
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
from homeassistant.setup import async_setup_component
|
||||
@@ -32,8 +31,8 @@ async def test_play_media_service_is_registered(hass: HomeAssistant) -> None:
|
||||
await hass.config_entries.async_setup(mock_config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert hass.services.has_service(DOMAIN, SERVICE_PLAY_MEDIA)
|
||||
assert hass.services.has_service(DOMAIN, SERVICE_EXTRACT_MEDIA_URL)
|
||||
assert hass.services.has_service(DOMAIN, MediaExtractorService.PLAY_MEDIA)
|
||||
assert hass.services.has_service(DOMAIN, MediaExtractorService.EXTRACT_MEDIA_URL)
|
||||
assert len(hass.config_entries.async_entries(DOMAIN))
|
||||
|
||||
|
||||
@@ -60,8 +59,8 @@ async def test_extract_media_service(
|
||||
assert (
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_EXTRACT_MEDIA_URL,
|
||||
{ATTR_URL: url},
|
||||
MediaExtractorService.EXTRACT_MEDIA_URL,
|
||||
{MediaExtractorServiceArgument.URL: url},
|
||||
blocking=True,
|
||||
return_response=True,
|
||||
)
|
||||
@@ -83,8 +82,8 @@ async def test_extracting_playlist_no_entries(
|
||||
) as exc_info:
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_EXTRACT_MEDIA_URL,
|
||||
{ATTR_URL: YOUTUBE_EMPTY_PLAYLIST},
|
||||
MediaExtractorService.EXTRACT_MEDIA_URL,
|
||||
{MediaExtractorServiceArgument.URL: YOUTUBE_EMPTY_PLAYLIST},
|
||||
blocking=True,
|
||||
return_response=True,
|
||||
)
|
||||
@@ -114,7 +113,7 @@ async def test_play_media_service(
|
||||
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_PLAY_MEDIA,
|
||||
MediaExtractorService.PLAY_MEDIA,
|
||||
{
|
||||
"entity_id": "media_player.bedroom",
|
||||
"media_content_type": media_content_type,
|
||||
@@ -144,7 +143,7 @@ async def test_download_error(
|
||||
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_PLAY_MEDIA,
|
||||
MediaExtractorService.PLAY_MEDIA,
|
||||
{
|
||||
"entity_id": "media_player.bedroom",
|
||||
"media_content_type": "VIDEO",
|
||||
@@ -171,7 +170,7 @@ async def test_no_target_entity(
|
||||
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_PLAY_MEDIA,
|
||||
MediaExtractorService.PLAY_MEDIA,
|
||||
{
|
||||
"device_id": "fb034c3a9fefe47c584c32a6b51817eb",
|
||||
"media_content_type": "VIDEO",
|
||||
@@ -198,7 +197,7 @@ async def test_playlist(
|
||||
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_PLAY_MEDIA,
|
||||
MediaExtractorService.PLAY_MEDIA,
|
||||
{
|
||||
"entity_id": "media_player.bedroom",
|
||||
"media_content_type": "VIDEO",
|
||||
@@ -225,7 +224,7 @@ async def test_playlist_no_entries(
|
||||
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_PLAY_MEDIA,
|
||||
MediaExtractorService.PLAY_MEDIA,
|
||||
{
|
||||
"entity_id": "media_player.bedroom",
|
||||
"media_content_type": "VIDEO",
|
||||
@@ -264,7 +263,7 @@ async def test_query_error(
|
||||
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_PLAY_MEDIA,
|
||||
MediaExtractorService.PLAY_MEDIA,
|
||||
{
|
||||
"entity_id": "media_player.bedroom",
|
||||
"media_content_type": "VIDEO",
|
||||
@@ -306,7 +305,7 @@ async def test_cookiefile_detection(
|
||||
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_PLAY_MEDIA,
|
||||
MediaExtractorService.PLAY_MEDIA,
|
||||
{
|
||||
"entity_id": "media_player.bedroom",
|
||||
"media_content_type": "VIDEO",
|
||||
@@ -321,7 +320,7 @@ async def test_cookiefile_detection(
|
||||
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_PLAY_MEDIA,
|
||||
MediaExtractorService.PLAY_MEDIA,
|
||||
{
|
||||
"entity_id": "media_player.bedroom",
|
||||
"media_content_type": "VIDEO",
|
||||
|
||||
Reference in New Issue
Block a user