mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 05:07:41 +00:00
Add support for attribute caching to the switch platform (#106258)
This commit is contained in:
parent
ebdf7b9c8c
commit
83e1ba338a
@ -5,6 +5,7 @@ from datetime import timedelta
|
|||||||
from enum import StrEnum
|
from enum import StrEnum
|
||||||
from functools import partial
|
from functools import partial
|
||||||
import logging
|
import logging
|
||||||
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
@ -32,6 +33,11 @@ from homeassistant.loader import bind_hass
|
|||||||
|
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from functools import cached_property
|
||||||
|
else:
|
||||||
|
from homeassistant.backports.functools import cached_property
|
||||||
|
|
||||||
SCAN_INTERVAL = timedelta(seconds=30)
|
SCAN_INTERVAL = timedelta(seconds=30)
|
||||||
|
|
||||||
ENTITY_ID_FORMAT = DOMAIN + ".{}"
|
ENTITY_ID_FORMAT = DOMAIN + ".{}"
|
||||||
@ -108,13 +114,18 @@ class SwitchEntityDescription(ToggleEntityDescription, frozen_or_thawed=True):
|
|||||||
device_class: SwitchDeviceClass | None = None
|
device_class: SwitchDeviceClass | None = None
|
||||||
|
|
||||||
|
|
||||||
class SwitchEntity(ToggleEntity):
|
CACHED_PROPERTIES_WITH_ATTR_ = {
|
||||||
|
"device_class",
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class SwitchEntity(ToggleEntity, cached_properties=CACHED_PROPERTIES_WITH_ATTR_):
|
||||||
"""Base class for switch entities."""
|
"""Base class for switch entities."""
|
||||||
|
|
||||||
entity_description: SwitchEntityDescription
|
entity_description: SwitchEntityDescription
|
||||||
_attr_device_class: SwitchDeviceClass | None
|
_attr_device_class: SwitchDeviceClass | None
|
||||||
|
|
||||||
@property
|
@cached_property
|
||||||
def device_class(self) -> SwitchDeviceClass | None:
|
def device_class(self) -> SwitchDeviceClass | None:
|
||||||
"""Return the class of this entity."""
|
"""Return the class of this entity."""
|
||||||
if hasattr(self, "_attr_device_class"):
|
if hasattr(self, "_attr_device_class"):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user