@avantics/node-red-home-automation 3.0.0 → 4.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 (110) hide show
  1. package/README.md +285 -13
  2. package/dist/lib/activity-lighting-controller.d.ts +7 -1
  3. package/dist/lib/activity-lighting-controller.js +37 -3
  4. package/dist/lib/adopted-request.d.ts +26 -0
  5. package/dist/lib/adopted-request.js +44 -0
  6. package/dist/lib/avr-controller.d.ts +8 -1
  7. package/dist/lib/avr-controller.js +27 -3
  8. package/dist/lib/avr-publishing.d.ts +24 -0
  9. package/dist/lib/avr-publishing.js +80 -0
  10. package/dist/lib/backlighting-controller.d.ts +7 -1
  11. package/dist/lib/backlighting-controller.js +26 -3
  12. package/dist/lib/circadian.d.ts +1 -0
  13. package/dist/lib/circadian.js +3 -0
  14. package/dist/lib/command-echo.d.ts +2 -0
  15. package/dist/lib/command-echo.js +39 -15
  16. package/dist/lib/cover-availability.d.ts +7 -0
  17. package/dist/lib/cover-availability.js +24 -0
  18. package/dist/lib/cover-controller.d.ts +13 -1
  19. package/dist/lib/cover-controller.js +113 -30
  20. package/dist/lib/cover-group.d.ts +62 -0
  21. package/dist/lib/cover-group.js +125 -0
  22. package/dist/lib/cover-models.d.ts +37 -0
  23. package/dist/lib/cover-models.js +83 -0
  24. package/dist/lib/cover-publishing.d.ts +40 -0
  25. package/dist/lib/cover-publishing.js +102 -0
  26. package/dist/lib/cover-requests.d.ts +21 -0
  27. package/dist/lib/cover-requests.js +49 -0
  28. package/dist/lib/display-mode-controller.d.ts +4 -0
  29. package/dist/lib/display-mode-controller.js +26 -2
  30. package/dist/lib/ha-discovery.d.ts +27 -0
  31. package/dist/lib/ha-discovery.js +91 -0
  32. package/dist/lib/house-schedule-publishing.d.ts +23 -0
  33. package/dist/lib/house-schedule-publishing.js +71 -0
  34. package/dist/lib/house-schedule.d.ts +23 -0
  35. package/dist/lib/house-schedule.js +29 -0
  36. package/dist/lib/motion-policy.d.ts +9 -0
  37. package/dist/lib/motion-policy.js +45 -11
  38. package/dist/lib/motion-publishing.d.ts +28 -0
  39. package/dist/lib/motion-publishing.js +92 -0
  40. package/dist/lib/occupancy-publishing.d.ts +10 -0
  41. package/dist/lib/occupancy-publishing.js +45 -0
  42. package/dist/lib/open-close-cover.d.ts +92 -0
  43. package/dist/lib/open-close-cover.js +287 -0
  44. package/dist/lib/publishing-node.d.ts +39 -0
  45. package/dist/lib/publishing-node.js +72 -0
  46. package/dist/lib/retained-topics.d.ts +10 -0
  47. package/dist/lib/retained-topics.js +19 -0
  48. package/dist/lib/room-controller.d.ts +10 -0
  49. package/dist/lib/room-controller.js +68 -59
  50. package/dist/lib/room-lighting-discovery.d.ts +20 -1
  51. package/dist/lib/room-lighting-discovery.js +135 -13
  52. package/dist/lib/room-publishing.d.ts +42 -0
  53. package/dist/lib/room-publishing.js +182 -0
  54. package/dist/lib/room-schedule.d.ts +8 -0
  55. package/dist/lib/room-schedule.js +41 -0
  56. package/dist/lib/settings.d.ts +2 -0
  57. package/dist/lib/settings.js +20 -0
  58. package/dist/lib/source-capability-controller.d.ts +8 -1
  59. package/dist/lib/source-capability-controller.js +29 -3
  60. package/dist/lib/theatre-activity-controller.d.ts +0 -1
  61. package/dist/lib/theatre-activity-controller.js +27 -24
  62. package/dist/lib/theatre-ha-discovery.d.ts +3 -4
  63. package/dist/lib/theatre-ha-discovery.js +46 -36
  64. package/dist/lib/theatre-state-contract.d.ts +6 -0
  65. package/dist/lib/theatre-state-contract.js +83 -47
  66. package/dist/lib/theatre-state.d.ts +1 -1
  67. package/dist/lib/theatre-state.js +1 -1
  68. package/dist/lib/topic-names.d.ts +4 -0
  69. package/dist/lib/topic-names.js +31 -0
  70. package/dist/nodes/activity-lighting-controller.js +5 -3
  71. package/dist/nodes/adapter.d.ts +6 -1
  72. package/dist/nodes/adapter.js +65 -1
  73. package/dist/nodes/avr-controller.html +59 -5
  74. package/dist/nodes/avr-controller.js +74 -4
  75. package/dist/nodes/backlighting-controller.js +5 -3
  76. package/dist/nodes/cover-controller.d.ts +3 -0
  77. package/dist/nodes/cover-controller.html +177 -0
  78. package/dist/nodes/cover-controller.js +178 -0
  79. package/dist/nodes/cover-group.d.ts +3 -0
  80. package/dist/nodes/cover-group.html +89 -0
  81. package/dist/nodes/cover-group.js +84 -0
  82. package/dist/nodes/flows-started.d.ts +4 -0
  83. package/dist/nodes/flows-started.js +39 -0
  84. package/dist/nodes/house-schedule-mqtt.d.ts +3 -0
  85. package/dist/nodes/house-schedule-mqtt.html +61 -0
  86. package/dist/nodes/house-schedule-mqtt.js +80 -0
  87. package/dist/nodes/house-schedule.d.ts +3 -0
  88. package/dist/nodes/house-schedule.html +31 -0
  89. package/dist/nodes/house-schedule.js +51 -0
  90. package/dist/nodes/motion-policy.html +81 -5
  91. package/dist/nodes/motion-policy.js +69 -8
  92. package/dist/nodes/occupancy-fusion.html +69 -14
  93. package/dist/nodes/occupancy-fusion.js +46 -4
  94. package/dist/nodes/publishing-lifecycle.d.ts +25 -0
  95. package/dist/nodes/publishing-lifecycle.js +38 -0
  96. package/dist/nodes/room-controller.html +54 -5
  97. package/dist/nodes/room-controller.js +67 -8
  98. package/dist/nodes/shared-schedule.d.ts +6 -0
  99. package/dist/nodes/shared-schedule.js +15 -0
  100. package/dist/nodes/source-capability-controller.js +5 -3
  101. package/dist/nodes/theatre-activity-controller.html +1 -3
  102. package/dist/nodes/theatre-activity-controller.js +1 -2
  103. package/dist/nodes/theatre-state-contract.html +28 -5
  104. package/dist/nodes/theatre-state-contract.js +22 -4
  105. package/examples/Cover control.json +380 -0
  106. package/examples/Cover pair.json +230 -0
  107. package/examples/Home theatre activity.json +4 -4
  108. package/examples/House schedule.json +164 -0
  109. package/examples/Room lighting.json +4 -3
  110. package/package.json +6 -2
