Adjust image entity URL support (#95330)
parent
39f76b757d
commit
90c1263501
|
@ -27,7 +27,7 @@ from homeassistant.helpers.entity import Entity, EntityDescription
|
||||||
from homeassistant.helpers.entity_component import EntityComponent
|
from homeassistant.helpers.entity_component import EntityComponent
|
||||||
from homeassistant.helpers.event import async_track_time_interval
|
from homeassistant.helpers.event import async_track_time_interval
|
||||||
from homeassistant.helpers.httpx_client import get_async_client
|
from homeassistant.helpers.httpx_client import get_async_client
|
||||||
from homeassistant.helpers.typing import ConfigType
|
from homeassistant.helpers.typing import UNDEFINED, ConfigType, UndefinedType
|
||||||
|
|
||||||
from .const import DOMAIN, IMAGE_TIMEOUT # noqa: F401
|
from .const import DOMAIN, IMAGE_TIMEOUT # noqa: F401
|
||||||
|
|
||||||
|
@ -130,8 +130,10 @@ class ImageEntity(Entity):
|
||||||
# Entity Properties
|
# Entity Properties
|
||||||
_attr_content_type: str = DEFAULT_CONTENT_TYPE
|
_attr_content_type: str = DEFAULT_CONTENT_TYPE
|
||||||
_attr_image_last_updated: datetime | None = None
|
_attr_image_last_updated: datetime | None = None
|
||||||
|
_attr_image_url: str | None | UndefinedType = UNDEFINED
|
||||||
_attr_should_poll: bool = False # No need to poll image entities
|
_attr_should_poll: bool = False # No need to poll image entities
|
||||||
_attr_state: None = None # State is determined by last_updated
|
_attr_state: None = None # State is determined by last_updated
|
||||||
|
_cached_image: Image | None = None
|
||||||
|
|
||||||
def __init__(self, hass: HomeAssistant, verify_ssl: bool = False) -> None:
|
def __init__(self, hass: HomeAssistant, verify_ssl: bool = False) -> None:
|
||||||
"""Initialize an image entity."""
|
"""Initialize an image entity."""
|
||||||
|
@ -156,6 +158,11 @@ class ImageEntity(Entity):
|
||||||
"""The time when the image was last updated."""
|
"""The time when the image was last updated."""
|
||||||
return self._attr_image_last_updated
|
return self._attr_image_last_updated
|
||||||
|
|
||||||
|
@property
|
||||||
|
def image_url(self) -> str | None | UndefinedType:
|
||||||
|
"""Return URL of image."""
|
||||||
|
return self._attr_image_url
|
||||||
|
|
||||||
def image(self) -> bytes | None:
|
def image(self) -> bytes | None:
|
||||||
"""Return bytes of image."""
|
"""Return bytes of image."""
|
||||||
raise NotImplementedError()
|
raise NotImplementedError()
|
||||||
|
@ -186,24 +193,16 @@ class ImageEntity(Entity):
|
||||||
)
|
)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
if (url := await self.async_image_url()) is not None:
|
if self._cached_image:
|
||||||
# Ignore an empty url
|
return self._cached_image.content
|
||||||
if url == "":
|
if (url := self.image_url) is not UNDEFINED:
|
||||||
return None
|
if not url or (image := await _async_load_image_from_url(url)) is None:
|
||||||
if (image := await _async_load_image_from_url(url)) is None:
|
|
||||||
return None
|
return None
|
||||||
|
self._cached_image = image
|
||||||
self._attr_content_type = image.content_type
|
self._attr_content_type = image.content_type
|
||||||
return image.content
|
return image.content
|
||||||
return await self.hass.async_add_executor_job(self.image)
|
return await self.hass.async_add_executor_job(self.image)
|
||||||
|
|
||||||
def image_url(self) -> str | None:
|
|
||||||
"""Return URL of image."""
|
|
||||||
return None
|
|
||||||
|
|
||||||
async def async_image_url(self) -> str | None:
|
|
||||||
"""Return URL of image."""
|
|
||||||
return self.image_url()
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@final
|
@final
|
||||||
def state(self) -> str | None:
|
def state(self) -> str | None:
|
||||||
|
|
|
@ -54,16 +54,13 @@ class MockImageEntityInvalidContentType(image.ImageEntity):
|
||||||
class MockURLImageEntity(image.ImageEntity):
|
class MockURLImageEntity(image.ImageEntity):
|
||||||
"""Mock image entity."""
|
"""Mock image entity."""
|
||||||
|
|
||||||
|
_attr_image_url = "https://example.com/myimage.jpg"
|
||||||
_attr_name = "Test"
|
_attr_name = "Test"
|
||||||
|
|
||||||
async def async_added_to_hass(self):
|
async def async_added_to_hass(self):
|
||||||
"""Set the update time."""
|
"""Set the update time."""
|
||||||
self._attr_image_last_updated = dt_util.utcnow()
|
self._attr_image_last_updated = dt_util.utcnow()
|
||||||
|
|
||||||
async def async_image_url(self) -> str:
|
|
||||||
"""Return URL of image."""
|
|
||||||
return "https://example.com/myimage.jpg"
|
|
||||||
|
|
||||||
|
|
||||||
class MockImageNoStateEntity(image.ImageEntity):
|
class MockImageNoStateEntity(image.ImageEntity):
|
||||||
"""Mock image entity."""
|
"""Mock image entity."""
|
||||||
|
|
Loading…
Reference in New Issue