preact-homeassistant 0.3.0 → 0.4.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/README.md CHANGED
@@ -15,7 +15,7 @@ pnpm add preact preact-homeassistant
15
15
  ## Quick start
16
16
 
17
17
  ```tsx
18
- import { registerPreactCard, useEntity, css } from 'preact-homeassistant';
18
+ import { registerPreactCard, HACard, useEntity, css } from 'preact-homeassistant';
19
19
 
20
20
  css`
21
21
  .my-card { padding: 16px; }
@@ -26,11 +26,11 @@ function MyCardContent({ config }: { config: { entity: string } }) {
26
26
  const weather = useEntity(config.entity);
27
27
 
28
28
  return (
29
- <ha-card>
29
+ <HACard>
30
30
  <div class="card-content my-card">
31
31
  <span class="temperature">{weather?.state ?? '...'}</span>
32
32
  </div>
33
- </ha-card>
33
+ </HACard>
34
34
  );
35
35
  }
36
36
 
@@ -87,6 +87,33 @@ prop and uses hooks for everything else.
87
87
  The card renders into a Shadow DOM root. The editor renders into the light DOM
88
88
  (required for HA's own custom elements like `<ha-select>` to work).
89
89
 
90
+ ## `<HACard>`
91
+
92
+ Use `HACard` as the root of your card instead of a raw `<ha-card>`. It makes the
93
+ card fill the height Home Assistant assigns it.
94
+
95
+ In HA's **sections (grid)** layout, when a card is resized (e.g. to 3 rows) HA
96
+ gives the card's host element a definite height. A plain `<ha-card>` collapses to
97
+ its natural content height and renders slightly short, leaving a gap. `HACard`
98
+ sets the host and the `ha-card` to fill that height, so the card matches the slot
99
+ exactly. In layouts with no fixed height (masonry, auto rows) it safely collapses
100
+ back to natural height, so it's a drop-in replacement everywhere.
101
+
102
+ ```tsx
103
+ <HACard class="size-large" align="space-between">
104
+ <div class="card-content my-card">…</div>
105
+ </HACard>
106
+ ```
107
+
108
+ | Prop | Type | Default | Description |
109
+ |---|---|---|---|
110
+ | `align` | `HACardAlign` | `'top'` | How content is distributed vertically when the slot is taller than the content. Friendly aliases `top` / `center` / `bottom`, or any flex `justify-content` value (`space-between`, `space-around`, `space-evenly`, `flex-start`, `flex-end`). |
111
+ | `class` | `string` | — | Class applied to the underlying `ha-card`. |
112
+ | `children` | `ComponentChildren` | — | Card contents. |
113
+
114
+ `align` only positions content as a block. To make an inner section *stretch* to
115
+ absorb the extra height, give it `flex: 1` in your card's CSS.
116
+
90
117
  ## Hooks
91
118
 
92
119
  ### `useEntity(entityId)`
package/dist/index.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { ComponentChildren } from 'preact';
2
2
  import { ComponentType } from 'preact';
3
3
  import { Connection } from 'home-assistant-js-websocket';
4
+ import { createElement } from 'preact';
4
5
  import { HassConfig } from 'home-assistant-js-websocket';
5
6
  import { HassEntities } from 'home-assistant-js-websocket';
6
7
  import { HassEntity } from 'home-assistant-js-websocket';
@@ -153,6 +154,16 @@ export declare type ForecastType = 'daily' | 'hourly' | 'twice_daily';
153
154
  */
154
155
  export declare function getAllStyles(): string;
155
156
 
157
+ export declare function HACard({ align, class: className, children }: HACardProps): createElement.JSX.Element;
158
+
159
+ export declare type HACardAlign = 'top' | 'center' | 'bottom' | 'flex-start' | 'flex-end' | 'space-between' | 'space-around' | 'space-evenly';
160
+
161
+ declare interface HACardProps {
162
+ align?: HACardAlign;
163
+ class?: string;
164
+ children?: ComponentChildren;
165
+ }
166
+
156
167
  export declare function HAProvider({ hass, subscribeToEntity, subscribeToHass, cache, children, }: HAProviderProps): JSX.Element;
157
168
 
158
169
  declare interface HAProviderProps {
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
- import { jsx, jsxs } from "preact/jsx-runtime";
2
- import { createContext, render } from "preact";
1
+ import { jsx, jsxs, Fragment } from "preact/jsx-runtime";
2
+ import { createContext, render, createElement } from "preact";
3
3
  import { useRef, useMemo, useState, useEffect, useContext } from "preact/hooks";
4
4
  function readCache(cache, key) {
5
5
  return cache.get(key)?.data;
@@ -353,7 +353,7 @@ function registerPreactCard(options) {
353
353
  this._renderTree();
354
354
  }
355
355
  }
356
- class HACard extends BaseHACard {
356
+ class HACard2 extends BaseHACard {
357
357
  _shadowRoot;
358
358
  _entityChangeListeners = /* @__PURE__ */ new Map();
359
359
  _hassChangeListeners = /* @__PURE__ */ new Set();
@@ -432,7 +432,7 @@ function registerPreactCard(options) {
432
432
  return getStubConfig?.() ?? {};
433
433
  }
434
434
  }
435
- customElements.define(type, HACard);
435
+ customElements.define(type, HACard2);
436
436
  if (ConfigComponent) {
437
437
  const EditorComponent = ConfigComponent;
438
438
  class HACardEditor extends BaseHACard {
@@ -486,6 +486,27 @@ function registerPreactCard(options) {
486
486
  ""
487
487
  );
488
488
  }
489
+ const ALIGN_ALIASES = { top: "flex-start", bottom: "flex-end" };
490
+ function HACard({ align = "top", class: className, children }) {
491
+ const justify = ALIGN_ALIASES[align] ?? align;
492
+ return /* @__PURE__ */ jsxs(Fragment, { children: [
493
+ /* @__PURE__ */ jsx("style", { children: ":host{display:block;height:100%;box-sizing:border-box;}" }),
494
+ createElement(
495
+ "ha-card",
496
+ {
497
+ class: className,
498
+ style: {
499
+ height: "100%",
500
+ boxSizing: "border-box",
501
+ display: "flex",
502
+ flexDirection: "column",
503
+ justifyContent: justify
504
+ }
505
+ },
506
+ children
507
+ )
508
+ ] });
509
+ }
489
510
  function useResizeObserver(ref, callback, deps = []) {
490
511
  const callbackRef = useRef(callback);
491
512
  callbackRef.current = callback;
@@ -526,6 +547,7 @@ function useWidth(ref) {
526
547
  return width;
527
548
  }
528
549
  export {
550
+ HACard,
529
551
  HAProvider,
530
552
  css,
531
553
  getAllStyles,
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":["../src/cacheUtils.ts","../src/useCallbackStable.ts","../src/HAContext.tsx","../src/styleRegistry.ts","../src/registerPreactCard.tsx","../src/useResizeObserver.ts","../src/useWidth.ts"],"sourcesContent":["// In-memory cache helpers. The cache Map is owned per-card by the HAProvider\n// store (see HAContext), so its lifetime matches the card and it is\n// garbage-collected when the card is torn down. There is intentionally no\n// persistence, TTL, or size cap: freshness comes from entity subscriptions and\n// periodic refetch, not from cache expiry, and growth is bounded by the ranges\n// a single card visits in a session.\n\nexport interface CacheEntry<T> {\n data: T;\n}\n\nexport type Cache = Map<string, CacheEntry<unknown>>;\n\nexport function readCache<T>(cache: Cache, key: string): T | undefined {\n return (cache.get(key) as CacheEntry<T> | undefined)?.data;\n}\n\nexport function writeCache<T>(cache: Cache, key: string, data: T): void {\n cache.set(key, { data });\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 { type Cache, readCache, writeCache } from './cacheUtils';\nimport type {\n CalendarEvent,\n CalendarEventWithSource,\n EntityForId,\n FetchStatus,\n ForecastType,\n HomeAssistant,\n ServicesForId,\n WeatherForecast,\n} from './types';\nimport { useCallbackStable } from './useCallbackStable';\n\ntype SubscribeToHass = (callback: () => void) => () => void;\n\n// Default for providers that don't wire up hass-value notifications (Storybook,\n// tests). useHassValue then simply returns its initial value and never updates.\nconst noopSubscribeToHass: SubscribeToHass = () => () => {};\n\ninterface HAStore {\n getHass: () => HomeAssistant | undefined;\n subscribeToEntity: (entityId: string, callback: (entity: any) => void) => () => void;\n subscribeToHass: SubscribeToHass;\n // Per-card cache (events, forecasts, entities). Owned by the provider so its\n // lifetime is the card's — GC'd with the store when the card is torn down.\n cache: Cache;\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 subscribeToHass?: SubscribeToHass;\n // Optional injected cache (tests seed/inspect it); defaults to a fresh\n // per-provider Map held stable across re-renders.\n cache?: Cache;\n children: ComponentChildren;\n}\n\nexport function HAProvider({\n hass,\n subscribeToEntity,\n subscribeToHass,\n cache,\n children,\n}: HAProviderProps) {\n const hassRef = useRef(hass);\n hassRef.current = hass;\n\n const getHass = useCallbackStable(() => hassRef.current);\n\n const cacheRef = useRef<Cache>();\n if (!cacheRef.current) cacheRef.current = cache ?? new Map();\n\n const resolvedSubscribeToHass = subscribeToHass ?? noopSubscribeToHass;\n\n const store = useMemo<HAStore>(\n () => ({\n getHass,\n subscribeToEntity,\n subscribeToHass: resolvedSubscribeToHass,\n cache: cacheRef.current!,\n }),\n [getHass, subscribeToEntity, resolvedSubscribeToHass],\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.getHass()?.states[entityId] as EntityForId<T> | undefined;\n if (current) return current;\n return readCache<EntityForId<T>>(store.cache, cacheKey);\n });\n\n useEffect(() => {\n const unsubscribe = store.subscribeToEntity(entityId, (newEntity) => {\n setEntity(newEntity as EntityForId<T>);\n writeCache(store.cache, cacheKey, newEntity);\n });\n return unsubscribe;\n }, [entityId, store.subscribeToEntity, store.cache, 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\n/**\n * Subscribe to a derived slice of the `hass` object (e.g. config, themes) and\n * re-render only when that slice changes. Use this for non-entity values —\n * entity state goes through `useEntity`. The selector runs on every hass update\n * but only re-renders the consumer when `isEqual` reports a change, so it's\n * cheap for rarely-changing values like config/themes.\n */\nexport function useHassValue<T>(\n selector: (hass: HomeAssistant | undefined) => T,\n isEqual: (a: T, b: T) => boolean = Object.is,\n): T {\n const store = useHAStore();\n\n const selectorRef = useRef(selector);\n selectorRef.current = selector;\n const isEqualRef = useRef(isEqual);\n isEqualRef.current = isEqual;\n\n const [value, setValue] = useState<T>(() => selectorRef.current(store.getHass()));\n\n useEffect(() => {\n const unsubscribe = store.subscribeToHass(() => {\n const next = selectorRef.current(store.getHass());\n setValue((prev) => (isEqualRef.current(prev, next) ? prev : next));\n });\n return unsubscribe;\n }, [store.subscribeToHass, store.getHass]);\n\n return value;\n}\n\n/** Re-renders when `hass.config` changes (units, latitude/longitude, etc.). */\nexport function useHassConfig(): HomeAssistant['config'] | undefined {\n return useHassValue((hass) => hass?.config);\n}\n\n/** Re-renders when the active theme's dark mode flips. */\nexport function useDarkMode(): boolean {\n return useHassValue((hass) => hass?.themes?.darkMode ?? false);\n}\n\ntype ServiceCaller<T extends string> = <S extends keyof ServicesForId<T> & string>(\n service: S,\n ...args: ServicesForId<T>[S] extends undefined\n ? []\n : Record<string, never> extends Exclude<ServicesForId<T>[S], undefined>\n ? [data?: ServicesForId<T>[S]]\n : [data: ServicesForId<T>[S]]\n) => Promise<void>;\n\n/**\n * Returns a stable function that calls services on a specific HA entity.\n * The service domain is parsed from the entity ID prefix and `entity_id` is\n * auto-injected into every call. Service names and data shapes are strongly\n * typed via DomainServiceMap when the domain is registered. No-ops if hass\n * is not yet available or the entity ID is empty.\n *\n * const fanService = useService(config.entity); // `fan.${string}`\n * await fanService('turn_off');\n * await fanService('set_percentage', { percentage: 67 });\n */\nexport function useService<T extends string>(entityId: T): ServiceCaller<T> {\n const { getHass } = useHass();\n return useCallbackStable(((service: string, data?: object) => {\n const hass = getHass();\n if (!hass || !entityId.includes('.')) return Promise.resolve();\n const domain = entityId.split('.', 1)[0];\n return hass.callService(domain, service, { entity_id: entityId, ...data });\n }) as ServiceCaller<T>);\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 store = useHAStore();\n const [data, setData] = useState<T | undefined>(() => readCache<T>(store.cache, cacheKey));\n const [isFresh, setIsFresh] = useState(false);\n const [isFetching, setIsFetching] = useState(false);\n const [error, setError] = useState<Error | undefined>(undefined);\n\n // Stale-while-revalidate, key-change aware. When `cacheKey` changes we swap to\n // the new key's cached value synchronously (SWR hit) or keep the previously\n // rendered data (keep-previous-data on a cold key) — never blanking to a\n // loading state. The `deps` effect below issues the background refetch; the\n // only `'loading'` state is a true cold start (nothing cached, nothing fetched).\n const dataKeyRef = useRef(cacheKey);\n if (cacheKey !== dataKeyRef.current) {\n dataKeyRef.current = cacheKey;\n setIsFresh(false);\n setError(undefined);\n const cached = readCache<T>(store.cache, cacheKey);\n if (cached !== undefined) setData(cached);\n // cache miss: leave `data` as-is (keep-previous-data)\n }\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 writeCache(store.cache, 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 }, 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: CalendarEventWithSource[] | undefined;\n status: FetchStatus;\n error: Error | undefined;\n refetch: () => void;\n /**\n * Warm the cache for an arbitrary range (e.g. adjacent months) without\n * touching component state. Best-effort: skips ranges already cached and\n * swallows failures.\n */\n prefetch: (range: { start: Date; end: Date }) => void;\n}\n\nfunction calendarEventsCacheKey(\n entityIds: `calendar.${string}`[],\n range: { start: Date; end: Date },\n): string {\n return `events:${entityIds.join(',')}:${range.start.getTime()}-${range.end.getTime()}`;\n}\n\nasync function fetchCalendarRange(\n hass: HomeAssistant | undefined,\n entityIds: `calendar.${string}`[],\n range: { start: Date; end: Date },\n): Promise<CalendarEventWithSource[]> {\n if (!hass?.connection) {\n throw new Error('Home Assistant connection not available');\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: range.start.toISOString(),\n end_date_time: range.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/**\n * Fetch events from one or more calendars for a date range, with in-memory\n * (per-card) caching and stale-while-revalidate behavior. Events are tagged\n * with their source calendar ID. Returns `prefetch` to warm adjacent ranges.\n */\nexport function useCalendarEvents(\n entityIds: `calendar.${string}`[],\n options: { start: Date; end: Date },\n): UseCalendarEventsResult {\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(() => fetchCalendarRange(getHass(), entityIds, options));\n\n const {\n data: events,\n status,\n error,\n refetch,\n } = useCachedFetch(cacheKey, fetcher, [entityIdsKey, dateRangeKey]);\n\n const prefetch = useCallbackStable((range: { start: Date; end: Date }) => {\n const key = calendarEventsCacheKey(entityIds, range);\n if (store.cache.has(key)) return; // already warm\n fetchCalendarRange(getHass(), entityIds, range)\n .then((result) => writeCache(store.cache, key, result))\n .catch(() => {\n // best-effort prefetch; ignore failures\n });\n });\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, prefetch };\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\n// Grace period before a disconnected card is torn down. HA detaches and\n// re-attaches cards transiently (scroll virtualization, edit mode); only a card\n// that stays gone past this window is genuinely removed, so we defer the\n// Preact unmount — and the effect cleanups it triggers (timers, subscriptions)\n// — until then. A reconnect within the window cancels the teardown.\nconst TEARDOWN_GRACE_MS = 5000;\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 // Shared host-element plumbing: config/hass storage, connect/disconnect\n // lifecycle, deferred teardown, and the render skeleton. Subclasses supply\n // the render root and the tree to render, and may override hass behavior.\n abstract class BaseHACard extends HTMLElement {\n protected _hass?: HomeAssistant;\n protected _config?: TConfig;\n private _teardownTimer?: ReturnType<typeof setTimeout>;\n\n protected abstract _getRenderRoot(): Element | ShadowRoot;\n protected abstract _renderTree(): void;\n protected _renderUnconfigured(): void {}\n\n connectedCallback() {\n if (this._teardownTimer !== undefined) {\n clearTimeout(this._teardownTimer);\n this._teardownTimer = undefined;\n }\n this._maybeRenderOnConnect();\n }\n\n protected _maybeRenderOnConnect() {\n if (this._hass && this._config) {\n this._render();\n }\n }\n\n disconnectedCallback() {\n if (this._teardownTimer !== undefined) clearTimeout(this._teardownTimer);\n this._teardownTimer = setTimeout(() => {\n // Unmount the Preact tree, running effect cleanups (clears timers and\n // entity subscriptions). Scheduled, never synchronous, so a transient\n // disconnect+reconnect leaves the tree intact.\n render(null, this._getRenderRoot());\n this._teardownTimer = undefined;\n }, TEARDOWN_GRACE_MS);\n }\n\n setConfig(config: TConfig) {\n this._config = config;\n if (this._hass && this.isConnected) {\n this._render();\n }\n }\n\n protected _render() {\n if (!this._config || !this._hass) {\n this._renderUnconfigured();\n return;\n }\n this._renderTree();\n }\n }\n\n class HACard extends BaseHACard {\n private _shadowRoot: ShadowRoot;\n private _entityChangeListeners = new Map<string, Set<(entity: any) => void>>();\n private _hassChangeListeners = new Set<() => void>();\n\n constructor() {\n super();\n this._shadowRoot = this.attachShadow({ mode: 'open' });\n }\n\n protected _getRenderRoot() {\n return this._shadowRoot;\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 // Notify non-entity (config/themes) subscribers on every update. This does\n // not re-render the whole card — useHassValue re-renders only its own\n // consumer, and only when its selected slice actually changes.\n this._hassChangeListeners.forEach((listener) => listener());\n\n if (!prevStates && this._config && 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 _subscribeToHass = (callback: () => void) => {\n this._hassChangeListeners.add(callback);\n return () => {\n this._hassChangeListeners.delete(callback);\n };\n };\n\n protected _renderTree() {\n render(\n <HAProvider\n hass={this._hass}\n subscribeToEntity={this._subscribeToEntity}\n subscribeToHass={this._subscribeToHass}\n >\n <style>{getAllStyles()}</style>\n <Component config={this._config!} />\n </HAProvider>,\n this._shadowRoot,\n );\n }\n\n protected _renderUnconfigured() {\n if (UnconfiguredComponent) {\n render(<UnconfiguredComponent />, this._shadowRoot);\n }\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 BaseHACard {\n protected _getRenderRoot() {\n return this;\n }\n\n // The editor re-renders on every hass update: it passes `hass` straight to\n // HA's <ha-form>/<ha-selector>, whose entity pickers need a fresh hass to\n // stay current. (Unlike the card, which renders once then subscribes.)\n set hass(hass: HomeAssistant) {\n this._hass = hass;\n this._render();\n }\n\n // Render whenever config arrives, regardless of connection — HA may set\n // config/hass before connecting the editor element.\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 // Render to light DOM so HA's custom elements (ha-form etc.) work.\n protected _renderTree() {\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","import type { RefObject } from 'preact';\nimport { useEffect, useRef } from 'preact/hooks';\n\nexport interface ElementSize {\n width: number;\n height: number;\n}\n\nexport type ResizeCallback = (size: ElementSize) => void;\n\n/**\n * Observe an element's size via ResizeObserver. The callback fires:\n *\n * 1. Once after mount, with the element's current size.\n * 2. Whenever the element's size changes.\n * 3. Whenever `deps` change, re-firing with the current size — so callers\n * can re-run draws when their inputs change without re-creating the\n * observer.\n *\n * The callback is suppressed only while the element is detached from the\n * document. Zero width/height is delivered to the callback as-is — consumers\n * that need to skip degenerate sizes (e.g. canvas painters where a 0-sized\n * drawImage throws InvalidStateError) should add their own early return.\n *\n * Sizes are read from `offsetWidth` / `offsetHeight` (CSS pixels, includes\n * padding + border). The callback is held in a ref, so passing a fresh\n * function each render is safe — it never re-creates the observer.\n *\n * @example\n * const containerRef = useRef<HTMLDivElement>(null);\n * const canvasRef = useRef<HTMLCanvasElement>(null);\n *\n * useResizeObserver(\n * containerRef,\n * ({ width, height }) => {\n * if (width === 0 || height === 0) return; // optional, consumer's call\n * drawChart(canvasRef.current, forecast, width, height);\n * },\n * [forecast],\n * );\n */\nexport function useResizeObserver<T extends HTMLElement>(\n ref: RefObject<T>,\n callback: ResizeCallback,\n deps: unknown[] = [],\n): void {\n const callbackRef = useRef(callback);\n callbackRef.current = callback;\n\n // Set up the observer once per element. ResizeObserver fires once\n // synchronously-ish after `.observe()` with the current size, which\n // covers the initial draw.\n useEffect(() => {\n const element = ref.current;\n if (!element) return;\n\n const fire = () => {\n if (!element.isConnected) return;\n callbackRef.current({\n width: element.offsetWidth,\n height: element.offsetHeight,\n });\n };\n\n const observer = new ResizeObserver(fire);\n observer.observe(element);\n return () => observer.disconnect();\n // ref identity is stable across renders; observer setup runs once.\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, []);\n\n // Re-fire on dependency change. Skip the first render — the observer's\n // initial `.observe()` fire already delivers the mount-time size.\n const isFirstRun = useRef(true);\n useEffect(() => {\n if (isFirstRun.current) {\n isFirstRun.current = false;\n return;\n }\n const element = ref.current;\n if (!element || !element.isConnected) return;\n callbackRef.current({\n width: element.offsetWidth,\n height: element.offsetHeight,\n });\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, deps);\n}\n","import type { RefObject } from 'preact';\nimport { useState } from 'preact/hooks';\nimport { useResizeObserver } from './useResizeObserver';\n\n/**\n * Track the current width of a referenced element in CSS pixels.\n *\n * Returns `undefined` until the first non-zero measurement is observed, then\n * a positive number that updates as the element resizes. Once a real width\n * is captured the hook will never report `undefined` or `0` again, even\n * during HA layout transitions (dashboard switch, edit-mode toggle) — the\n * underlying ResizeObserver firings are silently dropped while the element\n * is detached or transiently zero-width, so the component renders with the\n * last good value instead of flashing through a degenerate state.\n *\n * Use this when you need a width value in JSX (responsive layout, prop to a\n * sized child). If you only need the value imperatively inside a draw\n * callback, prefer `useResizeObserver` directly — it doesn't allocate\n * component state or cause re-renders.\n *\n * @example\n * const ref = useRef<HTMLDivElement>(null);\n * const width = useWidth(ref);\n * return (\n * <div ref={ref}>\n * {width !== undefined && <Chart width={width} />}\n * </div>\n * );\n */\nexport function useWidth<T extends HTMLElement>(ref: RefObject<T>): number | undefined {\n const [width, setWidth] = useState<number | undefined>(undefined);\n useResizeObserver(ref, (size) => {\n if (size.width === 0) return;\n setWidth((prev) => (prev === size.width ? prev : size.width));\n });\n return width;\n}\n"],"names":[],"mappings":";;;AAaO,SAAS,UAAa,OAAc,KAA4B;AACrE,SAAQ,MAAM,IAAI,GAAG,GAAiC;AACxD;AAEO,SAAS,WAAc,OAAc,KAAa,MAAe;AACtE,QAAM,IAAI,KAAK,EAAE,KAAA,CAAM;AACzB;ACVO,SAAS,kBAA2D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B;AAAA,IACH;AAEA,UAAM,WAAW,IAAI,eAAe,IAAI;AACxC,aAAS,QAAQ,OAAO;AACxB,WAAO,MAAM,SAAS,WAAA;AAAA,EAGxB,GAAG,CAAA,CAAE;AAIL,QAAM,aAAa,OAAO,IAAI;AAC9B,YAAU,MAAM;AACd,QAAI,WAAW,SAAS;AACtB,iBAAW,UAAU;AACrB;AAAA,IACF;AACA,UAAM,UAAU,IAAI;AACpB,QAAI,CAAC,WAAW,CAAC,QAAQ,YAAa;AACtC,gBAAY,QAAQ;AAAA,MAClB,OAAO,QAAQ;AAAA,MACf,QAAQ,QAAQ;AAAA,IAAA,CACjB;AAAA,EAEH,GAAG,IAAI;AACT;AC1DO,SAAS,SAAgC,KAAuC;AACrF,QAAM,CAAC,OAAO,QAAQ,IAAI,SAA6B,MAAS;AAChE,oBAAkB,KAAK,CAAC,SAAS;AAC/B,QAAI,KAAK,UAAU,EAAG;AACtB,aAAS,CAAC,SAAU,SAAS,KAAK,QAAQ,OAAO,KAAK,KAAM;AAAA,EAC9D,CAAC;AACD,SAAO;AACT;"}
1
+ {"version":3,"file":"index.js","sources":["../src/cacheUtils.ts","../src/useCallbackStable.ts","../src/HAContext.tsx","../src/styleRegistry.ts","../src/registerPreactCard.tsx","../src/HACard.tsx","../src/useResizeObserver.ts","../src/useWidth.ts"],"sourcesContent":["// In-memory cache helpers. The cache Map is owned per-card by the HAProvider\n// store (see HAContext), so its lifetime matches the card and it is\n// garbage-collected when the card is torn down. There is intentionally no\n// persistence, TTL, or size cap: freshness comes from entity subscriptions and\n// periodic refetch, not from cache expiry, and growth is bounded by the ranges\n// a single card visits in a session.\n\nexport interface CacheEntry<T> {\n data: T;\n}\n\nexport type Cache = Map<string, CacheEntry<unknown>>;\n\nexport function readCache<T>(cache: Cache, key: string): T | undefined {\n return (cache.get(key) as CacheEntry<T> | undefined)?.data;\n}\n\nexport function writeCache<T>(cache: Cache, key: string, data: T): void {\n cache.set(key, { data });\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 { type Cache, readCache, writeCache } from './cacheUtils';\nimport type {\n CalendarEvent,\n CalendarEventWithSource,\n EntityForId,\n FetchStatus,\n ForecastType,\n HomeAssistant,\n ServicesForId,\n WeatherForecast,\n} from './types';\nimport { useCallbackStable } from './useCallbackStable';\n\ntype SubscribeToHass = (callback: () => void) => () => void;\n\n// Default for providers that don't wire up hass-value notifications (Storybook,\n// tests). useHassValue then simply returns its initial value and never updates.\nconst noopSubscribeToHass: SubscribeToHass = () => () => {};\n\ninterface HAStore {\n getHass: () => HomeAssistant | undefined;\n subscribeToEntity: (entityId: string, callback: (entity: any) => void) => () => void;\n subscribeToHass: SubscribeToHass;\n // Per-card cache (events, forecasts, entities). Owned by the provider so its\n // lifetime is the card's — GC'd with the store when the card is torn down.\n cache: Cache;\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 subscribeToHass?: SubscribeToHass;\n // Optional injected cache (tests seed/inspect it); defaults to a fresh\n // per-provider Map held stable across re-renders.\n cache?: Cache;\n children: ComponentChildren;\n}\n\nexport function HAProvider({\n hass,\n subscribeToEntity,\n subscribeToHass,\n cache,\n children,\n}: HAProviderProps) {\n const hassRef = useRef(hass);\n hassRef.current = hass;\n\n const getHass = useCallbackStable(() => hassRef.current);\n\n const cacheRef = useRef<Cache>();\n if (!cacheRef.current) cacheRef.current = cache ?? new Map();\n\n const resolvedSubscribeToHass = subscribeToHass ?? noopSubscribeToHass;\n\n const store = useMemo<HAStore>(\n () => ({\n getHass,\n subscribeToEntity,\n subscribeToHass: resolvedSubscribeToHass,\n cache: cacheRef.current!,\n }),\n [getHass, subscribeToEntity, resolvedSubscribeToHass],\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.getHass()?.states[entityId] as EntityForId<T> | undefined;\n if (current) return current;\n return readCache<EntityForId<T>>(store.cache, cacheKey);\n });\n\n useEffect(() => {\n const unsubscribe = store.subscribeToEntity(entityId, (newEntity) => {\n setEntity(newEntity as EntityForId<T>);\n writeCache(store.cache, cacheKey, newEntity);\n });\n return unsubscribe;\n }, [entityId, store.subscribeToEntity, store.cache, 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\n/**\n * Subscribe to a derived slice of the `hass` object (e.g. config, themes) and\n * re-render only when that slice changes. Use this for non-entity values —\n * entity state goes through `useEntity`. The selector runs on every hass update\n * but only re-renders the consumer when `isEqual` reports a change, so it's\n * cheap for rarely-changing values like config/themes.\n */\nexport function useHassValue<T>(\n selector: (hass: HomeAssistant | undefined) => T,\n isEqual: (a: T, b: T) => boolean = Object.is,\n): T {\n const store = useHAStore();\n\n const selectorRef = useRef(selector);\n selectorRef.current = selector;\n const isEqualRef = useRef(isEqual);\n isEqualRef.current = isEqual;\n\n const [value, setValue] = useState<T>(() => selectorRef.current(store.getHass()));\n\n useEffect(() => {\n const unsubscribe = store.subscribeToHass(() => {\n const next = selectorRef.current(store.getHass());\n setValue((prev) => (isEqualRef.current(prev, next) ? prev : next));\n });\n return unsubscribe;\n }, [store.subscribeToHass, store.getHass]);\n\n return value;\n}\n\n/** Re-renders when `hass.config` changes (units, latitude/longitude, etc.). */\nexport function useHassConfig(): HomeAssistant['config'] | undefined {\n return useHassValue((hass) => hass?.config);\n}\n\n/** Re-renders when the active theme's dark mode flips. */\nexport function useDarkMode(): boolean {\n return useHassValue((hass) => hass?.themes?.darkMode ?? false);\n}\n\ntype ServiceCaller<T extends string> = <S extends keyof ServicesForId<T> & string>(\n service: S,\n ...args: ServicesForId<T>[S] extends undefined\n ? []\n : Record<string, never> extends Exclude<ServicesForId<T>[S], undefined>\n ? [data?: ServicesForId<T>[S]]\n : [data: ServicesForId<T>[S]]\n) => Promise<void>;\n\n/**\n * Returns a stable function that calls services on a specific HA entity.\n * The service domain is parsed from the entity ID prefix and `entity_id` is\n * auto-injected into every call. Service names and data shapes are strongly\n * typed via DomainServiceMap when the domain is registered. No-ops if hass\n * is not yet available or the entity ID is empty.\n *\n * const fanService = useService(config.entity); // `fan.${string}`\n * await fanService('turn_off');\n * await fanService('set_percentage', { percentage: 67 });\n */\nexport function useService<T extends string>(entityId: T): ServiceCaller<T> {\n const { getHass } = useHass();\n return useCallbackStable(((service: string, data?: object) => {\n const hass = getHass();\n if (!hass || !entityId.includes('.')) return Promise.resolve();\n const domain = entityId.split('.', 1)[0];\n return hass.callService(domain, service, { entity_id: entityId, ...data });\n }) as ServiceCaller<T>);\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 store = useHAStore();\n const [data, setData] = useState<T | undefined>(() => readCache<T>(store.cache, cacheKey));\n const [isFresh, setIsFresh] = useState(false);\n const [isFetching, setIsFetching] = useState(false);\n const [error, setError] = useState<Error | undefined>(undefined);\n\n // Stale-while-revalidate, key-change aware. When `cacheKey` changes we swap to\n // the new key's cached value synchronously (SWR hit) or keep the previously\n // rendered data (keep-previous-data on a cold key) — never blanking to a\n // loading state. The `deps` effect below issues the background refetch; the\n // only `'loading'` state is a true cold start (nothing cached, nothing fetched).\n const dataKeyRef = useRef(cacheKey);\n if (cacheKey !== dataKeyRef.current) {\n dataKeyRef.current = cacheKey;\n setIsFresh(false);\n setError(undefined);\n const cached = readCache<T>(store.cache, cacheKey);\n if (cached !== undefined) setData(cached);\n // cache miss: leave `data` as-is (keep-previous-data)\n }\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 writeCache(store.cache, 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 }, 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: CalendarEventWithSource[] | undefined;\n status: FetchStatus;\n error: Error | undefined;\n refetch: () => void;\n /**\n * Warm the cache for an arbitrary range (e.g. adjacent months) without\n * touching component state. Best-effort: skips ranges already cached and\n * swallows failures.\n */\n prefetch: (range: { start: Date; end: Date }) => void;\n}\n\nfunction calendarEventsCacheKey(\n entityIds: `calendar.${string}`[],\n range: { start: Date; end: Date },\n): string {\n return `events:${entityIds.join(',')}:${range.start.getTime()}-${range.end.getTime()}`;\n}\n\nasync function fetchCalendarRange(\n hass: HomeAssistant | undefined,\n entityIds: `calendar.${string}`[],\n range: { start: Date; end: Date },\n): Promise<CalendarEventWithSource[]> {\n if (!hass?.connection) {\n throw new Error('Home Assistant connection not available');\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: range.start.toISOString(),\n end_date_time: range.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/**\n * Fetch events from one or more calendars for a date range, with in-memory\n * (per-card) caching and stale-while-revalidate behavior. Events are tagged\n * with their source calendar ID. Returns `prefetch` to warm adjacent ranges.\n */\nexport function useCalendarEvents(\n entityIds: `calendar.${string}`[],\n options: { start: Date; end: Date },\n): UseCalendarEventsResult {\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(() => fetchCalendarRange(getHass(), entityIds, options));\n\n const {\n data: events,\n status,\n error,\n refetch,\n } = useCachedFetch(cacheKey, fetcher, [entityIdsKey, dateRangeKey]);\n\n const prefetch = useCallbackStable((range: { start: Date; end: Date }) => {\n const key = calendarEventsCacheKey(entityIds, range);\n if (store.cache.has(key)) return; // already warm\n fetchCalendarRange(getHass(), entityIds, range)\n .then((result) => writeCache(store.cache, key, result))\n .catch(() => {\n // best-effort prefetch; ignore failures\n });\n });\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, prefetch };\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\n// Grace period before a disconnected card is torn down. HA detaches and\n// re-attaches cards transiently (scroll virtualization, edit mode); only a card\n// that stays gone past this window is genuinely removed, so we defer the\n// Preact unmount — and the effect cleanups it triggers (timers, subscriptions)\n// — until then. A reconnect within the window cancels the teardown.\nconst TEARDOWN_GRACE_MS = 5000;\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 // Shared host-element plumbing: config/hass storage, connect/disconnect\n // lifecycle, deferred teardown, and the render skeleton. Subclasses supply\n // the render root and the tree to render, and may override hass behavior.\n abstract class BaseHACard extends HTMLElement {\n protected _hass?: HomeAssistant;\n protected _config?: TConfig;\n private _teardownTimer?: ReturnType<typeof setTimeout>;\n\n protected abstract _getRenderRoot(): Element | ShadowRoot;\n protected abstract _renderTree(): void;\n protected _renderUnconfigured(): void {}\n\n connectedCallback() {\n if (this._teardownTimer !== undefined) {\n clearTimeout(this._teardownTimer);\n this._teardownTimer = undefined;\n }\n this._maybeRenderOnConnect();\n }\n\n protected _maybeRenderOnConnect() {\n if (this._hass && this._config) {\n this._render();\n }\n }\n\n disconnectedCallback() {\n if (this._teardownTimer !== undefined) clearTimeout(this._teardownTimer);\n this._teardownTimer = setTimeout(() => {\n // Unmount the Preact tree, running effect cleanups (clears timers and\n // entity subscriptions). Scheduled, never synchronous, so a transient\n // disconnect+reconnect leaves the tree intact.\n render(null, this._getRenderRoot());\n this._teardownTimer = undefined;\n }, TEARDOWN_GRACE_MS);\n }\n\n setConfig(config: TConfig) {\n this._config = config;\n if (this._hass && this.isConnected) {\n this._render();\n }\n }\n\n protected _render() {\n if (!this._config || !this._hass) {\n this._renderUnconfigured();\n return;\n }\n this._renderTree();\n }\n }\n\n class HACard extends BaseHACard {\n private _shadowRoot: ShadowRoot;\n private _entityChangeListeners = new Map<string, Set<(entity: any) => void>>();\n private _hassChangeListeners = new Set<() => void>();\n\n constructor() {\n super();\n this._shadowRoot = this.attachShadow({ mode: 'open' });\n }\n\n protected _getRenderRoot() {\n return this._shadowRoot;\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 // Notify non-entity (config/themes) subscribers on every update. This does\n // not re-render the whole card — useHassValue re-renders only its own\n // consumer, and only when its selected slice actually changes.\n this._hassChangeListeners.forEach((listener) => listener());\n\n if (!prevStates && this._config && 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 _subscribeToHass = (callback: () => void) => {\n this._hassChangeListeners.add(callback);\n return () => {\n this._hassChangeListeners.delete(callback);\n };\n };\n\n protected _renderTree() {\n render(\n <HAProvider\n hass={this._hass}\n subscribeToEntity={this._subscribeToEntity}\n subscribeToHass={this._subscribeToHass}\n >\n <style>{getAllStyles()}</style>\n <Component config={this._config!} />\n </HAProvider>,\n this._shadowRoot,\n );\n }\n\n protected _renderUnconfigured() {\n if (UnconfiguredComponent) {\n render(<UnconfiguredComponent />, this._shadowRoot);\n }\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 BaseHACard {\n protected _getRenderRoot() {\n return this;\n }\n\n // The editor re-renders on every hass update: it passes `hass` straight to\n // HA's <ha-form>/<ha-selector>, whose entity pickers need a fresh hass to\n // stay current. (Unlike the card, which renders once then subscribes.)\n set hass(hass: HomeAssistant) {\n this._hass = hass;\n this._render();\n }\n\n // Render whenever config arrives, regardless of connection — HA may set\n // config/hass before connecting the editor element.\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 // Render to light DOM so HA's custom elements (ha-form etc.) work.\n protected _renderTree() {\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","import { type ComponentChildren, createElement } from 'preact';\n\n// `align` controls how content sits within the height HA assigns. Friendly\n// aliases map to flex justify-content; any raw flex value also passes through.\n// Default 'top' keeps content compact at the top (today's look, but the card\n// box now fills the slot). Use 'center', 'bottom', or 'space-between' to spread.\nexport type HACardAlign =\n | 'top'\n | 'center'\n | 'bottom'\n | 'flex-start'\n | 'flex-end'\n | 'space-between'\n | 'space-around'\n | 'space-evenly';\n\nconst ALIGN_ALIASES: Record<string, string> = { top: 'flex-start', bottom: 'flex-end' };\n\ninterface HACardProps {\n align?: HACardAlign;\n class?: string;\n children?: ComponentChildren;\n}\n\n// Drop-in replacement for a raw <ha-card> that fills the height Home Assistant\n// assigns. In the sections (grid) layout HA gives the card's host element a\n// definite height; a plain ha-card collapses to natural content height and\n// renders slightly short. HACard makes the host and ha-card fill that height\n// and flex-distributes content per `align`.\n//\n// `ha-card` is created via createElement (rather than as a JSX intrinsic) so\n// the component carries no dependency on consumers declaring it in their JSX\n// IntrinsicElements — it just works when imported.\nexport function HACard({ align = 'top', class: className, children }: HACardProps) {\n const justify = ALIGN_ALIASES[align] ?? align;\n return (\n <>\n {/* Host must have a definite height for ha-card's 100% to resolve. In\n masonry/auto-row layouts the parent height is auto, so this safely\n collapses back to natural height. */}\n <style>{':host{display:block;height:100%;box-sizing:border-box;}'}</style>\n {createElement(\n 'ha-card',\n {\n class: className,\n style: {\n height: '100%',\n boxSizing: 'border-box',\n display: 'flex',\n flexDirection: 'column',\n justifyContent: justify,\n },\n },\n children,\n )}\n </>\n );\n}\n","import type { RefObject } from 'preact';\nimport { useEffect, useRef } from 'preact/hooks';\n\nexport interface ElementSize {\n width: number;\n height: number;\n}\n\nexport type ResizeCallback = (size: ElementSize) => void;\n\n/**\n * Observe an element's size via ResizeObserver. The callback fires:\n *\n * 1. Once after mount, with the element's current size.\n * 2. Whenever the element's size changes.\n * 3. Whenever `deps` change, re-firing with the current size — so callers\n * can re-run draws when their inputs change without re-creating the\n * observer.\n *\n * The callback is suppressed only while the element is detached from the\n * document. Zero width/height is delivered to the callback as-is — consumers\n * that need to skip degenerate sizes (e.g. canvas painters where a 0-sized\n * drawImage throws InvalidStateError) should add their own early return.\n *\n * Sizes are read from `offsetWidth` / `offsetHeight` (CSS pixels, includes\n * padding + border). The callback is held in a ref, so passing a fresh\n * function each render is safe — it never re-creates the observer.\n *\n * @example\n * const containerRef = useRef<HTMLDivElement>(null);\n * const canvasRef = useRef<HTMLCanvasElement>(null);\n *\n * useResizeObserver(\n * containerRef,\n * ({ width, height }) => {\n * if (width === 0 || height === 0) return; // optional, consumer's call\n * drawChart(canvasRef.current, forecast, width, height);\n * },\n * [forecast],\n * );\n */\nexport function useResizeObserver<T extends HTMLElement>(\n ref: RefObject<T>,\n callback: ResizeCallback,\n deps: unknown[] = [],\n): void {\n const callbackRef = useRef(callback);\n callbackRef.current = callback;\n\n // Set up the observer once per element. ResizeObserver fires once\n // synchronously-ish after `.observe()` with the current size, which\n // covers the initial draw.\n useEffect(() => {\n const element = ref.current;\n if (!element) return;\n\n const fire = () => {\n if (!element.isConnected) return;\n callbackRef.current({\n width: element.offsetWidth,\n height: element.offsetHeight,\n });\n };\n\n const observer = new ResizeObserver(fire);\n observer.observe(element);\n return () => observer.disconnect();\n // ref identity is stable across renders; observer setup runs once.\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, []);\n\n // Re-fire on dependency change. Skip the first render — the observer's\n // initial `.observe()` fire already delivers the mount-time size.\n const isFirstRun = useRef(true);\n useEffect(() => {\n if (isFirstRun.current) {\n isFirstRun.current = false;\n return;\n }\n const element = ref.current;\n if (!element || !element.isConnected) return;\n callbackRef.current({\n width: element.offsetWidth,\n height: element.offsetHeight,\n });\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, deps);\n}\n","import type { RefObject } from 'preact';\nimport { useState } from 'preact/hooks';\nimport { useResizeObserver } from './useResizeObserver';\n\n/**\n * Track the current width of a referenced element in CSS pixels.\n *\n * Returns `undefined` until the first non-zero measurement is observed, then\n * a positive number that updates as the element resizes. Once a real width\n * is captured the hook will never report `undefined` or `0` again, even\n * during HA layout transitions (dashboard switch, edit-mode toggle) — the\n * underlying ResizeObserver firings are silently dropped while the element\n * is detached or transiently zero-width, so the component renders with the\n * last good value instead of flashing through a degenerate state.\n *\n * Use this when you need a width value in JSX (responsive layout, prop to a\n * sized child). If you only need the value imperatively inside a draw\n * callback, prefer `useResizeObserver` directly — it doesn't allocate\n * component state or cause re-renders.\n *\n * @example\n * const ref = useRef<HTMLDivElement>(null);\n * const width = useWidth(ref);\n * return (\n * <div ref={ref}>\n * {width !== undefined && <Chart width={width} />}\n * </div>\n * );\n */\nexport function useWidth<T extends HTMLElement>(ref: RefObject<T>): number | undefined {\n const [width, setWidth] = useState<number | undefined>(undefined);\n useResizeObserver(ref, (size) => {\n if (size.width === 0) return;\n setWidth((prev) => (prev === size.width ? prev : size.width));\n });\n return width;\n}\n"],"names":["HACard"],"mappings":";;;AAaO,SAAS,UAAa,OAAc,KAA4B;AACrE,SAAQ,MAAM,IAAI,GAAG,GAAiC;AACxD;AAEO,SAAS,WAAc,OAAc,KAAa,MAAe;AACtE,QAAM,IAAI,KAAK,EAAE,KAAA,CAAM;AACzB;ACVO,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;ACFA,MAAM,sBAAuC,MAAM,MAAM;AAAC;AAW1D,MAAM,YAAY,cAA8B,IAAI;AAY7C,SAAS,WAAW;AAAA,EACzB;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,GAAoB;AAClB,QAAM,UAAU,OAAO,IAAI;AAC3B,UAAQ,UAAU;AAElB,QAAM,UAAU,kBAAkB,MAAM,QAAQ,OAAO;AAEvD,QAAM,WAAW,OAAA;AACjB,MAAI,CAAC,SAAS,kBAAkB,UAAU,6BAAa,IA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A,EAGxB,GAAG,CAAA,CAAE;AAIL,QAAM,aAAa,OAAO,IAAI;AAC9B,YAAU,MAAM;AACd,QAAI,WAAW,SAAS;AACtB,iBAAW,UAAU;AACrB;AAAA,IACF;AACA,UAAM,UAAU,IAAI;AACpB,QAAI,CAAC,WAAW,CAAC,QAAQ,YAAa;AACtC,gBAAY,QAAQ;AAAA,MAClB,OAAO,QAAQ;AAAA,MACf,QAAQ,QAAQ;AAAA,IAAA,CACjB;AAAA,EAEH,GAAG,IAAI;AACT;AC1DO,SAAS,SAAgC,KAAuC;AACrF,QAAM,CAAC,OAAO,QAAQ,IAAI,SAA6B,MAAS;AAChE,oBAAkB,KAAK,CAAC,SAAS;AAC/B,QAAI,KAAK,UAAU,EAAG;AACtB,aAAS,CAAC,SAAU,SAAS,KAAK,QAAQ,OAAO,KAAK,KAAM;AAAA,EAC9D,CAAC;AACD,SAAO;AACT;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "preact-homeassistant",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "description": "Preact hooks and helpers for building Home Assistant custom cards",
5
5
  "author": {
6
6
  "name": "Stu Kabakoff",
package/src/HACard.tsx ADDED
@@ -0,0 +1,58 @@
1
+ import { type ComponentChildren, createElement } from 'preact';
2
+
3
+ // `align` controls how content sits within the height HA assigns. Friendly
4
+ // aliases map to flex justify-content; any raw flex value also passes through.
5
+ // Default 'top' keeps content compact at the top (today's look, but the card
6
+ // box now fills the slot). Use 'center', 'bottom', or 'space-between' to spread.
7
+ export type HACardAlign =
8
+ | 'top'
9
+ | 'center'
10
+ | 'bottom'
11
+ | 'flex-start'
12
+ | 'flex-end'
13
+ | 'space-between'
14
+ | 'space-around'
15
+ | 'space-evenly';
16
+
17
+ const ALIGN_ALIASES: Record<string, string> = { top: 'flex-start', bottom: 'flex-end' };
18
+
19
+ interface HACardProps {
20
+ align?: HACardAlign;
21
+ class?: string;
22
+ children?: ComponentChildren;
23
+ }
24
+
25
+ // Drop-in replacement for a raw <ha-card> that fills the height Home Assistant
26
+ // assigns. In the sections (grid) layout HA gives the card's host element a
27
+ // definite height; a plain ha-card collapses to natural content height and
28
+ // renders slightly short. HACard makes the host and ha-card fill that height
29
+ // and flex-distributes content per `align`.
30
+ //
31
+ // `ha-card` is created via createElement (rather than as a JSX intrinsic) so
32
+ // the component carries no dependency on consumers declaring it in their JSX
33
+ // IntrinsicElements — it just works when imported.
34
+ export function HACard({ align = 'top', class: className, children }: HACardProps) {
35
+ const justify = ALIGN_ALIASES[align] ?? align;
36
+ return (
37
+ <>
38
+ {/* Host must have a definite height for ha-card's 100% to resolve. In
39
+ masonry/auto-row layouts the parent height is auto, so this safely
40
+ collapses back to natural height. */}
41
+ <style>{':host{display:block;height:100%;box-sizing:border-box;}'}</style>
42
+ {createElement(
43
+ 'ha-card',
44
+ {
45
+ class: className,
46
+ style: {
47
+ height: '100%',
48
+ boxSizing: 'border-box',
49
+ display: 'flex',
50
+ flexDirection: 'column',
51
+ justifyContent: justify,
52
+ },
53
+ },
54
+ children,
55
+ )}
56
+ </>
57
+ );
58
+ }
package/src/index.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export { registerPreactCard } from './registerPreactCard';
2
+ export { HACard, type HACardAlign } from './HACard';
2
3
  export {
3
4
  HAProvider,
4
5
  useEntity,