Improve image checks for generic camera (#69037)
parent
dcbca89d06
commit
2cb77c4702
|
@ -4,12 +4,13 @@ from __future__ import annotations
|
||||||
import contextlib
|
import contextlib
|
||||||
from errno import EHOSTUNREACH, EIO
|
from errno import EHOSTUNREACH, EIO
|
||||||
from functools import partial
|
from functools import partial
|
||||||
import imghdr
|
import io
|
||||||
import logging
|
import logging
|
||||||
from types import MappingProxyType
|
from types import MappingProxyType
|
||||||
from typing import Any
|
from typing import Any
|
||||||
from urllib.parse import urlparse, urlunparse
|
from urllib.parse import urlparse, urlunparse
|
||||||
|
|
||||||
|
import PIL
|
||||||
from async_timeout import timeout
|
from async_timeout import timeout
|
||||||
import av
|
import av
|
||||||
from httpx import HTTPStatusError, RequestError, TimeoutException
|
from httpx import HTTPStatusError, RequestError, TimeoutException
|
||||||
|
@ -110,9 +111,14 @@ def build_schema(
|
||||||
|
|
||||||
def get_image_type(image):
|
def get_image_type(image):
|
||||||
"""Get the format of downloaded bytes that could be an image."""
|
"""Get the format of downloaded bytes that could be an image."""
|
||||||
fmt = imghdr.what(None, h=image)
|
fmt = None
|
||||||
|
imagefile = io.BytesIO(image)
|
||||||
|
with contextlib.suppress(PIL.UnidentifiedImageError):
|
||||||
|
img = PIL.Image.open(imagefile)
|
||||||
|
fmt = img.format.lower()
|
||||||
|
|
||||||
if fmt is None:
|
if fmt is None:
|
||||||
# if imghdr can't figure it out, could be svg.
|
# if PIL can't figure it out, could be svg.
|
||||||
with contextlib.suppress(UnicodeDecodeError):
|
with contextlib.suppress(UnicodeDecodeError):
|
||||||
if image.decode("utf-8").lstrip().startswith("<svg"):
|
if image.decode("utf-8").lstrip().startswith("<svg"):
|
||||||
return "svg+xml"
|
return "svg+xml"
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
"domain": "generic",
|
"domain": "generic",
|
||||||
"name": "Generic Camera",
|
"name": "Generic Camera",
|
||||||
"config_flow": true,
|
"config_flow": true,
|
||||||
"requirements": ["av==9.0.0"],
|
"requirements": ["av==9.0.0", "pillow==9.0.1"],
|
||||||
"documentation": "https://www.home-assistant.io/integrations/generic",
|
"documentation": "https://www.home-assistant.io/integrations/generic",
|
||||||
"codeowners": ["@davet2001"],
|
"codeowners": ["@davet2001"],
|
||||||
"iot_class": "local_push"
|
"iot_class": "local_push"
|
||||||
|
|
|
@ -1201,6 +1201,7 @@ pi1wire==0.1.0
|
||||||
pilight==0.1.1
|
pilight==0.1.1
|
||||||
|
|
||||||
# homeassistant.components.doods
|
# homeassistant.components.doods
|
||||||
|
# homeassistant.components.generic
|
||||||
# homeassistant.components.image
|
# homeassistant.components.image
|
||||||
# homeassistant.components.proxy
|
# homeassistant.components.proxy
|
||||||
# homeassistant.components.qrcode
|
# homeassistant.components.qrcode
|
||||||
|
|
|
@ -798,6 +798,7 @@ pi1wire==0.1.0
|
||||||
pilight==0.1.1
|
pilight==0.1.1
|
||||||
|
|
||||||
# homeassistant.components.doods
|
# homeassistant.components.doods
|
||||||
|
# homeassistant.components.generic
|
||||||
# homeassistant.components.image
|
# homeassistant.components.image
|
||||||
# homeassistant.components.proxy
|
# homeassistant.components.proxy
|
||||||
# homeassistant.components.qrcode
|
# homeassistant.components.qrcode
|
||||||
|
|
Loading…
Reference in New Issue