Calling Kerberos RestFull API from RestFull Command

Hello,

I have started using Kerberos.io and I would like to start motiondetection when the alarm is armed.
Kerberos.io has a Restfull API but as a copy/paste ‘programmer’ I lack the skills to convert the python script example to a Restfull command.

The example api call is:

import requests
import json
import base64
url = "http://ip-of-pi/api/v1/condition/enabled"
username = "user"
password = "passw"
basicAuth = base64.b64encode('%s:%s' % (username, password))
headers = {"Authorization": "Basic " + basicAuth, "Content-Type": "application/json"}
data = '{"active": "true"}'
#Call REST API
response = requests.put(url, data=data, headers=headers)
print(response.text)

Could someone point me in the right direction how to convert this to a Hass Restfull command?
Thanks!

Maybe:

rest_command:
  kerberos_motion:
    url: "http://ip-of-pi/api/v1/condition/enabled"
    username: "user"
    password: "passw"
    headers:
      content_type: "application/json"
    method: put
    payload: '{"active": "{{ active }}"}'

Then call it like this:

- service: rest_command.kerberos_motion
  data:
    active: "true"

or

- service: rest_command.kerberos_motion
  data:
    active: "false"

Hi Phil, thank you for your response!

That looks simple but unfortunately nothing happens on the API, but also nothing is showing in the Hass logfile when testing from the developer tools. Changed some things which led to errors in the log so my changes are processed.

Just to see if the API is working I tested with curl and with the following command I had success:

curl -u user:password http://xxx.xxx.xxx.xxx/api/v1/condition/enabled --header Content-Type:application/json --request PUT --data {\"active\":\"true\"}

I looks quite the same! Any ideas for additional testing and/or logging?

Got it working! Had to move content_type out of headers and remove headers:

rest_command:
  kerberos_motion:
    url: "http://xxx.xxx.xxx.xxx/api/v1/condition/enabled"
    username: "user"
    password: "password"
    content_type: "application/json"
    method: put
    payload: '{"active": "{{ active }}"}'
    verify_ssl: "false"

Thank you Phil for pointing me the right direction!

D’oh! My mistake. I guess it should have been:

    headers:
      content-type: "application/json"

(Notice the dash instead of the underline.) And it would seem this:

    content_type: "application/json"

is just a shortcut for the above. I.e., either should work.

Glad you got it working! :smiley: