iKaew / hass-lifesmart-addon

LifeSmart Integration for Home Assistant
6 stars 1 forks source link

Add username + pwd method #13

Closed MapleEve closed 6 months ago

MapleEve commented 7 months ago
def lifesmart_Login(uid,pwd,appkey):
    url = "https://api.ilifesmart.com/app/auth.login"
    login_data = {
      "uid": uid,
      "pwd": pwd,
      "appkey": appkey
    }
    header = {'Content-Type': 'application/json'}
    req = urllib.request.Request(url=url, data=json.dumps(login_data).encode('utf-8'),headers=header, method='POST')
    response = json.loads(urllib.request.urlopen(req).read().decode('utf-8'))
    return response

def lifesmart_doAuth(userid,token,appkey):
    url = "https://api.ilifesmart.com/app/auth.do_auth"
    auth_data = {
      "userid": userid,
      "token": token,
      "appkey": appkey,
      "rgn": "cn"
    }
    header = {'Content-Type': 'application/json'}
    req = urllib.request.Request(url=url, data=json.dumps(auth_data).encode('utf-8'),headers=header, method='POST')
    response = json.loads(urllib.request.urlopen(req).read().decode('utf-8'))
    return response
MapleEve commented 7 months ago

This is the best way to get userid and user token

iKaew commented 6 months ago

Thank you. I'll try to put that into the addon.

MapleEve commented 6 months ago

Thank you. I'll try to put that into the addon.

Yes, this will keep user token alive for a quite long time, rather than just 1 year

res_login = lifesmart_Login(param['username'],param['passwd'],param['appkey'])
    if res_login['code'] == "error":
      _LOGGER.error("login fail: %s",str(res_login['message']))
    param['token'] = res_login['token']
    param['userid'] = res_login['userid']
    res_doauth = lifesmart_doAuth(param['userid'],param['token'],param['appkey'])
    if res_doauth['code'] == "error":
      _LOGGER.error("login fail: %s",str(res_doauth['message']))
    param['usertoken'] = res_doauth['usertoken']
MapleEve commented 6 months ago

besides, the rgn can be a seletive input in HACS config, rather than a domain input.