Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
roberodin committed Oct 18, 2019
1 parent 5383169 commit dbf79eb
Showing 1 changed file with 19 additions and 30 deletions.
49 changes: 19 additions & 30 deletions custom_components/samsungtv_custom/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@

_LOGGER = logging.getLogger(__name__)

MEDIA_TYPE_KEY = 'send_key'
MEDIA_TYPE_KEY = "send_key"
DEFAULT_NAME = "Samsung TV Remote"
DEFAULT_PORT = 55000
DEFAULT_TIMEOUT = 10
DEFAULT_TIMEOUT = 2
KEY_PRESS_TIMEOUT = 1.2
KNOWN_DEVICES_KEY = "samsungtv_known_devices"
SOURCES = {"TV": "KEY_TV", "HDMI": "KEY_HDMI"}
Expand Down Expand Up @@ -135,16 +135,14 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
# config.
ip_addr = socket.gethostbyname(host)
if ip_addr not in known_devices:
known_devices.add(ip_addr)
#known_devices.add(ip_addr)

if protocol == "ctl":
add_entities([SamsungTVDevice(host, port, name, timeout, mac, uuid, sourcelist, protocol)])
elif protocol == "ctl_beta":
add_entities([SamsungTVDevice(host, port, name, timeout, mac, uuid, sourcelist, protocol)])
elif protocol == "ctl_qled":
if protocol == "ctl_qled":
add_entities([SamsungTVDeviceQLED(host, port, name, timeout, mac, uuid, sourcelist, applist)])
elif protocol == "ws":
add_entities([SamsungTVDeviceWS(host, port, name, timeout, mac, uuid, sourcelist)])
else:
add_entities([SamsungTVDevice(host, port, name, timeout, mac, uuid, sourcelist, protocol)])

_LOGGER.info("Samsung TV %s:%d added as '%s'", host, port, name)
else:
Expand Down Expand Up @@ -358,11 +356,7 @@ def turn_on(self):

async def async_select_source(self, source):
"""Select input source."""
if source not in SOURCES:
_LOGGER.error("Unsupported source")
return

await self.hass.async_add_job(self.send_key, SOURCES[source])
await self.hass.async_add_job(self.send_key, self._sourcelist[source])


class SamsungTVDeviceQLED(MediaPlayerDevice):
Expand Down Expand Up @@ -529,7 +523,7 @@ def volume_level(self):
@property
def source(self):
"""Name of the current input source."""
if self._config['port'] == 8002:
if self._config['port'] in (8001,8002):
self._application = self.get_application()
if self._application.current_app() is None:
self._current_source = 'TV/HDMI'
Expand All @@ -543,13 +537,11 @@ def source(self):
@property
def source_list(self):
"""List of available input sources."""
if self._config['port'] == 8002:
source_list = ['TV/HDMI']
for app in self._applist:
source_list.append(app)
return source_list
else:
return self._sourcelist
source_list = ['TV/HDMI']
source_list.extend(list(self._sourcelist))
source_list.extend(list(self._applist))

return source_list

@property
def supported_features(self):
Expand Down Expand Up @@ -685,7 +677,7 @@ async def async_select_source(self, source):

def select_source(self, source):
"""Select input source."""
if self._config['port'] == 8002:
if source not in self._sourcelist:
if source == 'TV/HDMI':
self.get_application().stop(self._current_source)
else:
Expand All @@ -697,15 +689,16 @@ def async_select_source(self, source):
"""Select input source.
This method must be run in the event loop and returns a coroutine.
"""
return self.hass.async_add_job(self.select_source, source)
return self.hass.async_add_job(self.select_source, source)


class SamsungTVDeviceWS(MediaPlayerDevice):
"""Representation of a Samsung TV."""

def __init__(self, host, port, name, timeout, mac, uuid, sourcelist):
"""Initialize the Samsung device."""
# Load WS implementation from samsungtvws folder
from custom_components.samsungtv_custom.samsungtvws.remote import SamsungTVWS
from .samsungtvws.remote import SamsungTVWS

# Save a reference to the imported classes
self._name = name
Expand Down Expand Up @@ -872,7 +865,7 @@ async def async_play_media(self, media_type, media_id, **kwargs):
await self.hass.async_add_job(self.send_key, "KEY_" + digit)
await asyncio.sleep(KEY_PRESS_TIMEOUT, self.hass.loop)
await self.hass.async_add_job(self.send_key, "KEY_ENTER")
elif media_type == "send_key":
elif media_type == MEDIA_TYPE_KEY:
self.send_key(media_id)
else:
_LOGGER.error("Unsupported media type")
Expand All @@ -887,8 +880,4 @@ def turn_on(self):

async def async_select_source(self, source):
"""Select input source."""
if source not in SOURCES:
_LOGGER.error("Unsupported source")
return

await self.hass.async_add_job(self.send_key, SOURCES[source])
await self.hass.async_add_job(self.send_key, self._sourcelist[source])

0 comments on commit dbf79eb

Please sign in to comment.