mirror of
https://github.com/natekspencer/hacs-oasis_mini.git
synced 2025-12-06 18:44:14 -05:00
Address PR review
This commit is contained in:
@@ -17,6 +17,7 @@ from homeassistant.components.media_player import (
|
|||||||
|
|
||||||
from .pyoasiscontrol import OasisCloudClient
|
from .pyoasiscontrol import OasisCloudClient
|
||||||
from .pyoasiscontrol.const import TRACKS
|
from .pyoasiscontrol.const import TRACKS
|
||||||
|
from .pyoasiscontrol.utils import get_track_ids_from_playlist, get_url_for_image
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@@ -77,6 +78,7 @@ async def build_playlists_root(cloud: OasisCloudClient) -> BrowseMedia:
|
|||||||
thumbnail=get_first_image_for_playlist(playlist),
|
thumbnail=get_first_image_for_playlist(playlist),
|
||||||
)
|
)
|
||||||
for playlist in playlists
|
for playlist in playlists
|
||||||
|
if "id" in playlist
|
||||||
]
|
]
|
||||||
|
|
||||||
return BrowseMedia(
|
return BrowseMedia(
|
||||||
@@ -100,14 +102,7 @@ async def build_playlist_item(cloud: OasisCloudClient, playlist_id: int) -> Brow
|
|||||||
|
|
||||||
title = playlist.get("name") or f"Playlist #{playlist_id}"
|
title = playlist.get("name") or f"Playlist #{playlist_id}"
|
||||||
|
|
||||||
# Adjust this if the field name differs
|
track_ids = get_track_ids_from_playlist(playlist)
|
||||||
track_ids = playlist.get("patterns") or []
|
|
||||||
# Normalize to ints
|
|
||||||
try:
|
|
||||||
track_ids = [track["id"] for track in track_ids]
|
|
||||||
except Exception:
|
|
||||||
track_ids = []
|
|
||||||
|
|
||||||
children = [build_track_item(track_id) for track_id in track_ids]
|
children = [build_track_item(track_id) for track_id in track_ids]
|
||||||
|
|
||||||
return BrowseMedia(
|
return BrowseMedia(
|
||||||
@@ -133,11 +128,7 @@ def build_tracks_root() -> BrowseMedia:
|
|||||||
media_content_type=MEDIA_TYPE_OASIS_TRACK,
|
media_content_type=MEDIA_TYPE_OASIS_TRACK,
|
||||||
can_play=True,
|
can_play=True,
|
||||||
can_expand=False,
|
can_expand=False,
|
||||||
thumbnail=(
|
thumbnail=get_url_for_image(meta.get("image")),
|
||||||
f"https://app.grounded.so/uploads/{image}"
|
|
||||||
if (image := meta.get("image"))
|
|
||||||
else None
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
for track_id, meta in TRACKS.items()
|
for track_id, meta in TRACKS.items()
|
||||||
]
|
]
|
||||||
@@ -157,25 +148,23 @@ def build_tracks_root() -> BrowseMedia:
|
|||||||
def build_track_item(track_id: int) -> BrowseMedia:
|
def build_track_item(track_id: int) -> BrowseMedia:
|
||||||
"""Build a single track node for a given track id."""
|
"""Build a single track node for a given track id."""
|
||||||
meta = TRACKS.get(track_id) or {}
|
meta = TRACKS.get(track_id) or {}
|
||||||
title = meta.get("name") or f"Track #{track_id}"
|
|
||||||
image = meta.get("image")
|
|
||||||
|
|
||||||
return BrowseMedia(
|
return BrowseMedia(
|
||||||
title=title,
|
title=meta.get("name") or f"Track #{track_id}",
|
||||||
media_class=MediaClass.IMAGE,
|
media_class=MediaClass.IMAGE,
|
||||||
media_content_id=str(track_id),
|
media_content_id=str(track_id),
|
||||||
media_content_type=MEDIA_TYPE_OASIS_TRACK,
|
media_content_type=MEDIA_TYPE_OASIS_TRACK,
|
||||||
can_play=True,
|
can_play=True,
|
||||||
can_expand=False,
|
can_expand=False,
|
||||||
thumbnail=f"https://app.grounded.so/uploads/{image}",
|
thumbnail=get_url_for_image(meta.get("image")),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def get_first_image_for_playlist(playlist: dict[str, Any]) -> str | None:
|
def get_first_image_for_playlist(playlist: dict[str, Any]) -> str | None:
|
||||||
"""Get the first image from a playlist dictionary."""
|
"""Get the first image from a playlist dictionary."""
|
||||||
for track in playlist.get("patterns"):
|
for track in playlist.get("patterns") or []:
|
||||||
if image := track.get("image"):
|
if image := track.get("image"):
|
||||||
return f"https://app.grounded.so/uploads/{image}"
|
return get_url_for_image(image)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
@@ -239,7 +228,7 @@ async def async_search_media(
|
|||||||
media_content_type=MEDIA_TYPE_OASIS_PLAYLIST,
|
media_content_type=MEDIA_TYPE_OASIS_PLAYLIST,
|
||||||
can_play=True,
|
can_play=True,
|
||||||
can_expand=True,
|
can_expand=True,
|
||||||
thumbnail=None,
|
thumbnail=get_first_image_for_playlist(pl),
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -324,8 +324,8 @@ class OasisDeviceMediaPlayerEntity(OasisDeviceEntity, MediaPlayerEntity):
|
|||||||
track_ids = [track_id]
|
track_ids = [track_id]
|
||||||
|
|
||||||
else:
|
else:
|
||||||
track = list(filter(None, map(get_track_id, media_id.split(","))))
|
track_ids = list(filter(None, map(get_track_id, media_id.split(","))))
|
||||||
if not track:
|
if not track_ids:
|
||||||
raise ServiceValidationError(
|
raise ServiceValidationError(
|
||||||
translation_domain=DOMAIN,
|
translation_domain=DOMAIN,
|
||||||
translation_key="invalid_media",
|
translation_key="invalid_media",
|
||||||
|
|||||||
@@ -495,7 +495,7 @@ class OasisMqttClient(OasisClientProtocol):
|
|||||||
playlist (list[int]): Ordered list of track indices to apply as the device's playlist.
|
playlist (list[int]): Ordered list of track indices to apply as the device's playlist.
|
||||||
"""
|
"""
|
||||||
track_str = ",".join(map(str, playlist))
|
track_str = ",".join(map(str, playlist))
|
||||||
payload = f"WRIJOBLIST={track_str or '0'}"
|
payload = f"WRIJOBLIST={track_str}"
|
||||||
await self._publish_command(device, payload)
|
await self._publish_command(device, payload)
|
||||||
|
|
||||||
async def async_send_set_repeat_playlist_command(
|
async def async_send_set_repeat_playlist_command(
|
||||||
|
|||||||
@@ -14,7 +14,13 @@ from .const import (
|
|||||||
STATUS_SLEEPING,
|
STATUS_SLEEPING,
|
||||||
TRACKS,
|
TRACKS,
|
||||||
)
|
)
|
||||||
from .utils import _bit_to_bool, _parse_int, create_svg, decrypt_svg_content
|
from .utils import (
|
||||||
|
_bit_to_bool,
|
||||||
|
_parse_int,
|
||||||
|
create_svg,
|
||||||
|
decrypt_svg_content,
|
||||||
|
get_url_for_image,
|
||||||
|
)
|
||||||
|
|
||||||
if TYPE_CHECKING: # avoid runtime circular imports
|
if TYPE_CHECKING: # avoid runtime circular imports
|
||||||
from .clients import OasisCloudClient
|
from .clients import OasisCloudClient
|
||||||
@@ -399,10 +405,10 @@ class OasisDevice:
|
|||||||
Get the full HTTPS URL for the current track's image if available.
|
Get the full HTTPS URL for the current track's image if available.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
str: Full URL to the track image (https://app.grounded.so/uploads/<image>), or `None` if no image is available.
|
str: Full URL to the track image or `None` if no image is available.
|
||||||
"""
|
"""
|
||||||
if (track := self.track) and (image := track.get("image")):
|
if track := self.track:
|
||||||
return f"https://app.grounded.so/uploads/{image}"
|
return get_url_for_image(track.get("image"))
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
|||||||
@@ -202,7 +202,12 @@ def decrypt_svg_content(svg_content: dict[str, str]):
|
|||||||
|
|
||||||
def get_track_ids_from_playlist(playlist: dict[str, Any]) -> list[int]:
|
def get_track_ids_from_playlist(playlist: dict[str, Any]) -> list[int]:
|
||||||
"""Get a list of track ids from a playlist."""
|
"""Get a list of track ids from a playlist."""
|
||||||
return [track["id"] for track in (playlist.get("patterns") or [])]
|
return [track["id"] for track in (playlist.get("patterns") or []) if "id" in track]
|
||||||
|
|
||||||
|
|
||||||
|
def get_url_for_image(image: str | None) -> str | None:
|
||||||
|
"""Get the full URL for an image."""
|
||||||
|
return f"https://app.grounded.so/uploads/{image}" if image else None
|
||||||
|
|
||||||
|
|
||||||
def now() -> datetime:
|
def now() -> datetime:
|
||||||
|
|||||||
Reference in New Issue
Block a user