Procházet zdrojové kódy

Added fridge alarm through HA

pi před 3 měsíci
rodič
revize
f512c42bf2
2 změnil soubory, kde provedl 48 přidání a 10 odebrání
  1. 12 3
      mqtt.yaml
  2. 36 7
      pyscript/kitchen.py

+ 12 - 3
mqtt.yaml

@@ -76,10 +76,10 @@ binary_sensor:
 
 switch:
     - name: "Kitchen Audible Alarm"
-      state_topic: "/kitchen/alarm/state"
-      command_topic: "/kitchen/alarm/set"
+      state_topic: "kitchen/alarm/state"
+      command_topic: "kitchen/alarm/set"
       availability:
-      - topic: "kitchen/fridge/available"
+      - topic: "kitchen/alarm/available"
       payload_on: "on"
       payload_off: "off"
       state_on: "on"
@@ -87,3 +87,12 @@ switch:
       retain: true
       qos: 1
       unique_id: "kitchen_audible_alarm"
+
+button:
+    - name: "Kitchen Alarm Notification"
+      command_topic: "kitchen/alarm/notification"
+      payload_press: "notification"
+      qos: 1
+      retain: false
+      unique_id: "kitchen_alarm_notification"
+

+ 36 - 7
pyscript/kitchen.py

@@ -82,12 +82,41 @@ def handle_kitchen_worklights():
         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")
 
 
-@state_trigger("select.fridge_door_alarm")
-def reset_fridge_alarm_state(value=None, old_value=None):
-    log.info(f"********* FRIDGE ALARM CONFIG *********** To:" + value + " From:" + old_value)
-    if value == 'TEST':
-        #task.sleep(1)  # Wait 1 second
-        select.fridge_door_alarm = old_value
-        mqtt.publish(topic="kitchen/fridge/doorAlarmControl", payload=old_value , retain=True)