@avantics/node-red-home-automation 5.0.0 → 6.1.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 (91) hide show
  1. package/README.md +413 -77
  2. package/contract-examples/room-lighting.mjs +39 -27
  3. package/dist/lib/activity-lighting-controller.d.ts +7 -2
  4. package/dist/lib/activity-lighting-controller.js +75 -16
  5. package/dist/lib/avr-controller.d.ts +2 -2
  6. package/dist/lib/avr-controller.js +21 -26
  7. package/dist/lib/circadian-policy.d.ts +47 -0
  8. package/dist/lib/circadian-policy.js +111 -0
  9. package/dist/lib/circadian-publishing.d.ts +33 -0
  10. package/dist/lib/circadian-publishing.js +154 -0
  11. package/dist/lib/circadian-schedule.d.ts +4 -0
  12. package/dist/lib/circadian-schedule.js +12 -0
  13. package/dist/lib/command-echo.d.ts +8 -1
  14. package/dist/lib/command-echo.js +37 -6
  15. package/dist/lib/cover-controller.js +4 -0
  16. package/dist/lib/denon-protocol.js +3 -2
  17. package/dist/lib/enable-input.d.ts +3 -0
  18. package/dist/lib/enable-input.js +18 -0
  19. package/dist/lib/house-schedule.js +2 -2
  20. package/dist/lib/light-contract.d.ts +53 -0
  21. package/dist/lib/light-contract.js +11 -0
  22. package/dist/lib/motion-policy.d.ts +0 -1
  23. package/dist/lib/motion-policy.js +9 -8
  24. package/dist/lib/motion-publishing.js +2 -1
  25. package/dist/lib/mqtt-message.d.ts +6 -0
  26. package/dist/lib/mqtt-message.js +21 -0
  27. package/dist/lib/room-controller.d.ts +41 -27
  28. package/dist/lib/room-controller.js +422 -213
  29. package/dist/lib/room-holds.d.ts +34 -0
  30. package/dist/lib/room-holds.js +66 -0
  31. package/dist/lib/room-lighting-discovery.d.ts +0 -14
  32. package/dist/lib/room-lighting-discovery.js +19 -125
  33. package/dist/lib/room-publishing.d.ts +7 -12
  34. package/dist/lib/room-publishing.js +65 -125
  35. package/dist/lib/shelly-cover.d.ts +2 -4
  36. package/dist/lib/shelly-cover.js +30 -70
  37. package/dist/lib/shelly-gen2.d.ts +25 -0
  38. package/dist/lib/shelly-gen2.js +58 -0
  39. package/dist/lib/shelly-switch.d.ts +95 -0
  40. package/dist/lib/shelly-switch.js +237 -0
  41. package/dist/lib/theatre-state-contract.js +12 -4
  42. package/dist/lib/z2m-cover.d.ts +49 -0
  43. package/dist/lib/z2m-cover.js +120 -0
  44. package/dist/lib/z2m-light-adapter.d.ts +62 -0
  45. package/dist/lib/z2m-light-adapter.js +195 -0
  46. package/dist/lib/z2m-light.d.ts +2 -2
  47. package/dist/lib/z2m-light.js +11 -14
  48. package/dist/nodes/activity-lighting-controller.html +15 -6
  49. package/dist/nodes/activity-lighting-controller.js +21 -0
  50. package/dist/nodes/adapter.d.ts +5 -1
  51. package/dist/nodes/adapter.js +35 -2
  52. package/dist/nodes/avr-controller.html +20 -18
  53. package/dist/nodes/avr-controller.js +5 -5
  54. package/dist/nodes/circadian-policy.d.ts +3 -0
  55. package/dist/nodes/circadian-policy.html +145 -0
  56. package/dist/nodes/circadian-policy.js +162 -0
  57. package/dist/nodes/cover-controller.html +9 -3
  58. package/dist/nodes/cover-controller.js +1 -1
  59. package/dist/nodes/display-mode-controller.html +14 -17
  60. package/dist/nodes/display-mode-controller.js +4 -1
  61. package/dist/nodes/house-schedule-mqtt.html +5 -0
  62. package/dist/nodes/house-schedule-mqtt.js +1 -1
  63. package/dist/nodes/house-schedule.html +3 -3
  64. package/dist/nodes/motion-policy.html +10 -2
  65. package/dist/nodes/motion-policy.js +4 -1
  66. package/dist/nodes/occupancy-fusion.html +5 -0
  67. package/dist/nodes/occupancy-fusion.js +1 -1
  68. package/dist/nodes/refused-enable.d.ts +7 -0
  69. package/dist/nodes/refused-enable.js +24 -0
  70. package/dist/nodes/room-controller.html +232 -85
  71. package/dist/nodes/room-controller.js +71 -97
  72. package/dist/nodes/shelly-switch.d.ts +3 -0
  73. package/dist/nodes/shelly-switch.html +87 -0
  74. package/dist/nodes/shelly-switch.js +48 -0
  75. package/dist/nodes/theatre-state-contract.html +9 -3
  76. package/dist/nodes/theatre-state-contract.js +1 -1
  77. package/dist/nodes/z2m-cover.d.ts +3 -0
  78. package/dist/nodes/z2m-cover.html +68 -0
  79. package/dist/nodes/z2m-cover.js +41 -0
  80. package/dist/nodes/z2m-light.d.ts +3 -0
  81. package/dist/nodes/z2m-light.html +76 -0
  82. package/dist/nodes/z2m-light.js +51 -0
  83. package/examples/Cover control.json +28 -71
  84. package/examples/Home theatre activity.json +53 -19
  85. package/examples/House schedule.json +106 -24
  86. package/examples/Room lighting.json +57 -25
  87. package/examples/Shelly cover.json +5 -16
  88. package/examples/Shelly switch.json +197 -0
  89. package/package.json +12 -4
  90. package/dist/lib/room-schedule.d.ts +0 -8
  91. package/dist/lib/room-schedule.js +0 -41
