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

Changed to input-select for light mode

pi 1 éve
szülő
commit
699964f5b1
5 módosított fájl, 156 hozzáadás és 131 törlés
  1. 1 1
      automations.yaml
  2. 2 2
      pyscript/home_automations.py
  3. 69 98
      pyscript/state_goto.py
  4. 18 30
      pyscript/wall_buttons.py
  5. 66 0
      scenes.yaml

+ 1 - 1
automations.yaml

@@ -1 +1 @@
-[]
+[]

+ 2 - 2
pyscript/home_automations.py

@@ -12,7 +12,7 @@ def automate_home_auto_lights_off():
         pyscript.home_auto_lights_off_has_been_night = 'false'
         
         if input_boolean.auto_lights_off == 'on':
-        	pyscript.state_goto_all_off()
+            input_select.light_mode = 'All off'
 
 
 # Activate morning lights when Thomas gets up
@@ -22,4 +22,4 @@ def morning_lights_thomas():
         if switch.eng_heat_a == 'on' or switch.eng_heat_b == 'on':
             if sensor.lux_outside_the_garage != 'unknown' and int(sensor.lux_outside_the_garage) < 3:
                 log.info(f"********* THOMAS MORNING ON ***********")
-                pyscript.state_goto_morning()
+                input_select.light_mode = 'Morning'

+ 69 - 98
pyscript/state_goto.py

@@ -1,40 +1,61 @@
 from homeassistant.const import EVENT_STATE_CHANGED
+from datetime import datetime, time
+
+@state_trigger("input_select.light_mode")
+def monitor_service_calls(value=None):
+    if not value is None:
+        log.info(f"Light state change to: {value}")
+        if value == 'All on':
+            log.info("Go to All on")
+            pyscript.state_goto_all_on()
+        elif value == 'All off':
+            log.info("Go to All off")
+            pyscript.state_goto_all_off()
+        elif value == 'Morning':
+            log.info("Go to Morning")
+            pyscript.state_goto_morning()
+        elif value == 'Windows':
+            log.info("Go to Windows")
+            pyscript.state_goto_window()
+    else:
+        log.info("Light state was None")
+
+@service
+def handleWallButtonPressed():
+    log.info(f"The wall button has been pressed. Curr state: {input_select.light_mode}")
+
+    currHour = datetime.now().time().hour
+    
+    if( input_select.light_mode == 'All on' ):
+        input_select.light_mode = 'All off'
+
+    elif( input_select.light_mode == 'Morning' ):
+        input_select.light_mode = 'All off'
+
+    elif( input_select.light_mode == 'All off' ):
+        
+        log.info(f'Is All off. Hour is: {currHour}')
+        if( currHour == 5 or currHour == 6 ):
+            input_select.light_mode = 'Morning'
+        else:
+            input_select.light_mode = 'All on'
+    
+    elif( input_select.light_mode == 'Windows' ):
+        input_select.light_mode = 'All on'
 
-def getCurrentState():
-    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' ):
-        return 1
-    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' ):
-        return 2
-    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' ):
-        return 3
-    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' ):
-        return 4
 
 @service
 def state_goto_all_on():
     task.unique('state_goto_py')
-    cs = getCurrentState()
-    log.info(f"********* ALL ON *********** State: " + str(cs) )
+    log.info(f"********* ALL ON *********** State: " + input_select.light_mode )
     light.turn_on(entity_id='light.tradfri_bulb')   # Stora flyglampan
     light.turn_on(entity_id='light.hall_inner')
     light.turn_on(entity_id='light.hall_door')
-    #switch.turn_on(entity_id='switch.matsal')
+    light.turn_on(entity_id='light.rislampa')
+    switch.turn_on(entity_id='switch.matsal_altandorr')
+    switch.turn_on(entity_id='switch.matsal_piano')
     switch.turn_on(entity_id='switch.koksfonster')
     for x in range(2):
-        switch.turn_on(entity_id='switch.group_button_all')
-        task.sleep(0.5)
         #light.turn_on(entity_id='light.liv_room_table_lamp',brightness='1') # Temp disable when high energy prices
         #task.sleep(0.5)
         light.turn_on(entity_id='light.liv_room_corner_lamp',brightness='1')
@@ -47,36 +68,20 @@ def state_goto_all_on():
 @service
 def state_goto_window():
     task.unique('state_goto_py')
-    cs = getCurrentState()
-    log.info(f"********* WINDOW *********** State: " + str(cs) )
+    log.info(f"********* WINDOW *********** State: " + input_select.light_mode )
     light.turn_off(entity_id='light.tradfri_bulb')
-    #switch.turn_off(entity_id='switch.matsal')
     switch.turn_on(entity_id='switch.koksfonster')
+    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')
+    light.turn_off(entity_id='light.rislampa')
 
-    
-    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)
-            light.turn_off(entity_id='light.liv_room_corner_lamp')
-            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)
-            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)
-        for x in range(2):
-            light.turn_on(entity_id=' light.main_button_4')
-            task.sleep(0.5)
+    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_corner_lamp')
+        task.sleep(0.5)
     
     pyscript.kitchen_worklights_button_trigger()
     #pyscript.handle_bedroom_light()
