@avantics/node-red-home-automation 4.0.3 → 6.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 (116) hide show
  1. package/README.md +447 -64
  2. package/contract-examples/room-lighting.mjs +39 -27
  3. package/contract-examples/switch-gestures.mjs +1 -1
  4. package/contract-examples/volume-ownership.mjs +6 -5
  5. package/dist/lib/activity-lighting-controller.d.ts +9 -2
  6. package/dist/lib/activity-lighting-controller.js +107 -19
  7. package/dist/lib/adopted-request.d.ts +17 -0
  8. package/dist/lib/adopted-request.js +61 -7
  9. package/dist/lib/avr-controller.d.ts +9 -3
  10. package/dist/lib/avr-controller.js +71 -24
  11. package/dist/lib/avr-publishing.d.ts +3 -3
  12. package/dist/lib/avr-publishing.js +11 -5
  13. package/dist/lib/backlighting-controller.d.ts +2 -0
  14. package/dist/lib/backlighting-controller.js +27 -7
  15. package/dist/lib/circadian-policy.d.ts +47 -0
  16. package/dist/lib/circadian-policy.js +111 -0
  17. package/dist/lib/circadian-publishing.d.ts +33 -0
  18. package/dist/lib/circadian-publishing.js +154 -0
  19. package/dist/lib/circadian-schedule.d.ts +4 -0
  20. package/dist/lib/circadian-schedule.js +12 -0
  21. package/dist/lib/command-echo.d.ts +8 -1
  22. package/dist/lib/command-echo.js +37 -6
  23. package/dist/lib/cover-controller.js +4 -0
  24. package/dist/lib/cover-publishing.js +1 -1
  25. package/dist/lib/denon-protocol.d.ts +5 -1
  26. package/dist/lib/denon-protocol.js +27 -11
  27. package/dist/lib/display-mode-controller.d.ts +12 -5
  28. package/dist/lib/display-mode-controller.js +181 -87
  29. package/dist/lib/enable-input.d.ts +3 -0
  30. package/dist/lib/enable-input.js +18 -0
  31. package/dist/lib/house-schedule-publishing.js +1 -1
  32. package/dist/lib/house-schedule.js +2 -2
  33. package/dist/lib/light-contract.d.ts +53 -0
  34. package/dist/lib/light-contract.js +11 -0
  35. package/dist/lib/light-styling.d.ts +2 -2
  36. package/dist/lib/light-styling.js +5 -4
  37. package/dist/lib/motion-policy.d.ts +0 -1
  38. package/dist/lib/motion-policy.js +9 -8
  39. package/dist/lib/motion-publishing.js +3 -2
  40. package/dist/lib/mqtt-message.d.ts +6 -0
  41. package/dist/lib/mqtt-message.js +21 -0
  42. package/dist/lib/occupancy-publishing.js +1 -1
  43. package/dist/lib/power-reading.d.ts +5 -0
  44. package/dist/lib/power-reading.js +13 -0
  45. package/dist/lib/room-controller.d.ts +39 -29
  46. package/dist/lib/room-controller.js +314 -192
  47. package/dist/lib/room-lighting-discovery.d.ts +0 -14
  48. package/dist/lib/room-lighting-discovery.js +20 -126
  49. package/dist/lib/room-publishing.d.ts +10 -12
  50. package/dist/lib/room-publishing.js +65 -125
  51. package/dist/lib/shelly-cover.d.ts +44 -0
  52. package/dist/lib/shelly-cover.js +153 -0
  53. package/dist/lib/shelly-gen2.d.ts +25 -0
  54. package/dist/lib/shelly-gen2.js +58 -0
  55. package/dist/lib/shelly-switch.d.ts +95 -0
  56. package/dist/lib/shelly-switch.js +237 -0
  57. package/dist/lib/source-capability-controller.d.ts +2 -0
  58. package/dist/lib/source-capability-controller.js +27 -6
  59. package/dist/lib/switch-policy.d.ts +1 -1
  60. package/dist/lib/switch-policy.js +1 -1
  61. package/dist/lib/theatre-requests.d.ts +29 -0
  62. package/dist/lib/theatre-requests.js +89 -0
  63. package/dist/lib/theatre-state-contract.d.ts +5 -22
  64. package/dist/lib/theatre-state-contract.js +30 -77
  65. package/dist/lib/toggle-power.d.ts +41 -0
  66. package/dist/lib/toggle-power.js +74 -0
  67. package/dist/lib/topic-names.d.ts +1 -0
  68. package/dist/lib/topic-names.js +7 -0
  69. package/dist/lib/{display-transport.d.ts → transports/display-transport.d.ts} +1 -1
  70. package/dist/lib/{display-transport.js → transports/display-transport.js} +32 -7
  71. package/dist/lib/z2m-cover.d.ts +49 -0
  72. package/dist/lib/z2m-cover.js +120 -0
  73. package/dist/lib/z2m-light-adapter.d.ts +62 -0
  74. package/dist/lib/z2m-light-adapter.js +195 -0
  75. package/dist/lib/z2m-light.d.ts +8 -0
  76. package/dist/lib/z2m-light.js +61 -3
  77. package/dist/nodes/activity-lighting-controller.html +20 -3
  78. package/dist/nodes/activity-lighting-controller.js +21 -0
  79. package/dist/nodes/avr-controller.html +25 -6
  80. package/dist/nodes/avr-controller.js +16 -6
  81. package/dist/nodes/circadian-policy.d.ts +3 -0
  82. package/dist/nodes/circadian-policy.html +140 -0
  83. package/dist/nodes/circadian-policy.js +162 -0
  84. package/dist/nodes/cover-controller.html +4 -3
  85. package/dist/nodes/display-mode-controller.html +58 -11
  86. package/dist/nodes/display-mode-controller.js +19 -6
  87. package/dist/nodes/house-schedule.html +3 -3
  88. package/dist/nodes/motion-policy.html +5 -2
  89. package/dist/nodes/motion-policy.js +3 -0
  90. package/dist/nodes/refused-enable.d.ts +7 -0
  91. package/dist/nodes/refused-enable.js +24 -0
  92. package/dist/nodes/room-controller.html +215 -82
  93. package/dist/nodes/room-controller.js +71 -83
  94. package/dist/nodes/shelly-cover.d.ts +3 -0
  95. package/dist/nodes/shelly-cover.html +65 -0
  96. package/dist/nodes/shelly-cover.js +34 -0
  97. package/dist/nodes/shelly-switch.d.ts +3 -0
  98. package/dist/nodes/shelly-switch.html +82 -0
  99. package/dist/nodes/shelly-switch.js +48 -0
  100. package/dist/nodes/theatre-state-contract.html +4 -3
  101. package/dist/nodes/z2m-cover.d.ts +3 -0
  102. package/dist/nodes/z2m-cover.html +68 -0
  103. package/dist/nodes/z2m-cover.js +41 -0
  104. package/dist/nodes/z2m-light.d.ts +3 -0
  105. package/dist/nodes/z2m-light.html +72 -0
  106. package/dist/nodes/z2m-light.js +50 -0
  107. package/examples/Cover control.json +24 -56
  108. package/examples/Home theatre activity.json +54 -9
  109. package/examples/Home theatre equipment.json +4 -4
  110. package/examples/House schedule.json +106 -24
  111. package/examples/Room lighting.json +54 -11
  112. package/examples/Shelly cover.json +252 -0
  113. package/examples/Shelly switch.json +197 -0
  114. package/package.json +13 -4
  115. package/dist/lib/room-schedule.d.ts +0 -8
  116. package/dist/lib/room-schedule.js +0 -41
