示例#1
0
"""Example found in the readme."""
from sonyapilib.device import SonyDevice

if __name__ == "__main__":
    # device must be on for registration
    host = "10.0.0.102"
    device = SonyDevice(host, "SonyApiLib Python Test")
    device.register()
    pin = input("Enter the PIN displayed at your device: ")
    if not device.send_authentication(pin):
        print("Failed to register device")
        exit(1)

    apps = device.get_apps()
    device.start_app(apps[0])
    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()