home-assistant-lazy-context
Skill1 starsChanged 56 days agoApache-2.0View on GitHub
Home Assistant frontend lazy-context, memoization, and `hass` removal guidance. Use when migrating Lit components from `hass!: HomeAssistant`, `.hass=${...}`, or broad `hass` access to context slices.
In this repository51 other files it gives its agents.
- AGENTS.mdAGENTS.md
- agentic-workflowsSkill
- asking-questionsSkill
- branch-context-consumerSkill
- broSkill
- browser-accessSkill
- browser-controlSkill
- changeset-scopeSkill
- check-skill-updatesSkill
- chillSkill
- cleanup-unnecessary-variablesSkill
- code-reviewSkill
- decision-modelsSkill
- diagnoseSkill
- effect-principlesSkill
- evidence-firstSkill
- gh-stackSkill
- git-contextSkill
- github-development-rulesetsSkill
- github-repository-setupSkill
- grillingSkill
- handoffSkill
- herdr-workflowsSkill
- home-assistant-frontendSkill
- home-assistant-list-componentsSkill
- home-assistant-lit-renderingSkill
- human-step-guideSkill
- import-external-skillSkill
- lit-renderingSkill
- maintain-docsSkill
- opencode-effectSkill
- pitchfork-dev-serversSkill
- pkexec-rootSkill
- planSkill
- release-oxlint-rulesSkill
- remove-single-use-functionsSkill
- researchSkill
- retroSkill
- safe-process-signalsSkill
- session-coordinationSkill
- shared-workflowsSkill
- show-meSkill
- skill-prefixesSkill
- staged-implementationSkill
- task-focusSkill
- testingSkill
- to-questionnaireSkill
- types-enforce-tsSkill
- wranglerSkill
- writing-dot-skillsSkill
- writing-styleSkill
The file
Show its rules and text
We split this skill into the rules it gives an agent
23 rules. About 0 are common, 5 are distinctive, and 18 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈5
Distinctive · estimate
This author's own choices. Worth a look.
18
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 10
Use this skill when reviewing or editing Home Assistant frontend context, memoization, or hass removal work.
- Very specific: line 16
Do not keep hass!: HomeAssistant just to preserve the old public API unless the component is intentionally reused outside the app context …
- Very specific: line 53
Do not use. Each has a @deprecated comment naming the replacement:
- Very specific: line 67
Use @consumeLocalize() when a component only needs localize.
- Very specific: line 71
When a component adopts context, the hass property is removed.
- Very specific: line 76
Remove hass property, add @consume decorators for each needed slice.
- Very specific: line 77
Use ContextType<typeof fooContext> for typing.
- Very specific: line 82
Never pass the full HomeAssistant object.
- Distinctive: line 87
Prefer narrowing the helper over changing every caller — keeps the scope of the change small.
- Very specific: line 91
When a component adopts context, its helpers still need data.
- Very specific: line 155
Use the narrowest correct data source.
- Very specific: line 156
Do not use deprecated contexts.
- Very specific: line 157
Do not replicate hass or build local objects that mirror large parts of HomeAssistant.
- Distinctive: line 158
Do not add provider fallbacks when an existing app-level context/lazy context is already correct.
- Very specific: line 159
Do not duplicate LazyContextProvider lifecycle behavior in feature components.
- Distinctive: line 160
Do not create wrapper helpers that only forward existing utility calls.
- Very specific: line 164
Use ContextType<typeof context> for consumed context typing.
- Very specific: line 165
Use required (!) when lifecycle guarantees availability.
- Distinctive: line 166
Avoid excessive defensive guards around required consumed contexts.
- Very specific: line 172
Do not pass broad objects (hass, this, large mutable maps) into memoizeOne.
- Very specific: line 173
Do not widen signatures with pass-through localize, language, locale, or config values when class/context access already exists.
- Very specific: line 174
Do not add passthrough fields (_language, _locale, _config) only to route values into helpers.
- Distinctive: line 179
Do not add unnecessary comments or abstractions.
≈ “Common” is an estimate: at least 1 in 5 of the 213,897 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Home Assistant Lazy Context
Use this skill when reviewing or editing Home Assistant frontend context, memoization, or hass removal work.
Default Migration Rule
When a component adopts context, remove its hass property and replace each this.hass.* access with the narrowest context or value that supplies that data. Also remove .hass=${...} passthroughs to migrated child components; if a child still needs data, either migrate the child to context or pass a narrow prop such as localize, states, or Pick<HomeAssistant, "callWS">.
Do not keep hass!: HomeAssistant just to preserve the old public API unless the component is intentionally reused outside the app context tree or the user explicitly asks for compatibility.
Context Architecture (src/data/context/index.ts)
Three tiers of Lit contexts provided by context-mixin.ts:
Core Contexts (always subscribed)
| Context | Type | Contents |
registriesContext | HomeAssistantRegistries | entities, devices, areas, floors registries |
statesContext | HomeAssistant["states"] | Live entity state map |
servicesContext | HomeAssistant["services"] | Services map |
internationalizationContext | HomeAssistantInternationalization | localize, locale, language, loaders |
apiContext | HomeAssistantApi | callService, callWS, fetchWithAuth, hassUrl |
connectionContext | HomeAssistantConnection | connection, connected, debugConnection |
uiContext | HomeAssistantUI | themes, panels, sidebar, kiosk |
configContext | HomeAssistantConfig | auth, config, user, userData, systemData |
formattersContext | HomeAssistantFormatters | formatEntityState, formatEntityName, etc. |
entitiesContext | HomeAssistant["entities"] | Entity registry map |
devicesContext | HomeAssistant["devices"] | Device registry map |
areasContext | HomeAssistant["areas"] | Area registry map |
floorsContext | HomeAssistant["floors"] | Floor registry map |
Lazy Contexts (subscribed only when consumed)
Managed by LazyContextProvider -- WS subscription defers until first consumer, tears down after 5s idle when all consumers disconnect.
| Context | Type |
labelsContext | LabelRegistryEntry[] |
fullEntitiesContext | EntityRegistryEntry[] |
configEntriesContext | ConfigEntry[] |
manifestsContext | DomainManifestLookup |
Deprecated Contexts
Do not use. Each has a @deprecated comment naming the replacement:
connectionSingleContext→connectionContextlocalizeContext,localeContext→internationalizationContextconfigSingleContext,userContext,userDataContext,authContext→configContextthemesContext,selectedThemeContext,panelsContext→uiContext
Entity Decorators (src/common/decorators/consume-context-entry.ts)
@consumeEntityState({ entityIdPath })— resolves entity ID from host config path, subscribes tostatesContext, returnsHassEntity@consumeEntityStates({ entityIdPath })— same for array of entity IDs →HassEntity[]@consumeEntityRegistryEntry({ entityIdPath })— subscribes toentitiesContext, returnsEntityRegistryDisplayEntry@consumeLocalize()— subscribes tointernationalizationContextand narrows it toLocalizeFunc
Use @consumeLocalize() when a component only needs localize. Use internationalizationContext directly when it also needs locale, language, translationMetadata, loadBackendTranslation, or loadFragmentTranslation.
Migration: Removing hass
When a component adopts context, the hass property is removed. This requires revising all sub-components and helpers it passes hass to.
Decision Tree for Sub-Components
- Component can consume context directly (it's rendered in the app tree, not reused across unrelated trees):
- Remove
hassproperty, add@consumedecorators for each needed slice. - Use
ContextType<typeof fooContext>for typing.
- Remove
- Component is a shared utility (reused widely, context conversion would touch too many callers):
- Pass the individual item as a prop when scope is small (e.g.
localize: LocalizeFunc,states: HassEntities). - Pass
Pick<HomeAssistant, "callWS" | "localize">when the component needs a few unrelated slices and context is not viable. - Never pass the full
HomeAssistantobject.
- Pass the individual item as a prop when scope is small (e.g.
- Helper/utility functions (pure data functions):
- Narrow the parameter from
HomeAssistanttoPick<HomeAssistant, "callWS">(or the single value needed). - This lets the consuming component pass its context slice directly (e.g.
this._api) without reconstructinghass. - Prefer narrowing the helper over changing every caller — keeps the scope of the change small.
- Narrow the parameter from
Narrowing Helpers to Avoid Scope Explosion
When a component adopts context, its helpers still need data. Rather than passing a reconstructed hass-like object or converting every caller, narrow the helper signature so the context slice satisfies it directly:
// BEFORE: helper accepts full hass
export const fetchDeviceTriggers = (hass: HomeAssistant, deviceId: string) =>
hass.callWS<DeviceTrigger[]>({...});
// AFTER: helper accepts only what it uses
export const fetchDeviceTriggers = (
hass: Pick<HomeAssistant, "callWS">,
deviceId: string
) => hass.callWS<DeviceTrigger[]>({...});The component then passes its context slice directly:
// Component consumes apiContext (which satisfies Pick<HomeAssistant, "callWS">)
@state()
@consume({ context: apiContext, subscribe: true })
private _api!: ContextType<typeof apiContext>;
// Passes directly — no reconstruction needed
const triggers = await fetchDeviceTriggers(this._api, deviceId);This keeps the change focused: the dialog/panel adopts context, the helpers get narrowed types, and all other callers of those helpers still work unchanged (since HomeAssistant satisfies Pick<HomeAssistant, "callWS">).
When to Use Pick vs Individual Values
Pick<HomeAssistant, "callWS">— when the helper uses methods from a single context group and the existing param namehassstays readable. Keeps changes minimal.- Individual value (e.g.
states: HassEntities) — when passing a plain data map that the helper iterates/reads. More explicit, avoids thehass.prefix. Pickwith multiple keys — avoid unless strictly necessary. If a helper needs"callWS" | "localize", consider splitting it or accepting the grouped context type directly.
Consumption Pattern
@state()
@consume({ context: apiContext, subscribe: true })
private _api!: ContextType<typeof apiContext>;
@state()
@consume({ context: internationalizationContext, subscribe: true })
private _i18n!: ContextType<typeof internationalizationContext>;
@state()
@consume({ context: statesContext, subscribe: true })
private _states!: ContextType<typeof statesContext>;Usage: this._api.callWS(...), this._i18n.localize(...), this._states[entityId].
For localize-only components, prefer the helper:
@consumeLocalize()
private _localize!: LocalizeFunc;Rules
General
- Preserve runtime behavior and public API shape except for the
hassAPI intentionally removed by context migrations. - Use the narrowest correct data source; prefer contexts, lazy contexts, and entity decorators over broad
hassaccess. - Do not use deprecated contexts; use the replacement named in
@deprecatedcomments. - Do not replicate
hassor build local objects that mirror large parts ofHomeAssistant. - Do not add provider fallbacks when an existing app-level context/lazy context is already correct.
- Do not duplicate
LazyContextProviderlifecycle behavior in feature components. - Do not create wrapper helpers that only forward existing utility calls.
Typing
- Use
ContextType<typeof context>for consumed context typing. - Use required (
!) when lifecycle guarantees availability; use optional (?) only when value can be genuinely absent. - Avoid excessive defensive guards around required consumed contexts.
- For sub-components that can't use context, type props as narrow as possible: prefer
LocalizeFuncoverHomeAssistantInternationalization, preferPick<HomeAssistant, "callWS">overHomeAssistant.
Memoization
- Pass explicit narrow inputs only (actual state slice, config, primitive, small arrays).
- Do not pass broad objects (
hass,this, large mutable maps) intomemoizeOne. - Do not widen signatures with pass-through
localize,language,locale, or config values when class/context access already exists. - Do not add passthrough fields (
_language,_locale,_config) only to route values into helpers. - For rarely changed values like locale/config, read from existing class/context access at point of use.
Style
- Do not add unnecessary comments or abstractions.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

