Python API/Adapter for RESTful API?

Posted on
Wed Aug 04, 2010 10:22 am
jamus offline
Posts: 179
Joined: Dec 01, 2007

Python API/Adapter for RESTful API?

Has anyone developed a Python API or Adapter for the RESTful API?

I'm looking to write one as part of a project, but I don't want to invent the wheel.

Posted on
Wed Aug 04, 2010 10:57 am
jay (support) offline
Site Admin
User avatar
Posts: 18212
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Python API/Adapter for RESTful API?

Indigo 5.0 is going to have a native Plugin API that's Python-based. We really hope to have a beta out by the end of the year. I've never personally used the RESTful API from Python so I've not developed any reusable code for it.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Wed Aug 04, 2010 12:42 pm
jamus offline
Posts: 179
Joined: Dec 01, 2007

Re: Python API/Adapter for RESTful API?

I knew that the Python API was in the pipeline, but I wasn't sure what the timeline was or if the functionality I was looking for was going to be part of the API. It sounds like the Python API was going to be for plugins, which to me meant integrating technologies that aren't X10/Insteon.

I'm looking for an API that would be for Insteon/X10 devices and Indigo variables.

Posted on
Wed Aug 04, 2010 3:15 pm
jay (support) offline
Site Admin
User avatar
Posts: 18212
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Python API/Adapter for RESTful API?

Actually, plugins will not only have the ability to create devices, states, actions, etc, but will also have access to existing devices (regardless of technology), variables, action groups, etc. Pretty much any object that Indigo has the plugin will have access to as well. So, for instance, as someone on another thread was looking for, if you wanted to create a "stateful" group of devices, you could do that by watching device changes. You could create/update variables, or you could just create a custom "device" with its own states that represents the group.

At least that's the goal... ;)

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Wed Aug 04, 2010 3:29 pm
bschollnick2 offline
Posts: 1355
Joined: Oct 17, 2004
Location: Rochester, Ny

Re: Python API/Adapter for RESTful API?

jamus wrote:
Has anyone developed a Python API or Adapter for the RESTful API?

I'm looking to write one as part of a project, but I don't want to invent the wheel.


I have some code, but nothing that is too formalized... Are you looking for any specific abilities? What are you trying to manipulate?

------
My Plugins for Indigo (v4, v5, and v6) - http://bit.ly/U8XxPG

Security Script for v4 - http://bit.ly/QTgclf
for v5 - http://bit.ly/T6WBKu

Support Forum(s) - http://www.perceptiveautomation.com/userforum/viewforum.php?f=33

Posted on
Wed Aug 04, 2010 9:07 pm
jamus offline
Posts: 179
Joined: Dec 01, 2007

Re: Python API/Adapter for RESTful API?

bschollnick2 wrote:
jamus wrote:
Has anyone developed a Python API or Adapter for the RESTful API?

I'm looking to write one as part of a project, but I don't want to invent the wheel.


I have some code, but nothing that is too formalized... Are you looking for any specific abilities? What are you trying to manipulate?


I'd like to be able to:
1) Get the list of variables or devices.
2) Query variables
3) Set variables
4) Query state of devices
5) Turn on/off devices

Ideally, the api would look something like:
Code: Select all
# create indigo instance
indigo=IndigoServer("localhost")

#set variable
indigo.var["status"]="running"

#turn off the fan
indigo.dev["fan"].off()

#get state of the motion sensor
print indigo.dev["motion"].state()

#turn on all the lights
def light(n):
  return n.find("light")>=0

for device in filter(light,indigo.dev) :
  indigo.dev[device].on()

Posted on
Tue Aug 10, 2010 10:02 am
bschollnick2 offline
Posts: 1355
Joined: Oct 17, 2004
Location: Rochester, Ny

Re: Python API/Adapter for RESTful API?

jamus wrote:
bschollnick2 wrote:
jamus wrote:
Has anyone developed a Python API or Adapter for the RESTful API?

I'm looking to write one as part of a project, but I don't want to invent the wheel.


I have some code, but nothing that is too formalized... Are you looking for any specific abilities? What are you trying to manipulate?


I'd like to be able to:
1) Get the list of variables or devices.
2) Query variables
3) Set variables
4) Query state of devices
5) Turn on/off devices


Okay, I have a python module drafted for use for the Restful API. I am using Beautiful Soup for the majority of the XML translation... For some reason I just was unable to get ElementTree to work with the XML output by Indigo's server....

The API wrapper currently supports:

  • Fetch_devices_from_indigo
  • change_device_state_by_name
  • toggle_device_state_by_name
  • change_brightness_state_by_name
  • set_variable_by_name
  • get_variable_list
  • get_actiongroup_list
  • get_actiongroup_by_name
  • activate_actiongroup_by_name
  • get_variable_by_name
  • return_device_from_insteonaddr
  • return_device_from_x10_addr

I believe this would cover the majority of what you are trying to do.... I need to further document the wrapper, and to switch the device list to the XML style, instead of text... And decide on a proper manner to allow a versatile method to set the device settings, instead of relying on a hard coded method...

