Files
Gangoke 279995ba13 Refactor Kobra X integration into kobrax_lan component
All entites working

- Moved all existing functionality from custom_components/kobrax to custom_components/kobrax_lan.
- Updated manifest, const, and configuration flow to reflect the new component structure.
- Reimplemented API client, coordinator, entity, and platform files under the new component.
- Added support for image entities and binary sensors.
- Ensured compatibility with Home Assistant's latest standards and practices.
2026-05-16 23:32:07 -10:00

47 lines
1.5 KiB
Python

from __future__ import annotations
import base64
import binascii
from homeassistant.components.image import ImageEntity
from homeassistant.util import dt as dt_util
from .const import DOMAIN
from .entity import KobraXEntity
class KobraXGCodeImage(KobraXEntity, ImageEntity):
def __init__(self, coordinator, entry, key: str, name: str) -> None:
KobraXEntity.__init__(self, coordinator, entry, key, name)
ImageEntity.__init__(self, coordinator.hass)
self._attr_content_type = "image/png"
async def async_image(self) -> bytes | None:
api = self.hass.data[DOMAIN][self._entry.entry_id]["api"]
files = await api.async_get_files()
if not files:
return None
active_filename = self.state_data.get("filename")
selected = None
if active_filename:
selected = next((f for f in files if f.get("filename") == active_filename), None)
if selected is None:
selected = files[0]
thumb_b64 = selected.get("thumbnail_b64")
if not thumb_b64:
return None
try:
image = base64.b64decode(thumb_b64, validate=True)
except (ValueError, binascii.Error):
return None
self._attr_image_last_updated = dt_util.utcnow()
return image
async def async_setup_entry(hass, entry, async_add_entities):
coordinator = hass.data[DOMAIN][entry.entry_id]["coordinator"]
async_add_entities([KobraXGCodeImage(coordinator, entry, "gcode_thumbnail", "GCode Thumbnail")])