Jump to content

Connecting to Fibaro - api python


Recommended Posts

Hi all

How can I connect to Fibaro account to GET device info in python? 

I am having trouble with the basic stuff like authentication and GET call 

please help!

Link to post
Share on other sites

I think you banned from google?

but ok here is link for you http://letmegooglethat.com/?q=site%3Aforum.fibaro.com+python

 

and as bonus from me

 

#!/usr/bin/python
# -*- coding: utf-8 -*-.
# by tender
#

import io
import sys
import requests
import json
import configparser

# ------------------------------------------------------
def getConfigValue(name):
    config = configparser.ConfigParser()
    config.readfp(open(r'hc.ini'))
    try:
       return config.get("HC3", name)
    except:
       return None

hc2IP = getConfigValue("ipAddress")
hc2auth = (getConfigValue("login"), getConfigValue("password"))

baseUrl = 'http://' + hc2IP + '/api/'

def do_request(path, method="GET", params={}, headers={}):
        if method == "GET":
            response = requests.get(path, headers=headers, auth=hc2auth)
        elif method == "POST":
            headers["Content-Type"] = "application/json"
            response = requests.post(
                path,
                data=json.dumps(params),
                headers=headers,
                 auth=hc2auth)
        elif method == "PUT":
            headers["Content-Type"] = "application/json"
            response = requests.put(
                path,
                data=json.dumps(params),
                headers=headers,
                 auth=hc2auth)
        elif method == "DELETE":
            response = requests.delete(
                path,
                data=json.dumps(params),
                headers=headers,
                auth=hc2auth)
        return response

def backup():
    r = do_request(baseUrl + "scenes")
    if r.status_code == 200:
       data = r.json()
       if data != None:
          for scenes in data:
              id = scenes["id"]
              print("backuping #" + str(id) + " scene.")
              content = scenes["content"]
              data = json.loads(content)

              fname = "scene_" + str(id) + ".conditions" +  ".lua"
              with io.open(fname, "w", encoding="utf-8") as f:
                   f.write(data["conditions"])
                   f.close()

              fname = "scene_" + str(id) + ".actions" +  ".lua"
              with io.open(fname, "w", encoding="utf-8") as f:
                   f.write(data["actions"])
                   f.close()

    else:
        print("HTTP error: " + str(r.status_code))

    r = do_request(baseUrl + "devices?interface=quickApp")
    if r.status_code == 200:
       data = r.json()
       if data != None:
          for qa in data:
              id = qa["id"]
              print("backuping " + str(id) + " QA.")

              fname = "qa_" + str(id) + ".code" +  ".lua"
              with io.open(fname, "w", encoding="utf-8") as f:
                   f.write(qa["properties"]["mainFunction"])
                   f.close()

              fname = "qa_" + str(id) + ".layout" +  ".json"
              data = qa["properties"]["viewLayout"]
              with io.open(fname, "w", encoding="utf-8") as f:
                   json.dump(data, f)
                   f.close()
    else:
        print("HTTP error: " + str(r.status_code))


backup()

 

  • Like 1
  • Thanks 1
Link to post
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...