diff --git a/pylgtv/webos_client.py b/pylgtv/webos_client.py index 1dd20e6..4385719 100644 --- a/pylgtv/webos_client.py +++ b/pylgtv/webos_client.py @@ -127,12 +127,13 @@ def _register(self): logger.error('register failed to connect to %s', "ws://{}:{}".format(self.ip, self.port)); return False - logger.info('register websocket connected to %s', "ws://{}:{}".format(self.ip, self.port)); + logger.debug('register websocket connected to %s', "ws://{}:{}".format(self.ip, self.port)); try: yield from self._send_register_payload(websocket) finally: + logger.debug('close register connection to %s', "ws://{}:{}".format(self.ip, self.port)); yield from websocket.close() def register(self): @@ -148,12 +149,11 @@ def _command(self, msg): try: websocket = yield from websockets.connect( "ws://{}:{}".format(self.ip, self.port), timeout=self.timeout_connect) - except: logger.error('command failed to connect to %s', "ws://{}:{}".format(self.ip, self.port)); return False - logger.info('command websocket connected to %s', "ws://{}:{}".format(self.ip, self.port)); + logger.debug('command websocket connected to %s', "ws://{}:{}".format(self.ip, self.port)); try: yield from self._send_register_payload(websocket) @@ -168,6 +168,7 @@ def _command(self, msg): self.last_response = json.loads(raw_response) finally: + logger.debug('close command connection to %s', "ws://{}:{}".format(self.ip, self.port)); yield from websocket.close() def command(self, request_type, uri, payload): @@ -185,9 +186,13 @@ def command(self, request_type, uri, payload): } self.last_response = None - loop = asyncio.new_event_loop() - asyncio.set_event_loop(loop) - loop.run_until_complete(asyncio.wait_for(self._command(message), self.timeout_connect)) + + try: + loop = asyncio.new_event_loop() + asyncio.set_event_loop(loop) + loop.run_until_complete(asyncio.wait_for(self._command(message), self.timeout_connect, loop=loop)) + finally: + loop.close() def request(self, uri, payload=None): """Send a request.""" @@ -213,12 +218,12 @@ def send_message(self, message, icon_path=None): def get_apps(self): """Return all apps.""" self.request(EP_GET_APPS) - return self.last_response['payload']['launchPoints'] + return {} if self.last_response is None else self.last_response.get('payload').get('launchPoints') def get_current_app(self): """Get the current app id.""" self.request(EP_GET_CURRENT_APP_INFO) - return self.last_response['payload']['appId'] + return None if self.last_response is None else self.last_response.get('payload').get('appId') def launch_app(self, app): """Launch an app.""" @@ -243,12 +248,12 @@ def close_app(self, app): def get_services(self): """Get all services.""" self.request(EP_GET_SERVICES) - return self.last_response['payload']['services'] + return {} if self.last_response is None else self.last_response.get('payload').get('services') def get_software_info(self): """Return the current software status.""" self.request(EP_GET_SOFTWARE_INFO) - return self.last_response['payload'] + return {} if self.last_response is None else self.last_response.get('payload') def power_off(self): """Play media.""" @@ -267,7 +272,7 @@ def turn_3d_off(self): def get_inputs(self): """Get all inputs.""" self.request(EP_GET_INPUTS) - return self.last_response['payload']['devices'] + return {} if self.last_response is None else self.last_response.get('payload').get('devices') def get_input(self): """Get current input.""" @@ -283,11 +288,11 @@ def set_input(self, input): def get_audio_status(self): """Get the current audio status""" self.request(EP_GET_AUDIO_STATUS) - return self.last_response['payload'] + return {} if self.last_response is None else self.last_response.get('payload') def get_muted(self): """Get mute status.""" - return self.get_audio_status()['mute'] + return self.get_audio_status().get('mute') def set_mute(self, mute): """Set mute.""" @@ -298,7 +303,7 @@ def set_mute(self, mute): def get_volume(self): """Get the current volume.""" self.request(EP_GET_VOLUME) - return self.last_response['payload']['volume'] + return 0 if self.last_response is None else self.last_response.get('payload').get('volume') def set_volume(self, volume): """Set volume.""" @@ -327,17 +332,17 @@ def channel_down(self): def get_channels(self): """Get all tv channels.""" self.request(EP_GET_TV_CHANNELS) - return self.last_response['payload']['channelList'] + return {} if self.last_response is None else self.last_response.get('payload').get('channelList') def get_current_channel(self): """Get the current tv channel.""" self.request(EP_GET_CURRENT_CHANNEL) - return self.last_response['payload'] + return {} if self.last_response is None else self.last_response.get('payload') def get_channel_info(self): """Get the current channel info.""" self.request(EP_GET_CHANNEL_INFO) - return self.last_response['payload'] + return {} if self.last_response is None else self.last_response.get('payload') def set_channel(self, channel): """Set the current channel.""" diff --git a/setup.py b/setup.py index a3d2967..504feef 100644 --- a/setup.py +++ b/setup.py @@ -7,12 +7,12 @@ package_data = {'pylgtv': ['handshake.json']}, install_requires = ['websockets', 'asyncio'], zip_safe = True, - version = '0.1.5', + version = '0.1.6', description = 'Library to control webOS based LG Tv devices', author = 'Dennis Karpienski', author_email = 'dennis@karpienski.de', url = 'https://github.com/TheRealLink/pylgtv', - download_url = 'https://github.com/TheRealLink/pylgtv/archive/0.1.5.tar.gz', + download_url = 'https://github.com/TheRealLink/pylgtv/archive/0.1.6.tar.gz', keywords = ['webos', 'tv'], classifiers = [], ) \ No newline at end of file