ext_lights_control.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. state.persist('pyscript.ext_light_has_been_day','false')
  2. state.persist('pyscript.ext_light_has_been_night','false')
  3. # Set that it has been midnight, and that it's ok to turn the light on
  4. @time_trigger("once(00:30:00)")
  5. def ext_light_has_been_night():
  6. pyscript.ext_light_has_been_night = 'true'
  7. # Set that it has been noon, and that it's ok to turn off the light
  8. @time_trigger("once(12:30:00)")
  9. def ext_light_has_been_day():
  10. pyscript.ext_light_has_been_day = 'true'
  11. @state_trigger("int(sensor.lux_outside_the_garage) < 100", state_hold_false=0 )
  12. def automate_exterior_lights_on_in_evening():
  13. if( pyscript.ext_light_has_been_day == 'true' ):
  14. pyscript.ext_light_has_been_day = 'false'
  15. input_boolean.exterior_lights_wanted_state = 'on'
  16. @state_trigger("int(sensor.lux_outside_the_garage) > 50", state_hold_false=0)
  17. def automate_exterior_lights_off_in_morning():
  18. if( pyscript.ext_light_has_been_night == 'true' ):
  19. pyscript.ext_light_has_been_night = 'false'
  20. input_boolean.exterior_lights_wanted_state = 'off'
  21. @state_trigger("input_boolean.exterior_lights_wanted_state")
  22. @task_unique("control_ext_light", kill_me=True)
  23. def control_ext_light():
  24. # Set max number of iterations
  25. maxIterations = 5
  26. # Loop until we have succeeded to controlling the light
  27. while(
  28. ( (sensor.exterior_light_status != input_boolean.exterior_lights_wanted_state) or
  29. ( light.exterior_lights!= input_boolean.exterior_lights_wanted_state) ) and maxIterations > 0
  30. ):
  31. # Find out if we should turn the light on or off
  32. action = "turn_off" if input_boolean.exterior_lights_wanted_state == 'off' else "turn_on"
  33. service.call("light", action, entity_id="light.exterior_lights")
  34. service.call("light", action, entity_id="light.exterior_lights_repeater")
  35. log.info("Sleeping 60s.....")
  36. trig_info = task.wait_until(state_trigger=["input_boolean.exterior_lights_wanted_state","sensor.exterior_light_status"], timeout=60)
  37. maxIterations -= 1
  38. log.info(f"Exit control_ext_light")