Indigo Services in an imported class

Posted on
Fri Apr 23, 2021 6:32 am
stanleykrasnow offline
Posts: 13
Joined: Dec 10, 2018

Indigo Services in an imported class

]I am trying to use a class I wrote that needs Indigo services to execute. When the class is included in line, it works. When the class is imported from a file, it fails with the message " embedded script: global name 'indigo' is not defined". Can someone please help me learn what I'm not doing correctly? Thank you.

Here is documentation:[


Code: Select all
# ######################################################
CLASS DEFINITION:
class my_class:
    def __init__(self, dev_name):   # init method or constructor
        self.dev_name = dev_name
    def my_get_phone_status(self):
    # ######################################################
    #              Get Phone Status
    # ######################################################
        device = self.dev_name
        indigo.server.log("from inside, name = ", device)
        # print("from inside, name = ", device)
        # return (device)
        dev = indigo.devices[device]
        h = dev.states
        x = h.get ('status')
        return (str(x))
   
# ######################################################
CALLING  W/CLASS IMBEDDED INLINE:
xxx = my_class("SJK Pixel 3A XL")
status = xxx.my_get_phone_status()
indigo.server.log ("current status = ", status)
# ######################################################
OUTPUT:
Apr 23, 2021 at 7:34:12 AM
   SJK Pixel 3A XL                 from inside, name =
   up                              current status =
# ######################################################

CALLING  W/CLASS IMPORTED:
from My_file import my_class
xxx = my_class("SJK Pixel 3A XL")
status = xxx.my_get_phone_status()
indigo.server.log ("current status = ", status)
# ######################################################
OUTPUT:
Apr 23, 2021 at 7:34:38 AM
   Script Error                    embedded script: global name 'indigo' is not defined
   Script Error                    Exception Traceback (most recent call shown last):

     embedded script, line 3, at top level
     File "/Users/stanleykrasnow/Google Drive/1 DCQ Home Automatiion - Python/Utilities/My_file.py", line 22, in <module>
       status = xxx.my_get_phone_status()
     File "/Users/stanleykrasnow/Google Drive/1 DCQ Home Automatiion - Python/Utilities/My_file.py", line 9, in my_get_phone_status
       indigo.server.log("from inside, name = ", device)
NameError: global name 'indigo' is not defined

Posted on
Fri Apr 23, 2021 9:48 am
kw123 offline
User avatar
Posts: 7693
Joined: May 12, 2013
Location: Dallas, TX

Indigo Services in an imported class

1. you should post code with [code ][/code ] then the indents etc are visible


2. this work for me:
Code: Select all
class ThreadedTCPRequestHandler(SocketServer.BaseRequestHandler):

####-------------------------------------------------------------------------####
   def handle(self):
      try:
         indigo.server.log("hello")
         if indigo.activePlugin.ipNumberOK(self.client_address[0]):
....

indigo.activePlugin.xxx(abc)
#where xxx is the method in the plugin you want to call

you should post your full code with the [ code ] tag

Karl
Last edited by kw123 on Fri Apr 23, 2021 1:05 pm, edited 2 times in total.

Posted on
Fri Apr 23, 2021 9:49 am
FlyingDiver offline
User avatar
Posts: 5321
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Indigo Services in an imported class

You'll need to do an "import indigo" as well.

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Fri Apr 23, 2021 10:11 am
kw123 offline
User avatar
Posts: 7693
Joined: May 12, 2013
Location: Dallas, TX

Re: Indigo Services in an imported class

actually, I just tried w/o import indigo and that works fine.

I have also have
import indigo
at the top and I don't recall why I added it. ( needed in debug mode .. ? )

Karl

Posted on
Fri Apr 23, 2021 10:17 am
FlyingDiver offline
User avatar
Posts: 5321
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Indigo Services in an imported class

kw123 wrote:
actually, I just tried w/o import indigo and that works fine.

I have also have
import indigo
at the top and I don't recall why I added it. ( needed in debug mode .. ? )

Karl


It used to be required for plugins and scripts, but then it was done by default in the Plugin Host. But if your classes are in external files (that get imported), then I think you need the import statement in those files. I wasn't sure how the OP had his classes organized.

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Sat Apr 24, 2021 1:52 pm
jay (support) offline
Site Admin
User avatar
Posts: 16778
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Indigo Services in an imported class

I always put this at the top of my scripts just so it's clear:

Code: Select all
try:
    import indigo
except:
    print("This script uses the Indigo module and can only be run from Indigo itself, not directly from Python.")

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Sat Apr 24, 2021 1:53 pm
jay (support) offline
Site Admin
User avatar
Posts: 16778
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Indigo Services in an imported class

jay (support) wrote:
I always put this at the top of my scripts just so it's clear:

Code: Select all
try:
    import indigo
except:
    print("This script uses the Indigo module and can only be run from Indigo itself, not directly from Python.")


This also helps with IDEs/editors that mark module references that don't exist in some way (often annoying red underlines).

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 1 guest