1
0
mirror of https://github.com/elisspace/core.git synced 2026-08-29 15:43:55 +00:00

Add image platform to kitchen_sink

This commit is contained in:
Erik
2023-03-31 09:50:03 +02:00
parent fd8725bf35
commit 8870e342ad
3 changed files with 67 additions and 1 deletions

View File

@@ -25,7 +25,7 @@ import homeassistant.util.dt as dt_util
DOMAIN = "kitchen_sink"
COMPONENTS_WITH_DEMO_PLATFORM = [Platform.SENSOR, Platform.LOCK]
COMPONENTS_WITH_DEMO_PLATFORM = [Platform.SENSOR, Platform.LOCK, Platform.IMAGE]
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:

View File

@@ -0,0 +1,66 @@
"""Demo image platform."""
from __future__ import annotations
from pathlib import Path
from homeassistant.components.image import ImageEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from homeassistant.util import dt as dt_util
async def async_setup_platform(
hass: HomeAssistant,
config: ConfigType,
async_add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Set up image entities."""
async_add_entities(
[
DemoImage(
"kitchen_sink_image_001",
"QR Code",
"image/png",
"qr_code.png",
),
]
)
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the Everything but the Kitchen Sink config entry."""
await async_setup_platform(hass, {}, async_add_entities)
class DemoImage(ImageEntity):
"""Representation of an image entity."""
def __init__(
self,
unique_id: str,
name: str,
content_type: str,
image: str,
) -> None:
"""Initialize the image entity."""
super().__init__()
self._attr_name = name
self._attr_unique_id = unique_id
self.content_type = content_type
self._image_filename = image
async def async_added_to_hass(self):
"""Set the update time."""
self._attr_last_updated = dt_util.utcnow()
async def async_image(self) -> bytes | None:
"""Return bytes of image."""
image_path = Path(__file__).parent / self._image_filename
return await self.hass.async_add_executor_job(image_path.read_bytes)

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB