@avantics/node-red-home-automation 2.2.1 → 3.0.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.
Files changed (85) hide show
  1. package/README.md +76 -89
  2. package/contract-examples/room-lighting.mjs +12 -9
  3. package/dist/lib/activity-lighting-controller.d.ts +13 -25
  4. package/dist/lib/activity-lighting-controller.js +15 -11
  5. package/dist/lib/avr-controller.d.ts +17 -28
  6. package/dist/lib/avr-controller.js +165 -116
  7. package/dist/lib/backlighting-controller.d.ts +12 -25
  8. package/dist/lib/backlighting-controller.js +11 -18
  9. package/dist/lib/bounded-trace.d.ts +6 -0
  10. package/dist/lib/circadian-schedule.d.ts +11 -0
  11. package/dist/lib/circadian-schedule.js +63 -0
  12. package/dist/lib/circadian.d.ts +6 -0
  13. package/dist/lib/circadian.js +15 -0
  14. package/dist/lib/command-echo.d.ts +20 -0
  15. package/dist/lib/command-echo.js +82 -0
  16. package/dist/lib/cover-controller.d.ts +15 -28
  17. package/dist/lib/cover-controller.js +6 -8
  18. package/dist/lib/denon-protocol.d.ts +29 -0
  19. package/dist/lib/denon-protocol.js +76 -0
  20. package/dist/lib/display-mode-controller.d.ts +11 -6
  21. package/dist/lib/display-mode-controller.js +90 -39
  22. package/dist/lib/light-styling.d.ts +11 -0
  23. package/dist/lib/light-styling.js +15 -0
  24. package/dist/lib/motion-policy.d.ts +12 -22
  25. package/dist/lib/motion-policy.js +53 -56
  26. package/dist/lib/occupancy-fusion.d.ts +8 -12
  27. package/dist/lib/occupancy-fusion.js +3 -2
  28. package/dist/lib/room-controller.d.ts +13 -38
  29. package/dist/lib/room-controller.js +85 -180
  30. package/dist/lib/room-lighting-discovery.d.ts +9 -0
  31. package/dist/lib/room-lighting-discovery.js +45 -0
  32. package/dist/lib/settings.d.ts +5 -0
  33. package/dist/lib/settings.js +68 -0
  34. package/dist/lib/source-capability-controller.d.ts +32 -35
  35. package/dist/lib/source-capability-controller.js +51 -35
  36. package/dist/lib/switch-policy.d.ts +2 -7
  37. package/dist/lib/target-state.d.ts +24 -0
  38. package/dist/lib/target-state.js +38 -0
  39. package/dist/lib/theatre-activity-controller.d.ts +6 -108
  40. package/dist/lib/theatre-activity-controller.js +67 -318
  41. package/dist/lib/theatre-ha-discovery.d.ts +14 -0
  42. package/dist/lib/theatre-ha-discovery.js +163 -0
  43. package/dist/lib/theatre-metrics.d.ts +4 -0
  44. package/dist/lib/theatre-metrics.js +66 -0
  45. package/dist/lib/theatre-profiles.d.ts +23 -0
  46. package/dist/lib/theatre-profiles.js +73 -0
  47. package/dist/lib/theatre-refresh.d.ts +20 -0
  48. package/dist/lib/theatre-refresh.js +46 -0
  49. package/dist/lib/theatre-slo.js +7 -9
  50. package/dist/lib/theatre-state-contract.d.ts +7 -19
  51. package/dist/lib/theatre-state-contract.js +33 -169
  52. package/dist/lib/theatre-state.d.ts +110 -0
  53. package/dist/lib/theatre-state.js +139 -0
  54. package/dist/lib/wall-input.d.ts +2 -7
  55. package/dist/lib/z2m-light.d.ts +16 -0
  56. package/dist/lib/z2m-light.js +36 -0
  57. package/dist/nodes/activity-lighting-controller.html +5 -3
  58. package/dist/nodes/activity-lighting-controller.js +21 -52
  59. package/dist/nodes/adapter.d.ts +15 -0
  60. package/dist/nodes/adapter.js +60 -0
  61. package/dist/nodes/avr-controller.html +13 -4
  62. package/dist/nodes/avr-controller.js +23 -62
  63. package/dist/nodes/backlighting-controller.html +1 -1
  64. package/dist/nodes/backlighting-controller.js +25 -47
  65. package/dist/nodes/display-mode-controller.html +4 -4
  66. package/dist/nodes/display-mode-controller.js +22 -59
  67. package/dist/nodes/motion-policy.html +23 -10
  68. package/dist/nodes/motion-policy.js +14 -135
  69. package/dist/nodes/occupancy-fusion.html +2 -2
  70. package/dist/nodes/occupancy-fusion.js +23 -29
  71. package/dist/nodes/room-controller.html +92 -34
  72. package/dist/nodes/room-controller.js +46 -64
  73. package/dist/nodes/source-capability-controller.html +4 -13
  74. package/dist/nodes/source-capability-controller.js +17 -52
  75. package/dist/nodes/switch-policy.html +3 -3
  76. package/dist/nodes/switch-policy.js +23 -42
  77. package/dist/nodes/theatre-activity-controller.html +5 -3
  78. package/dist/nodes/theatre-activity-controller.js +23 -43
  79. package/dist/nodes/theatre-state-contract.html +55 -22
  80. package/dist/nodes/theatre-state-contract.js +30 -41
  81. package/examples/Home theatre activity.json +307 -43
  82. package/examples/Home theatre equipment.json +4 -4
  83. package/examples/Room lighting.json +15 -17
  84. package/package.json +3 -7
  85. 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
  [![CI](https://github.com/avantics-hq/node-red-home-automation/actions/workflows/ci.yml/badge.svg)](https://github.com/avantics-hq/node-red-home-automation/actions/workflows/ci.yml)
4
+ [![Docs](https://img.shields.io/badge/docs-GitHub%20Pages-0969da)](https://avantics-hq.github.io/node-red-home-automation/)
4
5
  [![Ko-fi](https://img.shields.io/badge/Ko--fi-support-FF5E5B?logo=ko-fi&logoColor=white)](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
- - **Adaptive lighting with manual ownership:** calculate circadian brightness and colour temperature without immediately overwriting wall-switch changes.
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
- ```mermaid
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
+ ![Architecture diagram. Inputs (wall switches, motion and presence sensors, illuminance, and theatre requests over MQTT) feed two groups of nodes. Room lighting: occupancy-fusion, motion-lighting-policy and switch-lighting-policy feed room-lighting-controller, which sends set commands to the lights (for example Zigbee2MQTT) and reads back their observed state. Theatre activities: theatre-state-contract passes activity intents to theatre-activity-controller and publishes retained theatre state and Home Assistant discovery; your flow routes each theatreCommand by target to the theatre equipment controllers (AVR, display mode, source capability, backlighting), which command the devices, or to activity-lighting-controller, which holds room lights; both report target state back to the activity controller.](https://avantics-hq.github.io/node-red-home-automation/images/architecture.svg)
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,13 +35,19 @@ 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
- Example: [`Room lighting.json`](examples/Room%20lighting.json).
38
+ policy, wall switches into the switch policy, and both policies into the room
39
+ controller. The room controller keeps a light a person set after the room went
40
+ vacant, so motion doesn't switch it straight off. Circadian
41
+ levels follow the room controller's wake and bed times (default 07:00 and
42
+ 23:00): they rise over the hour after waking, stay bright and cool through the
43
+ day, and wind down to the sleep settings over the three hours before bed. Times
44
+ are the Node-RED host's local time.
45
+ Example: [`Room lighting.json`](https://github.com/avantics-hq/node-red-home-automation/blob/main/examples/Room%20lighting.json).
84
46
 
85
47
  | Node | Purpose |
86
48
  |---|---|
87
49
  | `home-occupancy-fusion` | Combines PIR motion with optional presence sensing. |
88
- | `home-motion-lighting-policy` | Converts occupancy and illuminance into lighting intent with observable wall-input baselines. |
50
+ | `home-motion-lighting-policy` | Converts occupancy and illuminance into lighting intent. A `delay` message (in seconds) or `illuminance-cut-off` message overrides its settings until they are changed and redeployed. |
89
51
  | `home-switch-lighting-policy` | Converts raw edges or pre-classified button actions into lighting intent while exposing input freshness and availability. |
90
52
  | `home-room-lighting-controller` | Owns the room's light commands, adaptive styling, manual and held lights, and lighting state. |
91
53
 
@@ -93,7 +55,7 @@ Example: [`Room lighting.json`](examples/Room%20lighting.json).
93
55
 
94
56
  Each controller owns one kind of device. It accepts a generation-scoped intent,
95
57
  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).
58
+ 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
59
 
98
60
  | Node | Purpose |
99
61
  |---|---|
@@ -110,19 +72,24 @@ activity controller plans each target and sends `msg.theatreCommand` messages.
110
72
  Your flow routes each command by its `target` (`avr`, `display`, `source`,
111
73
  `lighting`, `backlighting`) to the matching controller's input
112
74
  (`msg.avr`, `msg.display`, `msg.sourceCapability`, `msg.activityLighting`,
113
- `msg.backlighting`), and wires each controller's output 2 back to the activity
114
- controller. Activity lighting feeds the room lighting controller.
115
- Example: [`Home theatre activity.json`](examples/Home%20theatre%20activity.json).
75
+ `msg.backlighting`); the example does this with one switch node and a change
76
+ node per target. Every controller reports its progress on output 2 as
77
+ `msg.theatre = {kind: "target_state", target, requestId, generation, status,
78
+ observedAt}`, alongside its own events: wire each controller's output 2 straight
79
+ back to the activity controller. The activity controller refreshes settled
80
+ targets, and each controller answers a refresh from fresh readings. Activity
81
+ lighting feeds the room lighting controller.
82
+ Example: [`Home theatre activity.json`](https://github.com/avantics-hq/node-red-home-automation/blob/main/examples/Home%20theatre%20activity.json).
116
83
 
117
84
  | Node | Purpose |
118
85
  |---|---|
119
- | `home-theatre-state-contract` | Bridges non-retained activity requests to retained MQTT truth and HA discovery. |
86
+ | `home-theatre-state-contract` | Bridges non-retained activity requests to retained MQTT truth and, optionally, HA discovery. |
120
87
  | `home-theatre-activity-controller` | Owns latest-request-wins activity state and aggregates target evidence. |
121
88
  | `home-activity-lighting-controller` | Applies activity styles to room lights and holds them while the activity is active. |
122
89
 
123
90
  Pure TypeScript policy modules live in `lib/`; thin Node-RED runtime and editor adapters live in `nodes/`.
124
91
 
125
- Importable example flows for the Node-RED editor live in [`examples/`](examples/) (**Import → Examples**). Executable, deployment-neutral contract examples live in [`contract-examples/`](contract-examples/) and exercise the same reducers as the runtime adapters.
92
+ 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
93
 
127
94
  ## Requirements
128
95
 
@@ -144,11 +111,28 @@ also attaches the package tarball and its SHA-256 checksum. Install a downloaded
144
111
  tarball through **Manage palette → Install → Upload module**, or with:
145
112
 
146
113
  ```sh
147
- npm install /path/to/avantics-node-red-home-automation-2.2.1.tgz
114
+ npm install /path/to/avantics-node-red-home-automation-3.0.0.tgz
148
115
  ```
149
116
 
150
117
  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.
151
118
 
119
+ ### Upgrading from 2.x
120
+
121
+ Version 3.0 changes some behaviour. Check these after upgrading:
122
+
123
+ - **Wall switches go through the switch lighting policy node.** The motion lighting policy no longer handles them,
124
+ and ignores switch messages, so rewire any wall switch that feeds a motion node. The room lighting controller now
125
+ keeps a light that a person set after the room went vacant, instead of the motion node.
126
+ - **The motion node's `delay` message takes seconds,** the same unit as its "Vacancy seconds" setting; it took
127
+ minutes before. A redeploy with a changed vacancy delay or lux cutoff now takes effect.
128
+ - **Settings are checked when you deploy.** An emptied or unusable setting shows "invalid configuration", and the
129
+ node's log says which setting is wrong. A setting your flow saved before it existed still takes its default.
130
+ - **No node assumes a room.** An empty area is allowed, except on the theatre state contract, which needs one to name
131
+ its topics. A room lighting controller with no area still controls its lights, but publishes no state or discovery.
132
+ - **MQTT state publishing and Home Assistant discovery are opt-in** on the room lighting controller and the theatre
133
+ state contract. New nodes start with them off. Nodes in a flow saved before 3.0 keep publishing until you untick
134
+ the setting. Switching discovery off removes the entities from Home Assistant.
135
+
152
136
  The whole-theatre MQTT contract uses non-retained requests at
153
137
  `home/<area>/theatre/request`; every request supplies a unique `requestId` and an
154
138
  explicit configured activity. Controller output is retained under `desired`,
@@ -230,18 +214,23 @@ or a newer owner who takes it over), a person (`manual: true`, an external
230
214
  command on the command topic, or a change seen at the light), or a release.
231
215
  Other intents, such as motion ON or OFF and owner-less values, leave it alone
232
216
  and are traced as `ignored held-by`; their other targets are still handled.
233
- The package's wall-switch policies mark presses with `person: true`. Owner-less
217
+ The switch policy marks presses with `person: true`. Owner-less
234
218
  intents, with or without values, reach a held light only with `manual: true` or
235
219
  `person: true`; a flow acting for a person, such as a dashboard, sets one.
236
- Switching a light off by its owner or a person returns it to `in-control`. On startup
237
- and whenever a target's state changes, the controller sends a retained message
238
- on its command output:
220
+ Switching a light off by its owner or a person returns it to `in-control`. With
221
+ **Publish state over MQTT** on (new nodes start with it off; nodes saved before
222
+ the setting existed keep it on), on startup and whenever a target's state
223
+ changes, the controller sends a retained message on its command output:
239
224
 
240
225
  ```js
241
226
  {topic: "home/<area>/lighting/<target>/control", payload: {state: "overridden", owner: "theatre"}, retain: true}
242
227
  ```
243
228
 
244
229
  Flows that filter that output by topic must pass the `/control` topics through.
230
+ It also publishes the circadian state on `home/<area>/circadian/state`, and
231
+ acts on ON or OFF sent to `home/<area>/circadian/set`, which is where the Home
232
+ Assistant circadian switch sends them: wire an MQTT in node subscribed to that
233
+ topic into the room controller.
245
234
 
246
235
  ## Receiver volume ownership
247
236
 
@@ -262,7 +251,8 @@ from fresh observed volume, accumulates rapid requests against the pending
262
251
  target, applies configured half-step bounds, retries through the existing
263
252
  bounded transport path, and confirms completion from fresh receiver evidence.
264
253
  Once confirmed, it releases the volume target; a later physical or external
265
- adjustment becomes the new baseline instead of being corrected.
254
+ adjustment becomes the new baseline instead of being corrected. A request still pending when Node-RED restarts is reported as
255
+ `volume_failed` with reason `restart`, not replayed.
266
256
 
267
257
  ## Contract examples
268
258
 
@@ -274,13 +264,13 @@ npm run examples
274
264
 
275
265
  | Example | Boundary demonstrated |
276
266
  |---|---|
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. |
267
+ | [`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. |
268
+ | [`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. |
269
+ | [`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. |
270
+ | [`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. |
271
+ | [`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. |
272
+ | [`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. |
273
+ | [`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
274
 
285
275
  Each example uses synthetic identifiers and configuration, asserts its central invariant, and prints the complete input/output contract as JSON.
286
276
 
@@ -291,15 +281,7 @@ The repository contains no home-network addresses, deployment coordinates, or li
291
281
  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
282
 
293
283
  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.
284
+ [`SECURITY.md`](https://github.com/avantics-hq/node-red-home-automation/blob/main/SECURITY.md), not through a public issue.
303
285
 
304
286
  ## Development
305
287
 
@@ -309,22 +291,27 @@ npm test
309
291
  npm run examples
310
292
  ```
311
293
 
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.
294
+ 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
295
 
314
296
  ## Contributing
315
297
 
316
- Contributions are welcome: see [CONTRIBUTING.md](CONTRIBUTING.md) and the
317
- [Code of Conduct](CODE_OF_CONDUCT.md).
298
+ Contributions are welcome: see [CONTRIBUTING.md](https://github.com/avantics-hq/node-red-home-automation/blob/main/CONTRIBUTING.md) and the
299
+ [Code of Conduct](https://github.com/avantics-hq/node-red-home-automation/blob/main/CODE_OF_CONDUCT.md).
300
+
301
+ ## Inspiration and thanks
318
302
 
319
- ## Acknowledgements
303
+ - [Node-RED](https://nodered.org): the platform these nodes run on.
304
+ - [Home Assistant](https://www.home-assistant.io): the nodes publish state and
305
+ discovery to it as a read-only view.
306
+ - [Zigbee2MQTT](https://www.zigbee2mqtt.io): the light transport the room
307
+ controller and the examples are designed around.
308
+ - [Adaptive Lighting](https://github.com/basnijholt/adaptive-lighting) by
309
+ basnijholt: the inspiration for circadian lighting in Node-RED.
320
310
 
321
- The circadian sun-position curve and the brightness and colour-temperature rules
322
- in the room lighting controller are adapted from
323
- [Adaptive Lighting](https://github.com/basnijholt/adaptive-lighting) by
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.
311
+ BRAVIA is a trademark of Sony Group Corporation. Node-RED, Home Assistant,
312
+ Zigbee2MQTT and the other names here are trademarks of their respective owners.
313
+ This project is not affiliated with or endorsed by any of them.
327
314
 
328
315
  ## License
329
316
 
330
- [MIT](LICENSE)
317
+ [MIT](https://github.com/avantics-hq/node-red-home-automation/blob/main/LICENSE)
@@ -1,3 +1,5 @@
1
+ import {scheduledLighting} from "../dist/lib/circadian.js";
2
+ import {resolveSchedule} from "../dist/lib/circadian-schedule.js";
1
3
  import {transitionRoom} from "../dist/lib/room-controller.js";
2
4
  import {assert, runWhenMain} from "./_shared.mjs";
3
5
 
@@ -13,17 +15,18 @@ export function run() {
13
15
  commandTopic: "demo/lighting/ceiling/set"
14
16
  }],
15
17
  initialSleep: false,
16
- latitude: 0,
17
- longitude: 0,
18
- sleepBrightnessPct: 5,
19
- maxBrightnessPct: 100,
20
- minBrightnessPct: 10,
21
- sleepColorTempKelvin: 1800,
22
- minColorTempKelvin: 2200,
23
- maxColorTempKelvin: 5000,
24
18
  refreshTransitionSeconds: 30,
25
19
  initialTransitionSeconds: 1
26
20
  };
21
+ const schedule = resolveSchedule("07:00", "23:00");
22
+ // The node builds the circadian lighting from its schedule and light
23
+ // settings; the room controller only asks it how a light should look.
24
+ configuration.lighting = scheduledLighting(schedule, {
25
+ sleepBrightnessPct: 5,
26
+ maxBrightnessPct: 100,
27
+ sleepColorTempKelvin: 1800,
28
+ maxColorTempKelvin: 5000
29
+ });
27
30
  const input = {
28
31
  lighting: {
29
32
  kind: "intent",
@@ -49,7 +52,7 @@ export function run() {
49
52
  configuration: {
50
53
  area: configuration.area,
51
54
  targets: configuration.targets.map(({id}) => id),
52
- solarCoordinates: "deployment-supplied"
55
+ schedule
53
56
  },
54
57
  inputs: [input],
55
58
  outputs,
@@ -1,4 +1,6 @@
1
+ import type { TraceEntry } from "./bounded-trace";
1
2
  import type { LightingEvent } from "./room-controller";
3
+ import { type TargetStateEvent } from "./target-state";
2
4
  export interface ActivityLightingProfile {
3
5
  targets: string[];
4
6
  values: Record<string, unknown>;
@@ -85,26 +87,17 @@ export interface ActivityLightingCommand {
85
87
  lighting?: LightingEvent;
86
88
  }
87
89
  export interface ActivityLightingOutputEvent {
88
- theatre: {
89
- kind: "target_state";
90
- target: "lighting";
91
- requestId: string;
92
- generation: number;
93
- status: string;
94
- observedAt: string;
95
- details: {
96
- profile: string;
97
- matched: number;
98
- preserved: number;
99
- expected: number;
100
- metrics: {
101
- attempts: number;
102
- retries: number;
103
- fallbackUsed: boolean;
104
- };
90
+ theatre: TargetStateEvent<"lighting", {
91
+ profile: string;
92
+ matched: number;
93
+ preserved: number;
94
+ expected: number;
95
+ metrics: {
96
+ attempts: number;
97
+ retries: number;
98
+ fallbackUsed: boolean;
105
99
  };
106
- failure?: ActivityLightingFailure;
107
- };
100
+ }, ActivityLightingFailure>;
108
101
  }
109
102
  export declare function normaliseLightingProfiles(value: unknown): ActivityLightingProfiles;
110
103
  export declare function normaliseActivityLightingConfig(config?: Partial<ActivityLightingConfig> & {
@@ -116,12 +109,7 @@ export declare function transitionActivityLighting(saved: Partial<ActivityLighti
116
109
  state: ActivityLightingState;
117
110
  commands: ActivityLightingCommand[];
118
111
  events: ActivityLightingOutputEvent[];
119
- traces: {
120
- event: string;
121
- outcome: string;
122
- reason: string | null;
123
- details: Record<string, unknown>;
124
- }[];
112
+ traces: TraceEntry<Record<string, unknown>>[];
125
113
  status: {
126
114
  phase: string;
127
115
  text: string;
@@ -3,7 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.normaliseLightingProfiles = normaliseLightingProfiles;
4
4
  exports.normaliseActivityLightingConfig = normaliseActivityLightingConfig;
5
5
  exports.transitionActivityLighting = transitionActivityLighting;
6
+ const settings_1 = require("./settings");
6
7
  const observation_time_1 = require("./observation-time");
8
+ const target_state_1 = require("./target-state");
7
9
  const PROFILE_NAME = /^[a-z][a-z0-9_-]*$/;
8
10
  const TARGET_NAME = /^[a-z][a-z0-9_-]*$/;
9
11
  function stable(value) {
@@ -52,12 +54,11 @@ function normaliseLightingProfiles(value) {
52
54
  return profiles;
53
55
  }
54
56
  function normaliseActivityLightingConfig(config = {}) {
55
- const owner = typeof config.owner === "string" ? config.owner.trim() : "";
56
57
  return {
57
58
  profiles: normaliseLightingProfiles(config.profiles),
58
- owner: owner || "activity",
59
- confirmationTimeoutMs: Math.max(1000, Number(config.confirmationTimeoutMs) || 15000),
60
- maxAttempts: Math.max(1, Math.floor(Number(config.maxAttempts) || 2))
59
+ owner: (0, settings_1.textSetting)(config.owner, "owner", "activity"),
60
+ confirmationTimeoutMs: Math.max(1000, (0, settings_1.numberSetting)(config.confirmationTimeoutMs, "confirmation timeout", 15000)),
61
+ maxAttempts: Math.max(1, Math.floor((0, settings_1.numberSetting)(config.maxAttempts, "max attempts", 2)))
61
62
  };
62
63
  }
63
64
  function initialState(saved = {}, profiles = {}) {
@@ -333,6 +334,11 @@ function transitionActivityLighting(saved, msg, suppliedConfig = {}, nowValue =
333
334
  else {
334
335
  commands.push(commandFor(state, config, "observe", 0));
335
336
  state.phase = "confirming";
337
+ // After a restart nothing else would end an unanswered observe: give
338
+ // it a deadline, so it re-applies and then fails as a live request.
339
+ if (event.kind === "startup" && state.blockedGeneration !== state.generation) {
340
+ state.pending = { attempt: 0, deadlineAt: nowMs + config.confirmationTimeoutMs };
341
+ }
336
342
  trace(event.kind, "observing", null, { profile: state.desired.profile });
337
343
  }
338
344
  }
@@ -344,12 +350,11 @@ function transitionActivityLighting(saved, msg, suppliedConfig = {}, nowValue =
344
350
  throw new Error("unknown activity lighting event kind");
345
351
  }
346
352
  if (state.desired) {
347
- const theatre = {
348
- kind: "target_state",
353
+ const theatre = (0, target_state_1.targetStateEvent)({
349
354
  target: "lighting",
350
355
  requestId: state.desired.requestId,
351
356
  generation: state.generation,
352
- status: state.phase === "failed" ? "failed" : state.phase === "ready" ? "ready" : state.phase,
357
+ status: (0, target_state_1.asTargetStatus)(state.phase, "confirming"),
353
358
  observedAt: now.toISOString(),
354
359
  details: {
355
360
  profile: state.desired.profile,
@@ -361,10 +366,9 @@ function transitionActivityLighting(saved, msg, suppliedConfig = {}, nowValue =
361
366
  retries: Math.max(0, (state.pending?.attempt ?? 1) - 1),
362
367
  fallbackUsed: false
363
368
  }
364
- }
365
- };
366
- if (state.failure)
367
- theatre.failure = { ...state.failure };
369
+ },
370
+ failure: state.failure
371
+ });
368
372
  events.push({ theatre });
369
373
  }
370
374
  return { state, commands, events, traces, status: statusFor(state) };
@@ -1,6 +1,7 @@
1
- export declare const QUERY_COMMAND = "PW?\rZM?\rSI?\rMV?\r";
2
- export type AvrPower = "on" | "standby";
3
- export type MainZone = "on" | "off";
1
+ import type { TraceEntry } from "./bounded-trace";
2
+ import { type TargetStateEvent } from "./target-state";
3
+ import { type AvrPower, type MainZone } from "./denon-protocol";
4
+ export { QUERY_COMMAND, formatVolumeCommand, parseAvrLine, type AvrPower, type MainZone, type ParsedAvrLine } from "./denon-protocol";
4
5
  export type AvrField = "power" | "mainZone" | "source" | "volume";
5
6
  export interface AvrConfig {
6
7
  area: string;
@@ -54,6 +55,7 @@ export interface AvrFailure {
54
55
  export interface AvrState {
55
56
  observed: AvrObserved;
56
57
  seen: Record<AvrField, boolean>;
58
+ fieldObservedAt: Record<AvrField, string | null>;
57
59
  desired: AvrDesired | null;
58
60
  generation: number;
59
61
  pending: AvrPending | null;
@@ -100,6 +102,12 @@ export interface AvrCommand {
100
102
  attempt: number;
101
103
  };
102
104
  }
105
+ export type AvrTargetState = TargetStateEvent<"avr", Record<string, unknown>, {
106
+ operation: string;
107
+ reason: string;
108
+ attempts: number;
109
+ occurredAt: string;
110
+ }>;
103
111
  export interface AvrOutputEvent {
104
112
  payload?: unknown;
105
113
  avr: {
@@ -108,37 +116,18 @@ export interface AvrOutputEvent {
108
116
  [key: string]: unknown;
109
117
  };
110
118
  }
111
- export type ParsedAvrLine = {
112
- field: "power";
113
- value: AvrPower;
114
- line: string;
115
- } | {
116
- field: "mainZone";
117
- value: MainZone;
118
- line: string;
119
- } | {
120
- field: "source";
121
- value: string | null;
122
- sourceCode: string;
123
- line: string;
124
- } | {
125
- field: "volume";
126
- value: number | null;
127
- line: string;
128
- };
129
119
  export declare function createAvrState(_config: AvrConfig, saved?: Partial<AvrState>): AvrState;
130
- export declare function formatVolumeCommand(value: number): string;
131
- export declare function parseAvrLine(value: unknown, sources: Record<string, string>): ParsedAvrLine | null;
120
+ export declare function normaliseAvrConfig(rawConfig: {
121
+ [K in keyof AvrConfig]?: unknown;
122
+ }): AvrConfig;
132
123
  export declare function transitionAvr(saved: Partial<AvrState> | undefined, msg: AvrMessage, rawConfig: Partial<AvrConfig>, nowValue?: Date | string | number): {
133
124
  state: AvrState;
134
125
  commands: AvrCommand[];
135
126
  events: AvrOutputEvent[];
136
- traces: {
137
- event: string;
138
- outcome: string;
139
- reason: string | null;
140
- details: Record<string, unknown>;
127
+ targetStates: {
128
+ theatre: AvrTargetState;
141
129
  }[];
130
+ traces: TraceEntry<Record<string, unknown>>[];
142
131
  status: {
143
132
  phase: string;
144
133
  text: string;