Exemple #1
0
    def __init__(self, host, mac, name, pin):
        """Initialize the Sony Bravia device."""

        self._pin = pin
        self._braviarc = BraviaRC(host, mac)
        self._name = name
        self._state = STATE_OFF
        self._muted = False
        self._program_name = None
        self._channel_name = None
        self._channel_number = None
        self._source = None
        self._source_list = []
        self._original_content_list = []
        self._content_mapping = {}
        self._duration = None
        self._content_uri = None
        self._id = None
        self._playing = False
        self._start_date_time = None
        self._program_media_type = None
        self._min_volume = None
        self._max_volume = None
        self._volume = None

        self._braviarc.connect(pin, CLIENTID_PREFIX, NICKNAME)
        if self._braviarc.is_connected():
            self.update()
        else:
            self._state = STATE_OFF
Exemple #2
0
def setup():
    global SONY_IP
    setup_network()
    logger.info("setup")

    braviarc = BraviaRC(SONY_IP)
    pin = SONY_PIN
    braviarc.connect(pin, 'sony', 'sonyrc')
Exemple #3
0
    def bravia_configuration_callback(data):
        """Handle the entry of user PIN."""

        pin = data.get("pin")
        _braviarc = BraviaRC(host)
        _braviarc.connect(pin, CLIENTID_PREFIX, NICKNAME)
        if _braviarc.is_connected():
            setup_bravia(config, pin, hass, add_entities)
        else:
            request_configuration(config, hass, add_entities)
Exemple #4
0
class BraviaTVDevice(MediaPlayerDevice):
    """Representation of a Sony Bravia TV."""
    def __init__(self, host, mac, name, pin):
        """Initialize the Sony Bravia device."""

        self._pin = pin
        self._braviarc = BraviaRC(host, mac)
        self._name = name
        self._state = STATE_OFF
        self._muted = False
        self._program_name = None
        self._channel_name = None
        self._channel_number = None
        self._source = None
        self._source_list = []
        self._original_content_list = []
        self._content_mapping = {}
        self._duration = None
        self._content_uri = None
        self._id = None
        self._playing = False
        self._start_date_time = None
        self._program_media_type = None
        self._min_volume = None
        self._max_volume = None
        self._volume = None

        self._braviarc.connect(pin, CLIENTID_PREFIX, NICKNAME)
        if self._braviarc.is_connected():
            self.update()
        else:
            self._state = STATE_OFF

    def update(self):
        """Update TV info."""
        if not self._braviarc.is_connected():
            if self._braviarc.get_power_status() != "off":
                self._braviarc.connect(self._pin, CLIENTID_PREFIX, NICKNAME)
            if not self._braviarc.is_connected():
                return

        # Retrieve the latest data.
        try:
            if self._state == STATE_ON:
                # refresh volume info:
                self._refresh_volume()
                self._refresh_channels()

            power_status = self._braviarc.get_power_status()
            if power_status == "active":
                self._state = STATE_ON
                playing_info = self._braviarc.get_playing_info()
                self._reset_playing_info()
                if playing_info is None or not playing_info:
                    self._channel_name = "App"
                else:
                    self._program_name = playing_info.get("programTitle")
                    self._channel_name = playing_info.get("title")
                    self._program_media_type = playing_info.get(
                        "programMediaType")
                    self._channel_number = playing_info.get("dispNum")
                    self._source = playing_info.get("source")
                    self._content_uri = playing_info.get("uri")
                    self._duration = playing_info.get("durationSec")
                    self._start_date_time = playing_info.get("startDateTime")
            else:
                self._state = STATE_OFF

        except Exception as exception_instance:  # pylint: disable=broad-except
            _LOGGER.error(exception_instance)
            self._state = STATE_OFF

    def _reset_playing_info(self):
        self._program_name = None
        self._channel_name = None
        self._program_media_type = None
        self._channel_number = None
        self._source = None
        self._content_uri = None
        self._duration = None
        self._start_date_time = None

    def _refresh_volume(self):
        """Refresh volume information."""
        volume_info = self._braviarc.get_volume_info()
        if volume_info is not None:
            self._volume = volume_info.get("volume")
            self._min_volume = volume_info.get("minVolume")
            self._max_volume = volume_info.get("maxVolume")
            self._muted = volume_info.get("mute")

    def _refresh_channels(self):
        if not self._source_list:
            self._content_mapping = self._braviarc.load_source_list()
            self._source_list = []
            for key in self._content_mapping:
                self._source_list.append(key)

    @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 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 volume_level(self):
        """Volume level of the media player (0..1)."""
        if self._volume is not None:
            return self._volume / 100
        return None

    @property
    def is_volume_muted(self):
        """Boolean if volume is currently muted."""
        return self._muted

    @property
    def supported_features(self):
        """Flag media player features that are supported."""
        return SUPPORT_BRAVIA

    @property
    def media_title(self):
        """Title of current playing media."""
        return_value = None
        if self._channel_name is not None:
            return_value = self._channel_name
            if self._program_name is not None:
                return_value = f"{return_value}: {self._program_name}"
        return return_value

    @property
    def media_content_id(self):
        """Content ID of current playing media."""
        return self._channel_name

    @property
    def media_duration(self):
        """Duration of current playing media in seconds."""
        return self._duration

    def set_volume_level(self, volume):
        """Set volume level, range 0..1."""
        self._braviarc.set_volume_level(volume)

    def turn_on(self):
        """Turn the media player on."""
        self._braviarc.turn_on()

    def turn_off(self):
        """Turn off media player."""
        self._braviarc.turn_off()

    def volume_up(self):
        """Volume up the media player."""
        self._braviarc.volume_up()

    def volume_down(self):
        """Volume down media player."""
        self._braviarc.volume_down()

    def mute_volume(self, mute):
        """Send mute command."""
        self._braviarc.mute_volume(mute)

    def select_source(self, source):
        """Set the input source."""
        if source in self._content_mapping:
            uri = self._content_mapping[source]
            self._braviarc.play_content(uri)

    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._playing = True
        self._braviarc.media_play()

    def media_pause(self):
        """Send media pause command to media player."""
        self._playing = False
        self._braviarc.media_pause()

    def media_next_track(self):
        """Send next track command."""
        self._braviarc.media_next_track()

    def media_previous_track(self):
        """Send the previous track command."""
        self._braviarc.media_previous_track()
