Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve ImageEntity URL processing #217

Merged
merged 1 commit into from
Nov 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions custom_components/polestar_api/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,27 @@

from __future__ import annotations

import datetime
import logging
from dataclasses import dataclass
from functools import cached_property
from typing import Final

import homeassistant.util.dt as dt_util
from homeassistant.components.image import ImageEntity, ImageEntityDescription
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import UndefinedType

from .const import DOMAIN as POLESTAR_API_DOMAIN
from .data import PolestarConfigEntry
from .entity import PolestarEntity
from .polestar import PolestarCar

try:
from propcache import cached_property
except ImportError:
from functools import cached_property


_LOGGER = logging.getLogger(__name__)


Expand Down Expand Up @@ -82,11 +88,19 @@ def __init__(
f"polestar_{car.get_unique_id()}_{entity_description.key}"
)
self._attr_translation_key = f"polestar_{entity_description.key}"
self._attr_image_last_updated = datetime.datetime.now()

@cached_property
def image_url(self) -> str | None:
def image_url(self) -> str | None | UndefinedType:
"""Return the image URL."""
return self.car.get_value(
self.entity_description.query, self.entity_description.field_name
value = self.car.get_value(
query=self.entity_description.query,
field_name=self.entity_description.field_name,
)
if value is None:
_LOGGER.debug("No URL found")
elif isinstance(value, str) and value != self._attr_image_url:
_LOGGER.debug("Returning URL %s", value)
self._attr_image_url = value
self._attr_image_last_updated = dt_util.utcnow()
return value
return self._attr_image_url