ext_lights_control.py 2.2 KB

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