preact-homeassistant 0.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.
package/dist/index.js ADDED
@@ -0,0 +1,459 @@
1
+ import { jsx, jsxs } from "preact/jsx-runtime";
2
+ import { createContext, render } from "preact";
3
+ import { useRef, useMemo, useState, useEffect, useContext } from "preact/hooks";
4
+ const CACHE_PREFIX = "preact-ha:";
5
+ const CACHE_EXPIRY_MS = 24 * 60 * 60 * 1e3;
6
+ function loadFromCache(key) {
7
+ try {
8
+ const raw = localStorage.getItem(`${CACHE_PREFIX}${key}`);
9
+ if (!raw) return void 0;
10
+ const entry = JSON.parse(raw);
11
+ if (Date.now() - entry.timestamp > CACHE_EXPIRY_MS) {
12
+ localStorage.removeItem(`${CACHE_PREFIX}${key}`);
13
+ return void 0;
14
+ }
15
+ return entry.data;
16
+ } catch {
17
+ return void 0;
18
+ }
19
+ }
20
+ function saveToCache(key, data) {
21
+ try {
22
+ const entry = { data, timestamp: Date.now() };
23
+ localStorage.setItem(`${CACHE_PREFIX}${key}`, JSON.stringify(entry));
24
+ } catch (e) {
25
+ console.warn("[preact-homeassistant cache] Failed to save:", e);
26
+ }
27
+ }
28
+ function useCallbackStable(callback) {
29
+ const callbackRef = useRef(callback);
30
+ callbackRef.current = callback;
31
+ const stableRef = useRef(null);
32
+ if (stableRef.current === null) {
33
+ stableRef.current = ((...args) => {
34
+ return callbackRef.current(...args);
35
+ });
36
+ }
37
+ return stableRef.current;
38
+ }
39
+ const HAContext = createContext(null);
40
+ function HAProvider({ hass, subscribeToEntity, children }) {
41
+ const hassRef = useRef(hass);
42
+ hassRef.current = hass;
43
+ const getHass = useCallbackStable(() => hassRef.current);
44
+ const store = useMemo(
45
+ () => ({
46
+ hass: hassRef.current,
47
+ getHass,
48
+ subscribeToEntity
49
+ }),
50
+ [getHass, subscribeToEntity]
51
+ );
52
+ return /* @__PURE__ */ jsx(HAContext.Provider, { value: store, children });
53
+ }
54
+ function useHAStore() {
55
+ const store = useContext(HAContext);
56
+ if (!store) {
57
+ throw new Error("useEntity/useHass must be used within an HAProvider");
58
+ }
59
+ return store;
60
+ }
61
+ function useEntity(entityId) {
62
+ const store = useHAStore();
63
+ const cacheKey = `entity:${entityId}`;
64
+ const [entity, setEntity] = useState(() => {
65
+ const current = store.hass?.states[entityId];
66
+ if (current) return current;
67
+ return loadFromCache(cacheKey);
68
+ });
69
+ useEffect(() => {
70
+ const unsubscribe = store.subscribeToEntity(entityId, (newEntity) => {
71
+ setEntity(newEntity);
72
+ saveToCache(cacheKey, newEntity);
73
+ });
74
+ return unsubscribe;
75
+ }, [entityId, store.subscribeToEntity, cacheKey]);
76
+ return entity;
77
+ }
78
+ function useHass() {
79
+ const store = useHAStore();
80
+ return { getHass: store.getHass };
81
+ }
82
+ function useCachedFetch(cacheKey, fetcher, deps) {
83
+ const [data, setData] = useState(() => loadFromCache(cacheKey));
84
+ const [isFresh, setIsFresh] = useState(false);
85
+ const [isFetching, setIsFetching] = useState(false);
86
+ const [error, setError] = useState(void 0);
87
+ const fetchIdRef = useRef(0);
88
+ const doFetch = useCallbackStable(async () => {
89
+ const fetchId = ++fetchIdRef.current;
90
+ setIsFetching(true);
91
+ setError(void 0);
92
+ try {
93
+ const result = await fetcher();
94
+ if (fetchId === fetchIdRef.current) {
95
+ setData(result);
96
+ setIsFresh(true);
97
+ saveToCache(cacheKey, result);
98
+ }
99
+ } catch (err) {
100
+ if (fetchId === fetchIdRef.current) {
101
+ setError(err instanceof Error ? err : new Error(String(err)));
102
+ }
103
+ } finally {
104
+ if (fetchId === fetchIdRef.current) {
105
+ setIsFetching(false);
106
+ }
107
+ }
108
+ });
109
+ useEffect(() => {
110
+ doFetch();
111
+ }, deps);
112
+ const status = useMemo(() => {
113
+ if (!data && isFetching) return "loading";
114
+ if (data && !isFresh && isFetching) return "cached";
115
+ if (data && isFresh && isFetching) return "refreshing";
116
+ return "ready";
117
+ }, [data, isFresh, isFetching]);
118
+ return { data, status, error, refetch: doFetch };
119
+ }
120
+ function useCalendarEvents(entityId, options) {
121
+ const { getHass } = useHass();
122
+ const [events, setEvents] = useState(void 0);
123
+ const [loading, setLoading] = useState(false);
124
+ const [error, setError] = useState(void 0);
125
+ const fetchIdRef = useRef(0);
126
+ const fetchEvents = useCallbackStable(async () => {
127
+ const hass = getHass();
128
+ if (!hass?.connection) {
129
+ setError(new Error("Home Assistant connection not available"));
130
+ return;
131
+ }
132
+ const fetchId = ++fetchIdRef.current;
133
+ setLoading(true);
134
+ setError(void 0);
135
+ try {
136
+ const result = await hass.connection.sendMessagePromise({
137
+ type: "call_service",
138
+ domain: "calendar",
139
+ service: "get_events",
140
+ service_data: {
141
+ start_date_time: options.start.toISOString(),
142
+ end_date_time: options.end.toISOString()
143
+ },
144
+ target: { entity_id: entityId },
145
+ return_response: true
146
+ });
147
+ if (fetchId === fetchIdRef.current) {
148
+ const entityEvents = result.response?.[entityId]?.events ?? [];
149
+ setEvents(entityEvents);
150
+ setLoading(false);
151
+ }
152
+ } catch (err) {
153
+ if (fetchId === fetchIdRef.current) {
154
+ setError(err instanceof Error ? err : new Error(String(err)));
155
+ setLoading(false);
156
+ }
157
+ }
158
+ });
159
+ useEffect(() => {
160
+ fetchEvents();
161
+ }, [entityId, options.start.getTime(), options.end.getTime(), fetchEvents]);
162
+ return { events, loading, error, refetch: fetchEvents };
163
+ }
164
+ function useMultiCalendarEvents(entityIds, options) {
165
+ const store = useHAStore();
166
+ const { getHass } = useHass();
167
+ const debounceTimerRef = useRef(null);
168
+ const entityIdsKey = entityIds.join(",");
169
+ const dateRangeKey = `${options.start.getTime()}-${options.end.getTime()}`;
170
+ const cacheKey = `events:${entityIdsKey}:${dateRangeKey}`;
171
+ const fetcher = useCallbackStable(async () => {
172
+ const hass = getHass();
173
+ if (!hass?.connection) {
174
+ throw new Error("Home Assistant connection not available");
175
+ }
176
+ if (entityIds.length === 0) {
177
+ return [];
178
+ }
179
+ const results = await Promise.all(
180
+ entityIds.map(async (entityId) => {
181
+ try {
182
+ const result = await hass.connection.sendMessagePromise({
183
+ type: "call_service",
184
+ domain: "calendar",
185
+ service: "get_events",
186
+ service_data: {
187
+ start_date_time: options.start.toISOString(),
188
+ end_date_time: options.end.toISOString()
189
+ },
190
+ target: { entity_id: entityId },
191
+ return_response: true
192
+ });
193
+ const calendarEvents = result.response?.[entityId]?.events ?? [];
194
+ return calendarEvents.map(
195
+ (event) => ({ ...event, calendarId: entityId })
196
+ );
197
+ } catch (err) {
198
+ console.error(`Failed to fetch events for ${entityId}:`, err);
199
+ return [];
200
+ }
201
+ })
202
+ );
203
+ return results.flat();
204
+ });
205
+ const {
206
+ data: events,
207
+ status,
208
+ error,
209
+ refetch
210
+ } = useCachedFetch(cacheKey, fetcher, [entityIdsKey, dateRangeKey]);
211
+ const debouncedRefetch = useCallbackStable(() => {
212
+ if (debounceTimerRef.current) {
213
+ clearTimeout(debounceTimerRef.current);
214
+ }
215
+ debounceTimerRef.current = setTimeout(() => refetch(), 500);
216
+ });
217
+ useEffect(() => {
218
+ const unsubscribes = entityIds.map(
219
+ (entityId) => store.subscribeToEntity(entityId, debouncedRefetch)
220
+ );
221
+ return () => {
222
+ unsubscribes.forEach((unsub) => unsub());
223
+ if (debounceTimerRef.current) {
224
+ clearTimeout(debounceTimerRef.current);
225
+ }
226
+ };
227
+ }, [entityIdsKey, store.subscribeToEntity, debouncedRefetch]);
228
+ return { events, status, error, refetch };
229
+ }
230
+ function useWeatherForecast(entityId, type) {
231
+ const store = useHAStore();
232
+ const { getHass } = useHass();
233
+ const cacheKey = `forecast:${entityId}:${type}`;
234
+ const debounceTimerRef = useRef(null);
235
+ const hourlyTimerRef = useRef(null);
236
+ const fetcher = useCallbackStable(async () => {
237
+ const hass = getHass();
238
+ if (!hass?.connection) {
239
+ throw new Error("Home Assistant connection not available");
240
+ }
241
+ const result = await hass.connection.sendMessagePromise({
242
+ type: "call_service",
243
+ domain: "weather",
244
+ service: "get_forecasts",
245
+ service_data: { type },
246
+ target: { entity_id: entityId },
247
+ return_response: true
248
+ });
249
+ return result.response?.[entityId]?.forecast ?? [];
250
+ });
251
+ const {
252
+ data: forecast,
253
+ status,
254
+ error,
255
+ refetch
256
+ } = useCachedFetch(cacheKey, fetcher, [entityId, type]);
257
+ const debouncedRefetch = useCallbackStable(() => {
258
+ if (debounceTimerRef.current) {
259
+ clearTimeout(debounceTimerRef.current);
260
+ }
261
+ debounceTimerRef.current = setTimeout(() => refetch(), 500);
262
+ });
263
+ const scheduleHourlyRefetch = useCallbackStable(() => {
264
+ if (hourlyTimerRef.current) {
265
+ clearTimeout(hourlyTimerRef.current);
266
+ }
267
+ const now = /* @__PURE__ */ new Date();
268
+ const nextHour = new Date(now);
269
+ nextHour.setHours(now.getHours() + 1, 0, 0, 0);
270
+ const msUntilNextHour = nextHour.getTime() - now.getTime();
271
+ hourlyTimerRef.current = setTimeout(() => {
272
+ refetch();
273
+ scheduleHourlyRefetch();
274
+ }, msUntilNextHour);
275
+ });
276
+ useEffect(() => {
277
+ scheduleHourlyRefetch();
278
+ }, [entityId, type, scheduleHourlyRefetch]);
279
+ useEffect(() => {
280
+ const unsubscribe = store.subscribeToEntity(entityId, debouncedRefetch);
281
+ return () => {
282
+ unsubscribe();
283
+ if (debounceTimerRef.current) {
284
+ clearTimeout(debounceTimerRef.current);
285
+ }
286
+ if (hourlyTimerRef.current) {
287
+ clearTimeout(hourlyTimerRef.current);
288
+ }
289
+ };
290
+ }, [entityId, store.subscribeToEntity, debouncedRefetch]);
291
+ return { forecast, status, error, refetch };
292
+ }
293
+ const styleRegistry = [];
294
+ const css = (strings, ...values) => {
295
+ const result = strings.reduce((acc, str, i) => acc + str + (values[i] ?? ""), "");
296
+ styleRegistry.push(result);
297
+ return result;
298
+ };
299
+ function registerRawStyles(styles) {
300
+ if (!styleRegistry.includes(styles)) {
301
+ styleRegistry.push(styles);
302
+ }
303
+ }
304
+ function getAllStyles() {
305
+ return styleRegistry.join("\n");
306
+ }
307
+ function registerPreactCard(options) {
308
+ const {
309
+ type,
310
+ name,
311
+ description,
312
+ Component,
313
+ ConfigComponent,
314
+ UnconfiguredComponent,
315
+ getStubConfig
316
+ } = options;
317
+ class HACard extends HTMLElement {
318
+ _hass;
319
+ _config;
320
+ _shadowRoot;
321
+ _hasRendered = false;
322
+ _entityChangeListeners = /* @__PURE__ */ new Map();
323
+ constructor() {
324
+ super();
325
+ this._shadowRoot = this.attachShadow({ mode: "open" });
326
+ }
327
+ connectedCallback() {
328
+ if (this._hass && this._config && !this._hasRendered) {
329
+ this._render();
330
+ }
331
+ }
332
+ disconnectedCallback() {
333
+ this._entityChangeListeners.clear();
334
+ }
335
+ set hass(hass) {
336
+ const prevStates = this._hass?.states;
337
+ this._hass = hass;
338
+ for (const [entityId, listeners] of this._entityChangeListeners) {
339
+ const newState = hass.states[entityId];
340
+ const oldState = prevStates?.[entityId];
341
+ if (newState !== oldState) {
342
+ listeners.forEach((listener) => listener(newState));
343
+ }
344
+ }
345
+ if (!prevStates && this._config && this.isConnected) {
346
+ this._render();
347
+ }
348
+ }
349
+ setConfig(config) {
350
+ this._config = config;
351
+ if (this._hass && this.isConnected) {
352
+ this._render();
353
+ }
354
+ }
355
+ _subscribeToEntity = (entityId, callback) => {
356
+ if (!this._entityChangeListeners.has(entityId)) {
357
+ this._entityChangeListeners.set(entityId, /* @__PURE__ */ new Set());
358
+ }
359
+ this._entityChangeListeners.get(entityId).add(callback);
360
+ return () => {
361
+ const listeners = this._entityChangeListeners.get(entityId);
362
+ if (listeners) {
363
+ listeners.delete(callback);
364
+ if (listeners.size === 0) {
365
+ this._entityChangeListeners.delete(entityId);
366
+ }
367
+ }
368
+ };
369
+ };
370
+ _render() {
371
+ if (!this._config || !this._hass) {
372
+ if (UnconfiguredComponent) {
373
+ render(/* @__PURE__ */ jsx(UnconfiguredComponent, {}), this._shadowRoot);
374
+ }
375
+ return;
376
+ }
377
+ render(
378
+ /* @__PURE__ */ jsxs(HAProvider, { hass: this._hass, subscribeToEntity: this._subscribeToEntity, children: [
379
+ /* @__PURE__ */ jsx("style", { children: getAllStyles() }),
380
+ /* @__PURE__ */ jsx(Component, { config: this._config })
381
+ ] }),
382
+ this._shadowRoot
383
+ );
384
+ this._hasRendered = true;
385
+ }
386
+ static getConfigElement() {
387
+ if (ConfigComponent) {
388
+ return document.createElement(`${type}-editor`);
389
+ }
390
+ return void 0;
391
+ }
392
+ static getStubConfig() {
393
+ return getStubConfig?.() ?? {};
394
+ }
395
+ }
396
+ customElements.define(type, HACard);
397
+ if (ConfigComponent) {
398
+ const EditorComponent = ConfigComponent;
399
+ class HACardEditor extends HTMLElement {
400
+ _hass;
401
+ _config;
402
+ set hass(hass) {
403
+ this._hass = hass;
404
+ this._render();
405
+ }
406
+ setConfig(config) {
407
+ this._config = config;
408
+ this._render();
409
+ }
410
+ _fireConfigChanged = (config) => {
411
+ this.dispatchEvent(
412
+ new CustomEvent("config-changed", {
413
+ detail: { config },
414
+ bubbles: true,
415
+ composed: true
416
+ })
417
+ );
418
+ };
419
+ _render() {
420
+ if (!this._hass || !this._config) return;
421
+ render(
422
+ /* @__PURE__ */ jsx(
423
+ EditorComponent,
424
+ {
425
+ hass: this._hass,
426
+ config: this._config,
427
+ onConfigChanged: this._fireConfigChanged
428
+ }
429
+ ),
430
+ this
431
+ );
432
+ }
433
+ }
434
+ customElements.define(`${type}-editor`, HACardEditor);
435
+ }
436
+ window.customCards = window.customCards || [];
437
+ window.customCards.push({ type, name, description });
438
+ console.info(
439
+ `%c ${name.toUpperCase()} %c loaded `,
440
+ "background: #3b82f6; color: white; font-weight: bold",
441
+ ""
442
+ );
443
+ }
444
+ export {
445
+ HAProvider,
446
+ css,
447
+ loadFromCache,
448
+ registerPreactCard,
449
+ registerRawStyles,
450
+ saveToCache,
451
+ useCachedFetch,
452
+ useCalendarEvents,
453
+ useCallbackStable,
454
+ useEntity,
455
+ useHass,
456
+ useMultiCalendarEvents,
457
+ useWeatherForecast
458
+ };
459
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sources":["../src/cacheUtils.ts","../src/useCallbackStable.ts","../src/HAContext.tsx","../src/styleRegistry.ts","../src/registerPreactCard.tsx"],"sourcesContent":["const CACHE_PREFIX = 'preact-ha:';\nconst CACHE_EXPIRY_MS = 24 * 60 * 60 * 1000;\n\ninterface CacheEntry<T> {\n data: T;\n timestamp: number;\n}\n\nexport function loadFromCache<T>(key: string): T | undefined {\n try {\n const raw = localStorage.getItem(`${CACHE_PREFIX}${key}`);\n if (!raw) return undefined;\n\n const entry: CacheEntry<T> = JSON.parse(raw);\n if (Date.now() - entry.timestamp > CACHE_EXPIRY_MS) {\n localStorage.removeItem(`${CACHE_PREFIX}${key}`);\n return undefined;\n }\n return entry.data;\n } catch {\n return undefined;\n }\n}\n\nexport function saveToCache<T>(key: string, data: T): void {\n try {\n const entry: CacheEntry<T> = { data, timestamp: Date.now() };\n localStorage.setItem(`${CACHE_PREFIX}${key}`, JSON.stringify(entry));\n } catch (e) {\n console.warn('[preact-homeassistant cache] Failed to save:', e);\n }\n}\n","import { useRef } from 'preact/hooks';\n\n/**\n * Creates a stable callback reference that always calls the latest version of the callback.\n * Unlike useCallback, this never changes identity, so it won't cause re-renders in children.\n *\n * @param callback The callback function to stabilize\n * @returns A stable function reference that always calls the latest callback\n */\nexport function useCallbackStable<T extends (...args: never[]) => unknown>(callback: T): T {\n const callbackRef = useRef<T>(callback);\n\n callbackRef.current = callback;\n\n // Create a stable function reference once\n const stableRef = useRef<T | null>(null);\n if (stableRef.current === null) {\n stableRef.current = ((...args: Parameters<T>) => {\n return callbackRef.current(...args);\n }) as T;\n }\n\n return stableRef.current;\n}\n","import { createContext } from 'preact';\nimport type { ComponentChildren } from 'preact';\nimport { useContext, useEffect, useMemo, useRef, useState } from 'preact/hooks';\n\nimport { loadFromCache, saveToCache } from './cacheUtils';\nimport type {\n CalendarEvent,\n CalendarEventWithSource,\n EntityForId,\n FetchStatus,\n ForecastType,\n HomeAssistant,\n WeatherForecast,\n} from './types';\nimport { useCallbackStable } from './useCallbackStable';\n\ninterface HAStore {\n hass: HomeAssistant | undefined;\n getHass: () => HomeAssistant | undefined;\n subscribeToEntity: (entityId: string, callback: (entity: any) => void) => () => void;\n}\n\nconst HAContext = createContext<HAStore | null>(null);\n\ninterface HAProviderProps {\n hass: HomeAssistant | undefined;\n subscribeToEntity: (entityId: string, callback: (entity: any) => void) => () => void;\n children: ComponentChildren;\n}\n\nexport function HAProvider({ hass, subscribeToEntity, children }: HAProviderProps) {\n const hassRef = useRef(hass);\n hassRef.current = hass;\n\n const getHass = useCallbackStable(() => hassRef.current);\n\n const store = useMemo<HAStore>(\n () => ({\n hass: hassRef.current,\n getHass,\n subscribeToEntity,\n }),\n [getHass, subscribeToEntity],\n );\n\n return <HAContext.Provider value={store}>{children}</HAContext.Provider>;\n}\n\nfunction useHAStore(): HAStore {\n const store = useContext(HAContext);\n if (!store) {\n throw new Error('useEntity/useHass must be used within an HAProvider');\n }\n return store;\n}\n\n/**\n * Subscribe to a specific entity by ID. Re-renders only when that entity changes.\n *\n * Returns a typed entity based on the domain prefix:\n * - 'calendar.xyz' -> CalendarEntity\n * - 'weather.xyz' -> WeatherEntity\n * - 'sun.sun' -> SunEntity\n * - other domains -> HassEntity (fallback)\n */\nexport function useEntity<T extends string>(entityId: T): EntityForId<T> | undefined {\n const store = useHAStore();\n const cacheKey = `entity:${entityId}`;\n\n const [entity, setEntity] = useState<EntityForId<T> | undefined>(() => {\n const current = store.hass?.states[entityId] as EntityForId<T> | undefined;\n if (current) return current;\n return loadFromCache<EntityForId<T>>(cacheKey);\n });\n\n useEffect(() => {\n const unsubscribe = store.subscribeToEntity(entityId, (newEntity) => {\n setEntity(newEntity as EntityForId<T>);\n saveToCache(cacheKey, newEntity);\n });\n return unsubscribe;\n }, [entityId, store.subscribeToEntity, cacheKey]);\n\n return entity;\n}\n\n/**\n * Get access to the full hass object for calling services / accessing config.\n * Does NOT re-render on entity changes. Use useEntity for that.\n */\nexport function useHass(): { getHass: () => HomeAssistant | undefined } {\n const store = useHAStore();\n return { getHass: store.getHass };\n}\n\ninterface UseCachedFetchResult<T> {\n data: T | undefined;\n status: FetchStatus;\n error: Error | undefined;\n refetch: () => void;\n}\n\n/**\n * Generic hook for fetching data with localStorage caching. Returns a cache-aware\n * status string to distinguish cached vs fresh data.\n */\nexport function useCachedFetch<T>(\n cacheKey: string,\n fetcher: () => Promise<T>,\n deps: unknown[],\n): UseCachedFetchResult<T> {\n const [data, setData] = useState<T | undefined>(() => loadFromCache<T>(cacheKey));\n const [isFresh, setIsFresh] = useState(false);\n const [isFetching, setIsFetching] = useState(false);\n const [error, setError] = useState<Error | undefined>(undefined);\n\n const fetchIdRef = useRef(0);\n\n const doFetch = useCallbackStable(async () => {\n const fetchId = ++fetchIdRef.current;\n setIsFetching(true);\n setError(undefined);\n\n try {\n const result = await fetcher();\n if (fetchId === fetchIdRef.current) {\n setData(result);\n setIsFresh(true);\n saveToCache(cacheKey, result);\n }\n } catch (err) {\n if (fetchId === fetchIdRef.current) {\n setError(err instanceof Error ? err : new Error(String(err)));\n }\n } finally {\n if (fetchId === fetchIdRef.current) {\n setIsFetching(false);\n }\n }\n });\n\n useEffect(() => {\n doFetch();\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, deps);\n\n const status: FetchStatus = useMemo(() => {\n if (!data && isFetching) return 'loading';\n if (data && !isFresh && isFetching) return 'cached';\n if (data && isFresh && isFetching) return 'refreshing';\n return 'ready';\n }, [data, isFresh, isFetching]);\n\n return { data, status, error, refetch: doFetch };\n}\n\ninterface UseCalendarEventsResult {\n events: CalendarEvent[] | undefined;\n loading: boolean;\n error: Error | undefined;\n refetch: () => void;\n}\n\n/**\n * Fetch calendar events for a date range from a single calendar.\n */\nexport function useCalendarEvents(\n entityId: `calendar.${string}`,\n options: { start: Date; end: Date },\n): UseCalendarEventsResult {\n const { getHass } = useHass();\n const [events, setEvents] = useState<CalendarEvent[] | undefined>(undefined);\n const [loading, setLoading] = useState(false);\n const [error, setError] = useState<Error | undefined>(undefined);\n\n const fetchIdRef = useRef(0);\n\n const fetchEvents = useCallbackStable(async () => {\n const hass = getHass();\n if (!hass?.connection) {\n setError(new Error('Home Assistant connection not available'));\n return;\n }\n\n const fetchId = ++fetchIdRef.current;\n setLoading(true);\n setError(undefined);\n\n try {\n const result = await hass.connection.sendMessagePromise<{\n response: { [entityId: string]: { events: CalendarEvent[] } };\n }>({\n type: 'call_service',\n domain: 'calendar',\n service: 'get_events',\n service_data: {\n start_date_time: options.start.toISOString(),\n end_date_time: options.end.toISOString(),\n },\n target: { entity_id: entityId },\n return_response: true,\n });\n\n if (fetchId === fetchIdRef.current) {\n const entityEvents = result.response?.[entityId]?.events ?? [];\n setEvents(entityEvents);\n setLoading(false);\n }\n } catch (err) {\n if (fetchId === fetchIdRef.current) {\n setError(err instanceof Error ? err : new Error(String(err)));\n setLoading(false);\n }\n }\n });\n\n useEffect(() => {\n fetchEvents();\n }, [entityId, options.start.getTime(), options.end.getTime(), fetchEvents]);\n\n return { events, loading, error, refetch: fetchEvents };\n}\n\ninterface UseMultiCalendarEventsResult {\n events: CalendarEventWithSource[] | undefined;\n status: FetchStatus;\n error: Error | undefined;\n refetch: () => void;\n}\n\n/**\n * Fetch events from multiple calendars for a date range, with localStorage\n * caching. Events are tagged with their source calendar ID.\n */\nexport function useMultiCalendarEvents(\n entityIds: `calendar.${string}`[],\n options: { start: Date; end: Date },\n): UseMultiCalendarEventsResult {\n const store = useHAStore();\n const { getHass } = useHass();\n\n const debounceTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);\n\n const entityIdsKey = entityIds.join(',');\n const dateRangeKey = `${options.start.getTime()}-${options.end.getTime()}`;\n const cacheKey = `events:${entityIdsKey}:${dateRangeKey}`;\n\n const fetcher = useCallbackStable(async () => {\n const hass = getHass();\n if (!hass?.connection) {\n throw new Error('Home Assistant connection not available');\n }\n\n if (entityIds.length === 0) {\n return [];\n }\n\n const results = await Promise.all(\n entityIds.map(async (entityId) => {\n try {\n const result = await hass.connection.sendMessagePromise<{\n response: { [key: string]: { events: CalendarEvent[] } };\n }>({\n type: 'call_service',\n domain: 'calendar',\n service: 'get_events',\n service_data: {\n start_date_time: options.start.toISOString(),\n end_date_time: options.end.toISOString(),\n },\n target: { entity_id: entityId },\n return_response: true,\n });\n\n const calendarEvents = result.response?.[entityId]?.events ?? [];\n return calendarEvents.map(\n (event): CalendarEventWithSource => ({ ...event, calendarId: entityId }),\n );\n } catch (err) {\n console.error(`Failed to fetch events for ${entityId}:`, err);\n return [];\n }\n }),\n );\n\n return results.flat();\n });\n\n const {\n data: events,\n status,\n error,\n refetch,\n } = useCachedFetch(cacheKey, fetcher, [entityIdsKey, dateRangeKey]);\n\n const debouncedRefetch = useCallbackStable(() => {\n if (debounceTimerRef.current) {\n clearTimeout(debounceTimerRef.current);\n }\n debounceTimerRef.current = setTimeout(() => refetch(), 500);\n });\n\n useEffect(() => {\n const unsubscribes = entityIds.map((entityId) =>\n store.subscribeToEntity(entityId, debouncedRefetch),\n );\n return () => {\n unsubscribes.forEach((unsub) => unsub());\n if (debounceTimerRef.current) {\n clearTimeout(debounceTimerRef.current);\n }\n };\n }, [entityIdsKey, store.subscribeToEntity, debouncedRefetch]);\n\n return { events, status, error, refetch };\n}\n\ninterface UseWeatherForecastResult {\n forecast: WeatherForecast[] | undefined;\n status: FetchStatus;\n error: Error | undefined;\n refetch: () => void;\n}\n\n/**\n * Fetch weather forecast data with localStorage caching. Auto-refetches at the\n * top of each hour and when the underlying entity changes (debounced).\n */\nexport function useWeatherForecast(\n entityId: `weather.${string}`,\n type: ForecastType,\n): UseWeatherForecastResult {\n const store = useHAStore();\n const { getHass } = useHass();\n const cacheKey = `forecast:${entityId}:${type}`;\n\n const debounceTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);\n const hourlyTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);\n\n const fetcher = useCallbackStable(async () => {\n const hass = getHass();\n if (!hass?.connection) {\n throw new Error('Home Assistant connection not available');\n }\n\n const result = await hass.connection.sendMessagePromise<{\n response: { [entityId: string]: { forecast: WeatherForecast[] } };\n }>({\n type: 'call_service',\n domain: 'weather',\n service: 'get_forecasts',\n service_data: { type },\n target: { entity_id: entityId },\n return_response: true,\n });\n\n return result.response?.[entityId]?.forecast ?? [];\n });\n\n const {\n data: forecast,\n status,\n error,\n refetch,\n } = useCachedFetch(cacheKey, fetcher, [entityId, type]);\n\n const debouncedRefetch = useCallbackStable(() => {\n if (debounceTimerRef.current) {\n clearTimeout(debounceTimerRef.current);\n }\n debounceTimerRef.current = setTimeout(() => refetch(), 500);\n });\n\n const scheduleHourlyRefetch = useCallbackStable(() => {\n if (hourlyTimerRef.current) {\n clearTimeout(hourlyTimerRef.current);\n }\n const now = new Date();\n const nextHour = new Date(now);\n nextHour.setHours(now.getHours() + 1, 0, 0, 0);\n const msUntilNextHour = nextHour.getTime() - now.getTime();\n\n hourlyTimerRef.current = setTimeout(() => {\n refetch();\n scheduleHourlyRefetch();\n }, msUntilNextHour);\n });\n\n useEffect(() => {\n scheduleHourlyRefetch();\n }, [entityId, type, scheduleHourlyRefetch]);\n\n useEffect(() => {\n const unsubscribe = store.subscribeToEntity(entityId, debouncedRefetch);\n return () => {\n unsubscribe();\n if (debounceTimerRef.current) {\n clearTimeout(debounceTimerRef.current);\n }\n if (hourlyTimerRef.current) {\n clearTimeout(hourlyTimerRef.current);\n }\n };\n }, [entityId, store.subscribeToEntity, debouncedRefetch]);\n\n return { forecast, status, error, refetch };\n}\n","// Style registry for Shadow DOM injection\n// Each .styles.ts file uses css`` which auto-registers\n\nconst styleRegistry: string[] = [];\n\n/**\n * CSS tagged template literal for syntax highlighting.\n * Automatically registers the styles with the global registry.\n */\nexport const css = (strings: TemplateStringsArray, ...values: unknown[]): string => {\n const result = strings.reduce((acc, str, i) => acc + str + (values[i] ?? ''), '');\n styleRegistry.push(result);\n return result;\n};\n\n/**\n * Register raw CSS string (e.g., from ?inline imports).\n * Only registers if not already present.\n */\nexport function registerRawStyles(styles: string): void {\n if (!styleRegistry.includes(styles)) {\n styleRegistry.push(styles);\n }\n}\n\n/**\n * Get all registered styles for Shadow DOM injection.\n */\nexport function getAllStyles(): string {\n return styleRegistry.join('\\n');\n}\n","import { type ComponentType, render } from 'preact';\nimport { HAProvider } from './HAContext';\nimport { getAllStyles } from './styleRegistry';\nimport type { HomeAssistant } from './types';\n\ninterface RegisterPreactCardOptions<TConfig> {\n type: string;\n name: string;\n description: string;\n Component: ComponentType<{ config: TConfig }>;\n ConfigComponent?: ComponentType<{\n hass: HomeAssistant;\n config: TConfig;\n onConfigChanged: (config: TConfig) => void;\n }>;\n UnconfiguredComponent?: ComponentType<{}>;\n getStubConfig?: () => Partial<TConfig>;\n}\n\ndeclare global {\n interface Window {\n customCards?: Array<{ type: string; name: string; description: string }>;\n }\n}\n\nexport function registerPreactCard<TConfig>(options: RegisterPreactCardOptions<TConfig>) {\n const {\n type,\n name,\n description,\n Component,\n ConfigComponent,\n UnconfiguredComponent,\n getStubConfig,\n } = options;\n\n class HACard extends HTMLElement {\n private _hass?: HomeAssistant;\n private _config?: TConfig;\n private _shadowRoot: ShadowRoot;\n private _hasRendered = false;\n private _entityChangeListeners = new Map<string, Set<(entity: any) => void>>();\n\n constructor() {\n super();\n this._shadowRoot = this.attachShadow({ mode: 'open' });\n }\n\n connectedCallback() {\n // HA disconnects + reconnects the element during edit-mode toggling.\n // Re-rendering on each connect causes Preact to lose its diff anchor and\n // append a duplicate tree, so only render once per attachment cycle.\n if (this._hass && this._config && !this._hasRendered) {\n this._render();\n }\n }\n\n disconnectedCallback() {\n this._entityChangeListeners.clear();\n }\n\n set hass(hass: HomeAssistant) {\n const prevStates = this._hass?.states;\n this._hass = hass;\n\n for (const [entityId, listeners] of this._entityChangeListeners) {\n const newState = hass.states[entityId];\n const oldState = prevStates?.[entityId];\n if (newState !== oldState) {\n listeners.forEach((listener) => listener(newState));\n }\n }\n\n // Render only when attached. HA may set hass/config before insertion;\n // rendering into a detached shadow root then again on connect duplicates\n // the tree. connectedCallback handles the detached-first-render case.\n if (!prevStates && this._config && this.isConnected) {\n this._render();\n }\n }\n\n setConfig(config: TConfig) {\n this._config = config;\n if (this._hass && this.isConnected) {\n this._render();\n }\n }\n\n private _subscribeToEntity = (entityId: string, callback: (entity: any) => void) => {\n if (!this._entityChangeListeners.has(entityId)) {\n this._entityChangeListeners.set(entityId, new Set());\n }\n this._entityChangeListeners.get(entityId)!.add(callback);\n\n return () => {\n const listeners = this._entityChangeListeners.get(entityId);\n if (listeners) {\n listeners.delete(callback);\n if (listeners.size === 0) {\n this._entityChangeListeners.delete(entityId);\n }\n }\n };\n };\n\n private _render() {\n if (!this._config || !this._hass) {\n if (UnconfiguredComponent) {\n render(<UnconfiguredComponent />, this._shadowRoot);\n }\n return;\n }\n\n render(\n <HAProvider hass={this._hass} subscribeToEntity={this._subscribeToEntity}>\n <style>{getAllStyles()}</style>\n <Component config={this._config} />\n </HAProvider>,\n this._shadowRoot,\n );\n this._hasRendered = true;\n }\n\n static getConfigElement() {\n if (ConfigComponent) {\n return document.createElement(`${type}-editor`);\n }\n return undefined;\n }\n\n static getStubConfig() {\n return getStubConfig?.() ?? {};\n }\n }\n\n customElements.define(type, HACard);\n\n if (ConfigComponent) {\n const EditorComponent = ConfigComponent;\n\n class HACardEditor extends HTMLElement {\n private _hass?: HomeAssistant;\n private _config?: TConfig;\n\n set hass(hass: HomeAssistant) {\n this._hass = hass;\n this._render();\n }\n\n setConfig(config: TConfig) {\n this._config = config;\n this._render();\n }\n\n private _fireConfigChanged = (config: TConfig) => {\n this.dispatchEvent(\n new CustomEvent('config-changed', {\n detail: { config },\n bubbles: true,\n composed: true,\n }),\n );\n };\n\n private _render() {\n if (!this._hass || !this._config) return;\n\n // Render to light DOM so HA's custom elements (ha-select etc.) work.\n render(\n <EditorComponent\n hass={this._hass}\n config={this._config}\n onConfigChanged={this._fireConfigChanged}\n />,\n this,\n );\n }\n }\n\n customElements.define(`${type}-editor`, HACardEditor);\n }\n\n window.customCards = window.customCards || [];\n window.customCards.push({ type, name, description });\n\n console.info(\n `%c ${name.toUpperCase()} %c loaded `,\n 'background: #3b82f6; color: white; font-weight: bold',\n '',\n );\n}\n"],"names":[],"mappings":";;;AAAA,MAAM,eAAe;AACrB,MAAM,kBAAkB,KAAK,KAAK,KAAK;AAOhC,SAAS,cAAiB,KAA4B;AAC3D,MAAI;AACF,UAAM,MAAM,aAAa,QAAQ,GAAG,YAAY,GAAG,GAAG,EAAE;AACxD,QAAI,CAAC,IAAK,QAAO;AAEjB,UAAM,QAAuB,KAAK,MAAM,GAAG;AAC3C,QAAI,KAAK,IAAA,IAAQ,MAAM,YAAY,iBAAiB;AAClD,mBAAa,WAAW,GAAG,YAAY,GAAG,GAAG,EAAE;AAC/C,aAAO;AAAA,IACT;AACA,WAAO,MAAM;AAAA,EACf,QAAQ;AACN,WAAO;AAAA,EACT;AACF;AAEO,SAAS,YAAe,KAAa,MAAe;AACzD,MAAI;AACF,UAAM,QAAuB,EAAE,MAAM,WAAW,KAAK,MAAI;AACzD,iBAAa,QAAQ,GAAG,YAAY,GAAG,GAAG,IAAI,KAAK,UAAU,KAAK,CAAC;AAAA,EACrE,SAAS,GAAG;AACV,YAAQ,KAAK,gDAAgD,CAAC;AAAA,EAChE;AACF;ACtBO,SAAS,kBAA2D,UAAgB;AACzF,QAAM,cAAc,OAAU,QAAQ;AAEtC,cAAY,UAAU;AAGtB,QAAM,YAAY,OAAiB,IAAI;AACvC,MAAI,UAAU,YAAY,MAAM;AAC9B,cAAU,WAAW,IAAI,SAAwB;AAC/C,aAAO,YAAY,QAAQ,GAAG,IAAI;AAAA,IACpC;AAAA,EACF;AAEA,SAAO,UAAU;AACnB;ACDA,MAAM,YAAY,cAA8B,IAAI;AAQ7C,SAAS,WAAW,EAAE,MAAM,mBAAmB,YAA6B;AACjF,QAAM,UAAU,OAAO,IAAI;AAC3B,UAAQ,UAAU;AAElB,QAAM,UAAU,kBAAkB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AAA;AAAA,MAEJ;AAAA,IAAA;AAGF,mBAAe,OAAO,GAAG,IAAI,WAAW,YAAY;AAAA,EACtD;AAEA,SAAO,cAAc,OAAO,eAAe,CAAA;AAC3C,SAAO,YAAY,KAAK,EAAE,MAAM,MAAM,aAAa;AAEnD,UAAQ;AAAA,IACN,MAAM,KAAK,YAAA,CAAa;AAAA,IACxB;AAAA,IACA;AAAA,EAAA;AAEJ;"}
package/package.json ADDED
@@ -0,0 +1,63 @@
1
+ {
2
+ "name": "preact-homeassistant",
3
+ "version": "0.1.0",
4
+ "description": "Preact hooks and helpers for building Home Assistant custom cards",
5
+ "author": {
6
+ "name": "Stu Kabakoff",
7
+ "email": "stutrek@gmail.com"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/stutrek/preact-homeassistant.git"
12
+ },
13
+ "license": "MIT",
14
+ "type": "module",
15
+ "main": "./dist/index.js",
16
+ "types": "./dist/index.d.ts",
17
+ "exports": {
18
+ ".": {
19
+ "types": "./dist/index.d.ts",
20
+ "import": "./dist/index.js"
21
+ }
22
+ },
23
+ "files": [
24
+ "dist",
25
+ "src",
26
+ "README.md",
27
+ "LICENSE"
28
+ ],
29
+ "keywords": [
30
+ "home-assistant",
31
+ "hacs",
32
+ "preact",
33
+ "custom-card",
34
+ "lovelace"
35
+ ],
36
+ "peerDependencies": {
37
+ "preact": "^10.0.0"
38
+ },
39
+ "dependencies": {
40
+ "home-assistant-js-websocket": "^9.6.0"
41
+ },
42
+ "devDependencies": {
43
+ "@biomejs/biome": "^1.9.4",
44
+ "@preact/preset-vite": "^2.10.2",
45
+ "@testing-library/preact": "^3.2.4",
46
+ "@types/node": "^24.10.1",
47
+ "jsdom": "^26.1.0",
48
+ "preact": "^10.27.2",
49
+ "typescript": "~5.9.3",
50
+ "vite": "^7.2.4",
51
+ "vite-plugin-dts": "^4.5.4",
52
+ "vitest": "^4.0.17"
53
+ },
54
+ "scripts": {
55
+ "build": "tsc --noEmit && vite build",
56
+ "test": "vitest run",
57
+ "test:watch": "vitest",
58
+ "lint": "biome check src",
59
+ "lint:fix": "biome check --write src",
60
+ "format": "biome format --write src",
61
+ "typecheck": "tsc --noEmit"
62
+ }
63
+ }