123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- from homeassistant.const import EVENT_STATE_CHANGED
- """
- Kitchen LED Documentation:
- Kitchen worktop lights use two different LED-Strips with two different color temperatures:
- 2700 and 4000K. They are controlled individually with two MOSFET PWM controllers.
- The PWM-controller (STM32-board) receives its information via a serial line (9600) from
- the Pi3. The Pi3 sends data in ASCII format <1234ABCD> to control the 2 channels.
- The Pi3 has a Python-script running as a service kitchen_led.service that acts like two separate
- Home Assistant MQTT Lights: "Kitchen worktop 4K" and "Kitchen worktop 27K". They can be
- controller with On/Off and brightness individually.
- The lights can be in 3 different modes: 1:Off, 2:Soft and 3:Work
- 3:Work is controlled by a manual switch (a helper) called "input_boolean.kitchen_worklights"
- When change the value of the switch the function kitchen_worklights_button_trigger() reacts
- to the events and start a control-chain.
- 2:Soft and 1:Off is controller when the household lights are changed between different modes.
- The function kitchen_worklights_button_trigger() is called directly from these goto-mode-functions.
- As a security measure the whole Power Supply to the LED-controller is electrically controlled
- with an IKEA Switch. So when the LEDs are Off, then the circuit is fully electrically Off.
- The full chain is:
- 1. Either by trigger or functional call the function kitchen_worklights_button_trigger() is called.
- It checks if the LEDs are to be one of the On-modes or Off. It then controls the IKEA Switch to turn the
- whole Kitchen LED on or off (electrically with a relay in the IKEA Switch).
- If the IKEA Switch was already On, then it calls handle_kitchen_worklights()
- 2. If the IKEA Switch was off and was turned on in step 1, then the event-trigger-function
- kitchen_worklights_security_switch_trigger() reacts to that state-change.
- If change Off-On then it waits a samll time before calling the third step handle_kitchen_worklights()
- If On-Off, then calls that function directly.
- 3. The last step in function handle_kitchen_worklights() is controlling the two separate
- MQTT-Lights in Home Assistant to achieve the setup that is wanted in each of the three modes.
- """
- # Reacts to when the Kitchen Worklights button is changed
- # This could be from HA UI or from web-interface
- # Or it can be called from a light mode change
- @service
- @state_trigger("input_boolean.kitchen_worklights")
- def kitchen_worklights_button_trigger():
- isChristmas = True if input_boolean.christmas == 'on' else False
- log.info(f"1. Kitchen worklight changed. State:{input_boolean.kitchen_worklights} Mode:{input_select.light_mode} Jul:{isChristmas}")
- if( input_boolean.kitchen_worklights=='on' or light.hall_door == 'on' ):
- log.info("1. Kitchen LED: IKEA-switch should be turned ON....")
- if( switch.kok_led_sec_switch == 'on' ):
- log.info("1. Kitchen LED IKEA-Switch already On, go directly to PWW-Control")
- handle_kitchen_worklights()
- else:
- log.info("1. Kitchen LED, Turn ON IKEA Switch")
- switch.turn_on(entity_id='switch.kok_led_sec_switch')
- else:
- log.info("1. Kitchen LED, IKEA switch OFF")
- switch.turn_off(entity_id='switch.kok_led_sec_switch')
- # Triggers when the IKEA Switch changes state. Goes On or Off
- @event_trigger(EVENT_STATE_CHANGED, "entity_id=='switch.kok_led_sec_switch'")
- def kitchen_worklights_security_switch_trigger(entity_id, new_state, old_state):
- log.info(f"2. Trigger on kitchen IKEA Switch:{switch.kok_led_sec_switch}")
- if( switch.kok_led_sec_switch=='on' ):
- log.info("2. Kitchen LED. Wait 300mS before control of PWM")
- task.sleep(0.3)
- handle_kitchen_worklights()
- # Controls the PWM of the kitchen workbench LED-Strips
- # Does not control the IKEA Switch that turns everything On/Off
- def handle_kitchen_worklights():
- log.info(f"3. Kitchen LED. Control PWMs:{input_boolean.kitchen_worklights} HALL:{light.hall_door}")
- if( input_boolean.kitchen_worklights == 'on' ):
- log.info("3. Kitchen LED. Full mode...")
- light.turn_on(entity_id='light.kitchen_worktop_4k',brightness='63')
- light.turn_on(entity_id='light.kitchen_worktop_27k',brightness='127')
- elif( light.hall_door == 'on' ):
- log.info("3. Kitchen LED. Soft mode...")
- light.turn_off(entity_id='light.kitchen_worktop_4k')
- light.turn_on(entity_id='light.kitchen_worktop_27k',brightness='12')
- else:
- log.info("3. Kitchen LED. Off mode...")
- light.turn_off(entity_id='light.kitchen_worktop_4k')
- light.turn_off(entity_id='light.kitchen_worktop_27k')
- #@state_trigger("binary_sensor.kitchen_fridge_door")
- #def light_turned_on(**kwargs):
- # log.info(f"got arguments {kwargs}")
- @state_trigger("binary_sensor.kitchen_fridge_door == 'on'")
- @task_unique('fridge_open_too_long', kill_me=True)
- def fridge_door_open_too_long():
- log.info("Fridge door has been opened")
- #service.call("button", "press", entity_id="button.kitchen_alarm_notification")
- t_info = task.wait_until(state_trigger="binary_sensor.kitchen_fridge_door == 'off'",timeout=30)
- if t_info["trigger_type"] != "timeout":
- log.info("Fridge door closed before timeout")
- return
- log.info("Fridge door has been opened for 30 seconds — do a notification.")
- service.call("button", "press", entity_id="button.kitchen_alarm_notification")
- t_info = task.wait_until(state_trigger="binary_sensor.kitchen_fridge_door == 'off'",timeout=10)
- if t_info["trigger_type"] != "timeout":
- log.info("Fridge door closed before timeout")
- return
- log.info("Fridge door has been opened for 40 seconds — do a notification.")
- service.call("button", "press", entity_id="button.kitchen_alarm_notification")
- t_info = task.wait_until(state_trigger="binary_sensor.kitchen_fridge_door == 'off'",timeout=10)
- if t_info["trigger_type"] != "timeout":
- log.info("Fridge door closed before timeout")
- return
- log.info("Fridge door has been opened for 50 seconds — do a notification.")
- service.call("button", "press", entity_id="button.kitchen_alarm_notification")
- t_info = task.wait_until(state_trigger="binary_sensor.kitchen_fridge_door == 'off'",timeout=10)
- if t_info["trigger_type"] != "timeout":
- log.info("Fridge door closed before timeout")
- return
- log.info("Fridge door has been opened for 60 seconds — activate alarm.")
- service.call("switch", "turn_on", entity_id="switch.kitchen_audible_alarm")
- t_info = task.wait_until(state_trigger="binary_sensor.kitchen_fridge_door == 'off'",timeout=5*60)
- log.info("Door closed or alarm has been active for 5 minutes — turn off.")
- service.call("switch", "turn_off", entity_id="switch.kitchen_audible_alarm")
|