state_goto.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. from homeassistant.const import EVENT_STATE_CHANGED
  2. def getCurrentState():
  3. if( light.main_button_1 == 'on' and # All ON
  4. light.main_button_2 == 'on' and
  5. light.main_button_3 == 'on' and
  6. light.main_button_4 == 'on' ):
  7. return 1
  8. elif( light.main_button_1 == 'off' and # Window
  9. light.main_button_2 == 'off' and
  10. light.main_button_3 == 'off' and
  11. light.main_button_4 == 'on' ):
  12. return 2
  13. elif( light.main_button_1 == 'off' and # Morning
  14. light.main_button_2 == 'on' and
  15. light.main_button_3 == 'off' and
  16. light.main_button_4 == 'on' ):
  17. return 3
  18. elif( light.main_button_1 == 'off' and # All OFF
  19. light.main_button_2 == 'off' and
  20. light.main_button_3 == 'off' and
  21. light.main_button_4 == 'off' ):
  22. return 4
  23. @service
  24. def state_goto_all_on():
  25. task.unique('state_goto_py')
  26. cs = getCurrentState()
  27. log.info(f"********* ALL ON *********** State: " + str(cs) )
  28. light.turn_on(entity_id='light.tradfri_bulb') # Stora flyglampan
  29. light.turn_on(entity_id='light.hall_inner')
  30. light.turn_on(entity_id='light.hall_door')
  31. #switch.turn_on(entity_id='switch.matsal')
  32. switch.turn_on(entity_id='switch.koksfonster')
  33. for x in range(2):
  34. switch.turn_on(entity_id='switch.group_button_all')
  35. task.sleep(0.5)
  36. #light.turn_on(entity_id='light.liv_room_table_lamp',brightness='1') # Temp disable when high energy prices
  37. #task.sleep(0.5)
  38. light.turn_on(entity_id='light.liv_room_corner_lamp',brightness='1')
  39. task.sleep(0.5)
  40. kitchen_worklights_button_trigger()
  41. #pyscript.handle_bedroom_light() # Temp disable when high energy prices
  42. @service
  43. def state_goto_window():
  44. task.unique('state_goto_py')
  45. cs = getCurrentState()
  46. log.info(f"********* WINDOW *********** State: " + str(cs) )
  47. light.turn_off(entity_id='light.tradfri_bulb')
  48. #switch.turn_off(entity_id='switch.matsal')
  49. switch.turn_on(entity_id='switch.koksfonster')
  50. light.turn_off(entity_id='light.hall_inner')
  51. light.turn_off(entity_id='light.hall_door')
  52. if cs == 4 or cs == 2: # From Off or already Window
  53. for x in range(2):
  54. light.turn_on(entity_id=' light.main_button_4')
  55. task.sleep(0.5)
  56. elif cs == 3: # From morning
  57. for x in range(2):
  58. light.turn_off(entity_id=' light.main_button_2')
  59. task.sleep(0.5)
  60. light.turn_off(entity_id='light.liv_room_corner_lamp')
  61. task.sleep(0.5)
  62. else: # From ALL ON
  63. for x in range(2):
  64. switch.turn_off(entity_id='switch.group_button_all')
  65. task.sleep(0.5)
  66. light.turn_off(entity_id='light.liv_room_table_lamp')
  67. task.sleep(0.5)
  68. light.turn_off(entity_id='light.liv_room_corner_lamp')
  69. task.sleep(0.5)
  70. for x in range(2):
  71. light.turn_on(entity_id=' light.main_button_4')
  72. task.sleep(0.5)
  73. kitchen_worklights_button_trigger()
  74. #pyscript.handle_bedroom_light()
  75. @service
  76. def state_goto_morning():
  77. task.unique('state_goto_py')
  78. cs = getCurrentState()
  79. log.info(f"********* MORNING *********** State: " + str(cs) )
  80. light.turn_off(entity_id='light.tradfri_bulb')
  81. #switch.turn_off(entity_id='switch.matsal')
  82. switch.turn_on(entity_id='switch.koksfonster')
  83. light.turn_off(entity_id='light.hall_inner')
  84. light.turn_on(entity_id='light.hall_door')
  85. if cs == 4 or cs == 3: # Off or already Morning
  86. for x in range(2):
  87. light.turn_on(entity_id=' light.main_button_4')
  88. task.sleep(0.5)
  89. light.turn_on(entity_id=' light.main_button_2')
  90. task.sleep(0.5)
  91. light.turn_on(entity_id='light.liv_room_corner_lamp',brightness='1')
  92. task.sleep(0.5)
  93. elif cs == 2: # From Window
  94. for x in range(2):
  95. light.turn_on(entity_id=' light.main_button_2')
  96. task.sleep(0.5)
  97. light.turn_on(entity_id='light.liv_room_corner_lamp',brightness='1')
  98. task.sleep(0.5)
  99. else: # From ALL ON
  100. for x in range(2):
  101. switch.turn_off(entity_id='switch.group_button_all')
  102. task.sleep(0.5)
  103. for x in range(2):
  104. light.turn_on(entity_id=' light.main_button_2')
  105. task.sleep(0.5)
  106. light.turn_on(entity_id=' light.main_button_4')
  107. task.sleep(0.5)
  108. light.turn_on(entity_id='light.liv_room_corner_lamp',brightness='1')
  109. task.sleep(0.5)
  110. kitchen_worklights_button_trigger()
  111. #pyscript.handle_bedroom_light()
  112. @service
  113. def state_goto_all_off():
  114. task.unique('state_goto_py')
  115. cs = getCurrentState()
  116. log.info(f"********* ALL OFF *********** State: " + str(cs) )
  117. light.turn_off(entity_id='light.tradfri_bulb')
  118. light.turn_off(entity_id='light.hall_inner')
  119. light.turn_off(entity_id='light.hall_door')
  120. switch.turn_off(entity_id='switch.koksfonster')
  121. #switch.turn_off(entity_id='switch.matsal') # Temp disable when high energy prices
  122. #switch.turn_off(entity_id='switch.tradfri_outlet') # Sovrum OFF # Temp disable when high energy prices
  123. for x in range(2):
  124. switch.turn_off(entity_id='switch.group_button_all')
  125. task.sleep(0.5)
  126. #light.turn_off(entity_id='light.liv_room_table_lamp') # Temp disable when high energy prices
  127. #task.sleep(0.5)
  128. light.turn_off(entity_id='light.liv_room_corner_lamp')
  129. task.sleep(0.5)
  130. input_boolean.movie_mode = 'off'
  131. kitchen_worklights_button_trigger()
  132. @state_trigger("input_boolean.movie_mode")
  133. def movie_mode_button_trigger():
  134. cs = getCurrentState()
  135. log.info(f"********* MOVIE MODE *********** To:" + input_boolean.movie_mode + " State: " + str(cs) )
  136. if input_boolean.movie_mode == 'off' and switch.group_button_all == 'on':
  137. log.info(f"Movie mode, turn lights ON")
  138. for x in range(2):
  139. light.turn_on(entity_id='light.liv_room_table_lamp',brightness='1')
  140. task.sleep(0.5)
  141. light.turn_on(entity_id='light.liv_room_corner_lamp',brightness='1')
  142. task.sleep(0.5)
  143. else:
  144. log.info(f"Movie mode, turn lights OFF")
  145. for x in range(2):
  146. light.turn_off(entity_id='light.liv_room_table_lamp')
  147. task.sleep(0.5)
  148. light.turn_off(entity_id='light.liv_room_corner_lamp')
  149. task.sleep(0.5)
  150. @event_trigger(EVENT_STATE_CHANGED, "entity_id=='input_button.av'")
  151. def on_ui_button_all_off_clicked(entity_id, new_state, old_state):
  152. pyscript.state_goto_all_off()
  153. @event_trigger(EVENT_STATE_CHANGED, "entity_id=='input_button.allt_pa'")
  154. def on_ui_button_all_on_clicked(entity_id, new_state, old_state):
  155. pyscript.state_goto_all_on()