8000 Add new methods to dump_devinfo by sdb9696 · Pull Request #1373 · python-kasa/python-kasa · GitHub
[go: up one dir, main page]

Skip to content

Add new methods to dump_devinfo #1373

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions devtools/helpers/smartcamrequests.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,7 @@
{"get": {"motor": {"name": ["capability"]}}},
{"get": {"audio_capability": {"name": ["device_speaker", "device_microphone"]}}},
{"get": {"audio_config": {"name": ["speaker", "microphone"]}}},
{"getMatterSetupInfo": {"matter": {}}},
{"getConnectStatus": {"onboarding": {"get_connect_status": {}}}},
{"scanApList": {"onboarding": {"scan": {}}}},
]
18 changes: 11 additions & 7 deletions kasa/protocols/smartprotocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
"bssid": lambda _: "000000000000",
"channel": lambda _: 0,
"oem_id": lambda x: "REDACTED_" + x[9::],
"hw_id": lambda x: "REDACTED_" + x[9::],
"fw_id": lambda x: "REDACTED_" + x[9::],
"setup_code": lambda x: re.sub(r"\w", "0", x), # matter
"setup_payload": lambda x: re.sub(r"\w", "0", x), # matter
"mfi_setup_code": lambda x: re.sub(r"\w", "0", x), # mfi_ for homekit
Expand Down Expand Up @@ -183,18 +185,18 @@ async def _execute_multiple_query(self, requests: dict, retry_count: int) -> dic
multi_result: dict[str, Any] = {}
smart_method = "multipleRequest"

end = len(requests)
# The SmartCamProtocol sends requests with a length 1 as a
# multipleRequest. The SmartProtocol doesn't so will never
# raise_on_error
raise_on_error = end == 1

Comment on lines +188 to +193
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With FORCE_SINGLE_REQUEST this needs to happen before building the multi_requests

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, but why, when neither end nor raise_on_error is used in the comprehension?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

raise_on_error is used to ensure that single requests always raise an error which is the default behaviour. When we introduced FORCE_SINGLE I missed the fact that they will not error correctly because end = len(multi_requests) gives zero.

multi_requests = [
{"method": method, "params": params} if params else {"method": method}
for method, params in requests.items()
if method not in FORCE_SINGLE_REQUEST
]

end = len(multi_requests)
# The SmartCamProtocol sends requests with a length 1 as a
# multipleRequest. The SmartProtocol doesn't so will never
# raise_on_error
raise_on_error = end == 1

# Break the requests down as there can be a size limit
step = self._multi_request_batch_size
if step == 1:
Expand Down Expand Up @@ -285,7 +287,9 @@ async def _execute_multiple_query(self, requests: dict, retry_count: int) -> dic
resp = await self._transport.send(
self.get_smart_request(method, params)
)
self._handle_response_error_code(resp, method, raise_on_error=False)
self._handle_response_error_code(
resp, method, raise_on_error=raise_on_error
)
Comment on lines +290 to +292
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With FORCE_SINGLE_REQUEST this also needs to use the raise_on_error variable

multi_result[method] = resp.get("result")
return multi_result

Expand Down
55 changes: 29 additions & 26 deletions tests/fakeprotocol_smartcam.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,35 +221,38 @@ async def _send_request(self, request_dict: dict):
return {**result, "error_code": 0}
else:
return {"error_code": -1}
elif method[:3] == "get":

if method in info:
params = request_dict.get("params")
if method in info:
result = copy.deepcopy(info[method])
if "start_index" in result and "sum" in result:
list_key = next(
iter([key for key in result if isinstance(result[key], list)])
)
start_index = (
start_index
if (params and (start_index := params.get("start_index")))
else 0
)

result[list_key] = result[list_key][
start_index : start_index + self.list_return_size
]
return {"result": result, "error_code": 0}
if (
# FIXTURE_MISSING is for service calls not in place when
# SMART fixtures started to be generated
missing_result := self.FIXTURE_MISSING_MAP.get(method)
) and missing_result[0] in self.components:
# Copy to info so it will work with update methods
info[method] = copy.deepcopy(missing_result[1])
result = copy.deepcopy(info[method])
return {"result": result, "error_code": 0}
result = copy.deepcopy(info[method])
if "start_index" in result and "sum" in result:
list_key = next(
iter([key for key in result if isinstance(result[key], list)])
)
start_index = (
start_index
if (params and (start_index := params.get("start_index")))
else 0
)

