From 906c9066532b9e418b86d82676263b977c56fe90 Mon Sep 17 00:00:00 2001 From: Dmitriy Date: Wed, 5 Jun 2024 17:55:59 +0400 Subject: [PATCH] Fix Ezviz last alarm picture (#112074) * Fix Ezviz last alarm picture * Review fixes --- homeassistant/components/ezviz/image.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/ezviz/image.py b/homeassistant/components/ezviz/image.py index 0c362f8cbe7..0fbc5cc6a68 100644 --- a/homeassistant/components/ezviz/image.py +++ b/homeassistant/components/ezviz/image.py @@ -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