Skip to content

Commit

Permalink
Add media_seek to streaming #594 by @Laxilef
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxIT committed Jan 29, 2025
1 parent d9045bf commit ccc2bcc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
7 changes: 3 additions & 4 deletions custom_components/yandex_station/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import network
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.entity_component import EntityComponent
from homeassistant.helpers.event import (
TrackTemplate,
Expand Down Expand Up @@ -430,12 +431,10 @@ async def action(event, updates: list[TrackTemplateResult]):
return track.async_remove


def get_platform(hass: HomeAssistant, entity_id: str) -> str | None:
def get_entity(hass: HomeAssistant, entity_id: str) -> Entity | None:
try:
ec: EntityComponent = hass.data["entity_components"]["media_player"]
for entity in ec.entities:
if entity.entity_id == entity_id:
return entity.platform.platform_name
return next(e for e in ec.entities if e.entity_id == entity_id)
except:
pass
return None
Expand Down
16 changes: 15 additions & 1 deletion custom_components/yandex_station/core/yandex_station.py
Original file line number Diff line number Diff line change
Expand Up @@ -950,10 +950,24 @@ async def async_select_source(self, source: str):
if self.sync_sources and (source := self.sync_sources.get(source)):
self.sync_enabled = True
if "platform" not in source:
source["platform"] = utils.get_platform(self.hass, source["entity_id"])
if entity := utils.get_entity(self.hass, source["entity_id"]):
source["platform"] = entity.platform.platform_name
else:
self.sync_enabled = False

async def async_media_seek(self, position):
await super().async_media_seek(position)

if self.sync_enabled:
entity_id = self.sync_sources[self._attr_source]["entity_id"]
if entity := utils.get_entity(self.hass, entity_id):
if entity.supported_features & MediaPlayerEntityFeature.SEEK:
await self.hass.services.async_call(
"media_player",
"media_seek",
{"entity_id": entity_id, "seek_position": position},
)

@callback
def async_set_state(self, data: dict):
super().async_set_state(data)
Expand Down

0 comments on commit ccc2bcc

Please sign in to comment.