For a generic introduction of entities, see entities architecture.
Below is an example switch entity that keeps track of its state in memory.
from homeassistant.components.switch import SwitchEntity
self._is_on = False
"""Name of the entity."""
return "My Switch"
"""If the switch is currently on or off."""
def turn_on(self, **kwargs):
"""Turn the switch on."""
self._is_on = True
def turn_off(self, **kwargs):
"""Turn the switch off."""
self._is_on = False
That's all there is to it to build a switch entity! Continue reading to learn more or check out the video tutorial.
Updating the entity
An entity represents a device. There are various strategies to keep your entity in sync with the state of the device, the most popular one being polling.
With polling, Home Assistant will ask the entity from time to time (depending on the update interval of the component) to fetch the latest state. Home Assistant will poll an entity when the
should_poll property returns
True (the default value). You can either implement your update logic using
update() or the async method
async_update(). This method should fetch the latest state from the device and store it in an instance variable for the properties to return it.
Subscribing to updates
When you subscribe to updates, your code is responsible for letting Home Assistant know that an update is available. Make sure you have the
should_poll property return
Whenever you receive a new state from your subscription, you can tell Home Assistant that an update is available by calling
schedule_update_ha_state() or async callback
async_schedule_update_ha_state(). Pass in the boolean
True to the method if you want Home Assistant to call your update method before writing the update to Home Assistant.
The entity base class has a few properties that are common among all entities in Home Assistant. These can be added to any entity regardless of the type. All these properties are optional and don't need to be implemented.
Properties should always only return information from memory and not do I/O (like network requests). Implement
async_update() to fetch data.
|attribution||string||The branding text required by the API provider.|
|available||boolean||Indicate if Home Assistant is able to read the state and control the underlying device.|
|device_class||string||Extra classification of what the device is. Each domain specifies their own. Device classes can come with extra requirements for unit of measurement and supported features.|
|entity_category||string||Classification of a non-primary entity. Set to |
|entity_picture||URL||Url of a picture to show for the entity.|
|extra_state_attributes||dict||Extra information to store in the state machine. It needs to be information that further explains the state, it should not be static information like firmware version.|
|name||string||Name of the entity|
|should_poll||boolean||Should Home Assistant check with the entity for an updated state. If set to |
|unique_id||string||A unique identifier for this entity. Needs to be unique within a platform (ie |
The following properties are also available on entities. However, they are for advanced use only and should be used with caution.
|entity_registry_enabled_default||boolean||Indicate if the entity should be enabled or disabled when first added to the entity registry. This includes fast-changing diagnostic entities or, assumingly less commonly used entities. For example, a sensor exposing RSSI or battery voltage should typically be set to |
|force_update||boolean||Write each update to the state machine, even if the data is the same. Example use: when you are directly reading the value from a connected sensor instead of a cache. Use with caution, will spam the state machine.|
|icon||icon||Icon to use in the frontend. Icons start with |
The following properties are used and controlled by Home Assistant, and should not be overridden by integrations.
|enabled||boolean||Indicate if entity is enabled in the entity registry. It also returns |
Writing property methods for each property is just a couple of lines of code, for example
def icon(self) -> str | None:
"""Icon of the entity."""
Entity class or instance attributes
Alternatively, a shorter form is to set Entity class or instance attributes according to either of the following patterns:
_attr_icon = "mdi:door"
def __init__(self, icon: str) -> None:
self._attr_icon = icon
This does exactly the same as the first example but relies on a default
implementation of the property in the base class. The name of the attribute
_attr_ followed by the property name. For example, the default
device_class property returns the
_attr_device_class class attribute.
Not all entity classes support the
_attr_ attributes for their entity
specific properties, please refer to the documentation for the respective
entity class for details.
If an integration needs to access its own properties it should access the property (
self.name), not the class or instance attribute (
Use these lifecycle hooks to execute code when certain events happen to the entity. All lifecycle hooks are async methods.
Called when an entity has their entity_id and hass object assigned, before it is written to the state machine for the first time. Example uses: restore the state, subscribe to updates or set callback/dispatch function/listener.
Called when an entity is about to be removed from Home Assistant. Example use: disconnect from the server or unsubscribe from updates.
Changing the entity model
If you want to add a new feature to an entity or any of its subtypes (light, switch, etc), you will need to propose it first in our architecture repo. Only additions will be considered that are common features among various vendors.