Exemple #1
0
def main():
    """ main function """
    args = parse_args()
    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')

    if args.action == 'power':
        if args.value == 'on' or args.value == 'off':
            tradfriActions.tradfri_power_light(hubip, apiuser, apikey, args.lightbulbid, args.value)
        else:
            sys.stderr.write('[-] Tradfri: power state can only be on/off\n')
            sys.exit(1)
    elif args.action == 'brightness':
        if 1 <= int(args.value) <= 100:
            tradfriActions.tradfri_dim_light(hubip, apiuser, apikey, args.lightbulbid, args.value)
        else:
            sys.stderr.write('[-] Tradfri: dim value can only be between 1 and 100\n')
            sys.exit(1)
    elif args.action == 'color':
        if args.value == 'warm' or args.value == 'normal' or args.value == 'cold':
            tradfriActions.tradfri_color_light(hubip, apiuser, apikey, args.lightbulbid, args.value)
        else:
            sys.stderr.write('[-] Tradfri: color value can only be warm/normal/cold\n')
            sys.exit(1)
Exemple #2
0
def main():
    """ main function """
    args = parse_args()
    conf = ConfigParser.ConfigParser()
    conf.read('tradfri.cfg')

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

    if args.action == 'power':
        if args.value == 'on' or args.value == 'off':
            tradfriActions.tradfri_power_light(hubip, clientid, securityid,
                                               args.lightbulbid, args.value)
        else:
            sys.stderr.write('[-] Tradfri: power state can only be on/off\n')
            sys.exit(1)
    elif args.action == 'brightness':
        if 1 <= int(args.value) <= 100:
            tradfriActions.tradfri_dim_light(hubip, clientid, securityid,
                                             args.lightbulbid, args.value)
        else:
            sys.stderr.write(
                '[-] Tradfri: dim value can only be between 1 and 100\n')
            sys.exit(1)
    elif args.action == 'color':
        if args.value == 'warm' or args.value == 'normal' or args.value == 'cold':
            tradfriActions.tradfri_color_light(hubip, clientid, securityid,
                                               args.lightbulbid, args.value)
        else:
            sys.stderr.write(
                '[-] Tradfri: color value can only be warm/normal/cold\n')
            sys.exit(1)
Exemple #3
0
def switch_off(hubip, apiuser, apikey, bulbid):

    if bulbid != 131073:
        tradfriActions.tradfri_power_light(hubip, apiuser, apikey, bulbid,
                                           'off')

    elif bulbid == 131073:
        tradfriActions.tradfri_power_group(hubip, apiuser, apikey, bulbid,
                                           "off")
Exemple #4
0
def main():
    """ main function """
    args = parse_args()
    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')

    if args.action == 'power':
        if args.value == 'on' or args.value == 'off':
            tradfriActions.tradfri_power_light(hubip, apiuser, apikey,
                                               args.lightbulbid, args.value)
        else:
            sys.stderr.write('[-] Tradfri: power state can only be on/off\n')
            sys.exit(1)
    elif args.action == 'brightness':
        if 1 <= int(args.value) <= 100:
            tradfriActions.tradfri_dim_light(hubip, apiuser, apikey,
                                             args.lightbulbid, args.value)
        else:
            sys.stderr.write(
                '[-] Tradfri: dim value can only be between 1 and 100\n')
            sys.exit(1)
    elif args.action == 'color':
        if args.value in tradfriActions.get_color_dict().keys():
            tradfriActions.tradfri_color_light(hubip, apiuser, apikey,
                                               args.lightbulbid, args.value)
        else:
            message = '[-] Tradfri: color value can only be: '
            for color in tradfriActions.get_color_dict().keys():
                if ' ' in color:
                    message += " '%s'," % color
                else:
                    message += " %s," % color

            sys.stderr.write("\n".join(wrap(message)))
            sys.exit(1)
Exemple #5
0
def commercialpost(devid, io):
    tradfriActions.tradfri_power_light("192.168.0.64", "MjSdr1HQaWBNLs3m",
                                       devid, io)
def on_message(client, userdata, msg):
    print(msg.topic+" "+str(msg.payload))
    
    topicelem = msg.topic.split("/")
    # Make sure there are at least 4 elements in the topic string
    devaddr = topicelem[2]
    
    print "devaddr is {}".format(devaddr)
    if devaddr == "update_devices":
        read_devices()
        return
    
    cmd = topicelem[3]
    
    group = False
    #print("Devaddr is {}".format(devaddr))
    ep = match_endpoint(groups, devaddr)
    
    # endpoint could be a group or individual device - we treat them differently
    if isinstance(ep, tradfrigroup):
        group = True
    
    if cmd == "brightness":
        print("Set {} - {} brightness to {}".format(ep.name, ep.id, msg.payload))
        #print("Brightness get mutex")
        mutex.acquire()
        if msg.payload.lower() == 'on' or msg.payload.lower() == 'off':
            if group == False:
                tradfriActions.tradfri_power_light(gatewayurl, username, userkey, ep.id, msg.payload.lower())
            else:
                tradfriActions.tradfri_power_group(gatewayurl, username, userkey, ep.id, msg.payload.lower())
        else:
            try:
                if 0 <= int(msg.payload) <= 100:
                    if 0 == int(msg.payload):
                        if group == False:
                            tradfriActions.tradfri_power_light(gatewayurl, username, userkey, ep.id, "off")
                        else:
                            tradfriActions.tradfri_power_group(gatewayurl, username, userkey, ep.id, "off")
                    else:
                        if group == False:
                            tradfriActions.tradfri_dim_light(gatewayurl, username, userkey, ep.id, msg.payload)
                        else:
                            tradfriActions.tradfri_dim_group(gatewayurl, username, userkey, ep.id, msg.payload)
                else:
                    sys.stderr.write('Tradfri: dim value can only be between 0 and 100\n')
            except:
                sys.stderr.write('unable to decode dimmer value {}\n'.format(msg.payload))
                pass
        mutex.release()
        #print("Brightness release mutex")
        #cmdready.set()
    if cmd == "power":
        print("Set {} power {}".format(ep.name, msg.payload))
        #print("Power get mutex")
        mutex.acquire()
        if msg.payload.lower() == 'on' or msg.payload.lower() == 'off':
            if group == False:
                tradfriActions.tradfri_power_light(gatewayurl, username, userkey, ep.id, msg.payload.lower())
            else:
                tradfriActions.tradfri_power_group(gatewayurl, username, userkey, ep.id, msg.payload.lower())
        else:
            sys.stderr.write('Tradfri: power state can only be on/off\n')
        mutex.release()
        #print("Power release mutex")
        #cmdready.set()
    if cmd == "temp":
        # if this is a group id we need to set the temp on individual bulbs as they can't be set as a group
        print("Set {} colour temperature {}% warm".format(ep.name, msg.payload))
        #print("Temp get mutex")
        mutex.acquire()
        try:
            if 0 <= int(msg.payload) <= 100:
                if group == True:
                    for bulb in ep.devices:
                        if bulb.isspectrum == True:
                            tradfriActions.tradfri_color_percent(gatewayurl, username, userkey, bulb.id, msg.payload)
                else:
                    tradfriActions.tradfri_color_percent(gatewayurl, username, userkey, ep.id, msg.payload)
            else:
                sys.stderr.write('Tradfri: colour value can only be between 0 and 100\n')
        except:
            sys.stderr.write('unable to decode colour value {}\n'.format(msg.payload))
        mutex.release()
        #print("Temp release mutex") 
        #cmdready.set()
    if cmd == "poll":
        cmdready.set()