ext_lights_control.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. @time_trigger("once(12:30:00)")
  2. def set_has_been_day_flag(**kwargs):
  3. # Set ext_light_has_been_day flag at 12:30 every day.
  4. log.info("Setting ext_light_has_been_day = ON (daily reset at 12:30)")
  5. input_boolean.ext_light_has_been_day = 'on'
  6. @time_trigger("once(00:30:00)")
  7. def set_has_been_night_flag(**kwargs):
  8. # Set night flags at 00:30 every day.
  9. log.info("Setting has_been_night flags = ON (00:30)")
  10. input_boolean.ext_light_has_been_night = 'on'
  11. input_boolean.home_auto_lights_off_has_been_night = 'on'
  12. @state_trigger("sensor.outside_lux != 'unknown' and int(sensor.outside_lux) > 710", state_hold_false=0)
  13. @state_active("input_boolean.ext_light_has_been_night == 'on'")
  14. def turn_off_exterior_lights_morning(**kwargs):
  15. # Turn off exterior lights in the morning when lux goes above 710.
  16. log.info(f"Lux {sensor.outside_lux} > 710, turning off exterior lights")
  17. input_boolean.ext_light_has_been_night = 'off'
  18. input_boolean.exterior_lights_wanted_state = 'off'
  19. @state_trigger("sensor.outside_lux != 'unknown' and int(sensor.outside_lux) < 500", state_hold_false=0)
  20. @state_active("input_boolean.ext_light_has_been_day == 'on'")
  21. def turn_on_exterior_lights_evening(**kwargs):
  22. # Turn on exterior lights in the evening when lux drops below 500.
  23. log.info(f"Lux {sensor.outside_lux} < 500, turning on exterior lights")
  24. input_boolean.ext_light_has_been_day = 'off'
  25. input_boolean.exterior_lights_wanted_state = 'on'
  26. @state_trigger("input_boolean.exterior_lights_wanted_state")
  27. @task_unique("control_ext_light", kill_me=True)
  28. def sync_exterior_lights(value=None, **kwargs):
  29. # Keep exterior lights in sync with wanted state (input_boolean).
  30. action = "turn_off" if input_boolean.exterior_lights_wanted_state == 'off' else "turn_on"
  31. isChristmas = True if input_boolean.christmas == 'on' else False
  32. log.info(f"sync_exterior_lights input: {value} Action: {action} Christmas:{isChristmas}")
  33. service.call("switch", action, entity_id="switch.1_mini_gen3_wood_shed")
  34. service.call("switch", action, entity_id="switch.1_mini_gen3_front_door")
  35. if isChristmas:
  36. max_iterations = 3
  37. iteration = 1
  38. while(
  39. ( (sensor.exterior_light_status != input_boolean.exterior_lights_wanted_state) or
  40. (light.exterior_lights != input_boolean.exterior_lights_wanted_state) ) and
  41. iteration < max_iterations ):
  42. log.info(f"Christmas lights sync iteration {iteration} of {max_iterations}. Action: {action}, exterior_light_status: {sensor.exterior_light_status}, exterior_lights: {light.exterior_lights}, wanted_state: {input_boolean.exterior_lights_wanted_state}")
  43. service.call("light", action, entity_id="light.exterior_lights")
  44. service.call("light", action, entity_id="light.exterior_lights_repeater")
  45. log.info("Sleeping 6s.....")
  46. trig_info = task.wait_until(state_trigger=["input_boolean.exterior_lights_wanted_state","sensor.exterior_light_status"], timeout=6)
  47. log.info(f"Woke up from wait_until: {trig_info}")
  48. iteration += 1
  49. @service
  50. def thomas_testing():
  51. log.info(f"Running Thomas testing service {int(sensor.outside_lux)}")
  52. #set_has_been_day_flag()