Skip to content

Commit

Permalink
Improve encrypted image support
Browse files Browse the repository at this point in the history
  • Loading branch information
RenierM26 committed Jan 2, 2025
1 parent 853a28a commit 902b491
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 12 deletions.
20 changes: 19 additions & 1 deletion custom_components/ezviz_cloud/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,21 @@
)

from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_TIMEOUT, CONF_TYPE, CONF_URL, Platform
from homeassistant.const import (
CONF_PASSWORD,
CONF_TIMEOUT,
CONF_TYPE,
CONF_URL,
CONF_USERNAME,
Platform,
)
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady

from .const import (
ATTR_TYPE_CAMERA,
ATTR_TYPE_CLOUD,
CONF_ENC_KEY,
CONF_FFMPEG_ARGUMENTS,
CONF_RF_SESSION_ID,
CONF_SESSION_ID,
Expand Down Expand Up @@ -105,6 +113,16 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
# Cameras are accessed via local RTSP stream with unique credentials per camera.
# Separate camera entities allow for credential changes per camera.
if sensor_type == ATTR_TYPE_CAMERA and hass.data[DOMAIN]:
if not entry.data.get(CONF_ENC_KEY):
data = {
CONF_USERNAME: entry.data[CONF_USERNAME],
CONF_PASSWORD: entry.data[CONF_PASSWORD],
CONF_ENC_KEY: entry.data[CONF_PASSWORD],
CONF_TYPE: ATTR_TYPE_CAMERA,
}

hass.config_entries.async_update_entry(entry, data=data)

for item in hass.config_entries.async_entries(domain=DOMAIN):
if item.data[CONF_TYPE] == ATTR_TYPE_CLOUD:
_LOGGER.debug("Reload Ezviz main account with camera entry")
Expand Down
16 changes: 9 additions & 7 deletions custom_components/ezviz_cloud/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
ATTR_SERIAL,
ATTR_TYPE_CAMERA,
ATTR_TYPE_CLOUD,
CONF_ENC_KEY,
CONF_FFMPEG_ARGUMENTS,
CONF_RF_SESSION_ID,
CONF_SESSION_ID,
Expand Down Expand Up @@ -145,20 +146,21 @@ async def _validate_and_create_camera_rtsp(self, data: dict) -> ConfigFlowResult
# Create Ezviz API Client.
await self.hass.async_add_executor_job(ezviz_client.login)

# If no encryption key is provided, get it. Sometimes a old key is provided and doesn't work.
if data[CONF_PASSWORD] == "fetch_my_key":
data[CONF_PASSWORD] = await self.hass.async_add_executor_job(
_get_cam_enc_key, data, ezviz_client
)
# Fetch encryption key from ezviz api.
data[CONF_ENC_KEY] = await self.hass.async_add_executor_job(
_get_cam_enc_key, data, ezviz_client
)

# Test camera RTSP credentials.
await self.hass.async_add_executor_job(_wake_camera, data, ezviz_client)
# Test camera RTSP credentials. Older cameras still use the verification code on the camra and not the encryption key.
if data[CONF_PASSWORD] == "fetch_my_key":
await self.hass.async_add_executor_job(_wake_camera, data, ezviz_client)

return self.async_create_entry(
title=data[ATTR_SERIAL],
data={
CONF_USERNAME: data[CONF_USERNAME],
CONF_PASSWORD: data[CONF_PASSWORD],
CONF_ENC_KEY: data[CONF_ENC_KEY],
CONF_TYPE: ATTR_TYPE_CAMERA,
},
options=DEFAULT_OPTIONS,
Expand Down
1 change: 1 addition & 0 deletions custom_components/ezviz_cloud/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
CONF_SESSION_ID = "session_id"
CONF_RF_SESSION_ID = "rf_session_id"
CONF_EZVIZ_ACCOUNT = "ezviz_account"
CONF_ENC_KEY = "enc_key"

# Service names
SERVICE_WAKE_DEVICE = "wake_device"
Expand Down
9 changes: 5 additions & 4 deletions custom_components/ezviz_cloud/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@
from pyezvizapi.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.config_entries import SOURCE_IGNORE, ConfigEntry
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.util import dt as dt_util

from .const import DATA_COORDINATOR, DOMAIN
from .const import CONF_ENC_KEY, DATA_COORDINATOR, DOMAIN
from .coordinator import EzvizDataUpdateCoordinator
from .entity import EzvizEntity

Expand Down Expand Up @@ -57,7 +56,9 @@ def __init__(
)
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
camera.data[CONF_ENC_KEY]
if camera and camera.source != SOURCE_IGNORE
else None
)

async def _async_load_image_from_url(self, url: str) -> Image | None:
Expand Down

0 comments on commit 902b491

Please sign in to comment.