@bldgblocks/node-red-contrib-quietcool 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 BldgBlocks
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,135 @@
1
+ # node-red-contrib-quietcool
2
+
3
+ Node-RED nodes for controlling QuietCool whole house fans over BLE.
4
+
5
+ ## Requirements
6
+
7
+ - Raspberry Pi (or Linux with BLE)
8
+ - Python 3.9+
9
+ - BlueZ (pre-installed on Raspberry Pi OS)
10
+ - A QuietCool fan with BLE (AFG SMT ES-3.0 or similar)
11
+
12
+ ## Installation
13
+
14
+ ```bash
15
+ cd ~/.node-red
16
+ npm install /path/to/quietcool-ble/node-red-contrib-quietcool
17
+ ```
18
+
19
+ The `postinstall` script automatically creates a Python virtual environment and installs the `bleak` BLE library.
20
+
21
+ Restart Node-RED after installation:
22
+
23
+ ```bash
24
+ node-red-restart
25
+ ```
26
+
27
+ ## Setup
28
+
29
+ ### Quick Setup (recommended)
30
+
31
+ No phone app or HCI snoop needed — pair directly from Node-RED:
32
+
33
+ 1. Drag a **fan control** or **fan sensor** node onto the canvas
34
+ 2. Double-click and create a new fan configuration
35
+ 3. Click **Scan** to find your fan — select it from the dropdown
36
+ 4. Click **New** to generate a Phone ID
37
+ 5. On the fan controller, hold the **Pair** button for ~5 seconds until the LED blinks
38
+ 6. Click **Pair with Fan** in the editor
39
+ 7. Save and deploy — you're done!
40
+
41
+ ### Alternative: Extract Phone ID from existing app pairing
42
+
43
+ If you've already paired with the QuietCool mobile app, you can extract the Phone ID:
44
+
45
+ 1. Enable *Bluetooth HCI snoop log* in Android Developer Options
46
+ 2. Open the QuietCool app and connect to the fan
47
+ 3. Pull the log: `adb bugreport bugreport.zip`
48
+ 4. Extract and parse `btsnoop_hci.log` to find the `PhoneID` in a `Login` command
49
+
50
+ ### Manual fan discovery
51
+
52
+ If the Scan button doesn't work, find the address manually:
53
+
54
+ ```bash
55
+ bluetoothctl scan on
56
+ ```
57
+
58
+ Look for a device named `ATTICFAN_*`. Note the MAC address (e.g., `XX:XX:XX:XX:XX:XX`).
59
+
60
+ ## Nodes
61
+
62
+ ### fan control (`quietcool-control`)
63
+
64
+ Send commands to the fan. Available actions:
65
+
66
+ | Action | Description |
67
+ |--------|-------------|
68
+ | Turn Off | Sets fan to Idle mode |
69
+ | Smart Mode (TH) | Temperature/humidity auto mode |
70
+ | Run High | Continuous run at high speed |
71
+ | Run Low | Continuous run at low speed |
72
+ | Timer | Run for a set duration |
73
+ | Apply Preset | Apply a named profile (Summer, Winter, etc.) |
74
+ | Set Thresholds | Set custom temp/humidity thresholds |
75
+ | Pair | Pair with fan (must be in pairing mode) |
76
+ | Raw | Send any raw API command |
77
+
78
+ Actions can be overridden via `msg.payload`:
79
+
80
+ ```json
81
+ {
82
+ "action": "preset",
83
+ "args": { "name": "Summer" }
84
+ }
85
+ ```
86
+
87
+ ```json
88
+ {
89
+ "action": "timer",
90
+ "args": { "hours": 2, "minutes": 0, "speed": "HIGH" }
91
+ }
92
+ ```
93
+
94
+ ### fan sensor (`quietcool-sensor`)
95
+
96
+ Read data from the fan. Available queries:
97
+
98
+ | Query | Returns |
99
+ |-------|---------|
100
+ | State | Mode, speed, temperature (°F), humidity (%) |
101
+ | Full Status | Complete status with fan info, firmware, presets |
102
+ | Fan Info | Name, model, serial number |
103
+ | Firmware | Firmware and hardware version |
104
+ | Parameters | Current temp/humidity thresholds |
105
+ | Presets | List of preset profiles |
106
+ | Timer Remaining | Time left on active timer |
107
+
108
+ For State and Full Status queries, convenience fields are added:
109
+ - `msg.temperature` — Temperature in °F
110
+ - `msg.humidity` — Humidity %
111
+ - `msg.mode` — Current mode (Idle, Timer, TH)
112
+ - `msg.range` — Current speed (LOW, HIGH, CLOSE)
113
+
114
+ Optional **polling**: set a poll interval (seconds) to auto-query without input triggers.
115
+
116
+ ## Architecture
117
+
118
+ The Node-RED nodes communicate with the fan through a Python bridge process:
119
+
120
+ ```
121
+ Node-RED → stdin JSON → bridge.py → BLE/bleak → QuietCool Fan
122
+ Node-RED ← stdout JSON ← bridge.py ← BLE/bleak ← QuietCool Fan
123
+ ```
124
+
125
+ The bridge maintains a persistent BLE connection, avoiding the ~3 second reconnect overhead for each command. It spawns automatically when nodes are deployed and shuts down when they're removed.
126
+
127
+ ## Protocol
128
+
129
+ QuietCool fans use plain JSON over BLE GATT. All communication goes through a single characteristic (`0000ff01`) on service `000000ff`. The protocol requires a `Login` with a `PhoneID` before any commands are accepted.
130
+
131
+ Tested with firmware `IT-BLT-ATTICFAN_V2.6`.
132
+
133
+ ## License
134
+
135
+ MIT
@@ -0,0 +1,144 @@
1
+ <script type="text/html" data-template-name="quietcool-config">
2
+ <div class="form-row">
3
+ <label for="node-config-input-name"><i class="fa fa-tag"></i> Name</label>
4
+ <input type="text" id="node-config-input-name" placeholder="My Fan">
5
+ </div>
6
+ <div class="form-row">
7
+ <label for="node-config-input-address"><i class="fa fa-bluetooth"></i> BLE Address</label>
8
+ <div style="display:flex; gap:4px;">
9
+ <input type="text" id="node-config-input-address" placeholder="XX:XX:XX:XX:XX:XX" style="flex:1;">
10
+ <button type="button" id="quietcool-scan-btn" class="red-ui-button" title="Scan for fans"><i class="fa fa-search"></i> Scan</button>
11
+ </div>
12
+ </div>
13
+ <div id="quietcool-scan-results" style="display:none; margin-bottom:10px;">
14
+ <label>&nbsp;</label>
15
+ <select id="quietcool-fan-select" style="width:70%;"></select>
16
+ </div>
17
+ <div class="form-row">
18
+ <label for="node-config-input-phoneId"><i class="fa fa-key"></i> Phone ID</label>
19
+ <div style="display:flex; gap:4px;">
20
+ <input type="text" id="node-config-input-phoneId" placeholder="16-char hex string">
21
+ <button type="button" id="quietcool-genid-btn" class="red-ui-button" title="Generate random ID"><i class="fa fa-random"></i> New</button>
22
+ </div>
23
+ </div>
24
+ <div class="form-row">
25
+ <label>&nbsp;</label>
26
+ <button type="button" id="quietcool-pair-btn" class="red-ui-button" style="width:auto;">
27
+ <i class="fa fa-link"></i> Pair with Fan
28
+ </button>
29
+ <span id="quietcool-pair-status" style="margin-left:8px;"></span>
30
+ </div>
31
+ <div class="form-row">
32
+ <label>&nbsp;</label>
33
+ <input type="checkbox" id="node-config-input-autoConnect" style="width:auto; vertical-align:top;">
34
+ <label for="node-config-input-autoConnect" style="width:auto;"> Auto-connect on deploy</label>
35
+ </div>
36
+ <div class="form-tips">
37
+ <p><b>Quick Setup:</b></p>
38
+ <ol>
39
+ <li>Click <b>Scan</b> to find your fan and select it</li>
40
+ <li>Click <b>New</b> to generate a Phone ID</li>
41
+ <li>Put the fan in pairing mode: hold the <b>Pair</b> button on the controller for ~5 seconds until the LED blinks</li>
42
+ <li>Click <b>Pair with Fan</b></li>
43
+ <li>Save the config — you're done!</li>
44
+ </ol>
45
+ <p><b>Already paired from the app?</b> You can extract the Phone ID from
46
+ a Bluetooth HCI snoop log instead. See the README for details.</p>
47
+ </div>
48
+ </script>
49
+
50
+ <script type="text/html" data-help-name="quietcool-config">
51
+ <p>Configuration for a QuietCool whole house fan BLE connection.</p>
52
+ <p>Each fan needs its own config node with the BLE MAC address and Phone ID.</p>
53
+ <p>The config node manages a Python bridge process that maintains the BLE
54
+ connection to the fan. It starts automatically when nodes using this config
55
+ are deployed, and stops when they are removed.</p>
56
+ </script>
57
+
58
+ <script type="text/javascript">
59
+ RED.nodes.registerType("quietcool-config", {
60
+ category: "config",
61
+ defaults: {
62
+ name: { value: "" },
63
+ address: { value: "", required: true },
64
+ phoneId: { value: "", required: true },
65
+ autoConnect: { value: true },
66
+ },
67
+ label: function () {
68
+ return this.name || this.address || "QuietCool Fan";
69
+ },
70
+ oneditprepare: function () {
71
+ // Scan button
72
+ $("#quietcool-scan-btn").on("click", function () {
73
+ var btn = $(this);
74
+ btn.prop("disabled", true).find("i").removeClass("fa-search").addClass("fa-spinner fa-spin");
75
+ $.getJSON("quietcool/scan", function (data) {
76
+ btn.prop("disabled", false).find("i").removeClass("fa-spinner fa-spin").addClass("fa-search");
77
+ if (data.fans && data.fans.length > 0) {
78
+ var select = $("#quietcool-fan-select").empty();
79
+ data.fans.forEach(function (f) {
80
+ select.append($("<option>").val(f.address).text(f.name + " (" + f.address + ") RSSI: " + f.rssi));
81
+ });
82
+ $("#quietcool-scan-results").show();
83
+ select.off("change").on("change", function () {
84
+ $("#node-config-input-address").val($(this).val());
85
+ });
86
+ $("#node-config-input-address").val(data.fans[0].address);
87
+ } else {
88
+ RED.notify("No QuietCool fans found. Make sure Bluetooth is on and the fan is powered.", "warning");
89
+ }
90
+ }).fail(function () {
91
+ btn.prop("disabled", false).find("i").removeClass("fa-spinner fa-spin").addClass("fa-search");
92
+ RED.notify("Scan failed. Check that Bluetooth is enabled.", "error");
93
+ });
94
+ });
95
+
96
+ // Generate ID button
97
+ $("#quietcool-genid-btn").on("click", function () {
98
+ $.getJSON("quietcool/generate-id", function (data) {
99
+ if (data.phone_id) {
100
+ $("#node-config-input-phoneId").val(data.phone_id);
101
+ }
102
+ });
103
+ });
104
+
105
+ // Pair button
106
+ $("#quietcool-pair-btn").on("click", function () {
107
+ var address = $("#node-config-input-address").val();
108
+ var phoneId = $("#node-config-input-phoneId").val();
109
+ if (!address || !phoneId) {
110
+ RED.notify("Fill in BLE Address and Phone ID first.", "warning");
111
+ return;
112
+ }
113
+ var btn = $(this);
114
+ var status = $("#quietcool-pair-status");
115
+ btn.prop("disabled", true);
116
+ status.text("Connecting...").css("color", "#666");
117
+
118
+ $.ajax({
119
+ url: "quietcool/pair",
120
+ type: "POST",
121
+ contentType: "application/json",
122
+ data: JSON.stringify({ address: address, phoneId: phoneId }),
123
+ success: function (data) {
124
+ btn.prop("disabled", false);
125
+ if (data.paired) {
126
+ status.text("\u2713 " + (data.message || "Paired!")).css("color", "green");
127
+ if (data.phone_id) {
128
+ $("#node-config-input-phoneId").val(data.phone_id);
129
+ }
130
+ } else {
131
+ status.text("\u2717 " + (data.message || data.error || "Failed")).css("color", "red");
132
+ }
133
+ },
134
+ error: function (xhr) {
135
+ btn.prop("disabled", false);
136
+ var msg = "Pair failed";
137
+ try { msg = JSON.parse(xhr.responseText).error || msg; } catch (e) {}
138
+ status.text("\u2717 " + msg).css("color", "red");
139
+ },
140
+ });
141
+ });
142
+ },
143
+ });
144
+ </script>
@@ -0,0 +1,300 @@
1
+ /**
2
+ * QuietCool BLE config node — manages the Python bridge process lifecycle.
3
+ */
4
+
5
+ const { spawn } = require("child_process");
6
+ const path = require("path");
7
+ const readline = require("readline");
8
+
9
+ // Path to Python bridge (module scope — used by both config node and HTTP admin endpoints)
10
+ const pythonDir = path.join(__dirname, "..", "python");
11
+ const venvPython = path.join(pythonDir, ".venv", "bin", "python3");
12
+ const bridgeScript = path.join(pythonDir, "bridge.py");
13
+
14
+ module.exports = function (RED) {
15
+ function QuietCoolConfigNode(config) {
16
+ RED.nodes.createNode(this, config);
17
+ const node = this;
18
+
19
+ node.address = config.address;
20
+ node.phoneId = config.phoneId;
21
+ node.autoConnect = config.autoConnect !== false;
22
+ node.bridge = null;
23
+ node.bridgeReady = false;
24
+ node.connected = false;
25
+ node.pendingCallbacks = {};
26
+ node.msgCounter = 0;
27
+ node.users = new Set();
28
+
29
+ node.startBridge = function () {
30
+ if (node.bridge) return;
31
+
32
+ node.log(`Starting BLE bridge for ${node.address}`);
33
+
34
+ node.bridge = spawn(venvPython, [bridgeScript], {
35
+ cwd: pythonDir,
36
+ stdio: ["pipe", "pipe", "pipe"],
37
+ env: {
38
+ ...process.env,
39
+ QUIETCOOL_LOG_LEVEL: "WARNING",
40
+ },
41
+ });
42
+
43
+ // Read JSON responses from stdout
44
+ const rl = readline.createInterface({ input: node.bridge.stdout });
45
+ rl.on("line", (line) => {
46
+ try {
47
+ const msg = JSON.parse(line);
48
+
49
+ if (msg.type === "status") {
50
+ node.connected = msg.connected;
51
+ node.bridgeReady = true;
52
+ node.updateStatus();
53
+
54
+ if (
55
+ msg.detail === "bridge_ready" &&
56
+ node.autoConnect &&
57
+ node.address &&
58
+ node.phoneId
59
+ ) {
60
+ node.sendBridgeCommand("connect", {
61
+ address: node.address,
62
+ phone_id: node.phoneId,
63
+ });
64
+ }
65
+ return;
66
+ }
67
+
68
+ // Route response to waiting callback
69
+ const cb = node.pendingCallbacks[msg.id];
70
+ if (cb) {
71
+ delete node.pendingCallbacks[msg.id];
72
+ cb(msg);
73
+ }
74
+ } catch (e) {
75
+ node.warn(`Bridge parse error: ${e.message} - ${line}`);
76
+ }
77
+ });
78
+
79
+ // Log bridge stderr
80
+ const stderrRl = readline.createInterface({
81
+ input: node.bridge.stderr,
82
+ });
83
+ stderrRl.on("line", (line) => {
84
+ node.trace(`Bridge: ${line}`);
85
+ });
86
+
87
+ node.bridge.on("exit", (code, signal) => {
88
+ node.warn(`Bridge exited: code=${code} signal=${signal}`);
89
+ node.bridge = null;
90
+ node.bridgeReady = false;
91
+ node.connected = false;
92
+ node.updateStatus();
93
+
94
+ // Reject all pending callbacks
95
+ for (const id of Object.keys(node.pendingCallbacks)) {
96
+ node.pendingCallbacks[id]({
97
+ ok: false,
98
+ error: "Bridge process exited",
99
+ });
100
+ }
101
+ node.pendingCallbacks = {};
102
+
103
+ // Auto-restart after 5s if we still have users
104
+ if (node.users.size > 0) {
105
+ setTimeout(() => node.startBridge(), 5000);
106
+ }
107
+ });
108
+
109
+ node.bridge.on("error", (err) => {
110
+ node.error(`Bridge spawn error: ${err.message}`);
111
+ });
112
+ };
113
+
114
+ node.stopBridge = function () {
115
+ if (node.bridge) {
116
+ node.log("Stopping BLE bridge");
117
+ node.bridge.kill("SIGTERM");
118
+ node.bridge = null;
119
+ node.bridgeReady = false;
120
+ node.connected = false;
121
+ }
122
+ };
123
+
124
+ node.sendBridgeCommand = function (cmd, args, callback) {
125
+ if (!node.bridge || !node.bridgeReady) {
126
+ if (callback) {
127
+ callback({ ok: false, error: "Bridge not ready" });
128
+ }
129
+ return;
130
+ }
131
+
132
+ const id = `msg_${++node.msgCounter}`;
133
+ const msg = JSON.stringify({ id, cmd, args: args || {} }) + "\n";
134
+
135
+ if (callback) {
136
+ node.pendingCallbacks[id] = callback;
137
+ // Timeout after 15s
138
+ setTimeout(() => {
139
+ if (node.pendingCallbacks[id]) {
140
+ delete node.pendingCallbacks[id];
141
+ callback({ ok: false, error: "Command timeout" });
142
+ }
143
+ }, 15000);
144
+ }
145
+
146
+ node.bridge.stdin.write(msg);
147
+ };
148
+
149
+ node.registerUser = function (userNode) {
150
+ node.users.add(userNode.id);
151
+ if (!node.bridge) {
152
+ node.startBridge();
153
+ }
154
+ };
155
+
156
+ node.deregisterUser = function (userNode) {
157
+ node.users.delete(userNode.id);
158
+ if (node.users.size === 0) {
159
+ node.stopBridge();
160
+ }
161
+ };
162
+
163
+ node.updateStatus = function () {
164
+ for (const userId of node.users) {
165
+ const userNode = RED.nodes.getNode(userId);
166
+ if (userNode && userNode.updateNodeStatus) {
167
+ userNode.updateNodeStatus(node.connected);
168
+ }
169
+ }
170
+ };
171
+
172
+ node.on("close", function (done) {
173
+ node.stopBridge();
174
+ done();
175
+ });
176
+ }
177
+
178
+ RED.nodes.registerType("quietcool-config", QuietCoolConfigNode);
179
+
180
+ // ================================================================
181
+ // HTTP Admin Endpoints for editor UI (scan, pair, generate ID)
182
+ // ================================================================
183
+
184
+ // Scan for QuietCool fans
185
+ RED.httpAdmin.get(
186
+ "/quietcool/scan",
187
+ RED.auth.needsPermission("quietcool-config.write"),
188
+ function (req, res) {
189
+ const proc = spawn(venvPython, [bridgeScript], {
190
+ cwd: pythonDir,
191
+ stdio: ["pipe", "pipe", "pipe"],
192
+ });
193
+
194
+ let responded = false;
195
+ const rl = readline.createInterface({ input: proc.stdout });
196
+
197
+ rl.on("line", (line) => {
198
+ try {
199
+ const msg = JSON.parse(line);
200
+ if (msg.type === "status" && msg.detail === "bridge_ready") {
201
+ proc.stdin.write(
202
+ JSON.stringify({ id: "scan", cmd: "scan", args: { timeout: 8 } }) + "\n"
203
+ );
204
+ } else if (msg.id === "scan" && !responded) {
205
+ responded = true;
206
+ res.json(msg.ok ? msg.data : { error: msg.error });
207
+ proc.kill("SIGTERM");
208
+ }
209
+ } catch (e) {
210
+ /* ignore parse errors */
211
+ }
212
+ });
213
+
214
+ proc.on("error", (err) => {
215
+ if (!responded) {
216
+ responded = true;
217
+ res.status(500).json({ error: err.message });
218
+ }
219
+ });
220
+
221
+ setTimeout(() => {
222
+ if (!responded) {
223
+ responded = true;
224
+ res.status(504).json({ error: "Scan timeout" });
225
+ proc.kill("SIGTERM");
226
+ }
227
+ }, 15000);
228
+ }
229
+ );
230
+
231
+ // Generate a new Phone ID
232
+ RED.httpAdmin.get(
233
+ "/quietcool/generate-id",
234
+ RED.auth.needsPermission("quietcool-config.write"),
235
+ function (req, res) {
236
+ const crypto = require("crypto");
237
+ const id = crypto.randomBytes(8).toString("hex");
238
+ res.json({ phone_id: id });
239
+ }
240
+ );
241
+
242
+ // Pair with a fan
243
+ RED.httpAdmin.post(
244
+ "/quietcool/pair",
245
+ RED.auth.needsPermission("quietcool-config.write"),
246
+ function (req, res) {
247
+ const address = req.body.address;
248
+ const phoneId = req.body.phoneId;
249
+
250
+ if (!address || !phoneId) {
251
+ res.status(400).json({ error: "address and phoneId required" });
252
+ return;
253
+ }
254
+
255
+ const proc = spawn(venvPython, [bridgeScript], {
256
+ cwd: pythonDir,
257
+ stdio: ["pipe", "pipe", "pipe"],
258
+ });
259
+
260
+ let responded = false;
261
+ const rl = readline.createInterface({ input: proc.stdout });
262
+
263
+ rl.on("line", (line) => {
264
+ try {
265
+ const msg = JSON.parse(line);
266
+ if (msg.type === "status" && msg.detail === "bridge_ready") {
267
+ proc.stdin.write(
268
+ JSON.stringify({
269
+ id: "pair",
270
+ cmd: "pair",
271
+ args: { address, phone_id: phoneId },
272
+ }) + "\n"
273
+ );
274
+ } else if (msg.id === "pair" && !responded) {
275
+ responded = true;
276
+ res.json(msg.ok ? msg.data : { error: msg.error });
277
+ proc.kill("SIGTERM");
278
+ }
279
+ } catch (e) {
280
+ /* ignore */
281
+ }
282
+ });
283
+
284
+ proc.on("error", (err) => {
285
+ if (!responded) {
286
+ responded = true;
287
+ res.status(500).json({ error: err.message });
288
+ }
289
+ });
290
+
291
+ setTimeout(() => {
292
+ if (!responded) {
293
+ responded = true;
294
+ res.status(504).json({ error: "Pair timeout" });
295
+ proc.kill("SIGTERM");
296
+ }
297
+ }, 30000);
298
+ }
299
+ );
300
+ };