state_goto.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. #switch.turn_on(entity_id='switch.matsal')
  29. for x in range(2):
  30. switch.turn_on(entity_id='switch.group_button_all')
  31. task.sleep(0.5)
  32. #light.turn_on(entity_id='light.liv_room_table_lamp',brightness='1') # Temp disable when high energy prices
  33. #task.sleep(0.5)
  34. light.turn_on(entity_id='light.liv_room_corner_lamp',brightness='1')
  35. task.sleep(0.5)
  36. #pyscript.handle_bedroom_light() # Temp disable when high energy prices
  37. @service
  38. def state_goto_window():
  39. task.unique('state_goto_py')
  40. cs = getCurrentState()
  41. log.info(f"********* WINDOW *********** State: " + str(cs) )
  42. light.turn_off(entity_id='light.tradfri_bulb')
  43. switch.turn_off(entity_id='switch.matsal')
  44. if cs == 4 or cs == 2: # From Off or already Window
  45. for x in range(2):
  46. light.turn_on(entity_id=' light.main_button_4')
  47. task.sleep(0.5)
  48. elif cs == 3: # From morning
  49. for x in range(2):
  50. light.turn_off(entity_id=' light.main_button_2')
  51. task.sleep(0.5)
  52. light.turn_off(entity_id='light.liv_room_corner_lamp')
  53. task.sleep(0.5)
  54. else: # From ALL ON
  55. for x in range(2):
  56. switch.turn_off(entity_id='switch.group_button_all')
  57. task.sleep(0.5)
  58. light.turn_off(entity_id='light.liv_room_table_lamp')
  59. task.sleep(0.5)
  60. light.turn_off(entity_id='light.liv_room_corner_lamp')
  61. task.sleep(0.5)
  62. for x in range(2):
  63. light.turn_on(entity_id=' light.main_button_4')
  64. task.sleep(0.5)
  65. pyscript.handle_bedroom_light()
  66. @service
  67. def state_goto_morning():
  68. task.unique('state_goto_py')
  69. cs = getCurrentState()
  70. log.info(f"********* MORNING *********** State: " + str(cs) )
  71. light.turn_off(entity_id='light.tradfri_bulb')
  72. switch.turn_off(entity_id='switch.matsal')
  73. if cs == 4 or cs == 3: # Off or already Morning
  74. for x in range(2):
  75. light.turn_on(entity_id=' light.main_button_4')
  76. task.sleep(0.5)
  77. light.turn_on(entity_id=' light.main_button_2')
  78. task.sleep(0.5)
  79. light.turn_on(entity_id='light.liv_room_corner_lamp',brightness='1')
  80. task.sleep(0.5)
  81. elif cs == 2: # From Window
  82. for x in range(2):
  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. else: # From ALL ON
  88. for x in range(2):
  89. switch.turn_off(entity_id='switch.group_button_all')
  90. task.sleep(0.5)
  91. for x in range(2):
  92. light.turn_on(entity_id=' light.main_button_2')
  93. task.sleep(0.5)
  94. light.turn_on(entity_id=' light.main_button_4')
  95. task.sleep(0.5)
  96. light.turn_on(entity_id='light.liv_room_corner_lamp',brightness='1')
  97. task.sleep(0.5)
  98. pyscript.handle_bedroom_light()
  99. @service
  100. def state_goto_all_off():
  101. task.unique('state_goto_py')
  102. cs = getCurrentState()
  103. log.info(f"********* ALL OFF *********** State: " + str(cs) )
  104. light.turn_off(entity_id='light.tradfri_bulb')
  105. #switch.turn_off(entity_id='switch.matsal') # Temp disable when high energy prices
  106. #switch.turn_off(entity_id='switch.tradfri_outlet') # Sovrum OFF # Temp disable when high energy prices
  107. for x in range(2):
  108. switch.turn_off(entity_id='switch.group_button_all')
  109. task.sleep(0.5)
  110. #light.turn_off(entity_id='light.liv_room_table_lamp') # Temp disable when high energy prices
  111. #task.sleep(0.5)
  112. light.turn_off(entity_id='light.liv_room_corner_lamp')
  113. task.sleep(0.5)
  114. input_boolean.movie_mode = 'off'
  115. @state_trigger("input_boolean.movie_mode")
  116. def movie_mode_button_trigger():
  117. cs = getCurrentState()
  118. log.info(f"********* MOVIE MODE *********** To:" + input_boolean.movie_mode + " State: " + str(cs) )
  119. if input_boolean.movie_mode == 'off' and switch.group_button_all == 'on':
  120. log.info(f"Movie mode, turn lights ON")
  121. for x in range(2):
  122. light.turn_on(entity_id='light.liv_room_table_lamp',brightness='1')
  123. task.sleep(0.5)
  124. light.turn_on(entity_id='light.liv_room_corner_lamp',brightness='1')
  125. task.sleep(0.5)
  126. else:
  127. log.info(f"Movie mode, turn lights OFF")
  128. for x in range(2):
  129. light.turn_off(entity_id='light.liv_room_table_lamp')
  130. task.sleep(0.5)
  131. light.turn_off(entity_id='light.liv_room_corner_lamp')
  132. task.sleep(0.5)