From 5d1d4a23e5072bb5d226c5d1b719983c302577e2 Mon Sep 17 00:00:00 2001 From: Getslow6 <43093176+Getslow6@users.noreply.github.com> Date: Thu, 17 Apr 2025 09:42:21 +0000 Subject: [PATCH 1/3] Simplify DockerAPI init --- custom_components/monitor_docker/helpers.py | 44 +++++++++++++-------- 1 file changed, 28 insertions(+), 16 deletions(-) diff --git a/custom_components/monitor_docker/helpers.py b/custom_components/monitor_docker/helpers.py index e68b4a6..7d672ff 100644 --- a/custom_components/monitor_docker/helpers.py +++ b/custom_components/monitor_docker/helpers.py @@ -137,13 +137,33 @@ async def init(self, startCount: int = 0): if url is not None and url == "": url = None - # Try to fix unix:// to unix:/// (3 are required by aiodocker) - if url is not None and url.find("unix://") == 0 and url.find("unix:///") == -1: - url = url.replace("unix://", "unix:///") + # A Unix connection should contain 'unix://' in the URL + unixConnection = url is not None and url.find("unix://") == 0 + + # If it is not a Unix connection, it should be a TCP connection + tcpConnection = url is not None and not unixConnection + + if unixConnection: + _LOGGER.debug("[%s]: Docker URL contains a Unix socket connection: '%s'", self._instance, url) + + # Try to fix unix:// to unix:/// (3 are required by aiodocker) + if url.find("unix:///") == -1: + url = url.replace("unix://", "unix:///") + + elif tcpConnection: + _LOGGER.debug("[%s]: Docker URL contains a TCP connection: '%s'", self._instance, url) + + # When we reconnect with tcp, we should delay - docker is maybe not fully ready + if startCount > 0: + await asyncio.sleep(5) + + else: + _LOGGER.debug( + "[%s]: Docker URL is auto-detect (most likely using 'unix://var/run/docker.socket')", + self._instance, + ) + - # When we reconnect with tcp, we should delay - docker is maybe not fully ready - if startCount > 0 and url is not None and url.find("unix:") != 0: - await asyncio.sleep(5) # Remove Docker environment variables os.environ.pop("DOCKER_TLS_VERIFY", None) @@ -154,16 +174,8 @@ async def init(self, startCount: int = 0): self._tcp_session = None self._tcp_ssl_context = None - if url is not None: - _LOGGER.debug("[%s]: Docker URL is '%s'", self._instance, url) - else: - _LOGGER.debug( - "[%s]: Docker URL is auto-detect (most likely using 'unix://var/run/docker.socket')", - self._instance, - ) - - # If is not empty or an Unix socket, then do check TCP/SSL - if url and url.find("unix:") == -1: + # If is a TCP connection, then do check TCP/SSL + if tcpConnection: # Check if URL is valid if not ( url.find("tcp:") == 0 From a081db9262adc2007278d64fa22b62776987e5cf Mon Sep 17 00:00:00 2001 From: Getslow6 <43093176+Getslow6@users.noreply.github.com> Date: Thu, 17 Apr 2025 10:17:28 +0000 Subject: [PATCH 2/3] Remove unused find_rename functions --- custom_components/monitor_docker/button.py | 7 ------- custom_components/monitor_docker/sensor.py | 5 ----- custom_components/monitor_docker/switch.py | 7 ------- 3 files changed, 19 deletions(-) diff --git a/custom_components/monitor_docker/button.py b/custom_components/monitor_docker/button.py index 0f1dc89..80df94e 100644 --- a/custom_components/monitor_docker/button.py +++ b/custom_components/monitor_docker/button.py @@ -96,13 +96,6 @@ async def async_restart(parm) -> None: "Service restart failed, container '%s' is not configured", cname ) - def find_rename(d: dict[str, str], item: str) -> str: - for k in d: - if re.match(k, item): - return d[k] - - return item - if discovery_info is None: return diff --git a/custom_components/monitor_docker/sensor.py b/custom_components/monitor_docker/sensor.py index 72a0ba4..931e949 100644 --- a/custom_components/monitor_docker/sensor.py +++ b/custom_components/monitor_docker/sensor.py @@ -72,12 +72,7 @@ async def async_setup_platform( ): """Set up the Monitor Docker Sensor.""" - def find_rename(d: dict[str, str], item: str) -> str: - for k in d: - if re.match(k, item): - return d[k] - return item if discovery_info is None: return diff --git a/custom_components/monitor_docker/switch.py b/custom_components/monitor_docker/switch.py index 106737c..3b2663f 100644 --- a/custom_components/monitor_docker/switch.py +++ b/custom_components/monitor_docker/switch.py @@ -95,13 +95,6 @@ async def async_restart(parm) -> None: "Service restart failed, container '%s' is not configured", cname ) - def find_rename(d: dict[str, str], item: str) -> str: - for k in d: - if re.match(k, item): - return d[k] - - return item - if discovery_info is None: return From 7268fde44e73eb41e2ae99d4e902076d064bcf43 Mon Sep 17 00:00:00 2001 From: Getslow6 <43093176+Getslow6@users.noreply.github.com> Date: Thu, 17 Apr 2025 10:50:33 +0000 Subject: [PATCH 3/3] Move VERSION constant to const.py --- custom_components/monitor_docker/const.py | 1 + custom_components/monitor_docker/helpers.py | 3 +-- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/custom_components/monitor_docker/const.py b/custom_components/monitor_docker/const.py index dcf692a..12174eb 100644 --- a/custom_components/monitor_docker/const.py +++ b/custom_components/monitor_docker/const.py @@ -11,6 +11,7 @@ API = "api" CONFIG = "config" CONTAINER = "container" +VERSION = "1.20b3" CONF_CERTPATH = "certpath" CONF_CONTAINERS = "containers" diff --git a/custom_components/monitor_docker/helpers.py b/custom_components/monitor_docker/helpers.py index 7d672ff..03a10af 100644 --- a/custom_components/monitor_docker/helpers.py +++ b/custom_components/monitor_docker/helpers.py @@ -70,10 +70,9 @@ DOCKER_STATS_MEMORY_PERCENTAGE, DOMAIN, PRECISION, + VERSION, ) -VERSION = "1.20b3" - _LOGGER = logging.getLogger(__name__)