@avantics/node-red-home-automation 6.1.0 → 6.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (115) hide show
  1. package/README.md +260 -46
  2. package/dist/lib/activity-lighting-controller.d.ts +1 -0
  3. package/dist/lib/activity-lighting-controller.js +37 -10
  4. package/dist/lib/avr-controller.d.ts +3 -0
  5. package/dist/lib/avr-controller.js +24 -2
  6. package/dist/lib/backlighting-controller.d.ts +5 -2
  7. package/dist/lib/backlighting-controller.js +27 -2
  8. package/dist/lib/cover-publishing.js +20 -6
  9. package/dist/lib/display-mode-controller.d.ts +3 -0
  10. package/dist/lib/display-mode-controller.js +43 -16
  11. package/dist/lib/esphome-sensor.d.ts +73 -0
  12. package/dist/lib/esphome-sensor.js +261 -0
  13. package/dist/lib/home-assistant-setting.d.ts +23 -0
  14. package/dist/lib/home-assistant-setting.js +39 -0
  15. package/dist/lib/motion-policy.d.ts +1 -0
  16. package/dist/lib/motion-policy.js +21 -1
  17. package/dist/lib/occupancy-fusion.d.ts +1 -0
  18. package/dist/lib/occupancy-fusion.js +15 -2
  19. package/dist/lib/room-controller.d.ts +1 -0
  20. package/dist/lib/room-controller.js +5 -0
  21. package/dist/lib/room-publishing.d.ts +1 -0
  22. package/dist/lib/room-publishing.js +11 -0
  23. package/dist/lib/scene-publishing.d.ts +30 -0
  24. package/dist/lib/scene-publishing.js +63 -0
  25. package/dist/lib/scene.d.ts +69 -0
  26. package/dist/lib/scene.js +223 -0
  27. package/dist/lib/shelly-cover.js +5 -0
  28. package/dist/lib/shelly-relay-light.d.ts +24 -0
  29. package/dist/lib/shelly-relay-light.js +93 -0
  30. package/dist/lib/shelly-switch.d.ts +8 -0
  31. package/dist/lib/shelly-switch.js +37 -8
  32. package/dist/lib/source-capability-controller.d.ts +4 -1
  33. package/dist/lib/source-capability-controller.js +49 -2
  34. package/dist/lib/target-observation.d.ts +14 -0
  35. package/dist/lib/target-observation.js +22 -0
  36. package/dist/lib/theatre-activity-controller.d.ts +5 -0
  37. package/dist/lib/theatre-activity-controller.js +91 -12
  38. package/dist/lib/theatre-metrics.js +6 -4
  39. package/dist/lib/theatre-slo.js +3 -0
  40. package/dist/lib/theatre-state-contract.d.ts +4 -2
  41. package/dist/lib/theatre-state-contract.js +51 -8
  42. package/dist/lib/theatre-state.d.ts +2 -0
  43. package/dist/lib/theatre-state.js +13 -1
  44. package/dist/lib/wled.d.ts +48 -0
  45. package/dist/lib/wled.js +105 -0
  46. package/dist/lib/z2m-power.d.ts +38 -0
  47. package/dist/lib/z2m-power.js +83 -0
  48. package/dist/lib/z2m-sensor.d.ts +37 -0
  49. package/dist/lib/z2m-sensor.js +88 -0
  50. package/dist/nodes/apollo-r-pro-1.d.ts +2 -0
  51. package/dist/nodes/apollo-r-pro-1.html +80 -0
  52. package/dist/nodes/apollo-r-pro-1.js +4 -0
  53. package/dist/nodes/avr-controller.html +82 -21
  54. package/dist/nodes/avr-controller.js +24 -12
  55. package/dist/nodes/backlighting-controller.js +3 -2
  56. package/dist/nodes/circadian-policy.html +84 -20
  57. package/dist/nodes/circadian-policy.js +30 -11
  58. package/dist/nodes/cover-controller.html +87 -25
  59. package/dist/nodes/cover-controller.js +25 -12
  60. package/dist/nodes/device-side.d.ts +21 -0
  61. package/dist/nodes/device-side.js +76 -0
  62. package/dist/nodes/display-mode-controller.js +11 -2
  63. package/dist/nodes/esphome-sensor.d.ts +2 -0
  64. package/dist/nodes/esphome-sensor.js +77 -0
  65. package/dist/nodes/everything-presence-one.d.ts +2 -0
  66. package/dist/nodes/everything-presence-one.html +78 -0
  67. package/dist/nodes/everything-presence-one.js +4 -0
  68. package/dist/nodes/home-assistant-discovery.d.ts +26 -0
  69. package/dist/nodes/home-assistant-discovery.js +64 -0
  70. package/dist/nodes/house-schedule-mqtt.html +70 -5
  71. package/dist/nodes/house-schedule-mqtt.js +22 -8
  72. package/dist/nodes/motion-policy.html +87 -22
  73. package/dist/nodes/motion-policy.js +44 -11
  74. package/dist/nodes/mqtt-broker-link.d.ts +41 -0
  75. package/dist/nodes/mqtt-broker-link.js +91 -0
  76. package/dist/nodes/occupancy-fusion.html +86 -20
  77. package/dist/nodes/occupancy-fusion.js +25 -12
  78. package/dist/nodes/publishing-side.d.ts +20 -0
  79. package/dist/nodes/publishing-side.js +66 -0
  80. package/dist/nodes/room-controller.html +106 -35
  81. package/dist/nodes/room-controller.js +63 -16
  82. package/dist/nodes/scene.d.ts +3 -0
  83. package/dist/nodes/scene.html +247 -0
  84. package/dist/nodes/scene.js +128 -0
  85. package/dist/nodes/shelly-cover.html +47 -2
  86. package/dist/nodes/shelly-cover.js +19 -4
  87. package/dist/nodes/shelly-switch.html +76 -6
  88. package/dist/nodes/shelly-switch.js +32 -7
  89. package/dist/nodes/source-capability-controller.js +3 -2
  90. package/dist/nodes/theatre-activity-controller.html +4 -2
  91. package/dist/nodes/theatre-state-contract.html +72 -9
  92. package/dist/nodes/theatre-state-contract.js +33 -14
  93. package/dist/nodes/wled.d.ts +3 -0
  94. package/dist/nodes/wled.html +56 -0
  95. package/dist/nodes/wled.js +53 -0
  96. package/dist/nodes/z2m-cover.html +47 -2
  97. package/dist/nodes/z2m-cover.js +21 -4
  98. package/dist/nodes/z2m-light.html +49 -3
  99. package/dist/nodes/z2m-light.js +40 -4
  100. package/dist/nodes/z2m-power.d.ts +3 -0
  101. package/dist/nodes/z2m-power.html +58 -0
  102. package/dist/nodes/z2m-power.js +53 -0
  103. package/dist/nodes/z2m-sensor.d.ts +3 -0
  104. package/dist/nodes/z2m-sensor.html +69 -0
  105. package/dist/nodes/z2m-sensor.js +62 -0
  106. package/examples/Cover control.json +38 -91
  107. package/examples/Cover pair.json +36 -3
  108. package/examples/Home theatre activity.json +46 -91
  109. package/examples/Home theatre equipment.json +34 -2
  110. package/examples/House schedule.json +58 -80
  111. package/examples/Room lighting.json +45 -44
  112. package/examples/Scene.json +167 -0
  113. package/examples/Shelly cover.json +36 -105
  114. package/examples/Shelly switch.json +52 -148
  115. package/package.json +8 -2
