8000 Simplify device class detection for discovery, fix mistakenly hardcod… · python-kasa/python-kasa@c8a1e07 · GitHub
[go: up one dir, main page]

Skip to content

Commit c8a1e07

Browse files
committed
Simplify device class detection for discovery, fix mistakenly hardcoded timeout
1 parent 70061cb commit c8a1e07

File tree

1 file changed

+14
-25
lines changed

1 file changed

+14
-25
lines changed

kasa/discover.py

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,11 @@ def __init__(
3333
*,
3434
on_discovered: OnDiscoveredCallable = None,
3535
target: str = "255.255.255.255",
36-
timeout: int = 5,
3736
discovery_packets: int = 3,
3837
interface: Optional[str] = None,
3938
):
4039
self.transport = None
41-
self.tries = discovery_packets
42-
self.timeout = timeout
40+
self.discovery_packets = discovery_packets
4341
self.interface = interface
4442
self.on_discovered = on_discovered
4543
self.protocol = TPLinkSmartHomeProtocol()
@@ -65,7 +63,7 @@ def do_discover(self) -> None:
6563
req = json.dumps(Discover.DISCOVERY_QUERY)
6664
_LOGGER.debug("[DISCOVERY] %s >> %s", self.target, Discover.DISCOVERY_QUERY)
6765
encrypted_req = self.protocol.encrypt(req)
68-
for i in range(self.tries):
66+
for i in range(self.discovery_packets):
6967
self.transport.sendto(encrypted_req[4:], self.target) # type: ignore
7068

7169
def datagram_received(self, data, addr) -> None:
@@ -176,7 +174,6 @@ async def discover(
176174
lambda: _DiscoverProtocol(
177175
target=target,
178176
on_discovered=on_discovered,
179-
timeout=timeout,
180177
discovery_packets=discovery_packets,
181178
interface=interface,
182179
),
@@ -186,7 +183,7 @@ async def discover(
186183

187184
try:
188185
_LOGGER.debug("Waiting %s seconds for responses...", timeout)
189-
await asyncio.sleep(5)
186+
await asyncio.sleep(timeout)
190187
finally:
191188
transport.close()
192189

@@ -220,32 +217,24 @@ async def discover_single(host: str) -> SmartDevice:
220217
@staticmethod
221218
def _get_device_class(info: dict) -> Type[SmartDevice]:
222219
"""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")
233222

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!")
239227

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
242230

243-
elif "smartplug" in type_.lower():
231+
if "smartplug" in type_.lower():
244232
if "children" in sysinfo:
245233
return SmartStrip
246234

247235
return SmartPlug
248-
elif "smartbulb" in type_.lower():
236+
237+
if "smartbulb" in type_.lower():
249238
if "length" in sysinfo: # strips have length
250239
return SmartLightStrip
251240

0 commit comments

Comments
 (0)
0