forked from jalmeroth/hass_yamaha_musiccast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyamaha_musiccast.py
208 lines (163 loc) · 5.96 KB
/
yamaha_musiccast.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
"""
Example for configuration.yaml
media_player:
- platform: yamaha_musiccast
name: "Living Room"
host: 192.168.xxx.xx
port: 5005
"""
import logging
import voluptuous as vol
import homeassistant.helpers.config_validation as cv
from homeassistant.const import (
CONF_NAME, CONF_HOST, CONF_PORT,
STATE_UNKNOWN, STATE_ON
)
from homeassistant.components.media_player import (
MediaPlayerDevice, MEDIA_TYPE_MUSIC, PLATFORM_SCHEMA,
SUPPORT_PAUSE, SUPPORT_PREVIOUS_TRACK, SUPPORT_NEXT_TRACK,
SUPPORT_TURN_ON, SUPPORT_TURN_OFF, SUPPORT_PLAY,
SUPPORT_VOLUME_SET, SUPPORT_VOLUME_MUTE,
SUPPORT_SELECT_SOURCE, SUPPORT_STOP
)
_LOGGER = logging.getLogger(__name__)
SUPPORTED_FEATURES = (
SUPPORT_PLAY | SUPPORT_PAUSE | SUPPORT_STOP |
SUPPORT_PREVIOUS_TRACK | SUPPORT_NEXT_TRACK |
SUPPORT_TURN_ON | SUPPORT_TURN_OFF |
SUPPORT_VOLUME_SET | SUPPORT_VOLUME_MUTE |
SUPPORT_SELECT_SOURCE
)
REQUIREMENTS = ['pymusiccast==0.0.5']
DEFAULT_NAME = "Yamaha Receiver"
DEFAULT_PORT = 5005
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Required(CONF_HOST): cv.string,
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.positive_int,
})
def setup_platform(hass, config, add_devices, discovery_info=None):
import pymusiccast
_LOGGER.debug("config: {} ({})".format(config, type(config)))
name = config.get(CONF_NAME)
host = config.get(CONF_HOST)
port = config.get(CONF_PORT)
mcDevice = pymusiccast.mcDevice(host, udp_port=port)
_LOGGER.debug("mcDevice: {} / UDP Port: {}".format(mcDevice, port))
add_devices([YamahaDevice(mcDevice, name)], True)
class YamahaDevice(MediaPlayerDevice):
def __init__(self, mcDevice, name):
self._mcDevice = mcDevice
self._name = name
self._power = STATE_UNKNOWN
self._volume = 0
self._volumeMax = 0
self._mute = False
self._source = None
self._source_list = []
self._status = STATE_UNKNOWN
self._media_status = None
self._mcDevice.setYamahaDevice(self)
@property
def name(self):
return self._name
@property
def state(self):
if self._power == STATE_ON and self._status is not STATE_UNKNOWN:
return self._status
else:
return self._power
@property
def should_poll(self):
"""Push an update after each command."""
return True
@property
def is_volume_muted(self):
"""Boolean if volume is currently muted."""
return self._mute
@property
def volume_level(self):
"""Volume level of the media player (0..1)."""
return self._volume
@property
def supported_features(self):
"""Flag of features that are supported."""
return SUPPORTED_FEATURES
@property
def source(self):
"""Return the current input source."""
return self._source
@property
def source_list(self):
"""List of available input sources."""
return self._source_list
@property
def media_content_type(self):
if self._media_status is None:
return None
else:
return MEDIA_TYPE_MUSIC
@property
def media_duration(self):
return self._media_status.media_duration if self._media_status else None
@property
def media_image_url(self):
return self._media_status.media_image_url if self._media_status else None
@property
def media_artist(self):
return self._media_status.media_artist if self._media_status else None
@property
def media_album(self):
return self._media_status.media_album if self._media_status else None
@property
def media_track(self):
return self._media_status.media_track if self._media_status else None
@property
def media_title(self):
return self._media_status.media_title if self._media_status else None
def update(self):
_LOGGER.debug("update: {}".format(self.entity_id))
if not self.entity_id: # call from constructor setup_platform()
_LOGGER.debug("First run")
self._mcDevice.updateStatus(push=False)
else: # call from regular polling
# updateStatus_timer was set before
if self._mcDevice.updateStatus_timer:
_LOGGER.debug("is_alive: {}".format(self._mcDevice.updateStatus_timer.is_alive()))
# can happen if e.g. computer was suspended, while hass was running
if not self._mcDevice.updateStatus_timer.is_alive():
_LOGGER.debug("Reinitializing")
self._mcDevice.updateStatus()
def turn_on(self):
_LOGGER.debug("Turn device: on")
self._mcDevice.setPower(True)
def turn_off(self):
_LOGGER.debug("Turn device: off")
self._mcDevice.setPower(False)
def media_play(self):
_LOGGER.debug("Play")
self._mcDevice.setPlayback("play")
def media_pause(self):
_LOGGER.debug("Pause")
self._mcDevice.setPlayback("pause")
def media_stop(self):
_LOGGER.debug("Stop")
self._mcDevice.setPlayback("stop")
def media_previous_track(self):
_LOGGER.debug("Previous")
self._mcDevice.setPlayback("previous")
def media_next_track(self):
_LOGGER.debug("Next")
self._mcDevice.setPlayback("next")
def mute_volume(self, mute):
"""Send mute command."""
_LOGGER.debug("Mute volume: {}".format(mute))
self._mcDevice.setMute(mute)
def set_volume_level(self, volume):
"""Set volume level, range 0..1."""
_LOGGER.debug("Volume level: {} / {}".format(volume, volume * self._volumeMax))
self._mcDevice.setVolume(volume * self._volumeMax)
def select_source(self, source):
_LOGGER.debug("select_source: {}".format(source))
self._status = STATE_UNKNOWN
self._mcDevice.setInput(source)