[image] Support the other Pictogrammers icon sets memory: and mdil: (#8676)

This commit is contained in:
Clyde Stubbs 2025-05-05 13:31:16 +10:00 committed by GitHub
parent 0b032e5c19
commit ad99d7fb45
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 50 additions and 17 deletions

View File

@ -286,9 +286,18 @@ CONF_TRANSPARENCY = "transparency"
IMAGE_DOWNLOAD_TIMEOUT = 30 # seconds IMAGE_DOWNLOAD_TIMEOUT = 30 # seconds
SOURCE_LOCAL = "local" SOURCE_LOCAL = "local"
SOURCE_MDI = "mdi"
SOURCE_WEB = "web" SOURCE_WEB = "web"
SOURCE_MDI = "mdi"
SOURCE_MDIL = "mdil"
SOURCE_MEMORY = "memory"
MDI_SOURCES = {
SOURCE_MDI: "https://raw.githubusercontent.com/Templarian/MaterialDesign/master/svg/",
SOURCE_MDIL: "https://raw.githubusercontent.com/Pictogrammers/MaterialDesignLight/refs/heads/master/svg/",
SOURCE_MEMORY: "https://raw.githubusercontent.com/Pictogrammers/Memory/refs/heads/main/src/svg/",
}
Image_ = image_ns.class_("Image") Image_ = image_ns.class_("Image")
INSTANCE_TYPE = Image_ INSTANCE_TYPE = Image_
@ -313,12 +322,12 @@ def download_file(url, path):
return str(path) return str(path)
def download_mdi(value): def download_gh_svg(value, source):
mdi_id = value[CONF_ICON] if isinstance(value, dict) else value mdi_id = value[CONF_ICON] if isinstance(value, dict) else value
base_dir = external_files.compute_local_file_dir(DOMAIN) / "mdi" base_dir = external_files.compute_local_file_dir(DOMAIN) / source
path = base_dir / f"{mdi_id}.svg" path = base_dir / f"{mdi_id}.svg"
url = f"https://raw.githubusercontent.com/Templarian/MaterialDesign/master/svg/{mdi_id}.svg" url = MDI_SOURCES[source] + mdi_id + ".svg"
return download_file(url, path) return download_file(url, path)
@ -353,12 +362,12 @@ def validate_cairosvg_installed():
def validate_file_shorthand(value): def validate_file_shorthand(value):
value = cv.string_strict(value) value = cv.string_strict(value)
if value.startswith("mdi:"): parts = value.strip().split(":")
match = re.search(r"mdi:([a-zA-Z0-9\-]+)", value) if len(parts) == 2 and parts[0] in MDI_SOURCES:
match = re.match(r"[a-zA-Z0-9\-]+", parts[1])
if match is None: if match is None:
raise cv.Invalid("Could not parse mdi icon name.") raise cv.Invalid(f"Could not parse mdi icon name from '{value}'.")
icon = match.group(1) return download_gh_svg(parts[1], parts[0])
return download_mdi(icon)
if value.startswith("http://") or value.startswith("https://"): if value.startswith("http://") or value.startswith("https://"):
return download_image(value) return download_image(value)
@ -374,12 +383,20 @@ LOCAL_SCHEMA = cv.All(
local_path, local_path,
) )
MDI_SCHEMA = cv.All(
{ def mdi_schema(source):
cv.Required(CONF_ICON): cv.string, def validate_mdi(value):
}, return download_gh_svg(value, source)
download_mdi,
) return cv.All(
cv.Schema(
{
cv.Required(CONF_ICON): cv.string,
}
),
validate_mdi,
)
WEB_SCHEMA = cv.All( WEB_SCHEMA = cv.All(
{ {
@ -388,12 +405,13 @@ WEB_SCHEMA = cv.All(
download_image, download_image,
) )
TYPED_FILE_SCHEMA = cv.typed_schema( TYPED_FILE_SCHEMA = cv.typed_schema(
{ {
SOURCE_LOCAL: LOCAL_SCHEMA, SOURCE_LOCAL: LOCAL_SCHEMA,
SOURCE_MDI: MDI_SCHEMA,
SOURCE_WEB: WEB_SCHEMA, SOURCE_WEB: WEB_SCHEMA,
}, }
| {source: mdi_schema(source) for source in MDI_SOURCES},
key=CONF_SOURCE, key=CONF_SOURCE,
) )

View File

@ -69,3 +69,18 @@ image:
- id: another_alert_icon - id: another_alert_icon
file: mdi:alert-outline file: mdi:alert-outline
type: BINARY type: BINARY
- file: mdil:arrange-bring-to-front
id: mdil_id
resize: 50x50
type: binary
transparency: chroma_key
- file: mdi:beer
id: mdi_id
resize: 50x50
type: binary
transparency: chroma_key
- file: memory:alert-octagon
id: memory_id
resize: 50x50
type: binary
transparency: chroma_key