2019-04-03 15:40:03 +00:00
|
|
|
"""Support for interface with an Samsung TV."""
|
2018-06-26 14:22:10 +00:00
|
|
|
import asyncio
|
2018-09-09 12:26:06 +00:00
|
|
|
from datetime import timedelta
|
2018-01-29 22:55:39 +00:00
|
|
|
|
2019-12-09 13:38:01 +00:00
|
|
|
from samsungctl import Remote as SamsungRemote, exceptions as samsung_exceptions
|
2016-09-05 17:22:26 +00:00
|
|
|
import voluptuous as vol
|
2019-11-25 09:45:50 +00:00
|
|
|
from websocket import WebSocketException
|
2016-09-05 17:22:26 +00:00
|
|
|
|
2020-01-10 02:19:10 +00:00
|
|
|
from homeassistant.components.media_player import DEVICE_CLASS_TV, MediaPlayerDevice
|
2019-02-08 22:18:18 +00:00
|
|
|
from homeassistant.components.media_player.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
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,
|
|
|
|
)
|
2016-01-31 09:06:39 +00:00
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_HOST,
|
2020-01-10 02:19:10 +00:00
|
|
|
CONF_ID,
|
|
|
|
CONF_METHOD,
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_PORT,
|
|
|
|
STATE_OFF,
|
|
|
|
STATE_ON,
|
|
|
|
)
|
2016-09-05 17:22:26 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2020-01-10 02:19:10 +00:00
|
|
|
from homeassistant.helpers.script import Script
|
2017-12-03 17:34:45 +00:00
|
|
|
from homeassistant.util import dt as dt_util
|
2016-01-31 21:17:00 +00:00
|
|
|
|
2020-01-10 02:19:10 +00:00
|
|
|
from .const import CONF_MANUFACTURER, CONF_MODEL, CONF_ON_ACTION, DOMAIN, LOGGER
|
2016-01-31 18:12:00 +00:00
|
|
|
|
2018-09-09 12:26:06 +00:00
|
|
|
KEY_PRESS_TIMEOUT = 1.2
|
2019-07-31 19:25:30 +00:00
|
|
|
SOURCES = {"TV": "KEY_TV", "HDMI": "KEY_HDMI"}
|
|
|
|
|
|
|
|
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
|
|
|
|
)
|
|
|
|
|
2016-09-05 17:22:26 +00:00
|
|
|
|
2020-01-10 02:19:10 +00:00
|
|
|
async def async_setup_platform(
|
|
|
|
hass, config, add_entities, discovery_info=None
|
|
|
|
): # pragma: no cover
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Set up the Samsung TV platform."""
|
2020-01-10 02:19:10 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
|
|
|
"""Set up the Samsung TV from a config entry."""
|
|
|
|
turn_on_action = config_entry.data.get(CONF_ON_ACTION)
|
|
|
|
on_script = Script(hass, turn_on_action) if turn_on_action else None
|
|
|
|
async_add_entities([SamsungTVDevice(config_entry, on_script)])
|
2016-01-31 09:06:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SamsungTVDevice(MediaPlayerDevice):
|
2016-03-08 09:34:33 +00:00
|
|
|
"""Representation of a Samsung TV."""
|
2016-01-31 09:06:39 +00:00
|
|
|
|
2020-01-10 02:19:10 +00:00
|
|
|
def __init__(self, config_entry, on_script):
|
2016-09-05 17:22:26 +00:00
|
|
|
"""Initialize the Samsung device."""
|
2020-01-10 02:19:10 +00:00
|
|
|
self._config_entry = config_entry
|
|
|
|
self._name = config_entry.title
|
|
|
|
self._uuid = config_entry.data.get(CONF_ID)
|
|
|
|
self._manufacturer = config_entry.data.get(CONF_MANUFACTURER)
|
|
|
|
self._model = config_entry.data.get(CONF_MODEL)
|
|
|
|
self._on_script = on_script
|
|
|
|
self._update_listener = None
|
2016-01-31 09:06:39 +00:00
|
|
|
# Assume that the TV is not muted
|
|
|
|
self._muted = False
|
|
|
|
# Assume that the TV is in Play mode
|
|
|
|
self._playing = True
|
2018-10-15 09:42:27 +00:00
|
|
|
self._state = None
|
2016-01-31 09:06:39 +00:00
|
|
|
self._remote = None
|
2017-12-03 17:34:45 +00:00
|
|
|
# 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
|
2016-12-16 06:20:00 +00:00
|
|
|
# Generate a configuration for the Samsung library
|
|
|
|
self._config = {
|
2019-07-31 19:25:30 +00:00
|
|
|
"name": "HomeAssistant",
|
2020-01-10 02:19:10 +00:00
|
|
|
"description": self._name,
|
2019-07-31 19:25:30 +00:00
|
|
|
"id": "ha.component.samsung",
|
2020-01-10 02:19:10 +00:00
|
|
|
"method": config_entry.data[CONF_METHOD],
|
|
|
|
"port": config_entry.data.get(CONF_PORT),
|
|
|
|
"host": config_entry.data[CONF_HOST],
|
|
|
|
"timeout": 1,
|
2016-12-16 06:20:00 +00:00
|
|
|
}
|
2016-01-31 09:06:39 +00:00
|
|
|
|
|
|
|
def update(self):
|
2018-01-22 08:11:16 +00:00
|
|
|
"""Update state of device."""
|
2020-01-23 20:43:30 +00:00
|
|
|
if self._power_off_in_progress():
|
|
|
|
self._state = STATE_OFF
|
|
|
|
else:
|
|
|
|
if self._remote is not None:
|
|
|
|
# Close the current remote connection
|
|
|
|
self._remote.close()
|
|
|
|
self._remote = None
|
|
|
|
|
|
|
|
try:
|
|
|
|
self.get_remote()
|
|
|
|
if self._remote:
|
|
|
|
self._state = STATE_ON
|
|
|
|
except (
|
|
|
|
samsung_exceptions.UnhandledResponse,
|
|
|
|
samsung_exceptions.AccessDenied,
|
|
|
|
):
|
|
|
|
# We got a response so it's working.
|
|
|
|
self._state = STATE_ON
|
|
|
|
except (OSError, WebSocketException):
|
|
|
|
# Different reasons, e.g. hostname not resolveable
|
|
|
|
self._state = STATE_OFF
|
2016-01-31 09:06:39 +00:00
|
|
|
|
|
|
|
def get_remote(self):
|
2016-03-08 09:34:33 +00:00
|
|
|
"""Create or return a remote control instance."""
|
2016-01-31 09:06:39 +00:00
|
|
|
if self._remote is None:
|
|
|
|
# We need to create a new instance to reconnect.
|
2019-11-05 14:04:19 +00:00
|
|
|
self._remote = SamsungRemote(self._config.copy())
|
2016-01-31 09:06:39 +00:00
|
|
|
|
|
|
|
return self._remote
|
|
|
|
|
|
|
|
def send_key(self, key):
|
2016-03-08 09:34:33 +00:00
|
|
|
"""Send a key to the tv and handles exceptions."""
|
2019-07-31 19:25:30 +00:00
|
|
|
if self._power_off_in_progress() and key not in ("KEY_POWER", "KEY_POWEROFF"):
|
2019-10-25 12:32:12 +00:00
|
|
|
LOGGER.info("TV is powering off, not sending command: %s", key)
|
2017-12-03 17:34:45 +00:00
|
|
|
return
|
2016-01-31 09:06:39 +00:00
|
|
|
try:
|
2018-06-09 13:22:34 +00:00
|
|
|
# recreate connection if connection was dead
|
|
|
|
retry_count = 1
|
|
|
|
for _ in range(retry_count + 1):
|
|
|
|
try:
|
|
|
|
self.get_remote().control(key)
|
|
|
|
break
|
2019-11-25 09:45:50 +00:00
|
|
|
except (
|
|
|
|
samsung_exceptions.ConnectionClosed,
|
|
|
|
BrokenPipeError,
|
|
|
|
WebSocketException,
|
|
|
|
):
|
2018-06-09 13:22:34 +00:00
|
|
|
# BrokenPipe can occur when the commands is sent to fast
|
2019-11-25 09:45:50 +00:00
|
|
|
# WebSocketException can occur when timed out
|
2018-06-09 13:22:34 +00:00
|
|
|
self._remote = None
|
2019-11-25 09:45:50 +00:00
|
|
|
except (samsung_exceptions.UnhandledResponse, samsung_exceptions.AccessDenied):
|
2016-01-31 09:06:39 +00:00
|
|
|
# We got a response so it's on.
|
2019-10-25 12:32:12 +00:00
|
|
|
LOGGER.debug("Failed sending command %s", key, exc_info=True)
|
2018-06-09 13:22:34 +00:00
|
|
|
except OSError:
|
2019-11-25 09:45:50 +00:00
|
|
|
# Different reasons, e.g. hostname not resolveable
|
2020-01-23 20:43:30 +00:00
|
|
|
pass
|
2016-01-31 09:06:39 +00:00
|
|
|
|
2017-12-03 17:34:45 +00:00
|
|
|
def _power_off_in_progress(self):
|
2019-07-31 19:25:30 +00:00
|
|
|
return (
|
|
|
|
self._end_of_power_off is not None
|
|
|
|
and self._end_of_power_off > dt_util.utcnow()
|
|
|
|
)
|
2016-01-31 09:06:39 +00:00
|
|
|
|
2018-11-02 09:50:07 +00:00
|
|
|
@property
|
|
|
|
def unique_id(self) -> str:
|
|
|
|
"""Return the unique ID of the device."""
|
|
|
|
return self._uuid
|
|
|
|
|
2016-01-31 09:06:39 +00:00
|
|
|
@property
|
|
|
|
def name(self):
|
2016-03-08 09:34:33 +00:00
|
|
|
"""Return the name of the device."""
|
2016-01-31 09:06:39 +00:00
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
2016-03-08 09:34:33 +00:00
|
|
|
"""Return the state of the device."""
|
2016-01-31 09:06:39 +00:00
|
|
|
return self._state
|
|
|
|
|
2020-01-10 02:19:10 +00:00
|
|
|
@property
|
|
|
|
def device_info(self):
|
|
|
|
"""Return device specific attributes."""
|
|
|
|
return {
|
|
|
|
"name": self.name,
|
|
|
|
"identifiers": {(DOMAIN, self.unique_id)},
|
|
|
|
"manufacturer": self._manufacturer,
|
|
|
|
"model": self._model,
|
|
|
|
}
|
|
|
|
|
2016-01-31 09:06:39 +00:00
|
|
|
@property
|
|
|
|
def is_volume_muted(self):
|
2016-03-08 09:34:33 +00:00
|
|
|
"""Boolean if volume is currently muted."""
|
2016-01-31 09:06:39 +00:00
|
|
|
return self._muted
|
|
|
|
|
2019-06-17 22:00:11 +00:00
|
|
|
@property
|
|
|
|
def source_list(self):
|
|
|
|
"""List of available input sources."""
|
|
|
|
return list(SOURCES)
|
|
|
|
|
2016-01-31 09:06:39 +00:00
|
|
|
@property
|
2017-02-08 04:42:45 +00:00
|
|
|
def supported_features(self):
|
|
|
|
"""Flag media player features that are supported."""
|
2020-01-10 02:19:10 +00:00
|
|
|
if self._on_script:
|
2017-02-21 15:57:29 +00:00
|
|
|
return SUPPORT_SAMSUNGTV | SUPPORT_TURN_ON
|
2016-01-31 09:06:39 +00:00
|
|
|
return SUPPORT_SAMSUNGTV
|
|
|
|
|
2019-10-24 20:41:07 +00:00
|
|
|
@property
|
|
|
|
def device_class(self):
|
|
|
|
"""Set the device class to TV."""
|
|
|
|
return DEVICE_CLASS_TV
|
|
|
|
|
2016-01-31 09:06:39 +00:00
|
|
|
def turn_off(self):
|
2016-03-08 09:34:33 +00:00
|
|
|
"""Turn off media player."""
|
2017-12-03 17:34:45 +00:00
|
|
|
self._end_of_power_off = dt_util.utcnow() + timedelta(seconds=15)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
if self._config["method"] == "websocket":
|
|
|
|
self.send_key("KEY_POWER")
|
2017-01-03 22:28:23 +00:00
|
|
|
else:
|
2019-07-31 19:25:30 +00:00
|
|
|
self.send_key("KEY_POWEROFF")
|
2016-11-18 06:00:18 +00:00
|
|
|
# Force closing of remote session to provide instant UI feedback
|
2017-12-07 16:30:51 +00:00
|
|
|
try:
|
|
|
|
self.get_remote().close()
|
2018-06-09 13:22:34 +00:00
|
|
|
self._remote = None
|
2017-12-07 16:30:51 +00:00
|
|
|
except OSError:
|
2019-10-25 12:32:12 +00:00
|
|
|
LOGGER.debug("Could not establish connection.")
|
2016-01-31 09:06:39 +00:00
|
|
|
|
|
|
|
def volume_up(self):
|
2016-03-08 09:34:33 +00:00
|
|
|
"""Volume up the media player."""
|
2019-07-31 19:25:30 +00:00
|
|
|
self.send_key("KEY_VOLUP")
|
2016-01-31 09:06:39 +00:00
|
|
|
|
|
|
|
def volume_down(self):
|
2016-03-08 09:34:33 +00:00
|
|
|
"""Volume down media player."""
|
2019-07-31 19:25:30 +00:00
|
|
|
self.send_key("KEY_VOLDOWN")
|
2016-01-31 09:06:39 +00:00
|
|
|
|
|
|
|
def mute_volume(self, mute):
|
2016-03-08 09:34:33 +00:00
|
|
|
"""Send mute command."""
|
2019-07-31 19:25:30 +00:00
|
|
|
self.send_key("KEY_MUTE")
|
2016-01-31 09:06:39 +00:00
|
|
|
|
|
|
|
def media_play_pause(self):
|
2016-03-08 09:34:33 +00:00
|
|
|
"""Simulate play pause media player."""
|
2016-01-31 09:06:39 +00:00
|
|
|
if self._playing:
|
|
|
|
self.media_pause()
|
|
|
|
else:
|
|
|
|
self.media_play()
|
|
|
|
|
|
|
|
def media_play(self):
|
2016-03-08 09:34:33 +00:00
|
|
|
"""Send play command."""
|
2016-01-31 09:06:39 +00:00
|
|
|
self._playing = True
|
2019-07-31 19:25:30 +00:00
|
|
|
self.send_key("KEY_PLAY")
|
2016-01-31 09:06:39 +00:00
|
|
|
|
|
|
|
def media_pause(self):
|
2016-03-08 09:34:33 +00:00
|
|
|
"""Send media pause command to media player."""
|
2016-01-31 09:06:39 +00:00
|
|
|
self._playing = False
|
2019-07-31 19:25:30 +00:00
|
|
|
self.send_key("KEY_PAUSE")
|
2016-01-31 09:06:39 +00:00
|
|
|
|
|
|
|
def media_next_track(self):
|
2016-03-08 09:34:33 +00:00
|
|
|
"""Send next track command."""
|
2019-11-15 08:53:16 +00:00
|
|
|
self.send_key("KEY_CHUP")
|
2016-01-31 09:06:39 +00:00
|
|
|
|
|
|
|
def media_previous_track(self):
|
2016-03-08 09:34:33 +00:00
|
|
|
"""Send the previous track command."""
|
2019-11-15 08:53:16 +00:00
|
|
|
self.send_key("KEY_CHDOWN")
|
2016-01-31 09:06:39 +00:00
|
|
|
|
2018-06-26 14:22:10 +00:00
|
|
|
async def async_play_media(self, media_type, media_id, **kwargs):
|
|
|
|
"""Support changing a channel."""
|
|
|
|
if media_type != MEDIA_TYPE_CHANNEL:
|
2019-10-25 12:32:12 +00:00
|
|
|
LOGGER.error("Unsupported media type")
|
2018-06-26 14:22:10 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
# media_id should only be a channel number
|
|
|
|
try:
|
|
|
|
cv.positive_int(media_id)
|
|
|
|
except vol.Invalid:
|
2019-10-25 12:32:12 +00:00
|
|
|
LOGGER.error("Media ID must be positive integer")
|
2018-06-26 14:22:10 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
for digit in media_id:
|
2020-01-10 02:19:10 +00:00
|
|
|
await self.hass.async_add_executor_job(self.send_key, f"KEY_{digit}")
|
2018-06-26 14:22:10 +00:00
|
|
|
await asyncio.sleep(KEY_PRESS_TIMEOUT, self.hass.loop)
|
2020-01-10 02:19:10 +00:00
|
|
|
await self.hass.async_add_executor_job(self.send_key, "KEY_ENTER")
|
2018-06-26 14:22:10 +00:00
|
|
|
|
2020-01-10 02:19:10 +00:00
|
|
|
async def async_turn_on(self):
|
2016-03-08 09:34:33 +00:00
|
|
|
"""Turn the media player on."""
|
2020-01-10 02:19:10 +00:00
|
|
|
if self._on_script:
|
|
|
|
await self._on_script.async_run()
|
2019-06-17 22:00:11 +00:00
|
|
|
|
2020-01-10 02:19:10 +00:00
|
|
|
def select_source(self, source):
|
2019-06-17 22:00:11 +00:00
|
|
|
"""Select input source."""
|
|
|
|
if source not in SOURCES:
|
2019-10-25 12:32:12 +00:00
|
|
|
LOGGER.error("Unsupported source")
|
2019-06-17 22:00:11 +00:00
|
|
|
return
|
|
|
|
|
2020-01-10 02:19:10 +00:00
|
|
|
self.send_key(SOURCES[source])
|