def __init__(self, ipadress, macadress, psk, apikey, jeedomadress):
     self._ipadress = ipadress
     self._macadress = macadress
     self._psk = psk
     self._apikey = apikey
     self._jeedomadress = jeedomadress
     self._braviainstance = BraviaRC(self._ipadress, self._macadress)
     if self._braviainstance.connect(psk, 'Jeedom', 'Jeedom') == False:
         print("Récupération du pin")
         sys.exit()
Ejemplo n.º 2
0
class BraviaQuery:
    def __init__(self, ip, psk):
        self.ip = ip
        self.psk = psk
        self.mac = 'AA:BB:CC:DD:EE:FF'

        self.bravia = BraviaRC(self.ip, self.psk, self.mac)

    def poll(self):
        power_status = self.bravia.get_power_status()
        led_status = 'off' if (self.bravia.get_led_status()
                               == 'SimpleResponse') else 'on'

        return {'Power': power_status(), 'LED': led_status}

    def setMute(self, isOn):
        mode = "Demo" if str(isOn).lower() == "on" else "SimpleResponse"
        print("setting LED to " + str(str(isOn).lower() == "on").lower() +
              " with mode " + mode)
        self.bravia.set_led_status(mode,
                                   str(str(isOn).lower() == "on").lower())

        print("setting AudioMute OSD with " + str(str(isOn).lower() == "on"))
        self.bravia.set_audio_mute(str(isOn).lower() == "on")

    def setVolume(self, level):
        print("setting volume to " + str(level))
        self.bravia.set_volume_level(level / 100)
