@avantics/node-red-home-automation 2.2.1 → 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/README.md +46 -83
- package/contract-examples/room-lighting.mjs +3 -5
- package/dist/lib/avr-controller.d.ts +23 -0
- package/dist/lib/avr-controller.js +139 -39
- 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/display-mode-controller.d.ts +16 -0
- package/dist/lib/display-mode-controller.js +60 -1
- package/dist/lib/light-styling.d.ts +11 -0
- package/dist/lib/light-styling.js +15 -0
- package/dist/lib/occupancy-fusion.js +3 -2
- package/dist/lib/room-controller.d.ts +9 -14
- package/dist/lib/room-controller.js +15 -58
- package/dist/lib/source-capability-controller.d.ts +2 -0
- package/dist/lib/source-capability-controller.js +20 -3
- package/dist/nodes/avr-controller.html +10 -1
- package/dist/nodes/avr-controller.js +11 -11
- package/dist/nodes/display-mode-controller.html +2 -2
- package/dist/nodes/display-mode-controller.js +3 -2
- package/dist/nodes/room-controller.html +19 -15
- package/dist/nodes/room-controller.js +20 -21
- package/dist/nodes/theatre-activity-controller.html +1 -1
- package/dist/nodes/theatre-activity-controller.js +7 -0
- package/examples/Home theatre activity.json +299 -36
- package/examples/Room lighting.json +8 -10
- package/package.json +3 -7
- package/THIRD-PARTY-NOTICES.md +0 -218
package/README.md
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
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/)
|
|
4
5
|
[](https://ko-fi.com/avantics)
|
|
5
6
|
|
|
6
7
|
State-aware Node-RED controllers for reliable lighting and home-theatre automation.
|
|
@@ -12,7 +13,7 @@ Most home automations translate an event directly into a command. This package a
|
|
|
12
13
|
## What it solves
|
|
13
14
|
|
|
14
15
|
- **Occupancy without flicker:** combine PIR motion with optional presence sensing before changing room state.
|
|
15
|
-
- **
|
|
16
|
+
- **Circadian lighting with manual ownership:** calculate circadian brightness and colour temperature without immediately overwriting wall-switch changes.
|
|
16
17
|
- **Honest wall inputs:** preserve restart baselines while exposing unconfirmed, stale and unavailable physical-input evidence.
|
|
17
18
|
- **Reliable AV transitions:** coordinate AVR inputs, TV power, projector warm-up and cooldown, and motorised screens from observed state rather than blind toggles.
|
|
18
19
|
- **Honest source readiness:** model controllable, externally controlled, and unavailable playback sources without claiming success from an input selection alone.
|
|
@@ -20,52 +21,7 @@ Most home automations translate an event directly into a command. This package a
|
|
|
20
21
|
- **Bounded receiver volume:** coordinate absolute and relative requests through one transport owner while preserving later physical adjustments.
|
|
21
22
|
- **Recoverable automation:** adopt one fresh, uniquely matching physical activity after lost state; otherwise coordinate a safe Off request.
|
|
22
23
|
|
|
23
|
-
|
|
24
|
-
flowchart LR
|
|
25
|
-
subgraph In[Inputs]
|
|
26
|
-
SENS[Motion and presence sensors]
|
|
27
|
-
LUX[Illuminance]
|
|
28
|
-
WALL[Wall switches]
|
|
29
|
-
REQ[Theatre request<br/>MQTT .../theatre/request]
|
|
30
|
-
end
|
|
31
|
-
subgraph Room[Room lighting]
|
|
32
|
-
OCC[occupancy-fusion]
|
|
33
|
-
MOT[motion-lighting-policy]
|
|
34
|
-
SW[switch-lighting-policy]
|
|
35
|
-
ROOM[room-lighting-controller]
|
|
36
|
-
end
|
|
37
|
-
subgraph Act[Theatre activities]
|
|
38
|
-
CON[theatre-state-contract]
|
|
39
|
-
ACT[theatre-activity-controller]
|
|
40
|
-
ROUTE{{your flow: route by target}}
|
|
41
|
-
AL[activity-lighting-controller]
|
|
42
|
-
end
|
|
43
|
-
subgraph Eq[Theatre equipment]
|
|
44
|
-
EQ[avr / display-mode /<br/>source-capability /<br/>backlighting controllers]
|
|
45
|
-
end
|
|
46
|
-
LIGHTS[(Lights, e.g. Zigbee2MQTT)]
|
|
47
|
-
DEV[(Device transports)]
|
|
48
|
-
STATE[(Retained theatre state<br/>and HA discovery)]
|
|
49
|
-
|
|
50
|
-
SENS --> OCC --> MOT
|
|
51
|
-
LUX --> MOT
|
|
52
|
-
WALL --> SW
|
|
53
|
-
MOT --> ROOM
|
|
54
|
-
SW --> ROOM
|
|
55
|
-
ROOM -- set commands --> LIGHTS
|
|
56
|
-
LIGHTS -. observed state .-> ROOM
|
|
57
|
-
REQ --> CON
|
|
58
|
-
CON -- activity intent --> ACT
|
|
59
|
-
ACT -- state snapshot --> CON
|
|
60
|
-
CON --> STATE
|
|
61
|
-
ACT -- theatreCommand --> ROUTE
|
|
62
|
-
ROUTE --> EQ
|
|
63
|
-
ROUTE --> AL
|
|
64
|
-
EQ -- device commands --> DEV
|
|
65
|
-
EQ -. target state .-> ACT
|
|
66
|
-
AL -. target state .-> ACT
|
|
67
|
-
AL -- held-light intents --> ROOM
|
|
68
|
-
```
|
|
24
|
+

|
|
69
25
|
|
|
70
26
|
Each controller keeps the desired and the observed state, sends semantic
|
|
71
27
|
commands, and reports done only when fresh observations confirm them.
|
|
@@ -79,8 +35,12 @@ The nodes come in three groups. Each group has an importable example flow
|
|
|
79
35
|
|
|
80
36
|
Motion, presence, illuminance and wall switches drive one room's lights, with
|
|
81
37
|
circadian styling and manual ownership. Wire occupancy fusion into the motion
|
|
82
|
-
policy, and the motion and switch policies into the room controller.
|
|
83
|
-
|
|
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).
|
|
84
44
|
|
|
85
45
|
| Node | Purpose |
|
|
86
46
|
|---|---|
|
|
@@ -93,7 +53,7 @@ Example: [`Room lighting.json`](examples/Room%20lighting.json).
|
|
|
93
53
|
|
|
94
54
|
Each controller owns one kind of device. It accepts a generation-scoped intent,
|
|
95
55
|
sends commands for your device transport on output 1, and reports target state
|
|
96
|
-
on output 2. Example: [`Home theatre equipment.json`](examples/Home%20theatre%20equipment.json).
|
|
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).
|
|
97
57
|
|
|
98
58
|
| Node | Purpose |
|
|
99
59
|
|---|---|
|
|
@@ -110,9 +70,14 @@ activity controller plans each target and sends `msg.theatreCommand` messages.
|
|
|
110
70
|
Your flow routes each command by its `target` (`avr`, `display`, `source`,
|
|
111
71
|
`lighting`, `backlighting`) to the matching controller's input
|
|
112
72
|
(`msg.avr`, `msg.display`, `msg.sourceCapability`, `msg.activityLighting`,
|
|
113
|
-
`msg.backlighting`)
|
|
114
|
-
|
|
115
|
-
|
|
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).
|
|
116
81
|
|
|
117
82
|
| Node | Purpose |
|
|
118
83
|
|---|---|
|
|
@@ -122,7 +87,7 @@ Example: [`Home theatre activity.json`](examples/Home%20theatre%20activity.json)
|
|
|
122
87
|
|
|
123
88
|
Pure TypeScript policy modules live in `lib/`; thin Node-RED runtime and editor adapters live in `nodes/`.
|
|
124
89
|
|
|
125
|
-
Importable example flows for the Node-RED editor live in [`examples/`](examples
|
|
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.
|
|
126
91
|
|
|
127
92
|
## Requirements
|
|
128
93
|
|
|
@@ -144,7 +109,7 @@ also attaches the package tarball and its SHA-256 checksum. Install a downloaded
|
|
|
144
109
|
tarball through **Manage palette → Install → Upload module**, or with:
|
|
145
110
|
|
|
146
111
|
```sh
|
|
147
|
-
npm install /path/to/avantics-node-red-home-automation-2.
|
|
112
|
+
npm install /path/to/avantics-node-red-home-automation-2.3.0.tgz
|
|
148
113
|
```
|
|
149
114
|
|
|
150
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.
|
|
@@ -262,7 +227,8 @@ from fresh observed volume, accumulates rapid requests against the pending
|
|
|
262
227
|
target, applies configured half-step bounds, retries through the existing
|
|
263
228
|
bounded transport path, and confirms completion from fresh receiver evidence.
|
|
264
229
|
Once confirmed, it releases the volume target; a later physical or external
|
|
265
|
-
adjustment becomes the new baseline instead of being corrected.
|
|
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.
|
|
266
232
|
|
|
267
233
|
## Contract examples
|
|
268
234
|
|
|
@@ -274,13 +240,13 @@ npm run examples
|
|
|
274
240
|
|
|
275
241
|
| Example | Boundary demonstrated |
|
|
276
242
|
|---|---|
|
|
277
|
-
| [`occupancy-fusion.mjs`](contract-examples/occupancy-fusion.mjs) | Motion establishes occupancy; optional presence can hold it. |
|
|
278
|
-
| [`motion-lighting.mjs`](contract-examples/motion-lighting.mjs) | Occupancy and illuminance produce semantic lighting intent. |
|
|
279
|
-
| [`switch-gestures.mjs`](contract-examples/switch-gestures.mjs) | Wall actions produce toggle or explicit manual-brightness intent. |
|
|
280
|
-
| [`room-lighting.mjs`](contract-examples/room-lighting.mjs) | The room owner converts semantic intent into a transport command. |
|
|
281
|
-
| [`source-readiness.mjs`](contract-examples/source-readiness.mjs) | Fresh application evidence confirms source readiness. |
|
|
282
|
-
| [`volume-ownership.mjs`](contract-examples/volume-ownership.mjs) | Bounded relative requests accumulate and release ownership after fresh confirmation. |
|
|
283
|
-
| [`theatre-activity.mjs`](contract-examples/theatre-activity.mjs) | Coordinated activity waits for fresh target-owner evidence. |
|
|
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. |
|
|
284
250
|
|
|
285
251
|
Each example uses synthetic identifiers and configuration, asserts its central invariant, and prints the complete input/output contract as JSON.
|
|
286
252
|
|
|
@@ -291,15 +257,7 @@ The repository contains no home-network addresses, deployment coordinates, or li
|
|
|
291
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.
|
|
292
258
|
|
|
293
259
|
Report suspected vulnerabilities privately using the process in
|
|
294
|
-
[`SECURITY.md`](SECURITY.md), not through a public issue.
|
|
295
|
-
|
|
296
|
-
## Public repository boundary
|
|
297
|
-
|
|
298
|
-
This repository contains reusable controller code and general delivery status.
|
|
299
|
-
Live flows, private hostnames and IP addresses, Home Assistant entity IDs,
|
|
300
|
-
backup paths, credentials, and infrastructure operations belong only in the
|
|
301
|
-
private deployment repositories. `npm run scan:public` enforces the machine-
|
|
302
|
-
detectable parts of that boundary.
|
|
260
|
+
[`SECURITY.md`](https://github.com/avantics-hq/node-red-home-automation/blob/main/SECURITY.md), not through a public issue.
|
|
303
261
|
|
|
304
262
|
## Development
|
|
305
263
|
|
|
@@ -309,22 +267,27 @@ npm test
|
|
|
309
267
|
npm run examples
|
|
310
268
|
```
|
|
311
269
|
|
|
312
|
-
The strict TypeScript build, compiled test suite, executable contract examples, dependency audit, public-content scan, and secret scan run in CI. See [CONTRIBUTING.md](CONTRIBUTING.md) for local checks and the optional pre-commit setup.
|
|
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.
|
|
313
271
|
|
|
314
272
|
## Contributing
|
|
315
273
|
|
|
316
|
-
Contributions are welcome: see [CONTRIBUTING.md](CONTRIBUTING.md) and the
|
|
317
|
-
[Code of Conduct](CODE_OF_CONDUCT.md).
|
|
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
|
|
318
278
|
|
|
319
|
-
|
|
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.
|
|
320
286
|
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
basnijholt, licensed under Apache-2.0. See
|
|
325
|
-
[THIRD-PARTY-NOTICES.md](THIRD-PARTY-NOTICES.md). They are due to be replaced by
|
|
326
|
-
an original implementation.
|
|
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.
|
|
327
290
|
|
|
328
291
|
## License
|
|
329
292
|
|
|
330
|
-
[MIT](LICENSE)
|
|
293
|
+
[MIT](https://github.com/avantics-hq/node-red-home-automation/blob/main/LICENSE)
|
|
@@ -13,14 +13,12 @@ export function run() {
|
|
|
13
13
|
commandTopic: "demo/lighting/ceiling/set"
|
|
14
14
|
}],
|
|
15
15
|
initialSleep: false,
|
|
16
|
-
latitude: 0,
|
|
17
|
-
longitude: 0,
|
|
18
16
|
sleepBrightnessPct: 5,
|
|
19
17
|
maxBrightnessPct: 100,
|
|
20
|
-
minBrightnessPct: 10,
|
|
21
18
|
sleepColorTempKelvin: 1800,
|
|
22
|
-
minColorTempKelvin: 2200,
|
|
23
19
|
maxColorTempKelvin: 5000,
|
|
20
|
+
wakeTime: "07:00",
|
|
21
|
+
bedTime: "23:00",
|
|
24
22
|
refreshTransitionSeconds: 30,
|
|
25
23
|
initialTransitionSeconds: 1
|
|
26
24
|
};
|
|
@@ -49,7 +47,7 @@ export function run() {
|
|
|
49
47
|
configuration: {
|
|
50
48
|
area: configuration.area,
|
|
51
49
|
targets: configuration.targets.map(({id}) => id),
|
|
52
|
-
|
|
50
|
+
schedule: {wakeTime: configuration.wakeTime, bedTime: configuration.bedTime}
|
|
53
51
|
},
|
|
54
52
|
inputs: [input],
|
|
55
53
|
outputs,
|
|
@@ -54,6 +54,7 @@ export interface AvrFailure {
|
|
|
54
54
|
export interface AvrState {
|
|
55
55
|
observed: AvrObserved;
|
|
56
56
|
seen: Record<AvrField, boolean>;
|
|
57
|
+
fieldObservedAt: Record<AvrField, string | null>;
|
|
57
58
|
desired: AvrDesired | null;
|
|
58
59
|
generation: number;
|
|
59
60
|
pending: AvrPending | null;
|
|
@@ -100,6 +101,22 @@ export interface AvrCommand {
|
|
|
100
101
|
attempt: number;
|
|
101
102
|
};
|
|
102
103
|
}
|
|
104
|
+
export interface AvrTargetState {
|
|
105
|
+
kind: "target_state";
|
|
106
|
+
target: "avr";
|
|
107
|
+
requestId: string;
|
|
108
|
+
generation: number;
|
|
109
|
+
status: string;
|
|
110
|
+
observedAt: string;
|
|
111
|
+
details: Record<string, unknown>;
|
|
112
|
+
classification?: "manual_override";
|
|
113
|
+
failure?: {
|
|
114
|
+
operation: string;
|
|
115
|
+
reason: string;
|
|
116
|
+
attempts: number;
|
|
117
|
+
occurredAt: string;
|
|
118
|
+
};
|
|
119
|
+
}
|
|
103
120
|
export interface AvrOutputEvent {
|
|
104
121
|
payload?: unknown;
|
|
105
122
|
avr: {
|
|
@@ -129,10 +146,16 @@ export type ParsedAvrLine = {
|
|
|
129
146
|
export declare function createAvrState(_config: AvrConfig, saved?: Partial<AvrState>): AvrState;
|
|
130
147
|
export declare function formatVolumeCommand(value: number): string;
|
|
131
148
|
export declare function parseAvrLine(value: unknown, sources: Record<string, string>): ParsedAvrLine | null;
|
|
149
|
+
export declare function normaliseAvrConfig(rawConfig: Omit<Partial<AvrConfig>, "sources"> & {
|
|
150
|
+
sources?: unknown;
|
|
151
|
+
}): AvrConfig;
|
|
132
152
|
export declare function transitionAvr(saved: Partial<AvrState> | undefined, msg: AvrMessage, rawConfig: Partial<AvrConfig>, nowValue?: Date | string | number): {
|
|
133
153
|
state: AvrState;
|
|
134
154
|
commands: AvrCommand[];
|
|
135
155
|
events: AvrOutputEvent[];
|
|
156
|
+
targetStates: {
|
|
157
|
+
theatre: AvrTargetState;
|
|
158
|
+
}[];
|
|
136
159
|
traces: {
|
|
137
160
|
event: string;
|
|
138
161
|
outcome: string;
|
|
@@ -4,6 +4,7 @@ exports.QUERY_COMMAND = void 0;
|
|
|
4
4
|
exports.createAvrState = createAvrState;
|
|
5
5
|
exports.formatVolumeCommand = formatVolumeCommand;
|
|
6
6
|
exports.parseAvrLine = parseAvrLine;
|
|
7
|
+
exports.normaliseAvrConfig = normaliseAvrConfig;
|
|
7
8
|
exports.transitionAvr = transitionAvr;
|
|
8
9
|
exports.QUERY_COMMAND = "PW?\rZM?\rSI?\rMV?\r";
|
|
9
10
|
function parseSources(value) {
|
|
@@ -22,6 +23,9 @@ function parseSources(value) {
|
|
|
22
23
|
}
|
|
23
24
|
return Object.fromEntries(typedEntries);
|
|
24
25
|
}
|
|
26
|
+
function observedTime(value) {
|
|
27
|
+
return typeof value === "string" && Number.isFinite(Date.parse(value)) ? value : null;
|
|
28
|
+
}
|
|
25
29
|
function createAvrState(_config, saved = {}) {
|
|
26
30
|
const observed = saved.observed || {};
|
|
27
31
|
const seen = saved.seen || {};
|
|
@@ -38,6 +42,12 @@ function createAvrState(_config, saved = {}) {
|
|
|
38
42
|
volume: Number.isFinite(observed.volume) ? observed.volume : null,
|
|
39
43
|
at: typeof observed.at === "string" ? observed.at : null
|
|
40
44
|
},
|
|
45
|
+
fieldObservedAt: {
|
|
46
|
+
power: observedTime(saved.fieldObservedAt?.power),
|
|
47
|
+
mainZone: observedTime(saved.fieldObservedAt?.mainZone),
|
|
48
|
+
source: observedTime(saved.fieldObservedAt?.source),
|
|
49
|
+
volume: observedTime(saved.fieldObservedAt?.volume)
|
|
50
|
+
},
|
|
41
51
|
seen: {
|
|
42
52
|
power: seen.power === true,
|
|
43
53
|
mainZone: seen.mainZone === true,
|
|
@@ -120,7 +130,10 @@ function parseAvrLine(value, sources) {
|
|
|
120
130
|
}
|
|
121
131
|
return null;
|
|
122
132
|
}
|
|
123
|
-
|
|
133
|
+
// Validates AVR configuration and fills its defaults. The Node-RED adapter
|
|
134
|
+
// calls this at deploy, so a bad configuration shows on the node instead of
|
|
135
|
+
// failing every message.
|
|
136
|
+
function normaliseAvrConfig(rawConfig) {
|
|
124
137
|
const rawMinVolume = Number(rawConfig.minVolume);
|
|
125
138
|
const rawMaxVolume = Number(rawConfig.maxVolume);
|
|
126
139
|
const minVolume = roundVolume(rawMinVolume);
|
|
@@ -130,7 +143,7 @@ function transitionAvr(saved, msg, rawConfig, nowValue = new Date()) {
|
|
|
130
143
|
minVolume < 0 || maxVolume > 98 || minVolume >= maxVolume) {
|
|
131
144
|
throw new Error("volume bounds must be half-step values between 0 and 98 with minimum below maximum");
|
|
132
145
|
}
|
|
133
|
-
|
|
146
|
+
return {
|
|
134
147
|
area: rawConfig.area || "room",
|
|
135
148
|
sources: parseSources(rawConfig.sources),
|
|
136
149
|
retryMs: Math.max(250, Number(rawConfig.retryMs) || 2000),
|
|
@@ -138,16 +151,24 @@ function transitionAvr(saved, msg, rawConfig, nowValue = new Date()) {
|
|
|
138
151
|
minVolume,
|
|
139
152
|
maxVolume
|
|
140
153
|
};
|
|
154
|
+
}
|
|
155
|
+
function transitionAvr(saved, msg, rawConfig, nowValue = new Date()) {
|
|
156
|
+
const config = normaliseAvrConfig(rawConfig);
|
|
141
157
|
const now = nowValue instanceof Date ? nowValue : new Date(nowValue);
|
|
142
158
|
const nowMs = now.getTime();
|
|
143
159
|
const state = createAvrState(config, saved);
|
|
144
160
|
const commands = [];
|
|
145
161
|
const events = [];
|
|
162
|
+
const targetStates = [];
|
|
146
163
|
const traces = [];
|
|
147
164
|
const event = msg.avr || { kind: "unknown" };
|
|
165
|
+
const previousPhase = state.phase;
|
|
166
|
+
// Fields the receiver reported in this event.
|
|
167
|
+
const reportedFields = new Set();
|
|
148
168
|
const before = JSON.stringify({
|
|
149
169
|
observed: state.observed,
|
|
150
170
|
seen: state.seen,
|
|
171
|
+
fieldObservedAt: state.fieldObservedAt,
|
|
151
172
|
desired: state.desired,
|
|
152
173
|
generation: state.generation,
|
|
153
174
|
pending: state.pending,
|
|
@@ -233,9 +254,11 @@ function transitionAvr(saved, msg, rawConfig, nowValue = new Date()) {
|
|
|
233
254
|
attempt,
|
|
234
255
|
deadlineAt: nowMs + config.retryMs
|
|
235
256
|
};
|
|
236
|
-
state.phase =
|
|
237
|
-
? "
|
|
238
|
-
: "
|
|
257
|
+
state.phase = volumeOperation && blocked()
|
|
258
|
+
? "failed"
|
|
259
|
+
: operation === "query" || operation === "volume-query"
|
|
260
|
+
? "discovering"
|
|
261
|
+
: "transitioning";
|
|
239
262
|
if (operation === "volume")
|
|
240
263
|
state.volumeMetrics.commands += 1;
|
|
241
264
|
trace("command", "emitted", null, { operation, attempt });
|
|
@@ -270,6 +293,24 @@ function transitionAvr(saved, msg, rawConfig, nowValue = new Date()) {
|
|
|
270
293
|
const bounded = Math.max(config.minVolume, Math.min(config.maxVolume, rounded));
|
|
271
294
|
return { value: bounded, bounded: Math.abs(bounded - value) > 0.001 };
|
|
272
295
|
}
|
|
296
|
+
// The phase once no volume work is in flight; a failed request stays failed.
|
|
297
|
+
function blocked() {
|
|
298
|
+
return state.desired !== null && state.blockedGeneration === state.desired.generation;
|
|
299
|
+
}
|
|
300
|
+
function restingPhase() {
|
|
301
|
+
if (state.classification === "manual_override")
|
|
302
|
+
return "manual_override";
|
|
303
|
+
if (blocked())
|
|
304
|
+
return "failed";
|
|
305
|
+
return state.desired && targetSatisfied() ? "ready" : "idle";
|
|
306
|
+
}
|
|
307
|
+
// Every failed volume request is reported in one shape; operation and
|
|
308
|
+
// attempts are null when no command was involved (a restart).
|
|
309
|
+
function volumeFailed(requestId, reason, operation, attempts) {
|
|
310
|
+
state.volumeMetrics.failures += 1;
|
|
311
|
+
events.push({ avr: { kind: "volume_failed", area: config.area, requestId, reason, operation, attempts } });
|
|
312
|
+
trace("volume", "failed", reason, { requestId, operation, attempts });
|
|
313
|
+
}
|
|
273
314
|
function completeVolume() {
|
|
274
315
|
const request = state.volumeRequest;
|
|
275
316
|
if (!request || request.target === null)
|
|
@@ -292,11 +333,7 @@ function transitionAvr(saved, msg, rawConfig, nowValue = new Date()) {
|
|
|
292
333
|
bounded: request.bounded
|
|
293
334
|
});
|
|
294
335
|
state.volumeRequest = null;
|
|
295
|
-
state.phase =
|
|
296
|
-
? "manual_override"
|
|
297
|
-
: state.desired && targetSatisfied()
|
|
298
|
-
? "ready"
|
|
299
|
-
: "idle";
|
|
336
|
+
state.phase = restingPhase();
|
|
300
337
|
}
|
|
301
338
|
function abandonVolume(reason, observed = state.observed.volume) {
|
|
302
339
|
const request = state.volumeRequest;
|
|
@@ -320,11 +357,7 @@ function transitionAvr(saved, msg, rawConfig, nowValue = new Date()) {
|
|
|
320
357
|
target: request.target
|
|
321
358
|
});
|
|
322
359
|
state.volumeRequest = null;
|
|
323
|
-
state.phase =
|
|
324
|
-
? "manual_override"
|
|
325
|
-
: state.desired && targetSatisfied()
|
|
326
|
-
? "ready"
|
|
327
|
-
: "idle";
|
|
360
|
+
state.phase = restingPhase();
|
|
328
361
|
}
|
|
329
362
|
function resolveRelativeVolume() {
|
|
330
363
|
const request = state.volumeRequest;
|
|
@@ -400,8 +433,12 @@ function transitionAvr(saved, msg, rawConfig, nowValue = new Date()) {
|
|
|
400
433
|
trace("observation", "manual_override");
|
|
401
434
|
}
|
|
402
435
|
if (state.desired && state.classification !== "manual_override" && !targetSatisfied()) {
|
|
403
|
-
if
|
|
436
|
+
// A failed request still answers volume: sent if the receiver is on,
|
|
437
|
+
// rejected otherwise.
|
|
438
|
+
if (blocked()) {
|
|
439
|
+
reconcileVolume(allowQuery);
|
|
404
440
|
return;
|
|
441
|
+
}
|
|
405
442
|
if (!hasFreshTargetState()) {
|
|
406
443
|
state.phase = "discovering";
|
|
407
444
|
if (allowQuery)
|
|
@@ -460,10 +497,29 @@ function transitionAvr(saved, msg, rawConfig, nowValue = new Date()) {
|
|
|
460
497
|
return "zero-volume-delta";
|
|
461
498
|
return null;
|
|
462
499
|
}
|
|
463
|
-
if (event.kind === "
|
|
500
|
+
if (event.kind === "refresh") {
|
|
501
|
+
// Re-read the receiver without touching work in flight: desired state,
|
|
502
|
+
// pending commands, the volume request and the phase all carry on.
|
|
503
|
+
commands.push({
|
|
504
|
+
payload: exports.QUERY_COMMAND,
|
|
505
|
+
avrCommand: {
|
|
506
|
+
area: config.area,
|
|
507
|
+
operation: "query",
|
|
508
|
+
requestId: state.desired?.requestId || null,
|
|
509
|
+
generation: state.desired?.generation ?? state.generation,
|
|
510
|
+
attempt: 1
|
|
511
|
+
}
|
|
512
|
+
});
|
|
513
|
+
trace("refresh", "querying", null, { pending: state.pending?.operation ?? null });
|
|
514
|
+
forceStateEvent = true;
|
|
515
|
+
}
|
|
516
|
+
else if (event.kind === "startup") {
|
|
464
517
|
const requestBlocked = state.desired &&
|
|
465
518
|
state.blockedGeneration === state.desired.generation;
|
|
466
519
|
const abandonedVolumeRequest = state.volumeRequest?.requestId || null;
|
|
520
|
+
// Not replayed after a restart: the caller hears it failed.
|
|
521
|
+
if (abandonedVolumeRequest)
|
|
522
|
+
volumeFailed(abandonedVolumeRequest, "restart", null, null);
|
|
467
523
|
state.seen = { power: false, mainZone: false, source: false, volume: false };
|
|
468
524
|
state.pending = null;
|
|
469
525
|
state.volumeRequest = null;
|
|
@@ -506,6 +562,8 @@ function transitionAvr(saved, msg, rawConfig, nowValue = new Date()) {
|
|
|
506
562
|
let current;
|
|
507
563
|
const wasFresh = state.seen[parsed.field];
|
|
508
564
|
state.seen[parsed.field] = true;
|
|
565
|
+
state.fieldObservedAt[parsed.field] = now.toISOString();
|
|
566
|
+
reportedFields.add(parsed.field);
|
|
509
567
|
if (parsed.field === "source") {
|
|
510
568
|
previous = state.observed.sourceCode;
|
|
511
569
|
state.observed.source = parsed.value;
|
|
@@ -629,6 +687,10 @@ function transitionAvr(saved, msg, rawConfig, nowValue = new Date()) {
|
|
|
629
687
|
state.blockedGeneration = null;
|
|
630
688
|
state.classification = null;
|
|
631
689
|
state.lastAttempt = 0;
|
|
690
|
+
// Ready belonged to the previous request; left as it was, the drift
|
|
691
|
+
// check would read this request's unmet target as a manual change.
|
|
692
|
+
if (state.phase === "ready" || state.phase === "manual_override")
|
|
693
|
+
state.phase = "idle";
|
|
632
694
|
trace("intent", "accepted", null, {
|
|
633
695
|
requestId: event.requestId,
|
|
634
696
|
generation: event.generation,
|
|
@@ -700,8 +762,13 @@ function transitionAvr(saved, msg, rawConfig, nowValue = new Date()) {
|
|
|
700
762
|
state.volumeMetrics.intents += 1;
|
|
701
763
|
if (boundedThisIntent)
|
|
702
764
|
state.volumeMetrics.bounded += 1;
|
|
703
|
-
if (state.pending?.operation === "volume")
|
|
765
|
+
if (state.pending?.operation === "volume") {
|
|
704
766
|
state.pending = null;
|
|
767
|
+
// The last reading predates the command in flight, so it cannot
|
|
768
|
+
// confirm the new target: re-read before deciding.
|
|
769
|
+
if (target !== null && sameVolume(state.observed.volume, target))
|
|
770
|
+
state.seen.volume = false;
|
|
771
|
+
}
|
|
705
772
|
trace("volume", "accepted", null, {
|
|
706
773
|
requestId: event.requestId,
|
|
707
774
|
mode: event.mode,
|
|
@@ -730,27 +797,9 @@ function transitionAvr(saved, msg, rawConfig, nowValue = new Date()) {
|
|
|
730
797
|
state.pending = null;
|
|
731
798
|
if (isVolumeOperation(failed.operation)) {
|
|
732
799
|
const requestId = state.volumeRequest?.requestId || null;
|
|
733
|
-
state.volumeMetrics.failures += 1;
|
|
734
800
|
state.volumeRequest = null;
|
|
735
|
-
state.phase =
|
|
736
|
-
|
|
737
|
-
: state.desired && targetSatisfied()
|
|
738
|
-
? "ready"
|
|
739
|
-
: "idle";
|
|
740
|
-
events.push({
|
|
741
|
-
avr: {
|
|
742
|
-
kind: "volume_failed",
|
|
743
|
-
area: config.area,
|
|
744
|
-
requestId,
|
|
745
|
-
operation: failed.operation,
|
|
746
|
-
attempts: failed.attempt
|
|
747
|
-
}
|
|
748
|
-
});
|
|
749
|
-
trace("volume", "failed", "attempts-exhausted", {
|
|
750
|
-
requestId,
|
|
751
|
-
operation: failed.operation,
|
|
752
|
-
attempts: failed.attempt
|
|
753
|
-
});
|
|
801
|
+
state.phase = restingPhase();
|
|
802
|
+
volumeFailed(requestId, "attempts-exhausted", failed.operation, failed.attempt);
|
|
754
803
|
reconcile(false);
|
|
755
804
|
}
|
|
756
805
|
else {
|
|
@@ -786,6 +835,7 @@ function transitionAvr(saved, msg, rawConfig, nowValue = new Date()) {
|
|
|
786
835
|
const after = JSON.stringify({
|
|
787
836
|
observed: state.observed,
|
|
788
837
|
seen: state.seen,
|
|
838
|
+
fieldObservedAt: state.fieldObservedAt,
|
|
789
839
|
desired: state.desired,
|
|
790
840
|
generation: state.generation,
|
|
791
841
|
pending: state.pending,
|
|
@@ -843,6 +893,56 @@ function transitionAvr(saved, msg, rawConfig, nowValue = new Date()) {
|
|
|
843
893
|
: state.phase === "ready"
|
|
844
894
|
? { phase: "ready", text: (state.observed.power || "unknown") + " · " + source }
|
|
845
895
|
: { phase: "idle", text: (state.observed.power || "unknown") + " · " + source };
|
|
846
|
-
|
|
896
|
+
// The theatre activity controller's view of this target. Ready is stamped
|
|
897
|
+
// with the oldest report among the fields the request needs, so it is only
|
|
898
|
+
// as fresh as its least recent confirmation, and is re-sent only when the
|
|
899
|
+
// receiver reports one of those fields.
|
|
900
|
+
if (state.desired) {
|
|
901
|
+
const fields = requiredFields();
|
|
902
|
+
const status = state.phase === "manual_override"
|
|
903
|
+
? "transitioning"
|
|
904
|
+
: state.phase === "idle" || state.phase === "unknown" ? "confirming" : state.phase;
|
|
905
|
+
const confirmedAt = fields
|
|
906
|
+
.map((field) => state.fieldObservedAt[field])
|
|
907
|
+
.reduce((oldest, at) => at === null || oldest === null ? null : Date.parse(at) < Date.parse(oldest) ? at : oldest, now.toISOString());
|
|
908
|
+
const reported = fields.some((field) => reportedFields.has(field));
|
|
909
|
+
if (status !== "ready" || ((previousPhase !== "ready" || reported) && confirmedAt !== null)) {
|
|
910
|
+
const theatre = {
|
|
911
|
+
kind: "target_state",
|
|
912
|
+
target: "avr",
|
|
913
|
+
requestId: state.desired.requestId,
|
|
914
|
+
generation: state.desired.generation,
|
|
915
|
+
status,
|
|
916
|
+
observedAt: status === "ready" && confirmedAt !== null ? confirmedAt : now.toISOString(),
|
|
917
|
+
details: {
|
|
918
|
+
power: state.desired.power,
|
|
919
|
+
source: state.desired.source,
|
|
920
|
+
observed: {
|
|
921
|
+
power: state.observed.power,
|
|
922
|
+
mainZone: state.observed.mainZone,
|
|
923
|
+
source: state.observed.source
|
|
924
|
+
},
|
|
925
|
+
recovery: { power: state.observed.power, source: state.observed.source },
|
|
926
|
+
metrics: {
|
|
927
|
+
attempts: state.lastAttempt,
|
|
928
|
+
retries: Math.max(0, state.lastAttempt - 1),
|
|
929
|
+
fallbackUsed: false
|
|
930
|
+
}
|
|
931
|
+
}
|
|
932
|
+
};
|
|
933
|
+
if (state.classification)
|
|
934
|
+
theatre.classification = state.classification;
|
|
935
|
+
if (state.failure) {
|
|
936
|
+
theatre.failure = {
|
|
937
|
+
operation: state.failure.operation,
|
|
938
|
+
reason: "attempts-exhausted",
|
|
939
|
+
attempts: state.failure.attempts,
|
|
940
|
+
occurredAt: state.failure.at
|
|
941
|
+
};
|
|
942
|
+
}
|
|
943
|
+
targetStates.push({ theatre });
|
|
944
|
+
}
|
|
945
|
+
}
|
|
946
|
+
return { state, commands, events, targetStates, traces, status };
|
|
847
947
|
}
|
|
848
948
|
//# sourceMappingURL=avr-controller.js.map
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.alreadyStyled = alreadyStyled;
|
|
4
|
+
// Whether a light already shows a style, by its own latest report. A bulb
|
|
5
|
+
// that missed a command reports something else, so it is not "styled".
|
|
6
|
+
function alreadyStyled(style, observed) {
|
|
7
|
+
return observed?.on === true &&
|
|
8
|
+
Number(observed.brightness) === style.brightness &&
|
|
9
|
+
Number(observed.color_temp) === style.color_temp;
|
|
10
|
+
}
|
|
11
|
+
//# sourceMappingURL=circadian-refresh.js.map
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export declare const WAKE_RISE_MINUTES = 60;
|
|
2
|
+
export declare const WIND_DOWN_MINUTES = 180;
|
|
3
|
+
export declare const DEFAULT_WAKE_TIME = "07:00";
|
|
4
|
+
export declare const DEFAULT_BED_TIME = "23:00";
|
|
5
|
+
export interface DaySchedule {
|
|
6
|
+
wakeTime: string;
|
|
7
|
+
bedTime: string;
|
|
8
|
+
}
|
|
9
|
+
export declare function clockMinutes(value: unknown): number | null;
|
|
10
|
+
export declare function resolveSchedule(wakeTime?: unknown, bedTime?: unknown): DaySchedule;
|
|
11
|
+
export declare function scheduleLevel(now: Date, wakeTime: string, bedTime: string): number;
|