Fix Ezviz last alarm picture (#112074)

* Fix Ezviz last alarm picture

* Review fixes
This commit is contained in:
Dmitriy 2024-06-05 17:55:59 +04:00 committed by GitHub
parent d0a036c617
commit 906c906653
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -4,8 +4,12 @@ from __future__ import annotations
import logging
from pyezviz.exceptions import PyEzvizError
from pyezviz.utils import decrypt_image
from homeassistant.components.image import Image, ImageEntity, ImageEntityDescription
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_PASSWORD
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.util import dt as dt_util
@ -51,12 +55,28 @@ class EzvizLastMotion(EzvizEntity, ImageEntity):
self._attr_image_last_updated = dt_util.parse_datetime(
str(self.data["last_alarm_time"])
)
camera = hass.config_entries.async_entry_for_domain_unique_id(DOMAIN, serial)
self.alarm_image_password = (
camera.data[CONF_PASSWORD] if camera is not None else None
)
async def _async_load_image_from_url(self, url: str) -> Image | None:
"""Load an image by url."""
if response := await self._fetch_url(url):
image_data = response.content
if self.data["encrypted"] and self.alarm_image_password is not None:
try:
image_data = decrypt_image(
response.content, self.alarm_image_password
)
except PyEzvizError:
_LOGGER.warning(
"%s: Can't decrypt last alarm picture, looks like it was encrypted with other password",
self.entity_id,
)
image_data = response.content
return Image(
content=response.content,
content=image_data,
content_type="image/jpeg", # Actually returns binary/octet-stream
)
return None