@@ -84,39 +89,18 @@ def state_goto_window():
 @service
 def state_goto_morning():
     task.unique('state_goto_py')
-    cs = getCurrentState()
-    log.info(f"********* MORNING *********** State: " + str(cs) )
+    log.info(f"********* MORNING *********** State: " + str(input_select.light_mode) )
     light.turn_off(entity_id='light.tradfri_bulb')
-    #switch.turn_off(entity_id='switch.matsal')
+    switch.turn_off(entity_id='switch.matsal_altandorr')
+    switch.turn_on(entity_id='switch.matsal_piano')
     switch.turn_on(entity_id='switch.koksfonster')
     light.turn_off(entity_id='light.hall_inner')
     light.turn_on(entity_id='light.hall_door')
+    light.turn_off(entity_id='light.rislampa')
 
-    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')
-            task.sleep(0.5)
-            light.turn_on(entity_id='light.liv_room_corner_lamp',brightness='1')
-            task.sleep(0.5)
-    elif cs == 2:           # From Window
-        for x in range(2):
-            light.turn_on(entity_id=' light.main_button_2')
-            task.sleep(0.5)
-            light.turn_on(entity_id='light.liv_room_corner_lamp',brightness='1')
-            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_2')
-            task.sleep(0.5)
-            light.turn_on(entity_id=' light.main_button_4')
-            task.sleep(0.5)
-            light.turn_on(entity_id='light.liv_room_corner_lamp',brightness='1')
-            task.sleep(0.5)
+    for x in range(2):
+        light.turn_on(entity_id='light.liv_room_corner_lamp',brightness='1')
+        task.sleep(0.5)
     
     pyscript.kitchen_worklights_button_trigger()
     #pyscript.handle_bedroom_light()
@@ -124,18 +108,17 @@ def state_goto_morning():
 @service
 def state_goto_all_off():
     task.unique('state_goto_py')
-    cs = getCurrentState()
-    log.info(f"********* ALL OFF *********** State: " + str(cs) )
+    log.info(f"********* ALL OFF *********** State: " + input_select.light_mode )
     light.turn_off(entity_id='light.tradfri_bulb')
     light.turn_off(entity_id='light.hall_inner')
     light.turn_off(entity_id='light.hall_door')
+    light.turn_off(entity_id='light.rislampa')
 
     switch.turn_off(entity_id='switch.koksfonster')
-    #switch.turn_off(entity_id='switch.matsal')  # Temp disable when high energy prices
+    switch.turn_off(entity_id='switch.matsal_altandorr')
+    switch.turn_off(entity_id='switch.matsal_piano')
     #switch.turn_off(entity_id='switch.tradfri_outlet')  # Sovrum OFF  # Temp disable when high energy prices
     for x in range(2):
-        switch.turn_off(entity_id='switch.group_button_all')
-        task.sleep(0.5)
         #light.turn_off(entity_id='light.liv_room_table_lamp')  # Temp disable when high energy prices
         #task.sleep(0.5)
         light.turn_off(entity_id='light.liv_room_corner_lamp')
@@ -144,13 +127,10 @@ def state_goto_all_off():
     pyscript.kitchen_worklights_button_trigger()
 
 
-
-
 @state_trigger("input_boolean.movie_mode")
 def movie_mode_button_trigger():
-    cs = getCurrentState()
-    log.info(f"********* MOVIE MODE *********** To:" + input_boolean.movie_mode + " State: " + str(cs) )
-    if input_boolean.movie_mode == 'off' and switch.group_button_all == 'on':
+    log.info(f"********* MOVIE MODE *********** To:" + input_boolean.movie_mode + " State: " + input_select.light_mode )
+    if input_boolean.movie_mode == 'off' and input_select.light_mode == 'All on':
         log.info(f"Movie mode, turn lights ON")
         for x in range(2):
             light.turn_on(entity_id='light.liv_room_table_lamp',brightness='1')
@@ -167,12 +147,3 @@ def movie_mode_button_trigger():
             task.sleep(0.5)
 
 
-@event_trigger(EVENT_STATE_CHANGED, "entity_id=='input_button.av'")
-def on_ui_button_all_off_clicked(entity_id, new_state, old_state):
-    pyscript.state_goto_all_off()
-
-@event_trigger(EVENT_STATE_CHANGED, "entity_id=='input_button.allt_pa'")
-def on_ui_button_all_on_clicked(entity_id, new_state, old_state):
-    pyscript.state_goto_all_on()
-
-    

+ 18 - 30
pyscript/wall_buttons.py

@@ -1,3 +1,6 @@
+from homeassistant.const import EVENT_STATE_CHANGED
+
+
 """
 trigger file.wall_buttons.hallway_wall_button got event trigger, running action 
 (kwargs = {
@@ -10,10 +13,12 @@ trigger file.wall_buttons.hallway_wall_button got event trigger, running action
 'state': 'on'
 })
 
-        kalle = kwargs.context.get("entity_id", None)
-                ^
-AttributeError: 'dict' object has no attribute 'context'
-"""
+kwargs={
+'trigger_type': 'state', 
+'var_name': 'input_select.light_mode',
+ 'value': 'Morning',
+   'old_value': 'All on',
+     'context': <homeassistant.core.Context object at 0x7ff02cc49800>}
 
 # Mode Panel:
 # 1: All ON
@@ -21,23 +26,11 @@ AttributeError: 'dict' object has no attribute 'context'
 # 3: Morning-mode
 # 4: All OFF
 
+@event_trigger(EVENT_CALL_SERVICE)
+def monitor_service_calls(**kwargs):
+    log.info(f"got EVENT_CALL_SERVICE with kwargs={kwargs}")
 
-
-"""
-
-ERROR:
-2021-05-13 20:52:14 ERROR (MainThread) [custom_components.pyscript.function] run_coro: got exception Traceback (most recent call last):
-  File "/config/custom_components/pyscript/eval.py", line 710, in call
-    raise TypeError(f"{self.name}() called with unexpected keyword arguments: {unexpected}")
-TypeError: hallway_wall_button() called with unexpected keyword arguments: state
-
-
-got button_pressed: kwargs={
-    'trigger_type': 'event', 
-    'event_type': 'button_pressed', 
-    'context': Context(user_id=None, parent_id=None, id='01ce32cfcc4bed50ce9301ae967fa951'), 
-    'entity_id': 'switch.mode_panel_4', 
-    'state': 'off'}
+The events below (mode panel 1-4) are incoming as a rflink, newkaku-switch.
 """
 
 
@@ -47,24 +40,19 @@ def hallway_wall_button(entity_id=None, state=None):
     log.info(f"*************** *  got EVENT_CALL_SERVICE with Id:{entity_id}  State:{state}")
 
     if( entity_id == 'switch.hall_switch' ):
-        if( switch.group_button_all == 'on'):
-            pyscript.state_goto_all_off()
-        else:
-            pyscript.state_goto_all_on()
+        pyscript.handleWallButtonPressed()
     
     if( entity_id == 'switch.mode_panel_1' ):   # All ON
-        pyscript.state_goto_all_on()
+        input_select.light_mode = 'All on'
     
     if( entity_id == 'switch.mode_panel_2' ):   # Window
-        pyscript.state_goto_window()
-        #log.info(f"This mode has been disabled.")
+        input_select.light_mode = 'Windows'
     
     if( entity_id == 'switch.mode_panel_3' ):   # Morning
-        #log.info(f"This mode has been disabled.")
-        pyscript.state_goto_morning()
+        input_select.light_mode = 'Morning'
     
     if( entity_id == 'switch.mode_panel_4' ):   # All OFF
-        pyscript.state_goto_all_off()
+        input_select.light_mode = 'All off'
 
     if( entity_id == 'switch.mode_panel_kwl' ):   # Kitchen work lights
         input_boolean.toggle(entity_id='input_boolean.kitchen_worklights')

+ 66 - 0
scenes.yaml

@@ -0,0 +1,66 @@
+- id: '1698571163168'
+  name: All off
+  entities:
+    switch.matsal_altandorr:
+      friendly_name: Matsal altandörr
+      state: 'off'
+    light.hall_door:
+      min_color_temp_kelvin: 2202
+      max_color_temp_kelvin: 4000
+      min_mireds: 250
+      max_mireds: 454
+      supported_color_modes:
+      - color_temp
+      friendly_name: Hall_door
+      supported_features: 32
+      state: 'off'
+    light.tradfri_bulb:
+      supported_color_modes:
+      - brightness
+      friendly_name: VRum Stora Flyglampan
+      supported_features: 32
+      state: 'off'
+    light.hall_inner:
+      min_color_temp_kelvin: 2202
+      max_color_temp_kelvin: 4000
+      min_mireds: 250
+      max_mireds: 454
+      supported_color_modes:
+      - color_temp
+      friendly_name: Hall_inner
+      supported_features: 32
+      state: 'off'
+    light.rislampa:
+      min_color_temp_kelvin: 2202
+      max_color_temp_kelvin: 4000
+      min_mireds: 250
+      max_mireds: 454
+      supported_color_modes:
+      - color_temp
+      friendly_name: Rislampa
+      supported_features: 32
+      state: 'off'
+    switch.koksfonster:
+      friendly_name: Köksfönster
+      state: 'off'
+    light.liv_room_corner_lamp:
+      supported_color_modes:
+      - brightness
+      friendly_name: Liv_room_corner_lamp
+      supported_features: 0
+      state: 'off'
+    light.liv_room_table_lamp:
+      supported_color_modes:
+      - brightness
+      friendly_name: Liv_room_table_lamp
+      supported_features: 0
+      state: 'off'
+    switch.matsal_piano:
+      friendly_name: Matsal Piano
+      state: 'off'
+  icon: mdi:ceiling-light-multiple
+  metadata:
+    light.liv_room_corner_lamp:
+      entity_only: true
+    light.liv_room_table_lamp:
+      entity_only: true