Jump to content

Welcome to Smart Home Forum by FIBARO

Dear Guest,

 

as you can notice parts of Smart Home Forum by FIBARO is not available for you. You have to register in order to view all content and post in our community. Don't worry! Registration is a simple free process that requires minimal information for you to sign up. Become a part of of Smart Home Forum by FIBARO by creating an account.

 

As a member you can:

  •     Start new topics and reply to others
  •     Follow topics and users to get email updates
  •     Get your own profile page and make new friends
  •     Send personal messages
  •     ... and learn a lot about our system!

 

Regards,

Smart Home Forum by FIBARO Team


Tesla Quickapp


RoReAl

Recommended Posts

Hi!

Is there anyone here trying to figure out how to communicate with Tesla API?

I want to communicate with my car and adapt the charging current level based on my total power use, so I don’t pass a set power limit.

I’m willing to pay if someone has code of worth.

  • Like 1
Link to comment
Share on other sites

Hi @RoReAl, this sounds like a really useful use case.

 

if you want to write a quick app yourself, this tutorial (based on HC2) will help

Please login or register to see this link.

 

If you want to use an existing Tesla QA, do a search on the forum because someone already wrote one. Though I think the API has recently changed and it needs to be reworked.

 

good luck and perhaps post your progress on the other existing thread.

 

 

Link to comment
Share on other sites

  • Topic Author
  • Yes I have seen that code and i whant something like that for HC3 and Quickapp. Hopefully someone can refer to a similar LUA project.

    Link to comment
    Share on other sites

    • 11 months later...

    I've started to work on this subject, based on the new version of the API that's described here:

    Please login or register to see this link.

    But the journey will be quite long.

    So far, I've managed to generate all the material to build the first authentification step, which is not as simple as they can expect. the generated url works fine in postman (returning me the html page with the hidden section. But the exact same url sent from my HC3 will return a 403 error, meaning that tesla understood my request but considered it as forbidden. There is absolutely no message returned with that error, making it hard to understand the root cause.

    However, I highly suspect that this is because the HC3 HTTPclient doesn't manage, at least by default, cookies. And from what I can see in postman, a bunch of cookies are returned and will be useful for the next steps.

     

    I've tried to check the virtual device code that was available on this forum, but found nothing useful: no url in it that could provide me hints of syntax, no mention of cookies. I'll continue to work on this, the next step being to find out what really the HC3 is sending to tesla.com and comparing it with what postman sends and then trying to find out a way to instruct HC3 to send the same data as postman. 

     

    I think this topic should be collectively managed with each participant providing his knowledge to finally succeed in connecting to Tesla.

     

    The first step I've managed to build is not as simple as it may look: indeed, I had to find out how to hash in lua the challenge-code (random string), using the correct method (sha2 with 256 bits is the answer, no sha3) and then how to encode the answer in url base 64. TO ensure this, I've found a library that had all the needed functions. The only trick being that to encode in urlbase64, it needs to be converted from string to binary and then from binary to urlbase64.  The name of this library is sha2lib_v0.1.fqa (can be found on the market place). It contains two tabs and the sha2 has to be imported in the Tesla QA in order to be callable by your app.

     

    Then, the next tricky thing is to determine how to pass variables to the tesla server and how to manage spaces (%20?) or if quotes are needed. And last, how to force http client to use https. The result of all those steps looks like this:

    Please login or register to see this link.

     

    code_challenge is the hash of a ramdom string and state is a ramdom string. 

     

    For information, here is the full code I've used to generate the url and to send it, without success so far, to the Tesla server:

        local sha256 = sha.sha256
        local hex2bin=sha.hex2bin
        local bin2base64=sha.bin2base64
        --génère une chaine aléatoire de 86 caractères de long comme code_verifier qui sera utilisée dans la procédure d'identification
        code_verifier=""
        for i=1,86 do -- boucle pour arriver à une chaine de caractères de 86 de long
            a=math.random(48,122)
            while (a<65 and a>57or (a<97 and a>90do
                a=math.random(48,122)
            end
            -- à ce stade, a ne contient que des codes ascii correspondant à des lettres ou des chiffres
            code_verifier=code_verifier .. string.char(a)
        end
        state=""
        for i=1,10 do -- boucle pour arriver à une chaine de caractères de 10 de long
            a=math.random(48,122)
            while (a<65 and a>57or (a<97 and a>90do
                a=math.random(48,122)
            end
            -- à ce stade, a ne contient que des codes ascii correspondant à des lettres ou des chiffres
            state=state .. string.char(a)
        end
        self:debug("state: ",state) -- state contient à ce stade une chaine hexadecimale aléatoire de 10 caractères
        --string.byte("Z")
        self:debug("code_verifier: ",code_verifier) -- code_verifier contient à ce stade une chaine hexadecimale aléatoire de 86 caractères
        -- genère le hash sha256 encodé en urlbase64 de code_verifier
        code_challenge=bin2base64(hex2bin(sha256(code_verifier)))
        self:debug("code_challenge: ",code_challenge) -- code_challenge contient le hash sha256 encodé en urlbase64 de code_verifier
    --------------------------------------------------------------------
        MyURL="https://auth.tesla.com/oauth2/v3/authorize?client_id=ownerapi&code_challenge=" .. code_challenge .."&code_challenge_method=S256&redirect_uri=https://auth.tesla.com/void/callback&response_type=code&scope=openid%20email%20offline_access&state=" ..state.."&login_hint=" .. self.Login
        self:debug("MyURL: ", MyURL)
        self.http = net.HTTPClient({timeout=3000, sync= true})
        self.http:request(MyURL, 
            {
            options = { method = "GET"
                        headers = {['User-Agent']="PostmanRuntime/7.30.0", Accept="*/*" } },
            success = function(response) 
            print("Status", response.status)
            print("Data", response.data)
          end,
          error = function(message)
            self:error(message)
          end
        })
     
    You can note the snyc=true option in the call of the HTTPclient that is meant to provide synchronous calls instead of the native asynchronous mode. As the steps to connect to Tesla are obviously sequential, I through this was easier to proceed this way. I wanted to highlight this as I've been searching for such an option during weeks and never found it on fibaro forums. And I find it much easier than using the strategy of the function having another function running within its arguments.
     
    So, if there is someone that knows how to manage cookies receiving from the server with an httpclient call (= creating a cookie storage and then accessing to it), that would probably be a big step forward to establish the communication between an HC3 and a Tesla.
     
    Also, if someone knows a simple way to parse a returned HTML message from a server, this would help a lot for the next step that is getting the returned parameters in the html hidden section.
    • Thanks 1
    Link to comment
    Share on other sites

    • 2 months later...

    Join the conversation

    You can post now and register later. If you have an account, sign in now to post with your account.

    Guest
    Reply to this topic...

    ×   Pasted as rich text.   Paste as plain text instead

      Only 75 emoji are allowed.

    ×   Your link has been automatically embedded.   Display as a link instead

    ×   Your previous content has been restored.   Clear editor

    ×   You cannot paste images directly. Upload or insert images from URL.

    ×
    ×
    • Create New...