Anyone that is interested, please let me know, and I can send this to you, and/or decide on a method to allow group access to this... Any suggestions for improvements, or feature requests, are certainly welcome.

Mail Me at benjamin AT schollnick DOT net

------
My Plugins for Indigo (v4, v5, and v6) - http://bit.ly/U8XxPG

Security Script for v4 - http://bit.ly/QTgclf
for v5 - http://bit.ly/T6WBKu

Support Forum(s) - http://www.perceptiveautomation.com/userforum/viewforum.php?f=33

Posted on
Tue Aug 10, 2010 12:35 pm
jamus offline
Posts: 179
Joined: Dec 01, 2007

Re: Python API/Adapter for RESTful API?

I think github can help with the group access part.

What error were you getting with ElementTree? I wonder if it's choking on the missing <?xml …>.

Posted on
Tue Aug 10, 2010 2:14 pm
bschollnick2 offline
Posts: 1355
Joined: Oct 17, 2004
Location: Rochester, Ny

Re: Python API/Adapter for RESTful API?

jamus wrote:
I think github can help with the group access part.

What error were you getting with ElementTree? I wonder if it's choking on the missing <?xml …>.


Elementtree was returning empty lists for the items, and other components... I tried adding in the Doctype statement, but didn't seem to make any difference... The XML looked clean, so I'm at a loss at why Elementtree was seemingly ignoring it.

I've made the code available through Google Code, I already had an account setup from way back....

http://code.google.com/p/schollnick-programming/source/browse/#svn/trunk/Indigo%20Restful%20API

I will make it available through the blog site as well, once I am comfortable with it being stable...

------
My Plugins for Indigo (v4, v5, and v6) - http://bit.ly/U8XxPG

Security Script for v4 - http://bit.ly/QTgclf
for v5 - http://bit.ly/T6WBKu

Support Forum(s) - http://www.perceptiveautomation.com/userforum/viewforum.php?f=33

Posted on
Tue Aug 10, 2010 3:46 pm
jamus offline
Posts: 179
Joined: Dec 01, 2007

Re: Python API/Adapter for RESTful API?

bschollnick2 wrote:

Elementtree was returning empty lists for the items, and other components... I tried adding in the Doctype statement, but didn't seem to make any difference... The XML looked clean, so I'm at a loss at why Elementtree was seemingly ignoring it.


Do you by chance have a snippet of code and XML that exhibits this problem?

Posted on
Wed Aug 11, 2010 6:01 am
bschollnick2 offline
Posts: 1355
Joined: Oct 17, 2004
Location: Rochester, Ny

Re: Python API/Adapter for RESTful API?

jamus wrote:
bschollnick2 wrote:

Elementtree was returning empty lists for the items, and other components... I tried adding in the Doctype statement, but didn't seem to make any difference... The XML looked clean, so I'm at a loss at why Elementtree was seemingly ignoring it.


Do you by chance have a snippet of code and XML that exhibits this problem?


Any XML Request to the Indigo (v4.12) server from my code was experiencing the issue, but for example, a request to the weather channel's RSS/XML feed parsed fine...

Code: Select all
      def   fetch_web_page ( self, web_url):
         web_page = urllib.urlopen ( web_url ).readlines()
         return web_page

      def      process_xml_to_data ( self, xml_data ):
#         elem = XML(xml_data)
         xml_data = '<?xml version="1.0"?>\n' + xml_data
         elem = fromstring( xml_data ) #text) # same as XML(xml_data)
         print elem.items()
         return elem

xml_data = Indigo_server.fetch_web_page ( "http://127.0.0.1:8176/devices/EZIO8SA.xml")
xml_data = "".join (xml_data)
Indigo_server.process_xml_to_data ( xml_data )


I've tried a variety of different ways, but I'm no XML expert...

Hmmmph... I have used Elementtree before, when it wasn't part of the Python core, and I can't seemingly dig that code out... But I have found, something else.... For a coding project that someone else asked about earlier in the forums...

import xml.etree.ElementTree as ElementTree
import urllib2

def fetch_xml_page ( url_to_open ):
url = urllib2.Request (url=url_to_open, data=None)
url_pointer = urllib2.urlopen ( url )
#data_received = url_pointer.read()
tree = ElementTree.parse (url_pointer )
return tree

devices_xml = fetch_xml_page ('http://127.0.0.1:8176/insteonlinks/')
root = devices_xml.getroot()
for subnode in root.getchildren():
print 'http://127.0.0.1:8176%s' % subnode.get ('href')
child_data = fetch_xml_page ('http://127.0.0.1:8176%s' % subnode.get ('href'))
child_root = child_data.getroot ()
#print dir(child_root)
for x in child_root.getchildren():
for y in x.keys():
print "y - ",child_root.get(y)

Which works... I'll have to mess with that today or tomorrow and see if I can replace BeautifulSoup....

------
My Plugins for Indigo (v4, v5, and v6) - http://bit.ly/U8XxPG