class SonyBravia:
    """ Fetch teleinformation datas and call user callback
	each time all data are collected
	"""
    def __init__(self, ipadress, macadress, psk, apikey, jeedomadress):
        self._ipadress = ipadress
        self._macadress = macadress
        self._psk = psk
        self._apikey = apikey
        self._jeedomadress = jeedomadress
        self._braviainstance = BraviaRC(self._ipadress, self._macadress)
        if self._braviainstance.connect(psk, 'Jeedom', 'Jeedom') == False:
            print("Récupération du pin")
            sys.exit()

    def run(self):
        Donnees = {}
        _Donnees = {}
        Sources = {}
        Apps = {}
        _RAZ = datetime.now()
        _RazCalcul = 0
        _Separateur = "&"
        _tmp = ""
        _SendData = ""

        def target():
            self.process = None
            #print (self.cmd + _SendData)
            self.process = subprocess.Popen(self.cmd + _SendData, shell=True)
            self.process.communicate()
            self.timer.cancel()

        def timer_callback():
            if self.process.poll() is None:
                try:
                    self.process.kill()
                except OSError as error:
                    print("Error: %s " % error)
                print("Thread terminated")
            else:
                print("Thread not alive")

        tvstatus = ""
        try:
            Sources = self._braviainstance.load_source_list()
            Apps = self._braviainstance.load_app_list()
            for cle, valeur in Sources.items():
                _tmp += cle.replace(' ', '%20')
                _tmp += "|"
            #print (_tmp)
            Donnees["sources"] = _tmp
            _tmp = ""
            for cle, valeur in Apps.items():
                _tmp += cle.replace(' ', '%20') + "|"
            #print (_tmp)
            _tmp = _tmp.replace('&', '%26')
            _tmp = _tmp.replace('\'', '%27')
            Donnees["apps"] = _tmp
        except Exception:
            errorCom = "Connection error"
        while (1):
            _RazCalcul = datetime.now() - _RAZ
            if (_RazCalcul.seconds > 8):
                _RAZ = datetime.now()
                del Donnees
                del _Donnees
                Donnees = {}
                _Donnees = {}
            _SendData = ""
            try:
                tvstatus = self._braviainstance.get_power_status()
                Donnees["status"] = tvstatus
            except KeyError:
                print('TV not found')
                sys.exit()
            if tvstatus == 'active':
                try:
                    tvinfo = self._braviainstance.get_system_info()
                    Donnees["model"] = tvinfo['model']
                except:
                    print('Model not found')
                try:
                    vol = self._braviainstance.get_volume_info()
                    Donnees["volume"] = str(vol['volume'])
                except:
                    print('Volume not found')
                try:
                    tvPlaying = self._braviainstance.get_playing_info()
                    #print (tvPlaying)
                    if not tvPlaying:
                        Donnees["source"] = "Application"
                        Donnees["program"] = ""
                        Donnees["nom_chaine"] = ""
                        Donnees["debut"] = ""
                        Donnees["debut_p"] = ""
                        Donnees["fin_p"] = ''
                        Donnees["pourcent_p"] = '0'
                        Donnees["duree"] = ""
                        Donnees["chaine"] = ""
                    else:
                        Donnees["source"] = (
                            (tvPlaying['source'])[-4:]).upper() + (
                                tvPlaying['uri'])[-1:]
                        try:
                            if tvPlaying['dispNum'] is not None:
                                Donnees["chaine"] = tvPlaying[
                                    'dispNum'].replace(' ', '%20').replace(
                                        '\'', '%27').replace('é', '%C3%A9')
                        except:
                            print('not found')
                        try:
                            if tvPlaying['programTitle'] is not None:
                                Donnees["program"] = tvPlaying[
                                    'programTitle'].replace(
                                        ' ', '%20').replace('é',
                                                            '%C3%A9').replace(
                                                                '\'', '%27')
                        except:
                            print('program not found')
                        try:
                            if tvPlaying['title'] is not None:
                                Donnees["nom_chaine"] = tvPlaying[
                                    'title'].replace(' ', '%20').replace(
                                        '\'', '%27').replace('é', '%C3%A9')
                        except:
                            print('not found')
                        try:
                            if tvPlaying['startDateTime'] is not None:
                                if tvPlaying['startDateTime'] != '':
                                    Donnees["debut"] = tvPlaying[
                                        'startDateTime']
                                    Donnees["debut_p"], Donnees["fin_p"], Donnees[
                                        "pourcent_p"] = self._braviainstance.playing_time(
                                            tvPlaying['startDateTime'],
                                            tvPlaying['durationSec'])
                                else:
                                    Donnees["debut_p"] = ''
                                    Donnees["fin_p"] = ''
                                    Donnees["pourcent_p"] = '0'
                        except:
                            print('start date not found')
                        try:
                            if tvPlaying['durationSec'] is not None:
                                if tvPlaying['durationSec'] != '':
                                    Donnees["duree"] = str(
                                        tvPlaying['durationSec'])
                                else:
                                    Donnees["duree"] = '0'
                        except:
                            print('duration not found')
                except:
                    print('Playing Info not found')
            self.cmd = "curl -L -s -G --max-time 15 " + self._jeedomadress + " -d 'apikey=" + self._apikey + "&mac=" + self._macadress
            for cle, valeur in Donnees.items():
                if (cle in _Donnees):
                    if (Donnees[cle] != _Donnees[cle]):
                        _SendData += _Separateur + cle + '=' + valeur
                        _Donnees[cle] = valeur
                else:
                    _SendData += _Separateur + cle + '=' + valeur
                    _Donnees[cle] = valeur
            _SendData += "'"
            if _SendData != "'":
                try:
                    thread = threading.Thread(target=target)
                    self.timer = threading.Timer(int(5), timer_callback)
                    self.timer.start()
                    thread.start()
                except:
                    errorCom = "Connection error"
            time.sleep(2)

    def exit_handler(self, *args):
        self.terminate()
Ejemplo n.º 4
0
    try:
        psk = options.psk
    except:
        print('Erreur psk de la tv')
if options.command_type:
    try:
        command_type = options.command_type
    except:
        print('Erreur de commande')
if options.command_param:
    try:
        command_param = options.command_param
    except:
        print('Erreur de parametres')

_braviainstance = BraviaRC(ip, mac)
_braviainstance.connect(psk, 'Jeedom', 'Jeedom')

try:
    if command_type == 'turn_on':
        _braviainstance.turn_on()
    if command_type == 'turn_off':
        _braviainstance.turn_off()
    if command_type == 'select_source':
        _braviainstance.select_source(command_param)
    if command_type == 'set_volume':
        _braviainstance.set_volume_level(command_param)
    if command_type == 'start_app':
        _braviainstance.start_app(command_param)
    if command_type == 'volume_up':
        _braviainstance.volume_up()
Ejemplo n.º 5
0
    def __init__(self, ip, psk):
        self.ip = ip
        self.psk = psk
        self.mac = 'AA:BB:CC:DD:EE:FF'

        self.bravia = BraviaRC(self.ip, self.psk, self.mac)