Add support for waking up Samsung TVs over the network (#6114)

* Add support for waking up Samsung TVs over the network

* Return the correct supported features for samsungtv

* Update requirements_all
This commit is contained in:
Justin Dray 2017-02-22 01:57:29 +10:00 committed by Pascal Vizeli
parent 9f4a9585d2
commit b7b1429ac7
2 changed files with 17 additions and 6 deletions

View File

@ -12,12 +12,13 @@ import voluptuous as vol
from homeassistant.components.media_player import (
SUPPORT_NEXT_TRACK, SUPPORT_PAUSE, SUPPORT_PREVIOUS_TRACK,
SUPPORT_TURN_OFF, SUPPORT_VOLUME_MUTE, SUPPORT_VOLUME_STEP,
SUPPORT_PLAY, MediaPlayerDevice, PLATFORM_SCHEMA)
SUPPORT_PLAY, MediaPlayerDevice, PLATFORM_SCHEMA, SUPPORT_TURN_ON)
from homeassistant.const import (
CONF_HOST, CONF_NAME, STATE_OFF, STATE_ON, STATE_UNKNOWN, CONF_PORT)
CONF_HOST, CONF_NAME, STATE_OFF, STATE_ON, STATE_UNKNOWN, CONF_PORT,
CONF_MAC)
import homeassistant.helpers.config_validation as cv
REQUIREMENTS = ['samsungctl==0.6.0']
REQUIREMENTS = ['samsungctl==0.6.0', 'wakeonlan==0.2.2']
_LOGGER = logging.getLogger(__name__)
@ -54,6 +55,7 @@ def setup_platform(hass, config, add_devices, discovery_info=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:
tv_name, model, host = discovery_info
@ -70,7 +72,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
ip_addr = socket.gethostbyname(host)
if ip_addr not in known_devices:
known_devices.add(ip_addr)
add_devices([SamsungTVDevice(host, port, name, timeout)])
add_devices([SamsungTVDevice(host, port, name, timeout, mac)])
_LOGGER.info("Samsung TV %s:%d added as '%s'", host, port, name)
else:
_LOGGER.info("Ignoring duplicate Samsung TV %s:%d", host, port)
@ -79,14 +81,17 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
class SamsungTVDevice(MediaPlayerDevice):
"""Representation of a Samsung TV."""
def __init__(self, host, port, name, timeout):
def __init__(self, host, port, name, timeout, mac):
"""Initialize the Samsung device."""
from samsungctl import exceptions
from samsungctl import Remote
from wakeonlan import wol
# Save a reference to the imported classes
self._exceptions_class = exceptions
self._remote_class = Remote
self._name = name
self._mac = mac
self._wol = wol
# Assume that the TV is not muted
self._muted = False
# Assume that the TV is in Play mode
@ -158,6 +163,8 @@ class SamsungTVDevice(MediaPlayerDevice):
@property
def supported_features(self):
"""Flag media player features that are supported."""
if self._mac:
return SUPPORT_SAMSUNGTV | SUPPORT_TURN_ON
return SUPPORT_SAMSUNGTV
def turn_off(self):
@ -208,4 +215,7 @@ class SamsungTVDevice(MediaPlayerDevice):
def turn_on(self):
"""Turn the media player on."""
self.send_key('KEY_POWERON')
if self._mac:
self._wol.send_magic_packet(self._mac)
else:
self.send_key('KEY_POWERON')

View File

@ -726,6 +726,7 @@ vsure==0.11.1
vtjp==0.1.14
# homeassistant.components.media_player.panasonic_viera
# homeassistant.components.media_player.samsungtv
# homeassistant.components.media_player.webostv
# homeassistant.components.switch.wake_on_lan
wakeonlan==0.2.2