@avantics/node-red-home-automation 4.0.2 → 5.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 (60) hide show
  1. package/README.md +126 -4
  2. package/contract-examples/switch-gestures.mjs +1 -1
  3. package/contract-examples/volume-ownership.mjs +6 -5
  4. package/dist/lib/activity-lighting-controller.d.ts +2 -0
  5. package/dist/lib/activity-lighting-controller.js +46 -7
  6. package/dist/lib/adopted-request.d.ts +17 -0
  7. package/dist/lib/adopted-request.js +61 -7
  8. package/dist/lib/avr-controller.d.ts +9 -3
  9. package/dist/lib/avr-controller.js +76 -24
  10. package/dist/lib/avr-publishing.d.ts +3 -3
  11. package/dist/lib/avr-publishing.js +11 -5
  12. package/dist/lib/backlighting-controller.d.ts +2 -0
  13. package/dist/lib/backlighting-controller.js +37 -8
  14. package/dist/lib/cover-publishing.js +1 -1
  15. package/dist/lib/denon-protocol.d.ts +5 -1
  16. package/dist/lib/denon-protocol.js +26 -11
  17. package/dist/lib/display-mode-controller.d.ts +12 -5
  18. package/dist/lib/display-mode-controller.js +181 -87
  19. package/dist/lib/house-schedule-publishing.js +1 -1
  20. package/dist/lib/light-styling.d.ts +2 -2
  21. package/dist/lib/light-styling.js +5 -4
  22. package/dist/lib/motion-publishing.js +1 -1
  23. package/dist/lib/occupancy-publishing.js +1 -1
  24. package/dist/lib/power-reading.d.ts +5 -0
  25. package/dist/lib/power-reading.js +13 -0
  26. package/dist/lib/room-controller.d.ts +2 -3
  27. package/dist/lib/room-controller.js +18 -2
  28. package/dist/lib/room-lighting-discovery.js +1 -1
  29. package/dist/lib/room-publishing.d.ts +3 -0
  30. package/dist/lib/shelly-cover.d.ts +46 -0
  31. package/dist/lib/shelly-cover.js +193 -0
  32. package/dist/lib/source-capability-controller.d.ts +2 -0
  33. package/dist/lib/source-capability-controller.js +27 -6
  34. package/dist/lib/switch-policy.d.ts +1 -1
  35. package/dist/lib/switch-policy.js +1 -1
  36. package/dist/lib/theatre-requests.d.ts +29 -0
  37. package/dist/lib/theatre-requests.js +89 -0
  38. package/dist/lib/theatre-state-contract.d.ts +5 -22
  39. package/dist/lib/theatre-state-contract.js +18 -73
  40. package/dist/lib/toggle-power.d.ts +41 -0
  41. package/dist/lib/toggle-power.js +74 -0
  42. package/dist/lib/topic-names.d.ts +1 -0
  43. package/dist/lib/topic-names.js +7 -0
  44. package/dist/lib/{display-transport.d.ts → transports/display-transport.d.ts} +1 -1
  45. package/dist/lib/{display-transport.js → transports/display-transport.js} +32 -7
  46. package/dist/lib/z2m-light.d.ts +8 -0
  47. package/dist/lib/z2m-light.js +64 -3
  48. package/dist/nodes/activity-lighting-controller.html +8 -0
  49. package/dist/nodes/avr-controller.html +28 -6
  50. package/dist/nodes/avr-controller.js +16 -6
  51. package/dist/nodes/display-mode-controller.html +61 -11
  52. package/dist/nodes/display-mode-controller.js +16 -6
  53. package/dist/nodes/room-controller.js +14 -0
  54. package/dist/nodes/shelly-cover.d.ts +3 -0
  55. package/dist/nodes/shelly-cover.html +65 -0
  56. package/dist/nodes/shelly-cover.js +34 -0
  57. package/examples/Home theatre activity.json +5 -5
  58. package/examples/Home theatre equipment.json +4 -4
  59. package/examples/Shelly cover.json +252 -0
  60. package/package.json +3 -2
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.parseAvrLine = exports.formatVolumeCommand = exports.QUERY_COMMAND = void 0;
3
+ exports.LEGACY_VOLUME_UNITS = exports.parseAvrLine = exports.formatVolumeCommand = exports.QUERY_COMMAND = void 0;
4
4
  exports.createAvrState = createAvrState;
