kitchen.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. from homeassistant.const import EVENT_STATE_CHANGED
  2. """
  3. Kitchen LED Documentation:
  4. Kitchen worktop lights use two different LED-Strips with two different color temperatures:
  5. 2700 and 4000K. They are controlled individually with two MOSFET PWM controllers.
  6. The PWM-controller (STM32-board) receives its information via a serial line (9600) from
  7. the Pi3. The Pi3 sends data in ASCII format <1234ABCD> to control the 2 channels.
  8. The Pi3 has a Python-script running as a service kitchen_led.service that acts like two separate
  9. Home Assistant MQTT Lights: "Kitchen worktop 4K" and "Kitchen worktop 27K". They can be
  10. controller with On/Off and brightness individually.
  11. The lights can be in 3 different modes: 1:Off, 2:Soft and 3:Work
  12. 3:Work is controlled by a manual switch (a helper) called "input_boolean.kitchen_worklights"
  13. When change the value of the switch the function kitchen_worklights_button_trigger() reacts
  14. to the events and start a control-chain.
  15. 2:Soft and 1:Off is controller when the household lights are changed between different modes.
  16. The function kitchen_worklights_button_trigger() is called directly from these goto-mode-functions.
  17. As a security measure the whole Power Supply to the LED-controller is electrically controlled
  18. with an IKEA Switch. So when the LEDs are Off, then the circuit is fully electrically Off.
  19. The full chain is:
  20. 1. Either by trigger or functional call the function kitchen_worklights_button_trigger() is called.
  21. It checks if the LEDs are to be one of the On-modes or Off. It then controls the IKEA Switch to turn the
  22. whole Kitchen LED on or off (electrically with a relay in the IKEA Switch).
  23. If the IKEA Switch was already On, then it calls handle_kitchen_worklights()
  24. 2. If the IKEA Switch was off and was turned on in step 1, then the event-trigger-function
  25. kitchen_worklights_security_switch_trigger() reacts to that state-change.
  26. If change Off-On then it waits a samll time before calling the third step handle_kitchen_worklights()
  27. If On-Off, then calls that function directly.
  28. 3. The last step in function handle_kitchen_worklights() is controlling the two separate
  29. MQTT-Lights in Home Assistant to achieve the setup that is wanted in each of the three modes.
  30. """
  31. # Reacts to when the Kitchen Worklights button is changed
  32. # This could be from HA UI or from web-interface
  33. # Or it can be called from a light mode change
  34. @service
  35. @state_trigger("input_boolean.kitchen_worklights")
  36. def kitchen_worklights_button_trigger():
  37. isChristmas = True if input_boolean.christmas == 'on' else False
  38. log.info(f"1. Kitchen worklight changed. State:{input_boolean.kitchen_worklights} Mode:{input_select.light_mode} Jul:{isChristmas}")
  39. if( input_boolean.kitchen_worklights=='on' or light.hall_door == 'on' ):
  40. log.info("1. Kitchen LED: IKEA-switch should be turned ON....")
  41. if( switch.kok_led_sec_switch == 'on' ):
  42. log.info("1. Kitchen LED IKEA-Switch already On, go directly to PWW-Control")
  43. handle_kitchen_worklights()
  44. else:
  45. log.info("1. Kitchen LED, Turn ON IKEA Switch")
  46. switch.turn_on(entity_id='switch.kok_led_sec_switch')
  47. else:
  48. log.info("1. Kitchen LED, IKEA switch OFF")
  49. switch.turn_off(entity_id='switch.kok_led_sec_switch')
  50. # Triggers when the IKEA Switch changes state. Goes On or Off
  51. @event_trigger(EVENT_STATE_CHANGED, "entity_id=='switch.kok_led_sec_switch'")
  52. def kitchen_worklights_security_switch_trigger(entity_id, new_state, old_state):
  53. log.info(f"2. Trigger on kitchen IKEA Switch:{switch.kok_led_sec_switch}")
  54. if( switch.kok_led_sec_switch=='on' ):
  55. log.info("2. Kitchen LED. Wait 300mS before control of PWM")
  56. task.sleep(0.3)
  57. handle_kitchen_worklights()
  58. # Controls the PWM of the kitchen workbench LED-Strips
  59. # Does not control the IKEA Switch that turns everything On/Off
  60. def handle_kitchen_worklights():
  61. log.info(f"3. Kitchen LED. Control PWMs:{input_boolean.kitchen_worklights} HALL:{light.hall_door}")
  62. if( input_boolean.kitchen_worklights == 'on' ):
  63. log.info("3. Kitchen LED. Full mode...")
  64. light.turn_on(entity_id='light.kitchen_worktop_4k',brightness='63')
  65. light.turn_on(entity_id='light.kitchen_worktop_27k',brightness='127')
  66. elif( light.hall_door == 'on' ):
  67. log.info("3. Kitchen LED. Soft mode...")
  68. light.turn_off(entity_id='light.kitchen_worktop_4k')
  69. light.turn_on(entity_id='light.kitchen_worktop_27k',brightness='12')
  70. else:
  71. log.info("3. Kitchen LED. Off mode...")
  72. light.turn_off(entity_id='light.kitchen_worktop_4k')
  73. light.turn_off(entity_id='light.kitchen_worktop_27k')
  74. #@state_trigger("binary_sensor.kitchen_fridge_door")
  75. #def light_turned_on(**kwargs):
  76. # log.info(f"got arguments {kwargs}")
  77. @state_trigger("binary_sensor.kitchen_fridge_door == 'on'")
  78. @task_unique('fridge_open_too_long', kill_me=True)
  79. def fridge_door_open_too_long():
  80. log.info("Fridge door has been opened")
  81. #service.call("button", "press", entity_id="button.kitchen_alarm_notification")
  82. t_info = task.wait_until(state_trigger="binary_sensor.kitchen_fridge_door == 'off'",timeout=30)
  83. if t_info["trigger_type"] != "timeout":
  84. log.info("Fridge door closed before timeout")
  85. return
  86. log.info("Fridge door has been opened for 30 seconds — do a notification.")
  87. service.call("button", "press", entity_id="button.kitchen_alarm_notification")
  88. t_info = task.wait_until(state_trigger="binary_sensor.kitchen_fridge_door == 'off'",timeout=10)
  89. if t_info["trigger_type"] != "timeout":
  90. log.info("Fridge door closed before timeout")
  91. return
  92. log.info("Fridge door has been opened for 40 seconds — do a notification.")
  93. service.call("button", "press", entity_id="button.kitchen_alarm_notification")
  94. t_info = task.wait_until(state_trigger="binary_sensor.kitchen_fridge_door == 'off'",timeout=10)
  95. if t_info["trigger_type"] != "timeout":
  96. log.info("Fridge door closed before timeout")
  97. return
  98. log.info("Fridge door has been opened for 50 seconds — do a notification.")
  99. service.call("button", "press", entity_id="button.kitchen_alarm_notification")
  100. t_info = task.wait_until(state_trigger="binary_sensor.kitchen_fridge_door == 'off'",timeout=10)
  101. if t_info["trigger_type"] != "timeout":
  102. log.info("Fridge door closed before timeout")
  103. return
  104. log.info("Fridge door has been opened for 60 seconds — activate alarm.")
  105. service.call("switch", "turn_on", entity_id="switch.kitchen_audible_alarm")
  106. t_info = task.wait_until(state_trigger="binary_sensor.kitchen_fridge_door == 'off'",timeout=5*60)
  107. log.info("Door closed or alarm has been active for 5 minutes — turn off.")
  108. service.call("switch", "turn_off", entity_id="switch.kitchen_audible_alarm")