示例#1
0
    def read_ip_from_config(self, newip=False):
        filepath = os.path.join(self.configpath, 'ip.conf')

        if newip or not os.path.exists(filepath):
            try:
                import discoverhue
            except ImportError:
                ip = input("Please enter the ip of the Hue bridge: ")
            else:
                print("Discovering Hue bridge, please wait...")
                bridge_id, ip = discoverhue.find_bridges().popitem()
                ip = ip.split('/')[2].split(':')[0]
                print("Found Hue bridge at ip", ip)

            # Create non existing config directory
            if not os.path.exists(self.configpath):
                os.makedirs(self.configpath)

            # Store the username in a credential file
            with open(filepath, "w") as ip_file:
                ip_file.write(ip)

        else:
            with open(filepath, "r") as ip_file:
                ip = ip_file.read()

        return ip
示例#2
0
    def discover_bridges(self):
        bridges = []
        try:
            discovered_bridges = discoverhue.find_bridges()
        except Exception as e:
            self.logger.error(
                "discover_bridges: Exception in find_bridges(): {}".format(e))
            discovered_bridges = {}

        for br in discovered_bridges:
            br_info = {}
            br_info['mac'] = br
            br_info['ip'] = discovered_bridges[br].split('/')[2].split(':')[0]
            br_info['port'] = discovered_bridges[br].split('/')[2].split(
                ':')[1]
            r = requests.get('http://' + br_info['ip'] + ':' +
                             br_info['port'] + '/description.xml')
            if r.status_code == 200:
                xmldict = xmltodict.parse(r.text)
                br_info['friendlyName'] = str(
                    xmldict['root']['device']['friendlyName'])
                br_info['manufacturer'] = str(
                    xmldict['root']['device']['manufacturer'])
                br_info['manufacturerURL'] = str(
                    xmldict['root']['device']['manufacturerURL'])
                br_info['modelDescription'] = str(
                    xmldict['root']['device']['modelDescription'])
                br_info['modelName'] = str(
                    xmldict['root']['device']['modelName'])
                br_info['modelURL'] = str(
                    xmldict['root']['device']['modelURL'])
                br_info['modelNumber'] = str(
                    xmldict['root']['device']['modelNumber'])
                br_info['serialNumber'] = str(
                    xmldict['root']['device']['serialNumber'])
                br_info['UDN'] = str(xmldict['root']['device']['UDN'])
                br_info['gatewayName'] = str(xmldict['root']['device'].get(
                    'gatewayName', ''))

                br_info['URLBase'] = str(xmldict['root']['URLBase'])
                if br_info['modelName'] == 'Philips hue bridge 2012':
                    br_info['version'] = 'v1'
                elif br_info['modelName'] == 'Philips hue bridge 2015':
                    br_info['version'] = 'v2'
                else:
                    br_info['version'] = 'unknown'

                # get API information
                api_config = self.get_api_config_of_bridge(br_info['URLBase'])
                br_info['datastoreversion'] = api_config.get(
                    'datastoreversion', '')
                br_info['apiversion'] = api_config.get('apiversion', '')
                br_info['swversion'] = api_config.get('swversion', '')

            bridges.append(br_info)

        for bridge in bridges:
            self.logger.info("Discoverd bridge = {}".format(bridge))

        return bridges
示例#3
0
 def bridge(self):
     if self.hue_bridge == None:
         found_bridges = discoverhue.find_bridges()
         if len(found_bridges) > 0:
             bridge_ip = urlparse(list(found_bridges.values())[0]).netloc
             self.hue_bridge = Bridge(bridge_ip, self.username)
     return self.hue_bridge
示例#4
0
def Get_Bridge_IP():
    # the IP address of your bridge
    found = discoverhue.find_bridges()
    for bridge in found:
        ip = found[bridge]
    ip = ip.replace("http://", "")
    ip = ip.replace(":80/", "")
    return ip
示例#5
0
 def get_bridge_list():
     bridges = discoverhue.find_bridges()
     return list(
         map(
             lambda bridge: {
                 'id': bridge,
                 'value': urlparse(bridges[bridge]).hostname
             }, bridges))
示例#6
0
def main():
    logger.debug('Discovering')
    status = Status()
    found_bridges = discoverhue.find_bridges()
    if len(found_bridges) < 1:
        logger.fatal('No Hue bridges found')
        return

    for bridge in found_bridges:
        bridge_ip = urlparse(found_bridges[bridge]).netloc
        logger.debug('Found a bridge at {ip}'.format(ip=bridge_ip))
        status.set_bridge(bridge_ip)
    status.rooms()
示例#7
0
def get():

    devicetype = construct_devicetype()

    ui = ConsoleInterface()
    storage = CWDStorage()

    with storage(devicetype) as bridges_in_cfg:
        previous_bridges = bridges_in_cfg.copy()  # shallow copy should be ok
        bridges_in_lan = discoverhue.find_bridges(previous_bridges)

        # Replace whitelist info dropped by discoverhue
        for sn in bridges_in_lan.keys() & bridges_in_cfg.keys():
            bridges_in_lan[sn] = BridgeURL(bridges_in_lan[sn], bridges_in_cfg[sn].username)

        # Note: discovery does not return SN's not on the list
        # for sn in bridges_in_lan.keys() - bridges_in_cfg.keys():
        #     print('Bridges to add {}'.format(sn))

        # Nullify IP's of bridges not located
        for sn in previous_bridges:
            bridges_in_cfg[sn] = BridgeURL(None, bridges_in_cfg[sn].username)

        # TODO: better means to manage removal?
        keys_to_remove = []
        for sn, url_info in bridges_in_lan.items():
            # TODO: remove url_info from Bridge, so only one instance needed
            b = UrllibBridge(url_info)
            # if it validates then no config change and it should be returned
            # otherwise, it needs a new username and config must change
            if not b.validate_user():
                ui.prompt_for_button()
                try:
                    bridges_in_lan[sn].username = b.create_user()
                except:
                    # TODO: make a custom err
                    ui.message_not_pressed()
                    keys_to_remove.append(sn)

        # unresolved bridges should be removed from return list
        # and the usernames purged from config
        for sn in keys_to_remove:
            url_info = bridges_in_lan.pop(sn)
            bridges_in_cfg[sn].username = None

        # remainder of changes
        bridges_in_cfg.update(bridges_in_lan)

    return bridges_in_lan