@@ -1,20 +1,25 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.normaliseCoverConfig = normaliseCoverConfig;
4
+ exports.coverMoving = coverMoving;
4
5
  exports.transitionCover = transitionCover;
6
+ const cover_availability_1 = require("./cover-availability");
5
7
  const observation_time_1 = require("./observation-time");
8
+ const settings_1 = require("./settings");
6
9
  const target_state_1 = require("./target-state");
7
10
  const MOTIONS = { opening: true, closing: true, stopped: true };
8
11
  function normaliseCoverConfig(config = {}) {
9
- const tolerance = Number(config.positionTolerance);
10
- const pollMs = Math.max(250, Number(config.pollMs) || 5000);
12
+ const tolerance = (0, settings_1.numberSetting)(config.positionTolerance, "position tolerance", 2);
13
+ if (tolerance < 0)
14
+ throw new Error("position tolerance must not be negative");
15
+ const pollMs = Math.max(250, (0, settings_1.numberSetting)(config.pollMs, "poll interval", 5000));
11
16
  return {
12
- positionTolerance: Number.isFinite(tolerance) && tolerance >= 0 ? Math.min(tolerance, 50) : 2,
13
- invert: config.invert === true,
14
- travelTimeoutMs: Math.max(1000, Number(config.travelTimeoutMs) || 90000),
15
- maxAttempts: Math.max(1, Math.floor(Number(config.maxAttempts) || 2)),
17
+ positionTolerance: Math.min(tolerance, 50),
18
+ invert: (0, settings_1.booleanSetting)(config.invert, "invert", false),
19
+ travelTimeoutMs: Math.max(1000, (0, settings_1.numberSetting)(config.travelTimeoutMs, "travel timeout", 90000)),
20
+ maxAttempts: Math.max(1, Math.floor((0, settings_1.numberSetting)(config.maxAttempts, "maximum attempts", 2))),
16
21
  pollMs,
17
- settleMs: Math.max(250, Number(config.settleMs) || pollMs)
22
+ settleMs: Math.max(250, (0, settings_1.numberSetting)(config.settleMs, "settle time", pollMs))
18
23
  };
19
24
  }
20
25
  function initialState(saved = {}) {
@@ -27,7 +32,9 @@ function initialState(saved = {}) {
27
32
  failure: saved.failure && typeof saved.failure === "object" ? { ...saved.failure } : null,
28
33
  blockedGeneration: Number.isInteger(saved.blockedGeneration) ? saved.blockedGeneration : null,
29
34
  classification: saved.classification === "manual_override" ? "manual_override" : null,
30
- lastAttempt: Number.isInteger(saved.lastAttempt) && (saved.lastAttempt ?? -1) >= 0 ? saved.lastAttempt : 0
35
+ lastAttempt: Number.isInteger(saved.lastAttempt) && (saved.lastAttempt ?? -1) >= 0 ? saved.lastAttempt : 0,
36
+ available: saved.available !== false,
37
+ phaseBeforeUnavailable: typeof saved.phaseBeforeUnavailable === "string" ? saved.phaseBeforeUnavailable : null
31
38
  };
32
39
  }
