mirror of
https://github.com/home-assistant/developers.home-assistant.git
synced 2025-07-15 21:36:31 +00:00
Add blog post about use of cached_property in Entity (#1907)
This commit is contained in:
parent
6503c76ba2
commit
3e8358d438
11
blog/2023-09-14-entity_cached_property.md
Normal file
11
blog/2023-09-14-entity_cached_property.md
Normal file
@ -0,0 +1,11 @@
|
||||
---
|
||||
author: J. Nick Koston
|
||||
authorURL: https://github.com/bdraco
|
||||
title: Entities with dynamic `device_class` or `attribution`
|
||||
---
|
||||
|
||||
The `Entity` base class now caches `device_class` and `attribution` using the `@cached_property` decorator. The cache allows Home Assistant to avoid recalculating the device class and attribution each time the state for the entity is written to the state machine since these values are not normally expected to change.
|
||||
|
||||
If your custom integration defines `device_class` or `attribution` using a `@property`, it should switch to using `@cached_property` instead unless these values can change at run time. Integrations should avoid changing `device_class` at run time since anything that consumes the state may be unable to handle the `device_class` unexpectedly changing.
|
||||
|
||||
The `@cached_property` decorator should be imported from `homeassistant.backports.functools` since the built-in `@cached_property` contains undocumented locking functionality that is not desirable and has been removed in cpython 3.12. For more information, see [cached_property](https://docs.python.org/3.12/library/functools.html#functools.cached_property)
|
Loading…
x
Reference in New Issue
Block a user