state_goto.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. Also, please don't set a state variable inside a function that is trigged by EVENT_STATE_CHANGED,
  16. or make a service call that is triggered by EVENT_CALL_SERVICE, unless the trigger condition is False
  17. in those cases. Otherwise bad things will happen...
  18. """
  19. @event_trigger(EVENT_CALL_SERVICE, "domain == 'scene' and service == 'turn_on'")
  20. def monitor_scene_service_events(domain=None, service=None, service_data=None):
  21. log.info(f"Scene activated: >{service_data['entity_id']}<")
  22. if( service_data['entity_id'] == 'scene.test_scene_a'):
  23. log.info("Yes. It's A")
  24. elif( service_data['entity_id'] == 'scene.test_scene_b'):
  25. log.info("Yes. It's B")
  26. @state_trigger("input_select.light_mode")
  27. def monitor_service_calls(value=None):
  28. if not value is None:
  29. log.info(f"Light state change to: {value}")
  30. if value == 'All on':
  31. log.info("Go to All on")
  32. pyscript.state_goto_all_on()
  33. elif value == 'All off':
  34. log.info("Go to All off")
  35. pyscript.state_goto_all_off()
  36. elif value == 'Morning':
  37. log.info("Go to Morning")
  38. pyscript.state_goto_morning()
  39. elif value == 'Windows':
  40. log.info("Go to Windows")
  41. pyscript.state_goto_window()
  42. else:
  43. log.info("Light state was None")
  44. @service
  45. def handleWallButtonPressed():
  46. log.info(f"The wall button has been pressed. Curr state: {input_select.light_mode}")
  47. currHour = datetime.now().time().hour
  48. if( input_select.light_mode == 'All on' ):
  49. input_select.light_mode = 'All off'
  50. elif( input_select.light_mode == 'Morning' ):
  51. input_select.light_mode = 'All off'
  52. elif( input_select.light_mode == 'All off' ):
  53. log.info(f'Is All off. Hour is: {currHour}')
  54. if( currHour == 5 or currHour == 6 ):
  55. input_select.light_mode = 'Morning'
  56. else:
  57. input_select.light_mode = 'All on'
  58. elif( input_select.light_mode == 'Windows' ):
  59. input_select.light_mode = 'All on'
  60. @service
  61. def state_goto_all_on():
  62. task.unique('state_goto_py')
  63. log.info(f"********* ALL ON *********** State: " + input_select.light_mode )
  64. light.turn_on(entity_id='light.tradfri_bulb') # Stora flyglampan
  65. light.turn_on(entity_id='light.hall_inner', brightness=38, color_temp_kelvin=3800)
  66. light.turn_on(entity_id='light.hall_door', brightness=38, color_temp_kelvin=3800)
  67. light.turn_on(entity_id='light.rislampa')
  68. switch.turn_on(entity_id='switch.matsal_altandorr')
  69. switch.turn_on(entity_id='switch.matsal_piano')
  70. switch.turn_on(entity_id='switch.koksfonster')
  71. switch.turn_on(entity_id='switch.sovrum')
  72. for x in range(2):
  73. #light.turn_on(entity_id='light.liv_room_table_lamp',brightness='1') # Temp disable when high energy prices
  74. #task.sleep(0.5)
  75. light.turn_on(entity_id='light.liv_room_corner_lamp',brightness='1')
  76. task.sleep(0.5)
  77. pyscript.kitchen_worklights_button_trigger()
  78. #pyscript.handle_bedroom_light() # Temp disable when high energy prices
  79. @service
  80. def state_goto_window():
  81. task.unique('state_goto_py')
  82. log.info(f"********* WINDOW *********** State: " + input_select.light_mode )
  83. light.turn_off(entity_id='light.tradfri_bulb')
  84. switch.turn_on(entity_id='switch.koksfonster')
  85. switch.turn_off(entity_id='switch.matsal_altandorr')
  86. switch.turn_off(entity_id='switch.matsal_piano')
  87. light.turn_off(entity_id='light.hall_inner')
  88. light.turn_off(entity_id='light.hall_door')
  89. light.turn_off(entity_id='light.rislampa')
  90. switch.turn_off(entity_id='switch.sovrum')
  91. for x in range(2):
  92. light.turn_off(entity_id='light.liv_room_table_lamp')
  93. task.sleep(0.5)
  94. light.turn_off(entity_id='light.liv_room_corner_lamp')
  95. task.sleep(0.5)
  96. pyscript.kitchen_worklights_button_trigger()
  97. #pyscript.handle_bedroom_light()
  98. @service
  99. def state_goto_morning():
  100. task.unique('state_goto_py')
  101. log.info(f"********* MORNING *********** State: " + str(input_select.light_mode) )
  102. light.turn_off(entity_id='light.tradfri_bulb')
  103. switch.turn_on(entity_id='switch.matsal_altandorr')
  104. switch.turn_off(entity_id='switch.matsal_piano')
  105. switch.turn_off(entity_id='switch.koksfonster')
  106. light.turn_off(entity_id='light.hall_inner')
  107. light.turn_on(entity_id='light.hall_door', brightness=25, color_temp_kelvin=2570)
  108. light.turn_off(entity_id='light.rislampa')
  109. switch.turn_off(entity_id='switch.sovrum')
  110. for x in range(2):
  111. #light.turn_on(entity_id='light.liv_room_corner_lamp',brightness='1')
  112. light.turn_off(entity_id='light.liv_room_corner_lamp')
  113. task.sleep(0.5)
  114. pyscript.kitchen_worklights_button_trigger()
  115. #pyscript.handle_bedroom_light()
  116. @service
  117. def state_goto_all_off():
  118. task.unique('state_goto_py')
  119. log.info(f"********* ALL OFF *********** State: " + input_select.light_mode )
  120. light.turn_off(entity_id='light.tradfri_bulb')
  121. light.turn_off(entity_id='light.hall_inner')
  122. light.turn_off(entity_id='light.hall_door')
  123. light.turn_off(entity_id='light.rislampa')
  124. switch.turn_off(entity_id='switch.koksfonster')
  125. switch.turn_off(entity_id='switch.matsal_altandorr')
  126. switch.turn_off(entity_id='switch.matsal_piano')
  127. switch.turn_off(entity_id='switch.sovrum')
  128. for x in range(2):
  129. #light.turn_off(entity_id='light.liv_room_table_lamp') # Temp disable when high energy prices
  130. #task.sleep(0.5)
  131. light.turn_off(entity_id='light.liv_room_corner_lamp')
  132. task.sleep(0.5)
  133. input_boolean.movie_mode = 'off'
  134. pyscript.kitchen_worklights_button_trigger()
  135. @state_trigger("input_boolean.movie_mode")
  136. def movie_mode_button_trigger():
  137. log.info(f"********* MOVIE MODE *********** To:" + input_boolean.movie_mode + " State: " + input_select.light_mode )
  138. if input_boolean.movie_mode == 'off' and input_select.light_mode == 'All on':
  139. log.info(f"Movie mode, turn lights ON")
  140. for x in range(2):
  141. light.turn_on(entity_id='light.liv_room_table_lamp',brightness='1')
  142. task.sleep(0.5)
  143. light.turn_on(entity_id='light.liv_room_corner_lamp',brightness='1')
  144. task.sleep(0.5)
  145. else:
  146. log.info(f"Movie mode, turn lights OFF")
  147. for x in range(2):
  148. light.turn_off(entity_id='light.liv_room_table_lamp')
  149. task.sleep(0.5)
  150. light.turn_off(entity_id='light.liv_room_corner_lamp')
  151. task.sleep(0.5)