Skip to content

Commit 5301baf

Browse files
agnersmdegat01
authored andcommitted
Simplify check_port by using asyncio.get_running_loop()
1 parent 52684ff commit 5301baf

File tree

5 files changed

+6
-11
lines changed

5 files changed

+6
-11
lines changed

supervisor/addons/addon.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ async def watchdog_application(self) -> bool:
539539

540540
# TCP monitoring
541541
if s_prefix == "tcp":
542-
return await check_port(self.coresys.loop, self.ip_address, port)
542+
return await check_port(self.ip_address, port)
543543

544544
# lookup the correct protocol from config
545545
if t_proto:

supervisor/homeassistant/api.py

-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,6 @@ async def get_api_state(self) -> str | None:
141141

142142
# Check if port is up
143143
if not await check_port(
144-
self.coresys.loop,
145144
self.sys_homeassistant.ip_address,
146145
self.sys_homeassistant.api_port,
147146
):

supervisor/ingress.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,7 @@ async def get_dynamic_port(self, addon_slug: str) -> int:
163163
while (
164164
port is None
165165
or port in self.ports.values()
166-
or await check_port(
167-
self.coresys.loop, self.sys_docker.network.gateway, port
168-
)
166+
or await check_port(self.sys_docker.network.gateway, port)
169167
):
170168
port = random.randint(62000, 65500)
171169

supervisor/utils/__init__.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,13 @@ async def wrap_api(api, *args, **kwargs):
3939
return wrap_api
4040

4141

42-
async def check_port(
43-
loop: asyncio.AbstractEventLoop, address: IPv4Address, port: int
44-
) -> bool:
42+
async def check_port(address: IPv4Address, port: int) -> bool:
4543
"""Check if port is mapped."""
4644
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
4745
sock.setblocking(False)
4846
try:
4947
async with asyncio.timeout(0.5):
50-
await loop.sock_connect(sock, (str(address), port))
48+
await asyncio.get_running_loop().sock_connect(sock, (str(address), port))
5149
except (OSError, TimeoutError):
5250
return False
5351
finally:

tests/utils/test_check_port.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77

88
async def test_exists_open_port(coresys: CoreSys):
99
"""Test a exists network port."""
10-
assert await check_port(coresys.loop, ip_address("8.8.8.8"), 53)
10+
assert await check_port(ip_address("8.8.8.8"), 53)
1111

1212

1313
async def test_not_exists_port(coresys: CoreSys):
1414
"""Test a not exists network service."""
15-
assert not await check_port(coresys.loop, ip_address("192.0.2.1"), 53)
15+
assert not await check_port(ip_address("192.0.2.1"), 53)

0 commit comments

Comments
 (0)