@glasshome/sync-layer 0.3.2 → 0.5.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.
- package/dist/connection/auth.d.ts +5 -5
- package/dist/connection/types.d.ts +0 -10
- package/dist/index.js +1353 -29
- package/dist/worker/worker-main.js +1 -1
- package/package.json +1 -6
- package/dist/demo-data-3LO-p1RV.js +0 -1205
- package/dist/testing/fixtures.d.ts +0 -161
- package/dist/testing/helpers.d.ts +0 -160
- package/dist/testing/index.d.ts +0 -13
- package/dist/testing/index.js +0 -2067
- package/dist/testing/mock-behaviors/base.d.ts +0 -73
- package/dist/testing/mock-behaviors/climate.d.ts +0 -22
- package/dist/testing/mock-behaviors/cover.d.ts +0 -22
- package/dist/testing/mock-behaviors/index.d.ts +0 -11
- package/dist/testing/mock-behaviors/light.d.ts +0 -20
- package/dist/testing/mock-behaviors/lock.d.ts +0 -20
- package/dist/testing/mock-behaviors/media_player.d.ts +0 -42
- package/dist/testing/mock-behaviors/registry.d.ts +0 -55
- package/dist/testing/mock-behaviors/switch.d.ts +0 -17
- package/dist/testing/mock.d.ts +0 -196
- package/dist/testing/types.d.ts +0 -87
- /package/dist/{worker-main-DTbzEKor.js → worker-main-Cf-k4f2N.js} +0 -0
|
@@ -1,1205 +0,0 @@
|
|
|
1
|
-
import { createStore, reconcile } from "solid-js/store";
|
|
2
|
-
|
|
3
|
-
//#region src/core/store.ts
|
|
4
|
-
const initialState = {
|
|
5
|
-
conn: null,
|
|
6
|
-
connectionState: "disconnected",
|
|
7
|
-
connectionError: null,
|
|
8
|
-
hassUrl: null,
|
|
9
|
-
mediaProxyBase: null,
|
|
10
|
-
config: null,
|
|
11
|
-
entities: {},
|
|
12
|
-
entityRegistry: {},
|
|
13
|
-
devices: {},
|
|
14
|
-
areas: {},
|
|
15
|
-
floors: {},
|
|
16
|
-
labels: {},
|
|
17
|
-
statistics: {},
|
|
18
|
-
statisticsMetadata: {},
|
|
19
|
-
history: {},
|
|
20
|
-
forecasts: {},
|
|
21
|
-
streams: {}
|
|
22
|
-
};
|
|
23
|
-
if (globalThis.__GH_SYNC_LAYER_STORE__) console.error("[@glasshome/sync-layer] DUPLICATE store module instantiated. This means @glasshome/sync-layer was bundled into >1 chunk. Reads and writes will hit different singletons. Check vendor build externals and relative-vs-bare imports across entry points.");
|
|
24
|
-
globalThis.__GH_SYNC_LAYER_STORE__ = true;
|
|
25
|
-
const storeTuple = createStore(initialState);
|
|
26
|
-
const state = storeTuple[0];
|
|
27
|
-
const setState = storeTuple[1];
|
|
28
|
-
/**
|
|
29
|
-
* Reset store to initial state
|
|
30
|
-
*/
|
|
31
|
-
function resetStore() {
|
|
32
|
-
setState(reconcile(initialState));
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
//#endregion
|
|
36
|
-
//#region src/demo/energy-sim.ts
|
|
37
|
-
const SOLAR_PEAK_W = 5200;
|
|
38
|
-
const SUNRISE_HOUR = 6.5;
|
|
39
|
-
const SUNSET_HOUR = 20.5;
|
|
40
|
-
const ALWAYS_ON_W = 285;
|
|
41
|
-
const FRIDGE_W = 95;
|
|
42
|
-
const FRIDGE_CYCLE_MIN = 45;
|
|
43
|
-
const FRIDGE_ON_MIN = 18;
|
|
44
|
-
const BATTERY_CAPACITY_WH = 1e4;
|
|
45
|
-
const BATTERY_MIN_SOC = 10;
|
|
46
|
-
const BATTERY_MAX_SOC = 100;
|
|
47
|
-
const BATTERY_POWER_CAP_W = 3e3;
|
|
48
|
-
const MIDNIGHT_SOC = 55;
|
|
49
|
-
const SOC_STEP_MIN = 5;
|
|
50
|
-
/** Integer hash -> uint32, used to seed mulberry32 deterministically. */
|
|
51
|
-
function hashInt(n) {
|
|
52
|
-
let h = n | 0;
|
|
53
|
-
h = Math.imul(h ^ h >>> 16, 73244475);
|
|
54
|
-
h = Math.imul(h ^ h >>> 16, 73244475);
|
|
55
|
-
h = (h ^ h >>> 16) >>> 0;
|
|
56
|
-
return h;
|
|
57
|
-
}
|
|
58
|
-
/** Deterministic [0,1) generator seeded by an integer. */
|
|
59
|
-
function mulberry32(seed) {
|
|
60
|
-
let a = seed >>> 0;
|
|
61
|
-
return () => {
|
|
62
|
-
a |= 0;
|
|
63
|
-
a = a + 1831565813 | 0;
|
|
64
|
-
let t = Math.imul(a ^ a >>> 15, 1 | a);
|
|
65
|
-
t = t + Math.imul(t ^ t >>> 7, 61 | t) ^ t;
|
|
66
|
-
return ((t ^ t >>> 14) >>> 0) / 4294967296;
|
|
67
|
-
};
|
|
68
|
-
}
|
|
69
|
-
function localTime(timestampMs) {
|
|
70
|
-
const d = new Date(timestampMs);
|
|
71
|
-
const hours = d.getHours();
|
|
72
|
-
const minutes = d.getMinutes();
|
|
73
|
-
const seconds = d.getSeconds();
|
|
74
|
-
const hourFrac = hours + minutes / 60 + seconds / 3600;
|
|
75
|
-
const minuteOfDay = hours * 60 + minutes + seconds / 60;
|
|
76
|
-
const offsetMs = d.getTimezoneOffset() * 6e4;
|
|
77
|
-
const dayKey = Math.floor((timestampMs - offsetMs) / 864e5);
|
|
78
|
-
return {
|
|
79
|
-
hourFrac,
|
|
80
|
-
minuteOfDay,
|
|
81
|
-
dayOfWeek: d.getDay(),
|
|
82
|
-
dayKey
|
|
83
|
-
};
|
|
84
|
-
}
|
|
85
|
-
function localMidnightMs(timestampMs, lt) {
|
|
86
|
-
return timestampMs - lt.minuteOfDay * 6e4;
|
|
87
|
-
}
|
|
88
|
-
/** Smooth multiplicative cloud factor in [0.55, 1.0], seeded per day. */
|
|
89
|
-
function cloudFactor(lt) {
|
|
90
|
-
const rand = mulberry32(hashInt(lt.dayKey * 2654435761));
|
|
91
|
-
const phase1 = rand() * Math.PI * 2;
|
|
92
|
-
const phase2 = rand() * Math.PI * 2;
|
|
93
|
-
const phase3 = rand() * Math.PI * 2;
|
|
94
|
-
const m = lt.minuteOfDay;
|
|
95
|
-
const wave = .16 * Math.sin(m / 37 * Math.PI * 2 + phase1) + .14 * Math.sin(m / 73 * Math.PI * 2 + phase2) + .05 * Math.sin(m / 11 * Math.PI * 2 + phase3);
|
|
96
|
-
const f = .85 + wave;
|
|
97
|
-
return Math.max(.55, Math.min(1, f));
|
|
98
|
-
}
|
|
99
|
-
function solarPower(lt) {
|
|
100
|
-
if (lt.hourFrac <= SUNRISE_HOUR || lt.hourFrac >= SUNSET_HOUR) return 0;
|
|
101
|
-
const frac = (lt.hourFrac - SUNRISE_HOUR) / (SUNSET_HOUR - SUNRISE_HOUR);
|
|
102
|
-
const shape = Math.sin(frac * Math.PI) ** 1.3;
|
|
103
|
-
return SOLAR_PEAK_W * shape * cloudFactor(lt);
|
|
104
|
-
}
|
|
105
|
-
/** Coarse solar elevation in degrees for sun.sun attributes. */
|
|
106
|
-
function sunElevation(lt) {
|
|
107
|
-
if (lt.hourFrac <= SUNRISE_HOUR || lt.hourFrac >= SUNSET_HOUR) return -12;
|
|
108
|
-
const frac = (lt.hourFrac - SUNRISE_HOUR) / (SUNSET_HOUR - SUNRISE_HOUR);
|
|
109
|
-
return 60 * Math.sin(frac * Math.PI);
|
|
110
|
-
}
|
|
111
|
-
function fridgePower(lt) {
|
|
112
|
-
const rand = mulberry32(hashInt(lt.dayKey * 40503 + 7));
|
|
113
|
-
const phase = rand() * FRIDGE_CYCLE_MIN;
|
|
114
|
-
const pos = (lt.minuteOfDay + phase) % FRIDGE_CYCLE_MIN;
|
|
115
|
-
return pos < FRIDGE_ON_MIN ? FRIDGE_W : 0;
|
|
116
|
-
}
|
|
117
|
-
/** A square wave: `power` while inside [start, start+durationMin). */
|
|
118
|
-
function windowPower(minuteOfDay, startMin, durationMin, power) {
|
|
119
|
-
return minuteOfDay >= startMin && minuteOfDay < startMin + durationMin ? power : 0;
|
|
120
|
-
}
|
|
121
|
-
function kettlePower(lt) {
|
|
122
|
-
return windowPower(lt.minuteOfDay, 430, 4, 2e3) + windowPower(lt.minuteOfDay, 1e3, 4, 2e3);
|
|
123
|
-
}
|
|
124
|
-
function ovenPower(lt) {
|
|
125
|
-
const start = 1125;
|
|
126
|
-
if (lt.minuteOfDay < start || lt.minuteOfDay >= start + 40) return 0;
|
|
127
|
-
const into = lt.minuteOfDay - start;
|
|
128
|
-
const cyclePos = into % 6.5;
|
|
129
|
-
return cyclePos < 4 ? 2200 : 0;
|
|
130
|
-
}
|
|
131
|
-
function dishwasherPower(lt) {
|
|
132
|
-
const start = 1270;
|
|
133
|
-
if (lt.minuteOfDay < start || lt.minuteOfDay >= start + 65) return 0;
|
|
134
|
-
const into = lt.minuteOfDay - start;
|
|
135
|
-
if (into < 12) return 1900;
|
|
136
|
-
if (into >= 57) return 1900;
|
|
137
|
-
return 120;
|
|
138
|
-
}
|
|
139
|
-
function washerPower(lt) {
|
|
140
|
-
if (![
|
|
141
|
-
1,
|
|
142
|
-
4,
|
|
143
|
-
6
|
|
144
|
-
].includes(lt.dayOfWeek)) return 0;
|
|
145
|
-
const start = 630;
|
|
146
|
-
if (lt.minuteOfDay < start || lt.minuteOfDay >= start + 50) return 0;
|
|
147
|
-
const into = lt.minuteOfDay - start;
|
|
148
|
-
return into < 14 ? 2100 : 250;
|
|
149
|
-
}
|
|
150
|
-
function evChargerPower(lt) {
|
|
151
|
-
if (![
|
|
152
|
-
2,
|
|
153
|
-
4,
|
|
154
|
-
0
|
|
155
|
-
].includes(lt.dayOfWeek)) return 0;
|
|
156
|
-
const start = 1100;
|
|
157
|
-
const stop = 1300;
|
|
158
|
-
if (lt.minuteOfDay < start || lt.minuteOfDay >= stop) return 0;
|
|
159
|
-
const into = lt.minuteOfDay - start;
|
|
160
|
-
const ramp = into < 2 ? into / 2 : 1;
|
|
161
|
-
return 7200 * ramp;
|
|
162
|
-
}
|
|
163
|
-
const HEAT_PUMP_PEAK_W = 3200;
|
|
164
|
-
/**
|
|
165
|
-
* A smooth heat-pump load: a slow morning/evening envelope carrying a
|
|
166
|
-
* few-minute shimmer. Its peaks push the home deficit past the battery's power
|
|
167
|
-
* cap, so grid import (and the running cost) visibly rise and fall on live
|
|
168
|
-
* ticks instead of sitting at zero. Gated out of the deep-night window so the
|
|
169
|
-
* quiet 02:00-03:00 baseline is unchanged.
|
|
170
|
-
*/
|
|
171
|
-
function heatPumpPower(lt) {
|
|
172
|
-
const h = lt.hourFrac;
|
|
173
|
-
let envelope = 0;
|
|
174
|
-
if (h >= 6 && h <= 9) envelope = Math.sin((h - 6) / 3 * Math.PI);
|
|
175
|
-
else if (h >= 16 && h <= 23) envelope = Math.sin((h - 16) / 7 * Math.PI);
|
|
176
|
-
else return 0;
|
|
177
|
-
const shimmer = .7 + .3 * Math.sin(lt.minuteOfDay * Math.PI * 2);
|
|
178
|
-
return HEAT_PUMP_PEAK_W * envelope * shimmer;
|
|
179
|
-
}
|
|
180
|
-
/** Smooth occupancy envelope: extra load mornings and evenings. */
|
|
181
|
-
function occupancyEnvelope(lt) {
|
|
182
|
-
const h = lt.hourFrac;
|
|
183
|
-
let e = 0;
|
|
184
|
-
if (h >= 7 && h <= 9) e += 80 + 270 * Math.sin((h - 7) / 2 * Math.PI);
|
|
185
|
-
if (h >= 17.5 && h <= 23) e += 80 + 270 * Math.sin((h - 17.5) / 5.5 * Math.PI);
|
|
186
|
-
return e;
|
|
187
|
-
}
|
|
188
|
-
/** Low-amplitude smooth noise multiplier, ±4%, seeded per day. */
|
|
189
|
-
function homeNoise(lt) {
|
|
190
|
-
const rand = mulberry32(hashInt(lt.dayKey * 22699 + 13));
|
|
191
|
-
const phase = rand() * Math.PI * 2;
|
|
192
|
-
return 1 + .04 * Math.sin(lt.minuteOfDay / 23 * Math.PI * 2 + phase);
|
|
193
|
-
}
|
|
194
|
-
const socCache = /* @__PURE__ */ new Map();
|
|
195
|
-
/** Net surplus (solar - home) at a given local-time sample, in watts. */
|
|
196
|
-
function netSurplusAt(timestampMs) {
|
|
197
|
-
const lt = localTime(timestampMs);
|
|
198
|
-
const solar = solarPower(lt);
|
|
199
|
-
const home = homePower(lt);
|
|
200
|
-
return solar - home;
|
|
201
|
-
}
|
|
202
|
-
/**
|
|
203
|
-
* Battery SOC (%) at every {@link SOC_STEP_MIN}-minute step from local
|
|
204
|
-
* midnight. Returns step `i` = SOC after integrating `i` steps. Index 0 is
|
|
205
|
-
* the midnight SOC. Memoized per local day (pure computation).
|
|
206
|
-
*/
|
|
207
|
-
function socPrefix(dayKey, midnightMs) {
|
|
208
|
-
const cached = socCache.get(dayKey);
|
|
209
|
-
if (cached) return cached;
|
|
210
|
-
const steps = Math.ceil(24 * 60 / SOC_STEP_MIN);
|
|
211
|
-
const arr = new Float64Array(steps + 1);
|
|
212
|
-
arr[0] = MIDNIGHT_SOC;
|
|
213
|
-
const stepMs = SOC_STEP_MIN * 6e4;
|
|
214
|
-
const stepHours = SOC_STEP_MIN / 60;
|
|
215
|
-
for (let i = 0; i < steps; i++) {
|
|
216
|
-
const soc = arr[i] ?? MIDNIGHT_SOC;
|
|
217
|
-
const sampleMs = midnightMs + i * stepMs;
|
|
218
|
-
const surplus = netSurplusAt(sampleMs);
|
|
219
|
-
let chargeW = 0;
|
|
220
|
-
let dischargeW = 0;
|
|
221
|
-
if (surplus > 0) {
|
|
222
|
-
const headroomWh = (BATTERY_MAX_SOC - soc) / 100 * BATTERY_CAPACITY_WH;
|
|
223
|
-
const headroomW = headroomWh / stepHours;
|
|
224
|
-
chargeW = Math.min(surplus, BATTERY_POWER_CAP_W, Math.max(0, headroomW));
|
|
225
|
-
} else if (surplus < 0) {
|
|
226
|
-
const availWh = (soc - BATTERY_MIN_SOC) / 100 * BATTERY_CAPACITY_WH;
|
|
227
|
-
const availW = availWh / stepHours;
|
|
228
|
-
dischargeW = Math.min(-surplus, BATTERY_POWER_CAP_W, Math.max(0, availW));
|
|
229
|
-
}
|
|
230
|
-
const deltaWh = (chargeW - dischargeW) * stepHours;
|
|
231
|
-
const nextSoc = soc + deltaWh / BATTERY_CAPACITY_WH * 100;
|
|
232
|
-
arr[i + 1] = Math.max(BATTERY_MIN_SOC, Math.min(BATTERY_MAX_SOC, nextSoc));
|
|
233
|
-
}
|
|
234
|
-
socCache.set(dayKey, arr);
|
|
235
|
-
return arr;
|
|
236
|
-
}
|
|
237
|
-
/** Battery SOC (%) at an exact timestamp, linearly interpolated between steps. */
|
|
238
|
-
function batterySoc(lt, midnightMs) {
|
|
239
|
-
const prefix = socPrefix(lt.dayKey, midnightMs);
|
|
240
|
-
const stepFloat = lt.minuteOfDay / SOC_STEP_MIN;
|
|
241
|
-
const i = Math.floor(stepFloat);
|
|
242
|
-
const frac = stepFloat - i;
|
|
243
|
-
const a = prefix[i] ?? MIDNIGHT_SOC;
|
|
244
|
-
const b = prefix[i + 1] ?? a;
|
|
245
|
-
return a + (b - a) * frac;
|
|
246
|
-
}
|
|
247
|
-
function homePower(lt) {
|
|
248
|
-
const base = ALWAYS_ON_W + fridgePower(lt) + kettlePower(lt) + ovenPower(lt) + dishwasherPower(lt) + washerPower(lt) + evChargerPower(lt) + heatPumpPower(lt) + occupancyEnvelope(lt);
|
|
249
|
-
return base * homeNoise(lt);
|
|
250
|
-
}
|
|
251
|
-
/**
|
|
252
|
-
* Compute every instantaneous energy value at a timestamp.
|
|
253
|
-
*
|
|
254
|
-
* Deterministic: equal timestamps always yield deep-equal samples. Obeys the
|
|
255
|
-
* conservation invariant
|
|
256
|
-
* `solar + gridImport + batteryDischarge === home + gridExport + batteryCharge`
|
|
257
|
-
* to within rounding.
|
|
258
|
-
*/
|
|
259
|
-
function simulateEnergy(timestampMs) {
|
|
260
|
-
const lt = localTime(timestampMs);
|
|
261
|
-
const midnightMs = localMidnightMs(timestampMs, lt);
|
|
262
|
-
const solarW = solarPower(lt);
|
|
263
|
-
const fridgeW = fridgePower(lt);
|
|
264
|
-
const ovenW = ovenPower(lt);
|
|
265
|
-
const dishwasherW = dishwasherPower(lt);
|
|
266
|
-
const washerW = washerPower(lt);
|
|
267
|
-
const evChargerW = evChargerPower(lt);
|
|
268
|
-
const alwaysOnW = ALWAYS_ON_W;
|
|
269
|
-
const homeW = homePower(lt);
|
|
270
|
-
const soc = batterySoc(lt, midnightMs);
|
|
271
|
-
const surplus = solarW - homeW;
|
|
272
|
-
let batteryChargeW = 0;
|
|
273
|
-
let batteryDischargeW = 0;
|
|
274
|
-
if (surplus > 0) {
|
|
275
|
-
const headroom = soc < BATTERY_MAX_SOC ? BATTERY_POWER_CAP_W : 0;
|
|
276
|
-
batteryChargeW = Math.min(surplus, headroom);
|
|
277
|
-
} else if (surplus < 0) {
|
|
278
|
-
const avail = soc > BATTERY_MIN_SOC ? BATTERY_POWER_CAP_W : 0;
|
|
279
|
-
batteryDischargeW = Math.min(-surplus, avail);
|
|
280
|
-
}
|
|
281
|
-
const remainingDeficit = homeW - solarW - batteryDischargeW + batteryChargeW;
|
|
282
|
-
const gridImportW = Math.max(0, remainingDeficit);
|
|
283
|
-
const gridExportW = Math.max(0, -remainingDeficit);
|
|
284
|
-
return {
|
|
285
|
-
solarW,
|
|
286
|
-
homeW,
|
|
287
|
-
batteryChargeW,
|
|
288
|
-
batteryDischargeW,
|
|
289
|
-
batterySocPct: soc,
|
|
290
|
-
gridImportW,
|
|
291
|
-
gridExportW,
|
|
292
|
-
fridgeW,
|
|
293
|
-
dishwasherW,
|
|
294
|
-
washerW,
|
|
295
|
-
ovenW,
|
|
296
|
-
evChargerW,
|
|
297
|
-
alwaysOnW
|
|
298
|
-
};
|
|
299
|
-
}
|
|
300
|
-
/** Whether the sun is above the horizon at a timestamp. */
|
|
301
|
-
function isSunUp(timestampMs) {
|
|
302
|
-
const lt = localTime(timestampMs);
|
|
303
|
-
return lt.hourFrac > SUNRISE_HOUR && lt.hourFrac < SUNSET_HOUR;
|
|
304
|
-
}
|
|
305
|
-
/** Next sunrise/sunset ISO strings relative to a timestamp. */
|
|
306
|
-
function sunEvents(timestampMs) {
|
|
307
|
-
const lt = localTime(timestampMs);
|
|
308
|
-
const midnightMs = localMidnightMs(timestampMs, lt);
|
|
309
|
-
const sunriseMs = midnightMs + SUNRISE_HOUR * 36e5;
|
|
310
|
-
const sunsetMs = midnightMs + SUNSET_HOUR * 36e5;
|
|
311
|
-
const nextRising = timestampMs < sunriseMs ? sunriseMs : sunriseMs + 864e5;
|
|
312
|
-
const nextSetting = timestampMs < sunsetMs ? sunsetMs : sunsetMs + 864e5;
|
|
313
|
-
return {
|
|
314
|
-
nextRising: new Date(nextRising).toISOString(),
|
|
315
|
-
nextSetting: new Date(nextSetting).toISOString(),
|
|
316
|
-
elevation: Math.round(sunElevation(lt) * 100) / 100
|
|
317
|
-
};
|
|
318
|
-
}
|
|
319
|
-
/** The energy sensor entity ids the simulation drives. */
|
|
320
|
-
const ENERGY_ENTITY_IDS = [
|
|
321
|
-
"sensor.solar_power",
|
|
322
|
-
"sensor.grid_import_power",
|
|
323
|
-
"sensor.grid_export_power",
|
|
324
|
-
"sensor.battery_charge_power",
|
|
325
|
-
"sensor.battery_discharge_power",
|
|
326
|
-
"sensor.battery_soc",
|
|
327
|
-
"sensor.home_power",
|
|
328
|
-
"sensor.fridge_power",
|
|
329
|
-
"sensor.dishwasher_power",
|
|
330
|
-
"sensor.washing_machine_power",
|
|
331
|
-
"sensor.oven_power",
|
|
332
|
-
"sensor.ev_charger_power",
|
|
333
|
-
"sensor.always_on_power"
|
|
334
|
-
];
|
|
335
|
-
/** Map an energy entity id to its numeric value from a sample (W, or % for SOC). */
|
|
336
|
-
function energyEntityValue(entityId, sample) {
|
|
337
|
-
switch (entityId) {
|
|
338
|
-
case "sensor.solar_power": return sample.solarW;
|
|
339
|
-
case "sensor.grid_import_power": return sample.gridImportW;
|
|
340
|
-
case "sensor.grid_export_power": return sample.gridExportW;
|
|
341
|
-
case "sensor.battery_charge_power": return sample.batteryChargeW;
|
|
342
|
-
case "sensor.battery_discharge_power": return sample.batteryDischargeW;
|
|
343
|
-
case "sensor.battery_soc": return sample.batterySocPct;
|
|
344
|
-
case "sensor.home_power": return sample.homeW;
|
|
345
|
-
case "sensor.fridge_power": return sample.fridgeW;
|
|
346
|
-
case "sensor.dishwasher_power": return sample.dishwasherW;
|
|
347
|
-
case "sensor.washing_machine_power": return sample.washerW;
|
|
348
|
-
case "sensor.oven_power": return sample.ovenW;
|
|
349
|
-
case "sensor.ev_charger_power": return sample.evChargerW;
|
|
350
|
-
case "sensor.always_on_power": return sample.alwaysOnW;
|
|
351
|
-
default: return void 0;
|
|
352
|
-
}
|
|
353
|
-
}
|
|
354
|
-
/** Whether an entity id is one of the simulated energy sensors. */
|
|
355
|
-
function isEnergyEntity(entityId) {
|
|
356
|
-
return energyEntityValue(entityId, ZERO_SAMPLE) !== void 0;
|
|
357
|
-
}
|
|
358
|
-
const ZERO_SAMPLE = {
|
|
359
|
-
solarW: 0,
|
|
360
|
-
homeW: 0,
|
|
361
|
-
batteryChargeW: 0,
|
|
362
|
-
batteryDischargeW: 0,
|
|
363
|
-
batterySocPct: 0,
|
|
364
|
-
gridImportW: 0,
|
|
365
|
-
gridExportW: 0,
|
|
366
|
-
fridgeW: 0,
|
|
367
|
-
dishwasherW: 0,
|
|
368
|
-
washerW: 0,
|
|
369
|
-
ovenW: 0,
|
|
370
|
-
evChargerW: 0,
|
|
371
|
-
alwaysOnW: 0
|
|
372
|
-
};
|
|
373
|
-
/** Round to 1 decimal for display-friendly state strings (whole % for SOC). */
|
|
374
|
-
function formatEnergyState(entityId, value) {
|
|
375
|
-
if (entityId === "sensor.battery_soc") return String(Math.round(value));
|
|
376
|
-
return String(Math.round(value));
|
|
377
|
-
}
|
|
378
|
-
/**
|
|
379
|
-
* Synthesize history points for one energy entity by sampling the model across
|
|
380
|
-
* the window at a fixed cadence (default 1-minute). Returns HA-compressed
|
|
381
|
-
* `{ lu, s }` points with `lu` in Unix seconds.
|
|
382
|
-
*/
|
|
383
|
-
function synthesizeEnergyHistory(entityId, startMs, endMs, stepMs = 6e4) {
|
|
384
|
-
const points = [];
|
|
385
|
-
for (let t = startMs; t <= endMs; t += stepMs) {
|
|
386
|
-
const value = energyEntityValue(entityId, simulateEnergy(t));
|
|
387
|
-
if (value === void 0) continue;
|
|
388
|
-
points.push({
|
|
389
|
-
lu: Math.round(t / 1e3),
|
|
390
|
-
s: formatEnergyState(entityId, value)
|
|
391
|
-
});
|
|
392
|
-
}
|
|
393
|
-
return points;
|
|
394
|
-
}
|
|
395
|
-
/** Bucket length in ms for a statistics period. */
|
|
396
|
-
function bucketMs(period) {
|
|
397
|
-
return period === "hour" ? 36e5 : 864e5;
|
|
398
|
-
}
|
|
399
|
-
/**
|
|
400
|
-
* Synthesize statistics buckets for one energy entity across a window.
|
|
401
|
-
*
|
|
402
|
-
* `mean`/`min`/`max` come from 5-minute power samples in the bucket; `sum` is
|
|
403
|
-
* the energy in Wh (mean W times bucket hours). `sum` is cumulative across the
|
|
404
|
-
* window, matching HA's monotonically increasing meter semantics.
|
|
405
|
-
*/
|
|
406
|
-
function synthesizeEnergyStatistics(entityId, startMs, endMs, period) {
|
|
407
|
-
const span = bucketMs(period);
|
|
408
|
-
const sampleMs = 5 * 6e4;
|
|
409
|
-
const buckets = [];
|
|
410
|
-
let cumulativeWh = 0;
|
|
411
|
-
for (let bStart = startMs; bStart < endMs; bStart += span) {
|
|
412
|
-
const bEnd = bStart + span;
|
|
413
|
-
let sum = 0;
|
|
414
|
-
let count = 0;
|
|
415
|
-
let min = Number.POSITIVE_INFINITY;
|
|
416
|
-
let max = Number.NEGATIVE_INFINITY;
|
|
417
|
-
for (let t = bStart; t < bEnd; t += sampleMs) {
|
|
418
|
-
const value = energyEntityValue(entityId, simulateEnergy(t));
|
|
419
|
-
if (value === void 0) continue;
|
|
420
|
-
sum += value;
|
|
421
|
-
count++;
|
|
422
|
-
if (value < min) min = value;
|
|
423
|
-
if (value > max) max = value;
|
|
424
|
-
}
|
|
425
|
-
if (count === 0) continue;
|
|
426
|
-
const mean = sum / count;
|
|
427
|
-
const bucketHours = span / 36e5;
|
|
428
|
-
const energyWh = entityId === "sensor.battery_soc" ? 0 : mean * bucketHours;
|
|
429
|
-
cumulativeWh += energyWh;
|
|
430
|
-
buckets.push({
|
|
431
|
-
start: bStart,
|
|
432
|
-
end: bEnd,
|
|
433
|
-
mean,
|
|
434
|
-
min: min === Number.POSITIVE_INFINITY ? mean : min,
|
|
435
|
-
max: max === Number.NEGATIVE_INFINITY ? mean : max,
|
|
436
|
-
sum: cumulativeWh,
|
|
437
|
-
change: energyWh / 1e3
|
|
438
|
-
});
|
|
439
|
-
}
|
|
440
|
-
return buckets;
|
|
441
|
-
}
|
|
442
|
-
/** The demo energy dashboard preferences wiring the simulated sensors. */
|
|
443
|
-
function demoEnergyPreferences() {
|
|
444
|
-
return {
|
|
445
|
-
energy_sources: [
|
|
446
|
-
{
|
|
447
|
-
type: "solar",
|
|
448
|
-
stat_energy_from: "sensor.solar_power"
|
|
449
|
-
},
|
|
450
|
-
{
|
|
451
|
-
type: "grid",
|
|
452
|
-
flow_from: [{ stat_energy_from: "sensor.grid_import_power" }],
|
|
453
|
-
flow_to: [{ stat_energy_to: "sensor.grid_export_power" }]
|
|
454
|
-
},
|
|
455
|
-
{
|
|
456
|
-
type: "battery",
|
|
457
|
-
stat_energy_from: "sensor.battery_discharge_power",
|
|
458
|
-
stat_energy_to: "sensor.battery_charge_power"
|
|
459
|
-
}
|
|
460
|
-
],
|
|
461
|
-
device_consumption: [
|
|
462
|
-
{
|
|
463
|
-
stat_consumption: "sensor.fridge_power",
|
|
464
|
-
name: "Fridge"
|
|
465
|
-
},
|
|
466
|
-
{
|
|
467
|
-
stat_consumption: "sensor.dishwasher_power",
|
|
468
|
-
name: "Dishwasher"
|
|
469
|
-
},
|
|
470
|
-
{
|
|
471
|
-
stat_consumption: "sensor.washing_machine_power",
|
|
472
|
-
name: "Washing Machine"
|
|
473
|
-
},
|
|
474
|
-
{
|
|
475
|
-
stat_consumption: "sensor.oven_power",
|
|
476
|
-
name: "Oven"
|
|
477
|
-
},
|
|
478
|
-
{
|
|
479
|
-
stat_consumption: "sensor.ev_charger_power",
|
|
480
|
-
name: "EV Charger"
|
|
481
|
-
},
|
|
482
|
-
{
|
|
483
|
-
stat_consumption: "sensor.always_on_power",
|
|
484
|
-
name: "Always On"
|
|
485
|
-
}
|
|
486
|
-
]
|
|
487
|
-
};
|
|
488
|
-
}
|
|
489
|
-
|
|
490
|
-
//#endregion
|
|
491
|
-
//#region src/demo/demo-data.ts
|
|
492
|
-
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
493
|
-
let contextCounter = 0;
|
|
494
|
-
function ctx() {
|
|
495
|
-
return {
|
|
496
|
-
id: `demo-${++contextCounter}`,
|
|
497
|
-
parent_id: null,
|
|
498
|
-
user_id: null
|
|
499
|
-
};
|
|
500
|
-
}
|
|
501
|
-
function makeEntity(entity_id, state$1, attributes = {}) {
|
|
502
|
-
return {
|
|
503
|
-
entity_id,
|
|
504
|
-
state: state$1,
|
|
505
|
-
attributes: {
|
|
506
|
-
friendly_name: friendlyName(entity_id),
|
|
507
|
-
...attributes
|
|
508
|
-
},
|
|
509
|
-
last_changed: now,
|
|
510
|
-
last_updated: now,
|
|
511
|
-
context: ctx()
|
|
512
|
-
};
|
|
513
|
-
}
|
|
514
|
-
/**
|
|
515
|
-
* One entity per weather scene the widget can render. Entity IDs use the
|
|
516
|
-
* `weather.demo_<slug>` form so a demo dashboard can reference them directly.
|
|
517
|
-
*/
|
|
518
|
-
const WEATHER_FIXTURES = [
|
|
519
|
-
{
|
|
520
|
-
slug: "sunny",
|
|
521
|
-
state: "sunny",
|
|
522
|
-
temp: 28,
|
|
523
|
-
apparent: 31,
|
|
524
|
-
humidity: 35,
|
|
525
|
-
pressure: 1018,
|
|
526
|
-
wind: 8,
|
|
527
|
-
low: 18
|
|
528
|
-
},
|
|
529
|
-
{
|
|
530
|
-
slug: "clear_night",
|
|
531
|
-
state: "clear-night",
|
|
532
|
-
temp: 14,
|
|
533
|
-
apparent: 12,
|
|
534
|
-
humidity: 55,
|
|
535
|
-
pressure: 1016,
|
|
536
|
-
wind: 5,
|
|
537
|
-
low: 9
|
|
538
|
-
},
|
|
539
|
-
{
|
|
540
|
-
slug: "cloudy",
|
|
541
|
-
state: "cloudy",
|
|
542
|
-
temp: 17,
|
|
543
|
-
apparent: 16,
|
|
544
|
-
humidity: 72,
|
|
545
|
-
pressure: 1010,
|
|
546
|
-
wind: 14,
|
|
547
|
-
low: 11
|
|
548
|
-
},
|
|
549
|
-
{
|
|
550
|
-
slug: "partly_cloudy",
|
|
551
|
-
state: "partlycloudy",
|
|
552
|
-
temp: 23,
|
|
553
|
-
apparent: 24,
|
|
554
|
-
humidity: 50,
|
|
555
|
-
pressure: 1014,
|
|
556
|
-
wind: 12,
|
|
557
|
-
low: 15
|
|
558
|
-
},
|
|
559
|
-
{
|
|
560
|
-
slug: "rainy",
|
|
561
|
-
state: "rainy",
|
|
562
|
-
temp: 12,
|
|
563
|
-
apparent: 10,
|
|
564
|
-
humidity: 88,
|
|
565
|
-
pressure: 1004,
|
|
566
|
-
wind: 18,
|
|
567
|
-
low: 8
|
|
568
|
-
},
|
|
569
|
-
{
|
|
570
|
-
slug: "pouring",
|
|
571
|
-
state: "pouring",
|
|
572
|
-
temp: 11,
|
|
573
|
-
apparent: 8,
|
|
574
|
-
humidity: 95,
|
|
575
|
-
pressure: 998,
|
|
576
|
-
wind: 26,
|
|
577
|
-
low: 7
|
|
578
|
-
},
|
|
579
|
-
{
|
|
580
|
-
slug: "snowy",
|
|
581
|
-
state: "snowy",
|
|
582
|
-
temp: -2,
|
|
583
|
-
apparent: -6,
|
|
584
|
-
humidity: 80,
|
|
585
|
-
pressure: 1020,
|
|
586
|
-
wind: 10,
|
|
587
|
-
low: -7
|
|
588
|
-
},
|
|
589
|
-
{
|
|
590
|
-
slug: "snowy_rainy",
|
|
591
|
-
state: "snowy-rainy",
|
|
592
|
-
temp: 1,
|
|
593
|
-
apparent: -2,
|
|
594
|
-
humidity: 92,
|
|
595
|
-
pressure: 1006,
|
|
596
|
-
wind: 16,
|
|
597
|
-
low: -2
|
|
598
|
-
},
|
|
599
|
-
{
|
|
600
|
-
slug: "lightning",
|
|
601
|
-
state: "lightning",
|
|
602
|
-
temp: 22,
|
|
603
|
-
apparent: 24,
|
|
604
|
-
humidity: 78,
|
|
605
|
-
pressure: 1001,
|
|
606
|
-
wind: 22,
|
|
607
|
-
low: 17
|
|
608
|
-
},
|
|
609
|
-
{
|
|
610
|
-
slug: "lightning_rainy",
|
|
611
|
-
state: "lightning-rainy",
|
|
612
|
-
temp: 19,
|
|
613
|
-
apparent: 18,
|
|
614
|
-
humidity: 90,
|
|
615
|
-
pressure: 996,
|
|
616
|
-
wind: 28,
|
|
617
|
-
low: 14
|
|
618
|
-
},
|
|
619
|
-
{
|
|
620
|
-
slug: "fog",
|
|
621
|
-
state: "fog",
|
|
622
|
-
temp: 8,
|
|
623
|
-
apparent: 6,
|
|
624
|
-
humidity: 98,
|
|
625
|
-
pressure: 1015,
|
|
626
|
-
wind: 4,
|
|
627
|
-
low: 6
|
|
628
|
-
},
|
|
629
|
-
{
|
|
630
|
-
slug: "hail",
|
|
631
|
-
state: "hail",
|
|
632
|
-
temp: 6,
|
|
633
|
-
apparent: 3,
|
|
634
|
-
humidity: 84,
|
|
635
|
-
pressure: 1002,
|
|
636
|
-
wind: 20,
|
|
637
|
-
low: 1
|
|
638
|
-
},
|
|
639
|
-
{
|
|
640
|
-
slug: "windy",
|
|
641
|
-
state: "windy",
|
|
642
|
-
temp: 18,
|
|
643
|
-
apparent: 15,
|
|
644
|
-
humidity: 60,
|
|
645
|
-
pressure: 1009,
|
|
646
|
-
wind: 42,
|
|
647
|
-
low: 12
|
|
648
|
-
},
|
|
649
|
-
{
|
|
650
|
-
slug: "exceptional",
|
|
651
|
-
state: "exceptional",
|
|
652
|
-
temp: 38,
|
|
653
|
-
apparent: 44,
|
|
654
|
-
humidity: 22,
|
|
655
|
-
pressure: 1005,
|
|
656
|
-
wind: 30,
|
|
657
|
-
low: 28
|
|
658
|
-
}
|
|
659
|
-
];
|
|
660
|
-
function makeWeatherEntity(w) {
|
|
661
|
-
const today = /* @__PURE__ */ new Date();
|
|
662
|
-
const day = (n) => new Date(today.getTime() + n * 864e5).toISOString();
|
|
663
|
-
return makeEntity(`weather.demo_${w.slug}`, w.state, {
|
|
664
|
-
temperature: w.temp,
|
|
665
|
-
temperature_unit: "°C",
|
|
666
|
-
apparent_temperature: w.apparent ?? w.temp,
|
|
667
|
-
humidity: w.humidity,
|
|
668
|
-
pressure: w.pressure,
|
|
669
|
-
pressure_unit: "hPa",
|
|
670
|
-
wind_speed: w.wind,
|
|
671
|
-
wind_speed_unit: "km/h",
|
|
672
|
-
wind_bearing: w.bearing ?? 220,
|
|
673
|
-
visibility: 10,
|
|
674
|
-
visibility_unit: "km",
|
|
675
|
-
uv_index: w.state === "sunny" ? 8 : 2,
|
|
676
|
-
forecast: [
|
|
677
|
-
{
|
|
678
|
-
datetime: day(0),
|
|
679
|
-
condition: w.state,
|
|
680
|
-
temperature: w.temp,
|
|
681
|
-
templow: w.low
|
|
682
|
-
},
|
|
683
|
-
{
|
|
684
|
-
datetime: day(1),
|
|
685
|
-
condition: "partlycloudy",
|
|
686
|
-
temperature: w.temp + 2,
|
|
687
|
-
templow: w.low
|
|
688
|
-
},
|
|
689
|
-
{
|
|
690
|
-
datetime: day(2),
|
|
691
|
-
condition: "cloudy",
|
|
692
|
-
temperature: w.temp + 1,
|
|
693
|
-
templow: w.low - 1
|
|
694
|
-
},
|
|
695
|
-
{
|
|
696
|
-
datetime: day(3),
|
|
697
|
-
condition: "sunny",
|
|
698
|
-
temperature: w.temp + 4,
|
|
699
|
-
templow: w.low + 1
|
|
700
|
-
},
|
|
701
|
-
{
|
|
702
|
-
datetime: day(4),
|
|
703
|
-
condition: "rainy",
|
|
704
|
-
temperature: w.temp - 2,
|
|
705
|
-
templow: w.low - 2
|
|
706
|
-
}
|
|
707
|
-
]
|
|
708
|
-
});
|
|
709
|
-
}
|
|
710
|
-
function friendlyName(entityId) {
|
|
711
|
-
const objectId = entityId.split(".")[1] ?? entityId;
|
|
712
|
-
return objectId.split("_").map((w) => w.charAt(0).toUpperCase() + w.slice(1)).join(" ");
|
|
713
|
-
}
|
|
714
|
-
function makeRegistryEntry(entity_id, overrides = {}) {
|
|
715
|
-
const domain = entity_id.split(".")[0] ?? "";
|
|
716
|
-
return {
|
|
717
|
-
aliases: [],
|
|
718
|
-
area_id: null,
|
|
719
|
-
capabilities: null,
|
|
720
|
-
categories: {},
|
|
721
|
-
config_entry_id: "demo_config",
|
|
722
|
-
config_subentry_id: null,
|
|
723
|
-
created_at: now,
|
|
724
|
-
device_class: null,
|
|
725
|
-
device_id: null,
|
|
726
|
-
disabled_by: null,
|
|
727
|
-
entity_category: null,
|
|
728
|
-
entity_id,
|
|
729
|
-
has_entity_name: false,
|
|
730
|
-
hidden_by: null,
|
|
731
|
-
icon: null,
|
|
732
|
-
id: `reg-${entity_id.replace(".", "-")}`,
|
|
733
|
-
labels: [],
|
|
734
|
-
modified_at: now,
|
|
735
|
-
name: null,
|
|
736
|
-
options: {},
|
|
737
|
-
original_device_class: null,
|
|
738
|
-
original_icon: null,
|
|
739
|
-
original_name: null,
|
|
740
|
-
platform: domain === "weather" ? "met" : "demo",
|
|
741
|
-
previous_unique_id: null,
|
|
742
|
-
suggested_object_id: null,
|
|
743
|
-
supported_features: 0,
|
|
744
|
-
translation_key: null,
|
|
745
|
-
unique_id: `demo_${entity_id.replace(".", "_")}`,
|
|
746
|
-
unit_of_measurement: null,
|
|
747
|
-
...overrides
|
|
748
|
-
};
|
|
749
|
-
}
|
|
750
|
-
function makeDevice(id, name, areaId, manufacturer = "Demo", model = "Virtual") {
|
|
751
|
-
return {
|
|
752
|
-
id,
|
|
753
|
-
name,
|
|
754
|
-
name_by_user: null,
|
|
755
|
-
manufacturer,
|
|
756
|
-
model,
|
|
757
|
-
model_id: null,
|
|
758
|
-
sw_version: "1.0.0",
|
|
759
|
-
hw_version: null,
|
|
760
|
-
area_id: areaId,
|
|
761
|
-
config_entries: ["demo_config"],
|
|
762
|
-
config_entries_subentries: {},
|
|
763
|
-
configuration_url: null,
|
|
764
|
-
connections: [],
|
|
765
|
-
created_at: now,
|
|
766
|
-
disabled_by: null,
|
|
767
|
-
entry_type: null,
|
|
768
|
-
identifiers: [["demo", id]],
|
|
769
|
-
labels: [],
|
|
770
|
-
modified_at: now,
|
|
771
|
-
primary_config_entry: "demo_config",
|
|
772
|
-
serial_number: null,
|
|
773
|
-
via_device_id: null
|
|
774
|
-
};
|
|
775
|
-
}
|
|
776
|
-
function makeArea(id, name, floorId = "ground_floor") {
|
|
777
|
-
return {
|
|
778
|
-
id,
|
|
779
|
-
name,
|
|
780
|
-
normalized_name: name.toLowerCase().replace(/\s+/g, "_"),
|
|
781
|
-
created_at: now,
|
|
782
|
-
modified_at: now,
|
|
783
|
-
aliases: [],
|
|
784
|
-
floor_id: floorId,
|
|
785
|
-
humidity_entity_id: null,
|
|
786
|
-
icon: null,
|
|
787
|
-
labels: [],
|
|
788
|
-
picture: null,
|
|
789
|
-
temperature_entity_id: null
|
|
790
|
-
};
|
|
791
|
-
}
|
|
792
|
-
/**
|
|
793
|
-
* Create ~30 curated apartment entity fixtures.
|
|
794
|
-
*
|
|
795
|
-
* Entities span lights, sensors, binary sensors, climate, covers,
|
|
796
|
-
* locks, switches, media player, weather, camera, scenes, and buttons
|
|
797
|
-
* across 6 areas: living_room, bedroom, kitchen, entry, utility, garage.
|
|
798
|
-
*/
|
|
799
|
-
function createDemoFixtures() {
|
|
800
|
-
contextCounter = 0;
|
|
801
|
-
const areas = {
|
|
802
|
-
living_room: makeArea("living_room", "Living Room"),
|
|
803
|
-
bedroom: makeArea("bedroom", "Bedroom"),
|
|
804
|
-
kitchen: makeArea("kitchen", "Kitchen"),
|
|
805
|
-
entry: makeArea("entry", "Entry"),
|
|
806
|
-
utility: makeArea("utility", "Utility"),
|
|
807
|
-
garage: makeArea("garage", "Garage")
|
|
808
|
-
};
|
|
809
|
-
areas.living_room.temperature_entity_id = "sensor.temperature_living";
|
|
810
|
-
areas.living_room.humidity_entity_id = "sensor.humidity_living";
|
|
811
|
-
const devices = {
|
|
812
|
-
living_room_lights: makeDevice("living_room_lights", "Living Room Lights", "living_room", "Philips", "Hue"),
|
|
813
|
-
kitchen_lights: makeDevice("kitchen_lights", "Kitchen Lights", "kitchen", "IKEA", "Tradfri"),
|
|
814
|
-
bedroom_lights: makeDevice("bedroom_lights", "Bedroom Lights", "bedroom", "Philips", "Hue"),
|
|
815
|
-
hallway_lights: makeDevice("hallway_lights", "Hallway Lights", "entry", "IKEA", "Tradfri"),
|
|
816
|
-
bathroom_lights: makeDevice("bathroom_lights", "Bathroom Lights", null),
|
|
817
|
-
thermostat_lr: makeDevice("thermostat_lr", "Living Room Thermostat", "living_room", "Nest", "Learning Thermostat"),
|
|
818
|
-
thermostat_br: makeDevice("thermostat_br", "Bedroom AC", "bedroom", "Daikin", "Split AC"),
|
|
819
|
-
blinds_lr: makeDevice("blinds_lr", "Living Room Blinds", "living_room", "Somfy", "Roller Shade"),
|
|
820
|
-
curtains_br: makeDevice("curtains_br", "Bedroom Curtains", "bedroom", "IKEA", "Fyrtur"),
|
|
821
|
-
garage_door_opener: makeDevice("garage_door_opener", "Garage Door Opener", "garage", "Chamberlain", "MyQ"),
|
|
822
|
-
shutters_lr: makeDevice("shutters_lr", "Living Room Shutters", "living_room", "Somfy", "Venetian Shutter"),
|
|
823
|
-
blinds_kitchen: makeDevice("blinds_kitchen", "Kitchen Blinds", "kitchen", "IKEA", "Praktlysing"),
|
|
824
|
-
lock_front: makeDevice("lock_front", "Front Door Lock", "entry", "Yale", "Assure Lock 2"),
|
|
825
|
-
lock_back: makeDevice("lock_back", "Back Door Lock", "entry", "August", "Smart Lock Pro"),
|
|
826
|
-
speaker_lr: makeDevice("speaker_lr", "Living Room Speaker", "living_room", "Sonos", "One"),
|
|
827
|
-
camera_front: makeDevice("camera_front", "Front Door Camera", "entry", "Ring", "Video Doorbell"),
|
|
828
|
-
door_sensor: makeDevice("door_sensor", "Front Door Sensor", "entry", "Aqara", "Door Sensor"),
|
|
829
|
-
motion_sensor: makeDevice("motion_sensor", "Hallway Motion Sensor", "entry", "Aqara", "Motion Sensor")
|
|
830
|
-
};
|
|
831
|
-
const entities = {};
|
|
832
|
-
const entityRegistry = {};
|
|
833
|
-
function add(e, regOverrides = {}) {
|
|
834
|
-
entities[e.entity_id] = e;
|
|
835
|
-
entityRegistry[e.entity_id] = makeRegistryEntry(e.entity_id, regOverrides);
|
|
836
|
-
}
|
|
837
|
-
add(makeEntity("light.living_room_main", "on", {
|
|
838
|
-
brightness: 179,
|
|
839
|
-
color_temp_kelvin: 3e3,
|
|
840
|
-
color_mode: "color_temp",
|
|
841
|
-
supported_color_modes: ["color_temp", "xy"]
|
|
842
|
-
}), {
|
|
843
|
-
device_id: "living_room_lights",
|
|
844
|
-
area_id: "living_room",
|
|
845
|
-
supported_features: 44
|
|
846
|
-
});
|
|
847
|
-
add(makeEntity("light.kitchen_counter", "off", { supported_color_modes: ["brightness"] }), {
|
|
848
|
-
device_id: "kitchen_lights",
|
|
849
|
-
area_id: "kitchen",
|
|
850
|
-
supported_features: 1
|
|
851
|
-
});
|
|
852
|
-
add(makeEntity("light.bedroom_ceiling", "on", {
|
|
853
|
-
brightness: 77,
|
|
854
|
-
color_temp_kelvin: 2700,
|
|
855
|
-
color_mode: "color_temp",
|
|
856
|
-
supported_color_modes: ["color_temp"]
|
|
857
|
-
}), {
|
|
858
|
-
device_id: "bedroom_lights",
|
|
859
|
-
area_id: "bedroom",
|
|
860
|
-
supported_features: 44
|
|
861
|
-
});
|
|
862
|
-
add(makeEntity("light.bathroom", "off", { supported_color_modes: ["brightness"] }), { device_id: "bathroom_lights" });
|
|
863
|
-
add(makeEntity("light.hallway", "on", {
|
|
864
|
-
brightness: 128,
|
|
865
|
-
color_mode: "brightness",
|
|
866
|
-
supported_color_modes: ["brightness"]
|
|
867
|
-
}), {
|
|
868
|
-
device_id: "hallway_lights",
|
|
869
|
-
area_id: "entry",
|
|
870
|
-
supported_features: 1
|
|
871
|
-
});
|
|
872
|
-
add(makeEntity("sensor.temperature_living", "22.5", {
|
|
873
|
-
unit_of_measurement: "°C",
|
|
874
|
-
device_class: "temperature",
|
|
875
|
-
state_class: "measurement"
|
|
876
|
-
}), {
|
|
877
|
-
area_id: "living_room",
|
|
878
|
-
device_class: "temperature",
|
|
879
|
-
unit_of_measurement: "°C"
|
|
880
|
-
});
|
|
881
|
-
add(makeEntity("sensor.humidity_living", "45", {
|
|
882
|
-
unit_of_measurement: "%",
|
|
883
|
-
device_class: "humidity",
|
|
884
|
-
state_class: "measurement"
|
|
885
|
-
}), {
|
|
886
|
-
area_id: "living_room",
|
|
887
|
-
device_class: "humidity",
|
|
888
|
-
unit_of_measurement: "%"
|
|
889
|
-
});
|
|
890
|
-
add(makeEntity("sensor.temperature_outdoor", "-2", {
|
|
891
|
-
unit_of_measurement: "°C",
|
|
892
|
-
device_class: "temperature",
|
|
893
|
-
state_class: "measurement"
|
|
894
|
-
}), {
|
|
895
|
-
device_class: "temperature",
|
|
896
|
-
unit_of_measurement: "°C"
|
|
897
|
-
});
|
|
898
|
-
add(makeEntity("sensor.power_consumption", "1.2", {
|
|
899
|
-
unit_of_measurement: "kW",
|
|
900
|
-
device_class: "power",
|
|
901
|
-
state_class: "measurement"
|
|
902
|
-
}), {
|
|
903
|
-
device_class: "power",
|
|
904
|
-
unit_of_measurement: "kW"
|
|
905
|
-
});
|
|
906
|
-
add(makeEntity("sensor.battery_door_sensor", "87", {
|
|
907
|
-
unit_of_measurement: "%",
|
|
908
|
-
device_class: "battery",
|
|
909
|
-
state_class: "measurement"
|
|
910
|
-
}), {
|
|
911
|
-
device_id: "door_sensor",
|
|
912
|
-
area_id: "entry",
|
|
913
|
-
device_class: "battery",
|
|
914
|
-
unit_of_measurement: "%"
|
|
915
|
-
});
|
|
916
|
-
add(makeEntity("binary_sensor.front_door", "off", { device_class: "door" }), {
|
|
917
|
-
device_id: "door_sensor",
|
|
918
|
-
area_id: "entry",
|
|
919
|
-
device_class: "door"
|
|
920
|
-
});
|
|
921
|
-
add(makeEntity("binary_sensor.motion_hallway", "off", { device_class: "motion" }), {
|
|
922
|
-
device_id: "motion_sensor",
|
|
923
|
-
area_id: "entry",
|
|
924
|
-
device_class: "motion"
|
|
925
|
-
});
|
|
926
|
-
add(makeEntity("binary_sensor.window_bedroom", "off", { device_class: "window" }), {
|
|
927
|
-
area_id: "bedroom",
|
|
928
|
-
device_class: "window"
|
|
929
|
-
});
|
|
930
|
-
add(makeEntity("climate.living_room_thermostat", "heat", {
|
|
931
|
-
temperature: 22,
|
|
932
|
-
current_temperature: 21.8,
|
|
933
|
-
hvac_modes: [
|
|
934
|
-
"off",
|
|
935
|
-
"heat",
|
|
936
|
-
"cool",
|
|
937
|
-
"auto"
|
|
938
|
-
],
|
|
939
|
-
hvac_action: "heating",
|
|
940
|
-
min_temp: 7,
|
|
941
|
-
max_temp: 35
|
|
942
|
-
}), {
|
|
943
|
-
device_id: "thermostat_lr",
|
|
944
|
-
area_id: "living_room",
|
|
945
|
-
supported_features: 385
|
|
946
|
-
});
|
|
947
|
-
add(makeEntity("climate.bedroom_ac", "off", {
|
|
948
|
-
temperature: 20,
|
|
949
|
-
current_temperature: 22.1,
|
|
950
|
-
hvac_modes: [
|
|
951
|
-
"off",
|
|
952
|
-
"cool",
|
|
953
|
-
"heat",
|
|
954
|
-
"dry",
|
|
955
|
-
"fan_only"
|
|
956
|
-
],
|
|
957
|
-
hvac_action: "idle",
|
|
958
|
-
min_temp: 16,
|
|
959
|
-
max_temp: 30
|
|
960
|
-
}), {
|
|
961
|
-
device_id: "thermostat_br",
|
|
962
|
-
area_id: "bedroom",
|
|
963
|
-
supported_features: 385
|
|
964
|
-
});
|
|
965
|
-
add(makeEntity("cover.living_room_blinds", "open", {
|
|
966
|
-
current_position: 80,
|
|
967
|
-
device_class: "shade"
|
|
968
|
-
}), {
|
|
969
|
-
device_id: "blinds_lr",
|
|
970
|
-
area_id: "living_room",
|
|
971
|
-
device_class: "shade",
|
|
972
|
-
supported_features: 15
|
|
973
|
-
});
|
|
974
|
-
add(makeEntity("cover.bedroom_curtains", "closed", {
|
|
975
|
-
current_position: 0,
|
|
976
|
-
device_class: "curtain"
|
|
977
|
-
}), {
|
|
978
|
-
device_id: "curtains_br",
|
|
979
|
-
area_id: "bedroom",
|
|
980
|
-
device_class: "curtain",
|
|
981
|
-
supported_features: 15
|
|
982
|
-
});
|
|
983
|
-
add(makeEntity("cover.garage_door", "closed", { device_class: "garage" }), {
|
|
984
|
-
device_id: "garage_door_opener",
|
|
985
|
-
area_id: "garage",
|
|
986
|
-
device_class: "garage",
|
|
987
|
-
supported_features: 11
|
|
988
|
-
});
|
|
989
|
-
add(makeEntity("cover.driveway_gate", "open", {
|
|
990
|
-
device_class: "gate",
|
|
991
|
-
assumed_state: true
|
|
992
|
-
}), {
|
|
993
|
-
area_id: "garage",
|
|
994
|
-
device_class: "gate",
|
|
995
|
-
supported_features: 3
|
|
996
|
-
});
|
|
997
|
-
add(makeEntity("cover.living_room_shutters", "open", {
|
|
998
|
-
current_position: 45,
|
|
999
|
-
current_tilt_position: 30,
|
|
1000
|
-
device_class: "shutter"
|
|
1001
|
-
}), {
|
|
1002
|
-
device_id: "shutters_lr",
|
|
1003
|
-
area_id: "living_room",
|
|
1004
|
-
device_class: "shutter",
|
|
1005
|
-
supported_features: 255
|
|
1006
|
-
});
|
|
1007
|
-
add(makeEntity("cover.kitchen_blinds", "open", {
|
|
1008
|
-
current_position: 100,
|
|
1009
|
-
device_class: "blind"
|
|
1010
|
-
}), {
|
|
1011
|
-
device_id: "blinds_kitchen",
|
|
1012
|
-
area_id: "kitchen",
|
|
1013
|
-
device_class: "blind",
|
|
1014
|
-
supported_features: 127
|
|
1015
|
-
});
|
|
1016
|
-
add(makeEntity("lock.front_door_lock", "locked", {}), {
|
|
1017
|
-
device_id: "lock_front",
|
|
1018
|
-
area_id: "entry",
|
|
1019
|
-
supported_features: 1
|
|
1020
|
-
});
|
|
1021
|
-
add(makeEntity("lock.back_door_lock", "locked", {}), {
|
|
1022
|
-
device_id: "lock_back",
|
|
1023
|
-
area_id: "entry",
|
|
1024
|
-
supported_features: 1
|
|
1025
|
-
});
|
|
1026
|
-
add(makeEntity("switch.coffee_machine", "off", { device_class: "outlet" }), {
|
|
1027
|
-
area_id: "kitchen",
|
|
1028
|
-
device_class: "outlet"
|
|
1029
|
-
});
|
|
1030
|
-
add(makeEntity("switch.fan_living_room", "on", { device_class: "switch" }), {
|
|
1031
|
-
area_id: "living_room",
|
|
1032
|
-
device_class: "switch"
|
|
1033
|
-
});
|
|
1034
|
-
add(makeEntity("media_player.living_room_speaker", "playing", {
|
|
1035
|
-
media_title: "Lo-fi Beats",
|
|
1036
|
-
media_artist: "Chill Station",
|
|
1037
|
-
media_content_type: "music",
|
|
1038
|
-
volume_level: .4,
|
|
1039
|
-
is_volume_muted: false,
|
|
1040
|
-
supported_features: 152461
|
|
1041
|
-
}), {
|
|
1042
|
-
device_id: "speaker_lr",
|
|
1043
|
-
area_id: "living_room",
|
|
1044
|
-
supported_features: 152461
|
|
1045
|
-
});
|
|
1046
|
-
for (const w of WEATHER_FIXTURES) add(makeWeatherEntity(w), { platform: "met" });
|
|
1047
|
-
add(makeEntity("camera.front_door_camera", "streaming", {
|
|
1048
|
-
entity_picture: "/api/camera_proxy/camera.front_door_camera",
|
|
1049
|
-
frontend_stream_type: "hls",
|
|
1050
|
-
access_token: "demo-token"
|
|
1051
|
-
}), {
|
|
1052
|
-
device_id: "camera_front",
|
|
1053
|
-
area_id: "entry",
|
|
1054
|
-
supported_features: 2
|
|
1055
|
-
});
|
|
1056
|
-
add(makeEntity("scene.movie_night", "scening", { entity_id: [
|
|
1057
|
-
"light.living_room_main",
|
|
1058
|
-
"cover.living_room_blinds",
|
|
1059
|
-
"media_player.living_room_speaker"
|
|
1060
|
-
] }), { area_id: "living_room" });
|
|
1061
|
-
add(makeEntity("scene.good_morning", "scening", { entity_id: [
|
|
1062
|
-
"light.bedroom_ceiling",
|
|
1063
|
-
"cover.bedroom_curtains",
|
|
1064
|
-
"switch.coffee_machine"
|
|
1065
|
-
] }), {});
|
|
1066
|
-
add(makeEntity("button.restart_home_assistant", "unknown", { device_class: "restart" }), {
|
|
1067
|
-
entity_category: "config",
|
|
1068
|
-
device_class: "restart"
|
|
1069
|
-
});
|
|
1070
|
-
add(makeEntity("button.update_firmware", "unknown", { device_class: "update" }), {
|
|
1071
|
-
entity_category: "config",
|
|
1072
|
-
device_class: "update"
|
|
1073
|
-
});
|
|
1074
|
-
add(makeEntity("sensor.door_lock_battery", "92", {
|
|
1075
|
-
unit_of_measurement: "%",
|
|
1076
|
-
device_class: "battery",
|
|
1077
|
-
state_class: "measurement"
|
|
1078
|
-
}), {
|
|
1079
|
-
device_id: "lock_front",
|
|
1080
|
-
area_id: "entry",
|
|
1081
|
-
device_class: "battery",
|
|
1082
|
-
unit_of_measurement: "%"
|
|
1083
|
-
});
|
|
1084
|
-
add(makeEntity("sensor.motion_sensor_battery", "65", {
|
|
1085
|
-
unit_of_measurement: "%",
|
|
1086
|
-
device_class: "battery",
|
|
1087
|
-
state_class: "measurement"
|
|
1088
|
-
}), {
|
|
1089
|
-
device_id: "motion_sensor",
|
|
1090
|
-
area_id: "entry",
|
|
1091
|
-
device_class: "battery",
|
|
1092
|
-
unit_of_measurement: "%"
|
|
1093
|
-
});
|
|
1094
|
-
const nowMs = Date.now();
|
|
1095
|
-
const sample = simulateEnergy(nowMs);
|
|
1096
|
-
const sun = sunEvents(nowMs);
|
|
1097
|
-
add(makeEntity("sun.sun", isSunUp(nowMs) ? "above_horizon" : "below_horizon", {
|
|
1098
|
-
friendly_name: "Sun",
|
|
1099
|
-
elevation: sun.elevation,
|
|
1100
|
-
next_rising: sun.nextRising,
|
|
1101
|
-
next_setting: sun.nextSetting
|
|
1102
|
-
}), {});
|
|
1103
|
-
const powerSensors = [
|
|
1104
|
-
{
|
|
1105
|
-
id: "sensor.solar_power",
|
|
1106
|
-
name: "Solar Production",
|
|
1107
|
-
deviceClass: "power",
|
|
1108
|
-
unit: "W"
|
|
1109
|
-
},
|
|
1110
|
-
{
|
|
1111
|
-
id: "sensor.grid_import_power",
|
|
1112
|
-
name: "Grid Import",
|
|
1113
|
-
deviceClass: "power",
|
|
1114
|
-
unit: "W"
|
|
1115
|
-
},
|
|
1116
|
-
{
|
|
1117
|
-
id: "sensor.grid_export_power",
|
|
1118
|
-
name: "Grid Export",
|
|
1119
|
-
deviceClass: "power",
|
|
1120
|
-
unit: "W"
|
|
1121
|
-
},
|
|
1122
|
-
{
|
|
1123
|
-
id: "sensor.battery_charge_power",
|
|
1124
|
-
name: "Battery Charge",
|
|
1125
|
-
deviceClass: "power",
|
|
1126
|
-
unit: "W"
|
|
1127
|
-
},
|
|
1128
|
-
{
|
|
1129
|
-
id: "sensor.battery_discharge_power",
|
|
1130
|
-
name: "Battery Discharge",
|
|
1131
|
-
deviceClass: "power",
|
|
1132
|
-
unit: "W"
|
|
1133
|
-
},
|
|
1134
|
-
{
|
|
1135
|
-
id: "sensor.battery_soc",
|
|
1136
|
-
name: "Battery",
|
|
1137
|
-
deviceClass: "battery",
|
|
1138
|
-
unit: "%"
|
|
1139
|
-
},
|
|
1140
|
-
{
|
|
1141
|
-
id: "sensor.home_power",
|
|
1142
|
-
name: "Home Consumption",
|
|
1143
|
-
deviceClass: "power",
|
|
1144
|
-
unit: "W"
|
|
1145
|
-
},
|
|
1146
|
-
{
|
|
1147
|
-
id: "sensor.fridge_power",
|
|
1148
|
-
name: "Fridge",
|
|
1149
|
-
deviceClass: "power",
|
|
1150
|
-
unit: "W"
|
|
1151
|
-
},
|
|
1152
|
-
{
|
|
1153
|
-
id: "sensor.dishwasher_power",
|
|
1154
|
-
name: "Dishwasher",
|
|
1155
|
-
deviceClass: "power",
|
|
1156
|
-
unit: "W"
|
|
1157
|
-
},
|
|
1158
|
-
{
|
|
1159
|
-
id: "sensor.washing_machine_power",
|
|
1160
|
-
name: "Washing Machine",
|
|
1161
|
-
deviceClass: "power",
|
|
1162
|
-
unit: "W"
|
|
1163
|
-
},
|
|
1164
|
-
{
|
|
1165
|
-
id: "sensor.oven_power",
|
|
1166
|
-
name: "Oven",
|
|
1167
|
-
deviceClass: "power",
|
|
1168
|
-
unit: "W"
|
|
1169
|
-
},
|
|
1170
|
-
{
|
|
1171
|
-
id: "sensor.ev_charger_power",
|
|
1172
|
-
name: "EV Charger",
|
|
1173
|
-
deviceClass: "power",
|
|
1174
|
-
unit: "W"
|
|
1175
|
-
},
|
|
1176
|
-
{
|
|
1177
|
-
id: "sensor.always_on_power",
|
|
1178
|
-
name: "Always On",
|
|
1179
|
-
deviceClass: "power",
|
|
1180
|
-
unit: "W"
|
|
1181
|
-
}
|
|
1182
|
-
];
|
|
1183
|
-
for (const s of powerSensors) {
|
|
1184
|
-
const value = energyEntityValue(s.id, sample) ?? 0;
|
|
1185
|
-
add(makeEntity(s.id, formatEnergyState(s.id, value), {
|
|
1186
|
-
friendly_name: s.name,
|
|
1187
|
-
unit_of_measurement: s.unit,
|
|
1188
|
-
device_class: s.deviceClass,
|
|
1189
|
-
state_class: "measurement"
|
|
1190
|
-
}), {
|
|
1191
|
-
area_id: "utility",
|
|
1192
|
-
device_class: s.deviceClass,
|
|
1193
|
-
unit_of_measurement: s.unit
|
|
1194
|
-
});
|
|
1195
|
-
}
|
|
1196
|
-
return {
|
|
1197
|
-
entities,
|
|
1198
|
-
entityRegistry,
|
|
1199
|
-
areas,
|
|
1200
|
-
devices
|
|
1201
|
-
};
|
|
1202
|
-
}
|
|
1203
|
-
|
|
1204
|
-
//#endregion
|
|
1205
|
-
export { ENERGY_ENTITY_IDS, createDemoFixtures, demoEnergyPreferences, energyEntityValue, formatEnergyState, isEnergyEntity, isSunUp, resetStore, setState, simulateEnergy, state, sunEvents, synthesizeEnergyHistory, synthesizeEnergyStatistics };
|