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

Add POC for image #353

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
8 changes: 7 additions & 1 deletion custom_components/bermuda/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import logging
from typing import Final

from homeassistant.const import Platform

from .log_spam_less import BermudaLogSpamLess

NAME = "Bermuda BLE Trilateration"
Expand Down Expand Up @@ -33,7 +35,11 @@
SWITCH = "switch"
DEVICE_TRACKER = "device_tracker"
# PLATFORMS = [BINARY_SENSOR, SENSOR, SWITCH]
PLATFORMS = [SENSOR, DEVICE_TRACKER]
PLATFORMS = [
Platform.SENSOR,
Platform.DEVICE_TRACKER,
# Platform.IMAGE
]

# Should probably retreive this from the component, but it's in "DOMAIN" *shrug*
DOMAIN_PRIVATE_BLE_DEVICE = "private_ble_device"
Expand Down
79 changes: 79 additions & 0 deletions custom_components/bermuda/image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
"""Image platform for Bermuda BLE Trilateration."""

from datetime import datetime
from random import randint
from typing import TYPE_CHECKING

import homeassistant.util.dt as dt_util
import svgwrite
from homeassistant import config_entries
from homeassistant.components.image import ImageEntity
from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.util import slugify

from .const import DOMAIN
from .entity import BermudaGlobalEntity

if TYPE_CHECKING:
from .coordinator import BermudaDataUpdateCoordinator


async def async_setup_entry(
hass: HomeAssistant,
entry: config_entries.ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Setup sensor platform."""
coordinator: BermudaDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
async_add_entities([BermudaImage(hass, coordinator, entry, "Main floor")])


class BermudaImage(BermudaGlobalEntity, ImageEntity):
"""Image entity for Bermuda."""

_attr_content_type = "image/svg+xml"
image_last_updated: datetime

def __init__(self, hass, coordinator, config_entry, floor) -> None:
super().__init__(coordinator, config_entry)
ImageEntity.__init__(self, hass)
self.image_bytes = b""
self.floor = floor
self._attr_entity_category = EntityCategory.DIAGNOSTIC
self._attr_image_last_updated = dt_util.utcnow()

def _handle_coordinator_update(self) -> None:
dwg = svgwrite.Drawing(size=(500, 500))
dwg.add(dwg.rect(insert=(0, 0), size=("100%", "100%"), fill="white"))

coordinates = [(20, 20), (100, 100)]
for x, y in coordinates:
radius = randint(10, 40) # noqa: S311

dwg.add(dwg.circle(center=(x, y), r=2, fill="black"))

dwg.add(dwg.circle(center=(x, y), r=radius, stroke="black", fill="none"))

# Convert SVG drawing to bytes
self.image_bytes = dwg.tostring().encode()
self._attr_image_last_updated = dt_util.utcnow()
super()._handle_coordinator_update()

async def async_image(self) -> bytes | None:
"""Return bytes of SVG image."""
return self.image_bytes

@property
def unique_id(self):
"""
"Uniquely identify this sensor so that it gets stored in the entity_registry,
and can be maintained / renamed etc by the user.
"""
return f"BERMUDA_GLOBAL_MAP_{slugify(self.floor)}"

@property
def name(self):
"""Gets the name of the sensor."""
return f"{self.floor} Map"
2 changes: 1 addition & 1 deletion custom_components/bermuda/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@
"integration_type": "device",
"iot_class": "calculated",
"issue_tracker": "https://github.com/agittins/bermuda/issues",
"requirements": [],
"requirements": ["pillow"],
"version": "0.0.0"
}