@@ -0,0 +1,34 @@
1
+ import type { LightCommandMessage } from "./light-contract";
2
+ export type HeldSpec = Pick<LightCommandMessage, "state" | "brightnessPct" | "colorTempKelvin" | "transitionSeconds" | "extra">;
3
+ export interface HeldFrom {
4
+ control: "in-control" | "manual";
5
+ on: boolean;
6
+ brightnessPct?: number;
7
+ colorTempKelvin?: number;
8
+ }
9
+ export interface LightHold {
10
+ owners: Array<{
11
+ owner: string;
12
+ spec: HeldSpec | null;
13
+ }>;
14
+ before: HeldFrom | null;
15
+ }
16
+ export type Released = {
17
+ kind: "none";
18
+ } | {
19
+ kind: "dropped";
20
+ } | {
21
+ kind: "next";
22
+ owner: string;
23
+ spec: HeldSpec | null;
24
+ } | {
25
+ kind: "restore";
26
+ before: HeldFrom | null;
27
+ };
28
+ export declare function newHold(owner: string, before: HeldFrom): LightHold;
29
+ export declare function legacyHold(owner: string): LightHold;
30
+ export declare function savedHolds(saved: unknown, overridden: (target: string) => boolean): Record<string, LightHold>;
31
+ export declare function addOwner(hold: LightHold, owner: string): "top" | "beneath" | "pushed";
32
+ export declare function setOwnerSpec(hold: LightHold, owner: string, spec: HeldSpec): void;
33
+ export declare function heldOff(hold: LightHold | undefined): boolean;
34
+ export declare function releaseOwner(hold: LightHold, owner: string | null): Released;
@@ -0,0 +1,66 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.newHold = newHold;
4
+ exports.legacyHold = legacyHold;
5
+ exports.savedHolds = savedHolds;
6
+ exports.addOwner = addOwner;
7
+ exports.setOwnerSpec = setOwnerSpec;
8
+ exports.heldOff = heldOff;
9
+ exports.releaseOwner = releaseOwner;
10
+ function newHold(owner, before) {
11
+ return { owners: [{ owner, spec: null }], before };
12
+ }
13
+ // The hold of a light overridden under state saved before 6.1: its one
14
+ // owner, with nothing known of what it was before.
15
+ function legacyHold(owner) {
16
+ return { owners: [{ owner, spec: null }], before: null };
17
+ }
18
+ function savedHolds(saved, overridden) {
19
+ if (!saved || typeof saved !== "object")
20
+ return {};
21
+ return Object.fromEntries(Object.entries(saved)
22
+ .filter(([target, hold]) => overridden(target) && Array.isArray(hold?.owners) && hold.owners.length > 0)
23
+ .map(([target, hold]) => [target, {
24
+ owners: hold.owners.map((entry) => ({ owner: String(entry.owner), spec: entry.spec ? { ...entry.spec } : null })),
25
+ before: hold.before ? { ...hold.before } : null
26
+ }]));
27
+ }
28
+ // An owner asking for a light already held: "top" when it already holds it,
29
+ // "beneath" when it is under a newer owner (its request is kept for when
30
+ // the newer lets go, and the light is left as it is), "pushed" when it is new
31
+ // and now on top.
32
+ function addOwner(hold, owner) {
33
+ const index = hold.owners.findIndex((entry) => entry.owner === owner);
34
+ if (index === hold.owners.length - 1)
35
+ return "top";
36
+ if (index >= 0)
37
+ return "beneath";
38
+ hold.owners.push({ owner, spec: null });
39
+ return "pushed";
40
+ }
41
+ function setOwnerSpec(hold, owner, spec) {
42
+ const entry = hold.owners.find((held) => held.owner === owner);
43
+ if (entry)
44
+ entry.spec = { ...spec, ...(spec.extra ? { extra: { ...spec.extra } } : {}) };
45
+ }
46
+ // Whether the owner on top holds the light off.
47
+ function heldOff(hold) {
48
+ return hold?.owners.at(-1)?.spec?.state === "OFF";
49
+ }
50
+ // Lets go of one owner, or of every owner (null).
51
+ function releaseOwner(hold, owner) {
52
+ const index = owner === null ? 0 : hold.owners.findIndex((entry) => entry.owner === owner);
53
+ if (index < 0)
54
+ return { kind: "none" };
55
+ if (owner !== null && index < hold.owners.length - 1) {
56
+ hold.owners.splice(index, 1);
57
+ return { kind: "dropped" };
58
+ }
59
+ if (owner !== null && hold.owners.length > 1) {
60
+ hold.owners.pop();
61
+ const next = hold.owners.at(-1);
62
+ return { kind: "next", owner: next.owner, spec: next.spec ? { ...next.spec } : null };
63
+ }
64
+ return { kind: "restore", before: hold.before };
65
+ }
66
+ //# sourceMappingURL=room-holds.js.map
@@ -1,28 +1,14 @@
1
1
  import { type DiscoveryOwner } from "./ha-discovery";
2
- import type { DaySchedule } from "./circadian-schedule";
3
2
  export interface DiscoveryMessage {
4
3
  topic: string;
5
4
  payload: unknown;
6
5
  retain: boolean;
7
6
  }
8
7
  export declare function roomTopic(area: string, name: string): string;
9
- export declare function circadianStateTopic(area: string): string;
10
- export declare function circadianCommandTopic(area: string): string;
11
8
  export declare function roomAvailabilityTopic(area: string): string;
12
9
  export declare function roomAvailabilityMessage(area: string, online: boolean): DiscoveryMessage;
13
10
  export declare function roomControlTopic(area: string, targetId: string): string;
14
- export declare function sleepStateTopic(area: string): string;
15
- export declare function sleepCommandTopic(area: string): string;
16
- export type ScheduleTime = "wake-time" | "bed-time";
17
- export declare function scheduleStateTopic(area: string, time: ScheduleTime): string;
18
- export declare function scheduleCommandTopic(area: string, time: ScheduleTime): string;
19
- export declare function sleepStateMessage(area: string, sleeping: boolean): DiscoveryMessage;
20
- export declare function scheduleStateMessages(area: string, schedule: DaySchedule): DiscoveryMessage[];
21
- export declare function circadianStateMessage(area: string, enabled: boolean): DiscoveryMessage;
22
11
  export interface RoomDiscoveryConfig extends DiscoveryOwner {
23
12
  displayName: string;
24
- timesFrom?: "room" | "house";
25
13
  }
26
- export declare function roomTimeDiscoveryTopics(config: RoomDiscoveryConfig): string[];
27
14
  export declare function roomDiscovery(config: RoomDiscoveryConfig, targetIds: string[]): DiscoveryMessage[];
28
- export declare function roomLightingDiscovery(config: RoomDiscoveryConfig, circadianEnabled: boolean): DiscoveryMessage[];
@@ -1,36 +1,20 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.roomTopic = roomTopic;
4
- exports.circadianStateTopic = circadianStateTopic;
5
- exports.circadianCommandTopic = circadianCommandTopic;
6
4
  exports.roomAvailabilityTopic = roomAvailabilityTopic;
7
5
  exports.roomAvailabilityMessage = roomAvailabilityMessage;
8
6
  exports.roomControlTopic = roomControlTopic;
9
- exports.sleepStateTopic = sleepStateTopic;
10
- exports.sleepCommandTopic = sleepCommandTopic;
11
- exports.scheduleStateTopic = scheduleStateTopic;
12
- exports.scheduleCommandTopic = scheduleCommandTopic;
13
- exports.sleepStateMessage = sleepStateMessage;
14
- exports.scheduleStateMessages = scheduleStateMessages;
15
- exports.circadianStateMessage = circadianStateMessage;
16
- exports.roomTimeDiscoveryTopics = roomTimeDiscoveryTopics;
17
7
  exports.roomDiscovery = roomDiscovery;
18
- exports.roomLightingDiscovery = roomLightingDiscovery;
19
- // The room controller's Home Assistant view: a discovery entry for its
20
- // circadian switch and the retained state that entry reads. Home Assistant
21
- // may send ON/OFF on the command topic, which the room controller acts on.
8
+ // The room controller's Home Assistant view: its availability, and a sensor
9
+ // for each light's control state (in-control, overridden or manual). Its
10
+ // circadian, sleep, wake and bed entities moved to the circadian lighting
11
+ // policy in 6.0 (circadian-publishing.ts).
22
12
  const ha_discovery_1 = require("./ha-discovery");
23
13
  const topic_names_1 = require("./topic-names");
24
14
  // The one place a room topic is named: home/<area ID>/<name>.
25
15
  function roomTopic(area, name) {
26
16
  return (0, topic_names_1.homeTopic)(area, name);
27
17
  }
28
- function circadianStateTopic(area) {
29
- return roomTopic(area, "circadian/state");
30
- }
31
- function circadianCommandTopic(area) {
32
- return roomTopic(area, "circadian/set");
33
- }
34
18
  // The room's own availability, online or offline, for Home Assistant.