@@ -0,0 +1,24 @@
1
+ import type { TraceEntry } from "./bounded-trace";
2
+ import { type ExpectedCommand } from "./command-echo";
3
+ import type { LightReading } from "./light-contract";
4
+ import type { MqttPublication } from "./mqtt-message";
5
+ export interface RelayLightState {
6
+ expected: ExpectedCommand[];
7
+ lastOn: boolean | null;
8
+ reportedAt: string | null;
9
+ externalOn: boolean | null;
10
+ }
11
+ export declare function createRelayLightState(saved: Partial<RelayLightState> | undefined): RelayLightState;
12
+ export interface RelayLightStep {
13
+ publications: MqttPublication[];
14
+ readings: LightReading[];
15
+ traces: TraceEntry[];
16
+ }
17
+ export declare function relayLightFromRoom(state: RelayLightState, light: {
18
+ kind?: unknown;
19
+ state?: unknown;
20
+ restyle?: unknown;
21
+ id?: unknown;
22
+ }, target: string, commandTopic: string, relayOn: boolean | null, nowMs: number): RelayLightStep;
23
+ export declare function relayLightCommandSeen(state: RelayLightState, payload: unknown, target: string, relayOn: boolean | null, nowMs: number): RelayLightStep;
24
+ export declare function relayLightReport(state: RelayLightState, on: boolean, previous: boolean | null, target: string, nowMs: number): RelayLightStep;
@@ -0,0 +1,93 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createRelayLightState = createRelayLightState;
4
+ exports.relayLightFromRoom = relayLightFromRoom;
5
+ exports.relayLightCommandSeen = relayLightCommandSeen;
6
+ exports.relayLightReport = relayLightReport;
7
+ const command_echo_1 = require("./command-echo");
8
+ function createRelayLightState(saved) {
9
+ return {
10
+ expected: Array.isArray(saved?.expected) ? saved.expected.map((entry) => ({ ...entry })) : [],
11
+ lastOn: typeof saved?.lastOn === "boolean" ? saved.lastOn : null,
12
+ reportedAt: typeof saved?.reportedAt === "string" ? saved.reportedAt : null,
13
+ externalOn: typeof saved?.externalOn === "boolean" ? saved.externalOn : null
14
+ };
15
+ }
16
+ // The relay command as remembered and matched: the room's power, as the echo
17
+ // queue reads it.
18
+ const asCommand = (on) => ({ state: on ? "ON" : "OFF" });
19
+ const step = () => ({ publications: [], readings: [], traces: [] });
20
+ // A room message for this light: a command switches the relay; an observe
21
+ // asks it to report. Brightness, colour and the rest don't apply to a relay
22
+ // and are left out (the room's target should be marked onOffOnly).
23
+ function relayLightFromRoom(state, light, target, commandTopic, relayOn, nowMs) {
24
+ const out = step();
25
+ if (light.kind === "observe") {
26
+ out.publications.push({ topic: commandTopic, payload: "status_update" });
27
+ out.traces.push({ event: "observe", outcome: "sent", reason: null, details: {} });
28
+ return out;
29
+ }
30
+ const on = light.state === "ON";
31
+ if (light.state !== "ON" && light.state !== "OFF") {
32
+ out.traces.push({ event: "command", outcome: "ignored", reason: "not-on-or-off", details: { state: String(light.state) } });
33
+ return out;
34
+ }
35
+ if (light.restyle === true) {
36
+ // A relay has no style: a restyle of a light already on changes nothing.
37
+ if (relayOn === true) {
38
+ out.readings.push({ kind: "skipped", target, id: String(light.id), observedAt: state.reportedAt ?? new Date(nowMs).toISOString() });
39
+ out.traces.push({ event: "command", outcome: "skipped", reason: "relay-has-no-style", details: { id: light.id } });
40
+ return out;
41
+ }
42
+ out.readings.push({ kind: "sent", target, id: String(light.id) });
43
+ }
44
+ state.expected = (0, command_echo_1.rememberCommand)(state.expected, asCommand(on), nowMs);
45
+ state.lastOn = on;
46
+ out.publications.push({ topic: commandTopic, payload: on ? "on" : "off" });
47
+ out.traces.push({ event: "command", outcome: "sent", reason: null, details: { id: light.id, power: on ? "on" : "off" } });
48
+ return out;
49
+ }
50
+ // A command seen on the relay's command topic: its own echo, or someone
51
+ // else's (another flow, the device's own app over MQTT), which the room may
52
+ // take as a person's change.
53
+ function relayLightCommandSeen(state, payload, target, relayOn, nowMs) {
54
+ const out = step();
55
+ const text = typeof payload === "string" ? payload.trim().toLowerCase() : "";
56
+ if (text !== "on" && text !== "off" && text !== "toggle")
57
+ return out;
58
+ if (text !== "toggle") {
59
+ const echo = (0, command_echo_1.checkCommandEcho)(state.expected, asCommand(text === "on"), nowMs);
60
+ state.expected = echo.queue;
61
+ if (echo.own) {
62
+ out.traces.push({ event: "command", outcome: "own", reason: "echo", details: { power: text } });
63
+ return out;
64
+ }
65
+ state.externalOn = text === "on";
66
+ }
67
+ else {
68
+ // A toggle's result: the opposite of the relay's last report, when known.
69
+ state.externalOn = relayOn === null ? null : !relayOn;
70
+ }
71
+ out.readings.push({ kind: "external", target, state: text.toUpperCase(), setsValues: false, observedAt: new Date(nowMs).toISOString() });
72
+ out.traces.push({ event: "command", outcome: "external", reason: null, details: { power: text } });
73
+ return out;
74
+ }
75
+ // The relay's output as it reports it: the room's reading, and a change it
76
+ // didn't cause (a person at a switch the relay follows, another flow) as
77
+ // someone else's, unless already reported from their command.
78
+ function relayLightReport(state, on, previous, target, nowMs) {
79
+ const out = step();
80
+ const observedAt = new Date(nowMs).toISOString();
81
+ const report = (0, command_echo_1.checkStateReport)(state.expected, on, nowMs);
82
+ state.expected = report.queue;
83
+ state.reportedAt = observedAt;
84
+ const changed = previous !== null && previous !== on;
85
+ if (changed && report.verdict === "external" && state.externalOn !== on) {
86
+ out.readings.push({ kind: "external", target, state: on ? "ON" : "OFF", setsValues: false, observedAt });
87
+ out.traces.push({ event: "relay", outcome: "external", reason: null, details: { on } });
88
+ }
89
+ state.externalOn = null;
90
+ out.readings.push({ kind: "observed", target, on, fields: [], showsLast: state.lastOn === on, observedAt });
91
+ return out;
92
+ }
93
+ //# sourceMappingURL=shelly-relay-light.js.map
@@ -1,5 +1,7 @@
1
1
  import type { TraceEntry } from "./bounded-trace";
