Beispiel #1
0
def main():
    """ main function """
    conf = ConfigParser.ConfigParser()
    conf.read('tradfri.cfg')

    hubip = conf.get('tradfri', 'hubip')
    securityid = conf.get('tradfri', 'securityid')

    lightbulb = []
    lightgroup = []

    print('[ ] Tradfri: acquiring all Tradfri devices, please wait ...')
    devices = tradfriStatus.tradfri_get_devices(hubip, securityid)
    groups = tradfriStatus.tradfri_get_groups(hubip, securityid)

    for deviceid in tqdm(range(len(devices)), desc='Tradfri lightbulbs', unit=' lightbulb'):
        lightbulb.append(tradfriStatus.tradfri_get_lightbulb(hubip, securityid,
                                                             str(devices[deviceid])))

    # sometimes the request are to fast, the will decline the request (flood security)
    # in this case you could increse the sleep timer
    time.sleep(.5)

    for groupid in tqdm(range(len(groups)), desc='Tradfri groups', unit=' group'):
        lightgroup.append(tradfriStatus.tradfri_get_group(hubip, securityid,
                                                          str(groups[groupid])))

    print('[+] Tradfri: device information gathered')
    print('===========================================================\n')

    for _ in range(len(lightbulb)):
        try:
            if lightbulb[_]["3311"][0]["5850"] == 0:
                print('bulb ID {}, name: {}, brightness: {}, state: off'
                      .format(lightbulb[_]["9003"], lightbulb[_]["9001"],
                              lightbulb[_]["3311"][0]["5851"]))
            else:
                print('bulb ID {}, name: {}, brightness: {}, state: on'
                      .format(lightbulb[_]["9003"], lightbulb[_]["9001"],
                              lightbulb[_]["3311"][0]["5851"]))
        except KeyError:
            # device is not a lightbulb but a remote control, dimmer or sensor
            pass

    print('\n')

    for _ in range(len(lightgroup)):
        if lightgroup[_]["5850"] == 0:
            print('group ID: {}, name: {}, state: off'
                  .format(lightgroup[_]["9003"], lightgroup[_]["9001"]))
        else:
            print('group ID: {}, name: {}, state: on'
                  .format(lightgroup[_]["9003"], lightgroup[_]["9001"]))
def main():
    """ main function """
    conf = ConfigParser.ConfigParser()
    conf.read('tradfri.cfg')

    hubip = conf.get('tradfri', 'hubip')
    securityid = conf.get('tradfri', 'securityid')

    lightbulb = []
    lightgroup = []

    print('[ ] tradfri: requireing all tradfri devices, please wait ...')
    devices = tradfriStatus.tradfri_get_devices(hubip, securityid)
    groups = tradfriStatus.tradfri_get_groups(hubip, securityid)

    for deviceid in tqdm(range(len(devices)), desc='tradfri lightbulbs', unit=' lightbulb'):
        lightbulb.append(tradfriStatus.tradfri_get_lightbulb(hubip, securityid,
                                                             str(devices[deviceid])))

    # sometimes the request are to fast, the will decline the request (flood security)
    # in this case you could increse the sleep timer
    time.sleep(.5)

    for groupid in tqdm(range(len(groups)), desc='tradfri groups', unit=' group'):
        lightgroup.append(tradfriStatus.tradfri_get_group(hubip, securityid,
                                                          str(groups[groupid])))

    print('[+] tradfri: device information gathered')
    print('===========================================================\n')

    for _ in range(len(lightbulb)):
        try:
            if lightbulb[_]["3311"][0]["5850"] == 0:
                print('bulbid {}, name: {}, bightness: {}, state: off'
                      .format(lightbulb[_]["9003"], lightbulb[_]["9001"],
                              lightbulb[_]["3311"][0]["5851"]))
            else:
                print('bulbid {}, name: {}, bightness: {}, state: on'
                      .format(lightbulb[_]["9003"], lightbulb[_]["9001"],
                              lightbulb[_]["3311"][0]["5851"]))
        except KeyError:
            # device is not a lightbulb but a remote control, dimmer or sensor
            pass

    print('\n')

    for _ in range(len(lightgroup)):
        if lightgroup[_]["5850"] == 0:
            print('groupid: {}, name: {}, state: off'
                  .format(lightgroup[_]["9003"], lightgroup[_]["9001"]))
        else:
            print('groupid: {}, name: {}, state: on'
                  .format(lightgroup[_]["9003"], lightgroup[_]["9001"]))
