@avantics/node-red-home-automation 0.0.0-stage → 2.3.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 +21 -0
- package/README.md +292 -2
- package/contract-examples/README.md +28 -0
- package/contract-examples/_shared.mjs +11 -0
- package/contract-examples/motion-lighting.mjs +42 -0
- package/contract-examples/occupancy-fusion.mjs +35 -0
- package/contract-examples/room-lighting.mjs +58 -0
- package/contract-examples/run.mjs +16 -0
- package/contract-examples/source-readiness.mjs +64 -0
- package/contract-examples/switch-gestures.mjs +34 -0
- package/contract-examples/theatre-activity.mjs +62 -0
- package/contract-examples/volume-ownership.mjs +59 -0
- package/dist/lib/activity-lighting-controller.d.ts +129 -0
- package/dist/lib/activity-lighting-controller.js +372 -0
- package/dist/lib/avr-controller.d.ts +169 -0
- package/dist/lib/avr-controller.js +948 -0
- package/dist/lib/backlighting-controller.d.ts +108 -0
- package/dist/lib/backlighting-controller.js +269 -0
- package/dist/lib/bounded-trace.d.ts +30 -0
- package/dist/lib/bounded-trace.js +36 -0
- package/dist/lib/circadian-refresh.d.ts +7 -0
- package/dist/lib/circadian-refresh.js +11 -0
- package/dist/lib/circadian-schedule.d.ts +11 -0
- package/dist/lib/circadian-schedule.js +63 -0
- package/dist/lib/circadian.d.ts +6 -0
- package/dist/lib/circadian.js +15 -0
- package/dist/lib/cover-controller.d.ts +130 -0
- package/dist/lib/cover-controller.js +336 -0
- package/dist/lib/display-mode-controller.d.ts +148 -0
- package/dist/lib/display-mode-controller.js +587 -0
- package/dist/lib/display-transport.d.ts +41 -0
- package/dist/lib/display-transport.js +177 -0
- package/dist/lib/light-styling.d.ts +11 -0
- package/dist/lib/light-styling.js +15 -0
- package/dist/lib/motion-policy.d.ts +72 -0
- package/dist/lib/motion-policy.js +251 -0
- package/dist/lib/observation-time.d.ts +10 -0
- package/dist/lib/observation-time.js +40 -0
- package/dist/lib/occupancy-fusion.d.ts +53 -0
- package/dist/lib/occupancy-fusion.js +131 -0
- package/dist/lib/room-controller.d.ts +113 -0
- package/dist/lib/room-controller.js +431 -0
- package/dist/lib/source-capability-controller.d.ts +144 -0
- package/dist/lib/source-capability-controller.js +494 -0
- package/dist/lib/switch-policy.d.ts +53 -0
- package/dist/lib/switch-policy.js +110 -0
- package/dist/lib/theatre-activity-controller.d.ts +165 -0
- package/dist/lib/theatre-activity-controller.js +846 -0
- package/dist/lib/theatre-slo.d.ts +49 -0
- package/dist/lib/theatre-slo.js +156 -0
- package/dist/lib/theatre-state-contract.d.ts +77 -0
- package/dist/lib/theatre-state-contract.js +391 -0
- package/dist/lib/wall-input.d.ts +62 -0
- package/dist/lib/wall-input.js +198 -0
- package/dist/nodes/activity-lighting-controller.d.ts +3 -0
- package/dist/nodes/activity-lighting-controller.html +38 -0
- package/dist/nodes/activity-lighting-controller.js +116 -0
- package/dist/nodes/avr-controller.d.ts +3 -0
- package/dist/nodes/avr-controller.html +47 -0
- package/dist/nodes/avr-controller.js +130 -0
- package/dist/nodes/backlighting-controller.d.ts +3 -0
- package/dist/nodes/backlighting-controller.html +31 -0
- package/dist/nodes/backlighting-controller.js +103 -0
- package/dist/nodes/display-mode-controller.d.ts +3 -0
- package/dist/nodes/display-mode-controller.html +66 -0
- package/dist/nodes/display-mode-controller.js +132 -0
- package/dist/nodes/motion-policy.d.ts +3 -0
- package/dist/nodes/motion-policy.html +35 -0
- package/dist/nodes/motion-policy.js +194 -0
- package/dist/nodes/occupancy-fusion.d.ts +3 -0
- package/dist/nodes/occupancy-fusion.html +32 -0
- package/dist/nodes/occupancy-fusion.js +53 -0
- package/dist/nodes/room-controller.d.ts +3 -0
- package/dist/nodes/room-controller.html +82 -0
- package/dist/nodes/room-controller.js +123 -0
- package/dist/nodes/source-capability-controller.d.ts +3 -0
- package/dist/nodes/source-capability-controller.html +46 -0
- package/dist/nodes/source-capability-controller.js +114 -0
- package/dist/nodes/switch-policy.d.ts +3 -0
- package/dist/nodes/switch-policy.html +29 -0
- package/dist/nodes/switch-policy.js +129 -0
- package/dist/nodes/theatre-activity-controller.d.ts +3 -0
- package/dist/nodes/theatre-activity-controller.html +32 -0
- package/dist/nodes/theatre-activity-controller.js +105 -0
- package/dist/nodes/theatre-state-contract.d.ts +3 -0
- package/dist/nodes/theatre-state-contract.html +41 -0
- package/dist/nodes/theatre-state-contract.js +83 -0
- package/examples/Home theatre activity.json +477 -0
- package/examples/Home theatre equipment.json +329 -0
- package/examples/Room lighting.json +212 -0
- package/package.json +62 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Avantics
|
|
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
CHANGED
|
@@ -1,3 +1,293 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Node-RED Home Automation
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://github.com/avantics-hq/node-red-home-automation/actions/workflows/ci.yml)
|
|
4
|
+
[](https://avantics-hq.github.io/node-red-home-automation/)
|
|
5
|
+
[](https://ko-fi.com/avantics)
|
|
6
|
+
|
|
7
|
+
State-aware Node-RED controllers for reliable lighting and home-theatre automation.
|
|
8
|
+
|
|
9
|
+
**[View the smart-home programme and completed project records →](https://avantics-hq.github.io/node-red-home-automation/)**
|
|
10
|
+
|
|
11
|
+
Most home automations translate an event directly into a command. This package adds a deterministic control layer between them: it tracks desired and observed state, preserves manual control, confirms outcomes, retries bounded failures, and rejects stale work.
|
|
12
|
+
|
|
13
|
+
## What it solves
|
|
14
|
+
|
|
15
|
+
- **Occupancy without flicker:** combine PIR motion with optional presence sensing before changing room state.
|
|
16
|
+
- **Circadian lighting with manual ownership:** calculate circadian brightness and colour temperature without immediately overwriting wall-switch changes.
|
|
17
|
+
- **Honest wall inputs:** preserve restart baselines while exposing unconfirmed, stale and unavailable physical-input evidence.
|
|
18
|
+
- **Reliable AV transitions:** coordinate AVR inputs, TV power, projector warm-up and cooldown, and motorised screens from observed state rather than blind toggles.
|
|
19
|
+
- **Honest source readiness:** model controllable, externally controlled, and unavailable playback sources without claiming success from an input selection alone.
|
|
20
|
+
- **Owned theatre atmosphere:** apply activity lighting only to active, non-manual room lights and confirm TV backlighting independently.
|
|
21
|
+
- **Bounded receiver volume:** coordinate absolute and relative requests through one transport owner while preserving later physical adjustments.
|
|
22
|
+
- **Recoverable automation:** adopt one fresh, uniquely matching physical activity after lost state; otherwise coordinate a safe Off request.
|
|
23
|
+
|
|
24
|
+

|
|
25
|
+
|
|
26
|
+
Each controller keeps the desired and the observed state, sends semantic
|
|
27
|
+
commands, and reports done only when fresh observations confirm them.
|
|
28
|
+
|
|
29
|
+
## Included nodes
|
|
30
|
+
|
|
31
|
+
The nodes come in three groups. Each group has an importable example flow
|
|
32
|
+
(**Import → Examples** in the Node-RED editor).
|
|
33
|
+
|
|
34
|
+
### Room lighting
|
|
35
|
+
|
|
36
|
+
Motion, presence, illuminance and wall switches drive one room's lights, with
|
|
37
|
+
circadian styling and manual ownership. Wire occupancy fusion into the motion
|
|
38
|
+
policy, and the motion and switch policies into the room controller. Circadian
|
|
39
|
+
levels follow the room controller's wake and bed times (default 07:00 and
|
|
40
|
+
23:00): they rise over the hour after waking, stay bright and cool through the
|
|
41
|
+
day, and wind down to the sleep settings over the three hours before bed. Times
|
|
42
|
+
are the Node-RED host's local time.
|
|
43
|
+
Example: [`Room lighting.json`](https://github.com/avantics-hq/node-red-home-automation/blob/main/examples/Room%20lighting.json).
|
|
44
|
+
|
|
45
|
+
| Node | Purpose |
|
|
46
|
+
|---|---|
|
|
47
|
+
| `home-occupancy-fusion` | Combines PIR motion with optional presence sensing. |
|
|
48
|
+
| `home-motion-lighting-policy` | Converts occupancy and illuminance into lighting intent with observable wall-input baselines. |
|
|
49
|
+
| `home-switch-lighting-policy` | Converts raw edges or pre-classified button actions into lighting intent while exposing input freshness and availability. |
|
|
50
|
+
| `home-room-lighting-controller` | Owns the room's light commands, adaptive styling, manual and held lights, and lighting state. |
|
|
51
|
+
|
|
52
|
+
### Home theatre equipment
|
|
53
|
+
|
|
54
|
+
Each controller owns one kind of device. It accepts a generation-scoped intent,
|
|
55
|
+
sends commands for your device transport on output 1, and reports target state
|
|
56
|
+
on output 2. Example: [`Home theatre equipment.json`](https://github.com/avantics-hq/node-red-home-automation/blob/main/examples/Home%20theatre%20equipment.json).
|
|
57
|
+
|
|
58
|
+
| Node | Purpose |
|
|
59
|
+
|---|---|
|
|
60
|
+
| `home-avr-controller` | Owns AVR power, zones, source selection and bounded absolute or relative volume from observed state. |
|
|
61
|
+
| `home-display-mode-controller` | Coordinates TV, projector, and screen transitions. |
|
|
62
|
+
| `home-source-capability-controller` | Gates source readiness on fresh availability, active-device, application or playback evidence. |
|
|
63
|
+
| `home-backlighting-controller` | Owns TV-backlight power with observation-gated confirmation. |
|
|
64
|
+
|
|
65
|
+
### Theatre activities
|
|
66
|
+
|
|
67
|
+
One request, such as "tv" or "off", drives every target. The state contract
|
|
68
|
+
turns MQTT requests into activity intents and publishes retained state; the
|
|
69
|
+
activity controller plans each target and sends `msg.theatreCommand` messages.
|
|
70
|
+
Your flow routes each command by its `target` (`avr`, `display`, `source`,
|
|
71
|
+
`lighting`, `backlighting`) to the matching controller's input
|
|
72
|
+
(`msg.avr`, `msg.display`, `msg.sourceCapability`, `msg.activityLighting`,
|
|
73
|
+
`msg.backlighting`); the example does this with one switch node and a change
|
|
74
|
+
node per target. Every controller reports its progress on output 2 as
|
|
75
|
+
`msg.theatre = {kind: "target_state", target, requestId, generation, status,
|
|
76
|
+
observedAt}`, alongside its own events: wire each controller's output 2 straight
|
|
77
|
+
back to the activity controller. The activity controller refreshes settled
|
|
78
|
+
targets, and each controller answers a refresh from fresh readings. Activity
|
|
79
|
+
lighting feeds the room lighting controller.
|
|
80
|
+
Example: [`Home theatre activity.json`](https://github.com/avantics-hq/node-red-home-automation/blob/main/examples/Home%20theatre%20activity.json).
|
|
81
|
+
|
|
82
|
+
| Node | Purpose |
|
|
83
|
+
|---|---|
|
|
84
|
+
| `home-theatre-state-contract` | Bridges non-retained activity requests to retained MQTT truth and HA discovery. |
|
|
85
|
+
| `home-theatre-activity-controller` | Owns latest-request-wins activity state and aggregates target evidence. |
|
|
86
|
+
| `home-activity-lighting-controller` | Applies activity styles to room lights and holds them while the activity is active. |
|
|
87
|
+
|
|
88
|
+
Pure TypeScript policy modules live in `lib/`; thin Node-RED runtime and editor adapters live in `nodes/`.
|
|
89
|
+
|
|
90
|
+
Importable example flows for the Node-RED editor live in [`examples/`](https://github.com/avantics-hq/node-red-home-automation/tree/main/examples) (**Import → Examples**). Executable, deployment-neutral contract examples live in [`contract-examples/`](https://github.com/avantics-hq/node-red-home-automation/tree/main/contract-examples) and exercise the same reducers as the runtime adapters.
|
|
91
|
+
|
|
92
|
+
## Requirements
|
|
93
|
+
|
|
94
|
+
- Node.js 22.9 or newer
|
|
95
|
+
- Node-RED 5.0.x
|
|
96
|
+
|
|
97
|
+
## Install
|
|
98
|
+
|
|
99
|
+
In Node-RED, open **Manage palette → Install** and search for
|
|
100
|
+
`@avantics/node-red-home-automation`. Or install it from your Node-RED user
|
|
101
|
+
directory (usually `~/.node-red`) and restart Node-RED:
|
|
102
|
+
|
|
103
|
+
```sh
|
|
104
|
+
npm install @avantics/node-red-home-automation
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
Each [GitHub Release](https://github.com/avantics-hq/node-red-home-automation/releases)
|
|
108
|
+
also attaches the package tarball and its SHA-256 checksum. Install a downloaded
|
|
109
|
+
tarball through **Manage palette → Install → Upload module**, or with:
|
|
110
|
+
|
|
111
|
+
```sh
|
|
112
|
+
npm install /path/to/avantics-node-red-home-automation-2.3.0.tgz
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
Add the installed nodes from the **home automation** section of the Node-RED palette, then configure the room, device hosts, MQTT topics, source mappings, and timing limits for your deployment.
|
|
116
|
+
|
|
117
|
+
The whole-theatre MQTT contract uses non-retained requests at
|
|
118
|
+
`home/<area>/theatre/request`; every request supplies a unique `requestId` and an
|
|
119
|
+
explicit configured activity. Controller output is retained under `desired`,
|
|
120
|
+
`current`, `phase`, `failure`, `availability`, `metrics`, `slo`, and
|
|
121
|
+
`health/<target>`. Metrics expose transition latency and outcome counts plus
|
|
122
|
+
per-target retry, fallback, confirmation-latency, failure, manual-override, and
|
|
123
|
+
availability evidence. The SLO evaluator keeps a bounded outcome window,
|
|
124
|
+
publishes success rate plus p50/p95 transition latency, and exposes an
|
|
125
|
+
actionable Home Assistant problem sensor. Defaults are a 60-second transition
|
|
126
|
+
budget, 95% success objective, 20 outcomes and five samples before normal
|
|
127
|
+
evaluation; active timeouts and explicit failures breach immediately. Expected
|
|
128
|
+
`waiting_for_source` and physical manual ownership do not create false
|
|
129
|
+
operational failures. Retained request messages are deliberately inert.
|
|
130
|
+
|
|
131
|
+
MQTT state topics may be retained so controllers can rebuild observed state
|
|
132
|
+
after a restart. Command and request topics must not be retained. Raw wall-input
|
|
133
|
+
reducers preserve a persisted baseline but report it as unconfirmed until this
|
|
134
|
+
runtime receives an observation. Retained observations and messages marked
|
|
135
|
+
`msg.baseline = true` establish an inert baseline; duplicate observations
|
|
136
|
+
refresh freshness without replaying commands. When no baseline exists, a live
|
|
137
|
+
non-retained state change remains an actionable edge for event-only inputs.
|
|
138
|
+
Send source `switch-availability` to invalidate a disconnected input baseline;
|
|
139
|
+
the first recovered observation then re-baselines without a gesture. An
|
|
140
|
+
observation timestamp may be supplied as `msg.observedAt` or
|
|
141
|
+
`msg.payload.observedAt`; otherwise only live, non-retained observations receive
|
|
142
|
+
the current timestamp. Across all controllers, an observation time more than one
|
|
143
|
+
second ahead of Node-RED's clock is treated as observed on receipt and traced as
|
|
144
|
+
`clock-skew`, so one device with a wrong clock cannot hold back later reports.
|
|
145
|
+
|
|
146
|
+
Unsolicited display, AVR, source, and backlighting changes latch manual
|
|
147
|
+
ownership instead of issuing corrective commands. A newer explicit activity
|
|
148
|
+
request resumes automation. Recovery adopts physical state only when fresh
|
|
149
|
+
display, AVR, source, and backlighting evidence matches exactly one configured
|
|
150
|
+
profile; missing or ambiguous startup evidence invokes the configured Off
|
|
151
|
+
activity.
|
|
152
|
+
|
|
153
|
+
## Room light control
|
|
154
|
+
|
|
155
|
+
The room lighting controller gives each target one control state:
|
|
156
|
+
|
|
157
|
+
- `in-control`: circadian lighting may style and refresh the light (the default).
|
|
158
|
+
- `overridden`: another owner, such as an activity or a scene, holds the light;
|
|
159
|
+
refresh and circadian changes leave it alone until it is released.
|
|
160
|
+
- `manual`: a person changed the light.
|
|
161
|
+
|
|
162
|
+
Add `owner` to an intent to override the targets it switches on. With
|
|
163
|
+
`preserveManual: true`, manual lights are skipped:
|
|
164
|
+
|
|
165
|
+
```js
|
|
166
|
+
msg.lighting = {
|
|
167
|
+
kind: "intent",
|
|
168
|
+
targets: ["wall", "lamp"],
|
|
169
|
+
state: "ON",
|
|
170
|
+
source: "activity",
|
|
171
|
+
owner: "theatre",
|
|
172
|
+
values: {brightness: 64, color_temp: 400},
|
|
173
|
+
preserveManual: true
|
|
174
|
+
};
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
`home-activity-lighting-controller` does this itself: while a profile is active
|
|
178
|
+
it overrides that profile's lights under its configured owner (default
|
|
179
|
+
`activity`), and when a new profile drops a light, including an off profile
|
|
180
|
+
with no targets, it releases it. Give each activity-lighting node that feeds the
|
|
181
|
+
same room controller its own Owner; with a shared name, one node's release
|
|
182
|
+
hands back lights the other still holds. A flow that sets its own `owner` must
|
|
183
|
+
set it on every lighting message from the node, releases included, or leave it
|
|
184
|
+
to the node's Owner setting.
|
|
185
|
+
|
|
186
|
+
Release overridden targets (default: all; with `owner`, only that owner's
|
|
187
|
+
lights). Released lights that are on are restyled as a refresh would:
|
|
188
|
+
|
|
189
|
+
```js
|
|
190
|
+
msg.lighting = {kind: "release", targets: ["wall", "lamp"], owner: "theatre"};
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
A held (`overridden`) light changes only for an intent with an `owner` (its own,
|
|
194
|
+
or a newer owner who takes it over), a person (`manual: true`, an external
|
|
195
|
+
command on the command topic, or a change seen at the light), or a release.
|
|
196
|
+
Other intents, such as motion ON or OFF and owner-less values, leave it alone
|
|
197
|
+
and are traced as `ignored held-by`; their other targets are still handled.
|
|
198
|
+
The package's wall-switch policies mark presses with `person: true`. Owner-less
|
|
199
|
+
intents, with or without values, reach a held light only with `manual: true` or
|
|
200
|
+
`person: true`; a flow acting for a person, such as a dashboard, sets one.
|
|
201
|
+
Switching a light off by its owner or a person returns it to `in-control`. On startup
|
|
202
|
+
and whenever a target's state changes, the controller sends a retained message
|
|
203
|
+
on its command output:
|
|
204
|
+
|
|
205
|
+
```js
|
|
206
|
+
{topic: "home/<area>/lighting/<target>/control", payload: {state: "overridden", owner: "theatre"}, retain: true}
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
Flows that filter that output by topic must pass the `/control` topics through.
|
|
210
|
+
|
|
211
|
+
## Receiver volume ownership
|
|
212
|
+
|
|
213
|
+
Send semantic requests to the AVR controller rather than publishing protocol
|
|
214
|
+
commands directly:
|
|
215
|
+
|
|
216
|
+
```js
|
|
217
|
+
msg.avr = {
|
|
218
|
+
kind: "volume",
|
|
219
|
+
requestId: "volume-control-1",
|
|
220
|
+
mode: "relative",
|
|
221
|
+
value: 1
|
|
222
|
+
};
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
`mode` may be `absolute` or `relative`. The controller resolves relative input
|
|
226
|
+
from fresh observed volume, accumulates rapid requests against the pending
|
|
227
|
+
target, applies configured half-step bounds, retries through the existing
|
|
228
|
+
bounded transport path, and confirms completion from fresh receiver evidence.
|
|
229
|
+
Once confirmed, it releases the volume target; a later physical or external
|
|
230
|
+
adjustment becomes the new baseline instead of being corrected. A request still pending when Node-RED restarts is reported as
|
|
231
|
+
`volume_failed` with reason `restart`, not replayed.
|
|
232
|
+
|
|
233
|
+
## Contract examples
|
|
234
|
+
|
|
235
|
+
Build and run all seven public contracts:
|
|
236
|
+
|
|
237
|
+
```sh
|
|
238
|
+
npm run examples
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
| Example | Boundary demonstrated |
|
|
242
|
+
|---|---|
|
|
243
|
+
| [`occupancy-fusion.mjs`](https://github.com/avantics-hq/node-red-home-automation/blob/main/contract-examples/occupancy-fusion.mjs) | Motion establishes occupancy; optional presence can hold it. |
|
|
244
|
+
| [`motion-lighting.mjs`](https://github.com/avantics-hq/node-red-home-automation/blob/main/contract-examples/motion-lighting.mjs) | Occupancy and illuminance produce semantic lighting intent. |
|
|
245
|
+
| [`switch-gestures.mjs`](https://github.com/avantics-hq/node-red-home-automation/blob/main/contract-examples/switch-gestures.mjs) | Wall actions produce toggle or explicit manual-brightness intent. |
|
|
246
|
+
| [`room-lighting.mjs`](https://github.com/avantics-hq/node-red-home-automation/blob/main/contract-examples/room-lighting.mjs) | The room owner converts semantic intent into a transport command. |
|
|
247
|
+
| [`source-readiness.mjs`](https://github.com/avantics-hq/node-red-home-automation/blob/main/contract-examples/source-readiness.mjs) | Fresh application evidence confirms source readiness. |
|
|
248
|
+
| [`volume-ownership.mjs`](https://github.com/avantics-hq/node-red-home-automation/blob/main/contract-examples/volume-ownership.mjs) | Bounded relative requests accumulate and release ownership after fresh confirmation. |
|
|
249
|
+
| [`theatre-activity.mjs`](https://github.com/avantics-hq/node-red-home-automation/blob/main/contract-examples/theatre-activity.mjs) | Coordinated activity waits for fresh target-owner evidence. |
|
|
250
|
+
|
|
251
|
+
Each example uses synthetic identifiers and configuration, asserts its central invariant, and prints the complete input/output contract as JSON.
|
|
252
|
+
|
|
253
|
+
## Configuration and security
|
|
254
|
+
|
|
255
|
+
The repository contains no home-network addresses, deployment coordinates, or live credentials. Device hosts and room coordinates are required configuration where applicable.
|
|
256
|
+
|
|
257
|
+
The display controller stores its Bravia PSK as a Node-RED credential. Keep credentials in Node-RED's encrypted credential store rather than flow fields or repository files.
|
|
258
|
+
|
|
259
|
+
Report suspected vulnerabilities privately using the process in
|
|
260
|
+
[`SECURITY.md`](https://github.com/avantics-hq/node-red-home-automation/blob/main/SECURITY.md), not through a public issue.
|
|
261
|
+
|
|
262
|
+
## Development
|
|
263
|
+
|
|
264
|
+
```sh
|
|
265
|
+
npm ci
|
|
266
|
+
npm test
|
|
267
|
+
npm run examples
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
The strict TypeScript build, compiled test suite, executable contract examples, dependency audit, public-content scan, and secret scan run in CI. See [CONTRIBUTING.md](https://github.com/avantics-hq/node-red-home-automation/blob/main/CONTRIBUTING.md) for local checks and the optional pre-commit setup.
|
|
271
|
+
|
|
272
|
+
## Contributing
|
|
273
|
+
|
|
274
|
+
Contributions are welcome: see [CONTRIBUTING.md](https://github.com/avantics-hq/node-red-home-automation/blob/main/CONTRIBUTING.md) and the
|
|
275
|
+
[Code of Conduct](https://github.com/avantics-hq/node-red-home-automation/blob/main/CODE_OF_CONDUCT.md).
|
|
276
|
+
|
|
277
|
+
## Inspiration and thanks
|
|
278
|
+
|
|
279
|
+
- [Node-RED](https://nodered.org): the platform these nodes run on.
|
|
280
|
+
- [Home Assistant](https://www.home-assistant.io): the nodes publish state and
|
|
281
|
+
discovery to it as a read-only view.
|
|
282
|
+
- [Zigbee2MQTT](https://www.zigbee2mqtt.io): the light transport the room
|
|
283
|
+
controller and the examples are designed around.
|
|
284
|
+
- [Adaptive Lighting](https://github.com/basnijholt/adaptive-lighting) by
|
|
285
|
+
basnijholt: the inspiration for circadian lighting in Node-RED.
|
|
286
|
+
|
|
287
|
+
BRAVIA is a trademark of Sony Group Corporation. Node-RED, Home Assistant,
|
|
288
|
+
Zigbee2MQTT and the other names here are trademarks of their respective owners.
|
|
289
|
+
This project is not affiliated with or endorsed by any of them.
|
|
290
|
+
|
|
291
|
+
## License
|
|
292
|
+
|
|
293
|
+
[MIT](https://github.com/avantics-hq/node-red-home-automation/blob/main/LICENSE)
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# Contract examples
|
|
2
|
+
|
|
3
|
+
These examples execute the package's pure reducers with deterministic, deployment-neutral inputs. They demonstrate the boundary between sensor or user events, semantic policy output, target-owner commands, and observed completion.
|
|
4
|
+
|
|
5
|
+
Run every example after compiling the package:
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm run examples
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Run one example and inspect its complete JSON contract:
|
|
12
|
+
|
|
13
|
+
```sh
|
|
14
|
+
npm run build
|
|
15
|
+
node contract-examples/source-readiness.mjs
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
| Example | Contract demonstrated |
|
|
19
|
+
|---|---|
|
|
20
|
+
| [`occupancy-fusion.mjs`](occupancy-fusion.mjs) | Motion establishes occupancy; optional presence can hold it. |
|
|
21
|
+
| [`motion-lighting.mjs`](motion-lighting.mjs) | Occupancy and illuminance produce semantic lighting intent. |
|
|
22
|
+
| [`switch-gestures.mjs`](switch-gestures.mjs) | A double action requests explicit manual full brightness. |
|
|
23
|
+
| [`room-lighting.mjs`](room-lighting.mjs) | One room controller converts intent into the transport command. |
|
|
24
|
+
| [`source-readiness.mjs`](source-readiness.mjs) | Fresh application evidence, not command success, confirms readiness. |
|
|
25
|
+
| [`volume-ownership.mjs`](volume-ownership.mjs) | Relative requests accumulate, bounds apply, and fresh evidence releases ownership. |
|
|
26
|
+
| [`theatre-activity.mjs`](theatre-activity.mjs) | A coordinated activity waits for fresh target-owner evidence. |
|
|
27
|
+
|
|
28
|
+
Each example asserts its central invariant before returning the configuration, inputs, commands or effects, outputs, and invariant as JSON. Identifiers, topics, timestamps, coordinates, and profiles are synthetic.
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import {pathToFileURL} from "node:url";
|
|
3
|
+
|
|
4
|
+
export {assert};
|
|
5
|
+
|
|
6
|
+
export function runWhenMain(moduleUrl, name, run) {
|
|
7
|
+
const entryUrl = process.argv[1] ? pathToFileURL(process.argv[1]).href : "";
|
|
8
|
+
if (moduleUrl === entryUrl) {
|
|
9
|
+
console.log(JSON.stringify({name, ...run()}, null, 2));
|
|
10
|
+
}
|
|
11
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import {transitionMotion} from "../dist/lib/motion-policy.js";
|
|
2
|
+
import {assert, runWhenMain} from "./_shared.mjs";
|
|
3
|
+
|
|
4
|
+
export const name = "motion-lighting";
|
|
5
|
+
|
|
6
|
+
export function run() {
|
|
7
|
+
const configuration = {
|
|
8
|
+
onTargets: ["ceiling", "worktop"],
|
|
9
|
+
offTargets: ["ceiling", "worktop", "accent"],
|
|
10
|
+
defaultIlluminanceCutoff: 120,
|
|
11
|
+
defaultDelaySeconds: 45,
|
|
12
|
+
switchWindowMs: 500,
|
|
13
|
+
switchFreshnessMs: 60_000
|
|
14
|
+
};
|
|
15
|
+
const inputs = [
|
|
16
|
+
{source: "illuminance", payload: 35},
|
|
17
|
+
{source: "occupancy", payload: {occupied: true, trigger: "motion"}}
|
|
18
|
+
];
|
|
19
|
+
let result = transitionMotion(undefined, inputs[0], configuration, true, 1000);
|
|
20
|
+
result = transitionMotion(result.state, inputs[1], configuration, result.globalEnabled, 2000);
|
|
21
|
+
const outputs = result.effects
|
|
22
|
+
.filter(({kind}) => kind === "send")
|
|
23
|
+
.map(({message}) => message);
|
|
24
|
+
|
|
25
|
+
assert.deepEqual(outputs, [{
|
|
26
|
+
lighting: {
|
|
27
|
+
kind: "intent",
|
|
28
|
+
targets: ["ceiling", "worktop"],
|
|
29
|
+
state: "ON",
|
|
30
|
+
source: "motion"
|
|
31
|
+
}
|
|
32
|
+
}]);
|
|
33
|
+
|
|
34
|
+
return {
|
|
35
|
+
configuration,
|
|
36
|
+
inputs,
|
|
37
|
+
outputs,
|
|
38
|
+
invariant: "The policy emits semantic lighting intent only when occupancy is enabled and dark enough."
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
runWhenMain(import.meta.url, name, run);
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import {transitionOccupancy} from "../dist/lib/occupancy-fusion.js";
|
|
2
|
+
import {assert, runWhenMain} from "./_shared.mjs";
|
|
3
|
+
|
|
4
|
+
export const name = "occupancy-fusion";
|
|
5
|
+
|
|
6
|
+
export function run() {
|
|
7
|
+
const configuration = {presenceMode: "hold"};
|
|
8
|
+
const inputs = [
|
|
9
|
+
{source: "presence", payload: {presence: true}},
|
|
10
|
+
{source: "motion", payload: {motion: true}},
|
|
11
|
+
{source: "motion", payload: {motion: false}},
|
|
12
|
+
{source: "presence", payload: {presence: false}}
|
|
13
|
+
];
|
|
14
|
+
let state;
|
|
15
|
+
const outputs = [];
|
|
16
|
+
|
|
17
|
+
for (const input of inputs) {
|
|
18
|
+
const result = transitionOccupancy(state, input, configuration);
|
|
19
|
+
state = result.state;
|
|
20
|
+
outputs.push(...result.outputs.map(({payload}) => payload));
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
assert.deepEqual(outputs.map(({occupied}) => occupied), [true, false]);
|
|
24
|
+
assert.equal(outputs[0].trigger, "motion");
|
|
25
|
+
assert.equal(outputs[1].reason, "all-sources-inactive");
|
|
26
|
+
|
|
27
|
+
return {
|
|
28
|
+
configuration,
|
|
29
|
+
inputs,
|
|
30
|
+
outputs,
|
|
31
|
+
invariant: "Presence can hold motion-established occupancy but cannot establish it."
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
runWhenMain(import.meta.url, name, run);
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import {transitionRoom} from "../dist/lib/room-controller.js";
|
|
2
|
+
import {assert, runWhenMain} from "./_shared.mjs";
|
|
3
|
+
|
|
4
|
+
export const name = "room-lighting";
|
|
5
|
+
|
|
6
|
+
export function run() {
|
|
7
|
+
const configuration = {
|
|
8
|
+
area: "demo_room",
|
|
9
|
+
displayName: "Demo Room",
|
|
10
|
+
targets: [{
|
|
11
|
+
id: "ceiling",
|
|
12
|
+
stateTopic: "demo/lighting/ceiling/state",
|
|
13
|
+
commandTopic: "demo/lighting/ceiling/set"
|
|
14
|
+
}],
|
|
15
|
+
initialSleep: false,
|
|
16
|
+
sleepBrightnessPct: 5,
|
|
17
|
+
maxBrightnessPct: 100,
|
|
18
|
+
sleepColorTempKelvin: 1800,
|
|
19
|
+
maxColorTempKelvin: 5000,
|
|
20
|
+
wakeTime: "07:00",
|
|
21
|
+
bedTime: "23:00",
|
|
22
|
+
refreshTransitionSeconds: 30,
|
|
23
|
+
initialTransitionSeconds: 1
|
|
24
|
+
};
|
|
25
|
+
const input = {
|
|
26
|
+
lighting: {
|
|
27
|
+
kind: "intent",
|
|
28
|
+
targets: ["ceiling"],
|
|
29
|
+
state: "ON",
|
|
30
|
+
source: "motion"
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
const result = transitionRoom(undefined, input, configuration, "2026-03-20T10:00:00Z");
|
|
34
|
+
const outputs = result.outputs.map(({topic, payload, retain, lighting_command}) => ({
|
|
35
|
+
topic,
|
|
36
|
+
payload,
|
|
37
|
+
retain,
|
|
38
|
+
lighting_command
|
|
39
|
+
}));
|
|
40
|
+
|
|
41
|
+
assert.equal(outputs.length, 1);
|
|
42
|
+
assert.equal(outputs[0].topic, "demo/lighting/ceiling/set");
|
|
43
|
+
assert.equal(outputs[0].payload.state, "ON");
|
|
44
|
+
assert.equal(outputs[0].lighting_command.source, "motion");
|
|
45
|
+
|
|
46
|
+
return {
|
|
47
|
+
configuration: {
|
|
48
|
+
area: configuration.area,
|
|
49
|
+
targets: configuration.targets.map(({id}) => id),
|
|
50
|
+
schedule: {wakeTime: configuration.wakeTime, bedTime: configuration.bedTime}
|
|
51
|
+
},
|
|
52
|
+
inputs: [input],
|
|
53
|
+
outputs,
|
|
54
|
+
invariant: "The room controller is the only transport command owner and records the policy source."
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
runWhenMain(import.meta.url, name, run);
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import * as occupancy from "./occupancy-fusion.mjs";
|
|
2
|
+
import * as motion from "./motion-lighting.mjs";
|
|
3
|
+
import * as switches from "./switch-gestures.mjs";
|
|
4
|
+
import * as room from "./room-lighting.mjs";
|
|
5
|
+
import * as source from "./source-readiness.mjs";
|
|
6
|
+
import * as volume from "./volume-ownership.mjs";
|
|
7
|
+
import * as theatre from "./theatre-activity.mjs";
|
|
8
|
+
|
|
9
|
+
const examples = [occupancy, motion, switches, room, source, volume, theatre];
|
|
10
|
+
const results = examples.map(({name, run}) => ({name, ...run()}));
|
|
11
|
+
|
|
12
|
+
if (process.argv.includes("--verify")) {
|
|
13
|
+
console.log(`Contract examples passed (${results.length}).`);
|
|
14
|
+
} else {
|
|
15
|
+
console.log(JSON.stringify(results, null, 2));
|
|
16
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import {transitionSource} from "../dist/lib/source-capability-controller.js";
|
|
2
|
+
import {assert, runWhenMain} from "./_shared.mjs";
|
|
3
|
+
|
|
4
|
+
export const name = "source-readiness";
|
|
5
|
+
|
|
6
|
+
export function run() {
|
|
7
|
+
const configuration = {
|
|
8
|
+
sources: {
|
|
9
|
+
streamer: {
|
|
10
|
+
activation: "command",
|
|
11
|
+
deactivation: "command",
|
|
12
|
+
confirmation: "application"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
pollMs: 1000,
|
|
16
|
+
confirmationTimeoutMs: 5000,
|
|
17
|
+
maxAttempts: 2
|
|
18
|
+
};
|
|
19
|
+
const intent = {
|
|
20
|
+
kind: "intent",
|
|
21
|
+
requestId: "watch-1",
|
|
22
|
+
generation: 1,
|
|
23
|
+
sourceId: "streamer",
|
|
24
|
+
action: "activate"
|
|
25
|
+
};
|
|
26
|
+
let result = transitionSource(undefined, {sourceCapability: intent}, configuration, "2026-01-01T12:00:00.000Z");
|
|
27
|
+
const commands = [...result.commands];
|
|
28
|
+
result = transitionSource(result.state, {sourceCapability: {
|
|
29
|
+
kind: "command_result",
|
|
30
|
+
generation: 1,
|
|
31
|
+
sourceId: "streamer",
|
|
32
|
+
ok: true
|
|
33
|
+
}}, configuration, "2026-01-01T12:00:00.100Z");
|
|
34
|
+
result = transitionSource(result.state, {sourceCapability: {
|
|
35
|
+
kind: "observed",
|
|
36
|
+
generation: 1,
|
|
37
|
+
sourceId: "streamer",
|
|
38
|
+
status: "active",
|
|
39
|
+
observedAt: "2026-01-01T12:00:00.200Z",
|
|
40
|
+
application: "player.example",
|
|
41
|
+
playback: "paused"
|
|
42
|
+
}}, configuration, "2026-01-01T12:00:00.200Z");
|
|
43
|
+
const evidence = result.events.at(-1).theatre;
|
|
44
|
+
|
|
45
|
+
assert.equal(result.state.phase, "ready");
|
|
46
|
+
assert.equal(evidence.details.readiness.required, "application");
|
|
47
|
+
assert.equal(evidence.details.readiness.satisfied, true);
|
|
48
|
+
assert.equal(evidence.details.readiness.playback, "paused");
|
|
49
|
+
|
|
50
|
+
return {
|
|
51
|
+
configuration,
|
|
52
|
+
inputs: [intent, {kind: "command_result", ok: true}, {
|
|
53
|
+
kind: "observed",
|
|
54
|
+
status: "active",
|
|
55
|
+
application: "player.example",
|
|
56
|
+
playback: "paused"
|
|
57
|
+
}],
|
|
58
|
+
commands,
|
|
59
|
+
outputs: [evidence],
|
|
60
|
+
invariant: "A successful command is not ready until fresh application evidence arrives."
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
runWhenMain(import.meta.url, name, run);
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import {transitionSwitchPolicy} from "../dist/lib/switch-policy.js";
|
|
2
|
+
import {assert, runWhenMain} from "./_shared.mjs";
|
|
3
|
+
|
|
4
|
+
export const name = "switch-gestures";
|
|
5
|
+
|
|
6
|
+
export function run() {
|
|
7
|
+
const configuration = {targets: ["ceiling"], switchWindowMs: 500, switchFreshnessMs: 60_000};
|
|
8
|
+
const input = {source: "switch-action", payload: {action: "double"}};
|
|
9
|
+
const result = transitionSwitchPolicy(undefined, input, configuration, 1000);
|
|
10
|
+
const outputs = result.effects
|
|
11
|
+
.filter(({kind}) => kind === "send")
|
|
12
|
+
.map(({message}) => message);
|
|
13
|
+
|
|
14
|
+
assert.deepEqual(outputs, [{
|
|
15
|
+
lighting: {
|
|
16
|
+
kind: "intent",
|
|
17
|
+
targets: ["ceiling"],
|
|
18
|
+
state: "ON",
|
|
19
|
+
source: "wall",
|
|
20
|
+
person: true,
|
|
21
|
+
manual: true,
|
|
22
|
+
values: {brightness: 254}
|
|
23
|
+
}
|
|
24
|
+
}]);
|
|
25
|
+
|
|
26
|
+
return {
|
|
27
|
+
configuration,
|
|
28
|
+
inputs: [input],
|
|
29
|
+
outputs,
|
|
30
|
+
invariant: "A double action requests explicit full brightness and manual ownership."
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
runWhenMain(import.meta.url, name, run);
|