@glasshome/sync-layer 0.1.14 → 0.1.15

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.
@@ -1,608 +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
- entities: {},
10
- entityRegistry: {},
11
- devices: {},
12
- areas: {},
13
- floors: {},
14
- labels: {},
15
- statistics: {},
16
- statisticsMetadata: {},
17
- history: {},
18
- forecasts: {},
19
- streams: {}
20
- };
21
- 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.");
22
- globalThis.__GH_SYNC_LAYER_STORE__ = true;
23
- const storeTuple = createStore(initialState);
24
- const state = storeTuple[0];
25
- const setState = storeTuple[1];
26
- /**
27
- * Reset store to initial state
28
- */
29
- function resetStore() {
30
- setState(reconcile(initialState));
31
- }
32
-
33
- //#endregion
34
- //#region src/demo/demo-data.ts
35
- const now = (/* @__PURE__ */ new Date()).toISOString();
36
- let contextCounter = 0;
37
- function ctx() {
38
- return {
39
- id: `demo-${++contextCounter}`,
40
- parent_id: null,
41
- user_id: null
42
- };
43
- }
44
- function makeEntity(entity_id, state$1, attributes = {}) {
45
- return {
46
- entity_id,
47
- state: state$1,
48
- attributes: {
49
- friendly_name: friendlyName(entity_id),
50
- ...attributes
51
- },
52
- last_changed: now,
53
- last_updated: now,
54
- context: ctx()
55
- };
56
- }
57
- /**
58
- * One entity per weather scene the widget can render. Entity IDs use the
59
- * `weather.demo_<slug>` form so a demo dashboard can reference them directly.
60
- */
61
- const WEATHER_FIXTURES = [
62
- {
63
- slug: "sunny",
64
- state: "sunny",
65
- temp: 28,
66
- apparent: 31,
67
- humidity: 35,
68
- pressure: 1018,
69
- wind: 8,
70
- low: 18
71
- },
72
- {
73
- slug: "clear_night",
74
- state: "clear-night",
75
- temp: 14,
76
- apparent: 12,
77
- humidity: 55,
78
- pressure: 1016,
79
- wind: 5,
80
- low: 9
81
- },
82
- {
83
- slug: "cloudy",
84
- state: "cloudy",
85
- temp: 17,
86
- apparent: 16,
87
- humidity: 72,
88
- pressure: 1010,
89
- wind: 14,
90
- low: 11
91
- },
92
- {
93
- slug: "partly_cloudy",
94
- state: "partlycloudy",
95
- temp: 23,
96
- apparent: 24,
97
- humidity: 50,
98
- pressure: 1014,
99
- wind: 12,
100
- low: 15
101
- },
102
- {
103
- slug: "rainy",
104
- state: "rainy",
105
- temp: 12,
106
- apparent: 10,
107
- humidity: 88,
108
- pressure: 1004,
109
- wind: 18,
110
- low: 8
111
- },
112
- {
113
- slug: "pouring",
114
- state: "pouring",
115
- temp: 11,
116
- apparent: 8,
117
- humidity: 95,
118
- pressure: 998,
119
- wind: 26,
120
- low: 7
121
- },
122
- {
123
- slug: "snowy",
124
- state: "snowy",
125
- temp: -2,
126
- apparent: -6,
127
- humidity: 80,
128
- pressure: 1020,
129
- wind: 10,
130
- low: -7
131
- },
132
- {
133
- slug: "snowy_rainy",
134
- state: "snowy-rainy",
135
- temp: 1,
136
- apparent: -2,
137
- humidity: 92,
138
- pressure: 1006,
139
- wind: 16,
140
- low: -2
141
- },
142
- {
143
- slug: "lightning",
144
- state: "lightning",
145
- temp: 22,
146
- apparent: 24,
147
- humidity: 78,
148
- pressure: 1001,
149
- wind: 22,
150
- low: 17
151
- },
152
- {
153
- slug: "lightning_rainy",
154
- state: "lightning-rainy",
155
- temp: 19,
156
- apparent: 18,
157
- humidity: 90,
158
- pressure: 996,
159
- wind: 28,
160
- low: 14
161
- },
162
- {
163
- slug: "fog",
164
- state: "fog",
165
- temp: 8,
166
- apparent: 6,
167
- humidity: 98,
168
- pressure: 1015,
169
- wind: 4,
170
- low: 6
171
- },
172
- {
173
- slug: "hail",
174
- state: "hail",
175
- temp: 6,
176
- apparent: 3,
177
- humidity: 84,
178
- pressure: 1002,
179
- wind: 20,
180
- low: 1
181
- },
182
- {
183
- slug: "windy",
184
- state: "windy",
185
- temp: 18,
186
- apparent: 15,
187
- humidity: 60,
188
- pressure: 1009,
189
- wind: 42,
190
- low: 12
191
- },
192
- {
193
- slug: "exceptional",
194
- state: "exceptional",
195
- temp: 38,
196
- apparent: 44,
197
- humidity: 22,
198
- pressure: 1005,
199
- wind: 30,
200
- low: 28
201
- }
202
- ];
203
- function makeWeatherEntity(w) {
204
- const today = /* @__PURE__ */ new Date();
205
- const day = (n) => new Date(today.getTime() + n * 864e5).toISOString();
206
- return makeEntity(`weather.demo_${w.slug}`, w.state, {
207
- temperature: w.temp,
208
- temperature_unit: "°C",
209
- apparent_temperature: w.apparent ?? w.temp,
210
- humidity: w.humidity,
211
- pressure: w.pressure,
212
- pressure_unit: "hPa",
213
- wind_speed: w.wind,
214
- wind_speed_unit: "km/h",
215
- wind_bearing: w.bearing ?? 220,
216
- visibility: 10,
217
- visibility_unit: "km",
218
- uv_index: w.state === "sunny" ? 8 : 2,
219
- forecast: [
220
- {
221
- datetime: day(0),
222
- condition: w.state,
223
- temperature: w.temp,
224
- templow: w.low
225
- },
226
- {
227
- datetime: day(1),
228
- condition: "partlycloudy",
229
- temperature: w.temp + 2,
230
- templow: w.low
231
- },
232
- {
233
- datetime: day(2),
234
- condition: "cloudy",
235
- temperature: w.temp + 1,
236
- templow: w.low - 1
237
- },
238
- {
239
- datetime: day(3),
240
- condition: "sunny",
241
- temperature: w.temp + 4,
242
- templow: w.low + 1
243
- },
244
- {
245
- datetime: day(4),
246
- condition: "rainy",
247
- temperature: w.temp - 2,
248
- templow: w.low - 2
249
- }
250
- ]
251
- });
252
- }
253
- function friendlyName(entityId) {
254
- const objectId = entityId.split(".")[1] ?? entityId;
255
- return objectId.split("_").map((w) => w.charAt(0).toUpperCase() + w.slice(1)).join(" ");
256
- }
257
- function makeRegistryEntry(entity_id, overrides = {}) {
258
- const domain = entity_id.split(".")[0] ?? "";
259
- return {
260
- aliases: [],
261
- area_id: null,
262
- capabilities: null,
263
- categories: {},
264
- config_entry_id: "demo_config",
265
- config_subentry_id: null,
266
- created_at: now,
267
- device_class: null,
268
- device_id: null,
269
- disabled_by: null,
270
- entity_category: null,
271
- entity_id,
272
- has_entity_name: false,
273
- hidden_by: null,
274
- icon: null,
275
- id: `reg-${entity_id.replace(".", "-")}`,
276
- labels: [],
277
- modified_at: now,
278
- name: null,
279
- options: {},
280
- original_device_class: null,
281
- original_icon: null,
282
- original_name: null,
283
- platform: domain === "weather" ? "met" : "demo",
284
- previous_unique_id: null,
285
- suggested_object_id: null,
286
- supported_features: 0,
287
- translation_key: null,
288
- unique_id: `demo_${entity_id.replace(".", "_")}`,
289
- unit_of_measurement: null,
290
- ...overrides
291
- };
292
- }
293
- function makeDevice(id, name, areaId, manufacturer = "Demo", model = "Virtual") {
294
- return {
295
- id,
296
- name,
297
- name_by_user: null,
298
- manufacturer,
299
- model,
300
- model_id: null,
301
- sw_version: "1.0.0",
302
- hw_version: null,
303
- area_id: areaId,
304
- config_entries: ["demo_config"],
305
- config_entries_subentries: {},
306
- configuration_url: null,
307
- connections: [],
308
- created_at: now,
309
- disabled_by: null,
310
- entry_type: null,
311
- identifiers: [["demo", id]],
312
- labels: [],
313
- modified_at: now,
314
- primary_config_entry: "demo_config",
315
- serial_number: null,
316
- via_device_id: null
317
- };
318
- }
319
- function makeArea(id, name, floorId = "ground_floor") {
320
- return {
321
- id,
322
- name,
323
- normalized_name: name.toLowerCase().replace(/\s+/g, "_"),
324
- created_at: now,
325
- modified_at: now,
326
- aliases: [],
327
- floor_id: floorId,
328
- humidity_entity_id: null,
329
- icon: null,
330
- labels: [],
331
- picture: null,
332
- temperature_entity_id: null
333
- };
334
- }
335
- /**
336
- * Create ~30 curated apartment entity fixtures.
337
- *
338
- * Entities span lights, sensors, binary sensors, climate, covers,
339
- * locks, switches, media player, weather, camera, scenes, and buttons
340
- * across 4 areas: living_room, bedroom, kitchen, entry.
341
- */
342
- function createDemoFixtures() {
343
- contextCounter = 0;
344
- const areas = {
345
- living_room: makeArea("living_room", "Living Room"),
346
- bedroom: makeArea("bedroom", "Bedroom"),
347
- kitchen: makeArea("kitchen", "Kitchen"),
348
- entry: makeArea("entry", "Entry")
349
- };
350
- areas.living_room.temperature_entity_id = "sensor.temperature_living";
351
- areas.living_room.humidity_entity_id = "sensor.humidity_living";
352
- const devices = {
353
- living_room_lights: makeDevice("living_room_lights", "Living Room Lights", "living_room", "Philips", "Hue"),
354
- kitchen_lights: makeDevice("kitchen_lights", "Kitchen Lights", "kitchen", "IKEA", "Tradfri"),
355
- bedroom_lights: makeDevice("bedroom_lights", "Bedroom Lights", "bedroom", "Philips", "Hue"),
356
- hallway_lights: makeDevice("hallway_lights", "Hallway Lights", "entry", "IKEA", "Tradfri"),
357
- bathroom_lights: makeDevice("bathroom_lights", "Bathroom Lights", null),
358
- thermostat_lr: makeDevice("thermostat_lr", "Living Room Thermostat", "living_room", "Nest", "Learning Thermostat"),
359
- thermostat_br: makeDevice("thermostat_br", "Bedroom AC", "bedroom", "Daikin", "Split AC"),
360
- blinds_lr: makeDevice("blinds_lr", "Living Room Blinds", "living_room", "Somfy", "Roller Shade"),
361
- curtains_br: makeDevice("curtains_br", "Bedroom Curtains", "bedroom", "IKEA", "Fyrtur"),
362
- lock_front: makeDevice("lock_front", "Front Door Lock", "entry", "Yale", "Assure Lock 2"),
363
- lock_back: makeDevice("lock_back", "Back Door Lock", "entry", "August", "Smart Lock Pro"),
364
- speaker_lr: makeDevice("speaker_lr", "Living Room Speaker", "living_room", "Sonos", "One"),
365
- camera_front: makeDevice("camera_front", "Front Door Camera", "entry", "Ring", "Video Doorbell"),
366
- door_sensor: makeDevice("door_sensor", "Front Door Sensor", "entry", "Aqara", "Door Sensor"),
367
- motion_sensor: makeDevice("motion_sensor", "Hallway Motion Sensor", "entry", "Aqara", "Motion Sensor")
368
- };
369
- const entities = {};
370
- const entityRegistry = {};
371
- function add(e, regOverrides = {}) {
372
- entities[e.entity_id] = e;
373
- entityRegistry[e.entity_id] = makeRegistryEntry(e.entity_id, regOverrides);
374
- }
375
- add(makeEntity("light.living_room_main", "on", {
376
- brightness: 179,
377
- color_temp_kelvin: 3e3,
378
- color_mode: "color_temp",
379
- supported_color_modes: ["color_temp", "xy"]
380
- }), {
381
- device_id: "living_room_lights",
382
- area_id: "living_room",
383
- supported_features: 44
384
- });
385
- add(makeEntity("light.kitchen_counter", "off", { supported_color_modes: ["brightness"] }), {
386
- device_id: "kitchen_lights",
387
- area_id: "kitchen",
388
- supported_features: 1
389
- });
390
- add(makeEntity("light.bedroom_ceiling", "on", {
391
- brightness: 77,
392
- color_temp_kelvin: 2700,
393
- color_mode: "color_temp",
394
- supported_color_modes: ["color_temp"]
395
- }), {
396
- device_id: "bedroom_lights",
397
- area_id: "bedroom",
398
- supported_features: 44
399
- });
400
- add(makeEntity("light.bathroom", "off", { supported_color_modes: ["brightness"] }), { device_id: "bathroom_lights" });
401
- add(makeEntity("light.hallway", "on", {
402
- brightness: 128,
403
- color_mode: "brightness",
404
- supported_color_modes: ["brightness"]
405
- }), {
406
- device_id: "hallway_lights",
407
- area_id: "entry",
408
- supported_features: 1
409
- });
410
- add(makeEntity("sensor.temperature_living", "22.5", {
411
- unit_of_measurement: "°C",
412
- device_class: "temperature",
413
- state_class: "measurement"
414
- }), {
415
- area_id: "living_room",
416
- device_class: "temperature",
417
- unit_of_measurement: "°C"
418
- });
419
- add(makeEntity("sensor.humidity_living", "45", {
420
- unit_of_measurement: "%",
421
- device_class: "humidity",
422
- state_class: "measurement"
423
- }), {
424
- area_id: "living_room",
425
- device_class: "humidity",
426
- unit_of_measurement: "%"
427
- });
428
- add(makeEntity("sensor.temperature_outdoor", "-2", {
429
- unit_of_measurement: "°C",
430
- device_class: "temperature",
431
- state_class: "measurement"
432
- }), {
433
- device_class: "temperature",
434
- unit_of_measurement: "°C"
435
- });
436
- add(makeEntity("sensor.power_consumption", "1.2", {
437
- unit_of_measurement: "kW",
438
- device_class: "power",
439
- state_class: "measurement"
440
- }), {
441
- device_class: "power",
442
- unit_of_measurement: "kW"
443
- });
444
- add(makeEntity("sensor.battery_door_sensor", "87", {
445
- unit_of_measurement: "%",
446
- device_class: "battery",
447
- state_class: "measurement"
448
- }), {
449
- device_id: "door_sensor",
450
- area_id: "entry",
451
- device_class: "battery",
452
- unit_of_measurement: "%"
453
- });
454
- add(makeEntity("binary_sensor.front_door", "off", { device_class: "door" }), {
455
- device_id: "door_sensor",
456
- area_id: "entry",
457
- device_class: "door"
458
- });
459
- add(makeEntity("binary_sensor.motion_hallway", "off", { device_class: "motion" }), {
460
- device_id: "motion_sensor",
461
- area_id: "entry",
462
- device_class: "motion"
463
- });
464
- add(makeEntity("binary_sensor.window_bedroom", "off", { device_class: "window" }), {
465
- area_id: "bedroom",
466
- device_class: "window"
467
- });
468
- add(makeEntity("climate.living_room_thermostat", "heat", {
469
- temperature: 22,
470
- current_temperature: 21.8,
471
- hvac_modes: [
472
- "off",
473
- "heat",
474
- "cool",
475
- "auto"
476
- ],
477
- hvac_action: "heating",
478
- min_temp: 7,
479
- max_temp: 35
480
- }), {
481
- device_id: "thermostat_lr",
482
- area_id: "living_room",
483
- supported_features: 385
484
- });
485
- add(makeEntity("climate.bedroom_ac", "off", {
486
- temperature: 20,
487
- current_temperature: 22.1,
488
- hvac_modes: [
489
- "off",
490
- "cool",
491
- "heat",
492
- "dry",
493
- "fan_only"
494
- ],
495
- hvac_action: "idle",
496
- min_temp: 16,
497
- max_temp: 30
498
- }), {
499
- device_id: "thermostat_br",
500
- area_id: "bedroom",
501
- supported_features: 385
502
- });
503
- add(makeEntity("cover.living_room_blinds", "open", {
504
- current_position: 80,
505
- device_class: "shade"
506
- }), {
507
- device_id: "blinds_lr",
508
- area_id: "living_room",
509
- device_class: "shade",
510
- supported_features: 15
511
- });
512
- add(makeEntity("cover.bedroom_curtains", "closed", {
513
- current_position: 0,
514
- device_class: "curtain"
515
- }), {
516
- device_id: "curtains_br",
517
- area_id: "bedroom",
518
- device_class: "curtain",
519
- supported_features: 15
520
- });
521
- add(makeEntity("lock.front_door_lock", "locked", {}), {
522
- device_id: "lock_front",
523
- area_id: "entry",
524
- supported_features: 1
525
- });
526
- add(makeEntity("lock.back_door_lock", "locked", {}), {
527
- device_id: "lock_back",
528
- area_id: "entry",
529
- supported_features: 1
530
- });
531
- add(makeEntity("switch.coffee_machine", "off", { device_class: "outlet" }), {
532
- area_id: "kitchen",
533
- device_class: "outlet"
534
- });
535
- add(makeEntity("switch.fan_living_room", "on", { device_class: "switch" }), {
536
- area_id: "living_room",
537
- device_class: "switch"
538
- });
539
- add(makeEntity("media_player.living_room_speaker", "playing", {
540
- media_title: "Lo-fi Beats",
541
- media_artist: "Chill Station",
542
- media_content_type: "music",
543
- volume_level: .4,
544
- is_volume_muted: false,
545
- supported_features: 152461
546
- }), {
547
- device_id: "speaker_lr",
548
- area_id: "living_room",
549
- supported_features: 152461
550
- });
551
- for (const w of WEATHER_FIXTURES) add(makeWeatherEntity(w), { platform: "met" });
552
- add(makeEntity("camera.front_door_camera", "streaming", {
553
- entity_picture: "/api/camera_proxy/camera.front_door_camera",
554
- frontend_stream_type: "hls",
555
- access_token: "demo-token"
556
- }), {
557
- device_id: "camera_front",
558
- area_id: "entry",
559
- supported_features: 2
560
- });
561
- add(makeEntity("scene.movie_night", "scening", { entity_id: [
562
- "light.living_room_main",
563
- "cover.living_room_blinds",
564
- "media_player.living_room_speaker"
565
- ] }), { area_id: "living_room" });
566
- add(makeEntity("scene.good_morning", "scening", { entity_id: [
567
- "light.bedroom_ceiling",
568
- "cover.bedroom_curtains",
569
- "switch.coffee_machine"
570
- ] }), {});
571
- add(makeEntity("button.restart_home_assistant", "unknown", { device_class: "restart" }), {
572
- entity_category: "config",
573
- device_class: "restart"
574
- });
575
- add(makeEntity("button.update_firmware", "unknown", { device_class: "update" }), {
576
- entity_category: "config",
577
- device_class: "update"
578
- });
579
- add(makeEntity("sensor.door_lock_battery", "92", {
580
- unit_of_measurement: "%",
581
- device_class: "battery",
582
- state_class: "measurement"
583
- }), {
584
- device_id: "lock_front",
585
- area_id: "entry",
586
- device_class: "battery",
587
- unit_of_measurement: "%"
588
- });
589
- add(makeEntity("sensor.motion_sensor_battery", "65", {
590
- unit_of_measurement: "%",
591
- device_class: "battery",
592
- state_class: "measurement"
593
- }), {
594
- device_id: "motion_sensor",
595
- area_id: "entry",
596
- device_class: "battery",
597
- unit_of_measurement: "%"
598
- });
599
- return {
600
- entities,
601
- entityRegistry,
602
- areas,
603
- devices
604
- };
605
- }
606
-
607
- //#endregion
608
- export { createDemoFixtures, resetStore, setState, state };