33
40
  function validPosition(value, label) {
@@ -36,11 +43,19 @@ function validPosition(value, label) {
36
43
  }
37
44
  return value;
38
45
  }
46
+ // Whether the cover is moving: as the device reports it, or, from a device
47
+ // without motion reports, while a move or stop it was sent is unconfirmed.
48
+ function coverMoving(state) {
49
+ const motion = state.observed?.motion ?? null;
50
+ if (motion !== null)
51
+ return motion !== "stopped";
52
+ return state.phase === "moving" || state.phase === "stopping";
53
+ }
39
54
  function statusFor(state) {
40
55
  const target = state.desired?.operation === "stop" && state.desired.position === null
41
56
  ? "stop"
42
57
  : state.desired?.position ?? "none";
43
- return { phase: state.phase, text: `${target}: ${state.phase}` };
58
+ return { phase: state.phase, text: `${target}: ${state.phase}`, moving: coverMoving(state) };
44
59
  }
45
60
  function transitionCover(saved, msg, suppliedConfig = {}, nowValue = new Date()) {
46
61
  const config = normaliseCoverConfig(suppliedConfig);
@@ -112,7 +127,64 @@ function transitionCover(saved, msg, suppliedConfig = {}, nowValue = new Date())
112
127
  state.phase = "ready";
113
128
  trace("observation", "ready", null, { position });
114
129
  }
130
+ // A request not carried out: nothing is sent, the request is answered
131
+ // with why (unavailable while offline, failed otherwise), and what the
132
+ // cover was doing before is kept.
133
+ function rejectRequest(intent, reason) {
134
+ const position = intent.kind === "set_position" ? intent.position : intent.kind === "open" ? 100 : intent.kind === "close" ? 0 : null;
135
+ const operation = position === null ? "stop" : "set_position";
136
+ events.push({ theatre: {
137
+ kind: "target_state",
138
+ target: "cover",
139
+ requestId: intent.requestId,
140
+ generation: intent.generation,
141
+ status: reason === "unavailable" ? "unavailable" : "failed",
142
+ observedAt: now.toISOString(),
143
+ details: {
144
+ operation,
145
+ position,
146
+ observedPosition: null,
147
+ motion: null,
148
+ source: typeof intent.source === "string" && intent.source ? intent.source : null,
149
+ recovery: {},
150
+ metrics: { attempts: 0, retries: 0, fallbackUsed: false }
151
+ },
152
+ failure: { operation, reason, occurredAt: now.toISOString() }
153
+ } });
154
+ trace("intent", "rejected", reason, { kind: intent.kind });
155
+ }
156
+ // Read the cover again rather than trust what it reported before: after a
157
+ // restart, or when it is back online.
158
+ function reobserve(reason) {
159
+ state.observed = null;
160
+ if (!state.desired) {
161
+ state.pending = null;
162
+ trace(reason, "idle");
163
+ }
164
+ else if (state.phase === "failed") {
165
+ state.pending = null;
166
+ trace(reason, "blocked", "failed");
167
+ }
168
+ else if (state.classification === "manual_override") {
169
+ state.pending = null;
170
+ emit("observe", 0);
171
+ trace(reason, "observing", "manual_override");
172
+ }
173
+ else {
174
+ const verifying = state.phase === "ready" && !state.pending;
175
+ issue("observe", 0, verifying);
176
+ trace(reason, "observing", null, { position: state.desired.position, verifying });
177
+ }
178
+ }
115
179
  function acceptIntent(intent) {
180
+ if (!state.available) {
181
+ rejectRequest(intent, "unavailable");
182
+ return;
183
+ }
184
+ if (intent.respectManual === true && state.classification === "manual_override") {
185
+ rejectRequest(intent, "manual-hold");
186
+ return;
187
+ }
116
188
  if (typeof intent.requestId !== "string" || !intent.requestId)
117
189
  throw new Error("cover requestId is required");
118
190
  if (!Number.isInteger(intent.generation) || intent.generation < 1) {
@@ -146,6 +218,16 @@ function transitionCover(saved, msg, suppliedConfig = {}, nowValue = new Date())
146
218
  state.blockedGeneration = null;
147
219
  state.classification = null;
148
220
  state.lastAttempt = 0;
221
+ // Already there and still: confirmed from what the cover reported since
222
+ // the last start, with nothing to send.
223
+ const observed = state.observed;
224
+ if (position !== null && observed && observed.motion !== "opening" && observed.motion !== "closing" &&
225
+ Math.abs(observed.position - position) <= config.positionTolerance) {
226
+ state.pending = null;
227
+ trace("intent", "accepted", "already-there", { kind: intent.kind, position, generation: intent.generation });
228
+ ready(position);
229
+ return;
230
+ }
149
231
  issue(state.desired.operation, 1);
150
232
  trace("intent", "accepted", null, { kind: intent.kind, position, generation: intent.generation });
151
233
  }
@@ -169,7 +251,13 @@ function transitionCover(saved, msg, suppliedConfig = {}, nowValue = new Date())
169
251
  const position = config.invert ? 100 - devicePosition : devicePosition;
170
252
  const motion = observation.motion ?? null;
171
253
  const previous = state.observed;
172
- state.observed = { position, motion, observedAt: bounded.observedAt };
254
+ state.observed = {
255
+ position,
256
+ motion,
257
+ observedAt: bounded.observedAt,
258
+ battery: typeof observation.battery === "number" && Number.isFinite(observation.battery) ? observation.battery : previous?.battery ?? null,
259
+ charging: typeof observation.charging === "string" ? observation.charging : previous?.charging ?? null
260
+ };
173
261
  if (!state.desired)
174
262
  return;
175
263
  const pending = state.pending;
@@ -276,26 +364,21 @@ function transitionCover(saved, msg, suppliedConfig = {}, nowValue = new Date())
276
364
  state.pending.nextAt = nowMs + config.pollMs;
277
365
  }
