Newbie help - how best to use REST API to control 3rd party device

Hi All,

I’m really new to HA and looking for some assistance please.

I’m running HA v0.116.2 on a Pi 4 and at this stage, not doing anything overly complicated: looking to learn and expand.

I’ve got the basic build setup, the mobile app installed and configured on my phone, and a few Tuya switches setup and running successfully, but I think it’s fair to say I’m still not “Comfortable” with really and truly understanding or using the configuration.yaml file.

My next goal is for HA to control a 3rd party device I have that exposes REST API’s (GET / POST) to allow control, however I’m a little confused how to get HA firing these.

The API’s exposed have a POST Method on the device I’m controlling but don’t report their state in the POST responses (you just get a 200 response), and I don’t want to be polling these - just firing to prod the command. Likewise, they use a base64 encoded Basic Bearer token Authentication method in the header.

I read through the REST and RESTful integration docs and a few forum posts / Youtube vids, but I’m not sure these are right for me as I understand they’re reliant upon the same REST API reporting a on/off state (for a switch) in the response which it’ll poll frequently: I don’t want or need this. (Correct me if I’m wrong here)

As far as I can tell, I have an option of using a “command line switch” - but even reading the docs (here https://www.home-assistant.io/integrations/switch.command_line/) I’m sadly still a little lost.

My simple REST API would look like;

URI - http://{mydeviceIP}:{port}/api/v1/Objects/522?7=1
Header - Authorization : Basic Y2xnZmZgcMk6Q2wxZmZwcwRh

Which I can fire as an API request from Postman etc successfully - With postman telling me that the cURL (which I’m not familiar with using) for this being;

curl -L -X POST 'http://<deviceIP>:<port>/api/v1/Objects/522?7=1/api/v1/Objects/522?7=1' -H 'Authorization: Basic Y2xnZmZgcMk6Q2wxZmZwcwRh'

which also seems to tally with what I’ve googled about “Create curl API post request” too … however trying to fire this in a windows cmd or Powershell, doesn’t appear to work … (Confused here) - I just get;

Invoke-WebRequest : Cannot bind parameter 'Headers'. Cannot convert the "Authorization: Basic
Y2xnZmZgcMk6Q2wxZmZwcwRh" value of type "System.String" to type "System.Collections.IDictionary".
At line:1 char:93
+ ... 1/Objects/522?7=1' -H 'Authorization: Basic Y2xnZmZgcMk6Q2wxZmZwcwRh'
+                           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Invoke-WebRequest], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

I’ve attempted to add this into my configuration.yaml file as;

switch:
  - platform: command_line
    switches:
      kitchen_light:
        command_on: "curl -L -X POST 'http://<deviceIP>:<port>/api/v1/Objects/522?7=1' -H 'Authorization: Basic Y2xnZmZgcMk6Q2wxZmZwcwRh'"
        command_off: "curl -L -X POST '<deviceIP>:<port>/api/v1/Objects/522?7=2' -H 'Authorization: Basic Y2xnZmZgcMk6Q2wxZmZwcwRh'"

… and restarted HA … which then shows me the switch.kitchen_light entity and in services, I can hit “Call Service” with the “switch.turn_on” service and “switch.kitchen_light” entity, but nothing happens …

I’m now not sure whether I’m just not getting the curl right (which is highly possible given I’ve never used it before) or whether I’ve got something wrong in the yaml …

Further, I want this to appear in the Lovelace UI just like my TUYA switches do, with an on/off toggle switch (thus firing the API request each time it’s toggled) if possible … but adding it just appears to show it as a massive lightning bolt that when pressed, also does nothing …

Really sorry if this is me being a newbie and missing something which anyone with any experience will be tutting and rolling their eyes at, but I’m just starting out learning here so please forgive me :slight_smile:

Really appreciate any help you can offer.

Anyone? … I’ve spent ages attempting various things, and just can’t seem to get anything to work :frowning:

What exactly are you trying to control with rest? If it is just a light or switch there must be an easier way than rest. If you can wrap your head around Node Red , it does rest commands and responses way easier than ha in my opinion and there is an addon to install inside home assistant, and will make integrating with things that ha doesn’t way easier(kind of a middle man). But let me know what you are trying to control with a link to the product if possible and I will do what I can.

Thanks for your response :slight_smile:

I’ve got various applications for being able to just hit a REST API in mind, so to some degree my question is agnostic of the device I’m trying to control, however the main API’s I’m hitting to start with here are exposed from a locally hosted application (no cloud based service here), but don’t have any published documentation I can point you at; and short of an xAP interface, the application doesn’t support any other integration methods.

That said, the POST requests are super simple URI’s of query string’s / params, but they do require “Basic Auth”, so needs the “Authorization : Basic (base 64 encoded token)” header value adding to the POST request, so the request is just;

URI - http://{mydeviceIP}:{port}/api/v1/Objects/522?7=1
Header - Authorization : Basic Y2xnZmZgcMk6Q2wxZmZwcwRh

which will get a response of;

{
    "SwitchAPI": {
        "Code": "200",
        "Message": "OK"
    }
}

Interesting comments around Node Red if this makes life easier … I have installed Node Red as part of the basic “HA Setup” guide I followed, but have yet to get my head around it. Based on your comments, I’ll have a tinker when I’ve got some time tomorrow to see if it makes sense to me.

Any other suggestions in the meantime are much appreciated :slight_smile:

Ok , sounds like you have a way better grip on rest api and general coding than i do, lol. I thought you were just trying to control a light or switch on your local network. With that said I have been able to communicate with http requests both get and post with node red and would never have been able to do so otherwise (way to dumb) . So sorry to waste your time but please do check out node red because with the experience that it sounds like you have you will love it. I have learned what i know (enough to get in trouble) by watching videos on you tube. This guy really knows his stuff and is easy to follow for me , your mileage may vary.

If not just a simple youtube search for node red will get you started straight away.
Good Luck

Brill … OK … so managed to find 15 minutes tonight just to play, and I can see that in Node Red, I can create a simple sequence within a flow that is;

Inject > http request > msg

and using debug, can see that firing this does hit the API and returns the expected payload :slight_smile: … Super.

In what is now probably a stupid, question, how do I now further this so that I can use it in a way that I can actually use it? For example, how do I end up with some form of switch showing on the lovelace UI, that when pressed, fires the API ?

I’ve tried to change the above simple sequence to be;

button > http request > msg
or
switch > http request > msg

but in both cases, I then don’t appear to be able to “see” this anywhere to add it my home assistant home page. Should this show up somewhere in my entities or services?

Hi,

bit late to the party, but did you ever find out what the best way is to incorporate such API calls in your HA Dashboard?