Skip to content
This repository has been archived by the owner on Sep 13, 2022. It is now read-only.

Commit

Permalink
Fix #3
Browse files Browse the repository at this point in the history
  • Loading branch information
georgezhao2010 authored Jul 16, 2021
1 parent 602dfb4 commit d97b7a8
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 26 deletions.
60 changes: 35 additions & 25 deletions custom_components/climate_ewelink/ewelinkcloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,44 +56,54 @@ def login(self):
json.dumps(payload).encode(),
digestmod=hashlib.sha256).digest()
auth = "Sign " + base64.b64encode(hex_dig).decode()
r = requests.post(f"https://{SERVERS[self._country]}/api/user/login", json=payload,
headers={'Authorization': auth})
if r.status_code == 200:
rejson = r.json()
if "at" in rejson and "user" in rejson and "apikey" in rejson["user"]:
self._apikey = rejson["user"]["apikey"]
self._token = rejson["at"]
return True
try:
r = requests.post(f"https://{SERVERS[self._country]}/api/user/login", json=payload,
headers={'Authorization': auth})
if r.status_code == 200:
rejson = r.json()
if "at" in rejson and "user" in rejson and "apikey" in rejson["user"]:
self._apikey = rejson["user"]["apikey"]
self._token = rejson["at"]
return True
except Exception as e:
_LOGGER.debug("Network error in login")
pass
return False

def get_devices(self):
payload = {'getTags': 1}
payload = update_payload(payload)
auth = "Bearer " + self._token
devices: EWeLinkDevice[dict] = {}
r = requests.get(f"https://{SERVERS[self._country]}/api/user/device", params=payload,
headers={'Authorization': auth})
if r.status_code == 200:
rejson = r.json()

for index in range(len(rejson['devicelist'])):
if rejson['devicelist'][index]['uiid'] in SUPPORTED_CLIMATES:
devices[rejson['devicelist'][index]['deviceid']] = EWeLinkDevice(
rejson['devicelist'][index]['deviceid'],
rejson['devicelist'][index])
else:
_LOGGER.debug(f"Unsupported device: {rejson['devicelist'][index]}")
try:
r = requests.get(f"https://{SERVERS[self._country]}/api/user/device", params=payload,
headers={'Authorization': auth})
if r.status_code == 200:
rejson = r.json()

for index in range(len(rejson['devicelist'])):
if rejson['devicelist'][index]['uiid'] in SUPPORTED_CLIMATES:
devices[rejson['devicelist'][index]['deviceid']] = EWeLinkDevice(
rejson['devicelist'][index]['deviceid'],
rejson['devicelist'][index])
else:
_LOGGER.debug(f"Unsupported device: {rejson['devicelist'][index]}")
except Exception as e:
_LOGGER.debug("Network error in get devices")
return devices;

def get_ws_url(self):
payload = {'accept': 'ws'}
payload = update_payload(payload)
auth = "Bearer " + self._token
r = requests.get(f"https://{SERVERS[self._country]}/dispatch/app", params=payload,
headers={'Authorization': auth})
if r.status_code == 200:
rejson = r.json()
return f"wss://{rejson['domain']}:{rejson['port']}/api/ws"
try:
r = requests.get(f"https://{SERVERS[self._country]}/dispatch/app", params=payload,
headers={'Authorization': auth})
if r.status_code == 200:
rejson = r.json()
return f"wss://{rejson['domain']}:{rejson['port']}/api/ws"
except Exception as e:
_LOGGER.debug("Network error in get ws url")
return None

@property
Expand Down
2 changes: 1 addition & 1 deletion custom_components/climate_ewelink/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"domain": "climate_ewelink",
"name": "Midea A/C via eWeLink",
"version": "v0.2.8",
"version": "v0.2.9",
"config_flow": true,
"documentation": "https://github.com/georgezhao2010/climate_ewelink",
"issue_tracker": "https://github.com/georgezhao2010/climate_ewelink/issues",
Expand Down

0 comments on commit d97b7a8

Please sign in to comment.