def main():
    """ main function """
    conf = ConfigParser.ConfigParser()
    script_dir = os.path.dirname(os.path.realpath(__file__))
    conf.read(script_dir + '/tradfri.cfg')

    hubip = conf.get('tradfri', 'hubip')
    apiuser = conf.get('tradfri', 'apiuser')
    apikey = conf.get('tradfri', 'apikey')

    lightbulb = []
    lightgroup = []

    print('[ ] Tradfri: acquiring all Tradfri devices, please wait ...')
    devices = tradfriStatus.tradfri_get_devices(hubip, apiuser, apikey)
    groups = tradfriStatus.tradfri_get_groups(hubip, apiuser, apikey)

    for deviceid in tqdm(range(len(devices)), desc='Tradfri devices', unit=' devices'):
        lightbulb.append(tradfriStatus.tradfri_get_lightbulb(hubip, apiuser, apikey,
                                                             str(devices[deviceid])))

    # sometimes the request are to fast, the will decline the request (flood security)
    # in this case you could increse the sleep timer
    time.sleep(.5)

    for groupid in tqdm(range(len(groups)), desc='Tradfri groups', unit=' group'):
        lightgroup.append(tradfriStatus.tradfri_get_group(hubip, apiuser, apikey,
                                                          str(groups[groupid])))

    print('[+] Tradfri: device information gathered')
    print('===========================================================\n')
    for _ in range(len(lightbulb)):
        try:
            brightness = lightbulb[_]["3311"][0]["5851"]
            try:
                warmth     = float(lightbulb[_]["3311"][0]["5711"])
                warmth     = round((warmth-250)/(454-250)*100,1)# reported as a percentage (100% maximum warmth)
            except KeyError:
                warmth = "NAN"

            if lightbulb[_]["3311"][0]["5850"] == 0:
                print('bulb ID {0:<5}, name: {1: <35}, brightness: {2: <3}, warmth: {3: >5}%, state: off'
                      .format(lightbulb[_]["9003"], lightbulb[_]["9001"],
                              brightness,warmth))
            else:
                print('bulb ID {0:<5}, name: {1: <35}, brightness: {2: <3}, warmth: {3: >5}%, state: on'
                      .format(lightbulb[_]["9003"], lightbulb[_]["9001"],
                              brightness,warmth))
        except KeyError:
            # device is not a lightbulb but a remote control, dimmer or sensor
            pass

    print('\n')

    for _ in range(len(lightgroup)):
        if lightgroup[_]["5850"] == 0:
            print('group ID: {0:<5}, name: {1: <16}, state: off'
                  .format(lightgroup[_]["9003"], lightgroup[_]["9001"]))
        else:
            print('group ID: {0:<5}, name: {1: <16}, state: on'
                  .format(lightgroup[_]["9003"], lightgroup[_]["9001"]))
Beispiel #4
0
def get_groups(grouplist):
    firstgroup = True
    firstbulb = True
    bulbreport = "{\"bulbs\":["
    groupreport = "{\"groups\":["
    for thisgroup in grouplist:
        groupstat = tradfriStatus.tradfri_get_group(gatewayurl, username, userkey, thisgroup.id)
        if groupstat != "":
            gpow = "OFF"
            
            thisgroup.name = groupstat["9001"]
            print("Read group {}".format(thisgroup.name))
            if int(groupstat["5850"]) > 0:
                thisgroup.power = "ON"
            else:
                thisgroup.power = "OFF"
            bright = groupstat["5851"]
            bright = (bright / 255) * 100 
            thisgroup.brightness = "{}".format(int(bright))
            if firstgroup is False:
                groupreport += ","
            firstgroup = False
            groupreport += "{{\"id\":\"{}\",\"name\":\"{}\"}}".format(thisgroup.id, thisgroup.name)
            devl = groupstat["9018"]["15002"]["9003"]
            for dev in devl:
                bulbstat = tradfriStatus.tradfri_get_lightbulb(gatewayurl, username, userkey, dev)
                onoff = "OFF"
                power = "OFF"
                try:
                    if bulbstat["3311"][0]["5850"] == 1:
                        onoff = "ON"
                    if bulbstat["9019"] == 1:
                        power = "ON"
                        gpower = "ON"
                except KeyError:
                    # device is not a lightbulb but a remote control, dimmer or sensor
                    continue
                print('Got bulb - ID: {}, name: {}, power {}, brightness: {}, state: {}'.format(bulbstat["9003"], bulbstat["9001"], power, bulbstat["3311"][0]["5851"], onoff))
                newdevice = tradfridevice()
                newdevice.id = bulbstat["9003"]
                newdevice.name = bulbstat["9001"]
                newdevice.power = onoff
                bright = bulbstat["3311"][0]["5851"]
                bright = (bright / 255) * 100 
                newdevice.brightness = "{}".format(int(bright))
                
                try:
                    valx = bulbstat["3311"][0]["5709"]
                    #valy = bulbstat["3311"][0]["5710"]
                    newdevice.isspectrum = True
                    valx -= 24930
                    #valy -= 24694
                    pc = valx / 82.05
                    newdevice.colour = int(pc)
                    print("Spectrum bulb temp {}%".format(newdevice.colour))
                except KeyError:    
                    newdevice.colour = 0
                    print("Not a spectrum bulb")
                    pass

                newdevice.rgb = False
                # no power reported for group so we derive from ANY of the bulbs
                thisgroup.active = gpower
                thisgroup.devices.append(newdevice)
                if firstbulb is False:
                    bulbreport += ","
                firstbulb = False
                bulbreport += '{{"id":"{}","name":"{}"}}'.format(newdevice.id, newdevice.name)
    groupreport += "]}"
    bulbreport += "]}"
    print("Publish bulblist - {}".format(bulbreport))
    client.publish("tradfri/bulblist", bulbreport, 0)
    print("Publish grouplist - {}".format(groupreport))
    client.publish("tradfri/grouplist", groupreport, 0)
Beispiel #5
0
     continue
 
 for thisgroup in groups:
     gonoff = "OFF"
     gpow = "OFF"
     gbrightness = 0
     gnumdevs = 0
     
     if thisgroup.devices is not None:
         for thisdev in thisgroup.devices:
     
             onoff = "OFF"
             power = "OFF"
             #print("Main get mutex")
             mutex.acquire()
             bulbstat = tradfriStatus.tradfri_get_lightbulb(gatewayurl, username, userkey, thisdev.id)
             mutex.release()
             #print("Main release mutex")
             #print('bulbstat is {}',format(bulbstat))
             if bulbstat != "":
                 try:
                     if bulbstat["3311"][0]["5850"] == 1:
                         onoff = "ON"
                     if bulbstat["9019"] == 1:
                         power = "ON"
                     bright = float(bulbstat["3311"][0]["5851"])
                     
                 except KeyError:
                     # Problem with the returned json - skip
                     print("Error polling bulb {}:{}".format(thisdev.name, thisdev.id))
                     continue