From 7a0bad06eb4e509d8c818c29585d9caa68c7f536 Mon Sep 17 00:00:00 2001 From: rhammen <75572839+rhammen@users.noreply.github.com> Date: Sat, 4 Jul 2026 02:57:50 +0200 Subject: [PATCH] =?UTF-8?q?fix(base):=20=F0=9F=90=9B=20make=20socket-close?= =?UTF-8?q?d=20detection=20portable=20to=20Windows?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - replace socket.MSG_DONTWAIT (POSIX-only, missing on Windows) with settimeout(0), which puts the socket into non-blocking mode portably and raises the same BlockingIOError the existing handler expects - MSG_PEEK alone still reads without consuming the data Fixes 3 tests in TestIsSocketClosed that previously failed on Windows because evaluating socket.MSG_DONTWAIT raised AttributeError before recv() was ever reached, silently falling through to the generic exception handler instead of exercising the intended branch. Co-Authored-By: Claude Sonnet 5 --- custom_components/luxtronik2/lux_helper.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/custom_components/luxtronik2/lux_helper.py b/custom_components/luxtronik2/lux_helper.py index e8e4f300..d44e9497 100644 --- a/custom_components/luxtronik2/lux_helper.py +++ b/custom_components/luxtronik2/lux_helper.py @@ -174,9 +174,11 @@ def _is_socket_closed(sock: socket.socket) -> bool: last_timeout: float | None = None try: last_timeout = sock.gettimeout() - # this will try to read bytes without blocking and also without removing them from buffer (peek only) - sock.settimeout(None) - data = sock.recv(16, socket.MSG_DONTWAIT | socket.MSG_PEEK) + # settimeout(0) puts the socket in non-blocking mode (raises BlockingIOError + # instead of waiting), portably across platforms - socket.MSG_DONTWAIT is + # POSIX-only and unavailable on Windows. MSG_PEEK reads without consuming. + sock.settimeout(0) + data = sock.recv(16, socket.MSG_PEEK) if len(data) == 0: return True except BlockingIOError: