Skip to content
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

Add function to get camera verification code. #10

Merged
merged 2 commits into from
Jan 12, 2025
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
1 change: 1 addition & 0 deletions pyezvizapi/api_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
API_ENDPOINT_DETECTION_SENSIBILITY_GET = "/api/device/queryAlgorithmConfig"
API_ENDPOINT_SET_DEFENCE_SCHEDULE = "/api/device/defence/plan2"
API_ENDPOINT_CAM_ENCRYPTKEY = "/api/device/query/encryptkey"
API_ENDPOINT_CAM_AUTH_CODE = "/v3/devconfig/authcode/query/"
API_ENDPOINT_CANCEL_ALARM = "/api/device/cancelAlarm"
API_ENDPOINT_DEVICE_SYS_OPERATION = "/api/device/v2/sysOper/"
API_ENDPOINT_DEVICE_STORAGE_STATUS = "/api/device/queryStorageStatus"
Expand Down
81 changes: 59 additions & 22 deletions pyezvizapi/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from .api_endpoints import (
API_ENDPOINT_ALARM_SOUND,
API_ENDPOINT_ALARMINFO_GET,
API_ENDPOINT_CAM_AUTH_CODE,
API_ENDPOINT_CAM_ENCRYPTKEY,
API_ENDPOINT_CANCEL_ALARM,
API_ENDPOINT_CHANGE_DEFENCE_STATUS,
Expand Down Expand Up @@ -340,7 +341,6 @@ def _api_get_pagelist(

return data


def get_alarminfo(self, serial: str, limit: int = 1, max_retries: int = 0) -> dict:
"""Get data from alarm info API for camera serial."""
if max_retries > MAX_RETRIES:
Expand Down Expand Up @@ -950,15 +950,11 @@ def set_video_enc(
if max_retries > MAX_RETRIES:
raise PyEzvizError("Can't gather proper data. Max retries exceeded.")

device_token_info = self.get_user_id()
cookies = {
"clientType": "3",
"clientVersion": "5.12.1.0517",
"userId": device_token_info["userId"],
"ASG_DisplayName": "home",
"C_SS": self._session.headers["sessionId"],
"lang": "en_US",
}
if enable == 2 and not old_password:
raise PyEzvizError("Old password is required when changing password.")

if new_password and not enable == 2:
raise PyEzvizError("New password is only required when changing password.")

try:
req = self._session.put(
Expand All @@ -968,14 +964,13 @@ def set_video_enc(
+ API_ENDPOINT_VIDEO_ENCRYPT,
data={
"deviceSerial": serial,
"isEncrypt": enable,
"oldPassword": old_password,
"isEncrypt": enable, # 1 = enable, 0 = disable, 2 = change password
"oldPassword": old_password, # required if changing.
"password": new_password,
"featureCode": FEATURE_CODE,
"validateCode": camera_verification_code,
"msgType": -1,
},
cookies=cookies,
timeout=self._timeout,
)

Expand Down Expand Up @@ -1368,6 +1363,54 @@ def get_cam_key(

return json_output["encryptkey"]

def get_cam_auth_code(self, serial: str, max_retries: int = 0) -> Any:
"""Get Camera auth code. This is the verification code on the camera sticker."""

if max_retries > MAX_RETRIES:
raise PyEzvizError("Can't gather proper data. Max retries exceeded.")

try:
req = self._session.get(
"https://"
+ self._token["api_url"]
+ API_ENDPOINT_CAM_AUTH_CODE
+ serial,
params={
"encrptPwd": "",
"msgAuthCode": "",
"senderType": 0,
},
timeout=self._timeout,
)

req.raise_for_status()

except requests.HTTPError as err:
if err.response.status_code == 401:
# session is wrong, need to relogin
self.login()
return self.get_cam_auth_code(serial, max_retries + 1)

raise HTTPError from err

try:
json_output = req.json()

except ValueError as err:
raise PyEzvizError(
"Impossible to decode response: "
+ str(err)
+ "\nResponse was: "
+ str(req.text)
) from err

if json_output["meta"]["code"] != 200:
raise PyEzvizError(
f"Could not get camera verification key: Got {json_output})"
)

return json_output["devAuthCode"]

def create_panoramic(self, serial: str, max_retries: int = 0) -> Any:
"""Create panoramic image."""

Expand Down Expand Up @@ -2044,29 +2087,23 @@ def get_device(self) -> Any:

def get_connection(self) -> Any:
"""Get ezviz connection infos filter."""
return self._api_get_pagelist(
page_filter="CONNECTION", json_key="CONNECTION"
)
return self._api_get_pagelist(page_filter="CONNECTION", json_key="CONNECTION")

def _get_status(self) -> Any:
"""Get ezviz status infos filter."""
return self._api_get_pagelist(page_filter="STATUS", json_key="STATUS")

def get_switch(self) -> Any:
"""Get ezviz switch infos filter."""
return self._api_get_pagelist(
page_filter="SWITCH", json_key="SWITCH"
)
return self._api_get_pagelist(page_filter="SWITCH", json_key="SWITCH")

def _get_wifi(self) -> Any:
"""Get ezviz wifi infos filter."""
return self._api_get_pagelist(page_filter="WIFI", json_key="WIFI")

def _get_nodisturb(self) -> Any:
"""Get ezviz nodisturb infos filter."""
return self._api_get_pagelist(
page_filter="NODISTURB", json_key="NODISTURB"
)
return self._api_get_pagelist(page_filter="NODISTURB", json_key="NODISTURB")

def _get_p2p(self) -> Any:
"""Get ezviz P2P infos filter."""
Expand Down