Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
vrachieru committed Oct 20, 2018
0 parents commit 7711bf2
Show file tree
Hide file tree
Showing 7 changed files with 239 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 Victor Rachieru

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<p align="center">
<img src="https://user-images.githubusercontent.com/5860071/47255992-611d9b00-d481-11e8-965d-d9816f254be2.png" width="300px" border="0" />
<br/>
<a href="https://github.com/vrachieru/samsung-tv-api/releases/latest">
<img src="https://img.shields.io/badge/version-1.0.0-brightgreen.svg?style=flat-square" alt="Version">
</a>
<a href="https://travis-ci.org/vrachieru/samsung-tv-api">
<img src="https://img.shields.io/travis/vrachieru/samsung-tv-api.svg?style=flat-square" alt="Version">
</a>
<br/>
Samsung Smart TV API wrapper
</p>

This project is a library for remote controlling Samsung televisions via a TCP/IP connection.
It currently supports modern (post-2016) TVs with Ethernet or Wi-Fi connectivity.

## Install

```bash
$ pip3 install git+https://github.com/vrachieru/samsung-tv-api.git
```
or
```bash
$ git clone https://github.com/vrachieru/samsung-tv-api.git
$ pip3 install ./samsung-tv-api
```

## Usage

```python
from samsungtv import SamsungTV

tv = SamsungTV('192.168.xxx.xxx')
tv.power() # toggle power
```

## License

MIT
7 changes: 7 additions & 0 deletions example/example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import sys
sys.path.append('../')

from samsungtv import SamsungTV

tv = SamsungTV('192.168.xxx.xxx')
tv.power()
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
websocket-client
1 change: 1 addition & 0 deletions samsungtv/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .remote import SamsungTV
138 changes: 138 additions & 0 deletions samsungtv/remote.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import base64
import json
import logging
import time
import websocket

class SamsungTV():

_URL_FORMAT = 'ws://{host}:{port}/api/v2/channels/samsung.remote.control?name={name}'

_KEY_INTERVAL = 1.5

def __init__(self, host, port=8001, name='SamsungTvRemote'):
self.connection = websocket.create_connection(
self._URL_FORMAT.format(**{
'host': host,
'port': port,
'name': self._serialize_string(name)
})
)

response = json.loads(self.connection.recv())
if response['event'] != 'ms.channel.connect':
self.close()
raise Exception(response)

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

def _serialize_string(self, string):
if isinstance(string, str):
string = str.encode(string)
return base64.b64encode(string).decode('utf-8')

def close(self):
if self.connection:
self.connection.close()
self.connection = None
logging.debug('Connection closed.')

def send_key(self, key, repeat=1):
for n in range(repeat):
payload = json.dumps({
'method': 'ms.remote.control',
'params': {
'Cmd': 'Click',
'DataOfCmd': key,
'Option': 'false',
'TypeOfRemote': 'SendRemoteKey'
}
})

logging.info('Sending key %s', key)
self.connection.send(payload)
time.sleep(self._KEY_INTERVAL)

# power
def power(self):
self.send_key('KEY_POWER')

# menu
def home(self):
self.send_key('KEY_HOME')

def menu(self):
self.send_key('KEY_MENU')

def source(self):
self.send_key('KEY_SOURCE')

def guide(self):
self.send_key('KEY_GUIDE')

def tools(self):
self.send_key('KEY_TOOLS')

def info(self):
self.send_key('KEY_INFO')

# navigation
def up(self, count=1):
self.send_key('KEY_UP', count)

def down(self, count=1):
self.send_key('KEY_DOWN', count)

def left(self, count=1):
self.send_key('KEY_LEFT', count)

def right(self, count=1):
self.send_key('KEY_RIGHT', count)

def enter(self, count=1):
self.send_key('KEY_ENTER', count)

def back(self, count=1):
self.send_key('KEY_RETURN', count)

# channel
def channel_list(self):
self.send_key('KEY_CH_LIST')

def channel(self, ch):
for c in str(ch):
self.digit(c)
self.enter()

def digit(self, d):
self.send_key('KEY_' + d)

def channel_up(self, count=1):
self.send_key('KEY_CHUP', count)

def channel_down(self, count=1):
self.send_key('KEY_CHDOWN', count)

# volume
def volume_up(self, count=1):
self.send_key('KEY_VOLUP', count)

def volume_down(self, count=1):
self.send_key('KEY_VOLDOWN', count)

def mute(self):
self.send_key('KEY_MUTE')

# extra
def red(self):
self.send_key('KEY_RED')

def green(self):
self.send_key('KEY_GREEN')

def yellow(self):
self.send_key('KEY_YELLOW')

def blue(self):
self.send_key('KEY_BLUE')
32 changes: 32 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from setuptools import find_packages, setup, Command

def readme():
with open('README.md') as readme_file:
return readme_file.read()

setup(
name='samsungtv',
version='1.0.0',
description='Samsung Smart TV API wrapper',
long_description=readme(),
long_description_content_type='text/markdown',
author='Victor Rachieru',
python_requires='>=3.0.0',
url='https://github.com/vrachieru/samsung-tv-api',
packages=find_packages(exclude=('tests',)),
install_requires=['websocket-client'],
include_package_data=True,
license='MIT',
classifiers=[
'Programming Language :: Python',
'Programming Language :: Python :: 3',
'Development Status :: 5 - Production/Stable',
'Intended Audience :: Developers',
'Intended Audience :: Education',
'Intended Audience :: Information Technology',
'Intended Audience :: Science/Research',
'License :: OSI Approved :: MIT License'
]
)

0 comments on commit 7711bf2

Please sign in to comment.