Fix netatmo browse_media return type (#39751)

This commit is contained in:
Martin Hjelmare 2020-09-07 16:57:22 +02:00 committed by GitHub
parent 7c86fa0203
commit fc2cddc452
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,12 +1,13 @@
"""Netatmo Media Source Implementation."""
import datetime as dt
import logging
import re
from typing import Optional, Tuple
from homeassistant.components.media_player.const import MEDIA_TYPE_VIDEO
from homeassistant.components.media_player.errors import BrowseError
from homeassistant.components.media_source.const import MEDIA_MIME_TYPES
from homeassistant.components.media_source.error import Unresolvable
from homeassistant.components.media_source.error import MediaSourceError, Unresolvable
from homeassistant.components.media_source.models import (
BrowseMediaSource,
MediaSource,
@ -17,9 +18,14 @@ from homeassistant.core import HomeAssistant, callback
from .const import DATA_CAMERAS, DATA_EVENTS, DOMAIN, MANUFACTURER
_LOGGER = logging.getLogger(__name__)
MIME_TYPE = "application/x-mpegURL"
class IncompatibleMediaSource(MediaSourceError):
"""Incompatible media source attributes."""
async def async_get_media_source(hass: HomeAssistant):
"""Set up Netatmo media source."""
return NetatmoSource(hass)
@ -44,7 +50,7 @@ class NetatmoSource(MediaSource):
async def async_browse_media(
self, item: MediaSourceItem, media_types: Tuple[str] = MEDIA_MIME_TYPES
) -> Optional[BrowseMediaSource]:
) -> BrowseMediaSource:
"""Return media."""
try:
source, camera_id, event_id = async_parse_identifier(item)
@ -55,7 +61,7 @@ class NetatmoSource(MediaSource):
def _browse_media(
self, source: str, camera_id: str, event_id: int
) -> Optional[BrowseMediaSource]:
) -> BrowseMediaSource:
"""Browse media."""
if camera_id and camera_id not in self.events:
raise BrowseError("Camera does not exist.")
@ -67,7 +73,7 @@ class NetatmoSource(MediaSource):
def _build_item_response(
self, source: str, camera_id: str, event_id: int = None
) -> Optional[BrowseMediaSource]:
) -> BrowseMediaSource:
if event_id and event_id in self.events[camera_id]:
created = dt.datetime.fromtimestamp(event_id)
thumbnail = self.events[camera_id][event_id].get("snapshot", {}).get("url")
@ -95,7 +101,10 @@ class NetatmoSource(MediaSource):
)
if not media.can_play and not media.can_expand:
return None
_LOGGER.debug(
"Camera %s with event %s without media url found", camera_id, event_id
)
raise IncompatibleMediaSource
if not media.can_expand:
return media
@ -109,7 +118,10 @@ class NetatmoSource(MediaSource):
media.children.append(child)
else:
for eid in self.events[camera_id]:
try:
child = self._build_item_response(source, camera_id, eid)
except IncompatibleMediaSource:
continue
if child:
media.children.append(child)