Forráskód Böngészése

Added Christmas mode switch

pi 1 éve
szülő
commit
458021c766
2 módosított fájl, 77 hozzáadás és 29 törlés
  1. 14 12
      pyscript/kitchen.py
  2. 63 17
      pyscript/state_goto.py

+ 14 - 12
pyscript/kitchen.py

@@ -38,45 +38,47 @@ MQTT-Lights in Home Assistant to achieve the setup that is wanted in each of the
 
 # 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():
-    log.info(f"Kitchen worklight changed. State: {input_boolean.kitchen_worklights}")
+    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("Kitchen LED: Sec switch ON")
+        log.info("1. Kitchen LED: IKEA-switch should be turned ON....")
         if( switch.kok_led_sec_switch == 'on' ):
-            log.info("K LED Sec Switch already On, go directly to PWW-Control")
+            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("Kitchen LED: Sec switch OFF")
+        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"kitchen_worklights_security_switch_trigger() {switch.kok_led_sec_switch}")
+    log.info(f"2. Trigger on kitchen IKEA Switch:{switch.kok_led_sec_switch}")
     if( switch.kok_led_sec_switch=='on' ):
-        log.info("Kitchen LED: Control PWM Wait...")
-        task.sleep(0.5)
-        log.info("Kitchen LED: Go....")
+        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"handle_kitchen_worklights WL:{input_boolean.kitchen_worklights} HALL:{light.hall_door}")
+    log.info(f"3. Kitchen LED. Control PWMs:{input_boolean.kitchen_worklights} HALL:{light.hall_door}")
     if( input_boolean.kitchen_worklights == 'on' ):
-        log.info("K LED Full mode...")
+        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("K LED Soft mode...")
+        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("K LED Off mode...")
+        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')
 

+ 63 - 17
pyscript/state_goto.py

