diff --git a/aiowebostv/endpoints.py b/aiowebostv/endpoints.py index 6b87264..454cd89 100644 --- a/aiowebostv/endpoints.py +++ b/aiowebostv/endpoints.py @@ -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" diff --git a/aiowebostv/webos_client.py b/aiowebostv/webos_client.py index fd67c35..2f7336d 100644 --- a/aiowebostv/webos_client.py +++ b/aiowebostv/webos_client.py @@ -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.""" @@ -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: @@ -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))) @@ -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) @@ -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): @@ -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") @@ -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) diff --git a/bandit.yaml b/bandit.yaml index 568f77d..46566cc 100644 --- a/bandit.yaml +++ b/bandit.yaml @@ -13,7 +13,6 @@ tests: - B318 - B319 - B320 - - B325 - B601 - B602 - B604 diff --git a/pylintrc b/pylintrc index f6dfdfa..bea33f2 100644 --- a/pylintrc +++ b/pylintrc @@ -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