desc
stringlengths
3
26.7k
decl
stringlengths
11
7.89k
bodies
stringlengths
8
553k
'Turn on the media player.'
def turn_on(self):
if self._mac: self._wol.send_magic_packet(self._mac)
'Volume up the media player.'
def volume_up(self):
self._client.volume_up()
'Volume down media player.'
def volume_down(self):
self._client.volume_down()
'Set volume level, range 0..1.'
def set_volume_level(self, volume):
tv_volume = (volume * 100) self._client.set_volume(tv_volume)
'Send mute command.'
def mute_volume(self, mute):
self._muted = mute self._client.set_mute(mute)
'Simulate play pause media player.'
def media_play_pause(self):
if self._playing: self.media_pause() else: self.media_play()
'Select input source.'
def select_source(self, source):
if self._source_list.get(source).get('title'): self._current_source_id = self._source_list[source]['id'] self._current_source = self._source_list[source]['title'] self._client.launch_app(self._source_list[source]['id']) elif self._source_list.get(source).get('label'): self._curre...
'Send play command.'
def media_play(self):
self._playing = True self._state = STATE_PLAYING self._client.play()
'Send media pause command to media player.'
def media_pause(self):
self._playing = False self._state = STATE_PAUSED self._client.pause()
'Send next track command.'
def media_next_track(self):
self._client.fast_forward()
'Send the previous track command.'
def media_previous_track(self):
self._client.rewind()
'Queue up event for processing.'
def put(self, item, block=True, timeout=None):
self._sonos_device.process_sonos_event(item)
'Initialize the Sonos device.'
def __init__(self, player):
self.volume_increment = 5 self._unique_id = player.uid self._player = player self._player_volume = None self._player_volume_muted = None self._speaker_info = None self._name = None self._status = None self._coordinator = None self._media_content_id = None self._media_duration...
'Subscribe sonos events.'
@asyncio.coroutine def async_added_to_hass(self):
self.hass.async_add_job(self._subscribe_to_player_events)
'Return the polling state.'
@property def should_poll(self):
return True
'Return an unique ID.'
@property def unique_id(self):
return self._unique_id
'Return the name of the device.'
@property def name(self):
return self._name
'Return the state of the device.'
@property def state(self):
if self._coordinator: return self._coordinator.state if (self._status in ('PAUSED_PLAYBACK', 'STOPPED')): return STATE_PAUSED if (self._status in ('PLAYING', 'TRANSITIONING')): return STATE_PLAYING if (self._status == 'OFF'): return STATE_OFF return STATE_IDLE
'Return true if player is a coordinator.'
@property def is_coordinator(self):
return (self._coordinator is None)
'Return soco device.'
@property def soco(self):
return self._player
'Return coordinator of this player.'
@property def coordinator(self):
return self._coordinator
'Retrieve latest state.'
def update(self):
if (self._speaker_info is None): self._speaker_info = self._player.get_speaker_info(True) self._name = self._speaker_info['zone_name'].replace(' (R)', '').replace(' (L)', '') self._favorite_sources = self._player.get_sonos_favorites()['favorites'] if self._last_avtransport_event: ...
'Process a service event coming from the speaker.'
def process_sonos_event(self, event):
next_track_image_url = None if (event.service == self._player.avTransport): self._last_avtransport_event = event self._media_radio_show = None if self._current_track_is_radio_stream: current_track_metadata = event.variables.get('current_track_meta_data') if curren...
'Volume level of the media player (0..1).'
@property def volume_level(self):
return (self._player_volume / 100.0)
'Return true if volume is muted.'
@property def is_volume_muted(self):
return self._player_volume_muted
'Content ID of current playing media.'
@property def media_content_id(self):
if self._coordinator: return self._coordinator.media_content_id return self._media_content_id
'Content type of current playing media.'
@property def media_content_type(self):
return MEDIA_TYPE_MUSIC
'Duration of current playing media in seconds.'
@property def media_duration(self):
if self._coordinator: return self._coordinator.media_duration return self._media_duration
'Position of current playing media in seconds.'
@property def media_position(self):
if self._coordinator: return self._coordinator.media_position return self._media_position
'When was the position of the current playing media valid. Returns value from homeassistant.util.dt.utcnow().'
@property def media_position_updated_at(self):
if self._coordinator: return self._coordinator.media_position_updated_at return self._media_position_updated_at
'Image url of current playing media.'
@property def media_image_url(self):
if self._coordinator: return self._coordinator.media_image_url return self._media_image_url
'Artist of current playing media, music track only.'
@property def media_artist(self):
if self._coordinator: return self._coordinator.media_artist return self._media_artist
'Album name of current playing media, music track only.'
@property def media_album_name(self):
if self._coordinator: return self._coordinator.media_album_name return self._media_album_name
'Title of current playing media.'
@property def media_title(self):
if self._coordinator: return self._coordinator.media_title return self._media_title
'Flag media player features that are supported.'
@property def supported_features(self):
if self._coordinator: return self._coordinator.supported_features supported = SUPPORT_SONOS if (not self._support_previous_track): supported = (supported ^ SUPPORT_PREVIOUS_TRACK) if (not self._support_next_track): supported = (supported ^ SUPPORT_NEXT_TRACK) if (not self._su...
'Volume up media player.'
@soco_error def volume_up(self):
self._player.volume += self.volume_increment
'Volume down media player.'
@soco_error def volume_down(self):
self._player.volume -= self.volume_increment
'Set volume level, range 0..1.'
@soco_error def set_volume_level(self, volume):
self._player.volume = str(int((volume * 100)))
'Mute (true) or unmute (false) media player.'
@soco_error def mute_volume(self, mute):
self._player.mute = mute
'Select input source.'
@soco_error @soco_coordinator def select_source(self, source):
if (source == SUPPORT_SOURCE_LINEIN): self._source_name = SUPPORT_SOURCE_LINEIN self._player.switch_to_line_in() elif (source == SUPPORT_SOURCE_TV): self._source_name = SUPPORT_SOURCE_TV self._player.switch_to_tv() else: fav = [fav for fav in self._favorite_sources if...
'Replace queue with playlist represented by src. Playlists can\'t be played directly with the self._player.play_uri API as they are actually composed of mulitple URLs. Until soco has suppport for playing a playlist, we\'ll need to parse the playlist item and replace the current queue in order to play it.'
def _replace_queue_with_playlist(self, src):
import soco import xml.etree.ElementTree as ET root = ET.fromstring(src['meta']) namespaces = {'item': 'urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/', 'desc': 'urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/'} desc = root.find('item:item', namespaces).find('desc:desc', namespaces).text res = [soco....
'List of available input sources.'
@property def source_list(self):
if self._coordinator: return self._coordinator.source_list model_name = self._speaker_info['model_name'] sources = [] if self._favorite_sources: for fav in self._favorite_sources: sources.append(fav['title']) if ('PLAY:5' in model_name): sources += [SUPPORT_SOURCE...
'Name of the current input source.'
@property def source(self):
if self._coordinator: return self._coordinator.source return self._source_name
'Turn off media player.'
@soco_error def turn_off(self):
if self._support_stop: self.media_stop()
'Send play command.'
@soco_error @soco_filter_upnperror(UPNP_ERRORS_TO_IGNORE) @soco_coordinator def media_play(self):
self._player.play()
'Send stop command.'
@soco_error @soco_filter_upnperror(UPNP_ERRORS_TO_IGNORE) @soco_coordinator def media_stop(self):
self._player.stop()
'Send pause command.'
@soco_error @soco_filter_upnperror(UPNP_ERRORS_TO_IGNORE) @soco_coordinator def media_pause(self):
self._player.pause()
'Send next track command.'
@soco_error @soco_coordinator def media_next_track(self):
self._player.next()
'Send next track command.'
@soco_error @soco_coordinator def media_previous_track(self):
self._player.previous()
'Send seek command.'
@soco_error @soco_coordinator def media_seek(self, position):
self._player.seek(str(datetime.timedelta(seconds=int(position))))
'Clear players playlist.'
@soco_error @soco_coordinator def clear_playlist(self):
self._player.clear_queue()
'Turn the media player on.'
@soco_error def turn_on(self):
if self.support_play: self.media_play()
'Send the play_media command to the media player. If ATTR_MEDIA_ENQUEUE is True, add `media_id` to the queue.'
@soco_error @soco_coordinator def play_media(self, media_type, media_id, **kwargs):
if kwargs.get(ATTR_MEDIA_ENQUEUE): from soco.exceptions import SoCoUPnPException try: self._player.add_uri_to_queue(media_id) except SoCoUPnPException: _LOGGER.error('Error parsing media uri "%s", please check it\'s a valid media resou...
'Join the player to a group.'
@soco_error def join(self, master):
coord = [device for device in self.hass.data[DATA_SONOS] if (device.entity_id == master)] if (coord and (master != self.entity_id)): coord = coord[0] if (coord.soco.group.coordinator != coord.soco): coord.soco.unjoin() self._player.join(coord.soco) self._coordinator =...
'Unjoin the player from a group.'
@soco_error def unjoin(self):
self._player.unjoin() self._coordinator = None
'Snapshot the player.'
@soco_error def snapshot(self, with_group=True):
from soco.snapshot import Snapshot self._soco_snapshot = Snapshot(self._player) self._soco_snapshot.snapshot() if with_group: self._snapshot_group = self._player.group if self._coordinator: self._coordinator.snapshot(False) else: self._snapshot_group = None
'Restore snapshot for the player.'
@soco_error def restore(self, with_group=True):
from soco.exceptions import SoCoException try: self._soco_snapshot.restore(False) except (TypeError, AttributeError, SoCoException): _LOGGER.debug('Error on restore %s', self.entity_id) if (with_group and self._snapshot_group): old = self._snapshot_group actual =...
'Set the timer on the player.'
@soco_error @soco_coordinator def set_sleep_timer(self, sleep_time):
self._player.set_sleep_timer(sleep_time)
'Clear the timer on the player.'
@soco_error @soco_coordinator def clear_sleep_timer(self):
self._player.set_sleep_timer(None)
'Set the alarm clock on the player.'
@soco_error @soco_coordinator def update_alarm(self, **data):
from soco import alarms a = None for alarm in alarms.get_alarms(self.soco): if (alarm._alarm_id == str(data[ATTR_ALARM_ID])): a = alarm if (a is None): _LOGGER.warning('did not find alarm with id %s', data[ATTR_ALARM_ID]) return if (ATTR_TIME in ...
'Return device specific state attributes.'
@property def device_state_attributes(self):
return {ATTR_IS_COORDINATOR: self.is_coordinator}
'Initialize the Denon device.'
def __init__(self, name, host):
self._name = name self._host = host self._pwstate = 'PWSTANDBY' self._volume = 0 self._volume_max = 60 self._source_list = NORMAL_INPUTS.copy() self._source_list.update(MEDIA_MODES) self._muted = False self._mediasource = '' self._mediainfo = '' self._should_setup_sources = T...
'Execute `command` and return the response.'
@classmethod def telnet_request(cls, telnet, command, all_lines=False):
_LOGGER.debug('Sending: %s', command) telnet.write((command.encode('ASCII') + '\r')) lines = [] while True: line = telnet.read_until('\r', timeout=0.2) if (not line): break lines.append(line.decode('ASCII').strip()) _LOGGER.debug('Recived: %s', line) ...
'Establish a telnet connection and sends `command`.'
def telnet_command(self, command):
telnet = telnetlib.Telnet(self._host) _LOGGER.debug('Sending: %s', command) telnet.write((command.encode('ASCII') + '\r')) telnet.read_very_eager() telnet.close()
'Get the latest details from the device.'
def update(self):
try: telnet = telnetlib.Telnet(self._host) except OSError: return False if self._should_setup_sources: self._setup_sources(telnet) self._should_setup_sources = False self._pwstate = self.telnet_request(telnet, 'PW?') for line in self.telnet_request(telnet, 'MV?', all_...
'Return the name of the device.'
@property def name(self):
return self._name
'Return the state of the device.'
@property def state(self):
if (self._pwstate == 'PWSTANDBY'): return STATE_OFF if (self._pwstate == 'PWON'): return STATE_ON return STATE_UNKNOWN
'Volume level of the media player (0..1).'
@property def volume_level(self):
return (self._volume / self._volume_max)
'Return boolean if volume is currently muted.'
@property def is_volume_muted(self):
return self._muted
'Return the list of available input sources.'
@property def source_list(self):
return sorted(list(self._source_list.keys()))
'Return the current media info.'
@property def media_title(self):
return self._mediainfo
'Flag media player features that are supported.'
@property def supported_features(self):
if (self._mediasource in MEDIA_MODES.values()): return (SUPPORT_DENON | SUPPORT_MEDIA_MODES) return SUPPORT_DENON
'Return the current input source.'
@property def source(self):
for (pretty_name, name) in self._source_list.items(): if (self._mediasource == name): return pretty_name
'Turn off media player.'
def turn_off(self):
self.telnet_command('PWSTANDBY')
'Volume up media player.'
def volume_up(self):
self.telnet_command('MVUP')
'Volume down media player.'
def volume_down(self):
self.telnet_command('MVDOWN')
'Set volume level, range 0..1.'
def set_volume_level(self, volume):
self.telnet_command(('MV' + str(round((volume * self._volume_max))).zfill(2)))
'Mute (true) or unmute (false) media player.'
def mute_volume(self, mute):
self.telnet_command(('MU' + ('ON' if mute else 'OFF')))
'Play media media player.'
def media_play(self):
self.telnet_command('NS9A')
'Pause media player.'
def media_pause(self):
self.telnet_command('NS9B')
'Pause media player.'
def media_stop(self):
self.telnet_command('NS9C')
'Send the next track command.'
def media_next_track(self):
self.telnet_command('NS9D')
'Send the previous track command.'
def media_previous_track(self):
self.telnet_command('NS9E')
'Turn the media player on.'
def turn_on(self):
self.telnet_command('PWON')
'Select input source.'
def select_source(self, source):
self.telnet_command(('SI' + self._source_list.get(source)))
'Initialize the MPD device.'
def __init__(self, server, port, password, name):
import mpd self.server = server self.port = port self._name = name self.password = password self._status = None self._currentsong = None self._playlists = [] self._currentplaylist = None self._is_connected = False self._client = mpd.MPDClient() self._client.timeout = 5 ...
'Connect to MPD.'
def _connect(self):
import mpd try: self._client.connect(self.server, self.port) except mpd.ConnectionError: return self._is_connected = True
'Disconnect from MPD.'
def _disconnect(self):
import mpd try: self._client.disconnect() except mpd.ConnectionError: pass self._is_connected = False self._status = None
'Fetch status from MPD.'
def _fetch_status(self):
self._status = self._client.status() self._currentsong = self._client.currentsong() self._update_playlists()
'True if MPD is available and connected.'
@property def available(self):
return self._is_connected
'Get the latest data and update the state.'
def update(self):
import mpd try: if (not self._is_connected): self._connect() self._fetch_status() except (mpd.ConnectionError, OSError, BrokenPipeError, ValueError): self._disconnect()
'Return the name of the device.'
@property def name(self):
return self._name
'Return the media state.'
@property def state(self):
if (self._status is None): return STATE_OFF elif (self._status['state'] == 'play'): return STATE_PLAYING elif (self._status['state'] == 'pause'): return STATE_PAUSED return STATE_ON
'Return the content ID of current playing media.'
@property def media_content_id(self):
return self._currentsong.get('file')
'Return the content type of current playing media.'
@property def media_content_type(self):
return MEDIA_TYPE_MUSIC
'Return the duration of current playing media in seconds.'
@property def media_duration(self):
return self._currentsong.get('time')
'Return the title of current playing media.'
@property def media_title(self):
name = self._currentsong.get('name', None) title = self._currentsong.get('title', None) if ((name is None) and (title is None)): return 'None' elif (name is None): return title elif (title is None): return name return '{}: {}'.format(name, title)
'Return the artist of current playing media (Music track only).'
@property def media_artist(self):
return self._currentsong.get('artist')
'Return the album of current playing media (Music track only).'
@property def media_album_name(self):
return self._currentsong.get('album')
'Return the volume level.'
@property def volume_level(self):
return (int(self._status['volume']) / 100)