Skip to content

Commit

Permalink
Fix: Removal of disabled device results in error and warning, time sy…
Browse files Browse the repository at this point in the history
…nc debug log
  • Loading branch information
JurajNyiri committed Jul 20, 2024
1 parent 23af274 commit 65ec7b9
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 23 deletions.
22 changes: 18 additions & 4 deletions custom_components/tapo_control/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,12 +295,26 @@ async def async_remove_entry(hass: HomeAssistant, entry: ConfigEntry) -> None:
hotDirPath = getHotDirPathForEntry(hass, entry_id)

# Delete all media stored in cold storage for entity
LOGGER.debug("Deleting cold storage files for entity " + entry_id + "...")
deleteDir(coldDirPath)
if coldDirPath:
LOGGER.debug("Deleting cold storage files for entity " + entry_id + "...")
await deleteDir(hass, coldDirPath)
else:
LOGGER.warn(
"No cold storage path found for entity"
+ entry_id
+ ". Not deleting anything."
)

# Delete all media stored in hot storage for entity
LOGGER.debug("Deleting hot storage files for entity " + entry_id + "...")
deleteDir(hotDirPath)
if hotDirPath:
LOGGER.debug("Deleting hot storage files for entity " + entry_id + "...")
await deleteDir(hass, hotDirPath)
else:
LOGGER.warn(
"No hot storage path found for entity"
+ entry_id
+ ". Not deleting anything."
)

# Remove the entry data
hass.data[DOMAIN].pop(entry_id, None)
Expand Down
52 changes: 33 additions & 19 deletions custom_components/tapo_control/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,33 +113,46 @@ def getDataPath():


def getColdDirPathForEntry(hass: HomeAssistant, entry_id: str):
if hass.data[DOMAIN][entry_id]["mediaSyncColdDir"] is False:
# Fast retrieval of path without file IO
if (
entry_id in hass.data[DOMAIN]
and hass.data[DOMAIN][entry_id]["mediaSyncColdDir"] is not False
):
return hass.data[DOMAIN][entry_id]["mediaSyncColdDir"].rstrip("/")

coldDirPath = os.path.join(getDataPath(), f".storage/{DOMAIN}/{entry_id}/")
if entry_id in hass.data[DOMAIN]:
entry: ConfigEntry = hass.data[DOMAIN][entry_id]["entry"]
media_sync_cold_storage_path = entry.data.get(MEDIA_SYNC_COLD_STORAGE_PATH)
if media_sync_cold_storage_path == "":
coldDirPath = os.path.join(getDataPath(), f".storage/{DOMAIN}/{entry_id}/")
else:
coldDirPath = f"{media_sync_cold_storage_path}/"
else: # if device is disabled, get entry from HA storage
entry: ConfigEntry = hass.config_entries.async_get_entry(entry_id)

media_sync_cold_storage_path = entry.data.get(MEDIA_SYNC_COLD_STORAGE_PATH)

if not media_sync_cold_storage_path == "":
coldDirPath = f"{media_sync_cold_storage_path}/"

if entry_id in hass.data[DOMAIN]:
pathlib.Path(coldDirPath + "/videos").mkdir(parents=True, exist_ok=True)
pathlib.Path(coldDirPath + "/thumbs").mkdir(parents=True, exist_ok=True)
hass.data[DOMAIN][entry_id]["mediaSyncColdDir"] = coldDirPath

coldDirPath = hass.data[DOMAIN][entry_id]["mediaSyncColdDir"]
coldDirPath = coldDirPath.rstrip("/")
return coldDirPath
return coldDirPath.rstrip("/")


def getHotDirPathForEntry(hass: HomeAssistant, entry_id: str):
if hass.data[DOMAIN][entry_id]["mediaSyncHotDir"] is False:
hotDirPath = os.path.join(getDataPath(), f"www/{DOMAIN}/{entry_id}/")
pathlib.Path(hotDirPath + "/videos").mkdir(parents=True, exist_ok=True)
pathlib.Path(hotDirPath + "/thumbs").mkdir(parents=True, exist_ok=True)
hass.data[DOMAIN][entry_id]["mediaSyncHotDir"] = hotDirPath
if hass.data[DOMAIN][entry_id]["mediaSyncHotDir"] is not False:
return hass.data[DOMAIN][entry_id]["mediaSyncHotDir"].rstrip("/")

hotDirPath = os.path.join(getDataPath(), f"www/{DOMAIN}/{entry_id}/")

if entry_id in hass.data[DOMAIN]:
if hass.data[DOMAIN][entry_id]["mediaSyncHotDir"] is False:
pathlib.Path(hotDirPath + "/videos").mkdir(parents=True, exist_ok=True)
pathlib.Path(hotDirPath + "/thumbs").mkdir(parents=True, exist_ok=True)
hass.data[DOMAIN][entry_id]["mediaSyncHotDir"] = hotDirPath

hotDirPath = hass.data[DOMAIN][entry_id]["mediaSyncHotDir"]
hotDirPath = hotDirPath.rstrip("/")
return hotDirPath
hotDirPath = hass.data[DOMAIN][entry_id]["mediaSyncHotDir"]
return hotDirPath.rstrip("/")


async def getRecordings(hass, entry_id, date):
Expand Down Expand Up @@ -379,15 +392,15 @@ async def mediaCleanup(hass, entry):
await deleteFilesOlderThan(hass, hotDirPath + "/thumbs/", HOT_DIR_DELETE_TIME)


def deleteDir(dirPath):
async def deleteDir(hass, dirPath):
if (
os.path.exists(dirPath)
and os.path.isdir(dirPath)
and dirPath != "/"
and "tapo_control/" in dirPath
):
LOGGER.debug("Deleting folder " + dirPath + "...")
shutil.rmtree(dirPath)
await hass.async_add_executor_job(shutil.rmtree, dirPath)


async def deleteFilesOlderThan(hass: HomeAssistant, dirPath, deleteOlderThan):
Expand Down Expand Up @@ -1216,6 +1229,7 @@ async def getLatestFirmwareVersion(hass, config_entry, entry, controller):
async def syncTime(hass, entry_id):
device_mgmt = hass.data[DOMAIN][entry_id]["onvifManagement"]
if device_mgmt:
LOGGER.debug("Syncing time for " + entry_id + "...")
now = datetime.datetime.utcnow()

time_params = device_mgmt.create_type("SetSystemDateAndTime")
Expand Down

0 comments on commit 65ec7b9

Please sign in to comment.