@@ -33,13 +33,11 @@ def __init__(
33
33
* ,
34
34
on_discovered : OnDiscoveredCallable = None ,
35
35
target : str = "255.255.255.255" ,
36
- timeout : int = 5 ,
37
36
discovery_packets : int = 3 ,
38
37
interface : Optional [str ] = None ,
39
38
):
40
39
self .transport = None
41
- self .tries = discovery_packets
42
- self .timeout = timeout
40
+ self .discovery_packets = discovery_packets
43
41
self .interface = interface
44
42
self .on_discovered = on_discovered
45
43
self .protocol = TPLinkSmartHomeProtocol ()
@@ -65,7 +63,7 @@ def do_discover(self) -> None:
65
63
req = json .dumps (Discover .DISCOVERY_QUERY )
66
64
_LOGGER .debug ("[DISCOVERY] %s >> %s" , self .target , Discover .DISCOVERY_QUERY )
67
65
encrypted_req = self .protocol .encrypt (req )
68
- for i in range (self .tries ):
66
+ for i in range (self .discovery_packets ):
69
67
self .transport .sendto (encrypted_req [4 :], self .target ) # type: ignore
70
68
71
69
def datagram_received (self , data , addr ) -> None :
@@ -176,7 +174,6 @@ async def discover(
176
174
lambda : _DiscoverProtocol (
177
175
target = target ,
178
176
on_discovered = on_discovered ,
179
- timeout = timeout ,
180
177
discovery_packets = discovery_packets ,
181
178
interface = interface ,
182
179
),
@@ -186,7 +183,7 @@ async def discover(
186
183
187
184
try :
188
185
_LOGGER .debug ("Waiting %s seconds for responses..." , timeout )
189
- await asyncio .sleep (5 )
186
+ await asyncio .sleep (timeout )
190
187
finally :
191
188
transport .close ()
192
189
@@ -220,32 +217,24 @@ async def discover_single(host: str) -> SmartDevice:
220
217
@staticmethod
221
218
def _get_device_class (info : dict ) -> Type [SmartDevice ]:
222
219
"""Find SmartDevice subclass for device described by passed data."""
223
- if "system" in info and "get_sysinfo" in info ["system" ]:
224
- sysinfo = info ["system" ]["get_sysinfo" ]
225
- if "type" in sysinfo :
226
- type_ = sysinfo ["type" ]
227
- elif "mic_type" in sysinfo :
228
- type_ = sysinfo ["mic_type" ]
229
- else :
230
- raise SmartDeviceException ("Unable to find the device type field!" )
231
- else :
232
- raise SmartDeviceException ("No 'system' nor 'get_sysinfo' in response" )
220
+ if "system" not in info or "get_sysinfo" not in info ["system" ]:
221
+ raise SmartDeviceException ("No 'system' or 'get_sysinfo' in response" )
233
222
234
- if (
235
- "smartlife.iot.dimmer" in info
236
- and "get_dimmer_parameters" in info ["smartlife.iot.dimmer" ]
237
- ):
238
- return SmartDimmer
223
+ sysinfo = info ["system" ]["get_sysinfo" ]
224
+ type_ = sysinfo .get ("type" , sysinfo .get ("mic_type" ))
225
+ if type_ is None :
226
+ raise SmartDeviceException ("Unable to find the device type field!" )
239
227
240
- elif "smartplug " in type_ . lower () and "children " in sysinfo :
241
- return SmartStrip
228
+ if "dev_name " in sysinfo and "Dimmer " in sysinfo [ "dev_name" ] :
229
+ return SmartDimmer
242
230
243
- elif "smartplug" in type_ .lower ():
231
+ if "smartplug" in type_ .lower ():
244
232
if "children" in sysinfo :
245
233
return SmartStrip
246
234
247
235
return SmartPlug
248
- elif "smartbulb" in type_ .lower ():
236
+
237
+ if "smartbulb" in type_ .lower ():
249
238
if "length" in sysinfo : # strips have length
250
239
return SmartLightStrip
251
240
0 commit comments