Skip to content

Commit

Permalink
Merge pull request #167 from Disane87/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
Disane87 authored Dec 16, 2024
2 parents b525677 + ab5adfb commit 91d1903
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 16 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
## [0.7.0-dev.2](https://github.com/Disane87/spoolman-homeassistant/compare/v0.7.0-dev.1...v0.7.0-dev.2) (2024-12-16)

### 🚀 Features

* added longitudinal color ([d2f2809](https://github.com/Disane87/spoolman-homeassistant/commit/d2f28098d6ebb169a7c9a0fb1e55685cc7d305a7)), closes [#150](https://github.com/Disane87/spoolman-homeassistant/issues/150)

## [0.7.0-dev.1](https://github.com/Disane87/spoolman-homeassistant/compare/v0.6.0...v0.7.0-dev.1) (2024-12-16)

### 🚀 Features

* added multicolor images for multicolor filaments ([bbd7025](https://github.com/Disane87/spoolman-homeassistant/commit/bbd7025a4f09784d741d0d94cf5604bf911df3d2)), closes [#150](https://github.com/Disane87/spoolman-homeassistant/issues/150)

## [0.6.0](https://github.com/Disane87/spoolman-homeassistant/compare/v0.5.0...v0.6.0) (2024-12-16)

### 🚀 Features
Expand Down
1 change: 1 addition & 0 deletions custom_components/spoolman/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

PUBLIC_IMAGE_PATH = "www/spoolman_images"
LOCAL_IMAGE_PATH = "/local/spoolman_images"
DEFAULT_SPOOL_COLOR_HEX = "FFFFFF"

EVENT_THRESHOLD_EXCEEDED = "spoolman_spool_threshold_exceeded"

Expand Down
60 changes: 46 additions & 14 deletions custom_components/spoolman/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from PIL import Image
from PIL import Image, ImageDraw

from .const import (
CONF_URL,
DEFAULT_SPOOL_COLOR_HEX,
DOMAIN,
EVENT_THRESHOLD_EXCEEDED,
LOCAL_IMAGE_PATH,
Expand Down Expand Up @@ -63,27 +64,58 @@ async def async_setup_entry(


def _generate_entity_picture(spool_data, image_dir):
"""Generate an entity picture with the specified color and save it to the www directory."""
filament = spool_data["filament"]

if filament.get("color_hex") is None:
"""Generate an entity picture with the specified color(s) and save it to the www directory."""
filament = spool_data.get("filament", {})

# Retrieve color(s)
multi_color_hexes = filament.get("multi_color_hexes", "").split(",")
color_hex = filament.get("color_hex", DEFAULT_SPOOL_COLOR_HEX)
multi_color_direction = filament.get("multi_color_direction", "coaxial")

# Determine colors: prioritize multi_color_hexes if available
if multi_color_hexes and any(c.strip() for c in multi_color_hexes):
colors = [c.strip() for c in multi_color_hexes if len(c.strip()) == 6]
elif color_hex:
colors = [color_hex]
else:
_LOGGER.warning(
"SpoolManCoordinator: Spool with ID '%s' has no color_hex set. Can't create entity picture.",
spool_data["id"],
"SpoolManCoordinator: Spool with ID '%s' has no valid color information.",
spool_data.get("id", "unknown"),
)
return None

color_hex = filament.get("color_hex", "FFFFFF")
image = Image.new("RGB", (100, 100), f"#{color_hex}")
image_name = f"spool_{spool_data['id']}.png"

# Check if the directory exists, and create it if it doesn't
# Create image
image_size = (100, 100)
image = Image.new("RGB", image_size, int(DEFAULT_SPOOL_COLOR_HEX, 16)) # Use integer for default white color
draw = ImageDraw.Draw(image)

# Draw colors
if len(colors) > 1:
if multi_color_direction == "coaxial":
step = image_size[0] // len(colors)
for i, color in enumerate(colors):
draw.rectangle([
(i * step, 0),
((i + 1) * step - 1, image_size[1])
], fill=f"#{color}")
elif multi_color_direction == "longitudinal":
step = image_size[1] // len(colors)
for i, color in enumerate(colors):
draw.rectangle([
(0, i * step),
(image_size[0], (i + 1) * step - 1)
], fill=f"#{color}")
else:
# Single color fallback
draw.rectangle([(0, 0), image_size], fill=f"#{colors[0]}")

# Save the image
image_name = f"spool_{spool_data.get('id', 'unknown')}.png"
os.makedirs(image_dir, exist_ok=True)

image_path = os.path.join(image_dir, image_name)
image.save(image_path)

# Get the URL for the saved image
# Return the URL for the saved image
return f"{LOCAL_IMAGE_PATH}/{image_name}"


Expand Down
2 changes: 1 addition & 1 deletion hacs.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
"name": "Spoolman",
"homeassistant": "2023.9.0",
"render_readme": true,
"filename": "spoolman-homeassistant_0.6.0.zip",
"filename": "spoolman-homeassistant_0.7.0-dev.2.zip",
"zip_release": true
}
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
colorlog==6.9.0
homeassistant>=2023.9.3
pip>=24.1,<25
ruff==0.8.0
ruff==0.8.3
pre-commit
black

0 comments on commit 91d1903

Please sign in to comment.