@@ -6,6 +6,8 @@ exports.transitionDisplay = transitionDisplay;
6
6
  const adopted_request_1 = require("./adopted-request");
7
7
  const settings_1 = require("./settings");
8
8
  const observation_time_1 = require("./observation-time");
9
+ const power_reading_1 = require("./power-reading");
10
+ const toggle_power_1 = require("./toggle-power");
9
11
  const target_state_1 = require("./target-state");
10
12
  const MODES = { off: true, tv: true, projector: true };
11
13
  const DEVICES = ["tv", "screen", "projector"];
@@ -28,19 +30,42 @@ function initialState(saved = {}) {
28
30
  ? saved.lastAttempt
29
31
  : 0,
30
32
  fallbackUsed: saved.fallbackUsed === true,
31
- adoptedGeneration: (0, adopted_request_1.savedAdoptedGeneration)(saved)
33
+ adoptedGeneration: (0, adopted_request_1.savedAdoptedGeneration)(saved),
34
+ carried: (0, adopted_request_1.savedCarried)(saved),
35
+ ...(saved.discovery && Number.isFinite(saved.discovery.nextAt) ? { discovery: { ...saved.discovery } } : {})
32
36
  };
33
37
  }
38
+ // The projector's power from its meter, with the display's own bands.
34
39
  function classifyProjectorPower(watts, previous = "unknown", config = {}) {
35
- const onWatts = Number(config.projectorOnWatts ?? 20);
36
- const offWatts = Number(config.projectorOffWatts ?? 10);
37
- if (!Number.isFinite(watts) || watts < 0)
38
- return "unknown";
39
- if (watts >= onWatts)
40
- return "on";
41
- if (watts <= offWatts)
42
- return "standby";
43
- return previous === "on" || previous === "standby" ? previous : "transitioning";
40
+ return (0, power_reading_1.classifyPower)(watts, previous, {
41
+ onWatts: Number(config.projectorOnWatts ?? 20),
42
+ offWatts: Number(config.projectorOffWatts ?? 10)
43
+ });
44
+ }
45
+ // What a projector power reading says: a meter's watts, classified against
46
+ // the on and standby watts, or the state itself from any adapter. A
47
+ // "transitioning" lamp is read as a reading between the two would be: it
48
+ // keeps its last settled state, or is transitioning if it has none. Given
49
+ // both, the state wins and the watts are kept for reporting.
50
+ const PROJECTOR_POWER_STATES = ["on", "standby", "transitioning"];
51
+ function projectorReading(event, previous, config) {
52
+ const watts = event.watts === undefined ? null : Number(event.watts);
53
+ if (watts !== null && (!Number.isFinite(watts) || watts < 0))
54
+ throw new Error("invalid projector power observation");
55
+ if (event.power === undefined) {
56
+ if (watts === null)
57
+ throw new Error("invalid projector power observation");
58
+ return { watts, power: classifyProjectorPower(watts, previous, config) };
59
+ }
60
+ // Lower case only. A smart plug's own ON and OFF are its relay, not the
61
+ // lamp: a projector in standby on a switched-on plug would read as on, and
62
+ // a request for TV mode would then toggle it on. So they fail as invalid.
63
+ const power = typeof event.power === "string" ? event.power.trim() : "";
64
+ if (!PROJECTOR_POWER_STATES.includes(power)) {
65
+ throw new Error("projector power must be on, standby or transitioning");
66
+ }
67
+ const settled = previous === "on" || previous === "standby" ? previous : "transitioning";
68
+ return { watts, power: power === "transitioning" ? settled : power };
44
69
  }
