hue/temperature.py

82 lines
2.5 KiB
Python
Raw Normal View History

2019-11-03 16:56:25 +01:00
#!/usr/bin/env python3
#
2020-05-07 16:13:45 +02:00
# Copyright 2019-2020, Mischa Peters <mischa AT high5 DOT nl>, High5!.
2019-11-03 16:56:25 +01:00
# Version 1.0 - 20191103
2020-05-07 16:13:45 +02:00
# Version 1.1 - 20200507 - added config file support
2019-11-03 16:56:25 +01:00
#
# Get temperaure from all sensors
#
# For exmaple:
2020-05-07 16:13:45 +02:00
# $ temperature.py <bridge name>
2019-11-03 16:56:25 +01:00
#
# Requires:
# - Python 3.x
#
import argparse
import ssl
import urllib.request
import json
import re
import collections
import math
2020-05-07 16:13:45 +02:00
import os
import configparser
2019-11-03 16:56:25 +01:00
parser = argparse.ArgumentParser(description="Get temperature from Hue Bridge")
2020-05-07 16:13:45 +02:00
parser.add_argument("bridgename", type=str, help="Hue Bridge name in specified in hue.conf")
2019-11-03 17:02:08 +01:00
parser.add_argument("-v", "--verbose", action='store_true', help="verbose")
parser.add_argument("-d", "--debug", action='store_true', help="debug")
2019-11-03 16:56:25 +01:00
try:
args = parser.parse_args()
2020-05-07 16:13:45 +02:00
bridgename = args.bridgename
2019-11-03 17:02:08 +01:00
verbose = args.verbose
debug = args.debug
2019-11-03 16:56:25 +01:00
except argparse.ArgumentError as e:
print(str(e))
2020-05-07 16:13:45 +02:00
config_files = ['./hue.conf', './.hue.conf', '/etc/hue.conf', '/etc/hue/hue.conf', os.path.expanduser('~/.hue.conf'), os.path.expanduser('~/hue.conf')]
config = configparser.RawConfigParser()
config.read(config_files)
bridge = config.get(bridgename, 'ip')
token = config.get(bridgename, 'token')
2019-11-03 16:56:25 +01:00
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:]
2019-11-17 13:06:10 +01:00
if json_data.get(i)['state']['temperature']:
temperature = round((json_data.get(i)['state']['temperature'] / 100), 1)
else:
temperature = 0
2019-11-03 17:02:08 +01:00
if verbose:
2019-11-06 22:24:39 +01:00
print (f"{name:<32s} - {temperature:>4}C (updated: {updated} UTC)")
2019-11-03 17:02:08 +01:00
elif debug:
2019-11-06 22:24:39 +01:00
print (f"{name:<32s} - {temperature:>4}C (updated: {updated} UTC - sensor: {i})")
2019-11-03 17:02:08 +01:00
else:
2019-11-06 22:24:39 +01:00
print (f"{name:<32s} - {temperature:>4}C")