Agent Skillshome-assistant/frontend › ha-frontend-events

ha-frontend-events

GitHub

提供Home Assistant前端事件处理规范,指导使用HASSDomEvent类型、fireEvent及自定义事件映射,确保类型安全与标准契约。

.agents/skills/ha-frontend-events/SKILL.md home-assistant/frontend

Trigger Scenarios

实现前端事件监听器 定义自定义事件契约 分发组件内部事件

Install

npx skills add home-assistant/frontend --skill ha-frontend-events -g -y
More Options

Non-standard path

npx skills add https://github.com/home-assistant/frontend/tree/dev/.agents/skills/ha-frontend-events -g -y

Use without installing

npx skills use home-assistant/frontend@ha-frontend-events

指定 Agent (Claude Code)

npx skills add home-assistant/frontend --skill ha-frontend-events -a claude-code -g -y

安装 repo 全部 skill

npx skills add home-assistant/frontend --all -g -y

预览 repo 内 skill

npx skills add home-assistant/frontend --list

SKILL.md

Frontmatter
{
    "name": "ha-frontend-events",
    "description": "Home Assistant frontend event patterns. Use when typing event handlers, using HASSDomEvent types, dispatching with fireEvent, or declaring HASSDomEvents and event maps."
}

HA Frontend Events

Use this skill when implementing or reviewing event listeners and custom event contracts. Cross-load ha-frontend-components when the work also involves dialogs, forms, alerts, shortcuts, tooltips, panels, Lovelace cards, or buttons.

Event Handling

Use the event types from src/common/dom/fire_event.ts instead of plain Event, generic CustomEvent, or element casts when they express the handler contract:

  • Use HASSDomCurrentTargetEvent<T> to read the element on which the listener was registered through ev.currentTarget.
  • Use HASSDomTargetEvent<T> only to read the element that originated the event through ev.target.
  • Use HASSDomEvent<T> to read a custom event payload through ev.detail.
  • Use ValueChangedEvent<T> from src/types.ts for the standard value-changed event.
  • Prefer an event type exported by the component being listened to, such as HaSelectSelectEvent<T, Clearable> or HaDropdownSelectEvent<TValue, TData>, over reconstructing its detail type.
  • Use ActionHandlerEvent from src/data/lovelace/action_handler.ts for Lovelace tap, hold, and double-tap handlers.

Import event and element types with import type:

import type {
  HASSDomCurrentTargetEvent,
  HASSDomEvent,
  HASSDomTargetEvent,
} from "../common/dom/fire_event";
import type { HaCheckbox } from "../components/ha-checkbox";
import type { HaEntityPicker } from "../components/entity/ha-entity-picker";
import type { HaRadioGroup } from "../components/radio/ha-radio-group";
import type { ValueChangedEvent } from "../types";

Type the handler so the selected property can be read directly. Do not cast ev.currentTarget or assign it to a single-use variable:

private _scopeChanged(ev: HASSDomCurrentTargetEvent<HaRadioGroup>): void {
  this._scope = ev.currentTarget.value;
}

private _checkedChanged(ev: HASSDomTargetEvent<HaCheckbox>): void {
  this._checked = ev.target.checked;
}

private _valueChanged(ev: ValueChangedEvent<string>): void {
  this._value = ev.detail.value;
}

private _itemSelected(ev: HASSDomEvent<{ id: string }>): void {
  this._selectedId = ev.detail.id;
}

Use intersections when a handler needs more than one facet of an event. Keep the native event type when the handler reads native fields such as key, modifier keys, dataTransfer, or focus relationships:

private _entityChanged(
  ev: ValueChangedEvent<string> & HASSDomCurrentTargetEvent<HaEntityPicker>
): void {
  ev.currentTarget.value = ev.detail.value;
}

private _keyDown(
  ev: KeyboardEvent & HASSDomCurrentTargetEvent<HTMLInputElement>
): void {
  if (ev.key === "Enter") {
    this._submit(ev.currentTarget.value);
  }
}

Dispatch Home Assistant component events with fireEvent() instead of constructing Event or CustomEvent directly. Register the event name and detail type by augmenting HASSDomEvents; use undefined when an event has no detail. fireEvent() constrains event names and supplied detail, and events bubble and cross shadow boundaries by default:

fireEvent(this, "item-selected", { id: item.id });
fireEvent(this, "refresh-requested");

When an event is already registered, derive handler and listener types from its registration rather than repeating the payload shape:

private _itemSelected(
  ev: HASSDomEvent<HASSDomEvents["item-selected"]>
): void {
  this._selectedId = ev.detail.id;
}

HASSDomEvents types fireEvent() calls. Augment HTMLElementEventMap for typed listeners on HTML elements, or GlobalEventHandlersEventMap when the event is handled on global event targets.

In component files, prefer placing global event declarations after the class at the bottom of the file. Preserve the existing placement when editing established files; foundational type, helper, and mixin files commonly keep declarations near the top before their consumers.

declare global {
  interface HASSDomEvents {
    "item-selected": { id: string };
    "refresh-requested": undefined;
  }

  interface HTMLElementEventMap {
    "item-selected": HASSDomEvent<HASSDomEvents["item-selected"]>;
  }
}

Version History

  • 3c7560a Current 2026-08-20 08:57

Same Skill Collection

.agents/skills/ha-frontend-components/SKILL.md
.agents/skills/ha-frontend-contexts/SKILL.md
.agents/skills/ha-frontend-demo/SKILL.md
.agents/skills/ha-frontend-gallery/SKILL.md
.agents/skills/ha-frontend-lit/SKILL.md
.agents/skills/ha-frontend-review/SKILL.md
.agents/skills/ha-frontend-styling/SKILL.md
.agents/skills/ha-frontend-testing/SKILL.md
.agents/skills/ha-frontend-types/SKILL.md
.agents/skills/ha-frontend-user-facing-text/SKILL.md

Metadata

Files
0
Version
3c7560a
Hash
e8c17634
Indexed
2026-08-20 08:57

- 위키
Copyright © 2011-2026 iteam. Current version is 2.155.2. UTC+08:00, 2026-08-26 03:23
浙ICP备14020137号-1 $방문자$