Fix Ezviz last alarm picture (#112074)
* Fix Ezviz last alarm picture * Review fixes
This commit is contained in:
parent
d0a036c617
commit
906c906653
1 changed files with 21 additions and 1 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Reference in a new issue