mirror of
https://github.com/home-assistant/core
synced 2024-08-28 03:36:46 +02:00
Add support for attribute caching to the image platform (#106333)
This commit is contained in:
parent
f097e2a2f6
commit
85e9bc6f5a
@ -8,7 +8,7 @@ from dataclasses import dataclass
|
|||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
import logging
|
import logging
|
||||||
from random import SystemRandom
|
from random import SystemRandom
|
||||||
from typing import Final, final
|
from typing import TYPE_CHECKING, Final, final
|
||||||
|
|
||||||
from aiohttp import hdrs, web
|
from aiohttp import hdrs, web
|
||||||
import httpx
|
import httpx
|
||||||
@ -30,6 +30,12 @@ from homeassistant.helpers.typing import UNDEFINED, ConfigType, UndefinedType
|
|||||||
|
|
||||||
from .const import DOMAIN, IMAGE_TIMEOUT # noqa: F401
|
from .const import DOMAIN, IMAGE_TIMEOUT # noqa: F401
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from functools import cached_property
|
||||||
|
else:
|
||||||
|
from homeassistant.backports.functools import cached_property
|
||||||
|
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
SCAN_INTERVAL: Final = timedelta(seconds=30)
|
SCAN_INTERVAL: Final = timedelta(seconds=30)
|
||||||
@ -122,7 +128,14 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
return await component.async_unload_entry(entry)
|
return await component.async_unload_entry(entry)
|
||||||
|
|
||||||
|
|
||||||
class ImageEntity(Entity):
|
CACHED_PROPERTIES_WITH_ATTR_ = {
|
||||||
|
"content_type",
|
||||||
|
"image_last_updated",
|
||||||
|
"image_url",
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class ImageEntity(Entity, cached_properties=CACHED_PROPERTIES_WITH_ATTR_):
|
||||||
"""The base class for image entities."""
|
"""The base class for image entities."""
|
||||||
|
|
||||||
_entity_component_unrecorded_attributes = frozenset(
|
_entity_component_unrecorded_attributes = frozenset(
|
||||||
@ -143,7 +156,7 @@ class ImageEntity(Entity):
|
|||||||
self.access_tokens: collections.deque = collections.deque([], 2)
|
self.access_tokens: collections.deque = collections.deque([], 2)
|
||||||
self.async_update_token()
|
self.async_update_token()
|
||||||
|
|
||||||
@property
|
@cached_property
|
||||||
def content_type(self) -> str:
|
def content_type(self) -> str:
|
||||||
"""Image content type."""
|
"""Image content type."""
|
||||||
return self._attr_content_type
|
return self._attr_content_type
|
||||||
@ -155,12 +168,12 @@ class ImageEntity(Entity):
|
|||||||
return self._attr_entity_picture
|
return self._attr_entity_picture
|
||||||
return ENTITY_IMAGE_URL.format(self.entity_id, self.access_tokens[-1])
|
return ENTITY_IMAGE_URL.format(self.entity_id, self.access_tokens[-1])
|
||||||
|
|
||||||
@property
|
@cached_property
|
||||||
def image_last_updated(self) -> datetime | None:
|
def image_last_updated(self) -> datetime | None:
|
||||||
"""The time when the image was last updated."""
|
"""Time the image was last updated."""
|
||||||
return self._attr_image_last_updated
|
return self._attr_image_last_updated
|
||||||
|
|
||||||
@property
|
@cached_property
|
||||||
def image_url(self) -> str | None | UndefinedType:
|
def image_url(self) -> str | None | UndefinedType:
|
||||||
"""Return URL of image."""
|
"""Return URL of image."""
|
||||||
return self._attr_image_url
|
return self._attr_image_url
|
||||||
|
Loading…
Reference in New Issue
Block a user