state_goto.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. # Important info:
  23. #
  24. # We are not allowed to resend buttons 1 and 2
  25. # 1: Glas bulb lamp in living room
  26. # 2: Corner lamp (IKEA-big-bulb) in living room
  27. # If ON is sent multiple times, then they will change DIM mode.
  28. @service
  29. def state_goto_all_on():
  30. task.unique('state_goto_py')
  31. log.info(f"********* ALL ON ***********")
  32. light.turn_on(entity_id='light.tradfri_bulb')
  33. for x in range(2):
  34. switch.turn_on(entity_id='switch.group_button_all')
  35. task.sleep(1)
  36. pyscript.handle_bedroom_light()
  37. @service
  38. def state_goto_window():
  39. task.unique('state_goto_py')
  40. cs = getCurrentState()
  41. log.info(f"********* WINDOW ***********")
  42. light.turn_off(entity_id='light.tradfri_bulb')
  43. if cs == 4 or cs == 2: # From Off or already Window
  44. for x in range(2):
  45. light.turn_on(entity_id=' light.main_button_4')
  46. task.sleep(0.5)
  47. elif cs == 3: # From morning
  48. for x in range(2):
  49. light.turn_off(entity_id=' light.main_button_2')
  50. task.sleep(0.5)
  51. else: # From ALL ON
  52. for x in range(2):
  53. switch.turn_off(entity_id='switch.group_button_all')
  54. task.sleep(0.5)
  55. for x in range(2):
  56. light.turn_on(entity_id=' light.main_button_4')
  57. task.sleep(0.5)
  58. pyscript.handle_bedroom_light()
  59. @service
  60. def state_goto_morning():
  61. task.unique('state_goto_py')
  62. cs = getCurrentState()
  63. log.info(f"********* MORNING ***********")
  64. light.turn_off(entity_id='light.tradfri_bulb')
  65. if cs == 4 or cs == 3: # Off or already Morning
  66. for x in range(2):
  67. light.turn_on(entity_id=' light.main_button_4')
  68. task.sleep(0.5)
  69. light.turn_on(entity_id=' light.main_button_2')
  70. elif cs == 2: # From Window
  71. light.turn_on(entity_id=' light.main_button_2')
  72. else: # From ALL ON
  73. for x in range(2):
  74. switch.turn_off(entity_id='switch.group_button_all')
  75. task.sleep(0.5)
  76. light.turn_on(entity_id=' light.main_button_2')
  77. for x in range(2):
  78. light.turn_on(entity_id=' light.main_button_4')
  79. task.sleep(0.5)
  80. pyscript.handle_bedroom_light()
  81. @service
  82. def state_goto_all_off():
  83. task.unique('state_goto_py')
  84. log.info(f"********* ALL OFF ***********")
  85. light.turn_off(entity_id='light.tradfri_bulb')
  86. switch.turn_off(entity_id='switch.tradfri_outlet') # Sovrum OFF
  87. for x in range(2):
  88. switch.turn_off(entity_id='switch.group_button_all')
  89. task.sleep(0.5)