Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/python
"""
Set a red alarm on a given light when the specified website is down
"""
import os
from phue import Bridge
from time import sleep
from requests import get
import sys
SCRIPT_PATH = os.path.dirname(__file__) # Directory of the current file
KEY_FILE = os.path.join(SCRIPT_PATH, ".python_hue") # Path to hidden key file
def alarm(bridge, light_id, tr_time, blink_count):
default = b.get_light(light_id) # Default state
animIn = {'transitiontime': tr_time,
'on': True,
'bri':254, # Full luminosity
'sat': 254, # Full saturation
'hue': 65535 # Red
}
animOut = {'transitiontime': tr_time,
'on': True,
'bri':5, # Almost off
'sat': 70, # Less saturation
'hue': 65535 # Still red
}
sleep_time = float(tr_time + 1) / 10.0
for i in range(0, blink_count):
b.set_light(light_id, animIn)
sleep(sleep_time)
b.set_light(light_id, animOut)
sleep(sleep_time)
animOut = {
'on': default['state']['on'],
'sat': default['state']['sat'],
'hue': default['state']['hue'],
'bri': default['state']['bri'],
}
b.set_light(light_id, animOut) # Reset to default state
if __name__ == "__main__":
b = Bridge(IP_ADDRESS, config_file_path=KEY_FILE)
b.connect()
site_down = False
try:
r = get("http://yourwbesite.tld/") # Or you can use sys.argv to specifiy the url when calling the script
if r.status_code >= 400:
site_down = True
except Exception:
site_down = True
if site_down:
alarm(b, 1, 5, 10)
sys.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment