Przeglądaj źródła

Pyscript is working for Piano-light.

Thomas Chef 4 lat temu
rodzic
commit
7f7fbb655c
3 zmienionych plików z 26 dodań i 47 usunięć
  1. 0 39
      packages/test_stuff.yaml
  2. 0 8
      pyscript/example.py
  3. 26 0
      pyscript/piano_light_control.py

+ 0 - 39
packages/test_stuff.yaml

@@ -1,5 +1,4 @@
 
-
 light:
   - platform: rflink
     devices:
@@ -21,42 +20,4 @@ sensor:
 input_boolean:
   piano_light_wanted_state:
     name: Piano-lamp, wanted state
-    initial: off
     icon: mdi:lightbulb
-
-automation:
-  - alias: Piano-lamp control automation
-    trigger:
-    - platform: state
-      entity_id: input_boolean.piano_light_wanted_state
-    - platform: event
-      event_type: timer.finished
-      event_data:
-        entity_id: timer.piano_lamp_control_timer
-    condition: 
-      condition: template
-      value_template: >
-        {% if state_attr('sensor.piano_lamp_repeater_status', 'status') == states('input_boolean.piano_light_wanted_state') %}
-          false
-        {% else %}
-          true
-        {% endif %}
-    action:
-    - service_template: light.turn_{{states('input_boolean.piano_light_wanted_state')}}
-      entity_id: light.piano_lamp_repeater
-    - service_template: light.turn_{{states('input_boolean.piano_light_wanted_state')}}
-      entity_id: light.piano_lamp
-
-    - service_template: >
-        {% if state_attr('sensor.piano_lamp_repeater_status', 'status') == states('input_boolean.piano_light_wanted_state') %}
-          timer.cancel
-        {% else %}
-          timer.start
-        {% endif %}
-      entity_id: timer.piano_lamp_control_timer
-    mode: single
-
-
-timer:
-  piano_lamp_control_timer:
-    duration: '00:00:30'

+ 0 - 8
pyscript/example.py

@@ -1,8 +0,0 @@
-@service
-def hello_world(action=None, id=None):
-    """hello_world example using pyscript."""
-    log.info(f"XXXXX    THOMAS      hello world: got action {action} id {id}")
-    if action == "turn_on" and id is not None:
-        light.turn_on(entity_id=id, brightness=255)
-    elif action == "fire" and id is not None:
-        event.fire(id, param1=12, pararm2=80)

+ 26 - 0
pyscript/piano_light_control.py

@@ -0,0 +1,26 @@
+
+@state_trigger("input_boolean.piano_light_wanted_state")
+@task_unique("piano_light_control", kill_me=True)
+def control_piano_light():
+
+    # Set max number of iterations
+    maxIterations = 10
+
+    # Loop until we have succeeded to controlling the light
+    while(
+             ( (sensor.piano_lamp_repeater_status != input_boolean.piano_light_wanted_state) or 
+               (                  light.piano_lamp!= input_boolean.piano_light_wanted_state) ) and maxIterations > 0
+    ):
+
+        # Find out if we should turn the light on or off
+        action = "turn_off" if input_boolean.piano_light_wanted_state == 'off' else "turn_on"
+        
+        service.call("light", action, entity_id="light.piano_lamp")
+        service.call("light", action, entity_id="light.piano_lamp_repeater")
+
+        log.info("Sleeping 60s.....")
+        trig_info = task.wait_until(state_trigger=["input_boolean.piano_light_wanted_state","sensor.piano_lamp_repeater_status"], timeout=60)
+
+        maxIterations -= 1
+    
+    log.info(f"Exit control_piano_light")