ext_light_control.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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("pyscript.ext_light_has_been_day == 'true' and int(sensor.lux_outside_the_garage) < 100")
  12. def automate_exterior_lights_on_in_evening():
  13. pyscript.ext_light_has_been_day = 'false'
  14. input_boolean.exterior_lights_wanted_state = 'on'
  15. @state_trigger("pyscript.ext_light_has_been_night == 'true' and int(sensor.lux_outside_the_garage) > 50")
  16. def automate_exterior_lights_off_in_morning():
  17. pyscript.ext_light_has_been_night = 'false'
  18. input_boolean.exterior_lights_wanted_state = 'off'
  19. @state_trigger("input_boolean.exterior_lights_wanted_state")
  20. @task_unique("control_ext_light", kill_me=True)
  21. def control_ext_light():
  22. # Set max number of iterations
  23. maxIterations = 5
  24. # Loop until we have succeeded to controlling the light
  25. while(
  26. ( (sensor.exterior_light_status != input_boolean.exterior_lights_wanted_state) or
  27. ( light.exterior_lights!= input_boolean.exterior_lights_wanted_state) ) and maxIterations > 0
  28. ):
  29. # Find out if we should turn the light on or off
  30. action = "turn_off" if input_boolean.exterior_lights_wanted_state == 'off' else "turn_on"
  31. service.call("light", action, entity_id="light.exterior_lights")
  32. service.call("light", action, entity_id="light.exterior_lights_repeater")
  33. log.info("Sleeping 60s.....")
  34. trig_info = task.wait_until(state_trigger=["input_boolean.exterior_lights_wanted_state","sensor.exterior_light_status"], timeout=60)
  35. maxIterations -= 1
  36. log.info(f"Exit control_ext_light")