9
9
10
10
import asyncio
11
11
from functools import partial
12
- from ipaddress import IPv4Address , IPv4Network
13
12
import logging
14
13
from typing import TYPE_CHECKING
15
14
@@ -132,6 +131,7 @@ async def scan(self) -> list[GatewayDescriptor]:
132
131
pass
133
132
finally :
134
133
await self ._stop ()
134
+
135
135
return self .found_gateways
136
136
137
137
async def _stop (self ) -> None :
@@ -145,15 +145,12 @@ async def _send_search_requests(self) -> None:
145
145
try :
146
146
af_inet = netifaces .ifaddresses (interface )[netifaces .AF_INET ]
147
147
ip_addr = af_inet [0 ]["addr" ]
148
- netmask = af_inet [0 ]["netmask" ]
149
- await self ._search_interface (interface , ip_addr , netmask )
148
+ await self ._search_interface (interface , ip_addr )
150
149
except KeyError :
151
150
logger .info ("Could not connect to an KNX/IP device on %s" , interface )
152
151
continue
153
152
154
- async def _search_interface (
155
- self , interface : str , ip_addr : str , netmask : str
156
- ) -> None :
153
+ async def _search_interface (self , interface : str , ip_addr : str ) -> None :
157
154
"""Send a search request on a specific interface."""
158
155
logger .debug ("Searching on %s / %s" , interface , ip_addr )
159
156
@@ -165,7 +162,7 @@ async def _search_interface(
165
162
)
166
163
167
164
udp_client .register_callback (
168
- partial (self ._response_rec_callback , interface = interface , netmask = netmask ),
165
+ partial (self ._response_rec_callback , interface = interface ),
169
166
[KNXIPServiceType .SEARCH_RESPONSE ],
170
167
)
171
168
await udp_client .connect ()
@@ -183,21 +180,12 @@ def _response_rec_callback(
183
180
knx_ip_frame : KNXIPFrame ,
184
181
udp_client : UDPClient ,
185
182
interface : str = "" ,
186
- netmask : str = "" ,
187
183
) -> None :
188
184
"""Verify and handle knxipframe. Callback from internal udpclient."""
189
185
if not isinstance (knx_ip_frame .body , SearchResponse ):
190
186
logger .warning ("Could not understand knxipframe" )
191
187
return
192
188
193
- address : IPv4Address = IPv4Address (knx_ip_frame .body .control_endpoint .ip_addr )
194
- network : IPv4Network = IPv4Network (
195
- f"{ udp_client .local_addr [0 ]} /{ netmask } " , False
196
- )
197
-
198
- if address not in network :
199
- return
200
-
201
189
gateway = GatewayDescriptor (
202
190
name = knx_ip_frame .body .device_name ,
203
191
ip_addr = knx_ip_frame .body .control_endpoint .ip_addr ,
@@ -229,7 +217,10 @@ def _response_rec_callback(
229
217
self ._add_found_gateway (gateway )
230
218
231
219
def _add_found_gateway (self , gateway : GatewayDescriptor ) -> None :
232
- if self .scan_filter .match (gateway ):
220
+ if self .scan_filter .match (gateway ) and not any (
221
+ _gateway .individual_address == gateway .individual_address
222
+ for _gateway in self .found_gateways
223
+ ):
233
224
self .found_gateways .append (gateway )
234
225
if 0 < self ._count_upper_bound <= len (self .found_gateways ):
235
226
self ._response_received_event .set ()
0 commit comments