Skip to content
This repository has been archived by the owner on Dec 2, 2019. It is now read-only.

Commit

Permalink
Merge pull request #6 from hmn/master
Browse files Browse the repository at this point in the history
fixed asyncio thread leak
  • Loading branch information
TheRealLink authored Apr 20, 2017
2 parents 4a9da21 + 2992bbf commit f07085a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
39 changes: 22 additions & 17 deletions pylgtv/webos_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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)
Expand All @@ -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):
Expand All @@ -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."""
Expand All @@ -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."""
Expand All @@ -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."""
Expand All @@ -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."""
Expand All @@ -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."""
Expand All @@ -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."""
Expand Down Expand Up @@ -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."""
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [],
)

0 comments on commit f07085a

Please sign in to comment.