A project utilizing MicroPython and a magnetic reed switch for triggering an alarm (email, Philips Hue light, etc)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

67 lines
1.8 KiB

"""
Various alarm related functions
"""
from time import sleep
def phue_fetch_light_states(bridge, group_id):
"""
Fetch the current states of each light.
Returns a list of dictionaries {"id": "<id>", "state": {}}
"""
lights = []
group = bridge.getGroup(group_id)
for light_id in group["lights"]:
light = bridge.getLight(light_id)
lights.append({"id": light_id, "state": light["state"]})
return lights
def phue_flash_lights(bridge, config):
"""
Transition between 2 colors
"""
transition_time = 2
sleep_time = 0.5
i = 1
while i < config["flash_count"]:
bridge.setGroup(
config["group"], rgb=config["color1"], transitiontime=transition_time, on=True
)
sleep(sleep_time)
bridge.setGroup(config["group"], rgb=config["color2"], transitiontime=transition_time)
sleep(sleep_time)
i = i + 1
def send_email(name, config):
"""
Send an email noting an issue
"""
import umail
smtp = umail.SMTP(config["server"], config["port"], ssl=config["ssl"])
smtp.login(config["username"], config["password"])
smtp.to(config["to"])
smtp.write("From: {}\n".format(config["from"]))
smtp.write("To: {}\n".format(config["to"]))
smtp.write("Subject: {}\n".format(name))
smtp.write("-- Generated by {} --\n".format(__name__))
smtp.send()
smtp.quit()
smtp = None
def trigger_alert(name, phue=None, smtp=None):
"""
Set off the various alerts
"""
if phue:
import uhue as hue
bridge = hue.Bridge()
lights = phue_fetch_light_states(bridge, phue["group"])
phue_flash_lights(bridge, phue)
# Reset light colors
for light in lights:
bridge.setLight(light["id"], **light["state"])
if smtp:
send_email(name, smtp)