-
Notifications
You must be signed in to change notification settings - Fork 177
/
media_player.py
932 lines (794 loc) · 29.2 KB
/
media_player.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
"""Support for interface with an Samsung TV."""
import asyncio
from datetime import timedelta
import logging
import socket
import json
import voluptuous as vol
import os
import websocket
import wakeonlan
import time
import requests
from homeassistant import util
from homeassistant.components.media_player import (
MediaPlayerEntity,
PLATFORM_SCHEMA,
DEVICE_CLASS_TV,
)
from homeassistant.components.media_player.const import (
MEDIA_TYPE_CHANNEL,
SUPPORT_NEXT_TRACK,
SUPPORT_PAUSE,
SUPPORT_PLAY,
SUPPORT_PLAY_MEDIA,
SUPPORT_PREVIOUS_TRACK,
SUPPORT_SELECT_SOURCE,
SUPPORT_TURN_OFF,
SUPPORT_TURN_ON,
SUPPORT_VOLUME_MUTE,
SUPPORT_VOLUME_STEP,
SUPPORT_VOLUME_SET,
MEDIA_TYPE_URL,
MEDIA_TYPE_VIDEO,
MEDIA_TYPE_PLAYLIST,
MEDIA_TYPE_MUSIC,
MEDIA_TYPE_APP
)
from homeassistant.const import (
CONF_HOST,
CONF_MAC,
CONF_NAME,
CONF_PORT,
CONF_TIMEOUT,
CONF_ID,
CONF_TOKEN,
STATE_OFF,
STATE_ON
)
import homeassistant.helpers.config_validation as cv
from homeassistant.util import dt as dt_util
_LOGGER = logging.getLogger(__name__)
MEDIA_TYPE_KEY = "send_key"
DEFAULT_NAME = "Samsung TV Remote"
DEFAULT_PORT = 55000
DEFAULT_TIMEOUT = 1.5
KEY_PRESS_TIMEOUT = 0.3
KNOWN_DEVICES_KEY = "samsungtv_known_devices"
SOURCES = {"TV": "KEY_TV", "HDMI": "KEY_HDMI"}
CONF_SOURCELIST = "sourcelist"
CONF_APPLIST = "applist"
CONF_PROTOCOL = "protocol"
CONF_ID = "id"
CONF_TOKEN = "token"
MIN_TIME_BETWEEN_FORCED_SCANS = timedelta(seconds=1)
MIN_TIME_BETWEEN_SCANS = timedelta(seconds=10)
SUPPORT_SAMSUNGTV = (
SUPPORT_PAUSE
| SUPPORT_VOLUME_STEP
| SUPPORT_VOLUME_MUTE
| SUPPORT_PREVIOUS_TRACK
| SUPPORT_SELECT_SOURCE
| SUPPORT_NEXT_TRACK
| SUPPORT_TURN_OFF
| SUPPORT_PLAY
| SUPPORT_PLAY_MEDIA
)
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Required(CONF_HOST): cv.string,
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
vol.Optional(CONF_MAC): cv.string,
vol.Optional(CONF_TIMEOUT, default=DEFAULT_TIMEOUT): cv.positive_int,
vol.Optional(CONF_SOURCELIST): cv.string,
vol.Optional(CONF_APPLIST): cv.string,
vol.Optional(CONF_PROTOCOL): cv.string,
vol.Optional(CONF_ID): cv.string,
vol.Optional(CONF_TOKEN): cv.string,
}
)
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the Samsung TV platform."""
known_devices = hass.data.get(KNOWN_DEVICES_KEY)
if known_devices is None:
known_devices = set()
hass.data[KNOWN_DEVICES_KEY] = known_devices
uuid = None
if config.get(CONF_SOURCELIST) is not None:
sourcelist = json.loads(config.get(CONF_SOURCELIST))
else:
sourcelist = SOURCES
if config.get(CONF_PROTOCOL) is not None:
protocol = config.get(CONF_PROTOCOL)
else:
protocol = "ctl"
if config.get(CONF_APPLIST) is not None:
applist = config.get(CONF_APPLIST).split(", ")
else:
applist = []
# Is this a manual configuration?
if config.get(CONF_HOST) is not None:
host = config.get(CONF_HOST)
port = config.get(CONF_PORT)
name = config.get(CONF_NAME)
mac = config.get(CONF_MAC)
timeout = config.get(CONF_TIMEOUT)
uuid = config.get(CONF_ID)
token = config.get(CONF_TOKEN)
elif discovery_info is not None:
tv_name = discovery_info.get("name")
model = discovery_info.get("model_name")
host = discovery_info.get("host")
name = f"{tv_name} ({model})"
port = DEFAULT_PORT
timeout = DEFAULT_TIMEOUT
mac = None
udn = discovery_info.get("udn")
if udn and udn.startswith("uuid:"):
uuid = udn[len("uuid:") :]
else:
_LOGGER.warning("Cannot determine device")
return
# Only add a device once, so discovered devices do not override manual
# config.
ip_addr = socket.gethostbyname(host)
if ip_addr not in known_devices:
#known_devices.add(ip_addr)
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, token)])
_LOGGER.info("Samsung TV %s:%d added as '%s'", host, port, name)
else:
_LOGGER.info("Ignoring duplicate Samsung TV %s:%d", host, port)
class SamsungTVDevice(MediaPlayerEntity):
"""Representation of a Samsung TV."""
def __init__(self, host, port, name, timeout, mac, uuid, sourcelist, protocol, token):
"""Initialize the Samsung device."""
if protocol == "ctl_beta":
from .samsungctl_080b import exceptions
from .samsungctl_080b import Remote
else:
from samsungctl import exceptions
from samsungctl import Remote
# Save a reference to the imported classes
self._exceptions_class = exceptions
self._remote_class = Remote
self._name = name
self._mac = mac
self._uuid = uuid
self._token = token
self._wol = wakeonlan
# Assume that the TV is not muted
self._muted = False
# Assume that the TV is in Play mode
self._playing = True
self._state = None
self._remote = None
# Mark the end of a shutdown command (need to wait 15 seconds before
# sending the next command to avoid turning the TV back ON).
self._end_of_power_off = None
# Generate a configuration for the Samsung library
self._config = {
"name": "HomeAssistant",
"description": name,
"id": "ha.component.samsung",
"port": port,
"host": host,
"timeout": timeout,
"token": token,
}
self._sourcelist = sourcelist
if self._token != "":
self._config["method"] = "encrypted"
elif self._config["port"] in (8001, 8002):
self._config["method"] = "websocket"
else:
self._config["method"] = "legacy"
def update(self):
"""Update state of device."""
if self._config["method"] == "encrypted" and self._config["port"] == 8001:
try:
r = requests.get(
"http://{}:8001/api/v2/".format(self._config["host"]), timeout=2
)
self._state = STATE_ON
_LOGGER.debug("State is on %s", repr(r))
# break
except:
_LOGGER.debug("State is off")
self._state = STATE_OFF
else:
self.send_key("KEY")
def get_remote(self):
"""Create or return a remote control instance."""
if self._remote is None:
# We need to create a new instance to reconnect.
self._remote = self._remote_class(self._config)
return self._remote
def send_key(self, key):
"""Send a key to the tv and handles exceptions."""
_LOGGER.debug("Send key %s", key)
if self._power_off_in_progress() and key not in ("KEY_POWER", "KEY_POWEROFF"):
_LOGGER.info("TV is powering off, not sending command: %s", key)
return
try:
# recreate connection if connection was dead
retry_count = 1
for _ in range(retry_count + 1):
try:
control_result = self.get_remote().control(key)
if not control_result:
_LOGGER.debug("Failed sending command %s. Retry!", key)
self._remote.close()
self._remote = None
self.get_remote().control(key)
break
except (self._exceptions_class.ConnectionClosed, BrokenPipeError):
# BrokenPipe can occur when the commands is sent to fast
self._remote = None
self._state = STATE_ON
except (
self._exceptions_class.UnhandledResponse,
self._exceptions_class.AccessDenied,
):
# We got a response so it's on.
self._state = STATE_ON
self._remote = None
_LOGGER.debug("Failed sending command %s", key, exc_info=True)
return
except OSError:
self._state = STATE_OFF
self._remote = None
if self._power_off_in_progress():
self._state = STATE_OFF
def _power_off_in_progress(self):
return (
self._end_of_power_off is not None
and self._end_of_power_off > dt_util.utcnow()
)
@property
def unique_id(self) -> str:
"""Return the unique ID of the device."""
return self._uuid
@property
def name(self):
"""Return the name of the device."""
return self._name
@property
def state(self):
"""Return the state of the device."""
return self._state
@property
def is_volume_muted(self):
"""Boolean if volume is currently muted."""
return self._muted
@property
def source_list(self):
"""List of available input sources."""
return list(self._sourcelist)
@property
def supported_features(self):
"""Flag media player features that are supported."""
if self._mac:
return SUPPORT_SAMSUNGTV | SUPPORT_TURN_ON
return SUPPORT_SAMSUNGTV
@property
def device_class(self):
"""Set the device class to TV."""
return DEVICE_CLASS_TV
def turn_off(self):
"""Turn off media player."""
self._end_of_power_off = dt_util.utcnow() + timedelta(seconds=15)
if self._config["method"] == "websocket":
self.send_key("KEY_POWER")
else:
self.send_key("KEY_POWEROFF")
# Force closing of remote session to provide instant UI feedback
try:
self._remote = None
except OSError:
_LOGGER.debug("Could not establish connection.")
def volume_up(self):
"""Volume up the media player."""
self.send_key("KEY_VOLUP")
def volume_down(self):
"""Volume down media player."""
self.send_key("KEY_VOLDOWN")
def mute_volume(self, mute):
"""Send mute command."""
self.send_key("KEY_MUTE")
def media_play_pause(self):
"""Simulate play pause media player."""
if self._playing:
self.media_pause()
else:
self.media_play()
def media_play(self):
"""Send play command."""
self._playing = True
self.send_key("KEY_PLAY")
def media_pause(self):
"""Send media pause command to media player."""
self._playing = False
self.send_key("KEY_PAUSE")
def media_next_track(self):
"""Send next track command."""
self.send_key("KEY_FF")
def media_previous_track(self):
"""Send the previous track command."""
self.send_key("KEY_REWIND")
async def async_play_media(self, media_type, media_id, **kwargs):
"""Support changing a channel."""
if media_type == MEDIA_TYPE_CHANNEL:
# media_id should only be a channel number
try:
cv.positive_int(media_id)
except vol.Invalid:
_LOGGER.error("Media ID must be positive integer")
return
for digit in media_id:
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 == MEDIA_TYPE_KEY:
self.send_key(media_id)
else:
_LOGGER.error("Unsupported media type")
return
def turn_on(self):
"""Turn the media player on."""
if self._mac:
wakeonlan.send_magic_packet(self._mac)
else:
self.send_key("KEY_POWERON")
async def async_select_source(self, source):
"""Select input source."""
await self.hass.async_add_job(self.send_key, self._sourcelist[source])
class SamsungTVDeviceQLED(MediaPlayerEntity):
"""Representation of a Samsung TV."""
def __init__(self, host, port, name, timeout, mac, uuid, sourcelist, applist):
"""Initialize the Samsung device."""
from .samsungctl_qled import exceptions
from .samsungctl_qled import Remote
from .samsungctl_qled import Application
from .samsungctl_qled import Upnp
# Save a reference to the imported classes
self._exceptions_class = exceptions
self._remote_class = Remote
self._application_class = Application
self._upnp_class = Upnp
self._name = name
self._mac = mac
self._uuid = uuid
self._wol = wakeonlan
self._sourcelist = sourcelist
self._applist = applist
# Assume that the TV is not muted
self._muted = False
self._volume = 0
# Assume that the TV is in Play mode
self._playing = True
self._state = None
self._remote = None
self._upnp = None
self._application = None
self._current_source = None
# Mark the end of a shutdown command (need to wait 15 seconds before
# sending the next command to avoid turning the TV back ON).
self._end_of_power_off = None
# Generate a configuration for the Samsung library
self._config = {
"name": "HomeAssistant",
"description": name,
"id": "ha.component.samsung",
"port": port,
"host": host,
"timeout": timeout,
}
if self._config["port"] in (8001, 8002):
self._config["method"] = "websocket"
else:
self._config["method"] = "legacy"
def update(self):
"""Update state of device."""
if self._config["port"] == 8002:
# retry_count = 1
# for _ in range(retry_count + 1):
try:
r = requests.get("http://{}:9197/dmr".format(self._config['host']), timeout=0.2)
self._state = STATE_ON
# break
except:
self._state = STATE_OFF
else:
self.send_key("KEY")
def get_remote(self):
"""Create or return a remote control instance."""
if self._remote is None:
# We need to create a new instance to reconnect.
self._remote = self._remote_class(self._config)
return self._remote
def get_upnp(self):
"""Create or return a upnp device."""
if self._upnp is None:
# We need to create a new instance to reconnect.
self._upnp = self._upnp_class(self._config)
return self._upnp
def get_application(self):
"""Create or return an application management object."""
if self._application is None:
self._application = self._application_class(self._config)
return self._application
def send_key(self, key):
"""Send a key to the tv and handles exceptions."""
if self._power_off_in_progress() and key not in ("KEY_POWER", "KEY_POWEROFF"):
_LOGGER.info("TV is powering off, not sending command: %s", key)
return
try:
# recreate connection if connection was dead
retry_count = 1
for _ in range(retry_count + 1):
try:
self.get_remote().control(key)
break
except (self._exceptions_class.ConnectionClosed, BrokenPipeError):
# BrokenPipe can occur when the commands is sent to fast
self._remote = None
self._state = STATE_ON
except (
self._exceptions_class.UnhandledResponse,
self._exceptions_class.AccessDenied,
):
# We got a response so it's on.
self._state = STATE_ON
self._remote = None
_LOGGER.debug("Failed sending command %s", key, exc_info=True)
return
except OSError:
self._state = STATE_OFF
self._remote = None
if self._power_off_in_progress():
self._state = STATE_OFF
def launch_app(self, application):
"""Launch an application."""
self.get_application().start(application)
def _power_off_in_progress(self):
return (
self._end_of_power_off is not None
and self._end_of_power_off > dt_util.utcnow()
)
@property
def unique_id(self) -> str:
"""Return the unique ID of the device."""
return self._uuid
@property
def name(self):
"""Return the name of the device."""
return self._name
@property
def state(self):
"""Return the state of the device."""
return self._state
@property
def is_volume_muted(self):
"""Boolean if volume is currently muted."""
if self._config["port"] == 8002:
self._upnp = self.get_upnp()
self._muted = self._upnp.get_mute()
return self._muted
@property
def volume_level(self):
"""Volume level of the media player (0..1)."""
if self._config["port"] == 8002:
self._upnp = self.get_upnp()
self._volume = int(self._upnp.get_volume()) / 100
return str(self._volume)
@property
def source(self):
"""Name of the current input source."""
if self._config['port'] in (8001,8002):
self._application = self.get_application()
if self._application.current_app() is None:
self._current_source = 'TV/HDMI'
return self._current_source
else:
self._current_source = self._application.current_app()
return self._current_source
else:
return self._current_source
@property
def source_list(self):
"""List of available input sources."""
source_list = ['TV/HDMI']
source_list.extend(list(self._sourcelist))
source_list.extend(list(self._applist))
return source_list
@property
def supported_features(self):
"""Flag media player features that are supported."""
if self._mac:
return SUPPORT_SAMSUNGTV | SUPPORT_TURN_ON
return SUPPORT_SAMSUNGTV
@property
def device_class(self):
"""Set the device class to TV."""
return DEVICE_CLASS_TV
def turn_off(self):
"""Turn off media player."""
self._end_of_power_off = dt_util.utcnow() + timedelta(seconds=15)
if self._config["method"] == "websocket":
self.send_key("KEY_POWER")
else:
self.send_key("KEY_POWEROFF")
# Force closing of remote session to provide instant UI feedback
try:
self.get_remote().close()
self._remote = None
except OSError:
_LOGGER.debug("Could not establish connection.")
def volume_up(self):
"""Volume up the media player."""
self.send_key("KEY_VOLUP")
def volume_down(self):
"""Volume down media player."""
self.send_key("KEY_VOLDOWN")
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."""
if self._config['port'] == 8002:
self._upnp = self.get_upnp()
self._upnp.set_volume(int(volume*100))
def media_play_pause(self):
"""Simulate play pause media player."""
if self._playing:
self.media_pause()
else:
self.media_play()
def media_play(self):
"""Send play command."""
self._playing = True
self.send_key("KEY_PLAY")
def media_pause(self):
"""Send media pause command to media player."""
self._playing = False
self.send_key("KEY_PAUSE")
def media_next_track(self):
"""Send next track command."""
self.send_key("KEY_FF")
def media_previous_track(self):
"""Send the previous track command."""
self.send_key("KEY_REWIND")
async def async_play_media(self, media_type, media_id, **kwargs):
# Type channel
if media_type == MEDIA_TYPE_CHANNEL:
# media_id should only be a channel number
try:
cv.positive_int(media_id)
except vol.Invalid:
_LOGGER.error('Media ID must be positive integer')
return
for digit in media_id:
await self.hass.async_add_job(self.send_key, 'KEY_' + digit)
await asyncio.sleep(KEY_PRESS_TIMEOUT, self.hass.loop)
# Launch an app
elif media_type == MEDIA_TYPE_APP:
try:
cv.string(media_id)
except vol.Invalid:
_LOGGER.error('Media ID must be a string (ex: "Youtube"')
return
await self.hass.async_add_job(self.launch_app, media_id)
# Send custom key
elif media_type == MEDIA_TYPE_KEY:
try:
cv.string(media_id)
except vol.Invalid:
_LOGGER.error('Media ID must be a string (ex: "KEY_HOME"')
return
await self.hass.async_add_job(self.send_key, media_id)
# Launch stream
elif (media_type == MEDIA_TYPE_URL or media_type == MEDIA_TYPE_MUSIC):
_LOGGER.error("Playing on TV " + str(media_id))
self._upnp = self.get_upnp()
self._upnp.set_current_media(media_id)
self._upnp.play()
# Launch stream
elif (media_type == "application/vnd.apple.mpegurl"):
_LOGGER.error("Playing Stream on TV " + str(media_id))
self._upnp = self.get_upnp()
self._upnp.set_current_media(str(media_id))
self._upnp.play()
else:
_LOGGER.error('Unsupported media type: {type} - {id}'.format(type=media_type, id=media_id))
def turn_on(self):
"""Turn the media player on."""
if self._mac:
self._wol.send_magic_packet(self._mac)
else:
self.send_key("KEY_POWERON")
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])
def select_source(self, source):
"""Select input source."""
if source not in self._sourcelist:
if source == 'TV/HDMI':
self.get_application().stop(self._current_source)
else:
self.launch_app(source)
else:
self.send_key(self._sourcelist[source])
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)
class SamsungTVDeviceWS(MediaPlayerEntity):
"""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 .samsungtvws.remote import SamsungTVWS
# Save a reference to the imported classes
self._name = name
self._mac = mac
self._uuid = uuid
# Assume that the TV is not muted
self._muted = False
# Assume that the TV is in Play mode
self._playing = True
self._state = None
# Mark the end of a shutdown command (need to wait 15 seconds before
# sending the next command to avoid turning the TV back ON).
self._end_of_power_off = None
self._sourcelist = sourcelist
token_file = os.path.dirname(os.path.realpath(__file__)) + '/tv-token.txt'
self._remote = SamsungTVWS(
name=name,
host=host,
port=port,
timeout=timeout,
key_press_delay=KEY_PRESS_TIMEOUT,
token_file=token_file
)
@util.Throttle(MIN_TIME_BETWEEN_SCANS, MIN_TIME_BETWEEN_FORCED_SCANS)
def update(self):
"""Update state of device."""
self.send_key("KEY", 1)
def send_key(self, key, retry_count = 1):
"""Send a key to the tv and handles exceptions."""
if self._power_off_in_progress() and key not in ("KEY_POWER", "KEY_POWEROFF"):
_LOGGER.info("TV is powering off, not sending command: %s", key)
return
try:
# recreate connection if connection was dead
for _ in range(retry_count + 1):
try:
self._remote.send_key(key)
break
except (
ConnectionResetError,
AttributeError,
BrokenPipeError
):
self._remote.close()
self._state = STATE_ON
except websocket._exceptions.WebSocketTimeoutException:
# We got a response so it's on.
self._state = STATE_ON
self._remote.close()
_LOGGER.debug("Failed sending command %s", key, exc_info=True)
except OSError:
self._state = STATE_OFF
self._remote.close()
if self._power_off_in_progress():
self._state = STATE_OFF
def _power_off_in_progress(self):
return (
self._end_of_power_off is not None
and self._end_of_power_off > dt_util.utcnow()
)
@property
def unique_id(self) -> str:
"""Return the unique ID of the device."""
return self._uuid
@property
def name(self):
"""Return the name of the device."""
return self._name
@property
def state(self):
"""Return the state of the device."""
return self._state
@property
def is_volume_muted(self):
"""Boolean if volume is currently muted."""
return self._muted
@property
def source_list(self):
"""List of available input sources."""
return list(self._sourcelist)
@property
def supported_features(self):
"""Flag media player features that are supported."""
if self._mac:
return SUPPORT_SAMSUNGTV | SUPPORT_TURN_ON
return SUPPORT_SAMSUNGTV
@property
def device_class(self):
"""Set the device class to TV."""
return DEVICE_CLASS_TV
def turn_off(self):
"""Turn off media player."""
self._end_of_power_off = dt_util.utcnow() + timedelta(seconds=15)
self.send_key("KEY_POWER")
# Force closing of remote session to provide instant UI feedback
try:
self._remote.close()
except OSError:
_LOGGER.debug("Could not establish connection.")
def volume_up(self):
"""Volume up the media player."""
self.send_key("KEY_VOLUP")
def volume_down(self):
"""Volume down media player."""
self.send_key("KEY_VOLDOWN")
def mute_volume(self, mute):
"""Send mute command."""
self.send_key("KEY_MUTE")
def media_play_pause(self):
"""Simulate play pause media player."""
if self._playing:
self.media_pause()
else:
self.media_play()
def media_play(self):
"""Send play command."""
self._playing = True
self.send_key("KEY_PLAY")
def media_pause(self):
"""Send media pause command to media player."""
self._playing = False
self.send_key("KEY_PAUSE")
def media_next_track(self):
"""Send next track command."""
self.send_key("KEY_FF")
def media_previous_track(self):
"""Send the previous track command."""
self.send_key("KEY_REWIND")
async def async_play_media(self, media_type, media_id, **kwargs):
"""Support changing a channel."""
if media_type == MEDIA_TYPE_CHANNEL:
# media_id should only be a channel number
try:
cv.positive_int(media_id)
except vol.Invalid:
_LOGGER.error("Media ID must be positive integer")
return
for digit in media_id:
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 == MEDIA_TYPE_KEY:
self.send_key(media_id)
else:
_LOGGER.error("Unsupported media type")
return
def turn_on(self):
"""Turn the media player on."""
if self._mac:
wakeonlan.send_magic_packet(self._mac)
else:
self.send_key("KEY_POWERON")
async def async_select_source(self, source):
"""Select input source."""
await self.hass.async_add_job(self.send_key, self._sourcelist[source])