Exemple #5
0
def on_message(client, userdata, msg):
    global SONY_IP
    global SONY_PIN
    #    global CURRENT_POSITION
    tokenized_topic = str(msg.topic).split("/")
    type = tokenized_topic[1]
    element = tokenized_topic[2]
    command = msg.payload.decode("utf-8")

    logger.info("Message type:{} element:{} command:{}".format(
        type, element, command))

    try:
        braviarc = BraviaRC(SONY_IP)
        pin = SONY_PIN
        braviarc.connect(pin, 'sony', 'sonyrc')
        if (braviarc.is_connected()):
            if (element == "RTV_TV_Input"):
                playing_content = braviarc.get_playing_info()
                if (command.startswith("HDMI")):
                    source = command.replace("__", " ")
                    source = source.replace("_", "/")
                    braviarc.select_source(source)
                    logger.info("sent: {}".format(source))
                elif (command.startswith("NETFLIX")):
                    braviarc.start_app("Netflix")
                elif (command.startswith("YOUTUBE")):
                    braviarc.start_app("YouTube")
            if (element.startswith("Command_Netflix")):
                braviarc.start_app("Netflix")
            if (element.startswith("Command_YouTube")):
                braviarc.start_app("YouTube")
            if (element.startswith("Command_TV")):
                braviarc.select_source("HDMI 1/MHL")
            if (element.startswith("Command_XBOX")):
                braviarc.select_source("HDMI 3")
            if (element.startswith("Command_Confirm")):
                braviarc.enter()
        else:
            logger.error("TV not connected")
    except Exception as e:
        logger.info("ERR Message type:{} element:{} command:{}".format(
            type, element, command))
        logger.error(e)
class BraviaMixin(ModuleMixin):
    tv = None

    orders = [{'words': ['traducir', 'traduce', 'tradúceme'],
               'action': 'self.get_translation()',
               'speak': '',
               'orders': None,
               },
              ]

    def __init__(self):
        super().__init__()

        if TV_IP and TV_PIN:
            self.tv = BraviaRC(TV_IP)
            self.tv.connect(TV_PIN, 'my_device_id', 'my device name')

    def get_power_status_tv(self):
        """returns: off, active or standby"""
        return self.tv.get_power_status()

    def turn_on_tv(self):
        self.tv.turn_on()

    def turn_off_tv(self):
        self.tv.turn_off()

    def volume_up_tv(self):
        self.tv.volume_up()

    def volume_down_tv(self):
        self.tv.volume_down()

    def mute_volume_tv(self):
        self.tv.mute_volume(None)

    def get_playing_info_tv(self):
        return self.tv.get_playing_info()['programTitle']

    def media_play_tv(self):
        self.tv.media_play()

    def media_pause_tv(self):
        self.tv.media_pause()

    def media_next_track_tv(self):
        self.tv.media_next_track()

    def media_previous_track_tv(self):
        self.tv.media_previous_track()
    def __init__(self):
        super().__init__()

        if TV_IP and TV_PIN:
            self.tv = BraviaRC(TV_IP)
            self.tv.connect(TV_PIN, 'my_device_id', 'my device name')
Exemple #8
0
    system_dictionary = yaml.load(content)
    SONY_IP = system_dictionary["sony_ip"]


def setup():
    global SONY_IP
    setup_network()
    logger.info("setup")

    braviarc = BraviaRC(SONY_IP)
    pin = SONY_PIN
    braviarc.connect(pin, 'sony', 'sonyrc')


setup()

braviarc = BraviaRC(SONY_IP)
pin = SONY_PIN
braviarc.connect(pin, 'sony', 'sonyrc')
if (braviarc.is_connected()):
    volume_info = braviarc.get_volume_info()
    print(volume_info)
    print('==')
    print(volume_info.get('volume'))
    print('==')
    braviarc.set_volume_level(0.2)
    # braviarc.volume_up()
    print('==')
    system_info = braviarc.get_system_info()
    print(system_info)
Exemple #9
0
#/user/bin/env python3

from braviarc.braviarc import BraviaRC
braviarc = BraviaRC('x9400e')

#connect to the instance (or register)
pin = '8404'
braviarc.connect(pin, 'HomeAssistant', 'Home Assistant')

#check connection
if braviarc.is_connected():

  #get power status
  power_status = braviarc.get_power_status()
  print (power_status)

  #get playing info
  playing_content = braviarc.get_playing_info()

  #print current playing channel
  print (playing_content.get('title'))

  #get volume info
  volume_info = braviarc.get_volume_info()

  #print current volume
  print (volume_info.get('volume'))

  #change channel
  braviarc.play_content('https://www.sample-videos.com/video123/mp4/720/big_buck_bunny_720p_20mb.mp4')