Private GIT

Skip to content
Snippets Groups Projects
Commit c945e187 authored by Marius's avatar Marius
Browse files

Implemented lights syncing, implemented handling of lists & dicts in fields

parent 682a1cf8
No related branches found
No related tags found
No related merge requests found
...@@ -24,6 +24,7 @@ class Hue2Influx: ...@@ -24,6 +24,7 @@ class Hue2Influx:
self._hue = Bridge(HUE_BRIDGE) self._hue = Bridge(HUE_BRIDGE)
self._data_sensors = [] self._data_sensors = []
self._data_lights = []
self._influxdb = None self._influxdb = None
self._data_influx = [] self._data_influx = []
...@@ -42,15 +43,16 @@ class Hue2Influx: ...@@ -42,15 +43,16 @@ class Hue2Influx:
print("hue2influx running ...") print("hue2influx running ...")
while True: while True:
self.sync_sensors() self.sync_sensors()
self.sync_lights()
time.sleep(60) time.sleep(60)
def _get_sensors(self): def _get_sensors(self):
self._sensors = self._hue.get_sensor_objects('id') self._data_sensors = self._hue.get_sensor_objects('id')
return True return True
def _convert_sensors_to_influx(self): def _convert_sensors_to_influx(self):
for sensor_key in self._sensors: for sensor_key in self._data_sensors:
sensor = self._sensors[sensor_key]._get(None) sensor = self._data_sensors[sensor_key]._get(None)
measurement = { measurement = {
"measurement": "hue-sensors", "measurement": "hue-sensors",
...@@ -80,12 +82,84 @@ class Hue2Influx: ...@@ -80,12 +82,84 @@ class Hue2Influx:
continue continue
elif state_key == 'temperature': elif state_key == 'temperature':
state_value = state_value / 100 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) self._data_influx.append(measurement)
return self._data_influx 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): def _put_influx(self):
success = self._influxdb.write_points(self._data_influx) success = self._influxdb.write_points(self._data_influx)
...@@ -99,6 +173,11 @@ class Hue2Influx: ...@@ -99,6 +173,11 @@ class Hue2Influx:
self._convert_sensors_to_influx() self._convert_sensors_to_influx()
return self._put_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): def quit(signum, frame):
signal.signal(signal.SIGINT, original_sigint) signal.signal(signal.SIGINT, original_sigint)
print("Goodbye!") print("Goodbye!")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment