Skip to content

Commit

Permalink
Text entity can now update camera enc key when changed.
Browse files Browse the repository at this point in the history
  • Loading branch information
RenierM26 committed Jan 5, 2025
1 parent 74f44d5 commit 10a925c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 14 deletions.
9 changes: 2 additions & 7 deletions custom_components/ezviz_cloud/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
CONF_TIMEOUT,
CONF_TYPE,
CONF_URL,
CONF_USERNAME,
Platform,
)
from homeassistant.core import HomeAssistant
Expand Down Expand Up @@ -146,12 +145,8 @@ async def async_migrate_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:

if entry.version == 1:
if entry.data[CONF_TYPE] == ATTR_TYPE_CAMERA:
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,
}
data = {**entry.data}
data[CONF_ENC_KEY] = entry.data[CONF_PASSWORD]

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

Expand Down
39 changes: 32 additions & 7 deletions custom_components/ezviz_cloud/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
)

from homeassistant.components.text import TextEntity, TextEntityDescription, TextMode
from homeassistant.config_entries import ConfigEntry
from homeassistant.config_entries import SOURCE_IGNORE, ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.restore_state import RestoreEntity

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

Expand All @@ -44,22 +44,33 @@ async def async_setup_entry(
DATA_COORDINATOR
]

async_add_entities(EzvizText(coordinator, camera) for camera in coordinator.data)
async_add_entities(
EzvizText(hass, coordinator, camera) for camera in coordinator.data
)


class EzvizText(EzvizBaseEntity, TextEntity, RestoreEntity):
"""Representation of a EZVIZ text entity."""

def __init__(
self,
hass: HomeAssistant,
coordinator: EzvizDataUpdateCoordinator,
serial: str,
) -> None:
"""Initialize the sensor."""
super().__init__(coordinator, serial)
self._attr_unique_id = f"{serial}_{TEXT_TYPE.key}"
self.entity_description = TEXT_TYPE
self._attr_native_value = "Unknown"
self._attr_native_value = None
self.camera = hass.config_entries.async_entry_for_domain_unique_id(
DOMAIN, serial
)
self.alarm_enc_key = (
self.camera.data[CONF_ENC_KEY]
if self.camera and self.camera.source != SOURCE_IGNORE
else None
)

async def async_added_to_hass(self) -> None:
"""Run when entity about to be added."""
Expand All @@ -83,12 +94,12 @@ def set_value(self, value: str) -> None:
f"Cannot set camera encryption key for {self.name}"
) from err

def update(self) -> None:
async def async_update(self) -> None:
"""Fetch data from EZVIZ."""
_LOGGER.debug("Updating %s", self.name)
try:
self._attr_native_value = self.coordinator.ezviz_client.get_cam_key(
self._serial,
self._attr_native_value = await self.hass.async_add_executor_job(
self.coordinator.ezviz_client.get_cam_key, self._serial
)

except (
Expand All @@ -97,3 +108,17 @@ def update(self) -> None:
PyEzvizError,
) as error:
raise HomeAssistantError(f"Invalid response from API: {error}") from error

# Encryption key changed, update config entry
if self.alarm_enc_key and self.camera:
if self.alarm_enc_key != self._attr_native_value:
await self.update_camera_config_entry(self.camera)

async def update_camera_config_entry(self, entry: ConfigEntry) -> None:
"""Update camera config entry."""
data = {**entry.data}
data[CONF_ENC_KEY] = self._attr_native_value
self.hass.config_entries.async_update_entry(
entry,
data=data,
)

0 comments on commit 10a925c

Please sign in to comment.