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


HC3 QuickApps coding - tips and tricks


jgab

Recommended Posts

I am trying to get the TQAE working with a QA I have on my Z-Box Hub. The TQAE would appear to be failing because the headers are blank in the HTTP request. I get a "400" error response. This works on the hub just fine. When I take the URL string for the POST request and drop it into Postman, it also works fine BUT if I turn off all the Postman defined headers I get a "400" error response. I added a Content-Length of zero and turned on the Host header variable within the Postman request and I get back the response I am expecting. Is the HTTP package on the hub adding headers to my request and the TQAE is not?

 

I have been wrestling with this for a while. I did add a host parameter to the request and now I get a "404" error response so I am on the right track (I hope).

 

Thanks in advance.

 

Peter

Link to comment
Share on other sites

  • Topic Author
  • 1 hour ago, PeterV959 said:

    I am trying to get the TQAE working with a QA I have on my Z-Box Hub. The TQAE would appear to be failing because the headers are blank in the HTTP request. I get a "400" error response. This works on the hub just fine. When I take the URL string for the POST request and drop it into Postman, it also works fine BUT if I turn off all the Postman defined headers I get a "400" error response. I added a Content-Length of zero and turned on the Host header variable within the Postman request and I get back the response I am expecting. Is the HTTP package on the hub adding headers to my request and the TQAE is not?

     

    I have been wrestling with this for a while. I did add a host parameter to the request and now I get a "404" error response so I am on the right track (I hope).

     

    Thanks in advance.

     

    Peter

    Is it a POST with empty data?

    Is there any combination of added headers in TQAE that is successful?

    At the moment, if there is no data to POST/PUT, TQA will set payload to "[]" and Content-Length:2

    ...could that be messing with your request and cause the 400/404?

    ...it's line 192 in TQAE.lua

    ...otherwise, TQAE net.HTTPClient will just forward all headers you add to the options parameter.

     

    Can you post what postman sends for a successful requests?

    Please login or register to see this code.

     

     

    Edited by jgab
    Link to comment
    Share on other sites

    I am trying to implement the Google Nest Thermostat QA by Guillaume Waignier. The steps to get the Client ID, Client Secret and Authorization code are tricky to navigate. But the funny thing is it (finally) started to work on my hub directly. I have struggled in vain to get it to work in the emulator. I think I narrowed it down to requiring a Content-Length and Host header entries. I have gotten the Host and Content-Length into the Options table for the HTTP request. I used both Host = '

    Please login or register to see this link.

    ' OR Host = ''  and a Content-Length = 2. That at least changes the response from Google from 400 to 404. The Client ID, Client Secret, and Authorization Code are all copied and pasted into the emulator lua files from the variables so that hasn't been the issue. I even self:debug(url) the string for the http call and copied it into Postman. Attached is the screen shot showing the post, the header options (only two checked but both must be present), and the result of the post.

     

    This is my first attempt to use a QA and kind of the hurdle to jump over to migrate away from SmartThings. Honestly, I found a few problems with Guillaume's code because he's in France and uses °C and I want °F. That has been changed and submitted for his review but I have learned a lot about QAs in general.

     

    Thanks for your advice.

     

    Peter

    Please login or register to see this attachment.

    Link to comment
    Share on other sites

  • Topic Author
  • host header is set automatically by the code based on the provided url. If you set it to something else you get an 404...

    content-length is automatically calculated.

    You postman example has a payload, why do you refer to setting content-length to 2?

    Do you also have a POST with empty payload?

    If you want an empty payload set data="" 

    I set data="[]" and content-length = 2 if data==nil, which is really a bug in a POST/PUT where data should be set to ""

     

    It's an HTTPS call, have you tested to set option checkCertificate = false ?

    Link to comment
    Share on other sites

    Thank you @jgab, I finally found the issue. I'm not savvy enough to understand the full underlying details but what needed to be done was to add two things to the headers in the OAuth Access Token request and the OAuth Refresh Token request.

    Please login or register to see this code.

    I also took your advice and changed the nil to '' for the ['Data'] field. Suddenly I had a working QA in the TQAE. Strangely, the code worked fine in the Z-Box Hub before. Elsewhere in the code there are API requests for commands to the Nest Thermostat device. I'm not sure yet whether they work. I need a "UI" to change a heating/cooling setpoint or thermostat mode (or I need to figure out how to use the Z-Box as the UI for the emulator).

     

    Thank you so much for the above help. I certainly didn't mean to imply there was a bug anywhere at all in the TQAE code. 🤭 That is truly work of love on your part. Having gotten this to work I can really begin to learn how to code a QA.

     

    Thank you again!

     

    Peter

    Edited by PeterV959
    • Like 1
    Link to comment
    Share on other sites

    I have two questions:

    1) I have been curious about the structure pane in the QA editor. I haven't found anything  that allows a display in that panel. What is it for?

     

    2) Is the lua compiler/interpreter/whatever it is using only 32 bit math? Seems rather antiquated. Or does it depend on the system? Even my IBM PC with its 8-bit 8088 processor could handle 64 bit math when it had to.

    Link to comment
    Share on other sites

  • Topic Author
  • 1) I have also difficulties in seeing the value of it - maybe someone else knows?

    2) HCL uses 32 bit ints - probably to try to save some space - but in general a bad decision.

    HC3 uses 64 bits and math.maxinteger = 9223372036854775807

     

    another example

     

    Edited by jgab
    • Like 1
    Link to comment
    Share on other sites

    Thank you, @jgab that's what I thought. The challenge is that I wanted to develop a QA that needs OAuth2 with PKCE. The PKCE relies on SHA256 which needs a minimum of 53 bits (at least the code I have seen posted by @tinman asserts out on my hub). I think I have a work around for this but I'm not thrilled by it. Still, for my simple needs and for the learning experience my small project will provide I will go down the path I have and decide what to do by the time I get ready to implement it in a production role.

     

    Peter

    Link to comment
    Share on other sites

  • Topic Author
  • So, continue the QuickAppChild journey. This library is probably the easiest to create and keep a pre-defined set of QuickAppChild devices in sync.

    Library:
     

    Please login or register to see this code.

     

    ...and then use it like this

    Please login or register to see this code.

     

    It's not so difficult to modify it for dynamically created children - the 'myChildrenTable' just needs to be modified with the added devices and persisted in a quickAppVariable.

    Edited by jgab
    • Like 3
    • Thanks 1
    Link to comment
    Share on other sites

  • Topic Author
  • 3 hours ago, tinman said:

    @jgab please edit to e instead of é

    Sorry, was the international heritage of the code...

    Link to comment
    Share on other sites

    I think in the following code:

    21 hours ago, jgab said:

    Please login or register to see this code.

    You mean to have

    Please login or register to see this code.

     

    The "c" is missing?

    Link to comment
    Share on other sites

  • Topic Author
  • 2 hours ago, PeterV959 said:

    I think in the following code:

    You mean to have

    Please login or register to see this code.

     

    The "c" is missing?

    I'm actually subclassing QuickAppChild to a new class QwikAppChild (then we can argue if we should spell it qwik or qwick...)

    Then the children we define we subclass from QwikAppChild (like the Class_Temp in the example)

    The reason is that our QwikAppChild class does a lot of common housekeeping, like keeping track of uid and class name.

    Our Class_Temp could add additional methods if we needed that from our temp child objects.

    Edited by jgab
    Link to comment
    Share on other sites

    I missed the later class definition, my bad. I wondered if you did that but I couldn't find a reference early on so presumed it might be a typo. Probably shouldn't jump the gun.

     

    Peter

    Link to comment
    Share on other sites

    • 2 weeks later...

    @jgab

    do you  know how to replace bit.bxor in lua 5.3 ?

    this is for two value: (3 ~ 5)  -- bitwise xor 

    but for table with more values as bellow?

    Please login or register to see this code.

     

    Link to comment
    Share on other sites

  • Topic Author
  • In QAs you have bit32.bxor that works like bit.bxor

    • Thanks 1
    Link to comment
    Share on other sites

    • 3 weeks later...

    I have a device discovery (for some lifx lights) which uses UDP broadcast to get responses from all lights and this all works OK. Is there any way on the HC3 to get the IP of the UDP received datagram so I can store the light IP as part of a device table? It seems that HC3 does not have the lua UDP receivefrom() function to give the IP.

    Link to comment
    Share on other sites

  • Topic Author
  • On 3/10/2023 at 12:33 AM, James R said:

    I have a device discovery (for some lifx lights) which uses UDP broadcast to get responses from all lights and this all works OK. Is there any way on the HC3 to get the IP of the UDP received datagram so I can store the light IP as part of a device table? It seems that HC3 does not have the lua UDP receivefrom() function to give the IP.

    No, it's a bit limited. The lights are not revealing their IP in the returned data ?

    Link to comment
    Share on other sites

  • Topic Author
  • 29 minutes ago, jgab said:

    No, it's a bit limited. The lights are not revealing their IP in the returned data ?

    actually, in receive success, add an extra argument

    Please login or register to see this code.

    and have a look at from...

    Edited by jgab
    • Like 1
    Link to comment
    Share on other sites

    Fantastic, thank you - that works. No the devices don't report their IP just their mac address

     

    This is all quite new to me (Fibaro, Lua..). Next step is to try to understand Child devices 😕 @jgabthanks for fibaro.extra that is a great help.

    Link to comment
    Share on other sites

    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...