35
19
  function roomAvailabilityTopic(area) {
36
20
  return roomTopic(area, "lighting/availability");
@@ -42,126 +26,36 @@ function roomAvailabilityMessage(area, online) {
42
26
  function roomControlTopic(area, targetId) {
43
27
  return roomTopic(area, "lighting/" + targetId + "/control");
44
28
  }
45
- // Sleep, and the wake and bed times, each with a state topic and a set
46
- // topic Home Assistant commands.
47
- function sleepStateTopic(area) {
48
- return roomTopic(area, "sleep/state");
49
- }
50
- function sleepCommandTopic(area) {
51
- return roomTopic(area, "sleep/set");
52
- }
53
- function scheduleStateTopic(area, time) {
54
- return roomTopic(area, time + "/state");
55
- }
56
- function scheduleCommandTopic(area, time) {
57
- return roomTopic(area, time + "/set");
58
- }
59
- function sleepStateMessage(area, sleeping) {
60
- return { topic: sleepStateTopic(area), payload: sleeping ? "ON" : "OFF", retain: true };
61
- }
62
- // The times in use, "HH:MM:SS" as Home Assistant's time entity reads them.
63
- function scheduleStateMessages(area, schedule) {
64
- return [
65
- { topic: scheduleStateTopic(area, "wake-time"), payload: schedule.wakeTime + ":00", retain: true },
66
- { topic: scheduleStateTopic(area, "bed-time"), payload: schedule.bedTime + ":00", retain: true }
67
- ];
68
- }
69
- // The retained circadian state, as Home Assistant reads it.
70
- function circadianStateMessage(area, enabled) {
71
- return { topic: circadianStateTopic(area), payload: enabled ? "ON" : "OFF", retain: true };
72
- }
73
- const CIRCADIAN_ENTITY = "circadian_lighting";
74
29
  function roomDevice(config) {
75
30
  return {
76
31
  ...(0, ha_discovery_1.discoveryDevice)(config, "lighting"),
77
32
  name: config.displayName + " Lighting Controller",
78
33
  manufacturer: "Node-RED",
79
- model: "Composable circadian lighting"
34
+ model: "Room lighting controller"
80
35
  };
81
36
  }
82
- // One more entity on the room's device, with the IDs, availability and
83
- // origin every room entity carries.
84
- function roomEntity(config, component, entity, name, fields) {
85
- const ids = (0, ha_discovery_1.discoveryEntity)(config, component, entity);
86
- return {
87
- topic: ids.topic,
88
- payload: {
89
- name: config.displayName + " " + name,
90
- unique_id: ids.uniqueId,
91
- default_entity_id: ids.defaultEntityId,
92
- ...fields,
93
- availability_topic: roomAvailabilityTopic(config.area),
94
- device: roomDevice(config),
95
- origin: ha_discovery_1.DISCOVERY_ORIGIN
96
- },
97
- retain: true
98
- };
99
- }
100
- // Every discovery entry the room publishes: its circadian switch, a sleep
101
- // switch, its wake and bed times, and each light's control state.
102
- function roomTimeEntities(config) {
103
- const time = (which, entity, name, icon) => roomEntity(config, "time", entity, name, {
104
- icon,
105
- command_topic: scheduleCommandTopic(config.area, which),
106
- state_topic: scheduleStateTopic(config.area, which),
107
- entity_category: "config"
108
- });
109
- return [
110
- time("wake-time", "wake_time", "Wake time", "mdi:weather-sunset-up"),
111
- time("bed-time", "bed_time", "Bed time", "mdi:weather-night")
112
- ];
113
- }
114
- // The discovery topics of the room's own wake and bed time entities.
115
- function roomTimeDiscoveryTopics(config) {
116
- return roomTimeEntities(config).map(({ topic }) => topic);
117
- }
37
+ // Each light's control state, as a sensor on the room's device.
118
38
  function roomDiscovery(config, targetIds) {
119
- const { area } = config;
120
- return [
121
- roomLightingDiscovery(config, false)[0],
122
- roomEntity(config, "switch", "sleep", "Sleep", {
123
- icon: "mdi:sleep",
124
- command_topic: sleepCommandTopic(area),
125
- state_topic: sleepStateTopic(area),
126
- payload_on: "ON",
127
- payload_off: "OFF",
128
- optimistic: false
129
- }),
130
- ...(config.timesFrom === "house" ? [] : roomTimeEntities(config)),
131
- ...targetIds.map((id) => roomEntity(config, "sensor", id + "_control", id + " control", {
132
- icon: "mdi:account-lock-outline",
133
- state_topic: roomControlTopic(area, id),
134
- value_template: "{{ value_json.state }}",
135
- json_attributes_topic: roomControlTopic(area, id),
136
- json_attributes_template: "{{ {'owner': value_json.owner} | tojson }}",
137
- entity_category: "diagnostic"
138
- }))
139
- ];
140
- }
141
- // The discovery entry for the room's circadian switch, then its state.
142
- function roomLightingDiscovery(config, circadianEnabled) {
143
- const { area, displayName } = config;
144
- const ids = (0, ha_discovery_1.discoveryEntity)(config, "switch", CIRCADIAN_ENTITY);
145
- return [
146
- {
39
+ return targetIds.map((id) => {
40
+ const ids = (0, ha_discovery_1.discoveryEntity)(config, "sensor", id + "_control");
41
+ return {
147
42
  topic: ids.topic,
148
43
  payload: {
149
- name: displayName + " Circadian Lighting",
44
+ name: config.displayName + " " + id + " control",
150
45
  unique_id: ids.uniqueId,
151
46
  default_entity_id: ids.defaultEntityId,
152
- icon: "mdi:clock-time-seven-outline",
153
- command_topic: circadianCommandTopic(area),
154
- state_topic: circadianStateTopic(area),
155
- availability_topic: roomAvailabilityTopic(area),
156
- payload_on: "ON",
157
- payload_off: "OFF",
158
- optimistic: false,
47
+ icon: "mdi:account-lock-outline",
48
+ state_topic: roomControlTopic(config.area, id),
49
+ value_template: "{{ value_json.state }}",
50
+ json_attributes_topic: roomControlTopic(config.area, id),
51
+ json_attributes_template: "{{ {'owner': value_json.owner, 'available': value_json.available} | tojson }}",
52
+ entity_category: "diagnostic",
53
+ availability_topic: roomAvailabilityTopic(config.area),
159
54
  device: roomDevice(config),
160
55
  origin: ha_discovery_1.DISCOVERY_ORIGIN
161
56
  },
162
57
  retain: true
163
- },
164
- circadianStateMessage(area, circadianEnabled)
165
- ];
58
+ };
59
+ });
166
60
  }
167
61
  //# sourceMappingURL=room-lighting-discovery.js.map
@@ -1,38 +1,34 @@
1
- import type { TraceEntry } from "./bounded-trace";
2
1
  import { type IdScheme } from "./ha-discovery";
3
2
  import { type RoomConfig, type RoomMessage, type RoomOutput, type RoomState } from "./room-controller";
4
3
  type PublishSettings = {
5
4
  area: string;
6
5
  publishState?: boolean;
7
6
  discovery?: boolean;
8
- timesFrom?: TimesFrom;
9
7
  };
10
- export type TimesFrom = "room" | "house";
11
8
  export interface RoomPublishingConfig {
12
9
  nodeId?: string;
13
10
  idScheme?: IdScheme;
14
- timesFrom?: TimesFrom;
15
11
  discoveryPrefix?: string;
16
12
  publishState?: boolean;
17
13
  discovery?: boolean;
18
14
  }
19
15
  type PublishingRoomConfig = RoomConfig & RoomPublishingConfig;
20
16
  export declare function roomRetainedTopics(config: PublishingRoomConfig): string[];
17
+ export declare function withoutCircadianState(recorded: readonly string[]): string[];
21
18
  export declare function roomPublishesState(config: PublishSettings): boolean;
22
19
  export declare function roomPublishesDiscovery(config: PublishSettings): boolean;
23
- export declare function roomStartingOutputs(config: PublishSettings): RoomOutput[];
20
+ export declare function roomStartingOutputs(config: PublishingRoomConfig): RoomOutput[];
24
21
  export declare function roomStoppingOutputs(config: PublishSettings): RoomOutput[];
25
- export declare function roomInbound(msg: RoomMessage, config: PublishSettings): {
26
- msg: RoomMessage;
27
- traces: TraceEntry[];
28
- };
29
22
  export declare function publishRoom(saved: Partial<RoomState> | undefined, msg: RoomMessage, config: PublishingRoomConfig, now?: Date | string | number): {
30
23
  state: RoomState;
24
+ commands: import("./light-contract").LightRoomMessage[];
25
+ traces: import("./bounded-trace").TraceEntry<Record<string, unknown>>[];
31
26
  controlChanged: string[];
32
27
  status: {
33
- circadianEnabled: boolean;
34
- sleeping: boolean;
28
+ styled: boolean;
29
+ styleSource: string | null;
35
30
  onCount: number;
31
+ offlineCount: number;
36
32
  manualCount: number;
37
33
  overriddenCount: number;
38
34
  control: {
@@ -40,6 +36,5 @@ export declare function publishRoom(saved: Partial<RoomState> | undefined, msg:
40
36
  };
41
37
  };
42
38
  outputs: RoomOutput[];
43
- traces: TraceEntry<Record<string, unknown>>[];
44
39
  };
45
40
  export {};
@@ -1,19 +1,22 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.roomRetainedTopics = roomRetainedTopics;
4
+ exports.withoutCircadianState = withoutCircadianState;
4
5
  exports.roomPublishesState = roomPublishesState;
5
6
  exports.roomPublishesDiscovery = roomPublishesDiscovery;
6
7
  exports.roomStartingOutputs = roomStartingOutputs;
7
8
  exports.roomStoppingOutputs = roomStoppingOutputs;
8
- exports.roomInbound = roomInbound;
9
9
  exports.publishRoom = publishRoom;
10
+ // The room's MQTT and Home Assistant side, as the theatre state contract is
11
+ // for the theatre: it turns the room controller's decisions into the
12
+ // retained topics other systems read. The room controller itself only
13
+ // decides about lights. Its circadian, sleep, wake and bed entities and set
14
+ // topics moved to the circadian lighting policy in 6.0.
15
+ const circadian_publishing_1 = require("./circadian-publishing");
10
16
  const ha_discovery_1 = require("./ha-discovery");
11
17
  const room_controller_1 = require("./room-controller");
12
18
  const room_lighting_discovery_1 = require("./room-lighting-discovery");
13
19
  const topic_names_1 = require("./topic-names");
14
- function ownTimes(config) {
15
- return config.timesFrom !== "house";
16
- }
17
20
  function targetIds(config) {
18
21
  return config.targets.map((target) => target.id);
19
22
  }
@@ -21,153 +24,91 @@ function discoveryConfig(config) {
21
24
  return { ...config, discoveryPrefix: config.discoveryPrefix ?? ha_discovery_1.DEFAULT_DISCOVERY_PREFIX };
22
25
  }
23
26
  // Every retained topic the room publishes, for clearing those it no longer
24
- // does after a change. The discovery entry counts whenever the room has an
25
- // area, as it is published or cleared at each start.
27
+ // does after a change. The discovery entries count whenever the room has an
28
+ // area, as they are published or cleared at each start.
26
29
  function roomRetainedTopics(config) {
27
30
  const topics = (0, topic_names_1.areaId)(config.area) ? (0, room_lighting_discovery_1.roomDiscovery)(discoveryConfig(config), targetIds(config)).map(({ topic }) => topic) : [];
28
31
  if (roomPublishesState(config)) {
29
- topics.push((0, room_lighting_discovery_1.roomAvailabilityTopic)(config.area), (0, room_lighting_discovery_1.circadianStateTopic)(config.area), (0, room_lighting_discovery_1.sleepStateTopic)(config.area));
30
- if (ownTimes(config))
31
- topics.push((0, room_lighting_discovery_1.scheduleStateTopic)(config.area, "wake-time"), (0, room_lighting_discovery_1.scheduleStateTopic)(config.area, "bed-time"));
32
+ topics.push((0, room_lighting_discovery_1.roomAvailabilityTopic)(config.area));
32
33
  topics.push(...config.targets.map((target) => (0, room_lighting_discovery_1.roomControlTopic)(config.area, target.id)));
33
34
  }
34
35
  return topics;
35
36
  }
36
- // Whether the room publishes state under its area, and its circadian
37
- // switch's discovery, which reads that state.
37
+ // The state topics of the circadian lighting policy, which a room published
38
+ // before 6.0 and may still have in its record of what it published. They are
39
+ // never the room's to clear now: a circadian node with the same area
40
+ // publishes them.
41
+ const CIRCADIAN_STATE = /^home\/[^/]+\/(?:circadian|sleep|wake-time|bed-time)\/state$/;
42
+ function withoutCircadianState(recorded) {
43
+ return recorded.filter((topic) => !CIRCADIAN_STATE.test(topic));
44
+ }
45
+ // Whether the room publishes its control states and availability under its
46
+ // area, and their discovery.
38
47
  function roomPublishesState(config) {
39
48
  return config.area !== "" && config.publishState !== false;
40
49
  }
41
50
  function roomPublishesDiscovery(config) {
42
51
  return roomPublishesState(config) && config.discovery !== false;
43
52
  }
44
- // What the room publishes as it starts and as it stops, when it publishes at
45
- // all, so Home Assistant shows its entities as available only while it runs.
46
- function roomStartingOutputs(config) {
47
- return roomPublishesState(config) ? [(0, room_lighting_discovery_1.roomAvailabilityMessage)(config.area, true)] : [];
53
+ // The circadian, sleep, wake and bed entities the room published before
54
+ // 6.0, cleared at every start: the circadian lighting policy publishes its
55
+ // own, and no 6.0 node publishes these, so clearing them is always safe.
56
+ function oldCircadianClears(config) {
57
+ // A room that publishes nothing cleared them itself before 6.0, when its
58
+ // publishing was switched off.
59
+ if ((0, topic_names_1.areaId)(config.area) === "" || !roomPublishesState(config))
60
+ return [];
61
+ const prefix = discoveryConfig(config).discoveryPrefix;
62
+ return (0, circadian_publishing_1.roomCircadianDiscoveryTopics)({ area: config.area, discoveryPrefix: prefix }, config.idScheme === "node" ? "node" : "area", config.nodeId)
63
+ .map((topic) => ({ topic, payload: "", retain: true }));
48
64
  }
49
- function roomStoppingOutputs(config) {
50
- return roomPublishesState(config) ? [(0, room_lighting_discovery_1.roomAvailabilityMessage)(config.area, false)] : [];
65
+ // Each light's control sensor, or with discovery switched off, empty
66
+ // retained entries that remove them.
67
+ function roomDiscoveryOutputs(config) {
68
+ if ((0, topic_names_1.areaId)(config.area) === "")
69
+ return [];
70
+ return (0, room_lighting_discovery_1.roomDiscovery)(discoveryConfig(config), targetIds(config))
71
+ .map((entry) => (roomPublishesDiscovery(config) ? entry : { topic: entry.topic, payload: "", retain: true }));
51
72
  }
52
- function onOff(payload) {
53
- const requested = typeof payload === "string" ? payload.trim().toUpperCase() : "";
54
- return requested === "ON" ? true : requested === "OFF" ? false : null;
73
+ // What the room publishes as the flows start and as it stops: its old
74
+ // circadian entries cleared, then with discovery on, its entries, and when it
75
+ // publishes at all, its availability, so Home Assistant shows its entities as
76
+ // available only while it runs. Removing its entries when discovery is off
77
+ // waits for the startup message: under the area scheme, another room could
78
+ // publish them.
79
+ function roomStartingOutputs(config) {
80
+ const entries = roomPublishesDiscovery(config) ? roomDiscoveryOutputs(config) : [];
81
+ return [...oldCircadianClears(config), ...entries, ...(roomPublishesState(config) ? [(0, room_lighting_discovery_1.roomAvailabilityMessage)(config.area, true)] : [])];
55
82
  }
56
- // The set topics the room reads, what each payload means as a room event,
57
- // and the trace reason when it means nothing.
58
- function setTopics(area, ownTimesToo) {
59
- const topics = [
60
- { name: "circadian-set", topic: (0, room_lighting_discovery_1.circadianCommandTopic)(area), invalid: "not-on-or-off",
61
- event: (payload) => {
62
- const enabled = onOff(payload);
63
- return enabled === null ? null : { kind: "circadian", enabled };
64
- } },
65
- { name: "sleep-set", topic: (0, room_lighting_discovery_1.sleepCommandTopic)(area), invalid: "not-on-or-off",
66
- event: (payload) => {
67
- const enabled = onOff(payload);
68
- return enabled === null ? null : { kind: "sleep", enabled };
69
- } },
70
- { name: "wake-time-set", topic: (0, room_lighting_discovery_1.scheduleCommandTopic)(area, "wake-time"), invalid: "not-a-time",
71
- event: (payload) => {
72
- const wakeTime = (0, ha_discovery_1.timeEntityValue)(payload);
73
- return wakeTime === null ? null : { kind: "schedule", wakeTime };
74
- } },
75
- { name: "bed-time-set", topic: (0, room_lighting_discovery_1.scheduleCommandTopic)(area, "bed-time"), invalid: "not-a-time",
76
- event: (payload) => {
77
- const bedTime = (0, ha_discovery_1.timeEntityValue)(payload);
78
- return bedTime === null ? null : { kind: "schedule", bedTime };
79
- } }
80
- ];
81
- // Following the house, the room's own wake and bed set topics are not read.
82
- return ownTimesToo ? topics : topics.filter(({ name }) => name !== "wake-time-set" && name !== "bed-time-set");
83
+ // At the startup message: as at the flows start, and with discovery off,
84
+ // its entries removed.
85
+ function roomStartupOutputs(config) {
86
+ return [...oldCircadianClears(config), ...roomDiscoveryOutputs(config), ...(roomPublishesState(config) ? [(0, room_lighting_discovery_1.roomAvailabilityMessage)(config.area, true)] : [])];
83
87
  }
84
- // MQTT the room reads, as room events: the set topics of its Home Assistant
85
- // entities (circadian lighting and sleep, ON or OFF; wake and bed times).
86
- // Read only while the room publishes, as the entities are only discovered
87
- // then; a bare MQTT message, or one a flow tagged as observed, is accepted.
88
- // Anything else passes through unchanged.
89
- function roomInbound(msg, config) {
90
- const event = msg.lighting || {};
91
- const set = roomPublishesState(config) && (!event.kind || event.kind === "observed" || event.kind === "command-observed")
92
- ? setTopics(config.area, ownTimes(config)).find(({ topic }) => topic === msg.topic)
93
- : undefined;
94
- if (!set)
95
- return { msg, traces: [] };
96
- if (msg.retain === true || event.retained === true) {
97
- return {
98
- msg: { ...msg, lighting: { kind: "ignored" } },
99
- traces: [{ event: set.name, outcome: "ignored", reason: "retained-message", details: {} }]
100
- };
101
- }
102
- const mapped = set.event(msg.payload);
103
- if (mapped)
104
- return { msg: { ...msg, lighting: mapped }, traces: [] };
105
- return {
106
- msg: { ...msg, lighting: { kind: "ignored" } },
107
- traces: [{
108
- event: set.name,
109
- outcome: "ignored",
110
- reason: set.invalid,
111
- details: { payload: typeof msg.payload === "string" ? msg.payload : null }
112
- }]
113
- };
88
+ function roomStoppingOutputs(config) {
89
+ return roomPublishesState(config) ? [(0, room_lighting_discovery_1.roomAvailabilityMessage)(config.area, false)] : [];
114
90
  }
115
91
  // A light's retained control state: in-control, overridden or manual.
116
92
  function controlMessage(config, state, targetId) {
117
93
  const entry = state.control[targetId];
94
+ const available = state.available[targetId];
118
95
  return {
119
96
  topic: (0, room_lighting_discovery_1.roomControlTopic)(config.area, targetId),
120
- payload: { state: entry?.state ?? "in-control", owner: entry?.state === "overridden" ? entry.owner : null },
97
+ // Its availability once its adapter has reported it: shown, never acted on.
98
+ payload: { state: entry?.state ?? "in-control", owner: entry?.state === "overridden" ? entry.owner : null,
99
+ ...(available !== undefined ? { available } : {}) },
121
100
  retain: true
122
101
  };
123
102
  }
124
- // The room's transition with its MQTT side: incoming MQTT becomes room
125
- // events, and the controller's decisions are published. At startup, the
126
- // discovery entry (or its removal), availability and circadian state come
127
- // first and every light's control state last; a circadian change publishes
128
- // the circadian state before the lights are restyled; any light whose control
129
- // state changed has it published after the commands.
103
+ // The room's transition with its MQTT side. At startup, the discovery
104
+ // entries (or their removal) and availability come first and every light's
105
+ // control state last; any light whose control state changed has it
106
+ // published after the commands.
130
107
  function publishRoom(saved, msg, config, now) {
131
- const inbound = roomInbound(msg, config);
132
- const result = (0, room_controller_1.transitionRoom)(saved, inbound.msg, config, now);
133
- const kind = inbound.msg.lighting?.kind;
108
+ const result = (0, room_controller_1.transitionRoom)(saved, msg, config, now);
109
+ const kind = msg.lighting?.kind;
134
110
  const publishes = roomPublishesState(config);
135
- const before = [];
136
- if (kind === "startup") {
137
- if ((0, topic_names_1.areaId)(config.area) !== "") {
138
- // Discovery switched off: empty retained entries remove the entities
139
- // from Home Assistant.
140
- for (const entry of (0, room_lighting_discovery_1.roomDiscovery)(discoveryConfig(config), targetIds(config))) {
141
- before.push(roomPublishesDiscovery(config) ? entry : { topic: entry.topic, payload: "", retain: true });
142
- }
143
- }
144
- if (publishes) {
145
- before.push((0, room_lighting_discovery_1.roomAvailabilityMessage)(config.area, true));
146
- before.push((0, room_lighting_discovery_1.circadianStateMessage)(config.area, result.state.circadianEnabled));
147
- before.push((0, room_lighting_discovery_1.sleepStateMessage)(config.area, result.state.sleeping));
148
- if (ownTimes(config))
149
- before.push(...(0, room_lighting_discovery_1.scheduleStateMessages)(config.area, result.state.schedule));
150
- }
151
- // Following the house, the room's own time entities and their state are
152
- // cleared, every start, whatever was published before.
153
- if (!ownTimes(config) && (0, topic_names_1.areaId)(config.area) !== "") {
154
- const ownTimeTopics = [
155
- ...(0, room_lighting_discovery_1.roomTimeDiscoveryTopics)(discoveryConfig(config)),
156
- (0, room_lighting_discovery_1.scheduleStateTopic)(config.area, "wake-time"),
157
- (0, room_lighting_discovery_1.scheduleStateTopic)(config.area, "bed-time")
158
- ];
159
- before.push(...ownTimeTopics.map((topic) => ({ topic, payload: "", retain: true })));
160
- }
161
- }
162
- else if (kind === "circadian" && publishes) {
163
- before.push((0, room_lighting_discovery_1.circadianStateMessage)(config.area, result.state.circadianEnabled));
164
- }
165
- else if (kind === "sleep" && publishes) {
166
- before.push((0, room_lighting_discovery_1.sleepStateMessage)(config.area, result.state.sleeping));
167
- }
168
- else if (kind === "schedule" && publishes && ownTimes(config)) {
169
- before.push(...(0, room_lighting_discovery_1.scheduleStateMessages)(config.area, result.state.schedule));
170
- }
111
+ const before = kind === "startup" ? roomStartupOutputs(config) : [];
171
112
  const controls = publishes
172
113
  ? config.targets
173
114
  .filter((target) => kind === "startup" || result.controlChanged.includes(target.id))
@@ -175,8 +116,7 @@ function publishRoom(saved, msg, config, now) {
175
116
  : [];
176
117
  return {
177
118
  ...result,
178
- outputs: [...before, ...result.outputs, ...controls],
179
- traces: [...inbound.traces, ...result.traces]
119
+ outputs: [...before, ...result.outputs, ...controls]
180
120
  };
181
121
  }
182
122
  //# sourceMappingURL=room-publishing.js.map
@@ -1,5 +1,7 @@
1
1
  import type { TraceEntry } from "./bounded-trace";
2
2
  import type { CoverCommand, CoverEvent } from "./cover-controller";
3
+ import { type MqttPublication } from "./mqtt-message";
4
+ export type { MqttPublication } from "./mqtt-message";
3
5
  export interface ShellyCoverConfig {
4
6
  topicPrefix: string;
5
7
  channel: number;
@@ -17,10 +19,6 @@ export interface ShellyCoverMessage {
17
19
  payload?: unknown;
18
20
  cover_command?: CoverCommand;
19
21
  }
20
- export interface MqttPublication {
21
- topic: string;
22
- payload: string;
23
- }
24
22
  export declare function normaliseShellyCoverConfig(config: {
25
23
  topicPrefix?: unknown;
26
24
  channel?: unknown;