Add support for new netdisco detection of Samsung Smart TV. (#4925)

pull/4939/head
Magnus Ihse Bursie 2016-12-16 07:20:00 +01:00 committed by Paulus Schoutsen
parent 7748867732
commit 103fffa0f4
2 changed files with 42 additions and 13 deletions

View File

@ -37,6 +37,7 @@ SERVICE_HANDLERS = {
'logitech_mediaserver': ('media_player', 'squeezebox'), 'logitech_mediaserver': ('media_player', 'squeezebox'),
'directv': ('media_player', 'directv'), 'directv': ('media_player', 'directv'),
'denonavr': ('media_player', 'denonavr'), 'denonavr': ('media_player', 'denonavr'),
'samsung_tv': ('media_player', 'samsungtv'),
} }
CONFIG_SCHEMA = vol.Schema({ CONFIG_SCHEMA = vol.Schema({

View File

@ -5,6 +5,7 @@ For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/media_player.samsungtv/ https://home-assistant.io/components/media_player.samsungtv/
""" """
import logging import logging
import socket
import voluptuous as vol import voluptuous as vol
@ -26,6 +27,8 @@ DEFAULT_NAME = 'Samsung TV Remote'
DEFAULT_PORT = 55000 DEFAULT_PORT = 55000
DEFAULT_TIMEOUT = 0 DEFAULT_TIMEOUT = 0
KNOWN_DEVICES_KEY = 'samsungtv_known_devices'
SUPPORT_SAMSUNGTV = SUPPORT_PAUSE | SUPPORT_VOLUME_STEP | \ SUPPORT_SAMSUNGTV = SUPPORT_PAUSE | SUPPORT_VOLUME_STEP | \
SUPPORT_VOLUME_MUTE | SUPPORT_PREVIOUS_TRACK | \ SUPPORT_VOLUME_MUTE | SUPPORT_PREVIOUS_TRACK | \
SUPPORT_NEXT_TRACK | SUPPORT_TURN_OFF SUPPORT_NEXT_TRACK | SUPPORT_TURN_OFF
@ -41,25 +44,42 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
# pylint: disable=unused-argument # pylint: disable=unused-argument
def setup_platform(hass, config, add_devices, discovery_info=None): def setup_platform(hass, config, add_devices, discovery_info=None):
"""Setup the Samsung TV platform.""" """Setup the Samsung TV platform."""
name = config.get(CONF_NAME) known_devices = hass.data.get(KNOWN_DEVICES_KEY)
if known_devices is None:
known_devices = set()
hass.data[KNOWN_DEVICES_KEY] = known_devices
# Generate a configuration for the Samsung library # Is this a manual configuration?
remote_config = { if config.get(CONF_HOST) is not None:
'name': 'HomeAssistant', host = config.get(CONF_HOST)
'description': config.get(CONF_NAME), port = config.get(CONF_PORT)
'id': 'ha.component.samsung', name = config.get(CONF_NAME)
'port': config.get(CONF_PORT), timeout = config.get(CONF_TIMEOUT)
'host': config.get(CONF_HOST), elif discovery_info is not None:
'timeout': config.get(CONF_TIMEOUT), tv_name, model, host = discovery_info
} name = "{} ({})".format(tv_name, model)
port = DEFAULT_PORT
timeout = DEFAULT_TIMEOUT
else:
_LOGGER.warning(
'Internal error on samsungtv component. Cannot determine device')
return
add_devices([SamsungTVDevice(name, remote_config)]) # 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_devices([SamsungTVDevice(host, port, name, timeout)])
_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): class SamsungTVDevice(MediaPlayerDevice):
"""Representation of a Samsung TV.""" """Representation of a Samsung TV."""
def __init__(self, name, config): def __init__(self, host, port, name, timeout):
"""Initialize the Samsung device.""" """Initialize the Samsung device."""
from samsungctl import Remote from samsungctl import Remote
# Save a reference to the imported class # Save a reference to the imported class
@ -71,7 +91,15 @@ class SamsungTVDevice(MediaPlayerDevice):
self._playing = True self._playing = True
self._state = STATE_UNKNOWN self._state = STATE_UNKNOWN
self._remote = None self._remote = None
self._config = config # Generate a configuration for the Samsung library
self._config = {
'name': 'HomeAssistant',
'description': name,
'id': 'ha.component.samsung',
'port': port,
'host': host,
'timeout': timeout,
}
def update(self): def update(self):
"""Retrieve the latest data.""" """Retrieve the latest data."""