Sample Python code for API access

Help for Z-Command related issues only
thearn
Posts: 2
Joined: Fri Nov 20, 2015 6:09 am

Sample Python code for API access

Postby thearn » Fri Nov 27, 2015 6:40 pm

Just thought I'd share some sample Python code for communicating with the Z-Command API. The only 3rd-party library required is the excellent `requests` library (`pip install requests`).

Just create an instance of the `ZWaveController` class. It will handle registration and refreshing of the user token as needed (by default, every 8 hours). I have been running this with a flask app for the last week or so without issues.

You can also get it from the following Gist: https://gist.github.com/thearn/9ee2f493720d0025f536

Code: Select all


import requests
import json
import sys
import time
timer = {}

hub_url = "192.168.1.2"
hours_to_refresh_token = 8

class ZWaveController(object):
    timer = {}
    token = ""
    token_time = time.time()
    seconds_to_refresh_token = 60*60*hours_to_refresh_token

    def _make_header(self):
        if self.token == "" or time.time() - self.token_time > self.seconds_to_refresh_token:
            self.token = self._get_new_token()
            token_time = time.time()
        headers = {
            'Authorization': "Token " + self.token
        }

        return headers

    def _get_new_token(self):
        url = "http://%s/api/tokenauth" % hub_url
        data = {"username":"admin","password":"admin"}
        response = requests.post(url, json=data)
        token = response.json()['responseObject']

        return token

    def get_devices(self):
        """
        Returns dictionary of all device data
        """
        url = "http://%s/api/devices" % hub_url
        headers = self._make_header()
        response = requests.get(url, headers=headers,  allow_redirects=False)

        return response.json()['responseObject']

    def get_device(self, node_id):
        """
        Returns status data of a single device
        """
        url = "http://%s/api/device/%s" % (hub_url, node_id)
        headers = self._make_header()
        response = requests.get(url, headers=headers,  allow_redirects=False)
        return response.json()['responseObject']

    def get_level(self, node_id):
        """
        Returns the level of a single device
        """
        obj = self.get_device(node_id)
        return obj['level']


    def set_level(self, node_id, level):
        """
        Sets the level of a single device.
        Rate-limited to one query per second.
        """
        if node_id in self.timer.keys():
            gap = time.time() - self.timer[node_id]
            if gap < 1:
                time.sleep(1 - gap)
        url = "http://%s/api/setdevicelevel/%s/%f" % (hub_url, node_id, level)
        headers = self._make_header()
        response = requests.post(url, headers=headers,  allow_redirects=False)


        self.timer[node_id] = time.time()
        return response.json()['responseObject']['level']

if __name__ == "__main__":
    import time
    z = ZWaveController()
    print z.get_devices()




admin
Site Admin
Posts: 50
Joined: Sun Dec 14, 2014 2:05 pm
Location: Washington
Contact:

Re: Sample Python code for API access

Postby admin » Fri Nov 27, 2015 8:35 pm

Great work thearn!
Thanks for sharing.


Return to “Z-Command help”

Who is online

Users browsing this forum: No registered users and 6 guests

cron