Skip to content

Commit

Permalink
Fix text sensor.
Browse files Browse the repository at this point in the history
  • Loading branch information
RenierM26 committed Jan 18, 2025
1 parent fc561d5 commit de58073
Showing 1 changed file with 33 additions and 15 deletions.
48 changes: 33 additions & 15 deletions custom_components/ezviz_cloud/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
HTTPError,
PyEzvizError,
)
from pyezvizapi.utils import return_password_hash

from homeassistant.components.text import TextEntity, TextEntityDescription, TextMode
from homeassistant.config_entries import SOURCE_IGNORE, ConfigEntry
Expand Down Expand Up @@ -69,13 +70,14 @@ def __init__(
self.alarm_enc_key = (
self.camera.data[CONF_ENC_KEY]
if self.camera and self.camera.source != SOURCE_IGNORE
else None
else "Unknown"
)

async def async_added_to_hass(self) -> None:
"""Run when entity about to be added."""
await super().async_added_to_hass()
if not (last_state := await self.async_get_last_state()):
self._attr_native_value = self.alarm_enc_key
return self.schedule_update_ha_state(force_refresh=True)
self._attr_native_value = last_state.state
return None
Expand All @@ -98,22 +100,38 @@ def set_value(self, value: str) -> None:
async def async_update(self) -> None:
"""Fetch data from EZVIZ."""
_LOGGER.debug("Updating %s", self.name)
try:
self._attr_native_value = await self.hass.async_add_executor_job(
self.coordinator.ezviz_client.get_cam_key, self._serial

if (
return_password_hash(self._attr_native_value)
!= self.data["encrypted_pwd_hash"]
):
_LOGGER.warning(
"%s: Password hash is different from password, password = %s, hash_of_pass = %s, hash_from_api = %s, fetching from api",
self.entity_id,
self._attr_native_value,
return_password_hash(self._attr_native_value),
self.data["encrypted_pwd_hash"],
)

except (
EzvizAuthTokenExpired,
EzvizAuthVerificationCode,
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)
try:
self._attr_native_value = await self.hass.async_add_executor_job(
self.coordinator.ezviz_client.get_cam_key, self._serial
)

except (
EzvizAuthTokenExpired,
EzvizAuthVerificationCode,
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:
self.async_write_ha_state()
await self.update_camera_config_entry(self.camera)

async def update_camera_config_entry(self, entry: ConfigEntry) -> None:
"""Update camera config entry."""
Expand Down

0 comments on commit de58073

Please sign in to comment.