if __name__ == "__main__": device = load_device() if not device: # device must be on for registration host = "192.168.178.23" device = SonyDevice(host, "Test123") device.register() pin = input("Enter the PIN displayed at your device: ") if device.send_authentication(pin): save_device() else: print("Registration failed") exit(1) # wake device is_on = device.get_power_status() if not is_on: device.power(True) status = device.get_playing_status() apps = device.get_apps() device.pause() for app in device.apps: if "youtube" in app.lower(): device.start_app(app) device.get_playing_status() # Play media device.play()
class SonyMediaPlayerDevice(MediaPlayerDevice): """Representation of a Sony mediaplayer.""" def __init__(self, host, name, pin, mac=None): """ Initialize the Sony mediaplayer device. Mac address is optional but neccessary for wake on LAN """ from sonyapilib.device import SonyDevice self._pin = pin self.sonydevice = SonyDevice(host, name) self._name = name self._state = STATE_OFF self._muted = False self._id = None self._playing = False self.sonydevice.pin = pin self.sonydevice.mac = mac try: self.sonydevice.update_service_urls() self.update() except Exception: # pylint: disable=broad-except self._state = STATE_OFF def update(self): """Update TV info.""" if not self.sonydevice.get_power_status(): self._state = STATE_OFF return self._state = STATE_ON # Retrieve the latest data. try: if self._state == STATE_ON: power_status = self.sonydevice.get_power_status() if power_status: playback_info = self.sonydevice.get_playing_status() if playback_info == "PLAYING": self._state = STATE_PLAYING elif playback_info == "PAUSED_PLAYBACK": self._state = STATE_PAUSED else: self._state = STATE_ON else: self._state = STATE_OFF except Exception as exception_instance: # pylint: disable=broad-except _LOGGER.error(exception_instance) self._state = STATE_OFF @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_SONY @property def media_title(self): """Title of current playing media.""" # the device used for testing does not send any # information about the media which is played return "" @property def media_content_id(self): """Content ID of current playing media.""" return "" @property def media_duration(self): """Duration of current playing media in seconds.""" return "" def turn_on(self): """Turn the media player on.""" self.sonydevice.power(True) def turn_off(self): """Turn off media player.""" self.sonydevice.power(False) def media_play_pause(self): """Simulate play pause media player.""" if self._playing: self.media_pause() else: self.media_play() def media_play(self): """Send play command.""" self._state = STATE_PLAYING self.sonydevice.play() def media_pause(self): """Send media pause command to media player.""" self._state = STATE_PAUSED self.sonydevice.pause() def media_next_track(self): """Send next track command.""" self.sonydevice.next() def media_previous_track(self): """Send the previous track command.""" self.sonydevice.prev() def media_stop(self): """Send stop command.""" self.sonydevice.stop()