Advertisement
Guest User

Untitled

a guest
Jan 6th, 2021
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.55 KB | None | 0 0
  1. from core.rules import rule
  2. from core.triggers import when
  3. from core.utils import send_command_if_different, post_update_if_different
  4. from core.log import logging, LOG_PREFIX
  5. from community.gatekeeper import Gatekeeper
  6.  
  7. log = logging.getLogger("{}.irrigation".format(LOG_PREFIX))
  8. gatekeeper = None
  9.  
  10.  
  11. @rule("Reset Irrigation at OH Start")
  12. @when("System started")
  13. def reset_irrigation(event):
  14.     log.info("System started, stopping irrigation")
  15.     stop_all_irrigation()
  16.  
  17.  
  18. @rule("Start Irrigation at 08:00")
  19. @when("Time cron 0 0 8 * * ?")
  20. #@when("Time cron * * * * * ?")
  21. @when("Item Irrigation_Manual received command ON")
  22. def start_irrigation(event):
  23.     #log.info("start_irrigation")
  24.     #log.info(items["Irrigation_Auto"])
  25.     #log.info(items["Irrigation_Manual"])
  26.     if items["Irrigation_Auto"] == ON or items["Irrigation_Manual"] == ON:
  27.         # Reset the gatekeeper
  28.         global gatekeeper
  29.         if gatekeeper:
  30.             gatekeeper.cancel_all()
  31.  
  32.         gatekeeper = Gatekeeper(start_irrigation.log)
  33.  
  34.         # Schedule the Irrigation Zone Commands
  35.         def command_zone(zone, cmd):
  36.             start_irrigation.log.info("{} Irrigation for {}".format("Starting" if cmd == ON else "Stopping"))
  37.             # send_command_if_different(zone, cmd)
  38.             post_update_if_different("Irrigation_Curr", zone.name if cmd == ON else "OFF")
  39.  
  40.         log.info(ir)
  41.         log.info(ir.getItem("gIrrigation"))
  42.         log.info(ir.getItem("gIrrigation").members)
  43.         for zone in sorted(ir.getItem("gIrrigation").members):
  44.             # Turn on the zone
  45.             log.info(zone)
  46.             continue
  47.             gatekeeper.add_command(int(items["{}_Time".format(zone.name)]), lambda: command_zone(zone, ON))
  48.             # Turn off the zone, have one second before turning on the next zone
  49.             gatekeeper.add_command(1, lambda: command_zone(zone, OFF))
  50.  
  51.  
  52. @rule("Cancel Irrigation")
  53. @when("Item Irrigation_Manual received command OFF ")
  54. def cancel_irrigation(event):
  55.     scriptUnloaded()
  56.     stop_all_irrigation()
  57.  
  58.  
  59. def scriptUnloaded():
  60.     global gatekeeper
  61.     if gatekeeper:
  62.         log.info("Cancelled gatekeeper")
  63.         gatekeeper.cancel_all()
  64.         gatekeeper = None
  65.  
  66.  
  67. def stop_all_irrigation():
  68.     for valve in [valve for valve in ir.getItem("gIrrigation").members if valve.state != ON]:
  69.         send_command_if_different(valve, OFF)
  70.     post_update_if_different("Irrigation_Curr", "OFF")
  71.  
  72.  
  73. @rule("Test Irrigation")
  74. @when("Item Irrigation_Test received command ON ")
  75. def cancel_irrigation(event):
  76.     print("Test")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement