mirror of
https://github.com/cyberjunky/home-assistant-garmin_connect.git
synced 2026-01-07 20:13:57 -05:00
Config migration improvements
This commit is contained in:
@@ -62,14 +62,15 @@ async def async_migrate_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
# Get the OAuth tokens
|
# Get the OAuth tokens
|
||||||
tokens = api.garth.dumps()
|
tokens = api.garth.dumps()
|
||||||
|
|
||||||
# Create new data with token, keeping the ID
|
# Create new data with token, ensuring we have a CONF_ID
|
||||||
new_data = {
|
new_data = {
|
||||||
CONF_ID: entry.data.get(CONF_ID, username),
|
CONF_ID: entry.data.get(CONF_ID, username),
|
||||||
CONF_TOKEN: tokens,
|
CONF_TOKEN: tokens,
|
||||||
}
|
}
|
||||||
|
|
||||||
# Update the config entry
|
# Update the config entry
|
||||||
hass.config_entries.async_update_entry(entry, data=new_data)
|
hass.config_entries.async_update_entry(
|
||||||
|
entry, data=new_data)
|
||||||
|
|
||||||
_LOGGER.info(
|
_LOGGER.info(
|
||||||
"Successfully migrated Garmin Connect config entry")
|
"Successfully migrated Garmin Connect config entry")
|
||||||
@@ -82,10 +83,21 @@ async def async_migrate_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
)
|
)
|
||||||
return False
|
return False
|
||||||
else:
|
else:
|
||||||
# No token and no username/password - config entry is incomplete
|
# No token and no username/password - config entry is incomplete/corrupted
|
||||||
_LOGGER.warning(
|
# Add a placeholder CONF_ID if missing to allow reauth to work properly
|
||||||
"Garmin Connect config entry is missing required data (no token and no credentials). "
|
if CONF_ID not in entry.data:
|
||||||
"This entry cannot be migrated automatically. A reauth flow will be triggered."
|
_LOGGER.info(
|
||||||
|
"Config entry missing CONF_ID, adding placeholder for reauth flow")
|
||||||
|
new_data = {
|
||||||
|
**entry.data,
|
||||||
|
CONF_ID: entry.entry_id, # Use entry_id as fallback
|
||||||
|
}
|
||||||
|
hass.config_entries.async_update_entry(
|
||||||
|
entry, data=new_data)
|
||||||
|
|
||||||
|
_LOGGER.info(
|
||||||
|
"Garmin Connect config entry is incomplete (missing token). "
|
||||||
|
"Reauthentication will be required to complete setup."
|
||||||
)
|
)
|
||||||
# Return True to allow setup to proceed, which will trigger reauth via ConfigEntryAuthFailed
|
# Return True to allow setup to proceed, which will trigger reauth via ConfigEntryAuthFailed
|
||||||
return True
|
return True
|
||||||
@@ -162,12 +174,13 @@ class GarminConnectDataUpdateCoordinator(DataUpdateCoordinator):
|
|||||||
try:
|
try:
|
||||||
# Check if the token exists in the entry data
|
# Check if the token exists in the entry data
|
||||||
if CONF_TOKEN not in self.entry.data:
|
if CONF_TOKEN not in self.entry.data:
|
||||||
_LOGGER.warning(
|
_LOGGER.info(
|
||||||
|
"Token not found in config entry. Reauthentication required."
|
||||||
|
)
|
||||||
|
raise ConfigEntryAuthFailed(
|
||||||
"Token not found in config entry. This may be an old or incomplete configuration. "
|
"Token not found in config entry. This may be an old or incomplete configuration. "
|
||||||
"A reauthentication flow will be initiated. Please check your notifications."
|
"A reauthentication flow will be initiated. Please check your notifications."
|
||||||
)
|
)
|
||||||
raise ConfigEntryAuthFailed(
|
|
||||||
"Token not found. Please reauthenticate via the notification prompt.")
|
|
||||||
|
|
||||||
await self.hass.async_add_executor_job(self.api.login, self.entry.data[CONF_TOKEN])
|
await self.hass.async_add_executor_job(self.api.login, self.entry.data[CONF_TOKEN])
|
||||||
except GarminConnectAuthenticationError as err:
|
except GarminConnectAuthenticationError as err:
|
||||||
|
|||||||
@@ -8,5 +8,5 @@
|
|||||||
"iot_class": "cloud_polling",
|
"iot_class": "cloud_polling",
|
||||||
"issue_tracker": "https://github.com/cyberjunky/home-assistant-garmin_connect/issues",
|
"issue_tracker": "https://github.com/cyberjunky/home-assistant-garmin_connect/issues",
|
||||||
"requirements": ["garminconnect>=0.2.31"],
|
"requirements": ["garminconnect>=0.2.31"],
|
||||||
"version": "0.2.35"
|
"version": "0.2.36"
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user