FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Fix dump_devinfo and add discovery_result to json · python-kasa/python-kasa@a077f19 · GitHub

Repository navigation

Commit a077f19

Browse files
committed
Fix dump_devinfo and add discovery_result to json
1 parent d3c2861 commit a077f19

6 files changed

Lines changed: 90 additions & 42 deletions

File tree

‎devtools/dump_devinfo.py‎

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,16 @@
77
Executing this script will several modules and methods one by one,
88
and finally execute a query to query all of them at once.
99
"""
10-
import asyncio
1110
import collections.abc
1211
import json
1312
import logging
1413
import re
1514
from collections import defaultdict, namedtuple
1615
from pprint import pprint
1716

18-
import click
17+
import asyncclick as click
1918

20-
from kasa import TPLinkSmartHomeProtocol
19+
from kasa import Credentials, Discover
2120

2221
Call = namedtuple("Call", "module method")
2322

@@ -35,6 +34,8 @@ def scrub(res):
3534
"longitude_i",
3635
"latitude",
3736
"longitude",
37+
"device_owner_hash",
38+
"device_id_hash",
3839
]
3940

4041
for k, v in res.items():
@@ -63,8 +64,22 @@ def default_to_regular(d):
6364

6465
@click.command()
6566
@click.argument("host")
67+
@click.option(
68+
"--username",
69+
default=None,
70+
required=False,
71+
envvar="TPLINK_CLOUD_USERNAME",
72+
help="Username/email address to authenticate to device.",
73+
)
74+
@click.option(
75+
"--password",
76+
default=None,
77+
required=False,
78+
envvar="TPLINK_CLOUD_PASSWORD",
79+
help="Password to use to authenticate to device.",
80+
)
6681
@click.option("-d", "--debug", is_flag=True)
67-
def cli(host, debug):
82+
async def cli(host, debug, username, password):
6883
"""Generate devinfo file for given device."""
6984
if debug:
7085
logging.basicConfig(level=logging.DEBUG)
@@ -83,15 +98,15 @@ def cli(host, debug):
8398

8499
successes = []
85100

86-
for test_call in items:
87-
88-
async def _run_query(test_call):
89-
protocol = TPLinkSmartHomeProtocol(host)
90-
return await protocol.query({test_call.module: {test_call.method: None}})
101+
credentials = Credentials(username=username, password=password)
102+
device = await Discover.discover_single(host, credentials=credentials)
91103

104+
for test_call in items:
92105
try:
93106
click.echo(f"Testing {test_call}..", nl=False)
94-
info = asyncio.run(_run_query(test_call))
107+
info = await device.protocol.query(
108+
{test_call.module: {test_call.method: None}}
109+
)
95110
resp = info[test_call.module]
96111
except Exception as ex:
97112
click.echo(click.style(f"FAIL {ex}", fg="red"))
@@ -111,19 +126,18 @@ async def _run_query(test_call):
111126

112127
final = default_to_regular(final)
113128

114-
async def _run_final_query():
115-
protocol = TPLinkSmartHomeProtocol(host)
116-
return await protocol.query(final_query)
117-
118129
try:
119-
final = asyncio.run(_run_final_query())
130+
final = await device.protocol.query(final_query)
120131
except Exception as ex:
121132
click.echo(
122133
click.style(
123134
f"Unable to query all successes at once: {ex}", bold=True, fg="red"
124135
)
125136
)
126137

138+
if device.discovery_info:
139+
final["discovery_result"] = device.discovery_info
140+
127141
click.echo("Got %s successes" % len(successes))
128142
click.echo(click.style("## device info file ##", bold=True))
129143

‎kasa/discover.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ class EncryptionScheme(BaseModel):
423423
mac: str
424424
mgt_encrypt_schm: EncryptionScheme
425425

426-
device_id: Optional[str] = Field(default=None, alias="device_type_hash")
426+
device_id: Optional[str] = Field(default=None, alias="device_id_hash")
427427
owner: Optional[str] = Field(default=None, alias="device_owner_hash")
428428
hw_ver: Optional[str] = None
429429
is_support_iot_cloud: Optional[bool] = None

‎kasa/smartdevice.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ def __init__(
211211
# checks in accessors. the @updated_required decorator does not ensure
212212
# mypy that these are not accessed incorrectly.
213213
self._last_update: Any = None
214+
self.discovery_info: Optional[Dict[str, Any]] = None
214215

215216
self._sys_info: Any = None # TODO: this is here to avoid changing tests
216217
self._features: Set[str] = set()
@@ -371,6 +372,7 @@ def update_from_discover_info(self, info: Dict[str, Any]) -> None:
371372
# This allows setting of some info properties directly
372373
# from partial discovery info that will then be found
373374
# by the requires_update decorator
375+
self.discovery_info = info
374376
self._set_sys_info(info)
375377

376378
def _set_sys_info(self, sys_info: Dict[str, Any]) -> None:
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"system": {
3+
"get_sysinfo": {
4+
"active_mode": "schedule",
5+
"alias": "Living Room Lamp",
6+
"dev_name": "Wi-Fi Smart Plug",
7+
"deviceId": "0000000000000000000000000000000000000000",
8+
"err_code": 0,
9+
"feature": "TIM",
10+
"fwId": "00000000000000000000000000000000",
11+
"hwId": "00000000000000000000000000000000",
12+
"hw_ver": "1.0",
13+
"icon_hash": "",
14+
"latitude": 0,
15+
"led_off": 0,
16+
"longitude": 0,
17+
"mac": "00:00:00:00:00:00",
18+
"model": "HS100(UK)",
19+
"oemId": "00000000000000000000000000000000",
20+
"on_time": 4102,
21+
"relay_state": 1,
22+
"rssi": -58,
23+
"sw_ver": "1.2.6 Build 200727 Rel.120236",
24+
"type": "IOT.SMARTPLUGSWITCH",
25+
"updating": 0
26+
}
27+
}
28+
}

‎kasa/tests/fixtures/HS100(UK)_4.1_1.1.0.json‎

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,46 @@
11
{
2-
"emeter": {
3-
"err_code": -1,
4-
"err_msg": "module not support"
5-
},
6-
"smartlife.iot.common.emeter": {
7-
"err_code": -1,
8-
"err_msg": "module not support"
9-
},
10-
"smartlife.iot.dimmer": {
11-
"err_code": -1,
12-
"err_msg": "module not support"
13-
},
14-
"smartlife.iot.smartbulb.lightingservice": {
15-
"err_code": -1,
16-
"err_msg": "module not support"
2+
"discovery_result": {
3+
"alias": "192.168.1.84",
4+
"device_id_hash": "00000000000000000000000000000000",
5+
"device_owner_hash": "00000000000000000000000000000000",
6+
"device_type_text": "IOT.SMARTPLUGSWITCH",
7+
"factory_default": true,
8+
"hw_ver": "4.1",
9+
"mac": "00-00-00-00-00-00",
10+
"mgt_encrypt_schm": {
11+
"encrypt_type": "KLAP",
12+
"http_port": 80,
13+
"is_support_https": false
14+
},
15+
"model": "HS100(UK)"
1716
},
1817
"system": {
1918
"get_sysinfo": {
20-
"active_mode": "schedule",
21-
"alias": "Unused 3",
19+
"active_mode": "none",
20+
"alias": "Bedroom Lamp 2",
2221
"dev_name": "Smart Wi-Fi Plug",
2322
"deviceId": "0000000000000000000000000000000000000000",
2423
"err_code": 0,
2524
"feature": "TIM",
26-
"fwId": "00000000000000000000000000000000",
2725
"hwId": "00000000000000000000000000000000",
2826
"hw_ver": "4.1",
2927
"icon_hash": "",
30-
"latitude": 0,
28+
"latitude_i": 0,
3129
"led_off": 0,
32-
"longitude": 0,
30+
"longitude_i": 0,
3331
"mac": "00:00:00:00:00:00",
32+
"mic_type": "IOT.SMARTPLUGSWITCH",
3433
"model": "HS100(UK)",
34+
"next_action": {
35+
"type": -1
36+
},
37+
"ntc_state": 0,
3538
"oemId": "00000000000000000000000000000000",
36-
"on_time": 0,
37-
"relay_state": 0,
39+
"on_time": 3115,
40+
"relay_state": 1,
3841
"rssi": -63,
42+
"status": "new",
3943
"sw_ver": "1.1.0 Build 201016 Rel.175121",
40-
"type": "IOT.SMARTPLUGSWITCH",
4144
"updating": 0
4245
}
4346
}

‎kasa/tests/newfakes.py‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -294,9 +294,10 @@ def __init__(self, info):
294294

295295
for target in info:
296296
# print("target %s" % target)
297-
for cmd in info[target]:
298-
# print("initializing tgt %s cmd %s" % (target, cmd))
299-
proto[target][cmd] = info[target][cmd]
297+
if target != "discovery_result":
298+
for cmd in info[target]:
299+
# print("initializing tgt %s cmd %s" % (target, cmd))
300+
proto[target][cmd] = info[target][cmd]
300301
# if we have emeter support, we need to add the missing pieces
301302
for module in ["emeter", "smartlife.iot.common.emeter"]:
302303
if (

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL