state_goto.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. from homeassistant.const import EVENT_STATE_CHANGED
  2. from datetime import datetime, time
  3. @state_trigger("input_select.light_mode")
  4. def monitor_service_calls(value=None):
  5. if not value is None:
  6. log.info(f"Light state change to: {value}")
  7. if value == 'All on':
  8. log.info("Go to All on")
  9. pyscript.state_goto_all_on()
  10. elif value == 'All off':
  11. log.info("Go to All off")
  12. pyscript.state_goto_all_off()
  13. elif value == 'Morning':
  14. log.info("Go to Morning")
  15. pyscript.state_goto_morning()
  16. elif value == 'Windows':
  17. log.info("Go to Windows")
  18. pyscript.state_goto_window()
  19. else:
  20. log.info("Light state was None")
  21. @service
  22. def handleWallButtonPressed():
  23. log.info(f"The wall button has been pressed. Curr state: {input_select.light_mode}")
  24. currHour = datetime.now().time().hour
  25. if( input_select.light_mode == 'All on' ):
  26. input_select.light_mode = 'All off'
  27. elif( input_select.light_mode == 'Morning' ):
  28. input_select.light_mode = 'All off'
  29. elif( input_select.light_mode == 'All off' ):
  30. log.info(f'Is All off. Hour is: {currHour}')
  31. if( currHour == 5 or currHour == 6 ):
  32. input_select.light_mode = 'Morning'
  33. else:
  34. input_select.light_mode = 'All on'
  35. elif( input_select.light_mode == 'Windows' ):
  36. input_select.light_mode = 'All on'
  37. @service
  38. def state_goto_all_on():
  39. task.unique('state_goto_py')
  40. log.info(f"********* ALL ON *********** State: " + input_select.light_mode )
  41. light.turn_on(entity_id='light.tradfri_bulb') # Stora flyglampan
  42. light.turn_on(entity_id='light.hall_inner')
  43. light.turn_on(entity_id='light.hall_door')
  44. light.turn_on(entity_id='light.rislampa')
  45. switch.turn_on(entity_id='switch.matsal_altandorr')
  46. switch.turn_on(entity_id='switch.matsal_piano')
  47. switch.turn_on(entity_id='switch.koksfonster')
  48. for x in range(2):
  49. #light.turn_on(entity_id='light.liv_room_table_lamp',brightness='1') # Temp disable when high energy prices
  50. #task.sleep(0.5)
  51. light.turn_on(entity_id='light.liv_room_corner_lamp',brightness='1')
  52. task.sleep(0.5)
  53. pyscript.kitchen_worklights_button_trigger()
  54. #pyscript.handle_bedroom_light() # Temp disable when high energy prices
  55. @service
  56. def state_goto_window():
  57. task.unique('state_goto_py')
  58. log.info(f"********* WINDOW *********** State: " + input_select.light_mode )
  59. light.turn_off(entity_id='light.tradfri_bulb')
  60. switch.turn_on(entity_id='switch.koksfonster')
  61. switch.turn_off(entity_id='switch.matsal_altandorr')
  62. switch.turn_off(entity_id='switch.matsal_piano')
  63. light.turn_off(entity_id='light.hall_inner')
  64. light.turn_off(entity_id='light.hall_door')
  65. light.turn_off(entity_id='light.rislampa')
  66. for x in range(2):
  67. light.turn_off(entity_id='light.liv_room_table_lamp')
  68. task.sleep(0.5)
  69. light.turn_off(entity_id='light.liv_room_corner_lamp')
  70. task.sleep(0.5)
  71. pyscript.kitchen_worklights_button_trigger()
  72. #pyscript.handle_bedroom_light()
  73. @service
  74. def state_goto_morning():
  75. task.unique('state_goto_py')
  76. log.info(f"********* MORNING *********** State: " + str(input_select.light_mode) )
  77. light.turn_off(entity_id='light.tradfri_bulb')
  78. switch.turn_off(entity_id='switch.matsal_altandorr')
  79. switch.turn_on(entity_id='switch.matsal_piano')
  80. switch.turn_on(entity_id='switch.koksfonster')
  81. light.turn_off(entity_id='light.hall_inner')
  82. light.turn_on(entity_id='light.hall_door')
  83. light.turn_off(entity_id='light.rislampa')
  84. for x in range(2):
  85. light.turn_on(entity_id='light.liv_room_corner_lamp',brightness='1')
  86. task.sleep(0.5)
  87. pyscript.kitchen_worklights_button_trigger()
  88. #pyscript.handle_bedroom_light()
  89. @service
  90. def state_goto_all_off():
  91. task.unique('state_goto_py')
  92. log.info(f"********* ALL OFF *********** State: " + input_select.light_mode )
  93. light.turn_off(entity_id='light.tradfri_bulb')
  94. light.turn_off(entity_id='light.hall_inner')
  95. light.turn_off(entity_id='light.hall_door')
  96. light.turn_off(entity_id='light.rislampa')
  97. switch.turn_off(entity_id='switch.koksfonster')
  98. switch.turn_off(entity_id='switch.matsal_altandorr')
  99. switch.turn_off(entity_id='switch.matsal_piano')
  100. #switch.turn_off(entity_id='switch.tradfri_outlet') # Sovrum OFF # Temp disable when high energy prices
  101. for x in range(2):
  102. #light.turn_off(entity_id='light.liv_room_table_lamp') # Temp disable when high energy prices
  103. #task.sleep(0.5)
  104. light.turn_off(entity_id='light.liv_room_corner_lamp')
  105. task.sleep(0.5)
  106. input_boolean.movie_mode = 'off'
  107. pyscript.kitchen_worklights_button_trigger()
  108. @state_trigger("input_boolean.movie_mode")
  109. def movie_mode_button_trigger():
  110. log.info(f"********* MOVIE MODE *********** To:" + input_boolean.movie_mode + " State: " + input_select.light_mode )
  111. if input_boolean.movie_mode == 'off' and input_select.light_mode == 'All on':
  112. log.info(f"Movie mode, turn lights ON")
  113. for x in range(2):
  114. light.turn_on(entity_id='light.liv_room_table_lamp',brightness='1')
  115. task.sleep(0.5)
  116. light.turn_on(entity_id='light.liv_room_corner_lamp',brightness='1')
  117. task.sleep(0.5)
  118. else:
  119. log.info(f"Movie mode, turn lights OFF")
  120. for x in range(2):
  121. light.turn_off(entity_id='light.liv_room_table_lamp')
  122. task.sleep(0.5)
  123. light.turn_off(entity_id='light.liv_room_corner_lamp')
  124. task.sleep(0.5)