Giter Site home page Giter Site logo

Comments (24)

syssi avatar syssi commented on August 28, 2024

It looks like the chinese API is a bit different. Do you know any details? Are you able to provide some traffic captures?

from home-assistant-niu-component.

goxofy avatar goxofy commented on August 28, 2024

@syssi
Thank you for reply!
What do you mean the traffic captures? Should I capture the app packages ?

from home-assistant-niu-component.

syssi avatar syssi commented on August 28, 2024

In my opinion the easiest way to capture some traffic between the android app and the miu cloud server is this one: https://github.com/cascha42/niu-info#how-to-get-your-token

In your case we want to know the payload of every request/response (the entire mitmproxy traffic capture).

from home-assistant-niu-component.

goxofy avatar goxofy commented on August 28, 2024

@syssi
Hi syssi,
I have captured the traffic packages, how could I send it to you ?
Thanks.

from home-assistant-niu-component.

syssi avatar syssi commented on August 28, 2024

Please send it to [email protected]. I will anonymize the important parts and try to add the details here.

from home-assistant-niu-component.

goxofy avatar goxofy commented on August 28, 2024

@syssi
Hi, I have sent it to you, please check.
Thank you

from home-assistant-niu-component.

syssi avatar syssi commented on August 28, 2024

This component requests the following API endpoints at the moment:

ACCOUNT_BASE_URL = "https://account-fk.niu.com"
LOGIN_URI = "/appv2/login"
API_BASE_URL = "https://app-api-fk.niu.com"
MOTOR_BATTERY_API_URI = "/v3/motor_data/battery_info"
MOTOR_INDEX_API_URI = "/v3/motor_data/index_info"
MOTOINFO_LIST_API_URI = "/motoinfo/list"
MOTOINFO_ALL_API_URI = "/motoinfo/overallTally"
TRACK_LIST_API_URI = "/v5/track/list/v2"

The capture of @goxofy uses the following endponts:

ACCOUNT_BASE_URL = "https://account.niu.com"
LOGIN_URI = "/v3/api/oauth2/token"
API_BASE_URL = "https://app-api.niu.com"
MOTOR_BATTERY_API_URI = "/v3/motor_data/battery_info"
MOTOR_INDEX_API_URI = "/v5/scooter/motor_data/index_info"
MOTOINFO_LIST_API_URI = "" # missing
MOTOINFO_ALL_API_URI = "/motoinfo/overallTally"
TRACK_LIST_API_URI = "/v5/track/list/v2"

It looks like the login endpoint has changed slightly.

from home-assistant-niu-component.

syssi avatar syssi commented on August 28, 2024

@goxofy Could you update these consts to the new values and give it another try:

ACCOUNT_BASE_URL = "https://account.niu.com"
LOGIN_URI = "/v3/api/oauth2/token"
API_BASE_URL = "https://app-api.niu.com"

from home-assistant-niu-component.

syssi avatar syssi commented on August 28, 2024

Oh. It looks like the request & response of the oauth2 token retrieval has changed a bit.

from home-assistant-niu-component.

goxofy avatar goxofy commented on August 28, 2024

@syssi
Hi Syssi,
I have update these consts and the error log seems to nothing changed.

2021-11-29 05:18:42 ERROR (MainThread) [homeassistant.components.sensor] Error while setting up niu platform for sensor
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 249, in _async_setup_platform
await asyncio.shield(task)
File "/usr/local/lib/python3.9/concurrent/futures/thread.py", line 52, in run
result = self.fn(*self.args, **self.kwargs)
File "/config/custom_components/niu/sensor.py", line 374, in setup_platform
token = get_token(username, password, country)
File "/config/custom_components/niu/sensor.py", line 287, in get_token
return data["data"]["token"]
KeyError: 'token'

from home-assistant-niu-component.

syssi avatar syssi commented on August 28, 2024

This sections the to be updated to be compatible to the new login endpoint: https://github.com/marcelwestrahome/home-assistant-niu-component/blob/master/custom_components/niu/sensor.py#L278-L287

This is the POST payload of the current implementation:

    data = {"account": username, "countryCode": cc, "password": password}

