Add information about using entity class attribute (#945)

Co-authored-by: Teemu R. <tpr@iki.fi>
This commit is contained in:
Franck Nijhof 2021-05-22 18:19:19 +02:00 committed by GitHub
parent a122e0b78a
commit 6a85f3685f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -5,8 +5,6 @@ sidebar_label: Introduction
For a generic introduction of entities, see [entities architecture](../architecture/devices-and-services.md). For a generic introduction of entities, see [entities architecture](../architecture/devices-and-services.md).
## Basic implementation ## Basic implementation
Below is an example switch entity that keeps track of their state in memory. Below is an example switch entity that keeps track of their state in memory.
@ -91,6 +89,37 @@ The following properties are used and controlled by Home Assistant, and should n
| ------- | ------- | ------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | ------- | ------- | ------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| enabled | boolean | `True` | Indicate if entity is enabled in the entity registry. It also returns `True` if the platform doesn't support the entity registry. Disabled entities will not be added to Home Assistant. | | enabled | boolean | `True` | Indicate if entity is enabled in the entity registry. It also returns `True` if the platform doesn't support the entity registry. Disabled entities will not be added to Home Assistant. |
## Entity class attributes
Writing property methods for each property is just a couple of lines of code,
for example
```python
class MySwitch(SwitchEntity):
@property
def icon(self) -> str | None:
"""Icon of the entity."""
return "mdi:door"
...
```
Alternatively, a shorter form is to set Entity class attributes according to the
following pattern:
```python
class MySwitch(SwitchEntity):
_attr_icon = "mdi:door"
...
```
This does exactly the same as the first example. Properties that can be set
like this, start with `_attr_` followed by the property name. For example,
the `device_class` property, has the `_attr_device_class` class attribute.
## Lifecycle hooks ## Lifecycle hooks
Use these lifecycle hooks to execute code when certain events happen to the entity. All lifecycle hooks are async methods. Use these lifecycle hooks to execute code when certain events happen to the entity. All lifecycle hooks are async methods.