2
2
  import { type MqttPublication } from "./mqtt-message";
3
+ import type { LightReading } from "./light-contract";
4
+ import { type RelayLightState } from "./shelly-relay-light";
3
5
  export declare const SHELLY_SWITCH_MODES: readonly ["auto", "detached", "relay"];
4
6
  export type ShellySwitchMode = typeof SHELLY_SWITCH_MODES[number];
5
7
  export declare const CONFIG_TIMEOUT_MS = 10000;
@@ -7,6 +9,7 @@ export interface ShellySwitchConfig {
7
9
  topicPrefix: string;
8
10
  channel: number;
9
11
  mode: ShellySwitchMode;
12
+ target: string | null;
10
13
  }
11
14
  export interface DeviceSetup {
12
15
  inMode: string;
@@ -21,6 +24,7 @@ export interface ShellySwitchState {
21
24
  online: boolean;
22
25
  relayOn: boolean | null;
23
26
  inputState: boolean | null;
27
+ light: RelayLightState;
24
28
  }
25
29
  export interface ShellySwitchMessage {
26
30
  topic?: unknown;
@@ -31,6 +35,7 @@ export interface ShellySwitchMessage {
31
35
  switchCommand?: {
32
36
  power?: unknown;
33
37
  };
38
+ light?: unknown;
34
39
  }
35
40
  export type SwitchReading = {
36
41
  source: "switch";
@@ -65,6 +70,7 @@ export declare function normaliseShellySwitchConfig(config: {
65
70
  topicPrefix?: unknown;
66
71
  channel?: unknown;
67
72
  mode?: unknown;
73
+ target?: unknown;
68
74
  }): ShellySwitchConfig;
69
75
  export declare function shellySwitchTopics(config: ShellySwitchConfig): {
70
76
  command: string;
@@ -83,11 +89,13 @@ export declare function transitionShellySwitch(saved: Partial<ShellySwitchState>
83
89
  state: ShellySwitchState;
84
90
  publications: MqttPublication[];
85
91
  readings: SwitchReading[];
92
+ lightReadings: LightReading[];
86
93
  traces: TraceEntry<Record<string, unknown>>[];
87
94
  status: {
88
95
  text: string;
89
96
  fill: "green" | "yellow" | "red" | "grey";
90
97
  };
98
+ ignored: boolean;
91
99
  };
92
100
  export declare function shellySwitchStatus(config: ShellySwitchConfig, state: ShellySwitchState): {
93
101
  text: string;
@@ -8,6 +8,7 @@ exports.transitionShellySwitch = transitionShellySwitch;
8
8
  exports.shellySwitchStatus = shellySwitchStatus;
9
9
  const mqtt_message_1 = require("./mqtt-message");
10
10
  const shelly_gen2_1 = require("./shelly-gen2");
11
+ const shelly_relay_light_1 = require("./shelly-relay-light");
11
12
  exports.SHELLY_SWITCH_MODES = ["auto", "detached", "relay"];
12
13
  // How long to wait for the device's configuration before taking it as
13
14
  // unreadable.
@@ -27,7 +28,9 @@ function normaliseShellySwitchConfig(config) {
27
28
  const mode = config.mode === undefined || config.mode === "" ? "auto" : String(config.mode);
28
29
  if (!exports.SHELLY_SWITCH_MODES.includes(mode))
29
30
  throw new Error("mode must be auto, detached or relay");
30
- return { topicPrefix, channel, mode: mode };
31
+ // Not set, or empty: not a room light.
32
+ const target = typeof config.target === "string" && config.target.trim() ? config.target.trim() : null;
33
+ return { topicPrefix, channel, mode: mode, target };
31
34
  }
32
35
  function shellySwitchTopics(config) {
33
36
  const component = (0, shelly_gen2_1.shellyTopics)(config.topicPrefix, `switch:${config.channel}`);
@@ -59,18 +62,26 @@ function transitionShellySwitch(saved, msg, config, now) {
59
62
  requestId: Number.isInteger(saved?.requestId) ? saved.requestId : 0,
60
63
  online: saved?.online !== false,
61
64
  relayOn: typeof saved?.relayOn === "boolean" ? saved.relayOn : null,
62
- inputState: typeof saved?.inputState === "boolean" ? saved.inputState : null
65
+ inputState: typeof saved?.inputState === "boolean" ? saved.inputState : null,
66
+ light: (0, shelly_relay_light_1.createRelayLightState)(saved?.light)
63
67
  };
64
68
  const topics = shellySwitchTopics(config);
65
69
  const nowMs = now.getTime();
66
70
  const observedAt = now.toISOString();
67
71
  const publications = [];
68
72
  const readings = [];
73
+ // For the room, with a Target.
74
+ const lightReadings = [];
69
75
  const traces = [];
70
76
  const trace = (event, outcome, reason = null, details = {}) => {
71
77
  traces.push({ event, outcome, reason, details });
72
78
  };
73
79
  const mode = () => effectiveMode(config, state);
80
+ function light(result) {
81
+ publications.push(...result.publications);
82
+ lightReadings.push(...result.readings);
83
+ traces.push(...result.traces);
84
+ }
74
85
  // Asks for the device's configuration and its whole status (the baseline).
75
86
  function readDevice(reason) {
76
87
  state.requestId += 1;
@@ -95,20 +106,31 @@ function transitionShellySwitch(saved, msg, config, now) {
95
106
  function relayOutput(status) {
96
107
  if (typeof status.output !== "boolean")
97
108
  return;
109
+ const previous = state.relayOn;
98
110
  state.relayOn = status.output;
111
+ if (config.target)
112
+ light((0, shelly_relay_light_1.relayLightReport)(state.light, status.output, previous, config.target, nowMs));
99
113
  if (mode() === "relay") {
100
114
  // The device names the source of the last change: its own input is a person at the switch.
101
115
  const person = status.source === "button" || status.source === "switch" || status.source === "input";
102
116
  readings.push({ switchState: { kind: "observed", on: status.output, person, observedAt } });
103
117
  }
104
- else if (mode() === "detached" && !status.output) {
118
+ else if (mode() === "detached" && !status.output && !config.target) {
105
119
  trace("relay", "off", "bulbs-unpowered");
106
120
  }
107
121
  }
108
122
  const inputKey = () => `input:${state.setup?.inputId ?? config.channel}`;
109
123
  const command = msg.switchCommand;
110
124
  const own = msg.shellySwitch;
111
- if ((0, mqtt_message_1.isObject)(own) && own.kind === "startup") {
125
+ const room = (0, mqtt_message_1.isObject)(msg.light) && (msg.light.kind === "command" || msg.light.kind === "observe") ? msg.light : null;
126
+ if (room) {
127
+ // The room's messages for every light arrive here; only this one's count.
128
+ if (!config.target || room.target !== config.target) {
129
+ return { state, publications, readings, lightReadings, traces, status: shellySwitchStatus(config, state), ignored: true };
130
+ }
131
+ light((0, shelly_relay_light_1.relayLightFromRoom)(state.light, room, config.target, topics.command, state.relayOn, nowMs));
132
+ }
133
+ else if ((0, mqtt_message_1.isObject)(own) && own.kind === "startup") {
112
134
  readDevice("startup");
113
135
  }
114
136
  else if ((0, mqtt_message_1.isObject)(own) && own.kind === "tick") {
@@ -207,15 +229,22 @@ function transitionShellySwitch(saved, msg, config, now) {
207
229
  readings.push({ source: "switch-availability", payload: message.online ? "online" : "offline" });
208
230
  if (mode() === "relay")
209
231
  readings.push({ switchState: { kind: "availability", online: message.online } });
232
+ if (config.target)
233
+ lightReadings.push({ kind: "availability", target: config.target, online: message.online });
210
234
  // Back online: read its configuration again, and a fresh baseline.
211
235
  if (message.online)
212
236
  readDevice("online");
213
237
  }
214
- else if (msg.topic !== topics.rpc && msg.topic !== topics.command && msg.topic !== topics.deviceCommand) {
238
+ else if (msg.topic === topics.command) {
239
+ // Its own commands, or someone else's: only a room light reads them.
240
+ if (config.target)
241
+ light((0, shelly_relay_light_1.relayLightCommandSeen)(state.light, msg.payload, config.target, state.relayOn, nowMs));
242
+ }
243
+ else if (msg.topic !== topics.rpc && msg.topic !== topics.deviceCommand) {
215
244
  trace("message", "ignored", "unrelated-message", { topic: typeof msg.topic === "string" ? msg.topic : null });
216
245
  }
217
246
  }
218
- return { state, publications, readings, traces, status: shellySwitchStatus(config, state) };
247
+ return { state, publications, readings, lightReadings, traces, status: shellySwitchStatus(config, state), ignored: false };
219
248
  }
220
249
  // What the node's status shows: the mode found, and anything to act on.
221
250
  function shellySwitchStatus(config, state) {
@@ -229,9 +258,9 @@ function shellySwitchStatus(config, state) {
229
258
  : { text: "configuration unreadable: set Mode", fill: "red" };
230
259
  }
231
260
  const found = config.mode === "auto" ? setup : `${mode} (set)${setup ? `, device ${setup}` : ""}`;
232
- if (mode === "detached" && state.relayOn === false)
261
+ if (mode === "detached" && state.relayOn === false && !config.target)
233
262
  return { text: `${found} · relay off: bulbs unpowered`, fill: "yellow" };
234
- const now = mode === "relay" && state.relayOn !== null ? ` · ${state.relayOn ? "on" : "off"}` : "";
263
+ const now = (mode === "relay" || config.target) && state.relayOn !== null ? ` · ${config.target ? config.target + " " : ""}${state.relayOn ? "on" : "off"}` : "";
235
264
  return { text: `${found ?? mode}${now}`, fill: "green" };
236
265
  }
237
266
  //# sourceMappingURL=shelly-switch.js.map
@@ -1,3 +1,4 @@
1
+ import { type TargetObservationOutput } from "./target-observation";
1
2
  import { type Carried } from "./adopted-request";
2
3
  import type { TraceEntry } from "./bounded-trace";
3
4
  import { type TargetStateEvent } from "./target-state";
@@ -57,6 +58,7 @@ export interface SourceState {
57
58
  refreshAsked: boolean;
58
59
  adoptedGeneration: number | null;
59
60
  carried?: Carried | null;
61
+ probe?: Record<string, SourceObservationStatus | null> | null;
60
62
  }
61
63
  export type SourceEvent = {
62
64
  kind: "intent";
@@ -100,7 +102,7 @@ export declare function normaliseConfig(config?: {
100
102
  export interface SourceCommand {
101
103
  sourceId: string | null;
102
104
  operation: SourceAction | "observe";
103
- requestId: string;
105
+ requestId: string | null;
104
106
  generation: number;
105
107
  attempt: number;
106
108
  }
@@ -140,6 +142,7 @@ export declare function transitionSource(saved: Partial<SourceState> | undefined
140
142
  state: SourceState;
141
143
  commands: SourceCommand[];
142
144
  events: SourceOutputEvent[];
145
+ observations: TargetObservationOutput[];
143
146
  traces: TraceEntry<Record<string, unknown>>[];
144
147
  status: {
145
148
  phase: string;
@@ -4,6 +4,7 @@ exports.normaliseCapabilities = normaliseCapabilities;
4
4
  exports.normaliseConfig = normaliseConfig;
5
5
  exports.readinessEvidence = readinessEvidence;
6
6
  exports.transitionSource = transitionSource;
7
+ const target_observation_1 = require("./target-observation");
7
8
  const adopted_request_1 = require("./adopted-request");
8
9
  const settings_1 = require("./settings");
9
10
  const observation_time_1 = require("./observation-time");
@@ -113,7 +114,8 @@ function initialState(saved = {}, sources = {}) {
113
114
  fallbackUsed: saved.fallbackUsed === true,
114
115
  refreshAsked: saved.refreshAsked === true,
115
116
  adoptedGeneration: (0, adopted_request_1.savedAdoptedGeneration)(saved),
116
- carried: (0, adopted_request_1.savedCarried)(saved)
117
+ carried: (0, adopted_request_1.savedCarried)(saved),
118
+ probe: saved.probe && typeof saved.probe === "object" && !Array.isArray(saved.probe) ? { ...saved.probe } : null
117
119
  };
118
120
  }
119
121
  function commandFor(state, operation, attempt = 1) {
@@ -206,6 +208,19 @@ function transitionSource(saved, msg, suppliedConfig = {}, nowValue = new Date()
206
208
  const event = msg?.sourceCapability;
207
209
  const commands = [];
208
210
  const events = [];
211
+ // Its answer to the theatre's recovery probe, for output 2 as the events.
212
+ const observations = [];
213
+ // What the probed sources show, in the activity profiles' terms: the one
214
+ // active source, or every source inactive; otherwise nothing is known.
215
+ function probeValues(statuses) {
216
+ const entries = Object.entries(statuses);
217
+ const active = entries.filter(([, status]) => status === "active" || status === "available").map(([sourceId]) => sourceId);
218
+ if (active.length === 1)
219
+ return { action: "activate", sourceId: active[0] };
220
+ if (entries.length && entries.every(([, status]) => status === "inactive"))
221
+ return { action: "deactivate" };
222
+ return {};
223
+ }
209
224
  const traces = [];
210
225
  if (!event || typeof event.kind !== "string") {
211
226
  throw new Error("msg.sourceCapability.kind is required");
@@ -353,6 +368,23 @@ function transitionSource(saved, msg, suppliedConfig = {}, nowValue = new Date()
353
368
  }
354
369
  }
355
370
  function acceptObservation(event) {
371
+ if (!state.desired && state.probe && Object.hasOwn(state.probe, event.sourceId)) {
372
+ // The probe's answer, whatever generation the transport echoes.
373
+ if (!Object.hasOwn(OBSERVATION_STATUSES, event.status))
374
+ throw new Error("unknown source observation status");
375
+ const probedAt = (0, observation_time_1.parseObservedAt)(event.observedAt);
376
+ if (probedAt === null || typeof event.observedAt !== "string")
377
+ throw new Error("source observedAt must be an ISO timestamp");
378
+ state.probe[event.sourceId] = event.status;
379
+ trace("probe", "read", null, { sourceId: event.sourceId, status: event.status });
380
+ if (Object.values(state.probe).every((status) => status !== null)) {
381
+ const values = probeValues(state.probe);
382
+ observations.push((0, target_observation_1.targetObservation)("source", (0, observation_time_1.boundObservedAt)(event.observedAt, probedAt, nowMs).observedAt, values));
383
+ state.probe = null;
384
+ trace("probe", "answered", null, values);
385
+ }
386
+ return;
387
+ }
356
388
  if (!Number.isInteger(event.generation) || event.generation < 1) {
357
389
  throw new Error("source observation generation must be a positive integer");
358
390
  }
@@ -494,6 +526,21 @@ function transitionSource(saved, msg, suppliedConfig = {}, nowValue = new Date()
494
526
  state.refreshAsked = true;
495
527
  trace("refresh", "querying", null, { sourceId: state.desired.sourceId });
496
528
  }
529
+ else if (!state.desired && (0, target_observation_1.isProbe)(event)) {
530
+ // The theatre's recovery probe: read every source that can be read, and
531
+ // say what they show. With none, nothing is known.
532
+ const readable = Object.entries(config.sources).filter(([, source]) => source.confirmation !== "unavailable").map(([sourceId]) => sourceId);
533
+ if (readable.length) {
534
+ state.probe = Object.fromEntries(readable.map((sourceId) => [sourceId, null]));
535
+ for (const sourceId of readable)
536
+ commands.push({ sourceId, operation: "observe", requestId: null, generation: 0, attempt: 0 });
537
+ trace("probe", "querying", null, { sources: readable });
538
+ }
539
+ else {
540
+ observations.push((0, target_observation_1.targetObservation)("source", now.toISOString(), {}));
541
+ trace("probe", "answered", "no-readable-source");
542
+ }
543
+ }
497
544
  else if (event.kind === "startup" || event.kind === "refresh") {
498
545
  state.refreshAsked = false;
499
546
  if (state.desired) {
@@ -535,6 +582,6 @@ function transitionSource(saved, msg, suppliedConfig = {}, nowValue = new Date()
535
582
  const outputEvent = (event.kind === "refresh" && state.refreshAsked) || replacedRequest ? null : targetEvent(state, now, config.sources);
536
583
  if (outputEvent)
537
584
  events.push(outputEvent);
538
- return { state, commands, events, traces, status: statusFor(state) };
585
+ return { state, commands, events, observations, traces, status: statusFor(state) };
539
586
  }
540
587
  //# sourceMappingURL=source-capability-controller.js.map
@@ -0,0 +1,14 @@
1
+ export declare function isProbe(event: {
2
+ kind?: unknown;
3
+ requestId?: unknown;
4
+ generation?: unknown;
5
+ }): boolean;
6
+ export interface TargetObservationOutput {
7
+ theatre: {
8
+ kind: "target_observation";
9
+ target: string;
10
+ observedAt: string;
11
+ values: Record<string, unknown>;
12
+ };
13
+ }
14
+ export declare function targetObservation(target: string, observedAt: string, values: Record<string, unknown>): TargetObservationOutput;
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isProbe = isProbe;
4
+ exports.targetObservation = targetObservation;
5
+ // The recovery probe and its answer, for every theatre target owner.
6
+ //
7
+ // After a start with nothing saved, the theatre activity controller sends
8
+ // each owner a refresh with no request (requestId null, generation 0): the
9
+ // probe. An owner with no request of its own answers it by reading its
10
+ // device and reporting what it shows, in the activity profiles' terms, on
11
+ // the output its target states use:
12
+ // msg.theatre = {kind: "target_observation", target, observedAt, values}
13
+ // The activity controller adopts the one activity those observations match,
14
+ // if any, and commands nothing.
15
+ function isProbe(event) {
16
+ return event.kind === "refresh" && (event.requestId === null || event.requestId === undefined || event.requestId === "") &&
17
+ !(Number.isInteger(event.generation) && event.generation >= 1);
18
+ }
19
+ function targetObservation(target, observedAt, values) {
20
+ return { theatre: { kind: "target_observation", target, observedAt, values } };
21
+ }
22
+ //# sourceMappingURL=target-observation.js.map
@@ -25,6 +25,11 @@ export type TheatreInputEvent = {
25
25
  details?: Record<string, unknown>;
26
26
  } | {
27
27
  kind: "startup" | "refresh";
28
+ } | {
29
+ kind: "target_observation";
30
+ target: TheatreTarget;
31
+ observedAt: string;
32
+ values: Record<string, unknown>;
28
33
  } | {
29
34
  kind: "tick";
30
35
  generation?: number;