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
|
|
|
|
2016-09-05 17:22:26 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2020-04-25 16:00:57 +00:00
|
|
|
from homeassistant.components.media_player import DEVICE_CLASS_TV, MediaPlayerEntity
|
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,
|
2020-02-03 19:34:02 +00:00
|
|
|
CONF_IP_ADDRESS,
|
2020-01-10 02:19:10 +00:00
|
|
|
CONF_METHOD,
|
2020-02-03 19:34:02 +00:00
|
|
|
CONF_NAME,
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_PORT,
|
2020-03-10 10:48:09 +00:00
|
|
|
CONF_TOKEN,
|
2019-07-31 19:25:30 +00:00
|
|
|
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-03-10 10:48:09 +00:00
|
|
|
from .bridge import SamsungTVBridge
|
2020-08-12 16:39:05 +00:00
|
|
|
from .const import (
|
|
|
|
CONF_MANUFACTURER,
|
|
|
|
CONF_MODEL,
|
|
|
|
CONF_ON_ACTION,
|
|
|
|
DEFAULT_NAME,
|
|
|
|
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_entry(hass, config_entry, async_add_entities):
|
|
|
|
"""Set up the Samsung TV from a config entry."""
|
2020-02-03 19:34:02 +00:00
|
|
|
ip_address = config_entry.data[CONF_IP_ADDRESS]
|
|
|
|
on_script = None
|
|
|
|
if (
|
|
|
|
DOMAIN in hass.data
|
|
|
|
and ip_address in hass.data[DOMAIN]
|
|
|
|
and CONF_ON_ACTION in hass.data[DOMAIN][ip_address]
|
|
|
|
and hass.data[DOMAIN][ip_address][CONF_ON_ACTION]
|
|
|
|
):
|
|
|
|
turn_on_action = hass.data[DOMAIN][ip_address][CONF_ON_ACTION]
|
2020-08-12 16:39:05 +00:00
|
|
|
on_script = Script(
|
|
|
|
hass, turn_on_action, config_entry.data.get(CONF_NAME, DEFAULT_NAME), DOMAIN
|
|
|
|
)
|
2020-03-16 23:37:10 +00:00
|
|
|
|
|
|
|
# Initialize bridge
|
|
|
|
data = config_entry.data.copy()
|
|
|
|
bridge = SamsungTVBridge.get_bridge(
|
|
|
|
data[CONF_METHOD], data[CONF_HOST], data[CONF_PORT], data.get(CONF_TOKEN),
|
|
|
|
)
|
|
|
|
if bridge.port is None and bridge.default_port is not None:
|
|
|
|
# For backward compat, set default port for websocket tv
|
|
|
|
data[CONF_PORT] = bridge.default_port
|
|
|
|
hass.config_entries.async_update_entry(config_entry, data=data)
|
|
|
|
bridge = SamsungTVBridge.get_bridge(
|
|
|
|
data[CONF_METHOD], data[CONF_HOST], data[CONF_PORT], data.get(CONF_TOKEN),
|
|
|
|
)
|
|
|
|
|
|
|
|
async_add_entities([SamsungTVDevice(bridge, config_entry, on_script)])
|
2016-01-31 09:06:39 +00:00
|
|
|
|
|
|
|
|
2020-04-25 16:00:57 +00:00
|
|
|
class SamsungTVDevice(MediaPlayerEntity):
|
2016-03-08 09:34:33 +00:00
|
|
|
"""Representation of a Samsung TV."""
|
2016-01-31 09:06:39 +00:00
|
|
|
|
2020-03-16 23:37:10 +00:00
|
|
|
def __init__(self, bridge, 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._manufacturer = config_entry.data.get(CONF_MANUFACTURER)
|
|
|
|
self._model = config_entry.data.get(CONF_MODEL)
|
2020-02-03 19:34:02 +00:00
|
|
|
self._name = config_entry.data.get(CONF_NAME)
|
2020-01-10 02:19:10 +00:00
|
|
|
self._on_script = on_script
|
2020-02-03 19:34:02 +00:00
|
|
|
self._uuid = config_entry.data.get(CONF_ID)
|
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
|
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
|
2020-03-16 23:37:10 +00:00
|
|
|
self._bridge = bridge
|
2020-03-10 10:48:09 +00:00
|
|
|
self._bridge.register_reauth_callback(self.access_denied)
|
|
|
|
|
|
|
|
def access_denied(self):
|
2020-08-08 12:41:02 +00:00
|
|
|
"""Access denied callback."""
|
2020-03-10 10:48:09 +00:00
|
|
|
LOGGER.debug("Access denied in getting remote object")
|
|
|
|
self.hass.add_job(
|
|
|
|
self.hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": "reauth"}, data=self._config_entry.data,
|
|
|
|
)
|
|
|
|
)
|
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:
|
2020-03-10 10:48:09 +00:00
|
|
|
self._state = STATE_ON if self._bridge.is_on() else STATE_OFF
|
2016-01-31 09:06:39 +00:00
|
|
|
|
|
|
|
def send_key(self, key):
|
2016-03-08 09:34:33 +00:00
|
|
|
"""Send a key to the tv and handles exceptions."""
|
2020-03-10 10:48:09 +00:00
|
|
|
if self._power_off_in_progress() and key != "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
|
2020-03-10 10:48:09 +00:00
|
|
|
self._bridge.send_key(key)
|
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)
|
|
|
|
|
2020-03-10 10:48:09 +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
|
2020-03-10 10:48:09 +00:00
|
|
|
self._bridge.close_remote()
|
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:
|
2020-08-21 12:17:47 +00:00
|
|
|
await self._on_script.async_run(context=self._context)
|
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])
|