Integration Service
window.lcards.core.integrationService: Backend availability probe, capability flags, global storage convenience API and the push channel from the Python actions.
Overview
IntegrationService probes the LCARdS Home Assistant integration on startup to learn whether the Python backend is installed and responding. Services and cards that call backend WebSocket commands check integrationService.available (or await integrationService.onReady()) first, so LCARdS degrades gracefully when the integration is absent.
It also wraps the global storage commands in four convenience methods (readStorage(), writeStorage(), deleteStorage(), resetStorage()), and after a successful probe subscribes to the lcards_event push channel that the integration's actions use to reach every browser.
File
src/core/services/IntegrationService.js (extends BaseService). Created in step 23 of the core initialization order; it has no initialize() call there and probes by itself.
Responsibilities
- Send
lcards/infoon the first HASS update that has a liveconnection, and retry a failed probe - Set
available,version,storageKeyCount,optionsandcapabilitiesfrom the reply - Resolve
onReady()once the probe chain has finished, successfully or not - Provide the four global storage helpers with built-in availability guards
- Subscribe to
lcards/subscribeand act on each pushedaction, after checking device and user targeting
WebSocket Commands Used
| Command | Used by | Admin only |
|---|---|---|
lcards/info | Probe (_runProbeAttempt()), refreshOptions() | No |
lcards/storage/get | readStorage(key) | Only for _user_* keys and for a read without a key (the full store) |
lcards/storage/set | writeStorage(updates) | Yes |
lcards/storage/delete | deleteStorage(key) | Yes |
lcards/storage/reset | resetStorage() | Yes |
lcards/subscribe | _startEventListener() | No |
The admin checks are in custom_components/lcards/websocket_api.py (_require_admin(), error code unauthorized). Other commands (storage/dump, storage/delete_many, the per-user and per-device commands, fire_event, diagnostics) are sent by other code directly; see Scoped Settings and the Backend WebSocket API.
Lifecycle
updateHass(hass) is called by the core on every HASS distribution. It always caches the HASS (retries and storage calls use the latest), and starts the probe chain exactly once: on the first HASS that has a connection (_probed guards it). The module-load stub HASS has no connection, so the probe starts with the first real HASS.
A failed attempt is logged with its reason and retried after 1 s, 3 s and 8 s (PROBE_RETRY_DELAYS_MS). Only when the fourth attempt fails does the service settle into degraded mode. onReady() resolves once, on the first success or after the final failure, so consumers never see a transient "unavailable".
Calling initialize(hass) again starts a fresh probe chain (it resets the attempt counter); onReady() has already resolved by then and does not resolve again.
Reconnects
initialize() also attaches a ready and a disconnected listener to the connection (_watchConnection(), once per connection object; hass.connection is the same object across reconnects, so the listeners stay for the page's life and never remove themselves mid-dispatch). On every ready the service gets the backend back itself (_reconnectBackend()): it sends lcards/info, takes the reply, and subscribes to lcards/subscribe again. A failure retries after 1, 2, 5, 10 and then every 30 s (RECONNECT_DELAYS_MS) while the socket is up, and stops after 10 minutes (RECONNECT_GIVE_UP_MS) until the next reconnect.
This matters after a Home Assistant restart (#426): the page often reconnects before the LCARdS integration has registered its commands. home-assistant-js-websocket resubscribes once and drops a subscription that fails, so with its own resubscribe the page never heard an lcards.* action again until a reload. The subscription is therefore made with { resubscribe: false }. The same retry runs after a first probe chain that ended degraded (a kiosk that loaded while Home Assistant was still starting), so available turns true once the integration answers. A failed lcards/subscribe retries the same way.
Properties
| Property | Type | Description |
|---|---|---|
available | boolean | true once lcards/info answered with available: true. Default false |
version | string | null | Version reported by the integration (DOMAIN_VERSION from const.py) |
storageKeyCount | number | null | Number of keys in backend storage (null while storage is not initialised) |
options | Object | null | Options of the first config entry: show_panel, sidebar_title, sidebar_icon, log_level, enable_previews; null when there is no entry |
capabilities | Set<string> | Capability strings from the reply; today the backend sends ['scoped_storage', 'scoped_storage_replace', 'own_user_reload', 'storage_get_many'] |
isReady | boolean (getter) | true once the probe chain has finished, whatever the outcome |
window.lcards.info() reports these as integration (available, a boolean), backendVersion (version) and preview (options.enable_previews) (src/lcards-main.js).
onReady() and refreshOptions()
const integration = window.lcards.core.integrationService;
await integration.onReady(); // never rejects, also when the backend is absent
if (integration.available) { /* backend calls are safe */ }Use onReady() rather than reading available early or the private _probed / _probeComplete flags. The core itself waits on it to load theme overrides and the connection overlay config.
refreshOptions() sends lcards/info again and updates options, capabilities and storageKeyCount in place (not available or version). Call it after the integration options change (for example the preview features switch) so UI reading options sees the new values without a reload. It is also window.lcards.refreshOptions() in the console.
capabilities
integration.capabilities.has('scoped_storage') // per-user / per-device storage commands exist
integration.capabilities.has('scoped_storage_replace') // their set commands take replace: true
integration.capabilities.has('own_user_reload') // a non-admin may fire reload_connection_config to their own sessionsScopedSettingsService checks scoped_storage before using the user and device scopes, and scoped_storage_replace before sending replace: true on a write. The backend sets the list in ws_lcards_info():
connection.send_result(msg["id"], {
"available": True,
"version": DOMAIN_VERSION,
"storage_key_count": storage_key_count,
"options": options,
"capabilities": ["scoped_storage", "scoped_storage_replace", "own_user_reload"],
})Global Storage Convenience API
All four methods return a fallback instead of throwing: undefined / false when the integration is unavailable, there is no connection, the call fails or the backend refuses it. Writes to the global store are admin-only on the backend; a refused call (unauthorized) is logged as "global storage needs a Home Assistant admin" and returns false.
Before offering a global write in UI, check canWriteGlobalStorage(hass) from src/core/services/storage-permissions.js (hass.user.is_admin === true), and start settings UIs on defaultSettingsScope(hass) ('global' for admins, 'user' otherwise). GLOBAL_WRITE_REFUSED_MSG in the same file is the error text to show when a save is refused. Non-admins keep their own user and device scopes through Scoped Settings.
readStorage(key) → Promise<any>
Returns the stored value for key (any JSON type), or undefined on a miss, an unavailable backend, an error or a refusal. Non-admins can read global keys; _user_<id> keys are refused (a user's own settings come from lcards/storage/user/get).
const integration = window.lcards.core.integrationService;
const prefs = await integration.readStorage('my_service_prefs');writeStorage(updates) → Promise<boolean>
Shallow-merges updates (a plain { key: value } object) into the store. Keys not in updates are kept; { key: null } stores null rather than removing the key (use deleteStorage()). Admin only.
const ok = await integration.writeStorage({ my_service_prefs: { theme: 'dark' } });
if (!ok) { /* show GLOBAL_WRITE_REFUSED_MSG or a save error */ }deleteStorage(key) → Promise<boolean>
Removes one key. Admin only.
resetStorage() → Promise<boolean>
Wipes the entire store (all keys, including every user and device record) and saves it empty. Irreversible. Admin only. Services must drop their in-memory caches afterwards, or their next save writes stale data back.
Usage pattern
import { canWriteGlobalStorage, GLOBAL_WRITE_REFUSED_MSG } from '../core/services/storage-permissions.js';
const integration = window.lcards.core.integrationService;
await integration.onReady();
if (!integration.available) {
lcardsLog.warn('[MyService] Integration unavailable: settings will not be persisted');
} else if (canWriteGlobalStorage(hass)) {
const ok = await integration.writeStorage({ my_setting: value });
if (!ok) this._error = GLOBAL_WRITE_REFUSED_MSG;
}Push Channel
After a successful probe, _startEventListener() subscribes with connection.subscribeMessage(cb, { type: 'lcards/subscribe' }). The backend's ws_subscribe() forwards every lcards_event on the Home Assistant event bus to the subscriber as the event's data dict, with no envelope. lcards/subscribe is not admin-gated (Home Assistant's own subscribeEvents is admin-only for custom event types), so non-admin dashboards receive the events too. The subscription is idempotent (_eventUnsubscribe and _eventSubscribing guards) and ends with the WebSocket connection; the service makes a new one after each reconnect (see Reconnects).
Targeting
Events are broadcast to every subscribed browser. _handleLcardsEvent() first calls _isEventTargetedAtMe(payload):
- No non-empty
target_device_idsand no non-emptytarget_user_ids: broadcast, accept. target_device_idscontains this browser'sdeviceIdentityManager.getDeviceId(): accept.target_user_idscontains the signed-in user's ID (hass.user.id, falling back to the core's current HASS): accept.- Otherwise: drop silently.
Both lists together mean union: matching either is enough. The Python side resolves target_device_names (against the stored device display_name, case-insensitive) and target_user_names (against Home Assistant user names) into these ID lists in _async_resolve_targets() (custom_components/lcards/services.py), and raises a validation error instead of broadcasting when targeting was asked for but matched nobody. The filtering itself happens in the browser.
Handled actions
payload.action | What the browser does |
|---|---|
fonts_changed | assetManager.reloadUserFonts(hass, payload.revision) when the backend has the fonts capability: the user fonts stylesheet is fetched again and the fonts record re-read (LCARdS Config > Fonts saved, or a file was added or deleted). After the probe the record is read once (loadUserFontSettings), and again after a reconnect |
reload | window.location.reload() |
set_log_level | window.lcards.setGlobalLogLevel(payload.level); levels include off |
set_alert_mode | window.lcards.setAlertMode(payload.mode, { skipHelperSync: true }): a local, targeted alert change that does not write back to input_select.lcards_alert_mode (that would reach every tab) |
clear_effect | window.lcards.screenEffect.clearSlot(payload.slot), or .clear() for all slots when slot is omitted |
play_sound | soundManager.playAsset(payload.asset_key) when an asset is named (bypasses scheme and overrides), else soundManager.play(payload.event_type) |
trigger_effect | For each slot in payload.layers (backdrop, color, canvas): null clears that slot (sem.clearSlot()); { preset, ...params } applies it, with sem.applySlotFor(slot, preset, params, duration) when payload.duration is set (each slot keeps its own timer) or sem.applySlot() otherwise. A slot left out is not touched; an unknown slot is logged |
show_portal_card | portalOverlayManager.show('ha-service', { content, layers, position, width, height, duration, dismiss }) (defaults: position: 'center', width/height: 'auto', dismiss: true) |
clear_portal_card | portalOverlayManager.hide('ha-service') |
fly_to | window.lcards.cards.astrometrics.flyTo(payload.card_id, { point } or { view }, { duration, warp }) |
set_clock | window.lcards.cards.astrometrics.setClock(payload.card_id, { date, rate }) |
borg_assimilate | borgAssimilationManager.assimilate(opts), mapping intro_duration → duration, transition_style → transitionStyle, intro_layers → intro, persistent_layers → persistentLayers, fontSwap, paletteHue, sound |
borg_deassimilate | borgAssimilationManager.deassimilate(opts), mapping with_outro → withOutro, outro_layers → outroLayers, revert_transition_style → revertTransitionStyle, sound |
reload_connection_config | connectionOverlayService.loadConfig(), then a lcards-connection-config-changed document event for an open config panel; skipped when payload.origin_device_id is this device (the saving tab already applied it) |
| anything else | Logged at debug and ignored |
Sources of events
- Actions in
services.pyfirelcards_eventthrough_fire_targeted_event(hass, action, extra, device_ids, user_ids), which leaves the target keys out of broadcast payloads. - Browsers relay events with
lcards/fire_event(admin only), for example the connectivity tab'sreload_connection_configandreload, andConnectionOverlayService. The one non-admin exception isreload_connection_configtargeted at the caller's own user id, whichConnectionOverlayService.saveConfig()sends after an own-user save (Backend API).
→ See Home Assistant Services for the Python action reference. → See Home Assistant Integration Architecture for the Python side and its sequence diagrams.
Degraded Mode
When available === false (integration not installed or not set up):
- The storage helpers return
undefined/falsewithout sending anything; no throws. - Services keep in-memory or built-in defaults; they do not fall back to
localStorage(the connection overlay keeps its ownlocalStoragecache for offline start). - Cards keep working: the bundle is loaded independently of the probe.
window.lcards.info()reportsintegration: falseandbackendVersion: null.