278
366
  }
367
+ else if (event.kind === "availability") {
368
+ const change = (0, cover_availability_1.applyCoverAvailability)(state, event.online);
369
+ if (change === "offline")
370
+ state.observed = null;
371
+ if (change)
372
+ trace("availability", change);
373
+ if (change === "online")
374
+ reobserve("online");
375
+ }
376
+ else if (!state.available) {
377
+ // Offline: nothing to observe or retry until it is back.
378
+ trace(event.kind, "ignored", "unavailable");
379
+ }
279
380
  else if (event.kind === "startup" || event.kind === "refresh") {
280
- state.observed = null;
281
- if (!state.desired) {
282
- state.pending = null;
283
- trace(event.kind, "idle");
284
- }
285
- else if (state.phase === "failed") {
286
- state.pending = null;
287
- trace(event.kind, "blocked", "failed");
288
- }
289
- else if (state.classification === "manual_override") {
290
- state.pending = null;
291
- emit("observe", 0);
292
- trace(event.kind, "observing", "manual_override");
293
- }
294
- else {
295
- const verifying = state.phase === "ready" && !state.pending;
296
- issue("observe", 0, verifying);
297
- trace(event.kind, "observing", null, { position: state.desired.position, verifying });
298
- }
381
+ reobserve(event.kind);
299
382
  }
