state_goto.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. def getCurrentState():
  2. if( light.main_button_1 == 'on' and # All ON
  3. light.main_button_2 == 'on' and
  4. light.main_button_3 == 'on' and
  5. light.main_button_4 == 'on' ):
  6. return 1
  7. elif( light.main_button_1 == 'off' and # Window
  8. light.main_button_2 == 'off' and
  9. light.main_button_3 == 'off' and
  10. light.main_button_4 == 'on' ):
  11. return 2
  12. elif( light.main_button_1 == 'off' and # Morning
  13. light.main_button_2 == 'on' and
  14. light.main_button_3 == 'off' and
  15. light.main_button_4 == 'on' ):
  16. return 3
  17. elif( light.main_button_1 == 'off' and # All OFF
  18. light.main_button_2 == 'off' and
  19. light.main_button_3 == 'off' and
  20. light.main_button_4 == 'off' ):
  21. return 4
  22. @service
  23. def state_goto_all_on():
  24. task.unique('state_goto_py')
  25. cs = getCurrentState()
  26. log.info(f"********* ALL ON *********** State: " + str(cs) )
  27. light.turn_on(entity_id='light.tradfri_bulb') # Stora flyglampan
  28. light.turn_on(entity_id='light.hall_inner')
  29. light.turn_on(entity_id='light.hall_door')
  30. #switch.turn_on(entity_id='switch.matsal')
  31. for x in range(2):
  32. switch.turn_on(entity_id='switch.group_button_all')
  33. task.sleep(0.5)
  34. #light.turn_on(entity_id='light.liv_room_table_lamp',brightness='1') # Temp disable when high energy prices
  35. #task.sleep(0.5)
  36. light.turn_on(entity_id='light.liv_room_corner_lamp',brightness='1')
  37. task.sleep(0.5)
  38. #pyscript.handle_bedroom_light() # Temp disable when high energy prices
  39. @service
  40. def state_goto_window():
  41. task.unique('state_goto_py')
  42. cs = getCurrentState()
  43. log.info(f"********* WINDOW *********** State: " + str(cs) )
  44. light.turn_off(entity_id='light.tradfri_bulb')
  45. switch.turn_off(entity_id='switch.matsal')
  46. light.turn_off(entity_id='light.hall_inner')
  47. light.turn_off(entity_id='light.hall_door')
  48. if cs == 4 or cs == 2: # From Off or already Window
  49. for x in range(2):
  50. light.turn_on(entity_id=' light.main_button_4')
  51. task.sleep(0.5)
  52. elif cs == 3: # From morning
  53. for x in range(2):
  54. light.turn_off(entity_id=' light.main_button_2')
  55. task.sleep(0.5)
  56. light.turn_off(entity_id='light.liv_room_corner_lamp')
  57. task.sleep(0.5)
  58. else: # From ALL ON
  59. for x in range(2):
  60. switch.turn_off(entity_id='switch.group_button_all')
  61. task.sleep(0.5)
  62. light.turn_off(entity_id='light.liv_room_table_lamp')
  63. task.sleep(0.5)
  64. light.turn_off(entity_id='light.liv_room_corner_lamp')
  65. task.sleep(0.5)
  66. for x in range(2):
  67. light.turn_on(entity_id=' light.main_button_4')
  68. task.sleep(0.5)
  69. pyscript.handle_bedroom_light()
  70. @service
  71. def state_goto_morning():
  72. task.unique('state_goto_py')
  73. cs = getCurrentState()
  74. log.info(f"********* MORNING *********** State: " + str(cs) )
  75. light.turn_off(entity_id='light.tradfri_bulb')
  76. switch.turn_off(entity_id='switch.matsal')
  77. light.turn_off(entity_id='light.hall_inner')
  78. light.turn_on(entity_id='light.hall_door')
  79. if cs == 4 or cs == 3: # Off or already Morning
  80. for x in range(2):
  81. light.turn_on(entity_id=' light.main_button_4')
  82. task.sleep(0.5)
  83. light.turn_on(entity_id=' light.main_button_2')
  84. task.sleep(0.5)
  85. light.turn_on(entity_id='light.liv_room_corner_lamp',brightness='1')
  86. task.sleep(0.5)
  87. elif cs == 2: # From Window
  88. for x in range(2):
  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. else: # From ALL ON
  94. for x in range(2):
  95. switch.turn_off(entity_id='switch.group_button_all')
  96. task.sleep(0.5)
  97. for x in range(2):
  98. light.turn_on(entity_id=' light.main_button_2')
  99. task.sleep(0.5)
  100. light.turn_on(entity_id=' light.main_button_4')
  101. task.sleep(0.5)
  102. light.turn_on(entity_id='light.liv_room_corner_lamp',brightness='1')
  103. task.sleep(0.5)
  104. pyscript.handle_bedroom_light()
  105. @service
  106. def state_goto_all_off():
  107. task.unique('state_goto_py')
  108. cs = getCurrentState()
  109. log.info(f"********* ALL OFF *********** State: " + str(cs) )
  110. light.turn_off(entity_id='light.tradfri_bulb')
  111. light.turn_off(entity_id='light.hall_inner')
  112. light.turn_off(entity_id='light.hall_door')
  113. #switch.turn_off(entity_id='switch.matsal') # Temp disable when high energy prices
  114. #switch.turn_off(entity_id='switch.tradfri_outlet') # Sovrum OFF # Temp disable when high energy prices
  115. for x in range(2):
  116. switch.turn_off(entity_id='switch.group_button_all')
  117. task.sleep(0.5)
  118. #light.turn_off(entity_id='light.liv_room_table_lamp') # Temp disable when high energy prices
  119. #task.sleep(0.5)
  120. light.turn_off(entity_id='light.liv_room_corner_lamp')
  121. task.sleep(0.5)
  122. input_boolean.movie_mode = 'off'
  123. @state_trigger("input_boolean.movie_mode")
  124. def movie_mode_button_trigger():
  125. cs = getCurrentState()
  126. log.info(f"********* MOVIE MODE *********** To:" + input_boolean.movie_mode + " State: " + str(cs) )
  127. if input_boolean.movie_mode == 'off' and switch.group_button_all == 'on':
  128. log.info(f"Movie mode, turn lights ON")
  129. for x in range(2):
  130. light.turn_on(entity_id='light.liv_room_table_lamp',brightness='1')
  131. task.sleep(0.5)
  132. light.turn_on(entity_id='light.liv_room_corner_lamp',brightness='1')
  133. task.sleep(0.5)
  134. else:
  135. log.info(f"Movie mode, turn lights OFF")
  136. for x in range(2):
  137. light.turn_off(entity_id='light.liv_room_table_lamp')
  138. task.sleep(0.5)
  139. light.turn_off(entity_id='light.liv_room_corner_lamp')
  140. task.sleep(0.5)