5
5
  exports.normaliseAvrConfig = normaliseAvrConfig;
6
6
  exports.transitionAvr = transitionAvr;
@@ -12,9 +12,17 @@ var denon_protocol_2 = require("./denon-protocol");
12
12
  Object.defineProperty(exports, "QUERY_COMMAND", { enumerable: true, get: function () { return denon_protocol_2.QUERY_COMMAND; } });
13
13
  Object.defineProperty(exports, "formatVolumeCommand", { enumerable: true, get: function () { return denon_protocol_2.formatVolumeCommand; } });
14
14
  Object.defineProperty(exports, "parseAvrLine", { enumerable: true, get: function () { return denon_protocol_2.parseAvrLine; } });
15
+ // Traced when a volume request gives `value` in the receiver's own units
16
+ // rather than `valueDb`: still read during 5.x, and the node warns.
17
+ exports.LEGACY_VOLUME_UNITS = "legacy-volume-units";
15
18
  function observedTime(value) {
16
19
  return typeof value === "string" && Number.isFinite(Date.parse(value)) ? value : null;
17
20
  }
21
+ // A number, or a finite numeric string, as a number; null otherwise.
22
+ function numberValue(value) {
23
+ const number = typeof value === "number" ? value : typeof value === "string" && value.trim() !== "" ? Number(value) : NaN;
24
+ return Number.isFinite(number) ? number : null;
25
+ }
18
26
  function createAvrState(_config, saved = {}) {
19
27
  const observed = saved.observed || {};
20
28
  const seen = saved.seen || {};
@@ -22,13 +30,19 @@ function createAvrState(_config, saved = {}) {
22
30
  ? saved.volumeRequest
23
31
  : null;
24
32
  const volumeMetrics = saved.volumeMetrics || {};
33
+ // A state saved before 5.0, from a context store, holds volumes in the
34
+ // receiver's units; a relative request's `requested` is a change, the same
35
+ // in both.
36
+ const savedInReceiverUnits = Object.keys(saved).length > 0 && saved.volumeUnits !== "dB";
37
+ const db = (value) => (savedInReceiverUnits ? (0, denon_protocol_1.legacyVolumeDb)(value, "absolute") : value);
25
38
  return {
39
+ volumeUnits: "dB",
26
40
  observed: {
27
41
  power: observed.power === "on" || observed.power === "standby" ? observed.power : null,
28
42
  mainZone: observed.mainZone === "on" || observed.mainZone === "off" ? observed.mainZone : null,
29
43
  source: typeof observed.source === "string" ? observed.source : null,
30
44
  sourceCode: typeof observed.sourceCode === "string" ? observed.sourceCode : null,
31
- volume: Number.isFinite(observed.volume) ? observed.volume : null,
45
+ volume: Number.isFinite(observed.volume) ? db(observed.volume) : null,
32
46
  at: typeof observed.at === "string" ? observed.at : null
33
47
  },
34
48
  fieldObservedAt: {
@@ -56,8 +70,10 @@ function createAvrState(_config, saved = {}) {
56
70
  ? {
57
71
  requestId: volumeRequest.requestId,
58
72
  mode: volumeRequest.mode,
59
- requested: Number(volumeRequest.requested) || 0,
60
- target: Number.isFinite(volumeRequest.target) ? volumeRequest.target : null,
73
+ requested: volumeRequest.mode === "absolute" && Number.isFinite(volumeRequest.requested)
74
+ ? db(volumeRequest.requested)
75
+ : Number(volumeRequest.requested) || 0,
76
+ target: Number.isFinite(volumeRequest.target) ? db(volumeRequest.target) : null,
61
77
  bounded: volumeRequest.bounded === true
62
78
  }
63
79
  : null,
@@ -86,16 +102,22 @@ function sameVolume(left, right) {
86
102
  // failing every message.
87
103
  // Settings may be editor strings. Missing or unusable numbers take their
88
104
  // defaults: retry 2000 ms (at least 250), 3 attempts (at least 1) and volume
89
- // bounds 0 to 60.
105
+ // bounds -80 to -20 dB. A flow saved before 5.0 has minVolume and maxVolume
106
+ // in the receiver's own 0-98, read when the dB settings are missing.
90
107
  function normaliseAvrConfig(rawConfig) {
91
- const rawMinVolume = (0, settings_1.numberSetting)(rawConfig.minVolume, "minimum volume", 0);
92
- const rawMaxVolume = (0, settings_1.numberSetting)(rawConfig.maxVolume, "maximum volume", 60);
108
+ // The editor keeps the old keys, empty once the dialog saves dB ones.
109
+ const given = (value) => value !== undefined && value !== null && String(value).trim() !== "";
110
+ const bound = (db, legacy, label, fallback) => !given(db) && given(legacy)
111
+ ? (0, denon_protocol_1.legacyVolumeDb)((0, settings_1.numberSetting)(legacy, label, fallback - denon_protocol_1.VOLUME_MIN_DB), "absolute")
112
+ : (0, settings_1.numberSetting)(db, label, fallback);
113
+ const rawMinVolume = bound(rawConfig.minVolumeDb, rawConfig.minVolume, "minimum volume", -80);
114
+ const rawMaxVolume = bound(rawConfig.maxVolumeDb, rawConfig.maxVolume, "maximum volume", -20);
93
115
  const minVolume = (0, denon_protocol_1.roundVolume)(rawMinVolume);
94
116
  const maxVolume = (0, denon_protocol_1.roundVolume)(rawMaxVolume);
95
117
  if (!Number.isFinite(rawMinVolume) || !Number.isFinite(rawMaxVolume) ||
96
118
  Math.abs(rawMinVolume - minVolume) > 0.001 || Math.abs(rawMaxVolume - maxVolume) > 0.001 ||
97
- minVolume < 0 || maxVolume > 98 || minVolume >= maxVolume) {
98
- throw new Error("volume bounds must be half-step values between 0 and 98 with minimum below maximum");
119
+ minVolume < denon_protocol_1.VOLUME_MIN_DB || maxVolume > denon_protocol_1.VOLUME_MAX_DB || minVolume >= maxVolume) {
120
+ throw new Error("volume bounds must be half-step values between -80 and 18 dB with minimum below maximum");
99
121
  }
100
122
  return {
101
123
  // Optional: an empty area means none.
@@ -103,8 +125,8 @@ function normaliseAvrConfig(rawConfig) {
103
125
  sources: (0, denon_protocol_1.parseSourceCodes)(rawConfig.sources),
104
126
  retryMs: Math.max(250, (0, settings_1.numberSetting)(rawConfig.retryMs, "retry interval", 2000)),
105
127
  maxAttempts: Math.max(1, Math.floor((0, settings_1.numberSetting)(rawConfig.maxAttempts, "max attempts", 3))),
106
- minVolume,
107
- maxVolume
128
+ minVolumeDb: minVolume,
129
+ maxVolumeDb: maxVolume
108
130
  };
109
131
  }
110
132
  function transitionAvr(saved, msg, rawConfig, nowValue = new Date()) {
@@ -245,7 +267,7 @@ function transitionAvr(saved, msg, rawConfig, nowValue = new Date()) {
245
267
  }
246
268
  function clampVolume(value) {
247
269
  const rounded = (0, denon_protocol_1.roundVolume)(value);
248
- const bounded = Math.max(config.minVolume, Math.min(config.maxVolume, rounded));
270
+ const bounded = Math.max(config.minVolumeDb, Math.min(config.maxVolumeDb, rounded));
249
271
  return { value: bounded, bounded: Math.abs(bounded - value) > 0.001 };
250
272
  }
251
273
  // The phase once no volume work is in flight; a failed request stays failed.
@@ -455,14 +477,30 @@ function transitionAvr(saved, msg, rawConfig, nowValue = new Date()) {
455
477
  // A refresh with the activity's wanted values, reaching a node with
456
478
  // nothing desired (it started with nothing saved): adopt them, and
457
479
  // rediscover below as at startup (see adopted-request.ts).
480
+ let replacedRequest = false;
458
481
  const adoptable = event.kind === "refresh" ? (0, adopted_request_1.adoptableRefresh)(event, state) : null;
459
- const adopted = adoptable !== null && event.kind === "refresh" &&
482
+ // The same wanted values as the work in hand: carry on under the new
483
+ // request rather than adopting it read-only (see carryOn).
484
+ const carried = event.kind === "refresh" && (0, adopted_request_1.carryOn)(state, adoptable, event.power === state.desired?.power && (event.source || null) === (state.desired?.source ?? null), state.classification === "manual_override" || state.phase === "failed" || state.blockedGeneration === state.desired?.generation);
485
+ if (carried)
486
+ trace("refresh", "continued", "same-request", { generation: state.generation });
487
+ const adopted = !carried && adoptable !== null && event.kind === "refresh" &&
460
488
  validateIntent({ kind: "intent", ...adoptable, power: event.power, source: event.source ?? null }) === null;
461
489
  if (adopted && event.kind === "refresh") {
490
+ const reason = (0, adopted_request_1.adoptionReason)(state.desired);
491
+ // Replacing a request the theatre already counts as ready, it answers
492
+ // from its reading, not with an interim state the theatre would take as
493
+ // drift.
494
+ replacedRequest = reason === "newer-generation";
495
+ // A request it replaces takes its classification and failure with it.
496
+ state.failure = null;
497
+ state.blockedGeneration = null;
498
+ state.classification = null;
499
+ state.lastAttempt = 0;
462
500
  state.desired = { ...adoptable, power: event.power, source: event.source || null };
463
501
  state.generation = adoptable.generation;
464
502
  state.adoptedGeneration = adoptable.generation;
465
- trace("refresh", "adopted", "nothing-desired", { power: event.power, source: event.source || null, generation: adoptable.generation });
503
+ trace("refresh", "adopted", reason, { power: event.power, source: event.source || null, generation: adoptable.generation });
466
504
  }
467
505
  if (event.kind === "refresh" && !adopted) {
468
506
  // Re-read the receiver without touching work in flight: desired state,
@@ -669,7 +707,21 @@ function transitionAvr(saved, msg, rawConfig, nowValue = new Date()) {
669
707
  forceStateEvent = true;
670
708
  }
671
709
  else if (event.kind === "volume") {
672
- const reason = validateVolumeIntent(event);
710
+ // In dB; a finite numeric string counts. `value` from before 5.0 is in
711
+ // the receiver's own units, still read during 5.x and traced so the
712
+ // node can warn; given with a usable valueDb, it is ignored and traced.
713
+ const valueDb = numberValue(event.valueDb);
714
+ const value = numberValue(event.value);
715
+ const legacyUnits = valueDb === null && event.value !== undefined;
716
+ const volume = { ...event, value: valueDb !== null ? valueDb
717
+ : value !== null && (event.mode === "absolute" || event.mode === "relative") ? (0, denon_protocol_1.legacyVolumeDb)(value, event.mode)
718
+ : value ?? NaN };
719
+ if (legacyUnits)
720
+ trace("volume", "accepted", exports.LEGACY_VOLUME_UNITS, { requestId: event.requestId, value: event.value });
721
+ if (valueDb !== null && event.value !== undefined) {
722
+ trace("volume", "accepted", "legacy-volume-units-ignored", { requestId: event.requestId, value: event.value });
723
+ }
724
+ const reason = validateVolumeIntent(volume);
673
725
  if (reason) {
674
726
  events.push({
675
727
  avr: {
@@ -688,32 +740,32 @@ function transitionAvr(saved, msg, rawConfig, nowValue = new Date()) {
688
740
  else {
689
741
  const existing = state.volumeRequest;
690
742
  let mode = event.mode;
691
- let requested = event.value;
743
+ let requested = volume.value;
692
744
  let target = null;
693
745
  let bounded = false;
694
746
  let boundedThisIntent = false;
695
747
  if (event.mode === "absolute") {
696
- const resolved = clampVolume(event.value);
748
+ const resolved = clampVolume(volume.value);
697
749
  target = resolved.value;
698
750
  bounded = resolved.bounded;
699
751
  boundedThisIntent = resolved.bounded;
700
752
  }
701
753
  else if (existing?.target !== null && existing?.target !== undefined) {
702
- const unresolvedTarget = existing.target + event.value;
754
+ const unresolvedTarget = existing.target + volume.value;
703
755
  const resolved = clampVolume(unresolvedTarget);
704
756
  mode = existing.mode;
705
757
  requested = existing.mode === "relative"
706
- ? existing.requested + event.value
758
+ ? existing.requested + volume.value
707
759
  : unresolvedTarget;
708
760
  target = resolved.value;
709
761
  bounded = existing.bounded || resolved.bounded;
710
762
  boundedThisIntent = resolved.bounded;
711
763
  }
712
764
  else if (existing?.mode === "relative") {
713
- requested = existing.requested + event.value;
765
+ requested = existing.requested + volume.value;
714
766
  }
715
767
  else if (state.seen.volume && state.observed.volume !== null) {
716
- const resolved = clampVolume(state.observed.volume + event.value);
768
+ const resolved = clampVolume(state.observed.volume + volume.value);
717
769
  target = resolved.value;
718
770
  bounded = resolved.bounded;
719
771
  boundedThisIntent = resolved.bounded;
@@ -739,7 +791,7 @@ function transitionAvr(saved, msg, rawConfig, nowValue = new Date()) {
739
791
  trace("volume", "accepted", null, {
740
792
  requestId: event.requestId,
741
793
  mode: event.mode,
742
- value: event.value,
794
+ value: volume.value,
743
795
  target,
744
796
  bounded
745
797
  });
@@ -842,7 +894,7 @@ function transitionAvr(saved, msg, rawConfig, nowValue = new Date()) {
842
894
  classification: state.classification,
843
895
  volume: {
844
896
  request: state.volumeRequest ? { ...state.volumeRequest } : null,
845
- bounds: { minimum: config.minVolume, maximum: config.maxVolume },
897
+ bounds: { minimum: config.minVolumeDb, maximum: config.maxVolumeDb },
846
898
  metrics: { ...state.volumeMetrics }
847
899
  },
848
900
  metrics: {
@@ -874,7 +926,7 @@ function transitionAvr(saved, msg, rawConfig, nowValue = new Date()) {
874
926
  // with the oldest report among the fields the request needs, so it is only
875
927
  // as fresh as its least recent confirmation, and is re-sent only when the
876
928
  // receiver reports one of those fields.
877
- if (state.desired) {
929
+ if (state.desired && !replacedRequest) {
878
930
  const fields = requiredFields();
879
931
  const status = state.phase === "manual_override"
880
932
  ? "transitioning"
@@ -1,8 +1,8 @@
1
1
  import type { AvrObserved } from "./avr-controller";
2
2
  import { type OptInPublishingConfig, type RetainedText } from "./publishing-node";
3
3
  export interface AvrPublishingConfig extends OptInPublishingConfig {
4
- minVolume: number;
5
- maxVolume: number;
4
+ minVolumeDb: number;
5
+ maxVolumeDb: number;
6
6
  }
7
7
  export type AvrPublication = RetainedText;
8
8
  export declare function receiverTopic(area: string, name: string): string;
@@ -13,7 +13,7 @@ export declare function avrVolumeRequest(msg: {
13
13
  payload?: unknown;
14
14
  retain?: unknown;
15
15
  }, config: AvrPublishingConfig): {
16
- value: number;
16
+ valueDb: number;
17
17
  } | {
18
18
  ignored: string;
19
19
  payload: string;
@@ -5,12 +5,17 @@ exports.receiverTopic = receiverTopic;
5
5
  exports.avrDiscovery = avrDiscovery;
6
6
  exports.avrVolumeRequest = avrVolumeRequest;
7
7
  exports.avrStateOutputs = avrStateOutputs;
8
+ // The receiver's MQTT and Home Assistant side: its observed power, source
9
+ // and volume as retained state, its availability, power and source sensors,
10
+ // and a volume number. All of it is opt-in (see publishing-node.ts); the
11
+ // receiver controller only commands and observes the receiver.
12
+ const denon_protocol_1 = require("./denon-protocol");
8
13
  const topic_names_1 = require("./topic-names");
9
14
  const publishing_node_1 = require("./publishing-node");
10
15
  const FIELDS = ["power", "source", "volume"];
11
16
  const DEVICE = { kind: "receiver", label: "Receiver", model: "AV receiver controller" };
12
17
  function receiverTopic(area, name) {
13
- return "home/" + (0, topic_names_1.areaId)(area) + "/receiver/" + name;
18
+ return (0, topic_names_1.homeTopic)(area, "receiver", name);
14
19
  }
15
20
  // A field's state as published, or null while it isn't known.
16
21
  function fieldValue(observed, field) {
@@ -32,9 +37,10 @@ function avrDiscovery(config) {
32
37
  icon: "mdi:volume-high",
33
38
  state_topic: receiverTopic(config.area, "volume/state"),
34
39
  command_topic: receiverTopic(config.area, "volume/set"),
35
- min: config.minVolume,
36
- max: config.maxVolume,
37
- step: 0.5,
40
+ min: config.minVolumeDb,
41
+ max: config.maxVolumeDb,
42
+ step: denon_protocol_1.VOLUME_STEP_DB,
43
+ unit_of_measurement: "dB",
38
44
  mode: "slider"
39
45
  })
40
46
  ];
@@ -54,7 +60,7 @@ function avrVolumeRequest(msg, config) {
54
60
  if (msg.retain === true)
55
61
  return { ignored: "retained-message", payload: text };
56
62
  const value = text === "" ? NaN : Number(text);
57
- return Number.isFinite(value) ? { value } : { ignored: "not-a-number", payload: text };
63
+ return Number.isFinite(value) ? { valueDb: value } : { ignored: "not-a-number", payload: text };
58
64
  }
59
65
  // The observed fields that changed since `before` (all known fields when
60
66
  // there's no `before`).
@@ -1,3 +1,4 @@
1
+ import { type Carried } from "./adopted-request";
1
2
  import type { TraceEntry } from "./bounded-trace";
2
3
  import { type TargetStateEvent } from "./target-state";
3
4
  export type BacklightingPower = "on" | "off";
@@ -38,6 +39,7 @@ export interface BacklightingState {
38
39
  classification: "manual_override" | null;
39
40
  lastAttempt: number;
40
41
  adoptedGeneration: number | null;
42
+ carried?: Carried | null;
41
43
  }
42
44
  export type BacklightingEvent = {
43
45
  kind: "intent";
@@ -27,7 +27,8 @@ function initialState(saved = {}) {
27
27
  lastAttempt: Number.isInteger(saved.lastAttempt) && (saved.lastAttempt ?? -1) >= 0
28
28
  ? saved.lastAttempt
29
29
  : 0,
30
- adoptedGeneration: (0, adopted_request_1.savedAdoptedGeneration)(saved)
30
+ adoptedGeneration: (0, adopted_request_1.savedAdoptedGeneration)(saved),
31
+ carried: (0, adopted_request_1.savedCarried)(saved)
31
32
  };
32
33
  }
33
34
  function commandFor(state, operation, attempt) {
@@ -129,7 +130,7 @@ function transitionBacklighting(saved, msg, suppliedConfig = {}, nowValue = new
129
130
  if (parsedAt === null || typeof observation.observedAt !== "string") {
130
131
  throw new Error("backlighting observedAt must be an ISO timestamp");
131
132
  }
132
- if (!state.desired || observation.generation !== state.generation) {
133
+ if (!state.desired || !(0, adopted_request_1.answersRequest)(state.carried, state.generation, observation.generation)) {
133
134
  trace("observation", "ignored", "stale-generation");
134
135
  return;
135
136
  }
@@ -180,12 +181,28 @@ function transitionBacklighting(saved, msg, suppliedConfig = {}, nowValue = new
180
181
  // A refresh with the activity's wanted values, reaching a node with
181
182
  // nothing desired (it started with nothing saved): adopt them, and observe
182
183
  // below as at startup (see adopted-request.ts).
184
+ let answerWithReading = false;
183
185
  const adoptable = event.kind === "refresh" ? (0, adopted_request_1.adoptableRefresh)(event, state) : null;
184
- if (adoptable && event.kind === "refresh" && typeof event.power === "string" && Object.hasOwn(POWERS, event.power)) {
186
+ // The same wanted values as the work in hand: carry on under the new
187
+ // request rather than adopting it read-only (see carryOn).
188
+ const carried = event.kind === "refresh" && (0, adopted_request_1.carryOn)(state, adoptable, event.power === state.desired?.power, state.classification === "manual_override" || state.phase === "failed" || state.blockedGeneration === state.desired?.generation);
189
+ if (carried)
190
+ trace("refresh", "continued", "same-request", { generation: state.generation });
191
+ if (!carried && adoptable && event.kind === "refresh" && typeof event.power === "string" && Object.hasOwn(POWERS, event.power)) {
192
+ const reason = (0, adopted_request_1.adoptionReason)(state.desired);
193
+ // Replacing a request the theatre already counts as ready, it answers
194
+ // from its reading, not with an interim state the theatre would take as
195
+ // drift.
196
+ answerWithReading = reason === "newer-generation";
197
+ // A request it replaces takes its classification and failure with it.
198
+ state.failure = null;
199
+ state.blockedGeneration = null;
200
+ state.classification = null;
201
+ state.lastAttempt = 0;
185
202
  state.desired = { ...adoptable, power: event.power, requestedAt: now.toISOString() };
186
203
  state.generation = adoptable.generation;
187
204
  state.adoptedGeneration = adoptable.generation;
188
- trace("refresh", "adopted", "nothing-desired", { power: event.power, generation: adoptable.generation });
205
+ trace("refresh", "adopted", reason, { power: event.power, generation: adoptable.generation });
189
206
  }
190
207
  if (event.kind === "intent") {
191
208
  acceptIntent(event);
@@ -194,7 +211,7 @@ function transitionBacklighting(saved, msg, suppliedConfig = {}, nowValue = new
194
211
  acceptObservation(event);
195
212
  }
196
213
  else if (event.kind === "command_result") {
197
- if (!state.desired || event.generation !== state.generation || !state.pending) {
214
+ if (!state.desired || !(0, adopted_request_1.answersRequest)(state.carried, state.generation, event.generation) || !state.pending) {
198
215
  trace("command-result", "ignored", "stale-generation");
199
216
  }
200
217
  else if (event.ok === true) {
@@ -228,6 +245,14 @@ function transitionBacklighting(saved, msg, suppliedConfig = {}, nowValue = new
228
245
  trace("command", "emitted", null, { operation: "observe", power: state.desired.power });
229
246
  }
230
247
  }
248
+ else if (event.kind === "refresh" && state.desired && state.phase === "ready" && !state.pending) {
249
+ // A settled, ready target is re-read without leaving ready, as the
250
+ // display, source and receiver are: its target state follows the
251
+ // reading, which classifies as any reading after ready does.
252
+ commands.push(commandFor(state, "observe", 0));
253
+ answerWithReading = true;
254
+ trace("refresh", "observing", "ready", { power: state.desired.power });
255
+ }
231
256
  else if (event.kind === "startup" || event.kind === "refresh") {
232
257
  state.observed = null;
233
258
  state.pending = null;
@@ -258,13 +283,17 @@ function transitionBacklighting(saved, msg, suppliedConfig = {}, nowValue = new
258
283
  state.phase = "manual_override";
259
284
  trace("command", "withheld", adopted_request_1.ADOPTED_MISMATCH);
260
285
  }
261
- if (state.desired) {
286
+ if (state.desired && !answerWithReading) {
287
+ const status = state.phase === "manual_override" ? "transitioning" : (0, target_state_1.asTargetStatus)(state.phase, "confirming");
262
288
  const theatre = (0, target_state_1.targetStateEvent)({
263
289
  target: "backlighting",
264
290
  requestId: state.desired.requestId,
265
291
  generation: state.generation,
266
- status: state.phase === "manual_override" ? "transitioning" : (0, target_state_1.asTargetStatus)(state.phase, "confirming"),
267
- observedAt: now.toISOString(),
292
+ status,
293
+ // Ready is only as fresh as the reading it rests on, as the display's
294
+ // is: a repeated intent or other input with nothing new read must not
295
+ // look like a new reading, or it would answer a theatre refresh.
296
+ observedAt: status === "ready" && state.observed ? state.observed.observedAt : now.toISOString(),
268
297
  details: {
269
298
  power: state.desired.power,
270
299
  observedPower: state.observed?.power ?? null,
@@ -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,39 @@ 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 request's value from before 5.0, in the receiver's own 0-98:
56
+ // absolute values convert to dB; relative ones are already a change in dB.
57
+ function legacyVolumeDb(value, mode) {
58
+ return mode === "absolute" ? value - DENON_ZERO_DB : value;
59
+ }
60
+ // A volume in dB as the receiver's "MV" command.
61
+ function formatVolumeCommand(db) {
62
+ const rounded = roundVolume(db);
63
+ if (!Number.isFinite(db) || rounded < exports.VOLUME_MIN_DB || rounded > exports.VOLUME_MAX_DB || Math.abs(db - rounded) > 0.001) {
64
+ throw new Error("volume must be a half-step between -80 and 18 dB");
51
65
  }
52
- const whole = Math.floor(rounded).toString().padStart(2, "0");
53
- return "MV" + whole + (Number.isInteger(rounded) ? "" : "5") + "\r";
66
+ const denon = rounded + DENON_ZERO_DB;
67
+ const whole = Math.floor(denon).toString().padStart(2, "0");
68
+ return "MV" + whole + (Number.isInteger(denon) ? "" : "5") + "\r";
54
69
  }
55
70
  function parseAvrLine(value, sources) {
56
71
  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
  } | {