فهرست منبع

Repeat codes in goto_mode scripts.

pi 4 سال پیش
والد
کامیت
0dffef7b48
2فایلهای تغییر یافته به همراه88 افزوده شده و 24 حذف شده
  1. 19 0
      pyscript/bedroom.py
  2. 69 24
      pyscript/state_goto.py

+ 19 - 0
pyscript/bedroom.py

@@ -0,0 +1,19 @@
+
+@state_trigger("binary_sensor.bedroom_door")
+def bedroom_door_sensor():
+    log.info(f"********* BEDROOM DOOR: "+binary_sensor.bedroom_door+" ***********")
+    handle_bedroom_light()
+
+
+@service
+@task_unique("handle_bedroom_light")
+def handle_bedroom_light():
+    log.info(f"********* BEDROOM LIGHT CONTROL ***********" + light.main_button_4 + " - "+ binary_sensor.bedroom_door)
+    task.sleep(1)
+    if light.main_button_4 == 'on' and binary_sensor.bedroom_door == 'on':
+        # If OPEN
+        switch.turn_on(entity_id='switch.tradfri_outlet')
+    else:
+        switch.turn_off(entity_id='switch.tradfri_outlet')
+
+    

+ 69 - 24
pyscript/state_goto.py

@@ -1,56 +1,101 @@
 
 
-"""
 def getCurrentState():
 def getCurrentState():
-    if( light.main_button_1 == 'on' && )    # All ON
-        light.main_button_2 == 'on' &&
-        light.main_button_3 == 'on' &&
+    if( light.main_button_1 == 'on' and     # All ON
+        light.main_button_2 == 'on' and
+        light.main_button_3 == 'on' and
         light.main_button_4 == 'on' ):
         light.main_button_4 == 'on' ):
         return 1
         return 1
-    elif( light.main_button_1 == 'off' && ) # Window
-        light.main_button_2 == 'off' &&
-        light.main_button_3 == 'off' &&
+    elif( light.main_button_1 == 'off' and  # Window
+        light.main_button_2 == 'off' and
+        light.main_button_3 == 'off' and
         light.main_button_4 == 'on' ):
         light.main_button_4 == 'on' ):
         return 2
         return 2
-    elif( light.main_button_1 == 'off' && ) # Morning
-        light.main_button_2 == 'on' &&
-        light.main_button_3 == 'off' &&
+    elif( light.main_button_1 == 'off' and  # Morning
+        light.main_button_2 == 'on' and
+        light.main_button_3 == 'off' and
         light.main_button_4 == 'on' ):
         light.main_button_4 == 'on' ):
         return 3
         return 3
-    elif( light.main_button_1 == 'off' && ) # All OFF
-        light.main_button_2 == 'off' &&
-        light.main_button_3 == 'off' &&
+    elif( light.main_button_1 == 'off' and  # All OFF
+        light.main_button_2 == 'off' and
+        light.main_button_3 == 'off' and
         light.main_button_4 == 'off' ):
         light.main_button_4 == 'off' ):
         return 4
         return 4
-"""
+
+# Important info:
+#
+# We are not allowed to resend buttons 1 and 2
+# 1: Glas bulb lamp in living room
+# 2: Corner lamp (IKEA-big-bulb) in living room
+# If ON is sent multiple times, then they will change DIM mode.
 
 
 @service
 @service
 def state_goto_all_on():
 def state_goto_all_on():
+    task.unique('state_goto_py')
     log.info(f"********* ALL ON ***********")
     log.info(f"********* ALL ON ***********")
-    switch.turn_on(entity_id='switch.group_button_all')
     light.turn_on(entity_id='light.tradfri_bulb')
     light.turn_on(entity_id='light.tradfri_bulb')
-    switch.turn_on(entity_id='switch.tradfri_outlet')   # Sovrum ON
+    for x in range(2):
+        switch.turn_on(entity_id='switch.group_button_all')
+        task.sleep(1)
+
+    pyscript.handle_bedroom_light()
+
 
 
 @service
 @service
 def state_goto_window():
 def state_goto_window():
+    task.unique('state_goto_py')
+    cs = getCurrentState()
     log.info(f"********* WINDOW ***********")
     log.info(f"********* WINDOW ***********")
-    switch.turn_off(entity_id='switch.group_button_all')
-    light.turn_on(entity_id=' light.main_button_4')
     light.turn_off(entity_id='light.tradfri_bulb')
     light.turn_off(entity_id='light.tradfri_bulb')
-    switch.turn_on(entity_id='switch.tradfri_outlet')   # Sovrum ON
+    
+    if cs == 4 or cs == 2:  # From Off or already Window
+        for x in range(2):
+            light.turn_on(entity_id=' light.main_button_4')
+            task.sleep(0.5)
+    elif cs == 3:           # From morning
+        for x in range(2):
+            light.turn_off(entity_id=' light.main_button_2')
+            task.sleep(0.5)
+    else:                   # From ALL ON
+        for x in range(2):
+            switch.turn_off(entity_id='switch.group_button_all')
+            task.sleep(0.5)
+        for x in range(2):
+            light.turn_on(entity_id=' light.main_button_4')
+            task.sleep(0.5)
+    
+    pyscript.handle_bedroom_light()
 
 
 @service
 @service
 def state_goto_morning():
 def state_goto_morning():
+    task.unique('state_goto_py')
+    cs = getCurrentState()
     log.info(f"********* MORNING ***********")
     log.info(f"********* MORNING ***********")
-    switch.turn_off(entity_id='switch.group_button_all')
-    light.turn_on(entity_id=' light.main_button_2')
-    light.turn_on(entity_id=' light.main_button_4')
     light.turn_off(entity_id='light.tradfri_bulb')
     light.turn_off(entity_id='light.tradfri_bulb')
-    switch.turn_off(entity_id='switch.tradfri_outlet')  # Sovrum OFF
+    if cs == 4 or cs == 3:  # Off or already Morning
+        for x in range(2):
+            light.turn_on(entity_id=' light.main_button_4')
+            task.sleep(0.5)
+        light.turn_on(entity_id=' light.main_button_2')
+    elif cs == 2:           # From Window
+        light.turn_on(entity_id=' light.main_button_2')
+    else:                   # From ALL ON
+        for x in range(2):
+            switch.turn_off(entity_id='switch.group_button_all')
+            task.sleep(0.5)
+        light.turn_on(entity_id=' light.main_button_2')
+        for x in range(2):
+            light.turn_on(entity_id=' light.main_button_4')
+            task.sleep(0.5)
+    
+    pyscript.handle_bedroom_light()
 
 
 @service
 @service
 def state_goto_all_off():
 def state_goto_all_off():
+    task.unique('state_goto_py')
     log.info(f"********* ALL OFF ***********")
     log.info(f"********* ALL OFF ***********")
-    switch.turn_off(entity_id='switch.group_button_all')
     light.turn_off(entity_id='light.tradfri_bulb')
     light.turn_off(entity_id='light.tradfri_bulb')
     switch.turn_off(entity_id='switch.tradfri_outlet')  # Sovrum OFF
     switch.turn_off(entity_id='switch.tradfri_outlet')  # Sovrum OFF
+    for x in range(2):
+        switch.turn_off(entity_id='switch.group_button_all')
+        task.sleep(0.5)