state_goto.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 == 'Windows':
  58. log.info("Go to Windows")
  59. pyscript.state_goto_window()
  60. elif value == 'Re-trigger':
  61. log.info("Re-trigger to be done here")
  62. # Set mode the the previous mode
  63. input_select.light_mode = old_value
  64. else:
  65. log.info("Light state was None")
  66. @service
  67. def handleWallButtonPressed():
  68. log.info(f"The wall button has been pressed. Curr state: {input_select.light_mode}")
  69. currHour = datetime.now().time().hour
  70. if( input_select.light_mode == 'All on' ):
  71. input_select.light_mode = 'All off'
  72. elif( input_select.light_mode == 'Morning' ):
  73. input_select.light_mode = 'All off'
  74. elif( input_select.light_mode == 'All off' ):
  75. log.info(f'Is All off. Hour is: {currHour}')
  76. if( currHour == 5 or currHour == 6 ):
  77. input_select.light_mode = 'Morning'
  78. else:
  79. input_select.light_mode = 'All on'
  80. elif( input_select.light_mode == 'Windows' ):
  81. input_select.light_mode = 'All on'
  82. @service
  83. def state_goto_all_on():
  84. task.unique('state_goto_py')
  85. isChristmas = True if input_boolean.christmas == 'on' else False
  86. log.info(f"********* ALL ON *********** State:{input_select.light_mode} Jul:{isChristmas}")
  87. light.turn_on(entity_id='light.tradfri_bulb') # Stora flyglampan
  88. light.turn_on(entity_id='light.hall_inner', brightness=38, color_temp_kelvin=3800)
  89. light.turn_on(entity_id='light.hall_door', brightness=38, color_temp_kelvin=3800)
  90. light.turn_on(entity_id='light.rislampa')
  91. switch.turn_on(entity_id='switch.matsal_altandorr')
  92. switch.turn_on(entity_id='switch.matsal_piano')
  93. switch.turn_on(entity_id='switch.koksfonster')
  94. switch.turn_on(entity_id='switch.sovrum')
  95. for x in range(2):
  96. #light.turn_on(entity_id='light.liv_room_table_lamp',brightness='1') # Temp disable when high energy prices
  97. #task.sleep(0.5)
  98. light.turn_on(entity_id='light.liv_room_corner_lamp',brightness='1')
  99. task.sleep(0.5)
  100. if( isChristmas ):
  101. light.turn_on(entity_id=' light.main_button_4') # Christmas lights
  102. task.sleep(0.5)
  103. pyscript.kitchen_worklights_button_trigger()
  104. #pyscript.handle_bedroom_light() # Temp disable when high energy prices
  105. @service
  106. def state_goto_window():
  107. task.unique('state_goto_py')
  108. isChristmas = True if input_boolean.christmas == 'on' else False
  109. log.info(f"********* WINDOW *********** State:{input_select.light_mode} Jul:{isChristmas}")
  110. light.turn_off(entity_id='light.tradfri_bulb')
  111. switch.turn_on(entity_id='switch.koksfonster')
  112. if( isChristmas ):
  113. switch.turn_on(entity_id='switch.matsal_altandorr')
  114. else:
  115. switch.turn_off(entity_id='switch.matsal_altandorr')
  116. switch.turn_off(entity_id='switch.matsal_piano')
  117. light.turn_off(entity_id='light.hall_inner')
  118. light.turn_off(entity_id='light.hall_door')
  119. light.turn_off(entity_id='light.rislampa')
  120. switch.turn_off(entity_id='switch.sovrum')
  121. for x in range(2):
  122. #light.turn_off(entity_id='light.liv_room_table_lamp')
  123. #task.sleep(0.5)
  124. light.turn_off(entity_id='light.liv_room_corner_lamp')
  125. task.sleep(0.5)
  126. if( isChristmas ):
  127. light.turn_on(entity_id=' light.main_button_4') # Christmas lights
  128. task.sleep(0.5)
  129. pyscript.kitchen_worklights_button_trigger()
  130. #pyscript.handle_bedroom_light()
  131. @service
  132. def state_goto_morning():
  133. task.unique('state_goto_py')
  134. isChristmas = True if input_boolean.christmas == 'on' else False
  135. log.info(f"********* MORNING *********** State:{input_select.light_mode} Jul:{isChristmas}")
  136. light.turn_off(entity_id='light.tradfri_bulb')
  137. switch.turn_on(entity_id='switch.matsal_altandorr')
  138. switch.turn_off(entity_id='switch.matsal_piano')
  139. if( isChristmas ):
  140. switch.turn_on(entity_id='switch.koksfonster')
  141. else:
  142. switch.turn_off(entity_id='switch.koksfonster')
  143. light.turn_off(entity_id='light.hall_inner')
  144. light.turn_on(entity_id='light.hall_door', brightness=25, color_temp_kelvin=2570)
  145. light.turn_off(entity_id='light.rislampa')
  146. switch.turn_off(entity_id='switch.sovrum')
  147. for x in range(2):
  148. #light.turn_on(entity_id='light.liv_room_corner_lamp',brightness='1')
  149. light.turn_off(entity_id='light.liv_room_corner_lamp')
  150. task.sleep(0.5)
  151. if( isChristmas ):
  152. light.turn_off(entity_id=' light.main_button_4') # Christmas lights
  153. task.sleep(0.5)
  154. pyscript.kitchen_worklights_button_trigger()
  155. #pyscript.handle_bedroom_light()
  156. @service
  157. def state_goto_all_off():
  158. task.unique('state_goto_py')
  159. isChristmas = True if input_boolean.christmas == 'on' else False
  160. log.info(f"********* ALL OFF *********** State:{input_select.light_mode} Jul:{isChristmas}")
  161. light.turn_off(entity_id='light.tradfri_bulb')
  162. light.turn_off(entity_id='light.hall_inner')
  163. light.turn_off(entity_id='light.hall_door')
  164. light.turn_off(entity_id='light.rislampa')
  165. switch.turn_off(entity_id='switch.koksfonster')
  166. switch.turn_off(entity_id='switch.matsal_altandorr')
  167. switch.turn_off(entity_id='switch.matsal_piano')
  168. switch.turn_off(entity_id='switch.sovrum')
  169. for x in range(2):
  170. #light.turn_off(entity_id='light.liv_room_table_lamp') # Temp disable when high energy prices
  171. #task.sleep(0.5)
  172. light.turn_off(entity_id='light.liv_room_corner_lamp')
  173. task.sleep(0.5)
  174. if( isChristmas ):
  175. light.turn_off(entity_id=' light.main_button_4') # Christmas lights
  176. task.sleep(0.5)
  177. input_boolean.movie_mode = 'off'
  178. pyscript.kitchen_worklights_button_trigger()
  179. @state_trigger("input_boolean.movie_mode")
  180. def movie_mode_button_trigger():
  181. log.info(f"********* MOVIE MODE *********** To:" + input_boolean.movie_mode + " State: " + input_select.light_mode )
  182. if input_boolean.movie_mode == 'off' and input_select.light_mode == 'All on':
  183. log.info(f"Movie mode, turn lights ON")
  184. for x in range(2):
  185. light.turn_on(entity_id='light.liv_room_table_lamp',brightness='1')
  186. task.sleep(0.5)
  187. light.turn_on(entity_id='light.liv_room_corner_lamp',brightness='1')
  188. task.sleep(0.5)
  189. else:
  190. log.info(f"Movie mode, turn lights OFF")
  191. for x in range(2):
  192. light.turn_off(entity_id='light.liv_room_table_lamp')
  193. task.sleep(0.5)
  194. light.turn_off(entity_id='light.liv_room_corner_lamp')
  195. task.sleep(0.5)