core/homeassistant/components/media_player/denon.py

163 lines
4.7 KiB
Python
Raw Normal View History

2015-09-06 10:07:12 +00:00
"""
2016-03-08 09:34:33 +00:00
Support for Denon Network Receivers.
2015-09-06 10:07:12 +00:00
2015-10-24 07:13:54 +00:00
For more details about this platform, please refer to the documentation at
2015-11-09 12:12:18 +00:00
https://home-assistant.io/components/media_player.denon/
2015-09-06 10:07:12 +00:00
"""
import logging
2016-02-19 05:27:50 +00:00
import telnetlib
2015-09-06 10:07:12 +00:00
from homeassistant.components.media_player import (
2016-02-19 05:27:50 +00:00
DOMAIN, SUPPORT_NEXT_TRACK, SUPPORT_PAUSE, SUPPORT_PREVIOUS_TRACK,
SUPPORT_TURN_OFF, SUPPORT_TURN_ON, SUPPORT_VOLUME_MUTE, SUPPORT_VOLUME_SET,
MediaPlayerDevice)
from homeassistant.const import CONF_HOST, STATE_OFF, STATE_ON, STATE_UNKNOWN
2015-09-06 10:07:12 +00:00
_LOGGER = logging.getLogger(__name__)
SUPPORT_DENON = SUPPORT_PAUSE | SUPPORT_VOLUME_SET | SUPPORT_VOLUME_MUTE | \
SUPPORT_PREVIOUS_TRACK | SUPPORT_NEXT_TRACK | \
SUPPORT_TURN_ON | SUPPORT_TURN_OFF
def setup_platform(hass, config, add_devices, discovery_info=None):
2016-03-08 09:34:33 +00:00
"""Setup the Denon platform."""
2015-09-06 10:07:12 +00:00
if not config.get(CONF_HOST):
_LOGGER.error(
"Missing required configuration items in %s: %s",
DOMAIN,
CONF_HOST)
return False
denon = DenonDevice(
config.get("name", "Music station"),
config.get("host")
)
if denon.update():
add_devices([denon])
return True
else:
return False
2015-09-06 10:07:12 +00:00
class DenonDevice(MediaPlayerDevice):
2016-03-08 09:34:33 +00:00
"""Representation of a Denon device."""
2015-09-06 10:07:12 +00:00
2016-02-02 08:31:36 +00:00
# pylint: disable=too-many-public-methods, abstract-method
2015-09-06 10:07:12 +00:00
def __init__(self, name, host):
2016-03-08 09:34:33 +00:00
"""Initialize the Denon device."""
2015-09-06 10:07:12 +00:00
self._name = name
self._host = host
self._pwstate = "PWSTANDBY"
self._volume = 0
self._muted = False
self._mediasource = ""
@classmethod
def telnet_request(cls, telnet, command):
2016-03-08 09:34:33 +00:00
"""Execute `command` and return the response."""
telnet.write(command.encode("ASCII") + b"\r")
return telnet.read_until(b"\r", timeout=0.2).decode("ASCII").strip()
def telnet_command(self, command):
2016-03-08 09:34:33 +00:00
"""Establish a telnet connection and sends `command`."""
telnet = telnetlib.Telnet(self._host)
telnet.write(command.encode("ASCII") + b"\r")
telnet.read_very_eager() # skip response
telnet.close()
def update(self):
2016-03-08 09:34:33 +00:00
"""Get the latest details from the device."""
2015-09-06 10:07:12 +00:00
try:
telnet = telnetlib.Telnet(self._host)
except ConnectionRefusedError:
return False
2015-09-06 10:07:12 +00:00
self._pwstate = self.telnet_request(telnet, "PW?")
# PW? sends also SISTATUS, which is not interesting
telnet.read_until(b"\r", timeout=0.2)
2015-09-06 10:07:12 +00:00
volume_str = self.telnet_request(telnet, "MV?")[len("MV"):]
self._volume = int(volume_str) / 60
self._muted = (self.telnet_request(telnet, "MU?") == "MUON")
self._mediasource = self.telnet_request(telnet, "SI?")[len("SI"):]
telnet.close()
return True
2015-09-06 10:07:12 +00:00
@property
def name(self):
2016-03-08 09:34:33 +00:00
"""Return the name of the device."""
2015-09-06 10:07:12 +00:00
return self._name
@property
def state(self):
2016-03-08 09:34:33 +00:00
"""Return the state of the device."""
if self._pwstate == "PWSTANDBY":
2015-09-06 10:07:12 +00:00
return STATE_OFF
if self._pwstate == "PWON":
return STATE_ON
2015-09-06 10:07:12 +00:00
return STATE_UNKNOWN
@property
def volume_level(self):
2016-03-08 09:34:33 +00:00
"""Volume level of the media player (0..1)."""
return self._volume
2015-09-06 10:07:12 +00:00
@property
def is_volume_muted(self):
2016-03-08 09:34:33 +00:00
"""Boolean if volume is currently muted."""
return self._muted
2015-09-06 10:07:12 +00:00
@property
def media_title(self):
2016-03-08 09:34:33 +00:00
"""Current media source."""
return self._mediasource
2015-09-06 10:07:12 +00:00
@property
def supported_media_commands(self):
2016-03-08 09:34:33 +00:00
"""Flag of media commands that are supported."""
2015-09-06 10:07:12 +00:00
return SUPPORT_DENON
def turn_off(self):
2016-03-08 09:34:33 +00:00
"""Turn off media player."""
self.telnet_command("PWSTANDBY")
2015-09-06 10:07:12 +00:00
def volume_up(self):
2016-03-08 09:34:33 +00:00
"""Volume up media player."""
self.telnet_command("MVUP")
2015-09-06 10:07:12 +00:00
def volume_down(self):
2016-03-08 09:34:33 +00:00
"""Volume down media player."""
self.telnet_command("MVDOWN")
2015-09-06 10:07:12 +00:00
def set_volume_level(self, volume):
2016-03-08 09:34:33 +00:00
"""Set volume level, range 0..1."""
2015-09-06 10:07:12 +00:00
# 60dB max
self.telnet_command("MV" + str(round(volume * 60)).zfill(2))
2015-09-06 10:07:12 +00:00
def mute_volume(self, mute):
2016-03-08 09:34:33 +00:00
"""Mute (true) or unmute (false) media player."""
self.telnet_command("MU" + ("ON" if mute else "OFF"))
2015-09-06 10:07:12 +00:00
def media_play(self):
2016-03-08 09:34:33 +00:00
"""Play media media player."""
self.telnet_command("NS9A")
2015-09-06 10:07:12 +00:00
def media_pause(self):
2016-03-08 09:34:33 +00:00
"""Pause media player."""
self.telnet_command("NS9B")
2015-09-06 10:07:12 +00:00
def media_next_track(self):
2016-03-08 09:34:33 +00:00
"""Send the next track command."""
self.telnet_command("NS9D")
2015-09-06 10:07:12 +00:00
def media_previous_track(self):
2016-03-08 09:34:33 +00:00
"""Send the previous track command."""
self.telnet_command("NS9E")
2015-09-06 10:07:12 +00:00
def turn_on(self):
2016-03-08 09:34:33 +00:00
"""Turn the media player on."""
self.telnet_command("PWON")