mirror of
https://github.com/natekspencer/hacs-oasis_mini.git
synced 2025-12-06 18:44:14 -05:00
Merge pull request #107 from natekspencer/track-image-url
Use helper to get image from track dictionary
This commit is contained in:
@@ -17,7 +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
|
from .pyoasiscontrol.utils import get_image_url_from_track, get_track_ids_from_playlist
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@@ -128,7 +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=get_url_for_image(meta.get("image")),
|
thumbnail=get_image_url_from_track(meta),
|
||||||
)
|
)
|
||||||
for track_id, meta in TRACKS.items()
|
for track_id, meta in TRACKS.items()
|
||||||
]
|
]
|
||||||
@@ -156,15 +156,15 @@ def build_track_item(track_id: int) -> 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=get_url_for_image(meta.get("image")),
|
thumbnail=get_image_url_from_track(meta),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
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") or []:
|
for track in playlist.get("patterns") or []:
|
||||||
if image := track.get("image"):
|
if image := get_image_url_from_track(track):
|
||||||
return get_url_for_image(image)
|
return image
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ from .utils import (
|
|||||||
_parse_int,
|
_parse_int,
|
||||||
create_svg,
|
create_svg,
|
||||||
decrypt_svg_content,
|
decrypt_svg_content,
|
||||||
get_url_for_image,
|
get_image_url_from_track,
|
||||||
)
|
)
|
||||||
|
|
||||||
if TYPE_CHECKING: # avoid runtime circular imports
|
if TYPE_CHECKING: # avoid runtime circular imports
|
||||||
@@ -407,9 +407,7 @@ class OasisDevice:
|
|||||||
Returns:
|
Returns:
|
||||||
str: Full URL to the track 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:
|
return get_image_url_from_track(self.track)
|
||||||
return get_url_for_image(track.get("image"))
|
|
||||||
return None
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def track_name(self) -> str | None:
|
def track_name(self) -> str | None:
|
||||||
|
|||||||
@@ -22,6 +22,8 @@ COLOR_LIGHT_SHADE = ("#FFFFFF", "#86888F")
|
|||||||
COLOR_MEDIUM_SHADE = ("#E5E2DE", "#86888F")
|
COLOR_MEDIUM_SHADE = ("#E5E2DE", "#86888F")
|
||||||
COLOR_MEDIUM_TINT = ("#B8B8B8", "#FFFFFF")
|
COLOR_MEDIUM_TINT = ("#B8B8B8", "#FFFFFF")
|
||||||
|
|
||||||
|
IMAGE_URL = "https://app.grounded.so/uploads/{image}"
|
||||||
|
|
||||||
|
|
||||||
def _bit_to_bool(val: str) -> bool:
|
def _bit_to_bool(val: str) -> bool:
|
||||||
"""Convert a bit string to bool."""
|
"""Convert a bit string to bool."""
|
||||||
@@ -200,15 +202,17 @@ def decrypt_svg_content(svg_content: dict[str, str]):
|
|||||||
return decrypted
|
return decrypted
|
||||||
|
|
||||||
|
|
||||||
|
def get_image_url_from_track(track: dict[str, Any] | None) -> str | None:
|
||||||
|
"""Get the image URL from a track."""
|
||||||
|
if not isinstance(track, dict):
|
||||||
|
return None
|
||||||
|
return IMAGE_URL.format(image=image) if (image := track.get("image")) else None
|
||||||
|
|
||||||
|
|
||||||
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 []) if "id" in track]
|
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:
|
||||||
return datetime.now(UTC)
|
return datetime.now(UTC)
|
||||||
|
|||||||
Reference in New Issue
Block a user