Add support for the Unitymedia Horizon HD Recorder (#14275)

* added new platform for the Unitymedia Horizon HD Recorder

* improve connection handling of the horizon platform

* remove unneeded parameters and fix spelling in the horizon platform

* abort or raise exception if connection to the device could not be established

* remove channel/source list and SELECT_SOURCE feature

* remove useless type check after cast and use a try block instead

* abort or raise exception if reconnect to device fails

* remove protocol specific code and restructure sending logic accordingly

* fix indentation to be pep8 complaint

* remove unused methods/properties

* fix unnecessary pylint commands and use a return to abert outside of setup_platform

* directly access config values
This commit is contained in:
Ben Lebherz 2018-06-10 15:38:55 +02:00 committed by Sebastian Muszynski
parent d5bbb6ffd2
commit 1da30032a0
3 changed files with 191 additions and 0 deletions

View File

@ -493,6 +493,7 @@ omit =
homeassistant/components/media_player/frontier_silicon.py
homeassistant/components/media_player/gpmdp.py
homeassistant/components/media_player/gstreamer.py
homeassistant/components/media_player/horizon.py
homeassistant/components/media_player/itunes.py
homeassistant/components/media_player/kodi.py
homeassistant/components/media_player/lg_netcast.py

View File

@ -0,0 +1,187 @@
"""
Support for the Unitymedia Horizon HD Recorder.
For more details about this platform, please refer to the documentation
https://home-assistant.io/components/media_player.horizon/
"""
from datetime import timedelta
import logging
import voluptuous as vol
from homeassistant.components.media_player import (
MediaPlayerDevice, PLATFORM_SCHEMA, MEDIA_TYPE_CHANNEL,
SUPPORT_NEXT_TRACK, SUPPORT_TURN_ON, SUPPORT_TURN_OFF, SUPPORT_PAUSE,
SUPPORT_PLAY, SUPPORT_PLAY_MEDIA, SUPPORT_PREVIOUS_TRACK)
from homeassistant.const import (CONF_HOST, CONF_NAME, CONF_PORT, STATE_OFF,
STATE_PAUSED, STATE_PLAYING)
from homeassistant.exceptions import PlatformNotReady
import homeassistant.helpers.config_validation as cv
import homeassistant.util as util
REQUIREMENTS = ['einder==0.3.1']
_LOGGER = logging.getLogger(__name__)
DEFAULT_NAME = "Horizon"
DEFAULT_PORT = 5900
MIN_TIME_BETWEEN_FORCED_SCANS = timedelta(seconds=1)
MIN_TIME_BETWEEN_SCANS = timedelta(seconds=10)
SUPPORT_HORIZON = SUPPORT_NEXT_TRACK | SUPPORT_PAUSE | SUPPORT_PLAY | \
SUPPORT_PLAY_MEDIA | SUPPORT_PREVIOUS_TRACK | SUPPORT_TURN_ON | \
SUPPORT_TURN_OFF
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,
})
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Set up the Horizon platform."""
from einder import Client, keys
from einder.exceptions import AuthenticationError
host = config[CONF_HOST]
name = config[CONF_NAME]
port = config[CONF_PORT]
try:
client = Client(host, port=port)
except AuthenticationError as msg:
_LOGGER.error("Authentication to %s at %s failed: %s", name, host, msg)
return
except OSError as msg:
# occurs if horizon box is offline
_LOGGER.error("Connection to %s at %s failed: %s", name, host, msg)
raise PlatformNotReady
_LOGGER.info("Connection to %s at %s established", name, host)
add_devices([HorizonDevice(client, name, keys)], True)
class HorizonDevice(MediaPlayerDevice):
"""Representation of a Horizon HD Recorder."""
def __init__(self, client, name, keys):
"""Initialize the remote."""
self._client = client
self._name = name
self._state = None
self._keys = keys
@property
def name(self):
"""Return the name of the remote."""
return self._name
@property
def state(self):
"""Return the state of the device."""
return self._state
@property
def supported_features(self):
"""Flag media player features that are supported."""
return SUPPORT_HORIZON
@util.Throttle(MIN_TIME_BETWEEN_SCANS, MIN_TIME_BETWEEN_FORCED_SCANS)
def update(self):
"""Update State using the media server running on the Horizon."""
if self._client.is_powered_on():
self._state = STATE_PLAYING
else:
self._state = STATE_OFF
def turn_on(self):
"""Turn the device on."""
if self._state is STATE_OFF:
self._send_key(self._keys.POWER)
def turn_off(self):
"""Turn the device off."""
if self._state is not STATE_OFF:
self._send_key(self._keys.POWER)
def media_previous_track(self):
"""Channel down."""
self._send_key(self._keys.CHAN_DOWN)
self._state = STATE_PLAYING
def media_next_track(self):
"""Channel up."""
self._send_key(self._keys.CHAN_UP)
self._state = STATE_PLAYING
def media_play(self):
"""Send play command."""
self._send_key(self._keys.PAUSE)
self._state = STATE_PLAYING
def media_pause(self):
"""Send pause command."""
self._send_key(self._keys.PAUSE)
self._state = STATE_PAUSED
def media_play_pause(self):
"""Send play/pause command."""
self._send_key(self._keys.PAUSE)
if self._state == STATE_PAUSED:
self._state = STATE_PLAYING
else:
self._state = STATE_PAUSED
def play_media(self, media_type, media_id, **kwargs):
"""Play media / switch to channel."""
if MEDIA_TYPE_CHANNEL == media_type:
try:
self._select_channel(int(media_id))
self._state = STATE_PLAYING
except ValueError:
_LOGGER.error("Invalid channel: %s", media_id)
else:
_LOGGER.error("Invalid media type %s. Supported type: %s",
media_type, MEDIA_TYPE_CHANNEL)
def _select_channel(self, channel):
"""Select a channel (taken from einder library, thx)."""
self._send(channel=channel)
def _send_key(self, key):
"""Send a key to the Horizon device."""
self._send(key=key)
def _send(self, key=None, channel=None):
"""Send a key to the Horizon device."""
from einder.exceptions import AuthenticationError
try:
if key:
self._client.send_key(key)
elif channel:
self._client.select_channel(channel)
except OSError as msg:
_LOGGER.error("%s disconnected: %s. Trying to reconnect...",
self._name, msg)
# for reconnect, first gracefully disconnect
self._client.disconnect()
try:
self._client.connect()
self._client.authorize()
except AuthenticationError as msg:
_LOGGER.error("Authentication to %s failed: %s", self._name,
msg)
return
except OSError as msg:
# occurs if horizon box is offline
_LOGGER.error("Reconnect to %s failed: %s", self._name, msg)
return
self._send(key=key, channel=channel)

View File

@ -279,6 +279,9 @@ dsmr_parser==0.11
# homeassistant.components.sensor.dweet
dweepy==0.3.0
# homeassistant.components.media_player.horizon
einder==0.3.1
# homeassistant.components.sensor.eliqonline
eliqonline==1.0.14