class MediaroomDevice(MediaPlayerEntity): """Representation of a Mediaroom set-up-box on the network.""" _attr_should_poll = False _attr_supported_features = (MediaPlayerEntityFeature.PAUSE | MediaPlayerEntityFeature.TURN_ON | MediaPlayerEntityFeature.TURN_OFF | MediaPlayerEntityFeature.VOLUME_STEP | MediaPlayerEntityFeature.VOLUME_MUTE | MediaPlayerEntityFeature.PLAY_MEDIA | MediaPlayerEntityFeature.STOP | MediaPlayerEntityFeature.NEXT_TRACK | MediaPlayerEntityFeature.PREVIOUS_TRACK | MediaPlayerEntityFeature.PLAY) def set_state(self, mediaroom_state): """Map pymediaroom state to HA state.""" state_map = { State.OFF: STATE_OFF, State.STANDBY: STATE_STANDBY, State.PLAYING_LIVE_TV: STATE_PLAYING, State.PLAYING_RECORDED_TV: STATE_PLAYING, State.PLAYING_TIMESHIFT_TV: STATE_PLAYING, State.STOPPED: STATE_PAUSED, State.UNKNOWN: STATE_UNAVAILABLE, } self._state = state_map[mediaroom_state] def __init__(self, host, device_id, optimistic=False, timeout=DEFAULT_TIMEOUT): """Initialize the device.""" self.host = host self.stb = Remote(host) _LOGGER.info("Found STB at %s%s", host, " - I'm optimistic" if optimistic else "") self._channel = None self._optimistic = optimistic self._state = STATE_PLAYING if optimistic else STATE_STANDBY self._name = f"Mediaroom {device_id if device_id else host}" self._available = True if device_id: self._unique_id = device_id else: self._unique_id = None @property def available(self): """Return True if entity is available.""" return self._available async def async_added_to_hass(self): """Retrieve latest state.""" async def async_notify_received(notify): """Process STB state from NOTIFY message.""" stb_state = self.stb.notify_callback(notify) # stb_state is None in case the notify is not from the current stb if not stb_state: return self.set_state(stb_state) _LOGGER.debug("STB(%s) is [%s]", self.host, self._state) self._available = True self.async_write_ha_state() self.async_on_remove( async_dispatcher_connect(self.hass, SIGNAL_STB_NOTIFY, async_notify_received)) async def async_play_media(self, media_type, media_id, **kwargs): """Play media.""" _LOGGER.debug("STB(%s) Play media: %s (%s)", self.stb.stb_ip, media_id, media_type) if media_type == MEDIA_TYPE_CHANNEL: if not media_id.isdigit(): _LOGGER.error("Invalid media_id %s: Must be a channel number", media_id) return media_id = int(media_id) elif media_type == MEDIA_TYPE_MEDIAROOM: if media_id not in COMMANDS: _LOGGER.error("Invalid media_id %s: Must be a command", media_id) return else: _LOGGER.error("Invalid media type %s", media_type) return try: await self.stb.send_cmd(media_id) if self._optimistic: self._state = STATE_PLAYING self._available = True except PyMediaroomError: self._available = False self.async_write_ha_state() @property def unique_id(self): """Return a unique ID.""" return self._unique_id @property def name(self): """Return the name of the device.""" return self._name @property def state(self): """Return the state of the device.""" return self._state @property def media_content_type(self): """Return the content type of current playing media.""" return MEDIA_TYPE_CHANNEL @property def media_channel(self): """Channel currently playing.""" return self._channel async def async_turn_on(self): """Turn on the receiver.""" try: self.set_state(await self.stb.turn_on()) if self._optimistic: self._state = STATE_PLAYING self._available = True except PyMediaroomError: self._available = False self.async_write_ha_state() async def async_turn_off(self): """Turn off the receiver.""" try: self.set_state(await self.stb.turn_off()) if self._optimistic: self._state = STATE_STANDBY self._available = True except PyMediaroomError: self._available = False self.async_write_ha_state() async def async_media_play(self): """Send play command.""" try: _LOGGER.debug("media_play()") await self.stb.send_cmd("PlayPause") if self._optimistic: self._state = STATE_PLAYING self._available = True except PyMediaroomError: self._available = False self.async_write_ha_state() async def async_media_pause(self): """Send pause command.""" try: await self.stb.send_cmd("PlayPause") if self._optimistic: self._state = STATE_PAUSED self._available = True except PyMediaroomError: self._available = False self.async_write_ha_state() async def async_media_stop(self): """Send stop command.""" try: await self.stb.send_cmd("Stop") if self._optimistic: self._state = STATE_PAUSED self._available = True except PyMediaroomError: self._available = False self.async_write_ha_state() async def async_media_previous_track(self): """Send Program Down command.""" try: await self.stb.send_cmd("ProgDown") if self._optimistic: self._state = STATE_PLAYING self._available = True except PyMediaroomError: self._available = False self.async_write_ha_state() async def async_media_next_track(self): """Send Program Up command.""" try: await self.stb.send_cmd("ProgUp") if self._optimistic: self._state = STATE_PLAYING self._available = True except PyMediaroomError: self._available = False self.async_write_ha_state() async def async_volume_up(self): """Send volume up command.""" try: await self.stb.send_cmd("VolUp") self._available = True except PyMediaroomError: self._available = False self.async_write_ha_state() async def async_volume_down(self): """Send volume up command.""" try: await self.stb.send_cmd("VolDown") except PyMediaroomError: self._available = False self.async_write_ha_state() async def async_mute_volume(self, mute): """Send mute command.""" try: await self.stb.send_cmd("Mute") except PyMediaroomError: self._available = False self.async_write_ha_state()
class MediaroomDevice(MediaPlayerDevice): """Representation of a Mediaroom set-up-box on the network.""" def set_state(self, mediaroom_state): """Map pymediaroom state to HA state.""" from pymediaroom import State state_map = { State.OFF: STATE_OFF, State.STANDBY: STATE_STANDBY, State.PLAYING_LIVE_TV: STATE_PLAYING, State.PLAYING_RECORDED_TV: STATE_PLAYING, State.PLAYING_TIMESHIFT_TV: STATE_PLAYING, State.STOPPED: STATE_PAUSED, State.UNKNOWN: STATE_UNAVAILABLE } self._state = state_map[mediaroom_state] def __init__( self, host, device_id, optimistic=False, timeout=DEFAULT_TIMEOUT): """Initialize the device.""" from pymediaroom import Remote self.host = host self.stb = Remote(host) _LOGGER.info("Found STB at %s%s", host, " - I'm optimistic" if optimistic else "") self._channel = None self._optimistic = optimistic self._state = STATE_PLAYING if optimistic else STATE_STANDBY self._name = 'Mediaroom {}'.format(device_id if device_id else host) self._available = True if device_id: self._unique_id = device_id else: self._unique_id = None @property def should_poll(self): """No polling needed.""" return False @property def available(self): """Return True if entity is available.""" return self._available async def async_added_to_hass(self): """Retrieve latest state.""" async def async_notify_received(notify): """Process STB state from NOTIFY message.""" stb_state = self.stb.notify_callback(notify) # stb_state is None in case the notify is not from the current stb if not stb_state: return self.set_state(stb_state) _LOGGER.debug("STB(%s) is [%s]", self.host, self._state) self._available = True self.async_schedule_update_ha_state() async_dispatcher_connect( self.hass, SIGNAL_STB_NOTIFY, async_notify_received) async def async_play_media(self, media_type, media_id, **kwargs): """Play media.""" from pymediaroom import PyMediaroomError _LOGGER.debug("STB(%s) Play media: %s (%s)", self.stb.stb_ip, media_id, media_type) if media_type != MEDIA_TYPE_CHANNEL: _LOGGER.error('invalid media type') return if not media_id.isdigit(): _LOGGER.error("media_id must be a channel number") return try: await self.stb.send_cmd(int(media_id)) if self._optimistic: self._state = STATE_PLAYING self._available = True except PyMediaroomError: self._available = False self.async_schedule_update_ha_state() @property def unique_id(self): """Return a unique ID.""" return self._unique_id @property def name(self): """Return the name of the device.""" 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_MEDIAROOM @property def media_content_type(self): """Return the content type of current playing media.""" return MEDIA_TYPE_CHANNEL @property def media_channel(self): """Channel currently playing.""" return self._channel async def async_turn_on(self): """Turn on the receiver.""" from pymediaroom import PyMediaroomError try: self.set_state(await self.stb.turn_on()) if self._optimistic: self._state = STATE_PLAYING self._available = True except PyMediaroomError: self._available = False self.async_schedule_update_ha_state() async def async_turn_off(self): """Turn off the receiver.""" from pymediaroom import PyMediaroomError try: self.set_state(await self.stb.turn_off()) if self._optimistic: self._state = STATE_STANDBY self._available = True except PyMediaroomError: self._available = False self.async_schedule_update_ha_state() async def async_media_play(self): """Send play command.""" from pymediaroom import PyMediaroomError try: _LOGGER.debug("media_play()") await self.stb.send_cmd('PlayPause') if self._optimistic: self._state = STATE_PLAYING self._available = True except PyMediaroomError: self._available = False self.async_schedule_update_ha_state() async def async_media_pause(self): """Send pause command.""" from pymediaroom import PyMediaroomError try: await self.stb.send_cmd('PlayPause') if self._optimistic: self._state = STATE_PAUSED self._available = True except PyMediaroomError: self._available = False self.async_schedule_update_ha_state() async def async_media_stop(self): """Send stop command.""" from pymediaroom import PyMediaroomError try: await self.stb.send_cmd('Stop') if self._optimistic: self._state = STATE_PAUSED self._available = True except PyMediaroomError: self._available = False self.async_schedule_update_ha_state() async def async_media_previous_track(self): """Send Program Down command.""" from pymediaroom import PyMediaroomError try: await self.stb.send_cmd('ProgDown') if self._optimistic: self._state = STATE_PLAYING self._available = True except PyMediaroomError: self._available = False self.async_schedule_update_ha_state() async def async_media_next_track(self): """Send Program Up command.""" from pymediaroom import PyMediaroomError try: await self.stb.send_cmd('ProgUp') if self._optimistic: self._state = STATE_PLAYING self._available = True except PyMediaroomError: self._available = False self.async_schedule_update_ha_state() async def async_volume_up(self): """Send volume up command.""" from pymediaroom import PyMediaroomError try: await self.stb.send_cmd('VolUp') self._available = True except PyMediaroomError: self._available = False self.async_schedule_update_ha_state() async def async_volume_down(self): """Send volume up command.""" from pymediaroom import PyMediaroomError try: await self.stb.send_cmd('VolDown') except PyMediaroomError: self._available = False self.async_schedule_update_ha_state() async def async_mute_volume(self, mute): """Send mute command.""" from pymediaroom import PyMediaroomError try: await self.stb.send_cmd('Mute') except PyMediaroomError: self._available = False self.async_schedule_update_ha_state()
class MediaroomDevice(MediaPlayerDevice): """Representation of a Mediaroom set-up-box on the network.""" def set_state(self, mediaroom_state): """Helper method to map pymediaroom states to HA states.""" from pymediaroom import State state_map = { State.OFF: STATE_OFF, State.STANDBY: STATE_STANDBY, State.PLAYING_LIVE_TV: STATE_PLAYING, State.PLAYING_RECORDED_TV: STATE_PLAYING, State.PLAYING_TIMESHIFT_TV: STATE_PLAYING, State.STOPPED: STATE_PAUSED, State.UNKNOWN: STATE_UNAVAILABLE } self._state = state_map[mediaroom_state] def __init__(self, host, device_id, optimistic=False, timeout=DEFAULT_TIMEOUT): """Initialize the device.""" from pymediaroom import Remote self.host = host self.stb = Remote(host) _LOGGER.info("Found STB at %s%s", host, " - I'm optimistic" if optimistic else "") self._channel = None self._optimistic = optimistic self._state = STATE_PLAYING if optimistic else STATE_STANDBY self._name = 'Mediaroom {}'.format(device_id if device_id else host) self._available = True if device_id: self._unique_id = device_id else: self._unique_id = None @property def should_poll(self): """No polling needed.""" return False @property def available(self): """Return True if entity is available.""" return self._available async def async_added_to_hass(self): """Retrieve latest state.""" async def async_notify_received(notify): """Process STB state from NOTIFY message.""" stb_state = self.stb.notify_callback(notify) # stb_state is None in case the notify is not from the current stb if not stb_state: return self.set_state(stb_state) _LOGGER.debug("STB(%s) is [%s]", self.host, self._state) self._available = True self.async_schedule_update_ha_state() async_dispatcher_connect(self.hass, SIGNAL_STB_NOTIFY, async_notify_received) async def async_play_media(self, media_type, media_id, **kwargs): """Play media.""" from pymediaroom import PyMediaroomError _LOGGER.debug("STB(%s) Play media: %s (%s)", self.stb.stb_ip, media_id, media_type) if media_type != MEDIA_TYPE_CHANNEL: _LOGGER.error('invalid media type') return if not media_id.isdigit(): _LOGGER.error("media_id must be a channel number") return try: await self.stb.send_cmd(int(media_id)) if self._optimistic: self._state = STATE_PLAYING self._available = True except PyMediaroomError: self._available = False self.async_schedule_update_ha_state() @property def unique_id(self): """Return a unique ID.""" return self._unique_id @property def name(self): """Return the name of the device.""" 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_MEDIAROOM @property def media_content_type(self): """Return the content type of current playing media.""" return MEDIA_TYPE_CHANNEL @property def media_channel(self): """Channel currently playing.""" return self._channel async def async_turn_on(self): """Turn on the receiver.""" from pymediaroom import PyMediaroomError try: self.set_state(await self.stb.turn_on()) if self._optimistic: self._state = STATE_PLAYING self._available = True except PyMediaroomError: self._available = False self.async_schedule_update_ha_state() async def async_turn_off(self): """Turn off the receiver.""" from pymediaroom import PyMediaroomError try: self.set_state(await self.stb.turn_off()) if self._optimistic: self._state = STATE_STANDBY self._available = True except PyMediaroomError: self._available = False self.async_schedule_update_ha_state() async def async_media_play(self): """Send play command.""" from pymediaroom import PyMediaroomError try: _LOGGER.debug("media_play()") await self.stb.send_cmd('PlayPause') if self._optimistic: self._state = STATE_PLAYING self._available = True except PyMediaroomError: self._available = False self.async_schedule_update_ha_state() async def async_media_pause(self): """Send pause command.""" from pymediaroom import PyMediaroomError try: await self.stb.send_cmd('PlayPause') if self._optimistic: self._state = STATE_PAUSED self._available = True except PyMediaroomError: self._available = False self.async_schedule_update_ha_state() async def async_media_stop(self): """Send stop command.""" from pymediaroom import PyMediaroomError try: await self.stb.send_cmd('Stop') if self._optimistic: self._state = STATE_PAUSED self._available = True except PyMediaroomError: self._available = False self.async_schedule_update_ha_state() async def async_media_previous_track(self): """Send Program Down command.""" from pymediaroom import PyMediaroomError try: await self.stb.send_cmd('ProgDown') if self._optimistic: self._state = STATE_PLAYING self._available = True except PyMediaroomError: self._available = False self.async_schedule_update_ha_state() async def async_media_next_track(self): """Send Program Up command.""" from pymediaroom import PyMediaroomError try: await self.stb.send_cmd('ProgUp') if self._optimistic: self._state = STATE_PLAYING self._available = True except PyMediaroomError: self._available = False self.async_schedule_update_ha_state() async def async_volume_up(self): """Send volume up command.""" from pymediaroom import PyMediaroomError try: await self.stb.send_cmd('VolUp') self._available = True except PyMediaroomError: self._available = False self.async_schedule_update_ha_state() async def async_volume_down(self): """Send volume up command.""" from pymediaroom import PyMediaroomError try: await self.stb.send_cmd('VolDown') except PyMediaroomError: self._available = False self.async_schedule_update_ha_state() async def async_mute_volume(self, mute): """Send mute command.""" from pymediaroom import PyMediaroomError try: await self.stb.send_cmd('Mute') except PyMediaroomError: self._available = False self.async_schedule_update_ha_state()