state_goto.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. from homeassistant.const import EVENT_STATE_CHANGED, EVENT_CALL_SERVICE
  2. from datetime import datetime, time
  3. """
  4. When receiving a scene activation event (Service call)
  5. got EVENT_STATE_CHANGED with kwargs=
  6. {'trigger_type': 'event',
  7. 'event_type': 'call_service',
  8. 'context': <homeassistant.core.Context object at 0x7ff02cde4800>,
  9. 'domain': 'scene',
  10. 'service': 'turn_on',
  11. 'service_data': {'entity_id': 'scene.test_scene_a'}
  12. }
  13. """
  14. """
  15. monitor_the_light_mode_selector() kwargs=
  16. {'trigger_type': 'state',
  17. 'var_name': 'input_select.light_mode',
  18. 'value': 'Morning',
  19. 'old_value': 'No action',
  20. 'context': <homeassistant.core.Context object at 0x7f3b26bff640>}
  21. """
  22. #@state_trigger("input_boolean.christmas")
  23. #def a_test_for_monitor_the_light_mode_selector(value=None, old_value=None):
  24. #def a_test_for_monitor_the_light_mode_selector(**kwargs):
  25. #log.info(f"a_test_for_monitor_the_light_mode_selector() kwargs={kwargs}")
  26. #log.info(f"a_test_for_monitor_the_light_mode_selector() {value} {old_value}")
  27. #log.info(f"Value:{input_boolean.christmas} Type:" + str( type(input_boolean.christmas) ))
  28. #if( input_boolean.christmas == 'on' ): log.info("ON")
  29. #if( input_boolean.christmas == 'off' ): log.info("OFF")
  30. #isChristmas = True if input_boolean.christmas == 'on' else False
  31. #log.info("BooL: " + str(x))
  32. """
  33. Also, please don't set a state variable inside a function that is trigged by EVENT_STATE_CHANGED,
  34. or make a service call that is triggered by EVENT_CALL_SERVICE, unless the trigger condition is False
  35. in those cases. Otherwise bad things will happen...
  36. """
  37. #@event_trigger(EVENT_CALL_SERVICE, "domain == 'scene' and service == 'turn_on'")
  38. #def monitor_scene_service_events(domain=None, service=None, service_data=None):
  39. # log.info(f"Scene activated: >{service_data['entity_id']}<")
  40. # if( service_data['entity_id'] == 'scene.test_scene_a'):
  41. # log.info("Yes. It's A")
  42. # elif( service_data['entity_id'] == 'scene.test_scene_b'):
  43. # log.info("Yes. It's B")
  44. @state_trigger("input_select.light_mode")
  45. def monitor_light_mode_selector(value=None, old_value=None):
  46. if not value is None:
  47. log.info(f"Light state change to: {value}, old value: {old_value}")
  48. if value == 'All on':
  49. log.info("Go to All on")
  50. pyscript.state_goto_all_on()
  51. elif value == 'All off':
  52. log.info("Go to All off")
  53. pyscript.state_goto_all_off()
  54. elif value == 'Morning':
  55. log.info("Go to Morning")
  56. pyscript.state_goto_morning()
  57. elif value == 'Dimmed morning':
  58. log.info("Go to Dimmed morning")
  59. pyscript.state_goto_dimmed_morning()
  60. elif value == 'Windows':
  61. log.info("Go to Windows")
  62. pyscript.state_goto_window()
  63. elif value == 'Re-trigger':
  64. log.info("Re-trigger to be done here")
  65. # Set mode the the previous mode
  66. input_select.light_mode = old_value
  67. else:
  68. log.info("Light state was None")
  69. @service
  70. def handleWallButtonPressed():
  71. log.info(f"The wall button has been pressed. Curr state: {input_select.light_mode}")
  72. currHour = datetime.now().time().hour
  73. if( input_select.light_mode == 'All on' ):
  74. input_select.light_mode = 'All off'
  75. elif( input_select.light_mode == 'Morning' ):
  76. input_select.light_mode = 'All off'
  77. elif( input_select.light_mode == 'All off' ):
  78. log.info(f'Is All off. Hour is: {currHour}')
  79. if( currHour == 5 or currHour == 6 ):
  80. input_select.light_mode = 'Morning'
  81. else:
  82. input_select.light_mode = 'All on'
  83. elif( input_select.light_mode == 'Windows' ):
  84. input_select.light_mode = 'All on'
  85. @service
  86. def state_goto_all_on():
  87. task.unique('state_goto_py')
  88. isChristmas = True if input_boolean.christmas == 'on' else False
  89. log.info(f"********* ALL ON *********** State:{input_select.light_mode} Jul:{isChristmas}")
  90. light.turn_on(entity_id='light.tradfri_bulb') # Stora flyglampan
  91. light.turn_on(entity_id='light.hall_inner', brightness=38, color_temp_kelvin=3800)
  92. light.turn_on(entity_id='light.hall_door', brightness=38, color_temp_kelvin=3800)
  93. light.turn_on(entity_id='light.rislampa')
  94. switch.turn_on(entity_id='switch.matsal_altandorr')
  95. switch.turn_on(entity_id='switch.matsal_piano')
  96. switch.turn_on(entity_id='switch.koksfonster')
  97. switch.turn_on(entity_id='switch.sovrum')
  98. light.turn_on(entity_id='light.shellydimmer1',brightness_pct=15)
  99. for x in range(2):
  100. #light.turn_on(entity_id='light.liv_room_table_lamp',brightness='1') # Temp disable when high energy prices
  101. #task.sleep(0.5)
  102. task.sleep(0.5)
  103. if( isChristmas ):
  104. light.turn_on(entity_id=' light.main_button_4') # Christmas lights
  105. task.sleep(0.5)
  106. pyscript.kitchen_worklights_button_trigger()
  107. #pyscript.handle_bedroom_light() # Temp disable when high energy prices
  108. @service
  109. def state_goto_window():
  110. task.unique('state_goto_py')
  111. isChristmas = True if input_boolean.christmas == 'on' else False
  112. log.info(f"********* WINDOW *********** State:{input_select.light_mode} Jul:{isChristmas}")
  113. light.turn_off(entity_id='light.tradfri_bulb')
  114. switch.turn_on(entity_id='switch.koksfonster')
  115. if( isChristmas ):
  116. switch.turn_on(entity_id='switch.matsal_altandorr')
  117. else:
  118. switch.turn_off(entity_id='switch.matsal_altandorr')
  119. switch.turn_off(entity_id='switch.matsal_piano')
  120. light.turn_off(entity_id='light.hall_inner')
  121. light.turn_off(entity_id='light.hall_door')
  122. light.turn_off(entity_id='light.rislampa')
  123. switch.turn_off(entity_id='switch.sovrum')
  124. light.turn_off(entity_id='light.shellydimmer1')
  125. for x in range(2):
  126. #light.turn_off(entity_id='light.liv_room_table_lamp')
  127. #task.sleep(0.5)
  128. task.sleep(0.5)
  129. if( isChristmas ):
  130. light.turn_on(entity_id=' light.main_button_4') # Christmas lights
  131. task.sleep(0.5)
  132. pyscript.kitchen_worklights_button_trigger()
  133. #pyscript.handle_bedroom_light()
  134. @service
  135. def state_goto_morning():
  136. task.unique('state_goto_py')
  137. isChristmas = True if input_boolean.christmas == 'on' else False
  138. log.info(f"********* MORNING *********** State:{input_select.light_mode} Jul:{isChristmas}")
  139. light.turn_off(entity_id='light.tradfri_bulb')
  140. switch.turn_on(entity_id='switch.matsal_altandorr')
  141. switch.turn_off(entity_id='switch.matsal_piano')
  142. if( isChristmas ):
  143. switch.turn_off(entity_id='switch.koksfonster')
  144. #switch.turn_on(entity_id='switch.koksfonster')
  145. else:
  146. switch.turn_off(entity_id='switch.koksfonster')
  147. light.turn_off(entity_id='light.hall_inner')
  148. light.turn_on(entity_id='light.hall_door', brightness=25, color_temp_kelvin=2570)
  149. light.turn_off(entity_id='light.rislampa')
  150. switch.turn_off(entity_id='switch.sovrum')
  151. light.turn_off(entity_id='light.shellydimmer1')
  152. for x in range(2):
  153. task.sleep(0.5)
  154. if( isChristmas ):
  155. light.turn_off(entity_id=' light.main_button_4') # Christmas lights
  156. task.sleep(0.5)
  157. pyscript.kitchen_worklights_button_trigger()
  158. #pyscript.handle_bedroom_light()
  159. @service
  160. def state_goto_dimmed_morning():
  161. task.unique('state_goto_py')
  162. isChristmas = True if input_boolean.christmas == 'on' else False
  163. log.info(f"********* DIMMED MORNING *********** State:{input_select.light_mode} Jul:{isChristmas}")
  164. light.turn_off(entity_id='light.tradfri_bulb')
  165. switch.turn_off(entity_id='switch.matsal_altandorr')
  166. switch.turn_off(entity_id='switch.matsal_piano')
  167. if( isChristmas ):
  168. switch.turn_off(entity_id='switch.koksfonster')
  169. else:
  170. switch.turn_off(entity_id='switch.koksfonster')
  171. light.turn_off(entity_id='light.hall_inner')
  172. light.turn_on(entity_id='light.hall_door', brightness=3, color_temp_kelvin=2202)
  173. light.turn_off(entity_id='light.rislampa')
  174. switch.turn_off(entity_id='switch.sovrum')
  175. light.turn_off(entity_id='light.shellydimmer1')
  176. for x in range(2):
  177. task.sleep(0.5)
  178. if( isChristmas ):
  179. light.turn_off(entity_id=' light.main_button_4') # Christmas lights
  180. task.sleep(0.5)
  181. pyscript.kitchen_worklights_button_trigger()
  182. #pyscript.handle_bedroom_light()
  183. @service
  184. def state_goto_all_off():
  185. task.unique('state_goto_py')
  186. isChristmas = True if input_boolean.christmas == 'on' else False
  187. log.info(f"********* ALL OFF *********** State:{input_select.light_mode} Jul:{isChristmas}")
  188. light.turn_off(entity_id='light.tradfri_bulb')
  189. light.turn_off(entity_id='light.hall_inner')
  190. light.turn_off(entity_id='light.hall_door')
  191. light.turn_off(entity_id='light.rislampa')
  192. switch.turn_off(entity_id='switch.koksfonster')
  193. switch.turn_off(entity_id='switch.matsal_altandorr')
  194. switch.turn_off(entity_id='switch.matsal_piano')
  195. switch.turn_off(entity_id='switch.sovrum')
  196. light.turn_off(entity_id='light.shellydimmer1')
  197. for x in range(2):
  198. #light.turn_off(entity_id='light.liv_room_table_lamp') # Temp disable when high energy prices
  199. #task.sleep(0.5)
  200. task.sleep(0.5)
  201. if( isChristmas ):
  202. light.turn_off(entity_id=' light.main_button_4') # Christmas lights
  203. task.sleep(0.5)
  204. input_boolean.movie_mode = 'off'
  205. pyscript.kitchen_worklights_button_trigger()
  206. @state_trigger("input_boolean.movie_mode")
  207. def movie_mode_button_trigger():
  208. log.info(f"********* MOVIE MODE *********** To:" + input_boolean.movie_mode + " State: " + input_select.light_mode )
  209. if input_boolean.movie_mode == 'off' and input_select.light_mode == 'All on':
  210. log.info(f"Movie mode, turn lights ON")
  211. light.turn_on(entity_id='light.shellydimmer1',brightness_pct=15)
  212. for x in range(2):
  213. light.turn_on(entity_id='light.liv_room_table_lamp',brightness='1')
  214. task.sleep(0.5)
  215. else:
  216. log.info(f"Movie mode, turn lights OFF")
  217. light.turn_off(entity_id='light.shellydimmer1')
  218. for x in range(2):
  219. light.turn_off(entity_id='light.liv_room_table_lamp')
  220. task.sleep(0.5)