Security Script for v4 - http://bit.ly/QTgclf
for v5 - http://bit.ly/T6WBKu

Support Forum(s) - http://www.perceptiveautomation.com/userforum/viewforum.php?f=33

Posted on
Wed Aug 11, 2010 12:26 pm
bschollnick2 offline
Posts: 1355
Joined: Oct 17, 2004
Location: Rochester, Ny

Re: Python API/Adapter for RESTful API?

Well, new update time...

* Removed dependency on BeautifulSoup
* Now using ElementTree for XML conversion
* Added get_device_list, and get_device_by_name
* depreciating fetch_devices_from_indigo, and related functions.
* Added set_thermostat_heating_mode, and set_thermostat_fan_mode

https://schollnick-programming.googleco ... unk/Indigo Restful API

If anyone would like to have SVN committ access, please let me know.

------
My Plugins for Indigo (v4, v5, and v6) - http://bit.ly/U8XxPG

Security Script for v4 - http://bit.ly/QTgclf
for v5 - http://bit.ly/T6WBKu

Support Forum(s) - http://www.perceptiveautomation.com/userforum/viewforum.php?f=33

Posted on
Wed Aug 11, 2010 2:33 pm
bschollnick2 offline
Posts: 1355
Joined: Oct 17, 2004
Location: Rochester, Ny

Re: Python API/Adapter for RESTful API?

Hrmph.... I'm being driven crazy here by a small issue with URLLIB2...

Any python assistance would be welcome...

I am attempting to add Basic Authentication to the Restful API wrapper, and it doesn't seem to be working... I've found plenty of examples, and they seem to be identical to my code...

Code: Select all
      def   set_server_authentication ( self, username, password):
      #
      #   Password handler - http://www.voidspace.org.uk/python/articles/urllib2.shtml
      #
         print "Setting auth"
         self.password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
         print self.server_address
         self.username = username
         self.password = password
         self.password_mgr.add_password(None, self.server_address, self.username, self.password)
         self.handler = urllib2.HTTPBasicAuthHandler( self.password_mgr)
         self.opener = build_opener( self.handler)
         try:
            self.opener ( self.server_address )
         except:
            print "failure"
         urllib2.install_opener( self.opener )
         #self.auth_handler = urllib2.HTTPBasicAuthHandler()
         #self.auth_handler.add_password ( realm=None, uri=self.server_address, user=username, passwd=password)
         #self.opener = urllib2.build_opener( self.auth_handler )
         #urllib2.install_opener ( self.opener )

         
      def   fetch_web_page ( self, web_url):
         """Wrapper around the urllib.urlopen call, that reads the web_url. 
         
         Intended for Internal use only.
         """
         web_page = urllib2.urlopen ( web_url )
         web_page = web_page.readlines()
         return web_page

      def      fetch_xml_page (self, url_to_open ):
         url       = urllib2.Request (url=url_to_open, data=None)
         url_pointer = urllib2.urlopen ( url )
         tree = ElementTree.parse (url_pointer )
         return tree


But if I have username/password toggled in Indigo, it dies with a 401 error... Any suggestions on what I have fouled up?

- Ben

------
My Plugins for Indigo (v4, v5, and v6) - http://bit.ly/U8XxPG

Security Script for v4 - http://bit.ly/QTgclf
for v5 - http://bit.ly/T6WBKu

Support Forum(s) - http://www.perceptiveautomation.com/userforum/viewforum.php?f=33

Posted on
Wed Aug 11, 2010 4:01 pm
jamus offline
Posts: 179
Joined: Dec 01, 2007

Re: Python API/Adapter for RESTful API?

bschollnick2 wrote:
Hrmph.... I'm being driven crazy here by a small issue with URLLIB2...

Any python assistance would be welcome...

I am attempting to add Basic Authentication to the Restful API wrapper, and it doesn't seem to be working... I've found plenty of examples, and they seem to be identical to my code...


The indigo web server expects Digest, not Basic, Authentication. See viewtopic.php?p=24826#p24826

Jamus

Posted on
Wed Aug 11, 2010 5:29 pm
bschollnick2 offline
Posts: 1355
Joined: Oct 17, 2004
Location: Rochester, Ny

Re: Python API/Adapter for RESTful API?

jamus wrote:
bschollnick2 wrote:
Hrmph.... I'm being driven crazy here by a small issue with URLLIB2...

Any python assistance would be welcome...

I am attempting to add Basic Authentication to the Restful API wrapper, and it doesn't seem to be working... I've found plenty of examples, and they seem to be identical to my code...


The indigo web server expects Digest, not Basic, Authentication. See viewtopic.php?p=24826#p24826

Jamus


Thank you Jamus! That solved it!

- Benjamin

------
My Plugins for Indigo (v4, v5, and v6) - http://bit.ly/U8XxPG

Security Script for v4 - http://bit.ly/QTgclf
for v5 - http://bit.ly/T6WBKu

Support Forum(s) - http://www.perceptiveautomation.com/userforum/viewforum.php?f=33

Who is online

Users browsing this forum: No registered users and 2 guests