kitchen.py 4.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. @service
  34. @state_trigger("input_boolean.kitchen_worklights")
  35. def kitchen_worklights_button_trigger():
  36. log.info(f"Kitchen worklight changed. State: {input_boolean.kitchen_worklights}")
  37. if( input_boolean.kitchen_worklights=='on' or light.hall_door == 'on' ):
  38. log.info("Kitchen LED: Sec switch ON")
  39. if( switch.kok_led_sec_switch == 'on' ):
  40. log.info("K LED Sec Switch already On, go directly to PWW-Control")
  41. handle_kitchen_worklights()
  42. else:
  43. switch.turn_on(entity_id='switch.kok_led_sec_switch')
  44. else:
  45. log.info("Kitchen LED: Sec switch OFF")
  46. switch.turn_off(entity_id='switch.kok_led_sec_switch')
  47. # Triggers when the IKEA Switch changes state. Goes On or Off
  48. @event_trigger(EVENT_STATE_CHANGED, "entity_id=='switch.kok_led_sec_switch'")
  49. def kitchen_worklights_security_switch_trigger(entity_id, new_state, old_state):
  50. log.info(f"kitchen_worklights_security_switch_trigger() {switch.kok_led_sec_switch}")
  51. if( switch.kok_led_sec_switch=='on' ):
  52. log.info("Kitchen LED: Control PWM Wait...")
  53. task.sleep(0.5)
  54. log.info("Kitchen LED: Go....")
  55. handle_kitchen_worklights()
  56. # Controls the PWM of the kitchen workbench LED-Strips
  57. # Does not control the IKEA Switch that turns everything On/Off
  58. def handle_kitchen_worklights():
  59. log.info(f"handle_kitchen_worklights WL:{input_boolean.kitchen_worklights} HALL:{light.hall_door}")
  60. if( input_boolean.kitchen_worklights == 'on' ):
  61. log.info("K LED Full mode...")
  62. light.turn_on(entity_id='light.kitchen_worktop_4k',brightness='63')
  63. light.turn_on(entity_id='light.kitchen_worktop_27k',brightness='127')
  64. elif( light.hall_door == 'on' ):
  65. log.info("K LED Soft mode...")
  66. light.turn_off(entity_id='light.kitchen_worktop_4k')
  67. light.turn_on(entity_id='light.kitchen_worktop_27k',brightness='12')
  68. else:
  69. log.info("K LED Off mode...")
  70. light.turn_off(entity_id='light.kitchen_worktop_4k')
  71. light.turn_off(entity_id='light.kitchen_worktop_27k')