Last active
January 13, 2021 19:21
-
-
Save mortie/24955584065d3eb025e1e7329b200d37 to your computer and use it in GitHub Desktop.
Color a phillips hue light according to your CPU temperature.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import wmi | |
import time | |
import phue | |
import os | |
hot = 70 # Celsius | |
cold = 60 # Celsius | |
timeout = 10 | |
ip = "10.0.101.23" | |
light_id = 4 | |
col_hot = { | |
"hue": 0, | |
"sat": 200 | |
} | |
col_cold = { | |
"hue": None, | |
"sat": None | |
} | |
hwmon = wmi.WMI(namespace="root/OpenHardwareMonitor") | |
bridge = None | |
lights = None | |
light = None | |
def huesetup(): | |
global bridge, lights, light | |
connected = False | |
while not connected: | |
try: | |
bridge = phue.Bridge(ip) | |
lights = bridge.get_light_objects("id") | |
light = lights[light_id] | |
connected = True | |
except: | |
print("Failed to connect to hue. Retrying.") | |
time.sleep(2) | |
huesetup() | |
def setlight(light, col): | |
global nextlight | |
try: | |
light.transitiontime = 20 | |
light.hue = col["hue"] | |
light.saturation = col["sat"] | |
time.sleep(light.transitiontime / 10) | |
except: | |
connected = False | |
print("Failed to set light color. Reconnecting to hue.") | |
huesetup() | |
setlight(light, col) | |
def gettemp(): | |
maxtemp = None | |
for s in hwmon.sensor(): | |
if "CPU" in s.Name and s.SensorType == "Temperature": | |
if maxtemp == None or s.Value > maxtemp: | |
maxtemp = s.Value | |
return maxtemp | |
def updatecold(): | |
global light, col_hot, col_cold | |
if not col_cold["hue"] == None: | |
if light.hue in (col_hot["hue"], col_cold["hue"]): | |
return | |
if light.saturation in (col_hot["sat"], col_cold["sat"]): | |
return | |
col_cold["hue"] = light.hue | |
col_cold["sat"] = light.saturation | |
print("Updating to new cold color:", col_cold) | |
updatecold() | |
def action(): | |
print("Changing color to hot.") | |
setlight(light, col_hot) | |
def deaction(): | |
print("Changing color to cold.") | |
setlight(light, col_cold) | |
action() | |
time.sleep(2) | |
deaction() | |
time.sleep(1) | |
actioned = False | |
while True: | |
updatecold() | |
temp = gettemp() | |
print("Logged "+str(temp)) | |
if not actioned and temp >= hot: | |
actioned = True | |
action() | |
elif actioned and temp <= cold: | |
actioned = False | |
deaction() | |
time.sleep(timeout) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment