ChatGPT has the following suggestion. Maybe I’ll try this in some near future:
Yes! You can create a Home Assistant custom integration that provides an SVG-to-JPG conversion service. This integration will:
1. Monitor a folder for SVG files (optional).
2. Convert SVG files to JPG using Python (cairosvg or Pillow).
3. Expose a Home Assistant service (image.convert_svg_to_jpg).
- Create the Custom Component Directory
Navigate to your Home Assistant custom_components folder (create it if necessary):
mkdir -p /config/custom_components/svg_to_jpg
cd /config/custom_components/svg_to_jpg
- Create manifest.json
This file tells Home Assistant about your integration.
{
"domain": "svg_to_jpg",
"name": "SVG to JPG Converter",
"version": "1.0.0",
"documentation": "https://github.com/your-repo/svg_to_jpg",
"dependencies": [],
"codeowners": ["@your-github"],
"requirements": ["cairosvg"]
}
cairosvg is a lightweight library for converting SVG to PNG or JPG.
- Create init.py
This initializes the integration and registers the service.
import logging
import os
import cairosvg
import homeassistant.helpers.config_validation as cv
import voluptuous as vol
from homeassistant.core import HomeAssistant, ServiceCall
from homeassistant.helpers.typing import ConfigType
_LOGGER = logging.getLogger(__name__)
DOMAIN = "svg_to_jpg"
SERVICE_CONVERT = "convert_svg_to_jpg"
CONF_INPUT_PATH = "input_path"
CONF_OUTPUT_PATH = "output_path"
SERVICE_SCHEMA = vol.Schema(
{
vol.Required(CONF_INPUT_PATH): cv.string,
vol.Required(CONF_OUTPUT_PATH): cv.string,
}
)
def setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Set up the SVG to JPG conversion service."""
def handle_convert(call: ServiceCall) -> None:
"""Handle the conversion request."""
input_path = call.data[CONF_INPUT_PATH]
output_path = call.data[CONF_OUTPUT_PATH]
if not os.path.exists(input_path):
_LOGGER.error("SVG file not found: %s", input_path)
return
try:
_LOGGER.info("Converting %s to %s", input_path, output_path)
cairosvg.svg2png(url=input_path, write_to=output_path.replace(".jpg", ".png"))
os.system(f"convert {output_path.replace('.jpg', '.png')} {output_path}") # Convert PNG to JPG
_LOGGER.info("Conversion successful: %s", output_path)
except Exception as e:
_LOGGER.error("Error converting file: %s", e)
hass.services.register(DOMAIN, SERVICE_CONVERT, handle_convert, schema=SERVICE_SCHEMA)
return True
- Create services.yaml
This defines how the service works.
convert_svg_to_jpg:
description: "Convert an SVG file to a JPG image"
fields:
input_path:
description: "Path to the input SVG file"
example: "/config/www/input.svg"
output_path:
description: "Path to save the output JPG file"
example: "/config/www/output.jpg"
- Add to configuration.yaml
Enable the integration:
svg_to_jpg:
Restart Home Assistant to apply the changes.
- Test the Service
Go to Developer Tools → Services and call:
service: svg_to_jpg.convert_svg_to_jpg
data:
input_path: "/config/www/input.svg"
output_path: "/config/www/output.jpg"
This will convert input.svg into output.jpg.
Next Steps
• Add a folder monitoring feature.
• Store images in a media folder (/media).
• Improve error handling.
Do you need additional features?