From 0aaad1686b801630a81255b3ce5a91cf13d5f675 Mon Sep 17 00:00:00 2001 From: mischa Date: Sun, 3 Nov 2019 16:56:25 +0100 Subject: [PATCH] add temperature.py --- temperature.py | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100755 temperature.py diff --git a/temperature.py b/temperature.py new file mode 100755 index 0000000..9c28666 --- /dev/null +++ b/temperature.py @@ -0,0 +1,62 @@ +#!/usr/bin/env python3 +# +# Copyright 2019, Mischa Peters , High5!. +# Version 1.0 - 20191103 +# +# Get temperaure from all sensors +# +# For exmaple: +# $ temperature.py +# +# Requires: +# - Python 3.x +# +import argparse +import ssl +import urllib.request +import json +import re +import collections +import math + +parser = argparse.ArgumentParser(description="Get temperature from Hue Bridge") +parser.add_argument("bridge", type=str, help="Hue Bridge IP") +parser.add_argument("token", type=str, help="Hue API Token") + +try: + args = parser.parse_args() + bridge = args.bridge + token = args.token + +except argparse.ArgumentError as e: + print(str(e)) + +no_cert_check = ssl.create_default_context() +no_cert_check.check_hostname=False +no_cert_check.verify_mode=ssl.CERT_NONE + +url = f"https://{bridge}/api/{token}/sensors" +req = urllib.request.Request(url) +with urllib.request.urlopen(req, context=no_cert_check) as response: + content = response.read() +json_data = json.loads(content) + +p = re.compile("([a-fA-F0-9]{2}:?){8}") +sensors = collections.defaultdict(list); + +for key in json_data: + if "uniqueid" in json_data[key]: + if p.search(json_data[key]['uniqueid']): + if json_data[key]['type'] == 'ZLLPresence': + sensors[json_data[key]['uniqueid'][:-8]].insert(0, key) + else: + sensors[json_data[key]['uniqueid'][:-8]].append(key) + +for key in sensors: + for i in sensors[key]: + if json_data.get(i)['type'] == 'ZLLPresence': + name = json_data.get(i)['name'] + if json_data.get(i)['type'] == 'ZLLTemperature': + updated = json_data.get(i)['state']['lastupdated'][-8:] + temperature = round((json_data.get(i)['state']['temperature'] / 100), 1) + print (f"{name:<25s} - {temperature}C (updated: {updated} UTC) {i}")