45
70
  function statusFor(state) {
46
71
  const mode = state.desired?.mode || "none";
@@ -78,31 +103,48 @@ function allSeen(state) {
78
103
  return DEVICES.every((device) => state.seen[device]);
79
104
  }
80
105
  function screenAtPosition(observed, target, tolerance) {
81
- if (!observed || !Number.isFinite(observed.position))
106
+ if (!observed || observed.position === null || !Number.isFinite(observed.position))
82
107
  return false;
83
108
  if (observed.state === "opening" || observed.state === "closing")
84
109
  return false;
85
110
  return Math.abs(observed.position - target) <= tolerance;
86
111
  }
112
+ // Closed: at 0, or reported closed.
113
+ function screenClosed(observed, config) {
114
+ return observed?.state === "closed" || screenAtPosition(observed, 0, config.screenTolerance);
115
+ }
116
+ // Open for projector mode: at the configured position, or with none
117
+ // configured, reported fully open (or stopped at the top).
118
+ function screenOpen(observed, config) {
119
+ if (config.screenOpenPosition !== null)
120
+ return screenAtPosition(observed, config.screenOpenPosition, config.screenTolerance);
121
+ return observed?.state === "open" || screenAtPosition(observed, 100, config.screenTolerance);
122
+ }
87
123
  function operationSatisfied(operation, observed, config) {
88
124
  switch (operation) {
89
125
  case "tv_on": return observed.tv?.power === "on";
90
126
  case "tv_standby": return observed.tv?.power === "standby";
91
- case "screen_open": return screenAtPosition(observed.screen, config.screenOpenPosition, config.screenTolerance);
92
- case "screen_close": return screenAtPosition(observed.screen, 0, config.screenTolerance);
93
- case "projector_start": return observed.projector?.power === "on";
94
- case "projector_settle": return observed.projector?.power === "on" || observed.projector?.power === "standby";
95
- case "projector_shutdown_observe":
96
- case "projector_shutdown_confirm":
97
- case "projector_cooldown": return observed.projector?.power === "standby";
98
- default: return false;
127
+ case "screen_open": return screenOpen(observed.screen, config);
128
+ case "screen_close": return screenClosed(observed.screen, config);
129
+ default: return (0, toggle_power_1.togglePowerSatisfied)(operation, observed.projector?.power) ?? false;
99
130
  }
100
131
  }
132
+ // Where projector mode opens the screen: a percentage above 0, or nothing
133
+ // (empty, missing) for the screen's own open command. There is no default
134
+ // position: each screen has its own.
135
+ function screenOpenPositionSetting(value) {
136
+ if (value === undefined || value === null || (typeof value === "string" && value.trim() === ""))
137
+ return null;
138
+ const position = (0, settings_1.numberSetting)(value, "screen open position", NaN);
139
+ if (!(position > 0 && position <= 100))
140
+ throw new Error("screen open position must be above 0 and at most 100, or empty");
141
+ return position;
142
+ }
101
143
  // Settings may be editor strings; missing or unusable numbers take defaults.
102
144
  function normaliseConfig(config = {}) {
103
145
  const projectorWarmupMs = Math.max(1000, (0, settings_1.numberSetting)(config.projectorWarmupMs, "projector warm-up", 60000));
104
146
  return {
105
- screenOpenPosition: (0, settings_1.numberSetting)(config.screenOpenPosition, "screen open position", 82),
147
+ screenOpenPosition: screenOpenPositionSetting(config.screenOpenPosition),
106
148
  screenTolerance: Math.max(0, (0, settings_1.numberSetting)(config.screenTolerance, "screen tolerance", 2)),
107
149
  pollMs: Math.max(250, (0, settings_1.numberSetting)(config.pollMs, "poll interval", 2000)),
108
150
  queryTimeoutMs: Math.max(1000, (0, settings_1.numberSetting)(config.queryTimeoutMs, "query timeout", 5000)),
@@ -124,19 +166,28 @@ function modeSatisfied(state, config) {
124
166
  if (state.desired.mode === "tv") {
125
167
  return state.observed.tv?.power === "on" &&
126
168
  state.observed.projector?.power === "standby" &&
127
- screenAtPosition(state.observed.screen, 0, config.screenTolerance);
169
+ screenClosed(state.observed.screen, config);
128
170
  }
129
171
  if (state.desired.mode === "projector") {
130
172
  return state.observed.tv?.power === "standby" &&
131
173
  state.observed.projector?.power === "on" &&
132
- screenAtPosition(state.observed.screen, config.screenOpenPosition, config.screenTolerance);
174
+ screenOpen(state.observed.screen, config);
133
175
  }
134
176
  return state.observed.tv?.power === "standby" &&
135
177
  state.observed.projector?.power === "standby" &&
136
- screenAtPosition(state.observed.screen, 0, config.screenTolerance);
178
+ screenClosed(state.observed.screen, config);
137
179
  }
138
180
  function transitionDisplay(saved, msg, suppliedConfig = {}, now = new Date()) {
139
181
  const config = normaliseConfig(suppliedConfig);
182
+ // The projector's toggle-only power, with the display's own timings.
183
+ const toggleTiming = {
184
+ pollMs: config.pollMs,
185
+ queryTimeoutMs: config.queryTimeoutMs,
186
+ warmupMs: config.projectorWarmupMs,
187
+ minimumWarmupMs: config.projectorMinimumWarmupMs,
188
+ shutdownConfirmMs: config.projectorShutdownConfirmMs,
189
+ cooldownMs: config.projectorCooldownMs
190
+ };
140
191
  const state = initialState(saved);
141
192
  const previousPhase = state.phase;
142
193
  // Set when a device reading is stored: only then may a settled display
@@ -152,6 +203,7 @@ function transitionDisplay(saved, msg, suppliedConfig = {}, now = new Date()) {
152
203
  }
153
204
  function fail(device, operation, reason) {
154
205
  state.pending = null;
206
+ delete state.discovery;
155
207
  state.phase = "failed";
156
208
  state.blockedGeneration = state.desired?.generation || state.generation;
157
209
  state.failure = { device, operation, reason, occurredAt: iso(now) };
@@ -196,27 +248,23 @@ function transitionDisplay(saved, msg, suppliedConfig = {}, now = new Date()) {
196
248
  const operation = power === "on" ? "tv_on" : "tv_standby";
197
249
  issue("tv", operation, commandFor(state, "tv", "set_power", { power }), config.tvTimeoutMs);
198
250
  }
199
- function issueScreen(position) {
200
- const operation = position === 0 ? "screen_close" : "screen_open";
201
- issue("screen", operation, commandFor(state, "screen", "set_position", { position }), config.screenTimeoutMs);
251
+ // The screen open or closed. Opening with an open position configured, the
252
+ // command carries the position; otherwise the transport uses the screen's
253
+ // own open or close. Closing always runs to the end, which also restores a
254
+ // position the screen has lost.
255
+ function issueScreen(open) {
256
+ const position = open && config.screenOpenPosition !== null ? { position: config.screenOpenPosition } : {};
257
+ issue("screen", open ? "screen_open" : "screen_close", commandFor(state, "screen", open ? "open" : "close", position), config.screenTimeoutMs);
202
258
  }
203
259
  function issueProjectorStart() {
204
- issue("projector", "projector_start", commandFor(state, "projector", "power_toggle", { reason: "start" }), config.projectorWarmupMs, false);
260
+ issue("projector", toggle_power_1.TOGGLE_START, commandFor(state, "projector", "power_toggle", { reason: "start" }), toggleTiming.warmupMs, false);
205
261
  if (!state.pending)
206
262
  throw new Error("projector start did not create pending state");
207
- state.pending.readyAt = now.getTime() + config.projectorMinimumWarmupMs;
263
+ state.pending.readyAt = (0, toggle_power_1.startReadyAt)(now.getTime(), toggleTiming);
208
264
  }
209
265
  function issueProjectorShutdown() {
210
266
  const command = commandFor(state, "projector", "power_toggle", { reason: "shutdown-request" });
211
- state.pending = {
212
- device: "projector",
213
- operation: "projector_shutdown_observe",
214
- command,
215
- attempt: 1,
216
- retryable: false,
217
- deadlineAt: now.getTime() + config.projectorShutdownConfirmMs + config.queryTimeoutMs,
218
- nextAt: now.getTime() + config.projectorShutdownConfirmMs
219
- };
267
+ state.pending = (0, toggle_power_1.shutdownRequestPending)("projector", command, now.getTime(), toggleTiming);
220
268
  state.phase = "transitioning";
221
269
  commands.push(command);
222
270
  trace("command", "emitted", null, { device: "projector", operation: "shutdown-request", attempt: 1 });
@@ -226,6 +274,8 @@ function transitionDisplay(saved, msg, suppliedConfig = {}, now = new Date()) {
226
274
  }
227
275
  function beginDiscovery() {
228
276
  state.phase = "discovering";
277
+ if (!state.discovery)
278
+ state.discovery = { attempt: 1, nextAt: now.getTime() + config.queryTimeoutMs };
229
279
  for (const device of DEVICES) {
230
280
  if (!state.seen[device] && !state.querying[device]) {
231
281
  state.querying[device] = true;
@@ -239,7 +289,7 @@ function transitionDisplay(saved, msg, suppliedConfig = {}, now = new Date()) {
239
289
  function awaitProjectorPower() {
240
290
  state.pending = {
241
291
  device: "projector",
242
- operation: "projector_settle",
292
+ operation: toggle_power_1.TOGGLE_SETTLE,
243
293
  command: queryFor(state, "projector"),
244
294
  attempt: 1,
245
295
  retryable: false,
@@ -286,8 +336,8 @@ function transitionDisplay(saved, msg, suppliedConfig = {}, now = new Date()) {
286
336
  return issueProjectorShutdown();
287
337
  return awaitProjectorPower();
288
338
  }
289
- if (!screenAtPosition(state.observed.screen, 0, config.screenTolerance))
290
- return issueScreen(0);
339
+ if (!screenClosed(state.observed.screen, config))
340
+ return issueScreen(false);
291
341
  return markReady();
292
342
  }
293
343
  // Make before break, as TV mode does: the projector warms up and the
@@ -299,8 +349,16 @@ function transitionDisplay(saved, msg, suppliedConfig = {}, now = new Date()) {
299
349
  return issueProjectorStart();
300
350
  return awaitProjectorPower();
301
351
  }
302
- if (!screenAtPosition(state.observed.screen, config.screenOpenPosition, config.screenTolerance)) {
303
- return issueScreen(config.screenOpenPosition);
352
+ if (!screenOpen(state.observed.screen, config)) {
353
+ // A position to go to needs a screen that knows where it is: close it
354
+ // first, which restores a lost position; one still without a
355
+ // position once closed isn't calibrated for positions.
356
+ if (config.screenOpenPosition !== null && state.observed.screen?.position === null) {
357
+ if (!screenClosed(state.observed.screen, config))
358
+ return issueScreen(false);
359
+ return fail("screen", "screen_open", "screen-not-calibrated");
360
+ }
361
+ return issueScreen(true);
304
362
  }
305
363
  if (state.observed.tv?.power !== "standby")
306
364
  return issueTv("standby");
@@ -313,15 +371,15 @@ function transitionDisplay(saved, msg, suppliedConfig = {}, now = new Date()) {
313
371
  return issueProjectorShutdown();
314
372
  return awaitProjectorPower();
315
373
  }
316
- if (!screenAtPosition(state.observed.screen, 0, config.screenTolerance))
317
- return issueScreen(0);
374
+ if (!screenClosed(state.observed.screen, config))
375
+ return issueScreen(false);
318
376
  return markReady();
319
377
  }
320
378
  function acceptObservation(event) {
321
379
  const device = event.device;
322
380
  if (!DEVICES.includes(device))
323
381
  throw new Error("unknown display observation device");
324
- if (Number.isInteger(event.generation) && state.desired && event.generation !== state.desired.generation) {
382
+ if (Number.isInteger(event.generation) && state.desired && !(0, adopted_request_1.answersRequest)(state.carried, state.desired.generation, event.generation)) {
325
383
  trace("observation", "ignored", "stale-generation", { device, generation: event.generation });
326
384
  return;
327
385
  }
@@ -344,22 +402,16 @@ function transitionDisplay(saved, msg, suppliedConfig = {}, now = new Date()) {
344
402
  state.observed.tv = { power: event.power, observedAt, source };
345
403
  }
346
404
  else if (device === "screen") {
347
- const position = Number(event.position);
348
- if (!Number.isFinite(position) || position < 0 || position > 100)
405
+ // A position, or none (a screen that doesn't know it): then only its
406
+ // state says whether it is open or closed.
407
+ const position = event.position === undefined || event.position === null ? null : Number(event.position);
408
+ if (position !== null && (!Number.isFinite(position) || position < 0 || position > 100))
349
409
  throw new Error("invalid screen position");
350
410
  state.observed.screen = { position, state: event.state || "unknown", observedAt, source };
351
411
  }
352
412
  else {
353
- const watts = Number(event.watts);
354
- if (!Number.isFinite(watts) || watts < 0)
355
- throw new Error("invalid projector power observation");
356
413
  const previous = state.observed.projector?.power || "unknown";
357
- state.observed.projector = {
358
- watts,
359
- power: classifyProjectorPower(watts, previous, config),
360
- observedAt,
361
- source
362
- };
414
+ state.observed.projector = { ...projectorReading(event, previous, config), observedAt, source };
363
415
  }
364
416
  state.seen[device] = true;
365
417
  observationAccepted = true;
@@ -375,37 +427,59 @@ function transitionDisplay(saved, msg, suppliedConfig = {}, now = new Date()) {
375
427
  }
376
428
  if (!state.pending || state.pending.device !== device)
377
429
  return reconcile();
378
- if (state.pending.operation === "projector_start" &&
379
- state.pending.readyAt !== undefined &&
380
- state.observed.projector?.power === "on" && now.getTime() < state.pending.readyAt) {
381
- state.pending.nextAt = Math.min(state.pending.deadlineAt, state.pending.readyAt);
430
+ const warming = (0, toggle_power_1.holdForWarmup)(state.pending, state.observed.projector?.power, now.getTime());
431
+ if (warming !== null) {
432
+ state.pending.nextAt = warming;
382
433
  return;
383
434
  }
384
435
  if (operationSatisfied(state.pending.operation, state.observed, config)) {
385
436
  state.pending = null;
386
437
  return reconcile();
387
438
  }
388
- if (state.pending.operation === "projector_shutdown_confirm" && device === "projector") {
389
- if (state.observed.projector?.power === "on") {
390
- const command = commandFor(state, "projector", "power_toggle", { reason: "shutdown-confirm" });
391
- commands.push(command);
392
- state.pending = {
393
- device: "projector",
394
- operation: "projector_cooldown",
395
- command,
396
- attempt: 1,
397
- retryable: false,
398
- deadlineAt: now.getTime() + config.projectorCooldownMs,
399
- nextAt: now.getTime() + config.pollMs
400
- };
401
- trace("command", "emitted", null, { device: "projector", operation: "shutdown-confirm", attempt: 1 });
402
- state.lastAttempt = Math.max(state.lastAttempt, 1);
403
- if (config.projectorPowerFallback)
404
- state.fallbackUsed = true;
405
- }
439
+ if (device === "projector" && (0, toggle_power_1.needsShutdownConfirm)(state.pending, state.observed.projector?.power)) {
440
+ const command = commandFor(state, "projector", "power_toggle", { reason: "shutdown-confirm" });
441
+ commands.push(command);
442
+ state.pending = (0, toggle_power_1.cooldownPending)("projector", command, now.getTime(), toggleTiming);
443
+ trace("command", "emitted", null, { device: "projector", operation: "shutdown-confirm", attempt: 1 });
444
+ state.lastAttempt = Math.max(state.lastAttempt, 1);
445
+ if (config.projectorPowerFallback)
446
+ state.fallbackUsed = true;
406
447
  }
407
448
  }
449
+ // A device that hasn't reported by the query timeout is asked again, up to
450
+ // maxAttempts in all; then the request fails naming it, rather than
451
+ // waiting in discovering for a reading that may never come.
452
+ function discoveryDue(event) {
453
+ const discovery = state.discovery;
454
+ if (!discovery || now.getTime() < discovery.nextAt)
455
+ return false;
456
+ if (Number.isInteger(event.generation) && state.desired && event.generation !== state.desired.generation)
457
+ return false;
458
+ const unseen = DEVICES.filter((device) => !state.seen[device]);
459
+ if (unseen.length === 0) {
460
+ delete state.discovery;
461
+ return false;
462
+ }
463
+ if (discovery.attempt < config.maxAttempts) {
464
+ discovery.attempt += 1;
465
+ discovery.nextAt = now.getTime() + config.queryTimeoutMs;
466
+ for (const device of unseen)
467
+ commands.push({ ...queryFor(state, device), attempt: discovery.attempt });
468
+ trace("discovery", "retried", "no-reading", { devices: unseen, attempt: discovery.attempt });
469
+ return false;
470
+ }
471
+ if (state.desired) {
472
+ fail(unseen[0], "discovery", "no-reading");
473
+ }
474
+ else {
475
+ delete state.discovery;
476
+ trace("discovery", "unanswered", "no-reading", { devices: unseen });
477
+ }
478
+ return true;
479
+ }
408
480
  function handleTick(event) {
481
+ if (discoveryDue(event))
482
+ return;
409
483
  const pending = state.pending;
410
484
  if (!pending)
411
485
  return reconcile();
@@ -415,11 +489,9 @@ function transitionDisplay(saved, msg, suppliedConfig = {}, now = new Date()) {
415
489
  }
416
490
  if (now.getTime() < pending.nextAt)
417
491
  return;
418
- if (pending.operation === "projector_shutdown_observe") {
492
+ if (pending.operation === toggle_power_1.TOGGLE_SHUTDOWN_OBSERVE) {
419
493
  commands.push(queryFor(state, "projector"));
420
- pending.operation = "projector_shutdown_confirm";
421
- pending.nextAt = now.getTime() + config.queryTimeoutMs;
422
- pending.deadlineAt = pending.nextAt;
494
+ (0, toggle_power_1.beginShutdownConfirm)(pending, now.getTime(), toggleTiming);
423
495
  trace("command", "emitted", null, { device: "projector", operation: "query-before-confirm", attempt: 1 });
424
496
  return;
425
497
  }
@@ -445,7 +517,7 @@ function transitionDisplay(saved, msg, suppliedConfig = {}, now = new Date()) {
445
517
  retry(pending, null);
446
518
  return;
447
519
  }
448
- return fail(pending.device, pending.operation, pending.operation === "projector_settle" ? "projector-power-unsettled" : "confirmation-timeout");
520
+ return fail(pending.device, pending.operation, pending.operation === toggle_power_1.TOGGLE_SETTLE ? "projector-power-unsettled" : "confirmation-timeout");
449
521
  }
450
522
  commands.push(queryFor(state, pending.device));
451
523
  pending.nextAt = Math.min(pending.deadlineAt, now.getTime() + config.pollMs);
@@ -456,13 +528,30 @@ function transitionDisplay(saved, msg, suppliedConfig = {}, now = new Date()) {
456
528
  // A refresh with the activity's wanted values, reaching a node with
457
529
  // nothing desired (it started with nothing saved): adopt them, and
458
530
  // rediscover below as at startup (see adopted-request.ts).
531
+ let replacedRequest = false;
459
532
  const adoptable = event.kind === "refresh" ? (0, adopted_request_1.adoptableRefresh)(event, state) : null;
460
- const adopted = adoptable !== null && event.kind === "refresh" && typeof event.mode === "string" && Object.hasOwn(MODES, event.mode);
533
+ // The same wanted values as the work in hand: carry on under the new
534
+ // request rather than adopting it read-only (see carryOn).
535
+ const carried = event.kind === "refresh" && (0, adopted_request_1.carryOn)(state, adoptable, event.mode === state.desired?.mode, state.classification === "manual_override" || state.phase === "failed" || state.blockedGeneration === state.desired?.generation);
536
+ if (carried)
537
+ trace("refresh", "continued", "same-request", { generation: state.generation });
538
+ const adopted = !carried && adoptable !== null && event.kind === "refresh" && typeof event.mode === "string" && Object.hasOwn(MODES, event.mode);
461
539
  if (adopted) {
540
+ const reason = (0, adopted_request_1.adoptionReason)(state.desired);
541
+ // Replacing a request the theatre already counts as ready, it answers
542
+ // from its reading, not with an interim state the theatre would take as
543
+ // drift.
544
+ replacedRequest = reason === "newer-generation";
545
+ // A request it replaces takes its classification and failure with it.
546
+ state.failure = null;
547
+ state.blockedGeneration = null;
548
+ state.classification = null;
549
+ state.lastAttempt = 0;
550
+ state.fallbackUsed = false;
462
551
  state.desired = { ...adoptable, mode: event.mode };
463
552
  state.generation = adoptable.generation;
464
553
  state.adoptedGeneration = adoptable.generation;
465
- trace("refresh", "adopted", "nothing-desired", { mode: event.mode, generation: adoptable.generation });
554
+ trace("refresh", "adopted", reason, { mode: event.mode, generation: adoptable.generation });
466
555
  }
467
556
  if (event.kind === "startup" || adopted) {
468
557
  // A projector power operation in flight cannot be repeated safely (a
@@ -472,6 +561,7 @@ function transitionDisplay(saved, msg, suppliedConfig = {}, now = new Date()) {
472
561
  state.pending = null;
473
562
  state.seen = { tv: false, screen: false, projector: false };
474
563
  state.querying = { tv: false, screen: false, projector: false };
564
+ delete state.discovery;
475
565
  beginDiscovery();
476
566
  if (inFlight) {
477
567
  const deadlineAt = Math.max(inFlight.deadlineAt, now.getTime() + config.queryTimeoutMs);
@@ -516,6 +606,7 @@ function transitionDisplay(saved, msg, suppliedConfig = {}, now = new Date()) {
516
606
  state.pending = state.pending && !state.pending.retryable ? state.pending : null;
517
607
  state.seen = { tv: false, screen: false, projector: false };
518
608
  state.querying = { tv: false, screen: false, projector: false };
609
+ delete state.discovery;
519
610
  beginDiscovery();
520
611
  trace("intent", "accepted", null, { mode: event.mode });
521
612
  }
@@ -524,7 +615,7 @@ function transitionDisplay(saved, msg, suppliedConfig = {}, now = new Date()) {
524
615
  acceptObservation(event);
525
616
  }
526
617
  else if (event.kind === "transport_failed") {
527
- if (Number.isInteger(event.generation) && state.desired && event.generation !== state.desired.generation) {
618
+ if (Number.isInteger(event.generation) && state.desired && !(0, adopted_request_1.answersRequest)(state.carried, state.desired.generation, event.generation)) {
528
619
  trace("transport", "ignored", "stale-generation", { device: event.device });
529
620
  }
530
621
  else if (state.querying[event.device]) {
@@ -569,6 +660,9 @@ function transitionDisplay(saved, msg, suppliedConfig = {}, now = new Date()) {
569
660
  // fresh as its least recent confirmation.
570
661
  // An adopted request only reads the device (see adopted-request.ts): a
571
662
  // power, toggle or screen command means the display isn't as wanted.
663
+ // Discovery is over once every device has reported.
664
+ if (allSeen(state))
665
+ delete state.discovery;
572
666
  const held = (0, adopted_request_1.readsOnly)(state, commands, (command) => command.operation.startsWith("query"));
573
667
  if (held.mismatch) {
574
668
  commands.splice(0, commands.length, ...held.commands);
@@ -578,7 +672,7 @@ function transitionDisplay(saved, msg, suppliedConfig = {}, now = new Date()) {
578
672
  events.push(displayEvent("manual_override", state, { reason: adopted_request_1.ADOPTED_MISMATCH }));
579
673
  trace("command", "withheld", adopted_request_1.ADOPTED_MISMATCH);
580
674
  }
581
- if (state.desired) {
675
+ if (state.desired && !replacedRequest) {
582
676
  const readings = DEVICES.map((device) => (0, observation_time_1.parseObservedAt)(state.observed[device]?.observedAt));
583
677
  const confirmedMs = readings.every((ms) => ms !== null) ? Math.min(...readings) : null;
584
678
  const status = state.phase === "manual_override"
@@ -0,0 +1,3 @@
1
+ export declare const NOT_TRUE_OR_FALSE = "not-true-or-false";
2
+ export declare function enableValue(value: unknown): boolean | null;
3
+ export declare function describeValue(value: unknown): string;
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.NOT_TRUE_OR_FALSE = void 0;
4
+ exports.enableValue = enableValue;
5
+ exports.describeValue = describeValue;
6
+ // An enable, sleep or pause input from a flow: true or false only. Anything
7
+ // else, such as "on" or "ON" (accepted before 6.0), is refused and traced,
8
+ // never read as off. Home Assistant's ON and OFF reach a node through its
9
+ // set topics, which translate them to true or false.
10
+ exports.NOT_TRUE_OR_FALSE = "not-true-or-false";
11
+ function enableValue(value) {
12
+ return typeof value === "boolean" ? value : null;
13
+ }
14
+ // A refused value as it can be shown in a trace and a warning.
15
+ function describeValue(value) {
16
+ return typeof value === "string" ? JSON.stringify(value) : String(value);
17
+ }
18
+ //# sourceMappingURL=enable-input.js.map
@@ -9,7 +9,7 @@ const publishing_node_1 = require("./publishing-node");
9
9
  const topic_names_1 = require("./topic-names");
10
10
  // The one place a house schedule topic is named.
11
11
  function houseTopic(name, path) {
12
- return "home/" + (0, topic_names_1.areaId)(name) + "/schedule/" + path;
12
+ return (0, topic_names_1.homeTopic)(name, "schedule", path);
13
13
  }
14
14
  const houseStateTopic = (name, which) => houseTopic(name, which + "/state");
15
15
  exports.houseStateTopic = houseStateTopic;
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.houseScheduleInUse = houseScheduleInUse;
4
4
  exports.transitionHouseSchedule = transitionHouseSchedule;
5
- const room_schedule_1 = require("./room-schedule");
5
+ const circadian_schedule_1 = require("./circadian-schedule");
6
6
  const settings_1 = require("./settings");
7
7
  // The times in use: each saved time while its setting is unchanged.
8
8
  function houseScheduleInUse(saved, setting) {
@@ -15,7 +15,7 @@ function transitionHouseSchedule(saved, event, setting) {
15
15
  const state = { schedule: houseScheduleInUse(saved, setting), scheduleSetting: { ...setting } };
16
16
  const traces = [];
17
17
  let changed = false;
18
- const next = (0, room_schedule_1.requestedSchedule)(state.schedule, event);
18
+ const next = (0, circadian_schedule_1.requestedSchedule)(state.schedule, event);
19
19
  if (!next) {
20
20
  traces.push({ event: "schedule", outcome: "ignored", reason: "invalid-times", details: { wakeTime: event.wakeTime, bedTime: event.bedTime } });
21
21
  }
@@ -0,0 +1,53 @@
1
+ export interface LightCommandMessage {
2
+ kind: "command";
3
+ target: string;
4
+ state: "ON" | "OFF";
5
+ brightnessPct?: number;
6
+ colorTempKelvin?: number;
7
+ transitionSeconds?: number;
8
+ extra?: Record<string, unknown>;
9
+ restyle?: boolean;
10
+ source: string;
11
+ id: string;
12
+ }
13
+ export interface LightObserveMessage {
14
+ kind: "observe";
15
+ target: string;
16
+ }
17
+ export type LightRoomMessage = LightCommandMessage | LightObserveMessage;
18
+ export type LightField = "brightness" | "colorTemp";
19
+ export type LightReading = {
20
+ kind: "observed";
21
+ target: string;
22
+ on: boolean;
23
+ brightnessPct?: number;
24
+ colorTempKelvin?: number;
25
+ fields: LightField[];
26
+ showsLast: boolean;
27
+ observedAt: string;
28
+ } | {
29
+ kind: "external";
30
+ target: string;
31
+ state: string;
32
+ setsValues: boolean;
33
+ observedAt: string;
34
+ } | {
35
+ kind: "echo-changed";
36
+ target: string;
37
+ id: string | null;
38
+ state: string;
39
+ } | {
40
+ kind: "skipped";
41
+ target: string;
42
+ id: string;
43
+ observedAt: string;
44
+ } | {
45
+ kind: "sent";
46
+ target: string;
47
+ id: string;
48
+ } | {
49
+ kind: "availability";
50
+ target: string;
51
+ online: boolean;
52
+ };
53
+ export declare function isLightReading(value: unknown): value is LightReading;
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isLightReading = isLightReading;
4
+ function isLightReading(value) {
5
+ if (!value || typeof value !== "object")
6
+ return false;
7
+ const reading = value;
8
+ return typeof reading.target === "string" &&
9
+ ["observed", "external", "echo-changed", "skipped", "sent", "availability"].includes(String(reading.kind));
10
+ }
11
+ //# sourceMappingURL=light-contract.js.map
@@ -5,7 +5,7 @@ export interface LightLevels {
5
5
  maxColorTempKelvin: number;
6
6
  }
7
7
  export interface LightStyle {
8
- brightness: number;
9
- color_temp: number;
8
+ brightnessPct: number;
9
+ colorTempKelvin: number;
10
10
  }
11
11
  export declare function styleAtLevel(level: number, levels: LightLevels): LightStyle;
@@ -3,13 +3,14 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.styleAtLevel = styleAtLevel;
4
4
  function styleAtLevel(level, levels) {
5
5
  const percent = levels.sleepBrightnessPct + (levels.maxBrightnessPct - levels.sleepBrightnessPct) * level;
6
- // Colour moves evenly in mireds, the unit bulbs take: equal mired steps look
7
- // evenly spaced, where equal kelvin steps would bunch up at the cool end.
6
+ // Colour moves evenly in mireds (reciprocal megakelvin): equal mired steps
7
+ // look evenly spaced, where equal kelvin steps would bunch up at the cool
8
+ // end.
8
9
  const sleepMired = 1_000_000 / levels.sleepColorTempKelvin;
9
10
  const dayMired = 1_000_000 / levels.maxColorTempKelvin;
10
11
  return {
11
- brightness: Math.min(254, Math.max(1, Math.round(percent * 254 / 100))),
12
- color_temp: Math.round(sleepMired + (dayMired - sleepMired) * level)
12
+ brightnessPct: percent,
13
+ colorTempKelvin: 1_000_000 / (sleepMired + (dayMired - sleepMired) * level)
13
14
  };
14
15
  }
15
16
  //# sourceMappingURL=light-styling.js.map
@@ -47,7 +47,6 @@ export type MotionEffect = {
47
47
  kind: "send";
48
48
  message: LightingIntent;
49
49
  };
50
- export declare function booleanOn(value: unknown): boolean;
51
50
  export declare function normaliseMotionConfig(raw: {
52
51
  onTargets?: unknown;
53
52
  offTargets?: unknown;