mirror of
https://github.com/esphome/esphome.git
synced 2024-11-14 19:18:09 +01:00
Revise tests and imports
This commit is contained in:
parent
f81206870d
commit
b31ac31136
1 changed files with 8 additions and 4 deletions
|
@ -6,8 +6,6 @@ import logging
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
import re
|
import re
|
||||||
|
|
||||||
from PIL import UnidentifiedImageError
|
|
||||||
|
|
||||||
from esphome import core, external_files
|
from esphome import core, external_files
|
||||||
import esphome.codegen as cg
|
import esphome.codegen as cg
|
||||||
from esphome.components import font
|
from esphome.components import font
|
||||||
|
@ -259,7 +257,7 @@ def load_svg_image(file: bytes, resize: tuple[int, int]):
|
||||||
|
|
||||||
async def to_code(config):
|
async def to_code(config):
|
||||||
# Local import only to allow "validate_pillow_installed" to run *before* importing it
|
# Local import only to allow "validate_pillow_installed" to run *before* importing it
|
||||||
from PIL import Image
|
from PIL import Image, UnidentifiedImageError
|
||||||
|
|
||||||
conf_file = config[CONF_FILE]
|
conf_file = config[CONF_FILE]
|
||||||
|
|
||||||
|
@ -272,6 +270,9 @@ async def to_code(config):
|
||||||
elif conf_file[CONF_SOURCE] == SOURCE_WEB:
|
elif conf_file[CONF_SOURCE] == SOURCE_WEB:
|
||||||
path = compute_local_image_path(conf_file).as_posix()
|
path = compute_local_image_path(conf_file).as_posix()
|
||||||
|
|
||||||
|
else:
|
||||||
|
raise core.EsphomeError("Unknown image source type")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with open(path, "rb") as f:
|
with open(path, "rb") as f:
|
||||||
file_contents = f.read()
|
file_contents = f.read()
|
||||||
|
@ -284,7 +285,10 @@ async def to_code(config):
|
||||||
if resize:
|
if resize:
|
||||||
image.thumbnail(resize)
|
image.thumbnail(resize)
|
||||||
except UnidentifiedImageError as exc:
|
except UnidentifiedImageError as exc:
|
||||||
if "<svg" in str(file_contents):
|
file_str = str(file_contents[:4096])
|
||||||
|
if file_str.startswith("<svg") or (
|
||||||
|
file_str.startswith("<?xml version=") and "<svg" in file_str
|
||||||
|
):
|
||||||
image = load_svg_image(file_contents, resize)
|
image = load_svg_image(file_contents, resize)
|
||||||
else:
|
else:
|
||||||
raise core.EsphomeError(f"Could not load image file {path}") from exc
|
raise core.EsphomeError(f"Could not load image file {path}") from exc
|
||||||
|
|
Loading…
Reference in a new issue