Help with first plugin please

Do you have questions about writing plugins or scripts in Python? Meet the coders here.
Post Reply
bandook
Posts: 8
Joined: Thu Oct 25, 2012 1:19 pm

Help with first plugin please

Post by bandook » Sat Dec 07, 2013 11:34 pm

Hi, all. Finally took the plunge and am trying to write a simple plugin to control some devices on my Homeseer server. Been learning slowly, but I 've hit a wall I've been stuck on for a few hours now. Without too much bla bla, take a look and tell me my dumb mistake please :)

Code: Select all

import eg
import urllib2

eg.RegisterPlugin()


class MyNewPlugin(eg.PluginBase):
    def __init__(self):
        self.AddAction(HallLightOn)
        self.AddAction(HallLightOff)
		
class HallLightOn(eg.ActionBase):
    def __call__(self):
	    response = urllib2.urlopen('http://192.168.1.102:86/HomeSeer_REST_API.aspx?function=devicestatusbyid&param1=2036&param2=on')
            if (response.read == 'false'): print "light on"	
What I'm trying to do is get the status of a device and return true or false. Then take the true/false and carry on from here. Ok, I can get EG to print "true" or "false" in the log when all I have is

Code: Select all

print response.read()

So I figured I could the make and if/else statement to tell it to turn the lights on or off. I'm just trying to get it to print for now, so I at least know its working. But it's not working as I expect it too. Sure I'm missing something obvious.

Basically my plan is to use a macro to turn a light off or on depending on the play state of xbmc. So I hit play button, it runs the macro and turns the light off if its on, and when I hit stop it turns light on if its off. I am able to control the light now with my script, I just cant figure out what I'm doing wrong using "if".

thanks for any insight into my blunder!
chris

User avatar
Pako
Plugin Developer
Posts: 2293
Joined: Sat Nov 11, 2006 1:31 pm
Location: Czech Republic
Contact:

Re: Help with first plugin please

Post by Pako » Sun Dec 08, 2013 6:38 am

I do not have the possibility to verify it practically.
But it appears that you are using the read method the wrong way.
There should be:

Code: Select all

            if (response.read() == 'false'): print "light on"
instead of:

Code: Select all

            if (response.read == 'false'): print "light on"Z
But good writing in Python should actually look like this:

Code: Select all

            if response.read() == 'false':
                print "light on"
Pako

bandook
Posts: 8
Joined: Thu Oct 25, 2012 1:19 pm

Re: Help with first plugin please

Post by bandook » Sun Dec 08, 2013 3:48 pm

Pako, I had a feeling you would come through for me :) Thank you! Once I changed false to False, it worked perfect. And I know what your saying about the indentations. I was trying to write it like you did, but was having indent errors so it only worked like I had it. I've fixed the indents so everything is working again.

Thanks again!
Chris

Post Reply