diff --git a/hue2influx.py b/hue2influx.py index 5066538011fe167d729ed4fef39e9e6772119ab7..62324fe61b6195c325b3d8c018bd77f3282dc871 100644 --- a/hue2influx.py +++ b/hue2influx.py @@ -24,6 +24,7 @@ class Hue2Influx: self._hue = Bridge(HUE_BRIDGE) self._data_sensors = [] + self._data_lights = [] self._influxdb = None self._data_influx = [] @@ -42,15 +43,16 @@ class Hue2Influx: print("hue2influx running ...") while True: self.sync_sensors() + self.sync_lights() time.sleep(60) def _get_sensors(self): - self._sensors = self._hue.get_sensor_objects('id') + self._data_sensors = self._hue.get_sensor_objects('id') return True def _convert_sensors_to_influx(self): - for sensor_key in self._sensors: - sensor = self._sensors[sensor_key]._get(None) + for sensor_key in self._data_sensors: + sensor = self._data_sensors[sensor_key]._get(None) measurement = { "measurement": "hue-sensors", @@ -80,12 +82,84 @@ class Hue2Influx: continue elif state_key == 'temperature': state_value = 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 - 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) @@ -99,6 +173,11 @@ class Hue2Influx: 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() + def quit(signum, frame): signal.signal(signal.SIGINT, original_sigint) print("Goodbye!")