Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get_mute, get_volume and set_volume #1

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion custom_components/samsungtv_custom/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
SUPPORT_TURN_ON,
SUPPORT_VOLUME_MUTE,
SUPPORT_VOLUME_STEP,
SUPPORT_VOLUME_SET,
)
from homeassistant.const import (
CONF_HOST,
Expand Down Expand Up @@ -55,6 +56,7 @@
SUPPORT_PAUSE
| SUPPORT_VOLUME_STEP
| SUPPORT_VOLUME_MUTE
| SUPPORT_VOLUME_SET
| SUPPORT_PREVIOUS_TRACK
| SUPPORT_SELECT_SOURCE
| SUPPORT_NEXT_TRACK
Expand Down Expand Up @@ -131,8 +133,9 @@ def __init__(self, host, port, name, timeout, mac, uuid, sourcelist):
self._name = name
self._mac = mac
self._uuid = uuid
# Assume that the TV is not muted
# Assume that the TV is not muted and volume is 0
self._muted = False
self._volume = 0
# Assume that the TV is in Play mode
self._playing = True
self._state = None
Expand Down Expand Up @@ -214,8 +217,15 @@ def state(self):
@property
def is_volume_muted(self):
"""Boolean if volume is currently muted."""
self._muted = self._remote.get_mute()
return self._muted

@property
def volume_level(self):
"""Volume level of the media player (0..1)."""
self._volume = int(self._remote.get_volume()) / 100
return str(self._volume)

@property
def source_list(self):
"""List of available input sources."""
Expand Down Expand Up @@ -251,6 +261,10 @@ def volume_down(self):
def mute_volume(self, mute):
"""Send mute command."""
self.send_key("KEY_MUTE")

def set_volume_level(self, volume):
"""Set volume level, range 0..1."""
self._remote.set_volume(int(volume*100))

def media_play_pause(self):
"""Simulate play pause media player."""
Expand Down
50 changes: 50 additions & 0 deletions custom_components/samsungtv_custom/samsungtvws/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import time
import ssl
import websocket
import xml.etree.ElementTree as ET
import requests


class SamsungTVWS():
Expand All @@ -40,6 +42,9 @@ def __init__(self, host, token=None, token_file=None, port=8002, timeout=None, k
self.key_press_delay = key_press_delay
self.name = name
self.connection = None

self.mute = False
self.volume = 0

def __exit__(self, type, value, traceback):
self.close()
Expand Down Expand Up @@ -260,3 +265,48 @@ def yellow(self):

def blue(self):
self.send_key('KEY_BLUE')

def SOAPrequest(self, action, arguments, protocole):
headers = {'SOAPAction': '"urn:schemas-upnp-org:service:{protocole}:1#{action}"'.format(action=action, protocole=protocole), 'content-type': 'text/xml'}
body = """<?xml version="1.0" encoding="utf-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<s:Body>
<u:{action} xmlns:u="urn:schemas-upnp-org:service:{protocole}:1">
<InstanceID>0</InstanceID>
{arguments}
</u:{action}>
</s:Body>
</s:Envelope>""".format(action=action, arguments=arguments, protocole=protocole)
response = None
try:
response = requests.post("http://{host}:9197/upnp/control/{protocole}1".format(host=self.host, protocole=protocole), data=body, headers=headers, timeout=0.1)
response = response.content
except:
pass
return response

def get_volume(self):
response = self.SOAPrequest('GetVolume', "<Channel>Master</Channel>", 'RenderingControl')
if response is not None:
volume_xml = response.decode('utf8')
tree = ET.fromstring(volume_xml)
for elem in tree.iter(tag='CurrentVolume'):
self.volume = elem.text
return self.volume


def set_volume(self, volume):
self.SOAPrequest('SetVolume', "<Channel>Master</Channel><DesiredVolume>{}</DesiredVolume>".format(volume), 'RenderingControl')

def get_mute(self):
response = self.SOAPrequest('GetMute', "<Channel>Master</Channel>", 'RenderingControl')
if response is not None:
# mute_xml = response.decode('utf8')
tree = ET.fromstring(response.decode('utf8'))
for elem in tree.iter(tag='CurrentMute'):
mute = elem.text
if (int(mute) == 0):
self.mute = False
else:
self.mute = True
return self.mute