| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -7,17 +7,16 @@ | |||
| 7 | 7 | Executing this script will several modules and methods one by one, | |
| 8 | 8 | and finally execute a query to query all of them at once. | |
| 9 | 9 | """ | |
| 10 | - import asyncio | ||
| 11 | 10 | import collections.abc | |
| 12 | 11 | import json | |
| 13 | 12 | import logging | |
| 14 | 13 | import re | |
| 15 | 14 | from collections import defaultdict, namedtuple | |
| 16 | 15 | from pprint import pprint | |
| 17 | 16 | ||
| 18 | - import click | ||
| 17 | + import asyncclick as click | ||
| 19 | 18 | ||
| 20 | - from kasa import TPLinkSmartHomeProtocol | ||
| 19 | + from kasa import Credentials, Discover | ||
| 21 | 20 | ||
| 22 | 21 | Call = namedtuple("Call", "module method") | |
| 23 | 22 | ||
@@ -35,6 +34,8 @@ def scrub(res): | |||
| 35 | 34 | "longitude_i", | |
| 36 | 35 | "latitude", | |
| 37 | 36 | "longitude", | |
| 37 | + "device_owner_hash", | ||
| 38 | + "device_id_hash", | ||
| 38 | 39 | ] | |
| 39 | 40 | ||
| 40 | 41 | for k, v in res.items(): | |
@@ -63,8 +64,22 @@ def default_to_regular(d): | |||
| 63 | 64 | ||
| 64 | 65 | @click.command() | |
| 65 | 66 | @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 | + ) | ||
| 66 | 81 | @click.option("-d", "--debug", is_flag=True) | |
| 67 | - def cli(host, debug): | ||
| 82 | + async def cli(host, debug, username, password): | ||
| 68 | 83 | """Generate devinfo file for given device.""" | |
| 69 | 84 | if debug: | |
| 70 | 85 | logging.basicConfig(level=logging.DEBUG) | |
@@ -83,15 +98,15 @@ def cli(host, debug): | |||
| 83 | 98 | ||
| 84 | 99 | successes = [] | |
| 85 | 100 | ||
| 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) | ||
| 91 | 103 | ||
| 104 | + for test_call in items: | ||
| 92 | 105 | try: | |
| 93 | 106 | 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 | + ) | ||
| 95 | 110 | resp = info[test_call.module] | |
| 96 | 111 | except Exception as ex: | |
| 97 | 112 | click.echo(click.style(f"FAIL {ex}", fg="red")) | |
@@ -111,19 +126,18 @@ async def _run_query(test_call): | |||
| 111 | 126 | ||
| 112 | 127 | final = default_to_regular(final) | |
| 113 | 128 | ||
| 114 | - async def _run_final_query(): | ||
| 115 | - protocol = TPLinkSmartHomeProtocol(host) | ||
| 116 | - return await protocol.query(final_query) | ||
| 117 | - | ||
| 118 | 129 | try: | |
| 119 | - final = asyncio.run(_run_final_query()) | ||
| 130 | + final = await device.protocol.query(final_query) | ||
| 120 | 131 | except Exception as ex: | |
| 121 | 132 | click.echo( | |
| 122 | 133 | click.style( | |
| 123 | 134 | f"Unable to query all successes at once: {ex}", bold=True, fg="red" | |
| 124 | 135 | ) | |
| 125 | 136 | ) | |
| 126 | 137 | ||
| 138 | + if device.discovery_info: | ||
| 139 | + final["discovery_result"] = device.discovery_info | ||
| 140 | + | ||
| 127 | 141 | click.echo("Got %s successes" % len(successes)) | |
| 128 | 142 | click.echo(click.style("## device info file ##", bold=True)) | |
| 129 | 143 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -423,7 +423,7 @@ class EncryptionScheme(BaseModel): | |||
| 423 | 423 | mac: str | |
| 424 | 424 | mgt_encrypt_schm: EncryptionScheme | |
| 425 | 425 | ||
| 426 | - device_id: Optional[str] = Field(default=None, alias="device_type_hash") | ||
| 426 | + device_id: Optional[str] = Field(default=None, alias="device_id_hash") | ||
| 427 | 427 | owner: Optional[str] = Field(default=None, alias="device_owner_hash") | |
| 428 | 428 | hw_ver: Optional[str] = None | |
| 429 | 429 | is_support_iot_cloud: Optional[bool] = None | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -211,6 +211,7 @@ def __init__( | |||
| 211 | 211 | # checks in accessors. the @updated_required decorator does not ensure | |
| 212 | 212 | # mypy that these are not accessed incorrectly. | |
| 213 | 213 | self._last_update: Any = None | |
| 214 | + self.discovery_info: Optional[Dict[str, Any]] = None | ||
| 214 | 215 | ||
| 215 | 216 | self._sys_info: Any = None # TODO: this is here to avoid changing tests | |
| 216 | 217 | self._features: Set[str] = set() | |
@@ -371,6 +372,7 @@ def update_from_discover_info(self, info: Dict[str, Any]) -> None: | |||
| 371 | 372 | # This allows setting of some info properties directly | |
| 372 | 373 | # from partial discovery info that will then be found | |
| 373 | 374 | # by the requires_update decorator | |
| 375 | + self.discovery_info = info | ||
| 374 | 376 | self._set_sys_info(info) | |
| 375 | 377 | ||
| 376 | 378 | def _set_sys_info(self, sys_info: Dict[str, Any]) -> None: | |
| Original file line number | Diff line number | Diff 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 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,43 +1,46 @@ | |||
| 1 | 1 | { | |
| 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)" | ||
| 17 | 16 | }, | |
| 18 | 17 | "system": { | |
| 19 | 18 | "get_sysinfo": { | |
| 20 | - "active_mode": "schedule", | ||
| 21 | - "alias": "Unused 3", | ||
| 19 | + "active_mode": "none", | ||
| 20 | + "alias": "Bedroom Lamp 2", | ||
| 22 | 21 | "dev_name": "Smart Wi-Fi Plug", | |
| 23 | 22 | "deviceId": "0000000000000000000000000000000000000000", | |
| 24 | 23 | "err_code": 0, | |
| 25 | 24 | "feature": "TIM", | |
| 26 | - "fwId": "00000000000000000000000000000000", | ||
| 27 | 25 | "hwId": "00000000000000000000000000000000", | |
| 28 | 26 | "hw_ver": "4.1", | |
| 29 | 27 | "icon_hash": "", | |
| 30 | - "latitude": 0, | ||
| 28 | + "latitude_i": 0, | ||
| 31 | 29 | "led_off": 0, | |
| 32 | - "longitude": 0, | ||
| 30 | + "longitude_i": 0, | ||
| 33 | 31 | "mac": "00:00:00:00:00:00", | |
| 32 | + "mic_type": "IOT.SMARTPLUGSWITCH", | ||
| 34 | 33 | "model": "HS100(UK)", | |
| 34 | + "next_action": { | ||
| 35 | + "type": -1 | ||
| 36 | + }, | ||
| 37 | + "ntc_state": 0, | ||
| 35 | 38 | "oemId": "00000000000000000000000000000000", | |
| 36 | - "on_time": 0, | ||
| 37 | - "relay_state": 0, | ||
| 39 | + "on_time": 3115, | ||
| 40 | + "relay_state": 1, | ||
| 38 | 41 | "rssi": -63, | |
| 42 | + "status": "new", | ||
| 39 | 43 | "sw_ver": "1.1.0 Build 201016 Rel.175121", | |
| 40 | - "type": "IOT.SMARTPLUGSWITCH", | ||
| 41 | 44 | "updating": 0 | |
| 42 | 45 | } | |
| 43 | 46 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -294,9 +294,10 @@ def __init__(self, info): | |||
| 294 | 294 | ||
| 295 | 295 | for target in info: | |
| 296 | 296 | # 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] | ||
| 300 | 301 | # if we have emeter support, we need to add the missing pieces | |
| 301 | 302 | for module in ["emeter", "smartlife.iot.common.emeter"]: | |
| 302 | 303 | if ( | |
| Back | FazBrowse Home | New Git URL |
0 commit comments