Your capture contains

password=PASSWORD&grant_type=password&scope=base&app_id=niu_ktdrr960&account=USER%40EXAMPLE.COM

Please adapt the payload to:

    data = {"account": username, "password": password, "grant_type": "password", "scope": "base", "app_id": "niu_ktdrr960"}

The response is also a bit different:

{
    "data": {
        "token": {
            "access_token": "eyJhb...HRkA",
            "refresh_token": "OGJJ...NMM3",
            "refresh_token_expires_in": 1640693648,
            "token_expires_in": 1638706448
        },
        "user": {[...]}
    },
    "desc": "ok",
    "status": 0
}

We need the access_token:

    data = json.loads(r.content.decode())
    return data["data"]["token"]["access_token"]

from home-assistant-niu-component.

goxofy avatar goxofy commented on August 28, 2024

@syssi
Hi Syssi,
I tried to send a request with httpie, and I got the right response. I found that the "pasword" const I must use encrypt one, not the plantext

from home-assistant-niu-component.

goxofy avatar goxofy commented on August 28, 2024

I have changed the value "password" with the encrypt one that I captured in my configuration.yaml, now it works no error.

But in home assistant some entities is not right status.

from home-assistant-niu-component.

syssi avatar syssi commented on August 28, 2024

A list of the bogus entities would be helpful to have a closer look.

from home-assistant-niu-component.

syssi avatar syssi commented on August 28, 2024

Could you tell me the version number of your Niu android app?

from home-assistant-niu-component.

goxofy avatar goxofy commented on August 28, 2024

@syssi
Hi Syssi,
My NIU android app version number is V4.6.48, this is not a Google Play version

from home-assistant-niu-component.

goxofy avatar goxofy commented on August 28, 2024

@syssi
PS: I found the MOTOINFO_LIST_API_URI seems like is "/v5/scooter/list"
Here is the response:

{
    "data": {
        "items": [
            {
                "carframe_id": "19XXXXXXXXXXXXX",
                "index_scooter_img": "",
                "index_scooter_img_dark": "",
                "isDefault": true,
                "isMaster": true,
                "product_type": "SMART_GOVA",
                "scooter_name": "F2",
                "sku_name": "GOVA F2 Citi 50  Gray",
                "sn_id": "F2TXXXXXXXXXXXXXXX"
            }
        ]
    },
    "desc": "成功",
    "status": 0,
    "trace": "成功"
}

PS: this uri use GET method

from home-assistant-niu-component.

syssi avatar syssi commented on August 28, 2024

I've poked around a bit. Could you generate the MD5 sum of your password? The result should be the encrypted password:

$ echo -n YOURPASSWD | md5sum 
fcefdbe34a5325b0805ceabe15584625  -

from home-assistant-niu-component.

syssi avatar syssi commented on August 28, 2024

@goxofy I've prepared a PR. Please give it a try! You have to use your plaintext password (instead of the hash) at your HA configuration: #32

from home-assistant-niu-component.

syssi avatar syssi commented on August 28, 2024

There is another thing: In the past the token didn't expire. It's possible the new access_token needs to be refreshed from time to time. If your Niu sensors gets unavailable some day a restart of Home Assistant will solve the issue (temporary) because a new login is performed and the new access_token will be retrieved. If this is the case we need to improve the component to re-authenticate if the token expires.

from home-assistant-niu-component.

goxofy avatar goxofy commented on August 28, 2024

Hi @syssi

thanks a lot!

I will try it today.

from home-assistant-niu-component.

goxofy avatar goxofy commented on August 28, 2024

Hi @syssi ,
I have tried this PR and it works well, thanks a lot.

Is it possible to refresh the token or execute a new login without restart the HA? I saw that the token expires time is about 18 days, is it right?

from home-assistant-niu-component.

m805500091 avatar m805500091 commented on August 28, 2024

感谢,我也解决了

from home-assistant-niu-component.

goxofy avatar goxofy commented on August 28, 2024

@syssi Hi Syssi, what's the meaning of leftTime(value=0) and gradeBattery(value=Null)?

from home-assistant-niu-component.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.