@avantics/node-red-home-automation 3.0.1 → 4.0.1

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 (108) hide show
  1. package/README.md +278 -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/cover-availability.d.ts +7 -0
  15. package/dist/lib/cover-availability.js +24 -0
  16. package/dist/lib/cover-controller.d.ts +13 -1
  17. package/dist/lib/cover-controller.js +145 -31
  18. package/dist/lib/cover-group.d.ts +62 -0
  19. package/dist/lib/cover-group.js +125 -0
  20. package/dist/lib/cover-models.d.ts +37 -0
  21. package/dist/lib/cover-models.js +83 -0
  22. package/dist/lib/cover-publishing.d.ts +40 -0
  23. package/dist/lib/cover-publishing.js +102 -0
  24. package/dist/lib/cover-requests.d.ts +21 -0
  25. package/dist/lib/cover-requests.js +49 -0
  26. package/dist/lib/display-mode-controller.d.ts +4 -0
  27. package/dist/lib/display-mode-controller.js +26 -2
  28. package/dist/lib/ha-discovery.d.ts +27 -0
  29. package/dist/lib/ha-discovery.js +91 -0
  30. package/dist/lib/house-schedule-publishing.d.ts +23 -0
  31. package/dist/lib/house-schedule-publishing.js +71 -0
  32. package/dist/lib/house-schedule.d.ts +23 -0
  33. package/dist/lib/house-schedule.js +29 -0
  34. package/dist/lib/motion-policy.d.ts +9 -0
  35. package/dist/lib/motion-policy.js +45 -11
  36. package/dist/lib/motion-publishing.d.ts +28 -0
  37. package/dist/lib/motion-publishing.js +92 -0
  38. package/dist/lib/occupancy-publishing.d.ts +10 -0
  39. package/dist/lib/occupancy-publishing.js +45 -0
  40. package/dist/lib/open-close-cover.d.ts +92 -0
  41. package/dist/lib/open-close-cover.js +287 -0
  42. package/dist/lib/publishing-node.d.ts +39 -0
  43. package/dist/lib/publishing-node.js +72 -0
  44. package/dist/lib/retained-topics.d.ts +10 -0
  45. package/dist/lib/retained-topics.js +19 -0
  46. package/dist/lib/room-controller.d.ts +9 -0
  47. package/dist/lib/room-controller.js +47 -56
  48. package/dist/lib/room-lighting-discovery.d.ts +20 -1
  49. package/dist/lib/room-lighting-discovery.js +135 -13
  50. package/dist/lib/room-publishing.d.ts +42 -0
  51. package/dist/lib/room-publishing.js +182 -0
  52. package/dist/lib/room-schedule.d.ts +8 -0
  53. package/dist/lib/room-schedule.js +41 -0
  54. package/dist/lib/settings.d.ts +2 -0
  55. package/dist/lib/settings.js +20 -0
  56. package/dist/lib/source-capability-controller.d.ts +8 -1
  57. package/dist/lib/source-capability-controller.js +29 -3
  58. package/dist/lib/theatre-activity-controller.d.ts +0 -1
  59. package/dist/lib/theatre-activity-controller.js +27 -24
  60. package/dist/lib/theatre-ha-discovery.d.ts +3 -4
  61. package/dist/lib/theatre-ha-discovery.js +46 -36
  62. package/dist/lib/theatre-state-contract.d.ts +6 -0
  63. package/dist/lib/theatre-state-contract.js +83 -47
  64. package/dist/lib/theatre-state.d.ts +1 -1
  65. package/dist/lib/theatre-state.js +1 -1
  66. package/dist/lib/topic-names.d.ts +4 -0
  67. package/dist/lib/topic-names.js +31 -0
  68. package/dist/nodes/activity-lighting-controller.js +5 -3
  69. package/dist/nodes/adapter.d.ts +6 -1
  70. package/dist/nodes/adapter.js +65 -1
  71. package/dist/nodes/avr-controller.html +59 -5
  72. package/dist/nodes/avr-controller.js +74 -4
  73. package/dist/nodes/backlighting-controller.js +5 -3
  74. package/dist/nodes/cover-controller.d.ts +3 -0
  75. package/dist/nodes/cover-controller.html +181 -0
  76. package/dist/nodes/cover-controller.js +178 -0
  77. package/dist/nodes/cover-group.d.ts +3 -0
  78. package/dist/nodes/cover-group.html +89 -0
  79. package/dist/nodes/cover-group.js +84 -0
  80. package/dist/nodes/flows-started.d.ts +4 -0
  81. package/dist/nodes/flows-started.js +39 -0
  82. package/dist/nodes/house-schedule-mqtt.d.ts +3 -0
  83. package/dist/nodes/house-schedule-mqtt.html +61 -0
  84. package/dist/nodes/house-schedule-mqtt.js +80 -0
  85. package/dist/nodes/house-schedule.d.ts +3 -0
  86. package/dist/nodes/house-schedule.html +31 -0
  87. package/dist/nodes/house-schedule.js +51 -0
  88. package/dist/nodes/motion-policy.html +81 -5
  89. package/dist/nodes/motion-policy.js +69 -8
  90. package/dist/nodes/occupancy-fusion.html +69 -14
  91. package/dist/nodes/occupancy-fusion.js +46 -4
  92. package/dist/nodes/publishing-lifecycle.d.ts +25 -0
  93. package/dist/nodes/publishing-lifecycle.js +38 -0
  94. package/dist/nodes/room-controller.html +49 -5
  95. package/dist/nodes/room-controller.js +67 -8
  96. package/dist/nodes/shared-schedule.d.ts +6 -0
  97. package/dist/nodes/shared-schedule.js +15 -0
  98. package/dist/nodes/source-capability-controller.js +5 -3
  99. package/dist/nodes/theatre-activity-controller.html +1 -3
  100. package/dist/nodes/theatre-activity-controller.js +1 -2
  101. package/dist/nodes/theatre-state-contract.html +28 -5
  102. package/dist/nodes/theatre-state-contract.js +22 -4
  103. package/examples/Cover control.json +380 -0
  104. package/examples/Cover pair.json +230 -0
  105. package/examples/Home theatre activity.json +4 -4
  106. package/examples/House schedule.json +164 -0
  107. package/examples/Room lighting.json +4 -3
  108. package/package.json +6 -2
