Skip to content

Commit dbf79eb

Browse files
committed
fixes
1 parent 5383169 commit dbf79eb

File tree

1 file changed

+19
-30
lines changed

1 file changed

+19
-30
lines changed

custom_components/samsungtv_custom/media_player.py

+19-30
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@
4646

4747
_LOGGER = logging.getLogger(__name__)
4848

49-
MEDIA_TYPE_KEY = 'send_key'
49+
MEDIA_TYPE_KEY = "send_key"
5050
DEFAULT_NAME = "Samsung TV Remote"
5151
DEFAULT_PORT = 55000
52-
DEFAULT_TIMEOUT = 10
52+
DEFAULT_TIMEOUT = 2
5353
KEY_PRESS_TIMEOUT = 1.2
5454
KNOWN_DEVICES_KEY = "samsungtv_known_devices"
5555
SOURCES = {"TV": "KEY_TV", "HDMI": "KEY_HDMI"}
@@ -135,16 +135,14 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
135135
# config.
136136
ip_addr = socket.gethostbyname(host)
137137
if ip_addr not in known_devices:
138-
known_devices.add(ip_addr)
138+
#known_devices.add(ip_addr)
139139

140-
if protocol == "ctl":
141-
add_entities([SamsungTVDevice(host, port, name, timeout, mac, uuid, sourcelist, protocol)])
142-
elif protocol == "ctl_beta":
143-
add_entities([SamsungTVDevice(host, port, name, timeout, mac, uuid, sourcelist, protocol)])
144-
elif protocol == "ctl_qled":
140+
if protocol == "ctl_qled":
145141
add_entities([SamsungTVDeviceQLED(host, port, name, timeout, mac, uuid, sourcelist, applist)])
146142
elif protocol == "ws":
147143
add_entities([SamsungTVDeviceWS(host, port, name, timeout, mac, uuid, sourcelist)])
144+
else:
145+
add_entities([SamsungTVDevice(host, port, name, timeout, mac, uuid, sourcelist, protocol)])
148146

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

359357
async def async_select_source(self, source):
360358
"""Select input source."""
361-
if source not in SOURCES:
362-
_LOGGER.error("Unsupported source")
363-
return
364-
365-
await self.hass.async_add_job(self.send_key, SOURCES[source])
359+
await self.hass.async_add_job(self.send_key, self._sourcelist[source])
366360

367361

368362
class SamsungTVDeviceQLED(MediaPlayerDevice):
@@ -529,7 +523,7 @@ def volume_level(self):
529523
@property
530524
def source(self):
531525
"""Name of the current input source."""
532-
if self._config['port'] == 8002:
526+
if self._config['port'] in (8001,8002):
533527
self._application = self.get_application()
534528
if self._application.current_app() is None:
535529
self._current_source = 'TV/HDMI'
@@ -543,13 +537,11 @@ def source(self):
543537
@property
544538
def source_list(self):
545539
"""List of available input sources."""
546-
if self._config['port'] == 8002:
547-
source_list = ['TV/HDMI']
548-
for app in self._applist:
549-
source_list.append(app)
550-
return source_list
551-
else:
552-
return self._sourcelist
540+
source_list = ['TV/HDMI']
541+
source_list.extend(list(self._sourcelist))
542+
source_list.extend(list(self._applist))
543+
544+
return source_list
553545

554546
@property
555547
def supported_features(self):
@@ -685,7 +677,7 @@ async def async_select_source(self, source):
685677

686678
def select_source(self, source):
687679
"""Select input source."""
688-
if self._config['port'] == 8002:
680+
if source not in self._sourcelist:
689681
if source == 'TV/HDMI':
690682
self.get_application().stop(self._current_source)
691683
else:
@@ -697,15 +689,16 @@ def async_select_source(self, source):
697689
"""Select input source.
698690
This method must be run in the event loop and returns a coroutine.
699691
"""
700-
return self.hass.async_add_job(self.select_source, source)
692+
return self.hass.async_add_job(self.select_source, source)
693+
701694

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

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

710703
# Save a reference to the imported classes
711704
self._name = name
@@ -872,7 +865,7 @@ async def async_play_media(self, media_type, media_id, **kwargs):
872865
await self.hass.async_add_job(self.send_key, "KEY_" + digit)
873866
await asyncio.sleep(KEY_PRESS_TIMEOUT, self.hass.loop)
874867
await self.hass.async_add_job(self.send_key, "KEY_ENTER")
875-
elif media_type == "send_key":
868+
elif media_type == MEDIA_TYPE_KEY:
876869
self.send_key(media_id)
877870
else:
878871
_LOGGER.error("Unsupported media type")
@@ -887,8 +880,4 @@ def turn_on(self):
887880

888881
async def async_select_source(self, source):
889882
"""Select input source."""
890-
if source not in SOURCES:
891-
_LOGGER.error("Unsupported source")
892-
return
893-
894-
await self.hass.async_add_job(self.send_key, SOURCES[source])
883+
await self.hass.async_add_job(self.send_key, self._sourcelist[source])

0 commit comments

Comments
 (0)