-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmedia_player.py
154 lines (122 loc) · 5.26 KB
/
media_player.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
"""
Support for Denon Network Receivers.
Based off:
https://github.com/home-assistant/home-assistant/blob/dev/homeassistant/components/media_player/denon.py
https://github.com/joopert/nad_receiver/blob/master/nad_receiver/__init__.py
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/media_player.denon/
"""
import logging
import voluptuous as vol
from homeassistant.components.media_player import (
MediaPlayerEntity, PLATFORM_SCHEMA)
from homeassistant.components.media_player.const import (
SUPPORT_SELECT_SOURCE, SUPPORT_TURN_OFF, SUPPORT_TURN_ON,
SUPPORT_VOLUME_MUTE, SUPPORT_VOLUME_SET,SUPPORT_VOLUME_STEP)
from homeassistant.const import (
CONF_NAME, STATE_OFF, STATE_ON, STATE_UNKNOWN)
import homeassistant.helpers.config_validation as cv
_LOGGER = logging.getLogger(__name__)
DEFAULT_NAME = 'Denon232 Receiver'
SUPPORT_DENON = SUPPORT_VOLUME_SET | SUPPORT_VOLUME_STEP | \
SUPPORT_VOLUME_MUTE | SUPPORT_TURN_ON | SUPPORT_TURN_OFF | SUPPORT_SELECT_SOURCE
CONF_SERIAL_PORT = 'serial_port'
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_SERIAL_PORT): cv.string,
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
})
NORMAL_INPUTS = {'CD': 'CD', 'DVD': 'DVD', 'TV': 'TV/CBL','HDP': 'HDP', 'Video Aux': 'V.AUX'}
# Sub-modes of 'NET/USB'
# {'USB': 'USB', 'iPod Direct': 'IPD', 'Internet Radio': 'IRP',
# 'Favorites': 'FVP'}
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Set up the Denon232 platform."""
from .denon232_receiver import Denon232Receiver
add_devices([Denon(
config.get(CONF_NAME),
Denon232Receiver(config.get(CONF_SERIAL_PORT))
)], True)
class Denon(MediaPlayerEntity):
"""Representation of a Denon device."""
def __init__(self, name, denon232_receiver):
"""Initialize the Denon Receiver device."""
self._name = name
self._pwstate = 'PWSTANDBY'
self._volume = 0
# Initial value 60dB, changed if we get a MVMAX
self._volume_max = 60
self._source_list = NORMAL_INPUTS.copy()
self._mediasource = ''
self._muted = False
self._denon232_receiver = denon232_receiver
def update(self):
"""Get the latest details from the device."""
self._pwstate = self._denon232_receiver.serial_command('PW?', response=True)
for line in self._denon232_receiver.serial_command('MV?', response=True, all_lines=True):
if line.startswith('MVMAX '):
# only grab two digit max, don't care about any half digit
self._volume_max = int(line[len('MVMAX '):len('MVMAX XX')])
_LOGGER.debug("MVMAX Value Saved: %s", self._volume_max)
continue
if line.startswith('MV'):
self._volume = int(line[len('MV'):len('MVXX')])
if self._volume == 99:
self._volume = 0
_LOGGER.debug("MV Value Saved: %s", self._volume)
self._muted = (self._denon232_receiver.serial_command('MU?', response=True) == 'MUON')
self._mediasource = self._denon232_receiver.serial_command('SI?', response=True)[len('SI'):]
@property
def name(self):
"""Return the name of the device."""
return self._name
@property
def state(self):
"""Return the state of the device."""
if self._pwstate == 'PWSTANDBY':
return STATE_OFF
else:
return STATE_ON
@property
def volume_level(self):
"""Volume level of the media player (0..1)."""
return self._volume / self._volume_max
@property
def is_volume_muted(self):
"""Return boolean if volume is currently muted."""
return self._muted
@property
def source_list(self):
"""Return the list of available input sources."""
return sorted(list(self._source_list.keys()))
@property
def supported_features(self):
"""Flag media player features that are supported."""
return SUPPORT_DENON
@property
def source(self):
"""Return the current input source."""
for pretty_name, name in self._source_list.items():
if self._mediasource == name:
return pretty_name
def turn_on(self):
"""Turn the media player on."""
self._denon232_receiver.serial_command('PWON')
def turn_off(self):
"""Turn off media player."""
self._denon232_receiver.serial_command('PWSTANDBY')
def volume_up(self):
"""Volume up media player."""
self._denon232_receiver.serial_command('MVUP')
def volume_down(self):
"""Volume down media player."""
self._denon232_receiver.serial_command('MVDOWN')
def set_volume_level(self, volume):
"""Set volume level, range 0..1."""
self._denon232_receiver.serial_command('MV' +
str(round(volume * self._volume_max)).zfill(2))
def mute_volume(self, mute):
"""Mute (true) or unmute (false) media player."""
self._denon232_receiver.serial_command('MU' + ('ON' if mute else 'OFF'))
def select_source(self, source):
"""Select input source."""
self._denon232_receiver.serial_command('SI' + self._source_list.get(source))