What is an Entity?
An entity is an abstract representation of a function in Home Assistant. Each entity represents a specific capability or feature, such as a light, switch, sensor, or any other controllable or observable element in your home automation system.Entities are uniquely identified by their domain, platform, and a unique ID provided by the integration.
Entity Structure
Every entity in Home Assistant has several key components:Entity ID
Entity IDs follow the formatdomain.object_id, for example:
light.living_roomsensor.temperatureswitch.bedroom_fan
Unique ID
Each entity has a unique identifier that persists across restarts and allows the entity to be tracked in the entity registry.State
The current state of the entity (e.g., “on”, “off”, “23.5”).Attributes
Additional information about the entity, such as:friendly_name: Human-readable namedevice_class: Classification of the entityunit_of_measurement: Unit for numeric statessupported_features: Bitmask of supported featuresicon: Icon to display in the UI
Entity Base Class
TheEntity class in homeassistant/helpers/entity.py is the foundation for all entities in Home Assistant.
Key Properties
Entity Lifecycle
Entities go through several states during their lifecycle:- NOT_ADDED: Entity not yet added to a platform
- ADDING: Preparing for addition to a platform
- ADDED: Entity added and active
- REMOVED: Entity removed from platform
State Management
Entities update their state using methods provided by the base class:Writing State
Entity Categories
Entities can be categorized to indicate their purpose:- CONFIG: Configuration parameters
- DIAGNOSTIC: Diagnostic information
- None: Primary functionality (default)
Entity Description
TheEntityDescription class provides metadata for entities:
Entity Registry
The entity registry keeps track of all entities in Home Assistant. It provides:- Persistent storage of entity configuration
- Entity ID generation
- Tracking of disabled/hidden entities
- Association with devices and config entries
Registry Entry
Each entity has a registry entry containing:Creating Custom Entities
To create a custom entity, subclass theEntity base class or a domain-specific entity class:
Best Practices
Performance
Use
_attr_ properties instead of @property methods when possible for better performance.State Updates
Call
async_write_ha_state() to update the state machine whenever the entity’s state changes.Polling
Set
should_poll = False for entities that push updates, rather than being polled.Device Association
Provide
device_info to associate entities with devices for better organization.Related Resources
Devices
Learn about device management and association
State Machine
Understand how entity states are managed