ha-entity-platform
Add or change a Home Assistant entity platform or an individual entity in this custom integration — sensor, binary_sensor, switch, button, number, select, fan, climate, cover, light, lock, and friends. Use when asked to "add a sensor", "add a new entity", "add a platform", "expose <value> as an entity", "add an entity to the device", "make this entity diagnostic", or when changing EntityDescription metadata, device classes, state classes, entity categories, or availability. Covers the platform directory layout, the coordinator-backed entity contract, unique IDs, device grouping, and the translation and validation steps that must follow. SYMPTOMS — load this if you are about to: hardcode `name=` or `icon=` in an EntityDescription instead of `translation_key`; omit `state_class` on a numeric measurement; call the API client from an entity property; set `_attr_unique_id` or `_attr_device_info` in a platform entity; or put two entity classes in one file.
- License 许可
- MIT
- 所属仓库
- amg0/ha_ksenia
- 最近更新
- 2026-11-15
- Stars
- ★ 0
- Forks
- 0
01 前言 Front Matter
- name
- ha-entity-platform
- description
- Add or change a Home Assistant entity platform or an individual entity in this custom integration — sensor, binary_sensor, switch, button, number, select, fan, climate, cover, light, lock, and friends. Use when asked to "add a sensor", "add a new entity", "add a platform", "expose <value> as an entity", "add an entity to the device", "make this entity diagnostic", or when changing EntityDescription metadata, device classes, state classes, entity categories, or availability. Covers the platform directory layout, the coordinator-backed entity contract, unique IDs, device grouping, and the translation and validation steps that must follow. SYMPTOMS — load this if you are about to: hardcode `name=` or `icon=` in an EntityDescription instead of `translation_key`; omit `state_class` on a numeric measurement; call the API client from an entity property; set `_attr_unique_id` or `_attr_device_info` in a platform entity; or put two entity classes in one file.
02 资源文件 6
- 参考文档references/climate-and-appliances.md3.2 KB
- 参考文档references/controls.md6.2 KB
- 参考文档references/lifecycle.md1.4 KB
- 参考文档references/other-platforms.md6.9 KB
- 参考文档references/platform-members.md7.9 KB
- 参考文档references/sensors.md3.3 KB
03 SKILL.md 正文
Add or change an entity platform
Entities are the user-visible surface of the integration. Getting the metadata right at creation time matters more than usual: unique IDs, device classes, and entity IDs are effectively permanent once a user has installed the integration.
Read blueprint.entities.instructions.md first —
it holds the rules this procedure assumes: base-class inheritance and MRO, required EntityDescription fields, the
coordinator-only data rule, availability, device info fields, PARALLEL_UPDATES, and device registry ownership. Copilot
injects it automatically when you edit an entity file; other agents must open it. Python style is in
blueprint.python.instructions.md.
This skill is the procedure and the decisions — it does not restate those rules.
Before implementing a platform, read its row — required members and the trap it carries:
| File | Platforms |
|---|---|
references/sensors.md |
sensor, binary_sensor — state class and unit rules |
references/controls.md |
switch, button, number, select, text, date, datetime, time, scene, siren, cover, valve, lock, alarm_control_panel, fan, light |
references/climate-and-appliances.md |
climate, water_heater, humidifier, vacuum, lawn_mower, media_player |
references/other-platforms.md |
event, update, image, camera, device_tracker, notify, todo, infrared and radio frequency consumers |
Lifecycle hooks beyond async_added_to_hass — any platform: references/lifecycle.md.
Before you write code
Clarify — ask the developer if any of this is missing:
- Which platform (
sensor,switch, …) and whether it already exists in the integration. - Which field of
coordinator.databacks the entity, and its unit and value range. - Whether it is a primary entity or a diagnostic/config entity (
EntityCategory). - For writable platforms: which API method performs the write.
Then read the closest existing sibling — for a new sensor that is
custom_components/<domain>/sensor/air_quality.py. Match it rather than inventing a second style.
Layout
Each platform is a package, one entity class per file:
custom_components/<domain>/<platform>/
├── __init__.py # async_setup_entry + aggregated ENTITY_DESCRIPTIONS
└── <group>.py # ENTITY_DESCRIPTIONS + entity class for one logical group
Once a value_fn description carries the value lookup, one entity class serves every group on the platform, and
copying it per file would be the duplication value_fn exists to remove. Give it its own module and leave the group
files holding descriptions only — this is what sensor/ does:
custom_components/<domain>/sensor/
├── __init__.py # async_setup_entry + aggregated ENTITY_DESCRIPTIONS
├── entity.py # the description subclass and the single entity class
├── air_quality.py # ENTITY_DESCRIPTIONS only
└── diagnostic.py # ENTITY_DESCRIPTIONS only
Keep the first shape while a platform has one group, and split when a second class would otherwise be a copy of the first.
Procedure
1. Decide the metadata, then write the descriptions
The mechanical rules are in the instructions file. What this step actually costs you is judgement:
keybecomes part of the unique ID. Pick it once — renaming it later is a breaking change (ha-breaking-changes).- Primary, diagnostic or config? Anything a user would not put on a dashboard is
EntityCategory.DIAGNOSTIC. Useful to few and noisy for everyone else — a signal strength, a value that changes every second — means disabled by default; hidden is something else. Which is which, and why the choice is final: Disabled and Hidden By Default. - Is there a matching
device_class? Check the platform's enum before inventing units or icons. A device class buys unit conversion, correct icons and voice-assistant behaviour for free. - Is the value a measurement? Then it needs a
state_class, or the user gets no history.
2. Entity class
Implement only the platform members and the value extraction; everything else comes from the base class.
class {ClassPrefix}AirQualitySensor(SensorEntity, {ClassPrefix}Entity):
"""Air quality sensor."""
entity_description: {ClassPrefix}SensorEntityDescription
@property
def native_value(self) -> StateType:
"""Return the current value."""
return self.entity_description.value_fn(self.coordinator.data)
A value that is simply missing from this poll is unknown, not unavailable — returning None above already does
that, and it keeps the entity usable in templates and automations.
Only override available when a single entity can genuinely be gone while the rest of the device is fine — a
missing key that means a missing sub-device. The base CoordinatorEntity already handles the whole-device case:
@property
def available(self) -> bool:
"""Return True if the backing sub-device is still present."""
return super().available and self.entity_description.key in self.coordinator.data
3. Register the platform
For a brand-new platform only:
- Add the
Platform.<NAME>member to thePLATFORMSlist incustom_components/<domain>/__init__.py(keep the list alphabetical). - Nothing else —
async_setup_entryalready forwards withasync_forward_entry_setups(entry, PLATFORMS)andasync_unload_entryalready tears down withasync_unload_platforms(entry, PLATFORMS).
4. Device grouping
The base entity places every entity on the config entry's device. Only deviate when the integration genuinely models
more than one device, and then decide whether each one is a part of one product (a child device) or a separate
product behind a hub (via_device_id) — the decision table and the single-owner rules are in
device-registry.md.
When devices come and go at runtime (dynamic-devices), add entities from a coordinator listener that diffs
set(coordinator.data) against a known_devices set. If the integration also removes devices that are gone
(stale-devices), drop each removed ID from known_devices too — otherwise a device that returns is never added
again until the entry reloads. Both rules: quality-scale-rules.md.
5. Translations
Add the translation_key under entity.<platform>.<key>.name in translations/en.json, plus state keys for enum
sensors. Do not touch other language files. See ha-translations.
6. Validate
script/lint && script/type-check
script/hassfest
script/test
Then restart Home Assistant (./script/develop) and confirm the entity appears with the expected name, unit, icon, and
category. If it does not appear at all, switch to ha-coordinator-debug.
Something happened, rather than something is
For a button press, a doorbell ring, a fault report — anything that occurs instead of holds — reach for an
event entity, not a sensor.
The anti-pattern: do not represent an event as entity state. A binary sensor that is on for 30 seconds after a
doorbell ring is wrong in both directions — it invents a duration the device never reported, and it loses a second
ring inside the window. event entities exist for exactly this.
Firing on the event bus directly is the fallback for what does not fit an entity at all:
- Event type is
<domain>_event; include thedevice_idfrom the device registry in the payload. - The wiring belongs in
__init__.py, not in an entity — an entity that is disabled would never subscribe. - A device that only fires events still has to be registered in the device registry by hand; nothing else creates it.
Listening is the mirror image: prefer a helper from homeassistant.helpers.event over hass.bus.async_listen, and
wrap the unsubscribe callable every helper returns in entry.async_on_unload(...).
Do not
- Do not add an entity for data the coordinator does not already fetch — extend the coordinator first, or you will be tempted to call the API from the entity.
- Do not write
significant_change.pyorreproduce_state.pyspeculatively. The first only matters when the integration's entities are exported to Google Assistant or Alexa and are noisy enough to need filtering (async_check_significant_change(hass, old_state, old_attrs, new_state, new_attrs, **kwargs) -> bool | None). The second is only for an integration that defines its own domain — entities added to an existing platform inherit scene support from it for free. - Do not rename an existing
key,translation_key, or unique ID without readingha-breaking-changes. - Do not skip the sibling-file read in step 0. Most "new" entities are a variation of one that already exists.
- Do not raise
ConfigEntryNotReadyfrom a platform'sasync_setup_entry— the config entry setup has already finished by then and nothing catches it.
The remaining hard rules are in
blueprint.entities.instructions.md; the per-platform
members are in the references/ files listed at the top.
04 同仓库更多 Skills
issues
Triage/fix the GitHub issue backlog via branch + PR, with auto-merge on green CI for this session.
ha-translations
Add or fix user-facing strings for this Home Assistant custom integration — translations/en.json and other languages, icons.json, entity names and states, config flow labels and errors, service action descriptions, exception messages, and repair issue text. Use when asked to "update translations", "add a translation key", "translate", "fix the entity name", "add strings for the new sensor/action/option", "add icons", or when hassfest reports a missing or unused translation key. Covers the key structure, which keys each feature needs, placeholders, the icons.json contract, and the project's strict rule about only touching en.json. SYMPTOMS — load this if you are about to: write user-facing English into Python; edit a language file other than `en.json`; add an entity without its `entity.<platform>.<key>.name` key; skip `data_description` on a config flow field; or set `icon=` instead of using `icons.json`.
ha-testing
Write, fix, or extend automated tests for this Home Assistant custom integration using pytest and pytest-homeassistant-custom-component. Use when asked to "add tests", "write a test", "the tests fail", "improve coverage", "add a regression test", "test the config flow", "update snapshots", or when a behavioural change needs verification before it can be called done. Covers the tests/ layout, the conftest.py fixtures this project needs, mocking the API client, config flow and entity test patterns, registry assertions, syrupy snapshots, freezing time, and the script/test commands. SYMPTOMS — load this if you are about to: instantiate an entity class directly instead of asserting on `hass.states`; write a test without `enable_custom_integrations`; use `time.sleep` instead of `async_fire_time_changed`; silence a DeprecationWarning to make the suite pass; or commit a snapshot you have not read.
ha-service-action
Add, change, or remove a Home Assistant service action (also called a "service" or "action") in this custom integration. Use when asked to "add a service", "add an action", "expose X as a service call", "add a service that returns data", "make this callable from an automation", or when editing services.yaml, the service_actions/ package, service schemas, service response data, or the async_setup() registration. Covers the services.yaml contract, probatio schemas, selectors, entity/device targeting, response data, translated error messages, and the Bronze quality-scale rule that actions must be registered in async_setup(). SYMPTOMS — load this if you are about to: register an action in `async_setup_entry()`; add a `services.yaml` field without a selector; add an `entity_id` field where `target:` belongs; raise a plain English `HomeAssistantError`; or swallow an action failure into a log line.
ha-release
Cut a release of this Home Assistant custom integration, or prepare the commits that feed it. Use when asked to "release", "cut a version", "bump the version", "prepare release notes", "what will the next version be", "write a commit message", "why didn't release-please pick this up", or when reviewing whether a change is user-facing. Covers the Conventional Commits rules commitlint enforces here, how release-please derives the version and changelog, the pre-1.0 bump rules, script/version and script/release-notes, skipping a commit from the notes, and the commit rules this project enforces for agents. SYMPTOMS — load this if you are about to: commit without an explicit request; offer to `git push`; label a user-facing fix as `chore` (chore, like every hidden type, never reaches the changelog and never triggers a release); write a commit subject over 100 characters or in sentence case; or edit the version in `manifest.json` by hand.
ha-quality-review
Perform a structured quality review of this Home Assistant custom integration, either as a full audit or scoped to a diff. Use when asked to "review the integration", "audit the code", "check quality scale", "are we bronze/silver/gold", "is this ready to release", "review my changes", "what's missing before publishing", or before opening a pull request that touches integration code. Covers running the automated gates first, then auditing architecture, the Integration Quality Scale rules, error handling, security, performance, user experience, and documentation — and reporting findings ranked by severity with concrete fixes. SYMPTOMS — load this if you are about to: report a review that only restates linter output; claim real-device testing that did not happen; give a finding without a file path and a fix; or judge quality without running `script/hassfest` and the test suite first.