Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#22 Add global play/pause #24

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions custom_components/ledfx/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,12 @@ async def set_audio_device(self, index: int, is_new: bool = False) -> dict:
)

return await self.request("audio/devices", Method.PUT, {"index": index})

async def toggle_play_pause(self) -> None:
"""toggle play/pause method.
"""

return await self.request("virtuals", Method.PUT)

async def run_scene(self, scene_id: str) -> dict:
"""scenes run method.
Expand Down
1 change: 1 addition & 0 deletions custom_components/ledfx/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
Platform.SWITCH,
Platform.NUMBER,
Platform.SELECT,
Platform.MEDIA_PLAYER,
]

"""Diagnostic const"""
Expand Down
99 changes: 99 additions & 0 deletions custom_components/ledfx/media_player.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
"""Media player component."""

from __future__ import annotations

from homeassistant.components.media_player import (
ENTITY_ID_FORMAT,
MediaPlayerEntity,
MediaPlayerEntityDescription,
MediaPlayerEntityFeature,
MediaPlayerState,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from .const import ATTR_STATE
from .entity import LedFxEntity
from .updater import LedFxUpdater, async_get_updater

MEDIA_PLAYERS: tuple[MediaPlayerEntityDescription, ...] = (
MediaPlayerEntityDescription(
key="media_player",
name="LedFx media player",
device_class="tv",
entity_registry_enabled_default=True,
),
)


async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up LedFx media player entry.

:param hass: HomeAssistant: Home Assistant object
:param config_entry: ConfigEntry: Config Entry object
:param async_add_entities: AddEntitiesCallback: Async add callback
"""

updater: LedFxUpdater = async_get_updater(hass, config_entry.entry_id)

entities: list[LedFxMediaPlayer] = [
LedFxMediaPlayer(
f"{config_entry.entry_id}-{description.key}",
description,
updater,
)
for description in MEDIA_PLAYERS
]
async_add_entities(entities)


class LedFxMediaPlayer(LedFxEntity, MediaPlayerEntity):
"""LedFx media player entry."""

def __init__(
self,
unique_id: str,
description: MediaPlayerEntityDescription,
updater: LedFxUpdater,
) -> None:
"""Initialize media player.

:param unique_id: str: Unique ID
:param description: MediaPlayerEntityDescription: MediaPlayerEntityDescription object
:param updater: LedFxUpdater: LedFx updater object
"""

LedFxEntity.__init__(self, unique_id, description, updater, ENTITY_ID_FORMAT)

self._supported_features = (
MediaPlayerEntityFeature.PLAY
| MediaPlayerEntityFeature.PAUSE
)

@property
def supported_features(self) -> MediaPlayerEntityFeature:
"""Supported features."""
return self._supported_features

def _handle_coordinator_update(self) -> None:
"""Update state."""

if self._updater.data.get("paused"):
self._attr_state = MediaPlayerState.PAUSED
else:
self._attr_state = MediaPlayerState.PLAYING

self.async_write_ha_state()

async def async_media_play(self) -> None:
"""Play media."""
await self._updater.client.toggle_play_pause()

async def async_media_pause(self) -> None:
"""Pause media."""
await self._updater.client.toggle_play_pause()
3 changes: 3 additions & 0 deletions custom_components/ledfx/updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,9 @@ async def update(self) -> dict:

self.data[ATTR_STATE] = codes.is_success(self.code)

v_response: dict = await self.client.virtuals()
self.data["paused"] = v_response["paused"]

return self.data

@cached_property
Expand Down