> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/home-assistant/core/llms.txt
> Use this file to discover all available pages before exploring further.

# Entity Base Class

> Base class for all Home Assistant entities with state and attribute management.

The `homeassistant.helpers.entity` module provides the `Entity` base class and related utilities for creating entities in Home Assistant.

## Entity Class

The `Entity` class is an abstract base class that all Home Assistant entities must inherit from.

### Key Properties

<ParamField path="entity_id" type="str">
  Unique identifier for the entity (format: `domain.object_id`)
</ParamField>

<ParamField path="name" type="str | UndefinedType | None">
  Name of the entity
</ParamField>

<ParamField path="state" type="StateType">
  Current state of the entity
</ParamField>

<ParamField path="available" type="bool" default="True">
  Whether the entity is available
</ParamField>

<ParamField path="unique_id" type="str | None">
  Unique identifier for the entity across restarts
</ParamField>

<ParamField path="device_info" type="DeviceInfo | None">
  Information about the device this entity belongs to
</ParamField>

<ParamField path="icon" type="str | None">
  Icon to use in the frontend (format: `mdi:icon-name`)
</ParamField>

<ParamField path="entity_picture" type="str | None">
  URL of picture to use in the frontend
</ParamField>

<ParamField path="assumed_state" type="bool" default="False">
  True if unable to access real state of the entity
</ParamField>

<ParamField path="should_poll" type="bool" default="True">
  True if entity has to be polled for state, False if entity pushes its state
</ParamField>

<ParamField path="supported_features" type="int | None">
  Flag of features supported by the entity
</ParamField>

<ParamField path="device_class" type="str | None">
  Device class of the entity from component DEVICE\_CLASSES
</ParamField>

<ParamField path="unit_of_measurement" type="str | None">
  Unit of measurement for the entity's value
</ParamField>

<ParamField path="entity_category" type="EntityCategory | None">
  Category of the entity (config, diagnostic)
</ParamField>

### Key Methods

#### async\_write\_ha\_state

Write the state to the state machine.

```python theme={null}
@callback
def async_write_ha_state(self) -> None:
    """Write the state to the state machine."""
```

Must be called from the event loop. This is the preferred method for updating entity state.

#### async\_update\_ha\_state

Update Home Assistant with current state of entity.

<ParamField path="force_refresh" type="bool" default="False">
  If True, update entity before setting state
</ParamField>

```python theme={null}
async def async_update_ha_state(self, force_refresh: bool = False) -> None:
    """Update Home Assistant with current state of entity."""
```

#### async\_schedule\_update\_ha\_state

Schedule an update ha state change task.

<ParamField path="force_refresh" type="bool" default="False">
  If True, force entity refresh
</ParamField>

```python theme={null}
@callback
def async_schedule_update_ha_state(self, force_refresh: bool = False) -> None:
    """Schedule an update ha state change task."""
```

#### async\_on\_remove

Add a function to call when entity is removed or not added.

<ParamField path="func" type="CALLBACK_TYPE" required>
  Callback function to call on removal
</ParamField>

```python theme={null}
@callback
def async_on_remove(self, func: CALLBACK_TYPE) -> None:
    """Add a function to call when entity is removed."""
```

## Helper Functions

### generate\_entity\_id

Generate a unique entity ID based on given entity IDs or used IDs.

<ParamField path="entity_id_format" type="str" required>
  Format string for entity ID (e.g., `"light.{}"`)
</ParamField>

<ParamField path="name" type="str | None" required>
  Name to use for generating the entity ID
</ParamField>

<ParamField path="current_ids" type="list[str] | None" default="None">
  List of currently used entity IDs
</ParamField>

<ParamField path="hass" type="HomeAssistant | None" default="None">
  Home Assistant instance (required if current\_ids is None)
</ParamField>

<ResponseField name="return" type="str">
  Generated unique entity ID
</ResponseField>

```python theme={null}
from homeassistant.helpers.entity import generate_entity_id

entity_id = generate_entity_id(
    "light.{}",
    "Living Room",
    hass=hass
)
# Returns: "light.living_room"
```

### get\_capability

Get a capability attribute of an entity.

<ParamField path="hass" type="HomeAssistant" required>
  Home Assistant instance
</ParamField>

<ParamField path="entity_id" type="str" required>
  Entity ID to get capability from
</ParamField>

<ParamField path="capability" type="str" required>
  Name of capability to retrieve
</ParamField>

<ResponseField name="return" type="Any | None">
  Capability value or None if not found
</ResponseField>

### get\_device\_class

Get device class of an entity.

<ParamField path="hass" type="HomeAssistant" required>
  Home Assistant instance
</ParamField>

<ParamField path="entity_id" type="str" required>
  Entity ID
</ParamField>

<ResponseField name="return" type="str | None">
  Device class or None
</ResponseField>

### get\_supported\_features

Get supported features for an entity.

<ParamField path="hass" type="HomeAssistant" required>
  Home Assistant instance
</ParamField>

<ParamField path="entity_id" type="str" required>
  Entity ID
</ParamField>

<ResponseField name="return" type="int">
  Bitfield of supported features (0 if none)
</ResponseField>

### get\_unit\_of\_measurement

Get unit of measurement of an entity.

<ParamField path="hass" type="HomeAssistant" required>
  Home Assistant instance
</ParamField>

<ParamField path="entity_id" type="str" required>
  Entity ID
</ParamField>

<ResponseField name="return" type="str | None">
  Unit of measurement or None
</ResponseField>

## EntityDescription

Dataclass that describes Home Assistant entities.

<ParamField path="key" type="str" required>
  Key identifier for this entity
</ParamField>

<ParamField path="device_class" type="str | None" default="None">
  Device class
</ParamField>

<ParamField path="entity_category" type="EntityCategory | None" default="None">
  Category of entity
</ParamField>

<ParamField path="entity_registry_enabled_default" type="bool" default="True">
  Whether entity should be enabled when first added
</ParamField>

<ParamField path="entity_registry_visible_default" type="bool" default="True">
  Whether entity should be visible when first added
</ParamField>

<ParamField path="force_update" type="bool" default="False">
  Force state update even if value hasn't changed
</ParamField>

<ParamField path="icon" type="str | None" default="None">
  Icon for the entity
</ParamField>

<ParamField path="has_entity_name" type="bool" default="False">
  Whether the name describes only the entity itself
</ParamField>

<ParamField path="name" type="str | UndefinedType | None" default="UNDEFINED">
  Name of the entity
</ParamField>

<ParamField path="translation_key" type="str | None" default="None">
  Translation key for the entity name
</ParamField>

<ParamField path="unit_of_measurement" type="str | None" default="None">
  Unit of measurement
</ParamField>

## Constants

* `SLOW_UPDATE_WARNING = 10` - Seconds before warning about slow updates
* `FLOAT_PRECISION` - Precision for float state representation
* `CAPABILITIES_UPDATE_LIMIT = 100` - Max capability updates per hour before warning
