state_goto.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. # Reacts to when the Kitchen Worklights button is changed
  133. # This could be from HA UI or from web-interface
  134. @state_trigger("input_boolean.kitchen_worklights")
  135. def kitchen_worklights_button_trigger():
  136. log.info(f"Kitchen worklight changed. State: {input_boolean.kitchen_worklights}")
  137. if( input_boolean.kitchen_worklights=='on' or light.hall_door == 'on' ):
  138. log.info("Kitchen LED: Sec switch ON")
  139. if( switch.kok_led_sec_switch == 'on' ):
  140. log.info("K LED Sec Switch already On, go directly to PWW-Control")
  141. handle_kitchen_worklights()
  142. else:
  143. switch.turn_on(entity_id='switch.kok_led_sec_switch')
  144. else:
  145. log.info("Kitchen LED: Sec switch OFF")
  146. switch.turn_off(entity_id='switch.kok_led_sec_switch')
  147. # Triggers when the IKEA Switch changes state. Goes On or Off
  148. @event_trigger(EVENT_STATE_CHANGED, "entity_id=='switch.kok_led_sec_switch'")
  149. def kitchen_worklights_security_switch_trigger(entity_id, new_state, old_state):
  150. log.info(f"kitchen_worklights_security_switch_trigger() {switch.kok_led_sec_switch}")
  151. if( switch.kok_led_sec_switch=='on' ):
  152. log.info("Kitchen LED: Control PWM Wait...")
  153. task.sleep(0.5)
  154. log.info("Kitchen LED: Go....")
  155. handle_kitchen_worklights()
  156. # Controls the PWM of the kitchen workbench LED-Strips
  157. # Does not control the IKEA Switch that turns everything On/Off
  158. def handle_kitchen_worklights():
  159. log.info(f"handle_kitchen_worklights WL:{input_boolean.kitchen_worklights} HALL:{light.hall_door}")
  160. if( input_boolean.kitchen_worklights == 'on' ):
  161. log.info("K LED Full mode...")
  162. light.turn_on(entity_id='light.kitchen_worktop_4k',brightness='63')
  163. light.turn_on(entity_id='light.kitchen_worktop_27k',brightness='127')
  164. elif( light.hall_door == 'on' ):
  165. log.info("K LED Soft mode...")
  166. light.turn_off(entity_id='light.kitchen_worktop_4k')
  167. light.turn_on(entity_id='light.kitchen_worktop_27k',brightness='12')
  168. else:
  169. log.info("K LED Off mode...")
  170. light.turn_off(entity_id='light.kitchen_worktop_4k')
  171. light.turn_off(entity_id='light.kitchen_worktop_27k')
  172. @state_trigger("input_boolean.movie_mode")
  173. def movie_mode_button_trigger():
  174. cs = getCurrentState()
  175. log.info(f"********* MOVIE MODE *********** To:" + input_boolean.movie_mode + " State: " + str(cs) )
  176. if input_boolean.movie_mode == 'off' and switch.group_button_all == 'on':
  177. log.info(f"Movie mode, turn lights ON")
  178. for x in range(2):
  179. light.turn_on(entity_id='light.liv_room_table_lamp',brightness='1')
  180. task.sleep(0.5)
  181. light.turn_on(entity_id='light.liv_room_corner_lamp',brightness='1')
  182. task.sleep(0.5)
  183. else:
  184. log.info(f"Movie mode, turn lights OFF")
  185. for x in range(2):
  186. light.turn_off(entity_id='light.liv_room_table_lamp')
  187. task.sleep(0.5)
  188. light.turn_off(entity_id='light.liv_room_corner_lamp')
  189. task.sleep(0.5)
  190. @event_trigger(EVENT_STATE_CHANGED, "entity_id=='input_button.av'")
  191. def on_ui_button_all_off_clicken(entity_id, new_state, old_state):
  192. pyscript.state_goto_all_off()
  193. @event_trigger(EVENT_STATE_CHANGED, "entity_id=='input_button.allt_pa'")
  194. def on_ui_button_all_on_clicken(entity_id, new_state, old_state):
  195. pyscript.state_goto_all_on()