@@ -13,24 +13,42 @@ got EVENT_STATE_CHANGED with kwargs=
      }
 """
 
+"""
+monitor_the_light_mode_selector() kwargs=
+{'trigger_type': 'state', 
+'var_name': 'input_select.light_mode', 
+'value': 'Morning', 
+'old_value': 'No action', 
+'context': <homeassistant.core.Context object at 0x7f3b26bff640>}
+"""
+#@state_trigger("input_boolean.christmas")
+#def a_test_for_monitor_the_light_mode_selector(value=None, old_value=None):
+#def a_test_for_monitor_the_light_mode_selector(**kwargs):
+    #log.info(f"a_test_for_monitor_the_light_mode_selector() kwargs={kwargs}")
+    #log.info(f"a_test_for_monitor_the_light_mode_selector() {value}   {old_value}")
+    #log.info(f"Value:{input_boolean.christmas}   Type:" + str( type(input_boolean.christmas) ))
+    #if( input_boolean.christmas == 'on' ): log.info("ON")
+    #if( input_boolean.christmas == 'off' ): log.info("OFF")
+    #isChristmas  = True if input_boolean.christmas == 'on' else False
+    #log.info("BooL: " + str(x))
 
 """
 Also, please don't set a state variable inside a function that is trigged by EVENT_STATE_CHANGED,
  or make a service call that is triggered by EVENT_CALL_SERVICE, unless the trigger condition is False
  in those cases. Otherwise bad things will happen...
 """
-@event_trigger(EVENT_CALL_SERVICE, "domain == 'scene' and service == 'turn_on'")
-def monitor_scene_service_events(domain=None, service=None, service_data=None):
-    log.info(f"Scene activated: >{service_data['entity_id']}<")
-    if( service_data['entity_id'] == 'scene.test_scene_a'):
-        log.info("Yes. It's A")
-    elif( service_data['entity_id'] == 'scene.test_scene_b'):
-        log.info("Yes. It's B")
+#@event_trigger(EVENT_CALL_SERVICE, "domain == 'scene' and service == 'turn_on'")
+#def monitor_scene_service_events(domain=None, service=None, service_data=None):
+#    log.info(f"Scene activated: >{service_data['entity_id']}<")
+#    if( service_data['entity_id'] == 'scene.test_scene_a'):
+#        log.info("Yes. It's A")
+#    elif( service_data['entity_id'] == 'scene.test_scene_b'):
+#        log.info("Yes. It's B")
 
 @state_trigger("input_select.light_mode")
-def monitor_service_calls(value=None):
+def monitor_light_mode_selector(value=None, old_value=None):
     if not value is None:
-        log.info(f"Light state change to: {value}")
+        log.info(f"Light state change to: {value}, old value: {old_value}")
         if value == 'All on':
             log.info("Go to All on")
             pyscript.state_goto_all_on()
@@ -43,6 +61,11 @@ def monitor_service_calls(value=None):
         elif value == 'Windows':
             log.info("Go to Windows")
             pyscript.state_goto_window()
+        elif value == 'Re-trigger':
+            log.info("Re-trigger to be done here")
+            # Set mode the the previous mode
+            input_select.light_mode = old_value
+
     else:
         log.info("Light state was None")
 
@@ -73,7 +96,8 @@ def handleWallButtonPressed():
 @service
 def state_goto_all_on():
     task.unique('state_goto_py')
-    log.info(f"********* ALL ON *********** State: " + input_select.light_mode )
+    isChristmas  = True if input_boolean.christmas == 'on' else False
+    log.info(f"********* ALL ON *********** State:{input_select.light_mode} Jul:{isChristmas}")
     light.turn_on(entity_id='light.tradfri_bulb')   # Stora flyglampan
     light.turn_on(entity_id='light.hall_inner', brightness=38, color_temp_kelvin=3800)
     light.turn_on(entity_id='light.hall_door', brightness=38, color_temp_kelvin=3800)
@@ -88,6 +112,9 @@ def state_goto_all_on():
         #task.sleep(0.5)
         light.turn_on(entity_id='light.liv_room_corner_lamp',brightness='1')
         task.sleep(0.5)
+        if( isChristmas ):
+            light.turn_on(entity_id=' light.main_button_4') # Christmas lights
+            task.sleep(0.5)
 
     pyscript.kitchen_worklights_button_trigger()
     #pyscript.handle_bedroom_light()   # Temp disable when high energy prices
@@ -96,10 +123,14 @@ def state_goto_all_on():
 @service
 def state_goto_window():
     task.unique('state_goto_py')
-    log.info(f"********* WINDOW *********** State: " + input_select.light_mode )
+    isChristmas  = True if input_boolean.christmas == 'on' else False
+    log.info(f"********* WINDOW *********** State:{input_select.light_mode} Jul:{isChristmas}")
     light.turn_off(entity_id='light.tradfri_bulb')
     switch.turn_on(entity_id='switch.koksfonster')
-    switch.turn_off(entity_id='switch.matsal_altandorr')
+    if( isChristmas ):
+        switch.turn_on(entity_id='switch.matsal_altandorr')
+    else:
+        switch.turn_off(entity_id='switch.matsal_altandorr')
     switch.turn_off(entity_id='switch.matsal_piano')
     light.turn_off(entity_id='light.hall_inner')
     light.turn_off(entity_id='light.hall_door')
@@ -107,10 +138,13 @@ def state_goto_window():
     switch.turn_off(entity_id='switch.sovrum')
 
     for x in range(2):
-        light.turn_off(entity_id='light.liv_room_table_lamp')
-        task.sleep(0.5)
+        #light.turn_off(entity_id='light.liv_room_table_lamp')
+        #task.sleep(0.5)
         light.turn_off(entity_id='light.liv_room_corner_lamp')
         task.sleep(0.5)
+        if( isChristmas ):
+            light.turn_on(entity_id=' light.main_button_4') # Christmas lights
+            task.sleep(0.5)
     
     pyscript.kitchen_worklights_button_trigger()
     #pyscript.handle_bedroom_light()
@@ -118,11 +152,15 @@ def state_goto_window():
 @service
 def state_goto_morning():
     task.unique('state_goto_py')
-    log.info(f"********* MORNING *********** State: " + str(input_select.light_mode) )
+    isChristmas  = True if input_boolean.christmas == 'on' else False
+    log.info(f"********* MORNING *********** State:{input_select.light_mode} Jul:{isChristmas}")
     light.turn_off(entity_id='light.tradfri_bulb')
     switch.turn_on(entity_id='switch.matsal_altandorr')
     switch.turn_off(entity_id='switch.matsal_piano')
-    switch.turn_off(entity_id='switch.koksfonster')
+    if( isChristmas ):
+        switch.turn_on(entity_id='switch.koksfonster')
+    else:
+        switch.turn_off(entity_id='switch.koksfonster')
     light.turn_off(entity_id='light.hall_inner')
     light.turn_on(entity_id='light.hall_door', brightness=25, color_temp_kelvin=2570)
     light.turn_off(entity_id='light.rislampa')
@@ -132,6 +170,9 @@ def state_goto_morning():
         #light.turn_on(entity_id='light.liv_room_corner_lamp',brightness='1')
         light.turn_off(entity_id='light.liv_room_corner_lamp')
         task.sleep(0.5)
+        if( isChristmas ):
+            light.turn_off(entity_id=' light.main_button_4') # Christmas lights
+            task.sleep(0.5)
     
     pyscript.kitchen_worklights_button_trigger()
     #pyscript.handle_bedroom_light()
@@ -139,7 +180,8 @@ def state_goto_morning():
 @service
 def state_goto_all_off():
     task.unique('state_goto_py')
-    log.info(f"********* ALL OFF *********** State: " + input_select.light_mode )
+    isChristmas  = True if input_boolean.christmas == 'on' else False
+    log.info(f"********* ALL OFF *********** State:{input_select.light_mode} Jul:{isChristmas}")
     light.turn_off(entity_id='light.tradfri_bulb')
     light.turn_off(entity_id='light.hall_inner')
     light.turn_off(entity_id='light.hall_door')
@@ -155,6 +197,10 @@ def state_goto_all_off():
         #task.sleep(0.5)
         light.turn_off(entity_id='light.liv_room_corner_lamp')
         task.sleep(0.5)
+        if( isChristmas ):
+            light.turn_off(entity_id=' light.main_button_4') # Christmas lights
+            task.sleep(0.5)
+    
     input_boolean.movie_mode = 'off'
     pyscript.kitchen_worklights_button_trigger()