@@ -0,0 +1,91 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ID_SCHEMES = exports.OFFLINE = exports.ONLINE = exports.DEFAULT_DISCOVERY_PREFIX = exports.DISCOVERY_ORIGIN = void 0;
4
+ exports.discoveryPrefixSetting = discoveryPrefixSetting;
5
+ exports.discoveryDevice = discoveryDevice;
6
+ exports.ownDiscoveryTopics = ownDiscoveryTopics;
7
+ exports.discoveryEntity = discoveryEntity;
8
+ exports.timeEntityValue = timeEntityValue;
9
+ // What every Home Assistant discovery entry shares: the origin that says
10
+ // which software published it, the discovery prefix setting, and the
11
+ // availability payloads.
12
+ const settings_1 = require("./settings");
13
+ const topic_names_1 = require("./topic-names");
14
+ const circadian_schedule_1 = require("./circadian-schedule");
15
+ // The package's own manifest, loaded as a module from the package root (two
16
+ // levels above dist/lib), so no file is read here. A JSON import would make
17
+ // the compiler copy package.json into dist/, which isn't published.
18
+ const manifest = require("../../package.json");
19
+ // Home Assistant's `origin` block, abbreviated `o`, with its `name`,
20
+ // `sw` (software version) and `url` (support URL).
21
+ exports.DISCOVERY_ORIGIN = Object.freeze({
22
+ name: manifest.name,
23
+ sw: manifest.version,
24
+ url: manifest.homepage.split("#")[0]
25
+ });
26
+ // Home Assistant's default discovery prefix.
27
+ exports.DEFAULT_DISCOVERY_PREFIX = "homeassistant";
28
+ // The topic prefix Home Assistant listens on for discovery; absent means
29
+ // Home Assistant's default.
30
+ function discoveryPrefixSetting(value) {
31
+ const prefix = (0, settings_1.textSetting)(value, "discovery prefix", exports.DEFAULT_DISCOVERY_PREFIX).replace(/^\/+|\/+$/g, "");
32
+ if (!prefix || /[+#]/.test(prefix))
33
+ throw new Error("discovery prefix must be a concrete MQTT topic prefix");
34
+ return prefix;
35
+ }
36
+ // A publishing node's availability, retained on its availability topic:
37
+ // online when it starts, offline when it stops. These are Home Assistant's
38
+ // default availability payloads, so entities need only the topic.
39
+ exports.ONLINE = "online";
40
+ exports.OFFLINE = "offline";
41
+ // How a node names its Home Assistant device and entities. "area" is how
42
+ // every node did before node IDs: unique IDs, discovery topics and the
43
+ // device come from the area. "node" names them after the Node-RED node, so
44
+ // they stay the same when the area changes. Saved nodes keep "area"; the
45
+ // editor gives new nodes "node".
46
+ exports.ID_SCHEMES = ["area", "node"];
47
+ // The node's ID in a form fit for unique IDs and discovery topics.
48
+ function nodeKey(nodeId) {
49
+ return "nrha_" + nodeId.replace(/[^A-Za-z0-9_-]+/g, "_");
50
+ }
51
+ // The device that groups a node's entities, one per node. Under "area" it
52
+ // is nodered_<area ID>_<kind>, for example nodered_kitchen_lighting.
53
+ function discoveryDevice(owner, kind) {
54
+ return { identifiers: [owner.idScheme === "node" ? nodeKey(owner.nodeId ?? "") : `nodered_${(0, topic_names_1.areaId)(owner.area)}_${kind}`] };
55
+ }
56
+ // Of the given topics, the discovery topics named after this node itself:
57
+ // under the "node" scheme, those whose object ID is nrha_<node ID>_<entity>
58
+ // (the key ends in "_", and the IDs Node-RED makes never contain one: hex
59
+ // digits, joined with "-" for a node in a subflow, so one node's key can't
60
+ // prefix another's; only an ID written by hand with "_" could). Under
61
+ // "area" none are, since another node may share the area, so a node can
62
+ // clear these without ever touching another node's topics.
63
+ function ownDiscoveryTopics(owner, topics) {
64
+ if (owner.idScheme !== "node" || !owner.nodeId)
65
+ return [];
66
+ const key = nodeKey(owner.nodeId) + "_";
67
+ return topics.filter((topic) => {
68
+ const parts = topic.split("/");
69
+ return topic.startsWith(owner.discoveryPrefix + "/") && parts.at(-1) === "config" && (parts.at(-2) ?? "").startsWith(key);
70
+ });
71
+ }
72
+ // An entity's discovery topic and IDs: the topic's object ID and the unique
73
+ // ID are <area ID>_<entity> under "area" and nrha_<node ID>_<entity> under
74
+ // "node". The entity ID Home Assistant first gives it comes from the area
75
+ // either way, with "-" as "_", since entity IDs allow only lowercase
76
+ // letters, digits and "_".
77
+ function discoveryEntity(owner, component, entity) {
78
+ const uniqueId = (owner.idScheme === "node" ? nodeKey(owner.nodeId ?? "") : (0, topic_names_1.areaId)(owner.area)) + "_" + entity;
79
+ return {
80
+ topic: `${owner.discoveryPrefix}/${component}/${uniqueId}/config`,
81
+ uniqueId,
82
+ defaultEntityId: `${component}.${(0, topic_names_1.areaId)(owner.area).replace(/-/g, "_")}_${entity}`
83
+ };
84
+ }
85
+ // A time from Home Assistant's time entity ("HH:MM:SS", in ISO format), or
86
+ // "HH:MM", as "HH:MM"; null if it isn't one.
87
+ function timeEntityValue(payload) {
88
+ const match = typeof payload === "string" ? /^(\d{2}:\d{2})(?::[0-5]\d(?:\.\d+)?)?$/.exec(payload.trim()) : null;
89
+ return match && (0, circadian_schedule_1.clockMinutes)(match[1]) !== null ? match[1] : null;
90
+ }
91
+ //# sourceMappingURL=ha-discovery.js.map
@@ -0,0 +1,23 @@
1
+ import type { TraceEntry } from "./bounded-trace";
2
+ import type { DaySchedule } from "./circadian-schedule";
3
+ import type { HouseScheduleEvent } from "./house-schedule";
4
+ import { type OptInPublishingConfig, type RetainedText } from "./publishing-node";
5
+ type ScheduleTime = "wake-time" | "bed-time";
6
+ export declare const houseStateTopic: (name: string, which: ScheduleTime) => string;
7
+ export declare const houseCommandTopic: (name: string, which: ScheduleTime) => string;
8
+ export type HouseSchedulePublishingConfig = OptInPublishingConfig;
9
+ export declare const houseAvailabilityTopic: (name: string) => string;
10
+ export declare function houseDiscovery(config: HouseSchedulePublishingConfig): RetainedText[];
11
+ export declare function houseStateOutputs(config: HouseSchedulePublishingConfig, schedule: DaySchedule): RetainedText[];
12
+ export declare const houseStartingOutputs: (config: OptInPublishingConfig, state: DaySchedule) => RetainedText[];
13
+ export declare const houseStoppingOutputs: (config: OptInPublishingConfig) => RetainedText[];
14
+ export declare const houseRetainedTopics: (config: OptInPublishingConfig) => string[];
15
+ export declare function houseInbound(msg: {
16
+ topic?: unknown;
17
+ payload?: unknown;
18
+ retain?: unknown;
19
+ }, config: HouseSchedulePublishingConfig): {
20
+ event: HouseScheduleEvent | null;
21
+ traces: TraceEntry[];
22
+ } | null;
23
+ export {};
@@ -0,0 +1,71 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.houseRetainedTopics = exports.houseStoppingOutputs = exports.houseStartingOutputs = exports.houseAvailabilityTopic = exports.houseCommandTopic = exports.houseStateTopic = void 0;
4
+ exports.houseDiscovery = houseDiscovery;
5
+ exports.houseStateOutputs = houseStateOutputs;
6
+ exports.houseInbound = houseInbound;
7
+ const ha_discovery_1 = require("./ha-discovery");
8
+ const publishing_node_1 = require("./publishing-node");
9
+ const topic_names_1 = require("./topic-names");
10
+ // The one place a house schedule topic is named.
11
+ function houseTopic(name, path) {
12
+ return "home/" + (0, topic_names_1.areaId)(name) + "/schedule/" + path;
13
+ }
14
+ const houseStateTopic = (name, which) => houseTopic(name, which + "/state");
15
+ exports.houseStateTopic = houseStateTopic;
16
+ const houseCommandTopic = (name, which) => houseTopic(name, which + "/set");
17
+ exports.houseCommandTopic = houseCommandTopic;
18
+ const DEVICE = { kind: "schedule", label: "Schedule", model: "House schedule" };
19
+ const houseAvailabilityTopic = (name) => houseTopic(name, "availability");
20
+ exports.houseAvailabilityTopic = houseAvailabilityTopic;
21
+ function houseDiscovery(config) {
22
+ const time = (which, id, name, icon) => (0, publishing_node_1.discoveryEntry)(config, DEVICE, (0, exports.houseAvailabilityTopic)(config.area), { component: "time", id, name, fields: {
23
+ icon,
24
+ command_topic: (0, exports.houseCommandTopic)(config.area, which),
25
+ state_topic: (0, exports.houseStateTopic)(config.area, which),
26
+ entity_category: "config"
27
+ } });
28
+ return [
29
+ time("wake-time", "wake_time", "Wake time", "mdi:weather-sunset-up"),
30
+ time("bed-time", "bed_time", "Bed time", "mdi:weather-night")
31
+ ];
32
+ }
33
+ function houseStateOutputs(config, schedule) {
34
+ // "HH:MM:SS", as Home Assistant's time entity reads it.
35
+ return (0, publishing_node_1.publishesState)(config)
36
+ ? [
37
+ (0, publishing_node_1.retainedText)((0, exports.houseStateTopic)(config.area, "wake-time"), schedule.wakeTime + ":00"),
38
+ (0, publishing_node_1.retainedText)((0, exports.houseStateTopic)(config.area, "bed-time"), schedule.bedTime + ":00")
39
+ ]
40
+ : [];
41
+ }
42
+ const house = (0, publishing_node_1.publishingNode)({
43
+ entries: houseDiscovery,
44
+ availabilityTopic: exports.houseAvailabilityTopic,
45
+ stateTopics: (config) => [(0, exports.houseStateTopic)(config.area, "wake-time"), (0, exports.houseStateTopic)(config.area, "bed-time")],
46
+ stateOutputs: houseStateOutputs
47
+ });
48
+ exports.houseStartingOutputs = house.starting;
49
+ exports.houseStoppingOutputs = house.stopping;
50
+ exports.houseRetainedTopics = house.retainedTopics;
51
+ // A time from Home Assistant on the house's wake-time or bed-time set topic,
52
+ // as a schedule event; retained messages and anything but a time are
53
+ // ignored with a trace. Read only while the house publishes.
54
+ function houseInbound(msg, config) {
55
+ if (!(0, publishing_node_1.publishesState)(config))
56
+ return null;
57
+ const which = msg.topic === (0, exports.houseCommandTopic)(config.area, "wake-time") ? "wake-time"
58
+ : msg.topic === (0, exports.houseCommandTopic)(config.area, "bed-time") ? "bed-time"
59
+ : null;
60
+ if (!which)
61
+ return null;
62
+ const name = which + "-set";
63
+ if (msg.retain === true)
64
+ return { event: null, traces: [{ event: name, outcome: "ignored", reason: "retained-message", details: {} }] };
65
+ const time = (0, ha_discovery_1.timeEntityValue)(msg.payload);
66
+ if (time === null) {
67
+ return { event: null, traces: [{ event: name, outcome: "ignored", reason: "not-a-time", details: { payload: typeof msg.payload === "string" ? msg.payload : null } }] };
68
+ }
69
+ return { event: which === "wake-time" ? { kind: "schedule", wakeTime: time } : { kind: "schedule", bedTime: time }, traces: [] };
70
+ }
71
+ //# sourceMappingURL=house-schedule-publishing.js.map
@@ -0,0 +1,23 @@
1
+ import type { TraceEntry } from "./bounded-trace";
2
+ import type { DaySchedule } from "./circadian-schedule";
3
+ export interface HouseScheduleState {
4
+ schedule: DaySchedule;
5
+ scheduleSetting: DaySchedule;
6
+ }
7
+ export interface HouseScheduleEvent {
8
+ kind: "schedule";
9
+ wakeTime?: unknown;
10
+ bedTime?: unknown;
11
+ }
12
+ export interface SharedSchedule {
13
+ readonly name: string;
14
+ schedule(): DaySchedule;
15
+ change(event: HouseScheduleEvent, traces?: TraceEntry[]): void;
16
+ subscribe(listener: (schedule: DaySchedule) => void): () => void;
17
+ }
18
+ export declare function houseScheduleInUse(saved: Partial<HouseScheduleState> | undefined, setting: DaySchedule): DaySchedule;
19
+ export declare function transitionHouseSchedule(saved: Partial<HouseScheduleState> | undefined, event: HouseScheduleEvent, setting: DaySchedule): {
20
+ state: HouseScheduleState;
21
+ changed: boolean;
22
+ traces: TraceEntry<Record<string, unknown>>[];
23
+ };
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.houseScheduleInUse = houseScheduleInUse;
4
+ exports.transitionHouseSchedule = transitionHouseSchedule;
5
+ const room_schedule_1 = require("./room-schedule");
6
+ const settings_1 = require("./settings");
7
+ // The times in use: each saved time while its setting is unchanged.
8
+ function houseScheduleInUse(saved, setting) {
9
+ return {
10
+ wakeTime: (0, settings_1.keptOverride)(saved?.schedule?.wakeTime, saved?.scheduleSetting?.wakeTime, setting.wakeTime),
11
+ bedTime: (0, settings_1.keptOverride)(saved?.schedule?.bedTime, saved?.scheduleSetting?.bedTime, setting.bedTime)
12
+ };
13
+ }
14
+ function transitionHouseSchedule(saved, event, setting) {
15
+ const state = { schedule: houseScheduleInUse(saved, setting), scheduleSetting: { ...setting } };
16
+ const traces = [];
17
+ let changed = false;
18
+ const next = (0, room_schedule_1.requestedSchedule)(state.schedule, event);
19
+ if (!next) {
20
+ traces.push({ event: "schedule", outcome: "ignored", reason: "invalid-times", details: { wakeTime: event.wakeTime, bedTime: event.bedTime } });
21
+ }
22
+ else if (next.wakeTime !== state.schedule.wakeTime || next.bedTime !== state.schedule.bedTime) {
23
+ state.schedule = next;
24
+ traces.push({ event: "schedule", outcome: "changed", reason: null, details: { ...next } });
25
+ changed = true;
26
+ }
27
+ return { state, changed, traces };
28
+ }
29
+ //# sourceMappingURL=house-schedule.js.map
@@ -4,7 +4,9 @@ export interface MotionConfig {
4
4
  defaultDelaySeconds: number;
5
5
  onTargets: string[];
6
6
  offTargets: string[];
7
+ maxPauseSeconds?: number;
7
8
  }
9
+ export declare const DEFAULT_MAX_PAUSE_SECONDS = 600;
8
10
  export interface MotionState {
9
11
  areaEnabled: boolean;
10
12
  illuminance: number | null;
@@ -15,10 +17,15 @@ export interface MotionState {
15
17
  offTimerGeneration: number;
16
18
  offTimerDueAt: number | null;
17
19
  offTimerArmedAt: number | null;
20
+ pausedBy: Array<{
21
+ owner: string;
22
+ until: number;
23
+ }>;
18
24
  }
19
25
  export interface MotionInput {
20
26
  source: string;
21
27
  payload?: unknown;
28
+ owner?: string;
22
29
  generation?: number;
23
30
  }
24
31
  export interface LightingIntent {
@@ -46,6 +53,7 @@ export declare function normaliseMotionConfig(raw: {
46
53
  offTargets?: unknown;
47
54
  defaultDelaySeconds?: unknown;
48
55
  defaultIlluminanceCutoff?: unknown;
56
+ maxPauseSeconds?: unknown;
49
57
  }): MotionConfig;
50
58
  export declare function createMotionState(config: MotionConfig, saved?: Partial<MotionState>): MotionState;
51
59
  export declare function lightingIntent(targets: string[], state: string, source: string, extra?: Record<string, unknown>): LightingIntent;
@@ -56,6 +64,7 @@ export declare function transitionMotion(saved: Partial<MotionState> | undefined
56
64
  traces: TraceEntry<Record<string, unknown>>[];
57
65
  status: {
58
66
  enabled: boolean;
67
+ paused: boolean;
59
68
  illuminance: number | null;
60
69
  delaySeconds: number;
61
70
  };
@@ -1,11 +1,13 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DEFAULT_MAX_PAUSE_SECONDS = void 0;
3
4
  exports.booleanOn = booleanOn;
4
5
  exports.normaliseMotionConfig = normaliseMotionConfig;
5
6
  exports.createMotionState = createMotionState;
6
7
  exports.lightingIntent = lightingIntent;
7
8
  exports.transitionMotion = transitionMotion;
8
9
  const settings_1 = require("./settings");
10
+ exports.DEFAULT_MAX_PAUSE_SECONDS = 600;
9
11
  function booleanOn(value) {
10
12
  return value === true || (typeof value === "string" && value.toLowerCase() === "on");
11
13
  }
@@ -32,7 +34,8 @@ function normaliseMotionConfig(raw) {
32
34
  onTargets,
33
35
  offTargets: targetList(raw.offTargets, "off targets", onTargets),
34
36
  defaultDelaySeconds: (0, settings_1.numberSetting)(raw.defaultDelaySeconds, "vacancy delay", 120),
35
- defaultIlluminanceCutoff: (0, settings_1.numberSetting)(raw.defaultIlluminanceCutoff, "illuminance cutoff", 10)
37
+ defaultIlluminanceCutoff: (0, settings_1.numberSetting)(raw.defaultIlluminanceCutoff, "illuminance cutoff", 10),
38
+ maxPauseSeconds: Math.max(1, (0, settings_1.numberSetting)(raw.maxPauseSeconds, "maximum pause", exports.DEFAULT_MAX_PAUSE_SECONDS))
36
39
  };
37
40
  }
38
41
  // Saved wall-switch state from earlier versions (wallInput and the older
@@ -40,16 +43,12 @@ function normaliseMotionConfig(raw) {
40
43
  function createMotionState(config, saved = {}) {
41
44
  return {
42
45
  areaEnabled: saved.areaEnabled !== false,
46
+ pausedBy: Array.isArray(saved.pausedBy)
47
+ ? saved.pausedBy.filter((pause) => typeof pause?.owner === "string" && Number.isFinite(pause.until)).map((pause) => ({ ...pause }))
48
+ : [],
43
49
  illuminance: saved.illuminance ?? null,
44
- // A saved value stands only while the setting it was based on is
45
- // unchanged; state saved before this rule recorded none, so the setting
46
- // applies.
47
- illuminanceCutoff: saved.illuminanceCutoffSetting === config.defaultIlluminanceCutoff && saved.illuminanceCutoff !== undefined
48
- ? saved.illuminanceCutoff
49
- : config.defaultIlluminanceCutoff,
50
- delaySeconds: saved.delaySecondsSetting === config.defaultDelaySeconds && saved.delaySeconds !== undefined
51
- ? saved.delaySeconds
52
- : config.defaultDelaySeconds,
50
+ illuminanceCutoff: (0, settings_1.keptOverride)(saved.illuminanceCutoff, saved.illuminanceCutoffSetting, config.defaultIlluminanceCutoff),
51
+ delaySeconds: (0, settings_1.keptOverride)(saved.delaySeconds, saved.delaySecondsSetting, config.defaultDelaySeconds),
53
52
  illuminanceCutoffSetting: config.defaultIlluminanceCutoff,
54
53
  delaySecondsSetting: config.defaultDelaySeconds,
55
54
  offTimerGeneration: Number(saved.offTimerGeneration) || 0,
@@ -73,8 +72,13 @@ function transitionMotion(saved, input, config, globalEnabled, now = Date.now())
73
72
  const effects = [];
74
73
  const traces = [];
75
74
  let nextGlobalEnabled = globalEnabled !== false;
75
+ // A pause past its time counts as released, even if its owner never said.
76
+ for (const pause of state.pausedBy.filter(({ until }) => until <= now)) {
77
+ traces.push({ event: "pause", outcome: "expired", reason: null, details: { owner: pause.owner } });
78
+ }
79
+ state.pausedBy = state.pausedBy.filter(({ until }) => until > now);
76
80
  function enabled() {
77
- return state.areaEnabled && nextGlobalEnabled;
81
+ return state.areaEnabled && nextGlobalEnabled && state.pausedBy.length === 0;
78
82
  }
79
83
  function trace(event, outcome, reason, details = {}) {
80
84
  traces.push({ event, outcome, reason, details });
@@ -123,6 +127,25 @@ function transitionMotion(saved, input, config, globalEnabled, now = Date.now())
123
127
  if (!enabled())
124
128
  cancelOffTimer("motion-disabled");
125
129
  }
130
+ else if (source === "pause") {
131
+ // true pauses for this owner until the maximum pause (sending it again
132
+ // renews it), false releases it; motion resumes once every owner has
133
+ // released, or their pauses have run out.
134
+ const owner = typeof input.owner === "string" ? input.owner.trim() : "";
135
+ if (!owner) {
136
+ trace("pause", "ignored", "owner-required");
137
+ }
138
+ else {
139
+ const pause = payload === true || booleanOn(payload);
140
+ const held = state.pausedBy.some((entry) => entry.owner === owner);
141
+ const others = state.pausedBy.filter((entry) => entry.owner !== owner);
142
+ const until = now + (config.maxPauseSeconds ?? exports.DEFAULT_MAX_PAUSE_SECONDS) * 1000;
143
+ state.pausedBy = pause ? [...others, { owner, until }] : others;
144
+ trace("pause", pause ? (held ? "renewed" : "paused") : held ? "released" : "unchanged", null, { owner, until: pause ? until : null });
145
+ if (!enabled())
146
+ cancelOffTimer("motion-paused");
147
+ }
148
+ }
126
149
  else if (source === "area-enabled") {
127
150
  const previous = state.areaEnabled;
128
151
  state.areaEnabled = booleanOn(payload);
@@ -230,6 +253,16 @@ function transitionMotion(saved, input, config, globalEnabled, now = Date.now())
230
253
  emitIntent(lightingIntent(config.offTargets, "OFF", "motion", { vacantSince }), "off-timer");
231
254
  }
232
255
  }
256
+ else if (source === "cold-start") {
257
+ // Started with nothing saved: a first deploy, or a redeploy or restart
258
+ // without a context store, when a running off-timer was lost. Whether the
259
+ // room is occupied is unknown, so vacancy starts now: the off-timer runs
260
+ // unless occupancy is reported first, so lights left on are never on for
261
+ // good.
262
+ if (enabled())
263
+ scheduleOffTimer();
264
+ trace("start", enabled() ? "schedule-off" : "suppressed", enabled() ? "no-saved-state" : "disabled", { targets: config.offTargets });
265
+ }
233
266
  else if (source.startsWith("switch")) {
234
267
  trace("message", "ignored", "wall switches belong to the switch policy", { source });
235
268
  }
@@ -240,6 +273,7 @@ function transitionMotion(saved, input, config, globalEnabled, now = Date.now())
240
273
  traces,
241
274
  status: {
242
275
  enabled: enabled(),
276
+ paused: state.pausedBy.length > 0,
243
277
  illuminance: state.illuminance,
244
278
  delaySeconds: state.delaySeconds
245
279
  }
@@ -0,0 +1,28 @@
1
+ import type { TraceEntry } from "./bounded-trace";
2
+ import type { MotionInput } from "./motion-policy";
3
+ import { type OptInPublishingConfig, type RetainedText } from "./publishing-node";
4
+ export type MotionPublishingConfig = OptInPublishingConfig;
5
+ export type MotionPublication = RetainedText;
6
+ export declare const MAX_DELAY_SECONDS = 86400;
7
+ export declare const motionEnabledTopic: (area: string, end: "state" | "set") => string;
8
+ export declare const motionDelayTopic: (area: string, end: "state" | "set") => string;
9
+ export declare const motionAvailabilityTopic: (area: string) => string;
10
+ export declare function motionDiscovery(config: MotionPublishingConfig): MotionPublication[];
11
+ export declare function motionStateOutputs(config: MotionPublishingConfig, state: {
12
+ areaEnabled: boolean;
13
+ delaySeconds: number;
14
+ }): MotionPublication[];
15
+ export declare const motionStartingOutputs: (config: OptInPublishingConfig, state: {
16
+ areaEnabled: boolean;
17
+ delaySeconds: number;
18
+ }) => RetainedText[];
19
+ export declare const motionStoppingOutputs: (config: OptInPublishingConfig) => RetainedText[];
20
+ export declare const motionRetainedTopics: (config: OptInPublishingConfig) => string[];
21
+ export declare function motionInbound(msg: {
22
+ topic?: unknown;
23
+ payload?: unknown;
24
+ retain?: unknown;
25
+ }, config: MotionPublishingConfig): {
26
+ input: MotionInput;
27
+ traces: TraceEntry[];
28
+ } | null;
@@ -0,0 +1,92 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.motionRetainedTopics = exports.motionStoppingOutputs = exports.motionStartingOutputs = exports.motionAvailabilityTopic = exports.motionDelayTopic = exports.motionEnabledTopic = exports.MAX_DELAY_SECONDS = void 0;
4
+ exports.motionDiscovery = motionDiscovery;
5
+ exports.motionStateOutputs = motionStateOutputs;
6
+ exports.motionInbound = motionInbound;
7
+ const topic_names_1 = require("./topic-names");
8
+ const publishing_node_1 = require("./publishing-node");
9
+ // The vacancy delays Home Assistant offers, in seconds: up to a day.
10
+ exports.MAX_DELAY_SECONDS = 86400;
11
+ function motionTopic(area, name) {
12
+ return "home/" + (0, topic_names_1.areaId)(area) + "/motion/" + name;
13
+ }
14
+ const motionEnabledTopic = (area, end) => motionTopic(area, "enabled/" + end);
15
+ exports.motionEnabledTopic = motionEnabledTopic;
16
+ const motionDelayTopic = (area, end) => motionTopic(area, "delay/" + end);
17
+ exports.motionDelayTopic = motionDelayTopic;
18
+ const motionAvailabilityTopic = (area) => motionTopic(area, "availability");
19
+ exports.motionAvailabilityTopic = motionAvailabilityTopic;
20
+ const DEVICE = { kind: "motion", label: "Motion Lighting", model: "Motion lighting policy" };
21
+ // The enable switch and the vacancy delay number, on one device per node.
22
+ function motionDiscovery(config) {
23
+ const availability = (0, exports.motionAvailabilityTopic)(config.area);
24
+ return [
25
+ (0, publishing_node_1.discoveryEntry)(config, DEVICE, availability, { component: "switch", id: "motion_lighting", name: "Motion lighting", fields: {
26
+ icon: "mdi:motion-sensor",
27
+ command_topic: (0, exports.motionEnabledTopic)(config.area, "set"),
28
+ state_topic: (0, exports.motionEnabledTopic)(config.area, "state"),
29
+ payload_on: "ON",
30
+ payload_off: "OFF",
31
+ optimistic: false
32
+ } }),
33
+ (0, publishing_node_1.discoveryEntry)(config, DEVICE, availability, { component: "number", id: "vacancy_delay", name: "Vacancy delay", fields: {
34
+ icon: "mdi:timer-outline",
35
+ command_topic: (0, exports.motionDelayTopic)(config.area, "set"),
36
+ state_topic: (0, exports.motionDelayTopic)(config.area, "state"),
37
+ device_class: "duration",
38
+ unit_of_measurement: "s",
39
+ min: 0,
40
+ max: exports.MAX_DELAY_SECONDS,
41
+ step: 1,
42
+ mode: "box",
43
+ entity_category: "config"
44
+ } })
45
+ ];
46
+ }
47
+ // This node's enable state and vacancy delay, whenever either may change.
48
+ function motionStateOutputs(config, state) {
49
+ if (!(0, publishing_node_1.publishesState)(config))
50
+ return [];
51
+ return [
52
+ (0, publishing_node_1.retainedText)((0, exports.motionEnabledTopic)(config.area, "state"), state.areaEnabled ? "ON" : "OFF"),
53
+ (0, publishing_node_1.retainedText)((0, exports.motionDelayTopic)(config.area, "state"), String(state.delaySeconds))
54
+ ];
55
+ }
56
+ const motion = (0, publishing_node_1.publishingNode)({
57
+ entries: motionDiscovery,
58
+ availabilityTopic: exports.motionAvailabilityTopic,
59
+ stateTopics: (config) => [(0, exports.motionEnabledTopic)(config.area, "state"), (0, exports.motionDelayTopic)(config.area, "state")],
60
+ stateOutputs: motionStateOutputs
61
+ });
62
+ exports.motionStartingOutputs = motion.starting;
63
+ exports.motionStoppingOutputs = motion.stopping;
64
+ exports.motionRetainedTopics = motion.retainedTopics;
65
+ // MQTT the node reads, as its own inputs: ON or OFF on the enable set topic,
66
+ // and a delay in seconds on the delay set topic. Read only while it
67
+ // publishes; retained messages and other payloads are ignored with a trace.
68
+ // Anything else passes through unchanged.
69
+ function motionInbound(msg, config) {
70
+ if (!(0, publishing_node_1.publishesState)(config))
71
+ return null;
72
+ const enabledSet = msg.topic === (0, exports.motionEnabledTopic)(config.area, "set");
73
+ const delaySet = msg.topic === (0, exports.motionDelayTopic)(config.area, "set");
74
+ if (!enabledSet && !delaySet)
75
+ return null;
76
+ const event = enabledSet ? "enabled-set" : "delay-set";
77
+ const ignored = (reason, details = {}) => ({ input: { source: "ignored", payload: undefined }, traces: [{ event, outcome: "ignored", reason, details }] });
78
+ if (msg.retain === true)
79
+ return ignored("retained-message");
80
+ const text = typeof msg.payload === "string" ? msg.payload.trim() : typeof msg.payload === "number" ? String(msg.payload) : "";
81
+ if (enabledSet) {
82
+ const requested = text.toUpperCase();
83
+ if (requested !== "ON" && requested !== "OFF")
84
+ return ignored("not-on-or-off", { payload: text });
85
+ return { input: { source: "area-enabled", payload: requested }, traces: [] };
86
+ }
87
+ const seconds = text === "" ? NaN : Number(text);
88
+ if (!Number.isFinite(seconds) || seconds < 0 || seconds > exports.MAX_DELAY_SECONDS)
89
+ return ignored("not-a-delay", { payload: text });
90
+ return { input: { source: "delay", payload: Math.round(seconds) }, traces: [] };
91
+ }
92
+ //# sourceMappingURL=motion-publishing.js.map
@@ -0,0 +1,10 @@
1
+ import { type OptInPublishingConfig, type RetainedText } from "./publishing-node";
2
+ export type OccupancyPublishingConfig = OptInPublishingConfig;
3
+ export type OccupancyPublication = RetainedText;
4
+ export declare function occupancyStateTopic(area: string): string;
5
+ export declare function occupancyAvailabilityTopic(area: string): string;
6
+ export declare function occupancyDiscovery(config: OccupancyPublishingConfig): OccupancyPublication;
7
+ export declare function occupancyStateOutputs(config: OccupancyPublishingConfig, occupied: boolean): OccupancyPublication[];
8
+ export declare const occupancyStartingOutputs: (config: OptInPublishingConfig, state: boolean) => RetainedText[];
9
+ export declare const occupancyStoppingOutputs: (config: OptInPublishingConfig) => RetainedText[];
10
+ export declare const occupancyRetainedTopics: (config: OptInPublishingConfig) => string[];
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.occupancyRetainedTopics = exports.occupancyStoppingOutputs = exports.occupancyStartingOutputs = void 0;
4
+ exports.occupancyStateTopic = occupancyStateTopic;
5
+ exports.occupancyAvailabilityTopic = occupancyAvailabilityTopic;
6
+ exports.occupancyDiscovery = occupancyDiscovery;
7
+ exports.occupancyStateOutputs = occupancyStateOutputs;
8
+ // Occupancy fusion's MQTT and Home Assistant side: the room's occupancy as a
9
+ // retained ON/OFF state, its availability, and an occupancy binary sensor.
10
+ // All of it is opt-in (see publishing-node.ts); the fusion itself only
11
+ // decides occupancy.
12
+ const topic_names_1 = require("./topic-names");
13
+ const publishing_node_1 = require("./publishing-node");
14
+ function occupancyTopic(area, name) {
15
+ return "home/" + (0, topic_names_1.areaId)(area) + "/occupancy/" + name;
16
+ }
17
+ function occupancyStateTopic(area) {
18
+ return occupancyTopic(area, "state");
19
+ }
20
+ function occupancyAvailabilityTopic(area) {
21
+ return occupancyTopic(area, "availability");
22
+ }
23
+ // The occupancy binary sensor's discovery entry.
24
+ function occupancyDiscovery(config) {
25
+ return (0, publishing_node_1.discoveryEntry)(config, { kind: "occupancy", label: "Occupancy", model: "Occupancy fusion" }, occupancyAvailabilityTopic(config.area), {
26
+ component: "binary_sensor",
27
+ id: "occupancy",
28
+ name: "Occupancy",
29
+ fields: { device_class: "occupancy", state_topic: occupancyStateTopic(config.area), payload_on: "ON", payload_off: "OFF" }
30
+ });
31
+ }
32
+ // The occupancy state, published whenever it changes.
33
+ function occupancyStateOutputs(config, occupied) {
34
+ return (0, publishing_node_1.publishesState)(config) ? [(0, publishing_node_1.retainedText)(occupancyStateTopic(config.area), occupied ? "ON" : "OFF")] : [];
35
+ }
36
+ const occupancy = (0, publishing_node_1.publishingNode)({
37
+ entries: (config) => [occupancyDiscovery(config)],
38
+ availabilityTopic: occupancyAvailabilityTopic,
39
+ stateTopics: (config) => [occupancyStateTopic(config.area)],
40
+ stateOutputs: occupancyStateOutputs
41
+ });
42
+ exports.occupancyStartingOutputs = occupancy.starting;
43
+ exports.occupancyStoppingOutputs = occupancy.stopping;
44
+ exports.occupancyRetainedTopics = occupancy.retainedTopics;
45
+ //# sourceMappingURL=occupancy-publishing.js.map