Advertisement
Guest User

Untitled

a guest
Feb 27th, 2020
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.90 KB | None | 0 0
  1. import hassapi as hass;
  2. import time;
  3.  
  4. """
  5.  
  6. Monitor cameras for motion, and turn lights on if after dark, then turn them off after no more motion is detected
  7.  
  8. """
  9.  
  10. class MotionLights(hass.Hass):
  11.   def initialize(self):
  12.  
  13.     self.log("MotionLights.initialize()")
  14.     last_motion_event = { }
  15.     light_delay = 30
  16.    
  17.     cameras = { }
  18.     cameras[ "c01_motion" ] = [ "switch.exterior_front_floods" ]
  19.     cameras[ "c03_motion" ] = [ "switch.exterior_front_floods" ]
  20.     cameras[ "c09_motion" ] = [ "switch.exterior_front_floods" ]
  21.     cameras[ "c11_motion" ] = [ "switch.exterior_front_floods" ]
  22.     cameras[ "c12_motion" ] = [ "switch.exterior_front_floods" ]
  23.     cameras[ "c19_motion" ] = [ "switch.exterior_driveway_floods" ]
  24.     cameras[ "c20_motion" ] = [ "switch.exterior_driveway_floods" ]
  25.     cameras[ "doorbird_front_door_motion_detected" ] = [ "switch.front_porch_overhead_light", "switch.exterior_front_floods", "switch.exterior_driveway_floods" ]
  26.  
  27.     for camera in cameras.keys():
  28.       self.listen_state(self.motion, "binary_sensor." + camera, new="on")
  29.  
  30.   def motion(self, entity, attribute, old, new, kwargs):
  31.     last_motion_event[ entity ] = time.time()
  32.    
  33.     if self.sun_down():
  34.         for light in cameras[entity]:
  35.           self.turn_on(light)
  36.           self.run_in(self.light_off, light_delay, camera = entity)
  37.  
  38.   def light_off(self, kwargs):
  39.     last_motion = 0
  40.    
  41.     if camera in last_motion_event:
  42.       last_motion = last_motion_event[camera]
  43.  
  44.     for light in cameras[camera]:
  45.       for othercamera in cameras.keys():
  46.         if othercamera in last_motion_event:
  47.           if light in othercamera:
  48.             if last_motion_event[othercamera] > last_motion:
  49.               last_motion = last_motion_event[othercamera];
  50.  
  51.     now = time.time()
  52.    
  53.     if now - last_motion > light_delay:
  54.       for light in cameras[camera]:
  55.         self.turn_off(light)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement