class HueReactor(object): def __init__(self, bridge_ip): self.bridge = Bridge(bridge_ip) self.sensors = [] self.sensors.append(HueMotionPatch(self.bridge, 5)) def add_button_action(self, button_name, button_handler): logger.info("Add button action: %s" % button_name) sensors = self.bridge.get_sensor_objects(mode='name') self.sensors.append( HueButtonAction(sensors[button_name], button_handler)) def change_scene(self, scene, groups): logger.info("setting lights to [%s] mode" % scene) for group_name in groups: logger.debug("[%s] setting lights to [%s] mode" % (group_name, scene)) try: self.bridge.run_scene(group_name, scene) except Exception as e: logger.warn("%s" % e) logging.exception("phue run scene exception") self.check_sensors() def check_sensors(self): for sensor in self.sensors: sensor.check_sensors()
def get_response_from_ip(bridge_ip): """Returns the phue sensors response for a bridge_ip.""" b = Bridge(bridge_ip) response = b.get_sensor_objects('name') return response
class Hue2Influx: def __init__(self): if HUE_BRIDGE == None or HUE_BRIDGE == '': print( 'Please set the environment variable HUE_BRIDGE to the IP/hostname of your Hue Bridge!' ) exit(-1) self._hue = Bridge(HUE_BRIDGE) self._data_sensors = [] self._data_lights = [] self._influxdb = None self._data_influx = [] if INFLUXDB_SERVER == None or INFLUXDB_SERVER == "" or INFLUXDB_DB == None or INFLUXDB_DB == "": print( "Please set at least the environment variables INFLUXDB_SERVER and INFLUXDB_DB!" ) exit(-1) if INFLUXDB_UDP == False: self._influxdb = InfluxDBClient(host=INFLUXDB_SERVER, port=INFLUXDB_PORT, database=INFLUXDB_DB, username=INFLUXDB_USER, password=INFLUXDB_PASS) else: self._influxdb = InfluxDBClient(host=INFLUXDB_SERVER, use_udp=True, udp_port=INFLUXDB_PORT, database=INFLUXDB_DB, username=INFLUXDB_USER, password=INFLUXDB_PASS) def run(self): print("hue2influx running ...") while True: if self.sync_sensors() == False: print("Error writing sensor points!") if self.sync_lights() == False: print("Error writing light points!") print('.', end='', flush=True) time.sleep(60) def _get_sensors(self): self._data_sensors = self._hue.get_sensor_objects('id') return True def _convert_sensors_to_influx(self): for sensor_key in self._data_sensors: sensor = self._data_sensors[sensor_key]._get(None) measurement = { "measurement": "hue-sensors", "tags": { "name": sensor['name'], "type": sensor['type'], "modelid": sensor['modelid'], "manufacturername": sensor['manufacturername'], "productname": sensor['productname'] if 'productname' in sensor else '', "on": sensor['config']['on'] if 'config' in sensor and 'on' in sensor['config'] else True }, "time": datetime.datetime.now( datetime.timezone.utc).astimezone().isoformat(), "fields": { "reachable": sensor['config']['reachable'] if 'config' in sensor and 'reachable' in sensor['config'] else True, "battery": sensor['config']['battery'] if 'config' in sensor and 'battery' in sensor['config'] else -1, "sensitivity": sensor['config']['sensitivity'] if 'config' in sensor and 'sensitivity' in sensor['config'] else -1, "sensitivitymax": sensor['config']['sensitivitymax'] if 'config' in sensor and 'sensitivitymax' in sensor['config'] else -1, "tholddark": sensor['config']['tholddark'] if 'config' in sensor and 'tholddark' in sensor['config'] else -1, "tholdoffset": sensor['config']['tholdoffset'] if 'config' in sensor and 'tholdoffset' in sensor['config'] else -1, } } for state_key in sensor['state']: state_value = sensor['state'][state_key] if state_key == 'lastupdated': continue elif state_key == 'temperature': measurement['fields'][state_key] = state_value / 100 elif type(state_value) is dict: measurement['fields'].update( self._flatten_dict(state_key, state_value)) elif type(state_value) is list: measurement['fields'].update( self._flatten_list(state_key, state_value)) else: measurement['fields'][state_key] = state_value self._data_influx.append(measurement) return self._data_influx def _get_lights(self): self._data_lights = self._hue.get_light_objects('id') return True def _convert_lights_to_influx(self): for light_key in self._data_lights: light = self._data_lights[light_key]._get(None) measurement = { "measurement": "hue-lights", "tags": { "name": light['name'], "type": light['type'], "modelid": light['modelid'], "manufacturername": light['manufacturername'], "productname": light['productname'] if 'productname' in light else '', "productid": light['productid'] if 'productid' in light else '', "archetype": light['config']['archetype'] if 'config' in light and 'archetype' in light['config'] else '', "function": light['config']['function'] if 'config' in light and 'function' in light['config'] else '', "direction": light['config']['direction'] if 'config' in light and 'direction' in light['config'] else '' }, "time": datetime.datetime.now( datetime.timezone.utc).astimezone().isoformat(), "fields": {} } for state_key in light['state']: state_value = light['state'][state_key] if type(state_value) is dict: measurement['fields'].update( self._flatten_dict(state_key, state_value)) elif type(state_value) is list: measurement['fields'].update( self._flatten_list(state_key, state_value)) else: measurement['fields'][state_key] = state_value self._data_influx.append(measurement) return self._data_influx def _flatten_dict(self, key_name, the_dict): flat_dict = {} for dict_key in the_dict: dict_value = the_dict[dict_key] flat_key = key_name + "_" + dict_key if type(dict_value) is dict: flat_dict.update(self._flatten_dict(flat_key, dict_value)) elif type(dict_value) is list: flat_dict.update(self._flatten_list(flat_key, dict_value)) else: flat_dict[flat_key] = dict_value return flat_dict def _flatten_list(self, key_name, the_list): flat_dict = {} for list_index in range(len(the_list)): list_value = the_list[list_index] flat_key = key_name + "_" + str(list_index) if type(list_value) is dict: flat_dict.update(self._flatten_dict(flat_key, list_value)) elif type(list_value) is list: flat_dict.update(self._flatten_list(flat_key, list_value)) else: flat_dict[flat_key] = list_value return flat_dict def _put_influx(self): success = self._influxdb.write_points(self._data_influx) if success == True: self._data_influx = [] return success def sync_sensors(self): self._get_sensors() self._convert_sensors_to_influx() return self._put_influx() def sync_lights(self): self._get_lights() self._convert_lights_to_influx() return self._put_influx()