state_goto.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. def getCurrentState():
  2. if( light.main_button_1 == 'on' and # All ON
  3. light.main_button_2 == 'on' and
  4. light.main_button_3 == 'on' and
  5. light.main_button_4 == 'on' ):
  6. return 1
  7. elif( light.main_button_1 == 'off' and # Window
  8. light.main_button_2 == 'off' and
  9. light.main_button_3 == 'off' and
  10. light.main_button_4 == 'on' ):
  11. return 2
  12. elif( light.main_button_1 == 'off' and # Morning
  13. light.main_button_2 == 'on' and
  14. light.main_button_3 == 'off' and
  15. light.main_button_4 == 'on' ):
  16. return 3
  17. elif( light.main_button_1 == 'off' and # All OFF
  18. light.main_button_2 == 'off' and
  19. light.main_button_3 == 'off' and
  20. light.main_button_4 == 'off' ):
  21. return 4
  22. @service
  23. def state_goto_all_on():
  24. task.unique('state_goto_py')
  25. cs = getCurrentState()
  26. log.info(f"********* ALL ON *********** State: " + str(cs) )
  27. light.turn_on(entity_id='light.tradfri_bulb')
  28. for x in range(2):
  29. switch.turn_on(entity_id='switch.group_button_all')
  30. task.sleep(1.0)
  31. if light.liv_room_table_lamp == 'off':
  32. light.turn_on(entity_id='light.liv_room_table_lamp')
  33. task.sleep(0.5)
  34. if light.liv_room_corner_lamp == 'off':
  35. light.turn_on(entity_id='light.liv_room_corner_lamp')
  36. pyscript.handle_bedroom_light()
  37. @service
  38. def state_goto_window():
  39. task.unique('state_goto_py')
  40. cs = getCurrentState()
  41. log.info(f"********* WINDOW *********** State: " + str(cs) )
  42. light.turn_off(entity_id='light.tradfri_bulb')
  43. if cs == 4 or cs == 2: # From Off or already Window
  44. for x in range(2):
  45. light.turn_on(entity_id=' light.main_button_4')
  46. task.sleep(0.5)
  47. elif cs == 3: # From morning
  48. for x in range(2):
  49. light.turn_off(entity_id=' light.main_button_2')
  50. task.sleep(0.5)
  51. else: # From ALL ON
  52. for x in range(2):
  53. switch.turn_off(entity_id='switch.group_button_all')
  54. task.sleep(0.5)
  55. light.turn_off(entity_id='light.liv_room_table_lamp')
  56. task.sleep(0.5)
  57. light.turn_off(entity_id='light.liv_room_corner_lamp')
  58. task.sleep(0.5)
  59. for x in range(2):
  60. light.turn_on(entity_id=' light.main_button_4')
  61. task.sleep(0.5)
  62. pyscript.handle_bedroom_light()
  63. @service
  64. def state_goto_morning():
  65. task.unique('state_goto_py')
  66. cs = getCurrentState()
  67. log.info(f"********* MORNING *********** State: " + str(cs) )
  68. light.turn_off(entity_id='light.tradfri_bulb')
  69. if cs == 4 or cs == 3: # Off or already Morning
  70. for x in range(2):
  71. light.turn_on(entity_id=' light.main_button_4')
  72. task.sleep(0.5)
  73. light.turn_on(entity_id=' light.main_button_2')
  74. task.sleep(0.5)
  75. if light.liv_room_corner_lamp == 'off':
  76. light.turn_on(entity_id='light.liv_room_corner_lamp')
  77. elif cs == 2: # From Window
  78. for x in range(2):
  79. light.turn_on(entity_id=' light.main_button_2')
  80. task.sleep(0.5)
  81. if light.liv_room_corner_lamp == 'off':
  82. light.turn_on(entity_id='light.liv_room_corner_lamp')
  83. else: # From ALL ON
  84. for x in range(2):
  85. switch.turn_off(entity_id='switch.group_button_all')
  86. task.sleep(0.5)
  87. for x in range(2):
  88. light.turn_on(entity_id=' light.main_button_2')
  89. task.sleep(0.5)
  90. light.turn_on(entity_id=' light.main_button_4')
  91. task.sleep(0.5)
  92. if light.liv_room_corner_lamp == 'off':
  93. light.turn_on(entity_id='light.liv_room_corner_lamp')
  94. pyscript.handle_bedroom_light()
  95. @service
  96. def state_goto_all_off():
  97. task.unique('state_goto_py')
  98. cs = getCurrentState()
  99. log.info(f"********* ALL OFF *********** State: " + str(cs) )
  100. light.turn_off(entity_id='light.tradfri_bulb')
  101. switch.turn_off(entity_id='switch.tradfri_outlet') # Sovrum OFF
  102. for x in range(2):
  103. switch.turn_off(entity_id='switch.group_button_all')
  104. task.sleep(0.5)
  105. light.turn_off(entity_id='light.liv_room_table_lamp')
  106. task.sleep(0.5)
  107. light.turn_off(entity_id='light.liv_room_corner_lamp')
  108. task.sleep(0.5)
  109. @state_trigger("input_boolean.movie_mode")
  110. def movie_mode_button_trigger():
  111. cs = getCurrentState()
  112. log.info(f"********* MOVIE MODE *********** To:" + input_boolean.movie_mode + " State: " + str(cs) )
  113. if input_boolean.movie_mode == 'off' and switch.group_button_all == 'on':
  114. log.info(f"Movie mode, turn lights ON")
  115. if light.liv_room_table_lamp == 'off':
  116. light.turn_on(entity_id='light.liv_room_table_lamp')
  117. task.sleep(0.5)
  118. if light.liv_room_corner_lamp == 'off':
  119. light.turn_on(entity_id='light.liv_room_corner_lamp')
  120. else:
  121. log.info(f"Movie mode, turn lights OFF")
  122. for x in range(2):
  123. light.turn_off(entity_id='light.liv_room_table_lamp')
  124. task.sleep(0.5)
  125. light.turn_off(entity_id='light.liv_room_corner_lamp')
  126. task.sleep(0.5)