Skip to content

Commit

Permalink
Merge pull request #183 from dala318/reauth_fix
Browse files Browse the repository at this point in the history
Fix Re-Authorization
  • Loading branch information
ualex73 authored Jan 29, 2025
2 parents 0dfc912 + 49b173e commit b935229
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 18 deletions.
20 changes: 14 additions & 6 deletions custom_components/monitor_docker/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@
CONF_URL,
Platform,
)
from homeassistant.core import HomeAssistant, IntegrationError
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import (
ConfigEntryNotReady,
ConfigEntryError,
ConfigEntryAuthFailed,
)
from homeassistant.helpers.typing import ConfigType
from homeassistant.helpers.reload import async_setup_reload_service

Expand Down Expand Up @@ -161,12 +165,16 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
hass.data[DOMAIN][entry.data[CONF_NAME]][API] = api

await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
except ConfigEntryAuthFailed:
# if api:
# await api.destroy()
raise
except Exception as err:
_LOGGER.error("[%s]: Failed to setup, error=%s", entry.data[CONF_NAME],str(err))

_LOGGER.error(
"[%s]: Failed to setup, error=%s", entry.data[CONF_NAME], str(err)
)
if api:
await api.destroy()

raise ConfigEntryNotReady(f"Failed to setup {err}") from err

return True
Expand Down Expand Up @@ -199,7 +207,7 @@ async def async_migrate_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
entry.minor_version,
)

class MigrateError(IntegrationError):
class MigrateError(ConfigEntryError):
"""Error to indicate there is was an error in version migration."""

installed_version = DockerConfigFlow.VERSION
Expand Down
32 changes: 20 additions & 12 deletions custom_components/monitor_docker/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,19 +215,27 @@ async def init(self, startCount: int = 0):
),
)

# Initiate the aiodocker instance now. Could raise an exception
self._api = aiodocker.Docker(
url=url,
connector=self._tcp_connector,
session=self._tcp_session,
ssl_context=self._tcp_ssl_context,
)
try:
# Initiate the aiodocker instance now. Could raise an exception
self._api = aiodocker.Docker(
url=url,
connector=self._tcp_connector,
session=self._tcp_session,
ssl_context=self._tcp_ssl_context,
)

versionInfo = await self._api.version()
version: str | None = versionInfo.get("Version", None)
versionInfo = await self._api.version()
version: str | None = versionInfo.get("Version", None)

# Pre 19.03 support memory calculation is dropped
_LOGGER.debug("[%s]: Docker version: %s", self._instance, version)
# Pre 19.03 support memory calculation is dropped
_LOGGER.debug("[%s]: Docker version: %s", self._instance, version)
except aiodocker.exceptions.DockerError as err:
_LOGGER.error(
"[%s]: Docker API connection failed: %s", self._instance, str(err)
)
raise ConfigEntryAuthFailed from err
except Exception:
raise

# Get the list of containers to monitor
containers = await self._api.containers.list(all=True)
Expand Down Expand Up @@ -936,7 +944,7 @@ async def init(self):
str(err),
exc_info=exc_info,
)
raise ConfigEntryAuthFailed(err) from err
return # Could be necessary to do something more here

self._task = asyncio.create_task(self._run())

Expand Down

0 comments on commit b935229

Please sign in to comment.