Skip to content

Commit

Permalink
Add support for media player state (#178)
Browse files Browse the repository at this point in the history
* Get media playback state

* Fix formatting

* remove obsolete test

* disable too-many-lines
  • Loading branch information
rale authored Feb 3, 2024
1 parent 31a423e commit b4cd5bc
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
1 change: 1 addition & 0 deletions aiowebostv/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
TURN_OFF_SCREEN = "com.webos.service.tvpower/power/turnOffScreen"
TURN_ON_SCREEN = "com.webos.service.tvpower/power/turnOnScreen"
GET_CONFIGS = "config/getConfigs"
GET_MEDIA_FOREGROUND_APP_INFO = "com.webos.media/getForegroundAppInfo"

# webOS TV internal Luna API endpoints
LUNA_SET_CONFIGS = "com.webos.service.config/setConfigs"
Expand Down
34 changes: 33 additions & 1 deletion aiowebostv/webos_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def __init__(
self._volume_step_lock = asyncio.Lock()
self._volume_step_delay = None
self._loop = asyncio.get_running_loop()
self._media_state = None

async def connect(self):
"""Connect to webOS TV device."""
Expand Down Expand Up @@ -205,6 +206,7 @@ async def connect_handler(self, res):
self.subscribe_apps(self.set_apps_state),
self.subscribe_inputs(self.set_inputs_state),
self.subscribe_sound_output(self.set_sound_output_state),
self.subscribe_media_foreground_app(self.set_media_state),
}
subscribe_tasks = set()
for state_update in subscribe_state_updates:
Expand Down Expand Up @@ -264,6 +266,7 @@ async def connect_handler(self, res):
self._software_info = None
self._hello_info = None
self._sound_output = None
self._media_state = None

for callback in self.state_update_callbacks:
closeout.add(asyncio.create_task(callback(self)))
Expand Down Expand Up @@ -424,6 +427,11 @@ def is_screen_on(self):
return self._power_state.get("state") != "Screen Off"
return False

@property
def media_state(self):
"""Return media player state."""
return self._media_state

async def register_state_update_callback(self, callback):
"""Register user state update callback."""
self.state_update_callbacks.append(callback)
Expand Down Expand Up @@ -569,6 +577,13 @@ async def set_sound_output_state(self, sound_output):
if self.state_update_callbacks and self.do_state_update:
await self.do_state_update_callbacks()

async def set_media_state(self, foreground_app_info):
"""Set TV media player state callback."""
self._media_state = foreground_app_info

if self.state_update_callbacks and self.do_state_update:
await self.do_state_update_callbacks()

# low level request handling

async def command(self, request_type, uri, payload=None, uid=None):
Expand Down Expand Up @@ -617,7 +632,11 @@ async def request(self, uri, payload=None, cmd_type="request", uid=None):
if payload is None:
raise WebOsTvCommandError(f"Invalid request response {response}")

return_value = payload.get("returnValue") or payload.get("subscribed")
return_value = (
payload.get("returnValue")
or payload.get("subscribed")
or payload.get("subscription")
)

if response.get("type") == "error":
error = response.get("error")
Expand Down Expand Up @@ -972,3 +991,16 @@ async def rewind(self):
async def fast_forward(self):
"""Fast Forward media."""
return await self.request(ep.MEDIA_FAST_FORWARD)

async def get_media_foreground_app(self):
"""Get media player state."""
res = await self.request(ep.GET_MEDIA_FOREGROUND_APP_INFO)
return res.get("foregroundAppInfo")

async def subscribe_media_foreground_app(self, callback):
"""Subscribe to changes in media player state."""

async def current_media(payload):
await callback(payload)

return await self.subscribe(current_media, ep.GET_MEDIA_FOREGROUND_APP_INFO)
1 change: 0 additions & 1 deletion bandit.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ tests:
- B318
- B319
- B320
- B325
- B601
- B602
- B604
Expand Down
3 changes: 2 additions & 1 deletion pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ disable=
too-many-arguments,
too-many-public-methods,
too-many-instance-attributes,
too-many-branches
too-many-branches,
too-many-lines

[REPORTS]
score=no
Expand Down

0 comments on commit b4cd5bc

Please sign in to comment.