result[list_key] = result[list_key][
start_index : start_index + self.list_return_size
]
return {"result": result, "error_code": 0}

if self.verbatim:
return {"error_code": -1}
Comment on lines +243 to 244
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both lines are added here even though the diff makes it look like only the if is added. This ensures that the fixture generation tests work and the test framework does not insert missing methods (as per the FakeSmartTransport)


if (
# FIXTURE_MISSING is for service calls not in place when
# SMART fixtures started to be generated
missing_result := self.FIXTURE_MISSING_MAP.get(method)
) and missing_result[0] in self.components:
# Copy to info so it will work with update methods
info[method] = copy.deepcopy(missing_result[1])
result = copy.deepcopy(info[method])
return {"result": result, "error_code": 0}

return {"error_code": -1}

async def close(self) -> None:
Expand Down
56 changes: 47 additions & 9 deletions tests/fixtures/smartcam/C210(EU)_2.0_1.4.3.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
"connect_type": "wireless",
"device_id": "0000000000000000000000000000000000000000",
"http_port": 443,
"last_alarm_time": "0",
"last_alarm_type": "",
"last_alarm_time": "1733422805",
"last_alarm_type": "motion",
"owner": "00000000000000000000000000000000",
"sd_status": "offline"
},
Expand All @@ -32,7 +32,8 @@
"mac": "40-AE-30-00-00-00",
"mgt_encrypt_schm": {
"is_support_https": true
}
},
"protocol_version": 1
}
},
"getAlertConfig": {
Expand Down Expand Up @@ -266,15 +267,22 @@
"getClockStatus": {
"system": {
"clock_status": {
"local_time": "2024-11-01 13:58:50",
"seconds_from_1970": 1730469530
"local_time": "2024-12-15 11:28:40",
"seconds_from_1970": 1734262120
}
}
},
"getConnectStatus": {
"onboarding": {
"get_connect_status": {
"status": 0
}
}
},
"getConnectionType": {
"link_type": "wifi",
"rssi": "3",
"rssiValue": -57,
"rssiValue": -61,
"ssid": "I01BU0tFRF9TU0lEIw=="
},
"getDetectionConfig": {
Expand Down Expand Up @@ -321,7 +329,7 @@
"getFirmwareAutoUpgradeConfig": {
"auto_upgrade": {
"common": {
"enabled": "on",
"enabled": "off",
"random_range": "120",
"time": "03:00"
}
Expand All @@ -338,8 +346,8 @@
"getLastAlarmInfo": {
"system": {
"last_alarm_info": {
"last_alarm_time": "0",
"last_alarm_type": ""
"last_alarm_time": "1733422805",
"last_alarm_type": "motion"
}
}
},
Expand Down Expand Up @@ -961,5 +969,35 @@
}
}
}
},
"scanApList": {
"onboarding": {
"scan": {
"ap_list": [
{
"auth": 4,
"bssid": "000000000000",
"encryption": 3,
"rssi": 2,
"ssid": "I01BU0tFRF9TU0lEIw=="
},
{
"auth": 4,
"bssid": "000000000000",
"encryption": 3,
"rssi": 1,
"ssid": "I01BU0tFRF9TU0lEIw=="
},
{
"auth": 4,
"bssid": "000000000000",
"encryption": 3,
"rssi": 0,
"ssid": "I01BU0tFRF9TU0lEIw=="
}
],
"wpa3_supported": "false"
}
}
}
}
119 changes: 113 additions & 6 deletions tests/fixtures/smartcam/H200(EU)_1.0_1.3.2.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"firmware_version": "1.3.2 Build 20240424 rel.75425",
"hardware_version": "1.0",
"ip": "127.0.0.123",
"isResetWiFi": false,
"is_support_iot_cloud": true,
"mac": "A8-6E-84-00-00-00",
"mgt_encrypt_schm": {
Expand Down Expand Up @@ -214,8 +215,8 @@
"fw_ver": "1.11.0 Build 230821 Rel.113553",
"hw_id": "00000000000000000000000000000000",
"hw_ver": "1.0",
"jamming_rssi": -108,
"jamming_signal_level": 2,
"jamming_rssi": -119,
"jamming_signal_level": 1,
"lastOnboardingTimestamp": 1714016798,
"mac": "202351000000",
"model": "S200B",
Expand All @@ -224,7 +225,7 @@
"parent_device_id": "0000000000000000000000000000000000000000",
"region": "Europe/London",
"report_interval": 16,
"rssi": -66,
"rssi": -60,
"signal_level": 3,
"specs": "EU",
"status": "online",
Expand All @@ -245,8 +246,17 @@
"getClockStatus": {
"system": {
"clock_status": {
"local_time": "2024-11-01 13:56:27",
"seconds_from_1970": 1730469387
"local_time": "1984-10-21 23:48:23",
"seconds_from_1970": 467246903
}
}
},
"getConnectStatus": {
"onboarding": {
"get_connect_status": {
"current_ssid": "",
"err_code": 0,
"status": 0
}
}
},
Expand Down Expand Up @@ -329,6 +339,10 @@
}
}
},
"getMatterSetupInfo": {
"setup_code": "00000000000",
"setup_payload": "00:000000-000000000000"
},
"getMediaEncrypt": {
"cet": {
"media_encrypt": {
Expand All @@ -353,7 +367,7 @@
"getSirenConfig": {
"duration": 300,
"siren_type": "Doorbell Ring 1",
"volume": "6"
"volume": "1"
},
"getSirenStatus": {
"status": "off",
Expand Down Expand Up @@ -389,5 +403,98 @@
"zone_id": "Europe/London"
}
}
},
"scanApList": {
"onboarding": {
"scan": {
"ap_list": [
{
"auth": 3,
"bssid": "000000000000",
"encryption": 2,
"rssi": 2,
"ssid": "I01BU0tFRF9TU0lEIw=="
},
{
"auth": 3,
"bssid": "000000000000",
"encryption": 2,
"rssi": 1,
"ssid": "I01BU0tFRF9TU0lEIw=="
},
{
"auth": 0,
"bssid": "000000000000",
"encryption": 0,
"rssi": 2,
"ssid": "I01BU0tFRF9TU0lEIw=="
},
{
"auth": 3,
"bssid": "000000000000",
"encryption": 2,
"rssi": 1,
"ssid": "I01BU0tFRF9TU0lEIw=="
},
{
"auth": 3,
"bssid": "000000000000",
"encryption": 2,
"rssi": 1,
"ssid": "I01BU0tFRF9TU0lEIw=="
},
{
"auth": 3,
"bssid": "000000000000",
"encryption": 2,
"rssi": 1,
"ssid": "I01BU0tFRF9TU0lEIw=="
},
{
"auth": 3,
"bssid": "000000000000",
"encryption": 2,
"rssi": 3,
"ssid": "I01BU0tFRF9TU0lEIw=="
},
{
"auth": 3,
"bssid": "000000000000",
"encryption": 2,
"rssi": 3,
"ssid": "I01BU0tFRF9TU0lEIw=="
},
{
"auth": 3,
"bssid": "000000000000",
"encryption": 2,
"rssi": 1,
"ssid": "I01BU0tFRF9TU0lEIw=="
},
{
"auth": 4,
"bssid": "000000000000",
"encryption": 3,
"rssi": 2,
"ssid": "I01BU0tFRF9TU0lEIw=="
},
{
"auth": 3,
"bssid": "000000000000",
"encryption": 2,
"rssi": 1,
"ssid": "I01BU0tFRF9TU0lEIw=="
},
{
"auth": 3,
"bssid": "000000000000",
"encryption": 2,
"rssi": 1,
"ssid": "I01BU0tFRF9TU0lEIw=="
}
],
"wpa3_supported": "false"
}
}
}
}
Loading
0