300
383
  else {
301
384
  throw new Error("unknown cover event kind");
@@ -307,7 +390,7 @@ function transitionCover(saved, msg, suppliedConfig = {}, nowValue = new Date())
307
390
  generation: state.generation,
308
391
  // Theatre target statuses: cover phases moving/stopping/manual_override
309
392
  // are all "transitioning" there.
310
- status: state.phase === "failed" || state.phase === "ready" || state.phase === "confirming"
393
+ status: state.phase === "failed" || state.phase === "ready" || state.phase === "confirming" || state.phase === "unavailable"
311
394
  ? state.phase
312
395
  : "transitioning",
313
396
  observedAt: now.toISOString(),
@@ -0,0 +1,62 @@
1
+ import type { TargetStatus } from "./target-state";
2
+ import { type TargetStateEvent } from "./target-state";
3
+ export declare function coverGroupMembers(value: unknown): string[];
4
+ type MemberOutcome = TargetStatus | "manual_override";
5
+ export declare const DEFAULT_GROUP_DEADLINE_SECONDS = 120;
6
+ export interface CoverGroupState {
7
+ request: {
8
+ requestId: string;
9
+ generation: number;
10
+ payload: unknown;
11
+ deadlineAt: number;
12
+ settled: boolean;
13
+ members: Record<string, {
14
+ requestId: string;
15
+ outcome: MemberOutcome;
16
+ reason: string | null;
17
+ }>;
18
+ } | null;
19
+ generation: number;
20
+ }
21
+ export type CoverGroupEvent = {
22
+ kind: "request";
23
+ payload: unknown;
24
+ stopIfMoving?: boolean;
25
+ respectManual?: boolean;
26
+ source?: string;
27
+ } | {
28
+ kind: "member";
29
+ requestId: string;
30
+ status: string;
31
+ classification?: string;
32
+ reason?: string | null;
33
+ } | {
34
+ kind: "tick";
35
+ };
36
+ export interface CoverGroupCommand {
37
+ topic: string;
38
+ payload: unknown;
39
+ coverRequestId: string;
40
+ stopIfMoving?: boolean;
41
+ respectManual?: boolean;
42
+ source?: string;
43
+ }
44
+ export declare function transitionCoverGroup(saved: Partial<CoverGroupState> | undefined, event: CoverGroupEvent, members: string[], now: Date, deadlineSeconds?: number): {
45
+ state: CoverGroupState;
46
+ commands: CoverGroupCommand[];
47
+ events: {
48
+ theatre: TargetStateEvent<"cover", Record<string, unknown>>;
49
+ }[];
50
+ traces: {
51
+ event: string;
52
+ outcome: string;
53
+ reason: string | null;
54
+ details: Record<string, unknown>;
55
+ }[];
56
+ status: {
57
+ text: string;
58
+ ready: number;
59
+ total: number;
60
+ };
61
+ };
62
+ export {};
@@ -0,0 +1,125 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DEFAULT_GROUP_DEADLINE_SECONDS = void 0;
4
+ exports.coverGroupMembers = coverGroupMembers;
5
+ exports.transitionCoverGroup = transitionCoverGroup;
6
+ const target_state_1 = require("./target-state");
7
+ const settings_1 = require("./settings");
8
+ const MEMBER = /^[a-z][a-z0-9_-]*$/;
9
+ function coverGroupMembers(value) {
10
+ const members = (0, settings_1.jsonSetting)(value, "members", []);
11
+ if (!Array.isArray(members) || members.length < 2)
12
+ throw new Error("members must be a JSON array of at least two names");
13
+ if (!members.every((name) => typeof name === "string" && MEMBER.test(name)))
14
+ throw new Error("members must be lowercase identifiers");
15
+ if (new Set(members).size !== members.length)
16
+ throw new Error("members must be unique");
17
+ return members;
18
+ }
19
+ exports.DEFAULT_GROUP_DEADLINE_SECONDS = 120;
20
+ const SETTLED = { ready: true, failed: true, unavailable: true, manual_override: true };
21
+ function transitionCoverGroup(saved, event, members, now, deadlineSeconds = exports.DEFAULT_GROUP_DEADLINE_SECONDS) {
22
+ const state = {
23
+ request: saved?.request ? { ...saved.request, settled: saved.request.settled === true, members: { ...saved.request.members } } : null,
24
+ generation: Number.isInteger(saved?.generation) ? saved.generation : 0
25
+ };
26
+ const commands = [];
27
+ const traces = [];
28
+ let changed = false;
29
+ const events = [];
30
+ // The group's answer for its open request, from what its members did.
31
+ function answer(supersededBy = null) {
32
+ const request = state.request;
33
+ const outcomes = Object.values(request.members);
34
+ const allReady = outcomes.every(({ outcome }) => outcome === "ready");
35
+ const allSettled = outcomes.every(({ outcome }) => SETTLED[outcome]);
36
+ const confirmed = Object.entries(request.members).filter(([, member]) => member.outcome === "ready").map(([name]) => name);
37
+ const status = supersededBy ? "failed" : allReady ? "ready" : allSettled ? "failed" : "transitioning";
38
+ if (status !== "transitioning")
39
+ request.settled = true;
40
+ const reason = supersededBy ? "superseded" : confirmed.length ? "partial" : "none-confirmed";
41
+ events.push({ theatre: (0, target_state_1.targetStateEvent)({
42
+ target: "cover",
43
+ requestId: request.requestId,
44
+ generation: request.generation,
45
+ status,
46
+ observedAt: now.toISOString(),
47
+ details: {
48
+ request: request.payload,
49
+ members: Object.fromEntries(Object.entries(request.members).map(([name, member]) => [name, { status: member.outcome, reason: member.reason }])),
50
+ confirmed,
51
+ ...(supersededBy ? { supersededBy } : {})
52
+ },
53
+ failure: status === "failed" ? { reason, confirmed } : null
54
+ }) });
55
+ }
56
+ // Members still silent at the deadline are not answering: the group
57
+ // settles with what it has.
58
+ if (state.request && !state.request.settled && now.getTime() >= state.request.deadlineAt) {
59
+ for (const [name, member] of Object.entries(state.request.members)) {
60
+ if (!SETTLED[member.outcome]) {
61
+ state.request.members[name] = { ...member, outcome: "failed", reason: "no-answer" };
62
+ traces.push({ event: "member", outcome: "failed", reason: "no-answer", details: { member: name } });
63
+ }
64
+ }
65
+ changed = true;
66
+ }
67
+ if (event.kind === "tick") {
68
+ // Only the deadline, above.
69
+ }
70
+ else if (event.kind === "request") {
71
+ if (state.request && !state.request.settled && !changed) {
72
+ // The open request gets its final answer before the new one goes out.
73
+ answer(`group-${now.getTime()}-${state.generation + 1}`);
74
+ traces.push({ event: "request", outcome: "superseded", reason: null, details: { requestId: state.request.requestId } });
75
+ }
76
+ else if (changed && state.request) {
77
+ answer();
78
+ changed = false;
79
+ }
80
+ state.generation += 1;
81
+ const requestId = `group-${now.getTime()}-${state.generation}`;
82
+ state.request = { requestId, generation: state.generation, payload: event.payload, deadlineAt: now.getTime() + deadlineSeconds * 1000, settled: false, members: {} };
83
+ for (const member of members) {
84
+ const memberRequestId = `${requestId}:${member}`;
85
+ state.request.members[member] = { requestId: memberRequestId, outcome: "transitioning", reason: null };
86
+ commands.push({
87
+ topic: member,
88
+ payload: event.payload,
89
+ coverRequestId: memberRequestId,
90
+ ...(event.stopIfMoving === true ? { stopIfMoving: true } : {}),
91
+ ...(event.respectManual === true ? { respectManual: true } : {}),
92
+ ...(typeof event.source === "string" && event.source ? { source: event.source } : {})
93
+ });
94
+ }
95
+ changed = true;
96
+ traces.push({ event: "request", outcome: "sent", reason: null, details: { requestId, members } });
97
+ }
98
+ else {
99
+ const entry = state.request && Object.entries(state.request.members).find(([, member]) => member.requestId === event.requestId);
100
+ if (!entry) {
101
+ traces.push({ event: "member", outcome: "ignored", reason: "other-request", details: { requestId: event.requestId } });
102
+ }
103
+ else {
104
+ const [name, member] = entry;
105
+ const outcome = event.classification === "manual_override" ? "manual_override" : event.status;
106
+ if (member.outcome !== outcome) {
107
+ state.request.members[name] = { ...member, outcome, reason: event.reason ?? null };
108
+ changed = true;
109
+ traces.push({ event: "member", outcome, reason: event.reason ?? null, details: { member: name } });
110
+ }
111
+ }
112
+ }
113
+ if (state.request && changed)
114
+ answer();
115
+ const counts = state.request ? Object.values(state.request.members) : [];
116
+ const ready = counts.filter(({ outcome }) => outcome === "ready").length;
117
+ return {
118
+ state,
119
+ commands,
120
+ events,
121
+ traces,
122
+ status: { text: state.request ? `${ready}/${counts.length} confirmed` : "idle", ready, total: counts.length }
123
+ };
124
+ }
125
+ //# sourceMappingURL=cover-group.js.map
@@ -0,0 +1,37 @@
1
+ export declare const COVER_TYPES: readonly ["position", "open-close"];
2
+ export type CoverType = typeof COVER_TYPES[number];
3
+ export interface CoverTransition {
4
+ state: unknown;
5
+ commands: Array<Record<string, unknown>>;
6
+ events: Array<Record<string, unknown>>;
7
+ traces: Array<{
8
+ event: string;
9
+ outcome: string;
10
+ reason: string | null;
11
+ details: Record<string, unknown>;
12
+ }>;
13
+ status: {
14
+ phase: string;
15
+ moving: boolean;
16
+ text: string;
17
+ battery: number | null;
18
+ observed: boolean;
19
+ };
20
+ }
21
+ export interface CoverView {
22
+ state: "open" | "closed" | "opening" | "closing" | "stopped";
23
+ position: number | null;
24
+ }
25
+ export interface CoverModel {
26
+ positions: boolean;
27
+ transition(saved: unknown, event: unknown, now: Date): CoverTransition;
28
+ moving(saved: unknown): boolean;
29
+ generation(saved: unknown): number;
30
+ tickAt(state: unknown): {
31
+ at: number;
32
+ generation: number;
33
+ } | null;
34
+ view(saved: unknown): CoverView | null;
35
+ }
36
+ export declare function positionCover(settings: Record<string, unknown>): CoverModel;
37
+ export declare function openCloseCover(settings: Record<string, unknown>): CoverModel;
@@ -0,0 +1,83 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.COVER_TYPES = void 0;
4
+ exports.positionCover = positionCover;
5
+ exports.openCloseCover = openCloseCover;
6
+ // The two kinds of cover a cover node can own, behind one shape, so the node
7
+ // treats them alike: a position cover (0 to 100) and an open/close-only
8
+ // cover. Each says how to take an event, whether the cover is moving, when
9
+ // to check on it next, and whether it takes positions.
10
+ const cover_controller_1 = require("./cover-controller");
11
+ const open_close_cover_1 = require("./open-close-cover");
12
+ exports.COVER_TYPES = ["position", "open-close"];
13
+ function positionCover(settings) {
14
+ const config = (0, cover_controller_1.normaliseCoverConfig)(settings);
15
+ return {
16
+ positions: true,
17
+ transition(saved, event, now) {
18
+ const result = (0, cover_controller_1.transitionCover)(saved, { cover: event }, config, now);
19
+ return {
20
+ state: result.state,
21
+ commands: result.commands,
22
+ events: result.events,
23
+ traces: result.traces,
24
+ status: { ...result.status, battery: result.state.observed?.battery ?? null, observed: result.state.observed !== null }
25
+ };
26
+ },
27
+ moving(saved) {
28
+ const state = saved;
29
+ return state ? (0, cover_controller_1.coverMoving)({ observed: state.observed ?? null, phase: state.phase ?? "idle" }) : false;
30
+ },
31
+ generation: (saved) => saved?.generation ?? 0,
32
+ tickAt(state) {
33
+ const cover = state;
34
+ return cover.pending ? { at: Math.min(cover.pending.nextAt, cover.pending.deadlineAt), generation: cover.generation } : null;
35
+ },
36
+ view(saved) {
37
+ const observed = saved?.observed;
38
+ if (!observed)
39
+ return null;
40
+ const motion = observed.motion === "opening" || observed.motion === "closing" ? observed.motion : null;
41
+ return { state: motion ?? (observed.position <= 0 ? "closed" : "open"), position: observed.position };
42
+ }
43
+ };
44
+ }
45
+ function openCloseCover(settings) {
46
+ const config = (0, open_close_cover_1.normaliseOpenCloseConfig)(settings);
47
+ return {
48
+ positions: false,
49
+ transition(saved, event, now) {
50
+ const result = (0, open_close_cover_1.transitionOpenCloseCover)(saved, event, config, now);
51
+ return {
52
+ state: result.state,
53
+ commands: result.commands,
54
+ events: (0, open_close_cover_1.openCloseTargetStates)(result, now),
55
+ traces: result.traces,
56
+ status: { ...result.status, battery: null, observed: result.state.reports }
57
+ };
58
+ },
59
+ moving(saved) {
60
+ const state = saved;
61
+ return state ? (0, open_close_cover_1.openCloseMoving)({ motion: state.motion ?? null, phase: state.phase ?? "idle" }) : false;
62
+ },
63
+ generation: (saved) => saved?.generation ?? 0,
64
+ tickAt(state) {
65
+ const cover = state;
66
+ return cover.pending ? { at: cover.pending.deadlineAt, generation: cover.generation } : null;
67
+ },
68
+ view(saved) {
69
+ const state = saved;
70
+ if (!state)
71
+ return null;
72
+ if (state.motion === "opening" || state.motion === "closing")
73
+ return { state: state.motion, position: null };
74
+ // Stopped partway is open, as a cover entity counts anything not fully
75
+ // closed: with no position, Home Assistant would otherwise keep showing
76
+ // the last move (closed after closing) and refuse to close it.
77
+ if (state.position === "stopped")
78
+ return { state: "open", position: null };
79
+ return state.position && state.position !== "unknown" ? { state: state.position, position: null } : null;
80
+ }
81
+ };
82
+ }
83
+ //# sourceMappingURL=cover-models.js.map
@@ -0,0 +1,40 @@
1
+ import type { TraceEntry } from "./bounded-trace";
2
+ import type { CoverView } from "./cover-models";
3
+ import { type OptInPublishingConfig, type RetainedText } from "./publishing-node";
4
+ export interface CoverPublishingConfig extends OptInPublishingConfig {
5
+ name: string;
6
+ positions: boolean;
7
+ }
8
+ export declare const coverStateTopic: (config: {
9
+ area: string;
10
+ name: string;
11
+ }) => string;
12
+ export declare const coverPositionTopic: (config: {
13
+ area: string;
14
+ name: string;
15
+ }) => string;
16
+ export declare const coverCommandTopic: (config: {
17
+ area: string;
18
+ name: string;
19
+ }) => string;
20
+ export declare const coverSetPositionTopic: (config: {
21
+ area: string;
22
+ name: string;
23
+ }) => string;
24
+ export declare const coverAvailabilityTopic: (config: {
25
+ area: string;
26
+ name: string;
27
+ }) => string;
28
+ export declare function coverDiscovery(config: CoverPublishingConfig): RetainedText[];
29
+ export declare function coverStateOutputs(config: CoverPublishingConfig, view: CoverView | null): RetainedText[];
30
+ export declare const coverStartingOutputs: (config: CoverPublishingConfig, state: CoverView | null) => RetainedText[];
31
+ export declare const coverStoppingOutputs: (config: CoverPublishingConfig) => RetainedText[];
32
+ export declare const coverRetainedTopics: (config: CoverPublishingConfig) => string[];
33
+ export declare function coverInbound(msg: {
34
+ topic?: unknown;
35
+ payload?: unknown;
36
+ retain?: unknown;
37
+ }, config: CoverPublishingConfig): {
38
+ request: string | number | null;
39
+ traces: TraceEntry[];
40
+ } | null;
@@ -0,0 +1,102 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.coverRetainedTopics = exports.coverStoppingOutputs = exports.coverStartingOutputs = exports.coverAvailabilityTopic = exports.coverSetPositionTopic = exports.coverCommandTopic = exports.coverPositionTopic = exports.coverStateTopic = void 0;
4
+ exports.coverDiscovery = coverDiscovery;
5
+ exports.coverStateOutputs = coverStateOutputs;
6
+ exports.coverInbound = coverInbound;
7
+ const publishing_node_1 = require("./publishing-node");
8
+ const topic_names_1 = require("./topic-names");
9
+ function coverTopic(config, path) {
10
+ return "home/" + (0, topic_names_1.areaId)(config.area) + "/cover/" + (0, topic_names_1.areaId)(config.name) + "/" + path;
11
+ }
12
+ const coverStateTopic = (config) => coverTopic(config, "state");
13
+ exports.coverStateTopic = coverStateTopic;
14
+ const coverPositionTopic = (config) => coverTopic(config, "position");
15
+ exports.coverPositionTopic = coverPositionTopic;
16
+ const coverCommandTopic = (config) => coverTopic(config, "set");
17
+ exports.coverCommandTopic = coverCommandTopic;
18
+ const coverSetPositionTopic = (config) => coverTopic(config, "position/set");
19
+ exports.coverSetPositionTopic = coverSetPositionTopic;
20
+ const coverAvailabilityTopic = (config) => coverTopic(config, "availability");
21
+ exports.coverAvailabilityTopic = coverAvailabilityTopic;
22
+ function device(config) {
23
+ return { kind: "cover_" + (0, topic_names_1.areaId)(config.name), label: "Cover " + config.name, model: "Cover controller" };
24
+ }
25
+ // One cover entity: open, close and stop always; set position and the
26
+ // position it reports for a position cover.
27
+ function coverDiscovery(config) {
28
+ const positions = config.positions ? {
29
+ position_topic: (0, exports.coverPositionTopic)(config),
30
+ set_position_topic: (0, exports.coverSetPositionTopic)(config),
31
+ position_open: 100,
32
+ position_closed: 0
33
+ } : {};
34
+ return [
35
+ (0, publishing_node_1.discoveryEntry)(config, device(config), (0, exports.coverAvailabilityTopic)(config), {
36
+ component: "cover",
37
+ id: "cover_" + (0, topic_names_1.areaId)(config.name),
38
+ name: config.name,
39
+ fields: {
40
+ command_topic: (0, exports.coverCommandTopic)(config),
41
+ payload_open: "OPEN",
42
+ payload_close: "CLOSE",
43
+ payload_stop: "STOP",
44
+ state_topic: (0, exports.coverStateTopic)(config),
45
+ state_open: "open",
46
+ state_opening: "opening",
47
+ state_closed: "closed",
48
+ state_closing: "closing",
49
+ state_stopped: "stopped",
50
+ optimistic: false,
51
+ ...positions
52
+ }
53
+ })
54
+ ];
55
+ }
56
+ // The cover's state, and its position when it has one, once it has
57
+ // reported; nothing before.
58
+ function coverStateOutputs(config, view) {
59
+ if (!(0, publishing_node_1.publishesState)(config) || !view)
60
+ return [];
61
+ return [
62
+ (0, publishing_node_1.retainedText)((0, exports.coverStateTopic)(config), view.state),
63
+ ...(config.positions && view.position !== null ? [(0, publishing_node_1.retainedText)((0, exports.coverPositionTopic)(config), String(Math.round(view.position)))] : [])
64
+ ];
65
+ }
66
+ const cover = (0, publishing_node_1.publishingNode)({
67
+ entries: coverDiscovery,
68
+ availabilityTopic: (_area, config) => (0, exports.coverAvailabilityTopic)(config),
69
+ stateTopics: (config) => [(0, exports.coverStateTopic)(config), ...(config.positions ? [(0, exports.coverPositionTopic)(config)] : [])],
70
+ stateOutputs: coverStateOutputs
71
+ });
72
+ exports.coverStartingOutputs = cover.starting;
73
+ exports.coverStoppingOutputs = cover.stopping;
74
+ exports.coverRetainedTopics = cover.retainedTopics;
75
+ // A request from Home Assistant on the cover's set topics, as the payload the
76
+ // cover node takes from a flow: "open", "close", "stop" or a position. Read
77
+ // only while it publishes; retained messages and anything else are ignored
78
+ // with a trace. Anything else passes through (null).
79
+ function coverInbound(msg, config) {
80
+ if (!(0, publishing_node_1.publishesState)(config))
81
+ return null;
82
+ const command = msg.topic === (0, exports.coverCommandTopic)(config);
83
+ const position = config.positions && msg.topic === (0, exports.coverSetPositionTopic)(config);
84
+ if (!command && !position)
85
+ return null;
86
+ const event = command ? "command-set" : "position-set";
87
+ const ignored = (reason, details = {}) => ({ request: null, traces: [{ event, outcome: "ignored", reason, details }] });
88
+ if (msg.retain === true)
89
+ return ignored("retained-message");
90
+ const text = typeof msg.payload === "string" ? msg.payload.trim() : typeof msg.payload === "number" ? String(msg.payload) : "";
91
+ if (command) {
92
+ const requested = text.toUpperCase();
93
+ if (requested !== "OPEN" && requested !== "CLOSE" && requested !== "STOP")
94
+ return ignored("not-open-close-or-stop", { payload: text });
95
+ return { request: requested.toLowerCase(), traces: [] };
96
+ }
97
+ const value = text === "" ? NaN : Number(text);
98
+ if (!Number.isFinite(value) || value < 0 || value > 100)
99
+ return ignored("not-a-position", { payload: text });
100
+ return { request: Math.round(value), traces: [] };
101
+ }
102
+ //# sourceMappingURL=cover-publishing.js.map