123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- from homeassistant.const import EVENT_STATE_CHANGED
- def getCurrentState():
- if( light.main_button_1 == 'on' and # All ON
- light.main_button_2 == 'on' and
- light.main_button_3 == 'on' and
- light.main_button_4 == 'on' ):
- return 1
- elif( light.main_button_1 == 'off' and # Window
- light.main_button_2 == 'off' and
- light.main_button_3 == 'off' and
- light.main_button_4 == 'on' ):
- return 2
- elif( light.main_button_1 == 'off' and # Morning
- light.main_button_2 == 'on' and
- light.main_button_3 == 'off' and
- light.main_button_4 == 'on' ):
- return 3
- elif( light.main_button_1 == 'off' and # All OFF
- light.main_button_2 == 'off' and
- light.main_button_3 == 'off' and
- light.main_button_4 == 'off' ):
- return 4
- @service
- def state_goto_all_on():
- task.unique('state_goto_py')
- cs = getCurrentState()
- log.info(f"********* ALL ON *********** State: " + str(cs) )
- light.turn_on(entity_id='light.tradfri_bulb') # Stora flyglampan
- light.turn_on(entity_id='light.hall_inner')
- light.turn_on(entity_id='light.hall_door')
- #switch.turn_on(entity_id='switch.matsal')
- switch.turn_on(entity_id='switch.koksfonster')
- for x in range(2):
- switch.turn_on(entity_id='switch.group_button_all')
- task.sleep(0.5)
- #light.turn_on(entity_id='light.liv_room_table_lamp',brightness='1') # Temp disable when high energy prices
- #task.sleep(0.5)
- light.turn_on(entity_id='light.liv_room_corner_lamp',brightness='1')
- task.sleep(0.5)
- kitchen_worklights_button_trigger()
- #pyscript.handle_bedroom_light() # Temp disable when high energy prices
- @service
- def state_goto_window():
- task.unique('state_goto_py')
- cs = getCurrentState()
- log.info(f"********* WINDOW *********** State: " + str(cs) )
- light.turn_off(entity_id='light.tradfri_bulb')
- #switch.turn_off(entity_id='switch.matsal')
- switch.turn_on(entity_id='switch.koksfonster')
- light.turn_off(entity_id='light.hall_inner')
- light.turn_off(entity_id='light.hall_door')
-
- if cs == 4 or cs == 2: # From Off or already Window
- for x in range(2):
- light.turn_on(entity_id=' light.main_button_4')
- task.sleep(0.5)
- elif cs == 3: # From morning
- for x in range(2):
- light.turn_off(entity_id=' light.main_button_2')
- task.sleep(0.5)
- light.turn_off(entity_id='light.liv_room_corner_lamp')
- task.sleep(0.5)
- else: # From ALL ON
- for x in range(2):
- switch.turn_off(entity_id='switch.group_button_all')
- task.sleep(0.5)
- light.turn_off(entity_id='light.liv_room_table_lamp')
- task.sleep(0.5)
- light.turn_off(entity_id='light.liv_room_corner_lamp')
- task.sleep(0.5)
- for x in range(2):
- light.turn_on(entity_id=' light.main_button_4')
- task.sleep(0.5)
-
- kitchen_worklights_button_trigger()
- #pyscript.handle_bedroom_light()
- @service
- def state_goto_morning():
- task.unique('state_goto_py')
- cs = getCurrentState()
- log.info(f"********* MORNING *********** State: " + str(cs) )
- light.turn_off(entity_id='light.tradfri_bulb')
- #switch.turn_off(entity_id='switch.matsal')
- switch.turn_on(entity_id='switch.koksfonster')
- light.turn_off(entity_id='light.hall_inner')
- light.turn_on(entity_id='light.hall_door')
- if cs == 4 or cs == 3: # Off or already Morning
- for x in range(2):
- light.turn_on(entity_id=' light.main_button_4')
- task.sleep(0.5)
- light.turn_on(entity_id=' light.main_button_2')
- task.sleep(0.5)
- light.turn_on(entity_id='light.liv_room_corner_lamp',brightness='1')
- task.sleep(0.5)
- elif cs == 2: # From Window
- for x in range(2):
- light.turn_on(entity_id=' light.main_button_2')
- task.sleep(0.5)
- light.turn_on(entity_id='light.liv_room_corner_lamp',brightness='1')
- task.sleep(0.5)
- else: # From ALL ON
- for x in range(2):
- switch.turn_off(entity_id='switch.group_button_all')
- task.sleep(0.5)
- for x in range(2):
- light.turn_on(entity_id=' light.main_button_2')
- task.sleep(0.5)
- light.turn_on(entity_id=' light.main_button_4')
- task.sleep(0.5)
- light.turn_on(entity_id='light.liv_room_corner_lamp',brightness='1')
- task.sleep(0.5)
-
- kitchen_worklights_button_trigger()
- #pyscript.handle_bedroom_light()
- @service
- def state_goto_all_off():
- task.unique('state_goto_py')
- cs = getCurrentState()
- log.info(f"********* ALL OFF *********** State: " + str(cs) )
- light.turn_off(entity_id='light.tradfri_bulb')
- light.turn_off(entity_id='light.hall_inner')
- light.turn_off(entity_id='light.hall_door')
- switch.turn_off(entity_id='switch.koksfonster')
- #switch.turn_off(entity_id='switch.matsal') # Temp disable when high energy prices
- #switch.turn_off(entity_id='switch.tradfri_outlet') # Sovrum OFF # Temp disable when high energy prices
- for x in range(2):
- switch.turn_off(entity_id='switch.group_button_all')
- task.sleep(0.5)
- #light.turn_off(entity_id='light.liv_room_table_lamp') # Temp disable when high energy prices
- #task.sleep(0.5)
- light.turn_off(entity_id='light.liv_room_corner_lamp')
- task.sleep(0.5)
- input_boolean.movie_mode = 'off'
- kitchen_worklights_button_trigger()
- @state_trigger("input_boolean.movie_mode")
- def movie_mode_button_trigger():
- cs = getCurrentState()
- log.info(f"********* MOVIE MODE *********** To:" + input_boolean.movie_mode + " State: " + str(cs) )
- if input_boolean.movie_mode == 'off' and switch.group_button_all == 'on':
- log.info(f"Movie mode, turn lights ON")
- for x in range(2):
- light.turn_on(entity_id='light.liv_room_table_lamp',brightness='1')
- task.sleep(0.5)
- light.turn_on(entity_id='light.liv_room_corner_lamp',brightness='1')
- task.sleep(0.5)
-
- else:
- log.info(f"Movie mode, turn lights OFF")
- for x in range(2):
- light.turn_off(entity_id='light.liv_room_table_lamp')
- task.sleep(0.5)
- light.turn_off(entity_id='light.liv_room_corner_lamp')
- task.sleep(0.5)
- @event_trigger(EVENT_STATE_CHANGED, "entity_id=='input_button.av'")
- def on_ui_button_all_off_clicked(entity_id, new_state, old_state):
- pyscript.state_goto_all_off()
- @event_trigger(EVENT_STATE_CHANGED, "entity_id=='input_button.allt_pa'")
- def on_ui_button_all_on_clicked(entity_id, new_state, old_state):
- pyscript.state_goto_all_on()
-
|