Skip to content

Commit

Permalink
fix #4 and bump a new version
Browse files Browse the repository at this point in the history
  • Loading branch information
xchwarze committed Feb 17, 2020
1 parent d19ae5b commit c56a975
Show file tree
Hide file tree
Showing 11 changed files with 26 additions and 13 deletions.
3 changes: 3 additions & 0 deletions build/lib/samsungtvws/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class ConnectionFailure(Exception):
"""Error during connection."""
pass
23 changes: 15 additions & 8 deletions build/lib/samsungtvws/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@
import time
import ssl
import websocket
from . import exceptions
from . import shortcuts

_LOGGING = logging.getLogger(__name__)

class SamsungTVWS:
_URL_FORMAT = 'ws://{host}:{port}/api/v2/channels/samsung.remote.control?name={name}'
Expand All @@ -43,6 +45,9 @@ def __init__(self, host, token=None, token_file=None, port=8001, timeout=None, k
self.name = name
self.connection = None

def __enter__(self):
return self

def __exit__(self, type, value, traceback):
self.close()

Expand Down Expand Up @@ -83,7 +88,8 @@ def _set_token(self, token):
with open(self.token_file, 'w') as token_file:
token_file.write(token)
else:
logging.info('New token %s', token)
_LOGGING.info('New token %s', token)
self.token = token

def _ws_send(self, payload):
if self.connection is None:
Expand All @@ -97,7 +103,7 @@ def open(self):
url = self._format_websocket_url(is_ssl)
sslopt = {'cert_reqs': ssl.CERT_NONE} if is_ssl else {}

logging.debug('WS url %s', url)
_LOGGING.debug('WS url %s', url)

self.connection = websocket.create_connection(
url,
Expand All @@ -109,18 +115,19 @@ def open(self):

if response.get('data') and response.get('data').get('token'):
token = response.get('data').get('token')
_LOGGING.debug('Got token %s', token)
self._set_token(token)

if response['event'] != 'ms.channel.connect':
self.close()
raise Exception(response)
raise exceptions.ConnectionFailure(response)

def close(self):
if self.connection:
self.connection.close()

self.connection = None
logging.debug('Connection closed.')
_LOGGING.debug('Connection closed.')

def send_key(self, key, repeat=1):
for _ in range(repeat):
Expand All @@ -134,7 +141,7 @@ def send_key(self, key, repeat=1):
}
})

logging.info('Sending key %s', key)
_LOGGING.info('Sending key %s', key)
self._ws_send(payload)

def run_app(self, app_id, app_type='DEEP_LINK', meta_tag=''):
Expand All @@ -153,11 +160,11 @@ def run_app(self, app_id, app_type='DEEP_LINK', meta_tag=''):
}
})

logging.info('Sending run app app_id: %s app_type: %s meta_tag: %s', app_id, app_type, meta_tag)
_LOGGING.info('Sending run app app_id: %s app_type: %s meta_tag: %s', app_id, app_type, meta_tag)
self._ws_send(payload)

def open_browser(self, url):
logging.info('Opening url in browser %s', url)
_LOGGING.info('Opening url in browser %s', url)
self.run_app(
'org.tizen.browser',
'NATIVE_LAUNCH',
Expand All @@ -173,7 +180,7 @@ def app_list(self):
}
})

logging.info('Get app list')
_LOGGING.info('Get app list')
self._ws_send(payload)
response = json.loads(self.connection.recv())
if response.get('data') and response.get('data').get('data'):
Expand Down
Binary file removed dist/samsungtvws-1.1.6-py3-none-any.whl
Binary file not shown.
Binary file removed dist/samsungtvws-1.1.6.tar.gz
Binary file not shown.
Binary file added dist/samsungtvws-1.2.0-py3-none-any.whl
Binary file not shown.
Binary file added dist/samsungtvws-1.2.0.tar.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
websocket-client
websocket-client==0.56.0
2 changes: 1 addition & 1 deletion samsungtvws.egg-info/PKG-INFO
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Metadata-Version: 2.1
Name: samsungtvws
Version: 1.1.4
Version: 1.2.0
Summary: Samsung Smart TV WS API wrapper
Home-page: https://github.com/xchwarze/samsung-tv-ws-api
Author: Xchwarze
Expand Down
1 change: 1 addition & 0 deletions samsungtvws.egg-info/SOURCES.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
README.md
setup.py
samsungtvws/__init__.py
samsungtvws/exceptions.py
samsungtvws/remote.py
samsungtvws/shortcuts.py
samsungtvws.egg-info/PKG-INFO
Expand Down
2 changes: 1 addition & 1 deletion samsungtvws.egg-info/requires.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
websocket-client
websocket-client==0.56.0
6 changes: 4 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@ def readme():

setup(
name='samsungtvws',
version='1.1.6',
version='1.2.0',
description='Samsung Smart TV WS API wrapper',
long_description=readme(),
long_description_content_type='text/markdown',
author='Xchwarze',
python_requires='>=3.0.0',
url='https://github.com/xchwarze/samsung-tv-ws-api',
packages=find_packages(exclude=('tests',)),
install_requires=['websocket-client'],
install_requires=[
'websocket-client==0.56.0'
],
include_package_data=True,
license='MIT',
classifiers=[
Expand Down

0 comments on commit c56a975

Please sign in to comment.