46
46
47
47
_LOGGER = logging .getLogger (__name__ )
48
48
49
- MEDIA_TYPE_KEY = ' send_key'
49
+ MEDIA_TYPE_KEY = " send_key"
50
50
DEFAULT_NAME = "Samsung TV Remote"
51
51
DEFAULT_PORT = 55000
52
- DEFAULT_TIMEOUT = 10
52
+ DEFAULT_TIMEOUT = 2
53
53
KEY_PRESS_TIMEOUT = 1.2
54
54
KNOWN_DEVICES_KEY = "samsungtv_known_devices"
55
55
SOURCES = {"TV" : "KEY_TV" , "HDMI" : "KEY_HDMI" }
@@ -135,16 +135,14 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
135
135
# config.
136
136
ip_addr = socket .gethostbyname (host )
137
137
if ip_addr not in known_devices :
138
- known_devices .add (ip_addr )
138
+ # known_devices.add(ip_addr)
139
139
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" :
145
141
add_entities ([SamsungTVDeviceQLED (host , port , name , timeout , mac , uuid , sourcelist , applist )])
146
142
elif protocol == "ws" :
147
143
add_entities ([SamsungTVDeviceWS (host , port , name , timeout , mac , uuid , sourcelist )])
144
+ else :
145
+ add_entities ([SamsungTVDevice (host , port , name , timeout , mac , uuid , sourcelist , protocol )])
148
146
149
147
_LOGGER .info ("Samsung TV %s:%d added as '%s'" , host , port , name )
150
148
else :
@@ -358,11 +356,7 @@ def turn_on(self):
358
356
359
357
async def async_select_source (self , source ):
360
358
"""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 ])
366
360
367
361
368
362
class SamsungTVDeviceQLED (MediaPlayerDevice ):
@@ -529,7 +523,7 @@ def volume_level(self):
529
523
@property
530
524
def source (self ):
531
525
"""Name of the current input source."""
532
- if self ._config ['port' ] == 8002 :
526
+ if self ._config ['port' ] in ( 8001 , 8002 ) :
533
527
self ._application = self .get_application ()
534
528
if self ._application .current_app () is None :
535
529
self ._current_source = 'TV/HDMI'
@@ -543,13 +537,11 @@ def source(self):
543
537
@property
544
538
def source_list (self ):
545
539
"""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
553
545
554
546
@property
555
547
def supported_features (self ):
@@ -685,7 +677,7 @@ async def async_select_source(self, source):
685
677
686
678
def select_source (self , source ):
687
679
"""Select input source."""
688
- if self . _config [ 'port' ] == 8002 :
680
+ if source not in self . _sourcelist :
689
681
if source == 'TV/HDMI' :
690
682
self .get_application ().stop (self ._current_source )
691
683
else :
@@ -697,15 +689,16 @@ def async_select_source(self, source):
697
689
"""Select input source.
698
690
This method must be run in the event loop and returns a coroutine.
699
691
"""
700
- return self .hass .async_add_job (self .select_source , source )
692
+ return self .hass .async_add_job (self .select_source , source )
693
+
701
694
702
695
class SamsungTVDeviceWS (MediaPlayerDevice ):
703
696
"""Representation of a Samsung TV."""
704
697
705
698
def __init__ (self , host , port , name , timeout , mac , uuid , sourcelist ):
706
699
"""Initialize the Samsung device."""
707
700
# Load WS implementation from samsungtvws folder
708
- from custom_components . samsungtv_custom .samsungtvws .remote import SamsungTVWS
701
+ from .samsungtvws .remote import SamsungTVWS
709
702
710
703
# Save a reference to the imported classes
711
704
self ._name = name
@@ -872,7 +865,7 @@ async def async_play_media(self, media_type, media_id, **kwargs):
872
865
await self .hass .async_add_job (self .send_key , "KEY_" + digit )
873
866
await asyncio .sleep (KEY_PRESS_TIMEOUT , self .hass .loop )
874
867
await self .hass .async_add_job (self .send_key , "KEY_ENTER" )
875
- elif media_type == "send_key" :
868
+ elif media_type == MEDIA_TYPE_KEY :
876
869
self .send_key (media_id )
877
870
else :
878
871
_LOGGER .error ("Unsupported media type" )
@@ -887,8 +880,4 @@ def turn_on(self):
887
880
888
881
async def async_select_source (self , source ):
889
882
"""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