state_goto.py 5.2 KB

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