core/homeassistant/components/samsungtv/media_player.py

323 lines
9.6 KiB
Python
Raw Normal View History

"""Support for interface with an Samsung TV."""
import asyncio
2018-09-09 12:26:06 +00:00
from datetime import timedelta
import logging
import socket
2016-09-05 17:22:26 +00:00
import voluptuous as vol
from homeassistant.components.media_player import (
MediaPlayerDevice,
PLATFORM_SCHEMA,
DEVICE_CLASS_TV,
)
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,
)
from homeassistant.const import (
2019-07-31 19:25:30 +00:00
CONF_HOST,
CONF_MAC,
CONF_NAME,
CONF_PORT,
CONF_TIMEOUT,
STATE_OFF,
STATE_ON,
)
2016-09-05 17:22:26 +00:00
import homeassistant.helpers.config_validation as cv
from homeassistant.util import dt as dt_util
_LOGGER = logging.getLogger(__name__)
2019-07-31 19:25:30 +00:00
DEFAULT_NAME = "Samsung TV Remote"
2016-09-05 17:22:26 +00:00
DEFAULT_PORT = 55000
2018-10-09 19:43:59 +00:00
DEFAULT_TIMEOUT = 1
2018-09-09 12:26:06 +00:00
KEY_PRESS_TIMEOUT = 1.2
2019-07-31 19:25:30 +00:00
KNOWN_DEVICES_KEY = "samsungtv_known_devices"
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
)
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,
}
)
2016-09-05 17:22:26 +00:00
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
# 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)
elif discovery_info is not None:
2019-07-31 19:25:30 +00:00
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
2017-03-05 22:52:15 +00:00
mac = None
2019-07-31 19:25:30 +00:00
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)
add_entities([SamsungTVDevice(host, port, name, timeout, mac, uuid)])
_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(MediaPlayerDevice):
2016-03-08 09:34:33 +00:00
"""Representation of a Samsung TV."""
def __init__(self, host, port, name, timeout, mac, uuid):
2016-09-05 17:22:26 +00:00
"""Initialize the Samsung device."""
2017-01-03 22:28:23 +00:00
from samsungctl import exceptions
from samsungctl import Remote
2018-02-06 00:04:31 +00:00
import wakeonlan
2019-07-31 19:25:30 +00:00
2017-01-03 22:28:23 +00:00
# Save a reference to the imported classes
self._exceptions_class = exceptions
self._remote_class = Remote
self._name = name
self._mac = mac
self._uuid = uuid
2018-02-06 00:04:31 +00:00
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 = {
2019-07-31 19:25:30 +00:00
"name": "HomeAssistant",
"description": name,
"id": "ha.component.samsung",
"port": port,
"host": host,
"timeout": timeout,
}
2019-07-31 19:25:30 +00:00
if self._config["port"] in (8001, 8002):
self._config["method"] = "websocket"
2017-01-03 22:28:23 +00:00
else:
2019-07-31 19:25:30 +00:00
self._config["method"] = "legacy"
2017-01-03 22:28:23 +00:00
def update(self):
"""Update state of device."""
self.send_key("KEY")
def get_remote(self):
2016-03-08 09:34:33 +00:00
"""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):
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"):
_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
2019-07-31 19:25:30 +00:00
except (self._exceptions_class.ConnectionClosed, BrokenPipeError):
# BrokenPipe can occur when the commands is sent to fast
self._remote = None
self._state = STATE_ON
2019-07-31 19:25:30 +00:00
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):
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()
)
@property
def unique_id(self) -> str:
"""Return the unique ID of the device."""
return self._uuid
@property
def name(self):
2016-03-08 09:34:33 +00:00
"""Return the name of the device."""
return self._name
@property
def state(self):
2016-03-08 09:34:33 +00:00
"""Return the state of the device."""
return self._state
@property
def is_volume_muted(self):
2016-03-08 09:34:33 +00:00
"""Boolean if volume is currently muted."""
return self._muted
@property
def source_list(self):
"""List of available input sources."""
return list(SOURCES)
@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):
2016-03-08 09:34:33 +00:00
"""Turn off media player."""
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")
# 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):
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")
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")
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")
def media_play_pause(self):
2016-03-08 09:34:33 +00:00
"""Simulate play pause media player."""
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."""
self._playing = True
2019-07-31 19:25:30 +00:00
self.send_key("KEY_PLAY")
def media_pause(self):
2016-03-08 09:34:33 +00:00
"""Send media pause command to media player."""
self._playing = False
2019-07-31 19:25:30 +00:00
self.send_key("KEY_PAUSE")
def media_next_track(self):
2016-03-08 09:34:33 +00:00
"""Send next track command."""
2019-07-31 19:25:30 +00:00
self.send_key("KEY_FF")
def media_previous_track(self):
2016-03-08 09:34:33 +00:00
"""Send the previous track command."""
2019-07-31 19:25:30 +00:00
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:
2019-07-31 19:25:30 +00:00
_LOGGER.error("Unsupported media type")
return
# media_id should only be a channel number
try:
cv.positive_int(media_id)
except vol.Invalid:
2019-07-31 19:25:30 +00:00
_LOGGER.error("Media ID must be positive integer")
return
for digit in media_id:
2019-07-31 19:25:30 +00:00
await self.hass.async_add_job(self.send_key, "KEY_" + digit)
await asyncio.sleep(KEY_PRESS_TIMEOUT, self.hass.loop)
2019-07-31 19:25:30 +00:00
await self.hass.async_add_job(self.send_key, "KEY_ENTER")
def turn_on(self):
2016-03-08 09:34:33 +00:00
"""Turn the media player on."""
if self._mac:
self._wol.send_magic_packet(self._mac)
else:
2019-07-31 19:25:30 +00:00
self.send_key("KEY_POWERON")
async def async_select_source(self, source):
"""Select input source."""
if source not in SOURCES:
2019-07-31 19:25:30 +00:00
_LOGGER.error("Unsupported source")
return
await self.hass.async_add_job(self.send_key, SOURCES[source])