state_goto.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. for x in range(2):
  33. switch.turn_on(entity_id='switch.group_button_all')
  34. task.sleep(0.5)
  35. #light.turn_on(entity_id='light.liv_room_table_lamp',brightness='1') # Temp disable when high energy prices
  36. #task.sleep(0.5)
  37. light.turn_on(entity_id='light.liv_room_corner_lamp',brightness='1')
  38. task.sleep(0.5)
  39. handle_kitchen_worklights()
  40. #pyscript.handle_bedroom_light() # Temp disable when high energy prices
  41. @service
  42. def state_goto_window():
  43. task.unique('state_goto_py')
  44. cs = getCurrentState()
  45. log.info(f"********* WINDOW *********** State: " + str(cs) )
  46. light.turn_off(entity_id='light.tradfri_bulb')
  47. switch.turn_off(entity_id='switch.matsal')
  48. light.turn_off(entity_id='light.hall_inner')
  49. light.turn_off(entity_id='light.hall_door')
  50. if cs == 4 or cs == 2: # From Off or already Window
  51. for x in range(2):
  52. light.turn_on(entity_id=' light.main_button_4')
  53. task.sleep(0.5)
  54. elif cs == 3: # From morning
  55. for x in range(2):
  56. light.turn_off(entity_id=' light.main_button_2')
  57. task.sleep(0.5)
  58. light.turn_off(entity_id='light.liv_room_corner_lamp')
  59. task.sleep(0.5)
  60. else: # From ALL ON
  61. for x in range(2):
  62. switch.turn_off(entity_id='switch.group_button_all')
  63. task.sleep(0.5)
  64. light.turn_off(entity_id='light.liv_room_table_lamp')
  65. task.sleep(0.5)
  66. light.turn_off(entity_id='light.liv_room_corner_lamp')
  67. task.sleep(0.5)
  68. for x in range(2):
  69. light.turn_on(entity_id=' light.main_button_4')
  70. task.sleep(0.5)
  71. handle_kitchen_worklights()
  72. #pyscript.handle_bedroom_light()
  73. @service
  74. def state_goto_morning():
  75. task.unique('state_goto_py')
  76. cs = getCurrentState()
  77. log.info(f"********* MORNING *********** State: " + str(cs) )
  78. light.turn_off(entity_id='light.tradfri_bulb')
  79. switch.turn_off(entity_id='switch.matsal')
  80. light.turn_off(entity_id='light.hall_inner')
  81. light.turn_on(entity_id='light.hall_door')
  82. if cs == 4 or cs == 3: # Off or already Morning
  83. for x in range(2):
  84. light.turn_on(entity_id=' light.main_button_4')
  85. task.sleep(0.5)
  86. light.turn_on(entity_id=' light.main_button_2')
  87. task.sleep(0.5)
  88. light.turn_on(entity_id='light.liv_room_corner_lamp',brightness='1')
  89. task.sleep(0.5)
  90. elif cs == 2: # From Window
  91. for x in range(2):
  92. light.turn_on(entity_id=' light.main_button_2')
  93. task.sleep(0.5)
  94. light.turn_on(entity_id='light.liv_room_corner_lamp',brightness='1')
  95. task.sleep(0.5)
  96. else: # From ALL ON
  97. for x in range(2):
  98. switch.turn_off(entity_id='switch.group_button_all')
  99. task.sleep(0.5)
  100. for x in range(2):
  101. light.turn_on(entity_id=' light.main_button_2')
  102. task.sleep(0.5)
  103. light.turn_on(entity_id=' light.main_button_4')
  104. task.sleep(0.5)
  105. light.turn_on(entity_id='light.liv_room_corner_lamp',brightness='1')
  106. task.sleep(0.5)
  107. handle_kitchen_worklights()
  108. #pyscript.handle_bedroom_light()
  109. @service
  110. def state_goto_all_off():
  111. task.unique('state_goto_py')
  112. cs = getCurrentState()
  113. log.info(f"********* ALL OFF *********** State: " + str(cs) )
  114. light.turn_off(entity_id='light.tradfri_bulb')
  115. light.turn_off(entity_id='light.hall_inner')
  116. light.turn_off(entity_id='light.hall_door')
  117. #switch.turn_off(entity_id='switch.matsal') # Temp disable when high energy prices
  118. #switch.turn_off(entity_id='switch.tradfri_outlet') # Sovrum OFF # Temp disable when high energy prices
  119. for x in range(2):
  120. switch.turn_off(entity_id='switch.group_button_all')
  121. task.sleep(0.5)
  122. #light.turn_off(entity_id='light.liv_room_table_lamp') # Temp disable when high energy prices
  123. #task.sleep(0.5)
  124. light.turn_off(entity_id='light.liv_room_corner_lamp')
  125. task.sleep(0.5)
  126. input_boolean.movie_mode = 'off'
  127. handle_kitchen_worklights()
  128. @state_trigger("input_boolean.kitchen_worklights")
  129. def kitchen_worklights_button_trigger():
  130. log.info(f"Kitchen worklight changed. State: {input_boolean.kitchen_worklights}")
  131. handle_kitchen_worklights()
  132. def handle_kitchen_worklights():
  133. log.info("handle_kitchen_worklights")
  134. if( input_boolean.kitchen_worklights == 'on' ):
  135. light.turn_on(entity_id='light.kitchen_worktop_4k',brightness='63')
  136. light.turn_on(entity_id='light.kitchen_worktop_27k',brightness='127')
  137. elif( light.hall_door == 'on' ):
  138. light.turn_off(entity_id='light.kitchen_worktop_4k')
  139. light.turn_on(entity_id='light.kitchen_worktop_27k',brightness='12')
  140. else:
  141. light.turn_off(entity_id='light.kitchen_worktop_4k')
  142. light.turn_off(entity_id='light.kitchen_worktop_27k')
  143. @state_trigger("input_boolean.movie_mode")
  144. def movie_mode_button_trigger():
  145. cs = getCurrentState()
  146. log.info(f"********* MOVIE MODE *********** To:" + input_boolean.movie_mode + " State: " + str(cs) )
  147. if input_boolean.movie_mode == 'off' and switch.group_button_all == 'on':
  148. log.info(f"Movie mode, turn lights ON")
  149. for x in range(2):
  150. light.turn_on(entity_id='light.liv_room_table_lamp',brightness='1')
  151. task.sleep(0.5)
  152. light.turn_on(entity_id='light.liv_room_corner_lamp',brightness='1')
  153. task.sleep(0.5)
  154. else:
  155. log.info(f"Movie mode, turn lights OFF")
  156. for x in range(2):
  157. light.turn_off(entity_id='light.liv_room_table_lamp')
  158. task.sleep(0.5)
  159. light.turn_off(entity_id='light.liv_room_corner_lamp')
  160. task.sleep(0.5)
  161. @event_trigger(EVENT_STATE_CHANGED, "entity_id=='input_button.av'")
  162. def on_ui_button_all_off_clicken(entity_id, new_state, old_state):
  163. pyscript.state_goto_all_off()
  164. @event_trigger(EVENT_STATE_CHANGED, "entity_id=='input_button.allt_pa'")
  165. def on_ui_button_all_on_clicken(entity_id, new_state, old_state):
  166. pyscript.state_goto_all_on()