1
0
mirror of https://github.com/natekspencer/hacs-oasis_mini.git synced 2025-12-06 18:44:14 -05:00

Encode svg in image entity

This commit is contained in:
Nathan Spencer
2025-11-24 01:12:13 +00:00
parent a6ecd740be
commit b459e3eb9d

View File

@@ -21,9 +21,9 @@ async def async_setup_entry(
) -> None: ) -> None:
""" """
Set up image entities for Oasis devices from a config entry. Set up image entities for Oasis devices from a config entry.
Creates an OasisDeviceImageEntity for each device in the entry's runtime data and registers them with Home Assistant. Creates an OasisDeviceImageEntity for each device in the entry's runtime data and registers them with Home Assistant.
Parameters: Parameters:
hass (HomeAssistant): Home Assistant core instance. hass (HomeAssistant): Home Assistant core instance.
entry (OasisDeviceConfigEntry): Config entry containing runtime data and device registrations. entry (OasisDeviceConfigEntry): Config entry containing runtime data and device registrations.
@@ -33,10 +33,10 @@ async def async_setup_entry(
def make_entities(new_devices: list[OasisDevice]): def make_entities(new_devices: list[OasisDevice]):
""" """
Create an Image entity for each OasisDevice using the enclosing config entry's runtime data. Create an Image entity for each OasisDevice using the enclosing config entry's runtime data.
Parameters: Parameters:
new_devices (list[OasisDevice]): Devices to create image entities for. new_devices (list[OasisDevice]): Devices to create image entities for.
Returns: Returns:
list[OasisDeviceImageEntity]: A list of image entity instances, one per device. list[OasisDeviceImageEntity]: A list of image entity instances, one per device.
""" """
@@ -65,9 +65,9 @@ class OasisDeviceImageEntity(OasisDeviceEntity, ImageEntity):
) -> None: ) -> None:
""" """
Create an Oasis device image entity tied to a coordinator and a specific device. Create an Oasis device image entity tied to a coordinator and a specific device.
Initializes the entity with the provided coordinator, device, and image description and synchronizes its initial state from the coordinator. Initializes the entity with the provided coordinator, device, and image description and synchronizes its initial state from the coordinator.
Parameters: Parameters:
coordinator (OasisDeviceCoordinator): Coordinator providing updates and Home Assistant context. coordinator (OasisDeviceCoordinator): Coordinator providing updates and Home Assistant context.
device (OasisDevice): The Oasis device this entity represents. device (OasisDevice): The Oasis device this entity represents.
@@ -80,9 +80,9 @@ class OasisDeviceImageEntity(OasisDeviceEntity, ImageEntity):
def image(self) -> bytes | None: def image(self) -> bytes | None:
""" """
Provide the entity's image bytes, generating and caching an SVG from the device when available. Provide the entity's image bytes, generating and caching an SVG from the device when available.
If the device cannot produce an SVG, the entity's image URL and last-updated timestamp are set and no bytes are returned. When an SVG is produced, the content type is set to "image/svg+xml" and the SVG bytes are cached for future calls. If the device cannot produce an SVG, the entity's image URL and last-updated timestamp are set and no bytes are returned. When an SVG is produced, the content type is set to "image/svg+xml" and the SVG bytes are cached for future calls.
Returns: Returns:
bytes: The image content bytes, or `None` if no image is available yet. bytes: The image content bytes, or `None` if no image is available yet.
""" """
@@ -92,14 +92,14 @@ class OasisDeviceImageEntity(OasisDeviceEntity, ImageEntity):
self._attr_image_last_updated = dt_util.now() self._attr_image_last_updated = dt_util.now()
return None return None
self._attr_content_type = "image/svg+xml" self._attr_content_type = "image/svg+xml"
self._cached_image = Image(self.content_type, svg) self._cached_image = Image(self.content_type, svg.encode())
return self._cached_image.content return self._cached_image.content
@callback @callback
def _handle_coordinator_update(self) -> None: def _handle_coordinator_update(self) -> None:
""" """
Update image metadata and cached image when the coordinator reports changes to the device's track or progress. Update image metadata and cached image when the coordinator reports changes to the device's track or progress.
If the device's track_id or progress changed and updates are allowed (the device is playing or there is no cached image), update image last-updated timestamp, record the new track_id and progress, clear the cached image to force regeneration, and set the image URL to UNDEFINED when the track contains inline SVG content or to the device's track_image_url otherwise. When Home Assistant is available, propagate the update to the base class handler. If the device's track_id or progress changed and updates are allowed (the device is playing or there is no cached image), update image last-updated timestamp, record the new track_id and progress, clear the cached image to force regeneration, and set the image URL to UNDEFINED when the track contains inline SVG content or to the device's track_image_url otherwise. When Home Assistant is available, propagate the update to the base class handler.
""" """
device = self.device device = self.device
@@ -120,4 +120,4 @@ class OasisDeviceImageEntity(OasisDeviceEntity, ImageEntity):
self._attr_image_url = device.track_image_url self._attr_image_url = device.track_image_url
if self.hass: if self.hass:
super()._handle_coordinator_update() super()._handle_coordinator_update()