1
0
mirror of https://github.com/natekspencer/hacs-oasis_mini.git synced 2025-11-17 17:43:41 -05:00

Add additional features

This commit is contained in:
Nathan Spencer
2024-07-30 23:44:43 -06:00
parent e77804ec0d
commit 3b90603bef
11 changed files with 217 additions and 35 deletions

View File

@@ -2,9 +2,7 @@
from __future__ import annotations
from datetime import datetime
from homeassistant.components.image import ImageEntity, ImageEntityDescription
from homeassistant.components.image import Image, ImageEntity, ImageEntityDescription
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
@@ -12,6 +10,7 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import DOMAIN
from .coordinator import OasisMiniCoordinator
from .entity import OasisMiniEntity
from .pyoasismini.const import TRACKS
from .pyoasismini.utils import draw_svg
IMAGE = ImageEntityDescription(key="image", name=None)
@@ -21,6 +20,8 @@ class OasisMiniImageEntity(OasisMiniEntity, ImageEntity):
"""Oasis Mini image entity."""
_attr_content_type = "image/svg+xml"
_track_id: int | None = None
_progress: int = 0
def __init__(
self,
@@ -31,15 +32,34 @@ class OasisMiniImageEntity(OasisMiniEntity, ImageEntity):
"""Initialize the entity."""
super().__init__(coordinator, entry_id, description)
ImageEntity.__init__(self, coordinator.hass)
@property
def image_last_updated(self) -> datetime | None:
"""The time when the image was last updated."""
return self.coordinator.last_updated
self._handle_coordinator_update()
def image(self) -> bytes | None:
"""Return bytes of image."""
return draw_svg(self.device.track, self.device.progress, "1")
if not self._cached_image:
self._cached_image = Image(
self.content_type, draw_svg(self.device.track, self._progress, "1")
)
return self._cached_image.content
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
if self._track_id != self.device.track_id or (
self._progress != self.device.progress and self.device.access_token
):
self._attr_image_last_updated = self.coordinator.last_updated
self._track_id = self.device.track_id
self._progress = self.device.progress
self._cached_image = None
if not self.device.access_token:
self._attr_image_url = (
f"https://app.grounded.so/uploads/{track['image']}"
if (track := TRACKS.get(str(self.device.track_id)))
and "image" in track
else None
)
if self.hass:
super()._handle_coordinator_update()
async def async_setup_entry(
@@ -47,5 +67,4 @@ async def async_setup_entry(
) -> None:
"""Set up Oasis Mini camera using config entry."""
coordinator: OasisMiniCoordinator = hass.data[DOMAIN][entry.entry_id]
if coordinator.device.access_token:
async_add_entities([OasisMiniImageEntity(coordinator, entry, IMAGE)])
async_add_entities([OasisMiniImageEntity(coordinator, entry, IMAGE)])