Browse Source

bleak: ensure target temp and state changes are taking effect

Thomas B 1 month ago
parent
commit
4fa5e53bfc
1 changed files with 28 additions and 9 deletions
  1. 28
    9
      test_bleak/flow.py

+ 28
- 9
test_bleak/flow.py View File

@@ -31,9 +31,28 @@ async def sleep(t):
31 31
         print_bar(i + 1, 0, w, "s")
32 32
     print()
33 33
 
34
+async def ensure_target_temp(client, temp):
35
+    for i in range(0, 10):
36
+        await set_target_temp(client, temp)
37
+        target = await get_target_temp(client)
38
+        if target == temp:
39
+            return
40
+        #print("retry target temp")
41
+    raise Exception("could not set target temp")
42
+
43
+async def ensure_state(client, state):
44
+    for i in range(0, 10):
45
+        await set_state(client, state)
46
+        heater, pump = state
47
+        t_heater, t_pump = await get_state(client)
48
+        if (heater == t_heater) and (pump == t_pump):
49
+            return
50
+        #print("retry state")
51
+    raise Exception("could not set state")
52
+
34 53
 async def wait_for_temp(client, temp):
35 54
     print("Setting temperature {}".format(temp))
36
-    await set_target_temp(client, temp)
55
+    await ensure_target_temp(client, temp)
37 56
 
38 57
     print("Waiting for temperature to rise...")
39 58
     start = await get_current_temp(client)
@@ -54,13 +73,13 @@ async def flow_step(client, temp, t_wait, t_pump):
54 73
     await sleep(t_wait)
55 74
 
56 75
     print("Pumping for {}s".format(t_pump))
57
-    await set_state(client, (True, True)) # turn on pump
76
+    await ensure_state(client, (True, True)) # turn on pump
58 77
     await sleep(t_pump)
59
-    await set_state(client, (True, False)) # turn off pump
78
+    await ensure_state(client, (True, False)) # turn off pump
60 79
 
61 80
 async def flow(client):
62 81
     print("Turning on heater")
63
-    await set_state(client, (True, False))
82
+    await ensure_state(client, (True, False))
64 83
 
65 84
     await flow_step(client, 185.0, 10.0, 7.0)
66 85
     await flow_step(client, 195.0, 5.0, 23.0)
@@ -69,15 +88,15 @@ async def flow(client):
69 88
     print("Notification by pumping three times...")
70 89
     for i in range(0, 3):
71 90
         await asyncio.sleep(1.0)
72
-        await set_state(client, (True, True)) # turn on pump
91
+        await ensure_state(client, (True, True)) # turn on pump
73 92
         await asyncio.sleep(1.0)
74
-        await set_state(client, (True, False)) # turn off pump
93
+        await ensure_state(client, (True, False)) # turn off pump
75 94
 
76 95
     print("Turning heater off")
77
-    await set_state(client, (False, False)) # turn off heater and pump
96
+    await ensure_state(client, (False, False)) # turn off heater and pump
78 97
 
79 98
     print("Resetting temperature")
80
-    await set_target_temp(client, 190.0)
99
+    await ensure_target_temp(client, 190.0)
81 100
 
82 101
 async def main(address):
83 102
     device = await ble_conn(address)
@@ -93,7 +112,7 @@ async def main(address):
93 112
             await flow(client)
94 113
         except:
95 114
             print("\nTurning heater and pump off")
96
-            await set_state(client, (False, False)) # turn off heater and pump
115
+            await ensure_state(client, (False, False)) # turn off heater and pump
97 116
             raise
98 117
 
99 118
         print("Disconnecting...")

Loading…
Cancel
Save