@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
@@ -0,0 +1,154 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.circadianRetainedTopics = exports.circadianStoppingOutputs = exports.circadianAvailabilityTopic = exports.circadianTimeTopic = exports.circadianSwitchTopic = void 0;
4
+ exports.circadianDiscovery = circadianDiscovery;
5
+ exports.roomCircadianDiscoveryTopics = roomCircadianDiscoveryTopics;
6
+ exports.circadianStateTopics = circadianStateTopics;
7
+ exports.circadianStateOutputs = circadianStateOutputs;
8
+ exports.circadianStartingOutputs = circadianStartingOutputs;
9
+ exports.circadianInbound = circadianInbound;
10
+ const ha_discovery_1 = require("./ha-discovery");
11
+ const publishing_node_1 = require("./publishing-node");
12
+ const topic_names_1 = require("./topic-names");
13
+ const circadianSwitchTopic = (area, which, end) => (0, topic_names_1.homeTopic)(area, which, end);
14
+ exports.circadianSwitchTopic = circadianSwitchTopic;
15
+ const circadianTimeTopic = (area, which, end) => (0, topic_names_1.homeTopic)(area, which, end);
16
+ exports.circadianTimeTopic = circadianTimeTopic;
17
+ const circadianAvailabilityTopic = (area) => (0, topic_names_1.homeTopic)(area, "circadian", "availability");
18
+ exports.circadianAvailabilityTopic = circadianAvailabilityTopic;
19
+ const DEVICE = { kind: "circadian", label: "Circadian Lighting", model: "Circadian lighting policy" };
20
+ const TIMES = [
21
+ ["wake-time", "wake_time", "Wake time", "mdi:weather-sunset-up"],
22
+ ["bed-time", "bed_time", "Bed time", "mdi:weather-night"]
23
+ ];
24
+ function circadianDiscovery(config) {
25
+ const availability = (0, exports.circadianAvailabilityTopic)(config.area);
26
+ const toggle = (which, id, name, icon) => (0, publishing_node_1.discoveryEntry)(config, DEVICE, availability, {
27
+ component: "switch", id, name, fields: {
28
+ icon,
29
+ command_topic: (0, exports.circadianSwitchTopic)(config.area, which, "set"),
30
+ state_topic: (0, exports.circadianSwitchTopic)(config.area, which, "state"),
31
+ payload_on: "ON",
32
+ payload_off: "OFF",
33
+ optimistic: false
34
+ }
35
+ });
36
+ return [
37
+ toggle("circadian", "circadian_lighting", "Circadian lighting", "mdi:clock-time-seven-outline"),
38
+ toggle("sleep", "sleep", "Sleep", "mdi:sleep"),
39
+ ...(config.timesFrom === "house" ? [] : TIMES.map(([which, id, name, icon]) => (0, publishing_node_1.discoveryEntry)(config, DEVICE, availability, {
40
+ component: "time", id, name, fields: {
41
+ icon,
42
+ command_topic: (0, exports.circadianTimeTopic)(config.area, which, "set"),
43
+ state_topic: (0, exports.circadianTimeTopic)(config.area, which, "state"),
44
+ entity_category: "config"
45
+ }
46
+ })))
47
+ ];
48
+ }
49
+ // The discovery topics of the circadian, sleep, wake and bed entities a room
50
+ // controller published under the area scheme before 6.0, which the policy
51
+ // clears before announcing its own, so Home Assistant can give the new
52
+ // entities the old entity IDs. (A room named after its node ID clears its
53
+ // own, at its start.)
54
+ function roomCircadianDiscoveryTopics(config, idScheme = "area", nodeId) {
55
+ const owner = { area: config.area, discoveryPrefix: config.discoveryPrefix, idScheme, nodeId };
56
+ return [
57
+ (0, ha_discovery_1.discoveryEntity)(owner, "switch", "circadian_lighting").topic,
58
+ (0, ha_discovery_1.discoveryEntity)(owner, "switch", "sleep").topic,
59
+ (0, ha_discovery_1.discoveryEntity)(owner, "time", "wake_time").topic,
60
+ (0, ha_discovery_1.discoveryEntity)(owner, "time", "bed_time").topic
61
+ ];
62
+ }
63
+ // The state topics the policy publishes, which a room controller with the
64
+ // same area must never clear.
65
+ function circadianStateTopics(area) {
66
+ return [
67
+ (0, exports.circadianSwitchTopic)(area, "circadian", "state"),
68
+ (0, exports.circadianSwitchTopic)(area, "sleep", "state"),
69
+ (0, exports.circadianTimeTopic)(area, "wake-time", "state"),
70
+ (0, exports.circadianTimeTopic)(area, "bed-time", "state")
71
+ ];
72
+ }
73
+ // "HH:MM:SS", as Home Assistant's time entity reads it.
74
+ function timeState(time) {
75
+ return time + ":00";
76
+ }
77
+ function circadianStateOutputs(config, state) {
78
+ if (!(0, publishing_node_1.publishesState)(config))
79
+ return [];
80
+ const outputs = [
81
+ (0, publishing_node_1.retainedText)((0, exports.circadianSwitchTopic)(config.area, "circadian", "state"), state.enabled ? "ON" : "OFF"),
82
+ (0, publishing_node_1.retainedText)((0, exports.circadianSwitchTopic)(config.area, "sleep", "state"), state.sleeping ? "ON" : "OFF")
83
+ ];
84
+ if (config.timesFrom !== "house") {
85
+ outputs.push((0, publishing_node_1.retainedText)((0, exports.circadianTimeTopic)(config.area, "wake-time", "state"), timeState(state.schedule.wakeTime)), (0, publishing_node_1.retainedText)((0, exports.circadianTimeTopic)(config.area, "bed-time", "state"), timeState(state.schedule.bedTime)));
86
+ }
87
+ return outputs;
88
+ }
89
+ const circadian = (0, publishing_node_1.publishingNode)({
90
+ entries: circadianDiscovery,
91
+ availabilityTopic: exports.circadianAvailabilityTopic,
92
+ stateTopics: (config) => circadianStateTopics(config.area).filter((topic) => config.timesFrom !== "house" || !/\/(wake|bed)-time\/state$/.test(topic)),
93
+ stateOutputs: circadianStateOutputs
94
+ });
95
+ // What it publishes as the flows start and at its startup message. First,
96
+ // with discovery on, the circadian, sleep, wake and bed entries older room
97
+ // controllers published before 6.0 under its area, cleared: those named
98
+ // after the area, and those named after each room node with its area
99
+ // (`roomNodeIds`, found in the flows, migrated or not). Then its own entries,
100
+ // availability and state, in the same batch, so Home Assistant always
101
+ // removes the old entities before it sees the new ones and can give them the
102
+ // old entity IDs. Following a house, its own time entities are cleared too.
103
+ function circadianStartingOutputs(config, state, roomNodeIds = []) {
104
+ return [...circadianClears(config, roomNodeIds), ...circadian.starting(config, state)];
105
+ }
106
+ function circadianClears(config, roomNodeIds) {
107
+ if (!(0, publishing_node_1.publishesDiscovery)(config))
108
+ return [];
109
+ const topics = [
110
+ ...roomCircadianDiscoveryTopics(config),
111
+ ...roomNodeIds.flatMap((nodeId) => roomCircadianDiscoveryTopics(config, "node", nodeId))
112
+ ];
113
+ if (config.timesFrom === "house") {
114
+ topics.push(...circadianDiscovery({ ...config, timesFrom: "own" }).map(({ topic }) => topic).filter((topic) => /\/time\//.test(topic)));
115
+ }
116
+ return [...new Set(topics)].map((topic) => (0, publishing_node_1.retainedText)(topic, ""));
117
+ }
118
+ exports.circadianStoppingOutputs = circadian.stopping;
119
+ exports.circadianRetainedTopics = circadian.retainedTopics;
120
+ // MQTT the policy reads, as its own events: ON or OFF on the circadian and
121
+ // sleep set topics, and a time on its own wake and bed set topics. Read only
122
+ // while it publishes; retained messages and other payloads are ignored with a
123
+ // trace. Anything else is not for these topics.
124
+ function circadianInbound(msg, config) {
125
+ if (!(0, publishing_node_1.publishesState)(config) || typeof msg.topic !== "string")
126
+ return null;
127
+ const area = config.area;
128
+ const sets = [
129
+ [(0, exports.circadianSwitchTopic)(area, "circadian", "set"), "circadian-set", (text) => onOff("circadian", text), "not-on-or-off"],
130
+ [(0, exports.circadianSwitchTopic)(area, "sleep", "set"), "sleep-set", (text) => onOff("sleep", text), "not-on-or-off"]
131
+ ];
132
+ if (config.timesFrom !== "house") {
133
+ sets.push([(0, exports.circadianTimeTopic)(area, "wake-time", "set"), "wake-time-set", (text) => time("wakeTime", text), "not-a-time"], [(0, exports.circadianTimeTopic)(area, "bed-time", "set"), "bed-time-set", (text) => time("bedTime", text), "not-a-time"]);
134
+ }
135
+ const set = sets.find(([topic]) => topic === msg.topic);
136
+ if (!set)
137
+ return null;
138
+ const [, name, read, invalid] = set;
139
+ const ignored = (reason, details = {}) => ({ event: { kind: "ignored" }, traces: [{ event: name, outcome: "ignored", reason, details }] });
140
+ if (msg.retain === true)
141
+ return ignored("retained-message");
142
+ const text = typeof msg.payload === "string" ? msg.payload.trim() : "";
143
+ const event = read(text);
144
+ return event ? { event, traces: [] } : ignored(invalid, { payload: text });
145
+ }
146
+ function onOff(kind, text) {
147
+ const requested = text.toUpperCase();
148
+ return requested === "ON" || requested === "OFF" ? { kind, enabled: requested === "ON" } : null;
149
+ }
150
+ function time(key, text) {
151
+ const value = (0, ha_discovery_1.timeEntityValue)(text);
152
+ return value === null ? null : { kind: "schedule", [key]: value };
153
+ }
154
+ //# sourceMappingURL=circadian-publishing.js.map
@@ -9,3 +9,7 @@ export interface DaySchedule {
9
9
  export declare function clockMinutes(value: unknown): number | null;
10
10
  export declare function resolveSchedule(wakeTime?: unknown, bedTime?: unknown): DaySchedule;
11
11
  export declare function scheduleLevel(now: Date, wakeTime: string, bedTime: string): number;
12
+ export declare function requestedSchedule(current: DaySchedule, event: {
13
+ wakeTime?: unknown;
14
+ bedTime?: unknown;
15
+ }): DaySchedule | null;
@@ -4,6 +4,7 @@ exports.DEFAULT_BED_TIME = exports.DEFAULT_WAKE_TIME = exports.WIND_DOWN_MINUTES
4
4
  exports.clockMinutes = clockMinutes;
5
5
  exports.resolveSchedule = resolveSchedule;
6
6
  exports.scheduleLevel = scheduleLevel;
7
+ exports.requestedSchedule = requestedSchedule;
7
8
  // A daily wake and bed schedule, and how far into the day a given moment is.
8
9
  // Pure: it knows nothing about lights, and takes the time it is asked about.
9
10
  // After waking, the day level rises over this long; before bed it winds down
@@ -60,4 +61,15 @@ function scheduleLevel(now, wakeTime, bedTime) {
60
61
  const untilBed = awakeMinutes - sinceWake;
61
62
  return untilBed < windDown ? ease(untilBed / windDown) : 1;
62
63
  }
64
+ // New times from a "schedule" message, each "HH:MM", with a time left out
65
+ // staying as it is; null if they aren't valid, different times.
66
+ function requestedSchedule(current, event) {
67
+ const next = {
68
+ wakeTime: typeof event.wakeTime === "string" ? event.wakeTime.trim() : current.wakeTime,
69
+ bedTime: typeof event.bedTime === "string" ? event.bedTime.trim() : current.bedTime
70
+ };
71
+ const wake = clockMinutes(next.wakeTime);
72
+ const bed = clockMinutes(next.bedTime);
73
+ return wake === null || bed === null || wake === bed ? null : next;
74
+ }
63
75
  //# sourceMappingURL=circadian-schedule.js.map
@@ -6,10 +6,17 @@ export interface ExpectedCommand {
6
6
  on?: boolean;
7
7
  power?: boolean;
8
8
  echoed?: boolean;
9
+ id?: string;
10
+ tentative?: boolean;
9
11
  }
10
12
  export declare function parsePayload(payload: unknown): unknown;
11
13
  export declare function fingerprint(payload: unknown): string;
12
- export declare function rememberCommand(queue: ExpectedCommand[] | undefined, payload: unknown, nowMs: number): ExpectedCommand[];
14
+ export declare function rememberCommand(queue: ExpectedCommand[] | undefined, payload: unknown, nowMs: number, { id, tentative }?: {
15
+ id?: string;
16
+ tentative?: boolean;
17
+ }): ExpectedCommand[];
18
+ export declare function commandSent(queue: ExpectedCommand[] | undefined, id: string, nowMs: number): ExpectedCommand[];
19
+ export declare function commandSkipped(queue: ExpectedCommand[] | undefined, id: string, nowMs: number): ExpectedCommand[];
13
20
  export type StateReportVerdict = "confirms" | "stale" | "external";
14
21
  export declare function checkStateReport(queue: ExpectedCommand[] | undefined, observedOn: boolean, nowMs: number): {
15
22
  queue: ExpectedCommand[];
@@ -4,6 +4,8 @@ exports.ECHO_QUEUE_LIMIT = exports.ECHO_WINDOW_MS = void 0;
4
4
  exports.parsePayload = parsePayload;
5
5
  exports.fingerprint = fingerprint;
6
6
  exports.rememberCommand = rememberCommand;
7
+ exports.commandSent = commandSent;
8
+ exports.commandSkipped = commandSkipped;
7
9
  exports.checkStateReport = checkStateReport;
8
10
  exports.checkCommandEcho = checkCommandEcho;
9
11
  // Telling the room controller's own light commands from other people's.
@@ -39,10 +41,23 @@ function fingerprint(payload) {
39
41
  function live(queue, nowMs) {
40
42
  return (queue || []).filter((entry) => entry.expires > nowMs).map((entry) => ({ ...entry }));
41
43
  }
42
- // Remember a command just sent.
43
- function rememberCommand(queue, payload, nowMs) {
44
+ // At most ECHO_QUEUE_LIMIT entries, tentative ones aside: the oldest go.
45
+ function trimmed(queue) {
46
+ let excess = queue.filter((entry) => entry.tentative !== true).length - exports.ECHO_QUEUE_LIMIT;
47
+ return queue.filter((entry) => {
48
+ if (excess > 0 && entry.tentative !== true) {
49
+ excess -= 1;
50
+ return false;
51
+ }
52
+ return true;
53
+ });
54
+ }
55
+ // Remember a command just sent: with an id, the room's own; tentative for a
56
+ // restyle its adapter may skip.
57
+ function rememberCommand(queue, payload, nowMs, { id, tentative = false } = {}) {
44
58
  const next = live(queue, nowMs);
45
- const entry = { fingerprint: fingerprint(payload), expires: nowMs + exports.ECHO_WINDOW_MS };
59
+ const entry = { fingerprint: fingerprint(payload), expires: nowMs + exports.ECHO_WINDOW_MS,
60
+ ...(id !== undefined ? { id } : {}), ...(tentative ? { tentative: true } : {}) };
46
61
  const requested = payload && typeof payload === "object"
47
62
  ? String(payload.state || "").toUpperCase()
48
63
  : "";
@@ -51,14 +66,30 @@ function rememberCommand(queue, payload, nowMs) {
51
66
  entry.power = entry.on;
52
67
  }
53
68
  next.push(entry);
54
- return next.slice(-exports.ECHO_QUEUE_LIMIT);
69
+ return trimmed(next);
70
+ }
71
+ // A tentative command its adapter sent after all: now an ordinary entry.
72
+ function commandSent(queue, id, nowMs) {
73
+ return trimmed(live(queue, nowMs).map((entry) => {
74
+ if (entry.id !== id)
75
+ return entry;
76
+ const { tentative: _tentative, ...sent } = entry;
77
+ return sent;
78
+ }));
79
+ }
80
+ // A tentative command its adapter skipped: as if never sent.
81
+ function commandSkipped(queue, id, nowMs) {
82
+ return live(queue, nowMs).filter((entry) => entry.id !== id);
55
83
  }
56
84
  function checkStateReport(queue, observedOn, nowMs) {
57
85
  const next = live(queue, nowMs);
58
- const latest = next.filter((entry) => entry.on !== undefined).at(-1);
86
+ // A tentative restyle isn't pending until its adapter says it sent it: a
87
+ // skipped one was never sent, so it must never make a report stale.
88
+ const pending = next.filter((entry) => entry.on !== undefined && entry.tentative !== true);
89
+ const latest = pending.at(-1);
59
90
  const verdict = latest === undefined ? "external" : latest.on === observedOn ? "confirms" : "stale";
60
91
  if (verdict === "confirms") {
61
- for (const entry of next)
92
+ for (const entry of pending)
62
93
  delete entry.on;
63
94
  }
64
95
  return { queue: next, verdict };
@@ -232,6 +232,10 @@ function transitionCover(saved, msg, suppliedConfig = {}, nowValue = new Date())
232
232
  trace("intent", "accepted", null, { kind: intent.kind, position, generation: intent.generation });
233
233
  }
234
234
  function acceptObservation(observation) {
235
+ // A cover that reports no position at all is the other cover type.
236
+ if (observation.position === undefined && ("state" in observation || observation.motion !== undefined)) {
237
+ throw new Error("observed cover reading has no position: for a cover that reports none, set the cover node's Cover to \"Open, close and stop only\"");
238
+ }
235
239
  const devicePosition = validPosition(observation.position, "observed cover position");
236
240
  if (observation.motion !== undefined && !Object.hasOwn(MOTIONS, observation.motion)) {
237
241
  throw new Error("unknown observed cover motion");
@@ -7,7 +7,7 @@ exports.coverInbound = coverInbound;
7
7
  const publishing_node_1 = require("./publishing-node");
8
8
  const topic_names_1 = require("./topic-names");
9
9
  function coverTopic(config, path) {
10
- return "home/" + (0, topic_names_1.areaId)(config.area) + "/cover/" + (0, topic_names_1.areaId)(config.name) + "/" + path;
10
+ return (0, topic_names_1.homeTopic)(config.area, "cover", (0, topic_names_1.areaId)(config.name), path);
11
11
  }
12
12
  const coverStateTopic = (config) => coverTopic(config, "state");
13
13
  exports.coverStateTopic = coverStateTopic;
@@ -24,6 +24,10 @@ export type ParsedAvrLine = {
24
24
  line: string;
25
25
  };
26
26
  export declare function parseSourceCodes(value: unknown): Record<string, string>;
27
+ export declare const VOLUME_STEP_DB = 0.5;
28
+ export declare const VOLUME_MIN_DB = -80;
29
+ export declare const VOLUME_MAX_DB = 18;
27
30
  export declare function roundVolume(value: number): number;
28
- export declare function formatVolumeCommand(value: number): string;
31
+ export declare function legacyVolumeDb(value: number, mode: "absolute" | "relative"): number;
32
+ export declare function formatVolumeCommand(db: number): string;
29
33
  export declare function parseAvrLine(value: unknown, sources: Record<string, string>): ParsedAvrLine | null;
@@ -1,9 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ZONE_ON = exports.POWER_STANDBY = exports.POWER_ON = exports.QUERY_COMMAND = void 0;
3
+ exports.VOLUME_MAX_DB = exports.VOLUME_MIN_DB = exports.VOLUME_STEP_DB = exports.ZONE_ON = exports.POWER_STANDBY = exports.POWER_ON = exports.QUERY_COMMAND = void 0;
4
4
  exports.selectSource = selectSource;
5
5
  exports.parseSourceCodes = parseSourceCodes;
6
6
  exports.roundVolume = roundVolume;
7
+ exports.legacyVolumeDb = legacyVolumeDb;
7
8
  exports.formatVolumeCommand = formatVolumeCommand;
8
9
  exports.parseAvrLine = parseAvrLine;
9
10
  // Asks for power, main zone, source and volume.
@@ -32,25 +33,40 @@ function parseSourceCodes(value) {
32
33
  }
33
34
  return Object.fromEntries(typedEntries);
34
35
  }
36
+ // The package's volume is in dB. A Denon receiver's master volume runs
37
+ // from 0 to 98 in half steps, where 80 is 0 dB: -80 to +18 dB. This module
38
+ // converts at its edges, so the receiver's strings stay as they are.
39
+ exports.VOLUME_STEP_DB = 0.5;
40
+ exports.VOLUME_MIN_DB = -80;
41
+ exports.VOLUME_MAX_DB = 18;
42
+ const DENON_ZERO_DB = 80;
35
43
  // Volume moves in half steps.
36
44
  function roundVolume(value) {
37
45
  return Math.round(value * 2) / 2;
38
46
  }
39
- // "MV" volume digits: two for a whole step, a trailing 5 for a half step.
47
+ // "MV" volume digits: two for a whole step, a trailing 5 for a half step;
48
+ // read as dB.
40
49
  function parseVolume(raw) {
41
50
  if (!/^\d{2,3}$/.test(raw))
42
51
  return null;
43
- if (raw.length === 2)
44
- return Number(raw);
45
- return Number(raw.slice(0, 2)) + Number(raw.slice(2)) / 10;
52
+ const denon = raw.length === 2 ? Number(raw) : Number(raw.slice(0, 2)) + Number(raw.slice(2)) / 10;
53
+ return denon - DENON_ZERO_DB;
46
54
  }
47
- function formatVolumeCommand(value) {
48
- const rounded = roundVolume(value);
49
- if (!Number.isFinite(value) || rounded < 0 || rounded > 98 || Math.abs(value - rounded) > 0.001) {
50
- throw new Error("volume must be a half-step between 0 and 98");
55
+ // A volume in the receiver's own 0-98 as dB: absolute values convert,
56
+ // relative ones are already a change in dB. Used only for a receiver state
57
+ // saved before 5.0, restored once (see createAvrState).
58
+ function legacyVolumeDb(value, mode) {
59
+ return mode === "absolute" ? value - DENON_ZERO_DB : value;
60
+ }
61
+ // A volume in dB as the receiver's "MV" command.
62
+ function formatVolumeCommand(db) {
63
+ const rounded = roundVolume(db);
64
+ if (!Number.isFinite(db) || rounded < exports.VOLUME_MIN_DB || rounded > exports.VOLUME_MAX_DB || Math.abs(db - rounded) > 0.001) {
65
+ throw new Error("volume must be a half-step between -80 and 18 dB");
51
66
  }
52
- const whole = Math.floor(rounded).toString().padStart(2, "0");
53
- return "MV" + whole + (Number.isInteger(rounded) ? "" : "5") + "\r";
67
+ const denon = rounded + DENON_ZERO_DB;
68
+ const whole = Math.floor(denon).toString().padStart(2, "0");
69
+ return "MV" + whole + (Number.isInteger(denon) ? "" : "5") + "\r";
54
70
  }
55
71
  function parseAvrLine(value, sources) {
56
72
  const line = String(value ?? "").trim();
@@ -1,9 +1,11 @@
1
+ import { type Carried } from "./adopted-request";
1
2
  import type { TraceEntry } from "./bounded-trace";
3
+ import { type PowerReading } from "./power-reading";
2
4
  import { type TargetStateEvent } from "./target-state";
3
5
  export type DisplayMode = "off" | "tv" | "projector";
4
6
  export type DisplayDevice = "tv" | "screen" | "projector";
5
7
  export interface DisplayConfig {
6
- screenOpenPosition: number;
8
+ screenOpenPosition: number | null;
7
9
  screenTolerance: number;
8
10
  pollMs: number;
9
11
  queryTimeoutMs: number;
@@ -31,12 +33,12 @@ interface TvObservation extends DeviceObservation {
31
33
  power: "on" | "standby";
32
34
  }
33
35
  interface ScreenObservation extends DeviceObservation {
34
- position: number;
36
+ position: number | null;
35
37
  state: string;
36
38
  }
37
39
  interface ProjectorObservation extends DeviceObservation {
38
- watts: number;
39
- power: "unknown" | "on" | "standby" | "transitioning";
40
+ watts: number | null;
41
+ power: PowerReading;
40
42
  }
41
43
  interface DisplayObserved {
42
44
  tv: TvObservation | null;
@@ -81,6 +83,11 @@ export interface DisplayState {
81
83
  lastAttempt: number;
82
84
  fallbackUsed: boolean;
83
85
  adoptedGeneration: number | null;
86
+ carried?: Carried | null;
87
+ discovery?: {
88
+ attempt: number;
89
+ nextAt: number;
90
+ };
84
91
  }
85
92
  export type DisplayInputEvent = {
86
93
  kind: "startup";
@@ -96,7 +103,7 @@ export type DisplayInputEvent = {
96
103
  observedAt?: string;
97
104
  source?: string;
98
105
  power?: string;
99
- position?: number;
106
+ position?: number | null;
100
107
  state?: string;
101
108
  watts?: number;
102
109
  } | {