bedroom.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. #
  6. # TypeError: bedroom_door_raw_sensor() called with unexpected keyword arguments: entity_id, new_state, old_state
  7. #
  8. @event_trigger(EVENT_STATE_CHANGED, "entity_id=='binary_sensor.bedroom_door_raw_sensor'")
  9. def bedroom_door_raw_sensor(entity_id, new_state, old_state):
  10. #log.info(f"monitor_bedroom_door_event: entity {entity_id} changed from {old_state} to {new_state}")
  11. log.info(f"********* BEDROOM RAW SENSOR: "+binary_sensor.bedroom_door_raw_sensor+" ***********")
  12. input_boolean.bedroom_door = binary_sensor.bedroom_door_raw_sensor
  13. #pyscript.handle_bedroom_light() # Temp disable when high energy prices
  14. # This service handles the window light in the bedroom
  15. # It reacts when the door opens/closes or when a state_goto-script is run
  16. # when someone has pressed a button on the wall or kitchen IPhone.
  17. @service
  18. def handle_bedroom_light():
  19. task.unique('handle_bedroom_light')
  20. log.info(f"********* BEDROOM LIGHT CONTROL **** Btn_4:" + light.main_button_4 + " Door:"+ input_boolean.bedroom_door)
  21. task.sleep(1)
  22. # Select which line to used based on if there is power in the door sensor battery
  23. if light.main_button_4 == 'on' and input_boolean.bedroom_door == 'on':
  24. #if light.main_button_4 == 'on': # Use if battery is out of power
  25. # If OPEN
  26. switch.turn_on(entity_id='switch.tradfri_outlet')
  27. else:
  28. switch.turn_off(entity_id='switch.tradfri_outlet')
  29. """
  30. # 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
  31. @state_trigger("binary_sensor.bedroom_door_raw_sensor")
  32. def bedroom_door_raw_sensor():
  33. log.info(f"********* BEDROOM RAW SENSOR: "+binary_sensor.bedroom_door_raw_sensor+" ***********")
  34. input_boolean.bedroom_door = binary_sensor.bedroom_door_raw_sensor
  35. pyscript.handle_bedroom_light()
  36. """