def disable_inactive_lights(): now_date = get_local_time() logger.info("begin disable_inactive_lights") for pin, room in PIN_TO_ROOM.items(): inactive = room.is_motion_timed_out(as_of_date=now_date) logger.info("room %s is timed out %r" % (room.name, inactive)) if not inactive: continue # Motion must have occurred in neighbor room afterward for this room to be inactive neighbor_room_names = settings.ROOM_GRAPH.get(room.name, None) if neighbor_room_names is not None: logger.info("evaluating neighbors (%s) for %s" % (neighbor_room_names, room.name)) subsequent_neighbor_motion = False for neighbor_room_name in neighbor_room_names: for pot_neighbor_room in settings.ROOMS: logger.info("evaluating potential neighbor (%s) for name %s" % (pot_neighbor_room.name, neighbor_room_name)) if neighbor_room_name == pot_neighbor_room.name: neighbor_corroborates_exit = pot_neighbor_room.last_motion is not None and pot_neighbor_room.last_motion > room.last_motion logger.info("evaluating room %s neighbor %s. Neighbor moved after this %r" % (room.name, neighbor_room_name, neighbor_corroborates_exit)) if neighbor_corroborates_exit: subsequent_neighbor_motion = True break if not subsequent_neighbor_motion: logger.info("Room %s is timed out but neighbors (%s) have not received motion afterward, " "so room must still be occupied." % (room.name, neighbor_room_names)) continue room.switch(on=False) logger.info("end disable_inactive_lights")
def on_motion(triggered_pin: int): now = get_local_time() is_motion_start = GPIO.input(triggered_pin) room = PIN_TO_ROOM[triggered_pin] logger.info("Motion %s in %s" % (("started" if is_motion_start else "stopped"), room.name)) # Ignore repeat motion stop events if not is_motion_start and not room.motion_started: return room.on_motion(now, is_motion_start=is_motion_start)
def party(): global party_process json = flask.request.get_json() enabled = json.get("enabled", False) logger.info("Got party state %r" % enabled) if enabled and not env.is_party_mode(): # Start party XD party_process = Popen(["python3", "./animate_web.py", "run"]) # async env.set_party_mode(True) elif not enabled and env.is_party_mode(): # Stop party :( env.set_party_mode(False) if party_process is not None: party_process.kill() party_process = None # Return lights to circadian color command = copy.deepcopy(COMMAND_FULL_ON) circadian_color = get_current_circadian_color(date=get_local_time()) command_all_lights(circadian_color.apply_to_command(command)) return "Party mode is now %r" % enabled
import time from support.color import get_next_circadian_color from support.hue import command_all_lights, hue from support.logger import get_logger from support.room import LightsOnDuringDayRoom from support.time_utils import get_local_time from settings import ROOMS # Logging logger = get_logger("circadian") while 1: now = get_local_time() next_color_date, next_color = get_next_circadian_color(date=now) sleep_time_s = (next_color_date - now).seconds logger.info("Sleeping until %s at %s", next_color.name, next_color_date.strftime('%Y/%m/%d %I:%M:%S %p')) time.sleep(sleep_time_s + 30) # Add padding to compensate for sleep inaccuracy logger.info("Adjusting hue for %s", next_color.name) command = next_color.apply_to_command({'transitiontime': 60 * 10}) # 60 s transition if next_color.name == 'Day': lights = [] for room in ROOMS: if not isinstance(room, LightsOnDuringDayRoom): lights += room.lights hue.set_light(lights, command) else: command_all_lights(command)
import json import forecastio from SECRETS import FORECAST_IO_KEY from support.time_utils import get_local_time FORECAST_FILENAME = 'forecast.json' LAT = 37.877881 LON = -122.269312 forecast_file = open(FORECAST_FILENAME, 'w') current_time = get_local_time() forecast = forecastio.load_forecast(FORECAST_IO_KEY, LAT, LON, time=current_time, lazy=True) ''' Forecastio daily data point has the following properties, some may be undefined if they do not apply to that day's weather: { 'd': { 'apparentTemperatureMax': 59.55, 'apparentTemperatureMaxTime': 1457204400, 'apparentTemperatureMin': 57.26, 'apparentTemperatureMinTime': 1457186400, 'cloudCover': 0.82, 'dewPoint': 55.44, 'humidity': 0.89, 'icon': 'rain', 'moonPhase': 0.88, 'ozone': 338.58, 'precipIntensity': 0.0647, 'precipIntensityMax': 0.2725,