Skip to content

Commit

Permalink
Bump config version.
Browse files Browse the repository at this point in the history
  • Loading branch information
RenierM26 committed Jan 5, 2025
1 parent d8b3e3c commit e845bd9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
44 changes: 30 additions & 14 deletions custom_components/ezviz_cloud/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,6 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:

# Initialize EZVIZ cloud entities
if PLATFORMS_BY_TYPE[sensor_type]:
# Initiate reauth config flow if account token if not present.
if not entry.data.get(CONF_SESSION_ID):
raise ConfigEntryAuthFailed

ezviz_client = EzvizClient(
token={
CONF_SESSION_ID: entry.data[CONF_SESSION_ID],
Expand Down Expand Up @@ -113,16 +109,6 @@ 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 Expand Up @@ -152,3 +138,33 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def _async_update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None:
"""Handle options update."""
await hass.config_entries.async_reload(entry.entry_id)


async def async_migrate_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Migrate old camera entry."""
_LOGGER.debug("Migrating from version %s.%s", entry.version, entry.minor_version)

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,
}

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

if entry.data[CONF_TYPE] == ATTR_TYPE_CLOUD:
if not entry.data.get(CONF_SESSION_ID):
raise ConfigEntryAuthFailed
hass.config_entries.async_update_entry(entry, data=entry.data, version=2)

_LOGGER.info(
"Migration to version %s.%s successful for %s account",
entry.version,
entry.minor_version,
entry.data[CONF_TYPE],
)

return True
2 changes: 1 addition & 1 deletion custom_components/ezviz_cloud/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def _get_cam_enc_key(data: dict, ezviz_client: EzvizClient) -> Any:
class EzvizConfigFlow(ConfigFlow, domain=DOMAIN):
"""Handle a config flow for EZVIZ."""

VERSION = 1
VERSION = 2

ip_address: str
username: str | None
Expand Down

0 comments on commit e845bd9

Please sign in to comment.