Skip to content

Commit

Permalink
Add: Migrate automatically unique ids
Browse files Browse the repository at this point in the history
  • Loading branch information
JurajNyiri committed Oct 5, 2023
1 parent 3df58a9 commit ad80dc0
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
42 changes: 41 additions & 1 deletion custom_components/tapo_control/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import hashlib
import logging

from homeassistant.core import HomeAssistant
from homeassistant.core import HomeAssistant, callback
from homeassistant.components.ffmpeg import CONF_EXTRA_ARGUMENTS
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
Expand All @@ -15,6 +15,7 @@
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
from homeassistant.util import dt
from homeassistant.components.media_source.error import Unresolvable
import homeassistant.helpers.entity_registry

from .const import (
CONF_RTSP_TRANSPORT,
Expand Down Expand Up @@ -184,6 +185,45 @@ async def async_migrate_entry(hass, config_entry: ConfigEntry):

config_entry.version = 14

if config_entry.version == 14:
LOGGER.warn("TEST")

host = config_entry.data.get(CONF_IP_ADDRESS)
username = config_entry.data.get(CONF_USERNAME)
password = config_entry.data.get(CONF_PASSWORD)
cloud_password = config_entry.data.get(CLOUD_PASSWORD)

try:
if cloud_password != "":
tapoController = await hass.async_add_executor_job(
registerController, host, "admin", cloud_password, cloud_password
)
else:
tapoController = await hass.async_add_executor_job(
registerController, host, username, password
)
camData = await getCamData(hass, tapoController)
macAddress = camData["basic_info"]["mac"]

@callback
def update_unique_id(entity_entry):
return {
"new_unique_id": "{}-{}".format(
macAddress, entity_entry.unique_id
).lower()
}

await homeassistant.helpers.entity_registry.async_migrate_entries(
hass, config_entry.entry_id, update_unique_id
)
except Exception as e:
LOGGER.error(
"Unable to connect to Tapo: Cameras Control controller: %s", str(e)
)
raise ConfigEntryNotReady

# config_entry.version = 15

LOGGER.info("Migration to version %s successful", config_entry.version)

return True
Expand Down
2 changes: 1 addition & 1 deletion custom_components/tapo_control/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
class FlowHandler(ConfigFlow):
"""Handle a config flow."""

VERSION = 14
VERSION = 15

@staticmethod
def async_get_options_flow(config_entry):
Expand Down
3 changes: 0 additions & 3 deletions custom_components/tapo_control/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@ def device_info(self) -> DeviceInfo:
@property
def unique_id(self) -> str:
id_suffix = "".join(self._name_suffix.split())
LOGGER.warn(
"{}-{}-{}".format(self._attributes["mac"], self._name, id_suffix).lower()
)
return "{}-{}-{}".format(self._attributes["mac"], self._name, id_suffix).lower()

def updateTapo(self, camData):
Expand Down

0 comments on commit ad80dc0

Please sign in to comment.