From 7adfb67c12c9aca41c917861272f2f8c08bd972e Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Sat, 20 Oct 2018 19:27:15 +0200 Subject: [PATCH] Fixed Kodi plugin --- platypush/backend/http/__init__.py | 5 ++- platypush/plugins/__init__.py | 4 +- platypush/plugins/media/kodi.py | 72 +++++++++++++++++++++++++----- 3 files changed, 68 insertions(+), 13 deletions(-) diff --git a/platypush/backend/http/__init__.py b/platypush/backend/http/__init__.py index 2f1601849..f7afa70b4 100644 --- a/platypush/backend/http/__init__.py +++ b/platypush/backend/http/__init__.py @@ -141,7 +141,10 @@ class HttpBackend(Backend): import websockets async def send_event(websocket): - await websocket.send(str(event)) + try: + await websocket.send(str(event)) + except Exception as e: + self.logger.warning('Error on websocket send_event: {}'.format(e)) loop = asyncio.new_event_loop() diff --git a/platypush/plugins/__init__.py b/platypush/plugins/__init__.py index 041f88cf1..25995913c 100644 --- a/platypush/plugins/__init__.py +++ b/platypush/plugins/__init__.py @@ -19,7 +19,9 @@ def action(f): output = output.output elif isinstance(output, tuple) and len(output) == 2: errors = output[1] \ - if isinstance(output.errors, list) else [output[1]] + if isinstance(output[1], list) else [output[1]] + + if len(errors) == 1 and errors[0] is None: errors = [] output = output[0] except Exception as e: if isinstance(args[0], Plugin): diff --git a/platypush/plugins/media/kodi.py b/platypush/plugins/media/kodi.py index 6ec0ac8ed..2d3fad840 100644 --- a/platypush/plugins/media/kodi.py +++ b/platypush/plugins/media/kodi.py @@ -233,7 +233,7 @@ class MediaKodiPlugin(Plugin): muted = self._get_kodi().Application.GetProperties( properties=['muted']).get('result', {}).get('muted') - result = self._get_kodi().Application.SetMute(mute=not muted) + result = self._get_kodi().Application.SetMute(mute=(not muted)) return (result.get('result'), result.get('error')) @action @@ -326,19 +326,71 @@ class MediaKodiPlugin(Plugin): fullscreen = self._get_kodi().GUI.GetProperties( properties=['fullscreen']).get('result', {}).get('fullscreen') - result = self._get_kodi().GUI.SetFullscreen(fullscreen=not fullscreen) + result = self._get_kodi().GUI.SetFullscreen(fullscreen=(not fullscreen)) + return (result.get('result'), result.get('error')) + + @action + def toggle_shuffle(self, player_id=None, shuffle=None, *args, **kwargs): + """ + Set/unset shuffle mode + """ + + if not player_id: + player_id = self._get_player_id() + + if shuffle is None: + shuffle = self._get_kodi().Player.GetProperties( + playerid=player_id, + properties=['shuffled']).get('result', {}).get('shuffled') + + result = self._get_kodi().Player.SetShuffle( + playerid=player_id, shuffle=(not shuffle)) + return (result.get('result'), result.get('error')) + + @action + def toggle_repeat(self, player_id=None, repeat=None, *args, **kwargs): + """ + Set/unset repeat mode + """ + + if not player_id: + player_id = self._get_player_id() + + if repeat is None: + repeat = self._get_kodi().Player.GetProperties( + playerid=player_id, + properties=['repeat']).get('result', {}).get('repeat') + + result = self._get_kodi().Player.SetRepeat( + playerid=player_id, + repeat='off' if repeat in ('one','all') else 'off') + return (result.get('result'), result.get('error')) @action def seek(self, position, player_id=None, *args, **kwargs): """ Move the cursor to the specified position in seconds + + :param position: Seek time in seconds + :type position: int """ if not player_id: player_id = self._get_player_id() - result = self._get_kodi().Player.Seek(position) + hours = int(position/3600) + minutes = int((position - hours*3600)/60) + seconds = position - hours*3600 - minutes*60 + + position = { + 'hours': hours, + 'minutes': minutes, + 'seconds': seconds, + 'milliseconds': 0, + } + + result = self._get_kodi().Player.Seek(playerid=player_id, value=position) return (result.get('result'), result.get('error')) @action @@ -354,13 +406,12 @@ class MediaKodiPlugin(Plugin): player_id = self._get_player_id() position = self._get_kodi().Player.GetProperties( - playerid=player_id, properties=['time']).get('result') + playerid=player_id, properties=['time']).get('result', {}).get('time', {}) position = position.get('hours', 0)*3600 + \ - position.get('minutes', 0)*60 + position.get('seconds', 0) + position.get('minutes', 0)*60 + position.get('seconds', 0) - delta_seconds - result = self._get_kodi().Player.Seek(position+delta_seconds) - return (result.get('result'), result.get('error')) + return self.seek(player_id=player_id, position=position) @action def forward(self, delta_seconds=60, player_id=None, *args, **kwargs): @@ -375,13 +426,12 @@ class MediaKodiPlugin(Plugin): player_id = self._get_player_id() position = self._get_kodi().Player.GetProperties( - playerid=player_id, properties=['time']).get('result') + playerid=player_id, properties=['time']).get('result', {}).get('time', {}) position = position.get('hours', 0)*3600 + \ - position.get('minutes', 0)*60 + position.get('seconds', 0) + position.get('minutes', 0)*60 + position.get('seconds', 0) + delta_seconds - result = self._get_kodi().Player.Seek(position-delta_seconds) - return (result.get('result'), result.get('error')) + return self.seek(player_id=player_id, position=position) # vim:sw=4:ts=4:et: