bedroom.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. from homeassistant.const import EVENT_STATE_CHANGED
  2. # This script reacts to the actual sensor on the door
  3. # It sets the input_boolean which keeps its state during restarts
  4. # Arguments could be: (entity_id=None, new_state=None, old_state=None):
  5. @event_trigger(EVENT_STATE_CHANGED, "entity_id=='binary_sensor.bedroom_door_raw_sensor'")
  6. def bedroom_door_raw_sensor():
  7. #log.info(f"monitor_bedroom_door_event: entity {entity_id} changed from {old_state} to {new_state}")
  8. log.info(f"********* BEDROOM RAW SENSOR: "+binary_sensor.bedroom_door_raw_sensor+" ***********")
  9. input_boolean.bedroom_door = binary_sensor.bedroom_door_raw_sensor
  10. pyscript.handle_bedroom_light()
  11. # This service handles the window light in the bedroom
  12. # It reacts when the door opens/closes or when a state_goto-script is run
  13. # when someone has pressed a button on the wall or kitchen IPhone.
  14. @service
  15. def handle_bedroom_light():
  16. task.unique('handle_bedroom_light')
  17. log.info(f"********* BEDROOM LIGHT CONTROL **** Btn_4:" + light.main_button_4 + " Door:"+ input_boolean.bedroom_door)
  18. task.sleep(1)
  19. # The door sensor is out of battery, and thats why I disable this for now
  20. #if light.main_button_4 == 'on' and input_boolean.bedroom_door == 'on':
  21. if light.main_button_4 == 'on':
  22. # If OPEN
  23. switch.turn_on(entity_id='switch.tradfri_outlet')
  24. else:
  25. switch.turn_off(entity_id='switch.tradfri_outlet')
  26. """
  27. # This way does not work, beacuse it does not react to "off" when the door is already "off/aka Unknown" after a restart of HA
  28. @state_trigger("binary_sensor.bedroom_door_raw_sensor")
  29. def bedroom_door_raw_sensor():
  30. log.info(f"********* BEDROOM RAW SENSOR: "+binary_sensor.bedroom_door_raw_sensor+" ***********")
  31. input_boolean.bedroom_door = binary_sensor.bedroom_door_raw_sensor
  32. pyscript.handle_bedroom_light()
  33. """