python hasattr custom device state

Posted on
Wed Jul 04, 2018 8:54 pm
mundmc offline
User avatar
Posts: 1060
Joined: Sep 14, 2012

python hasattr custom device state

Hi,
This is probably basic, but it is killing me. I have read every post I could find on the subject. I want to go through Indigo devices to find the ones that have a state called "MAC" (e.g. Karl's Unifi plugin devices). My understanding is it is not a standard device state, and I need to use hasattr to see if a device uses this state prior to trying to read the state.

Code: Select all
thisDevice = indigo.devices[1341466765]
testAttr = "MAC"

indigo.server.log(str(thisDevice.states)) #works, lists the device state info
indigo.server.log(str(thisDevice.states[testAttr])) #works,prints the MAC address

if hasattr(thisDevice.states, testAttr):
   indigo.server.log("It has a MAC state.")
   indigo.server.log(str(thisDevice.states[testAttr]))
else:
   indigo.server.log("State not detected")


For whatever reason, I cannot figure out how to use hasattr to tell whether a device has the state or not. In this case, I am certain the device has a state named "MAC".

Thank you in advance!

Posted on
Wed Jul 04, 2018 9:43 pm
Colorado4Wheeler offline
User avatar
Posts: 2794
Joined: Jul 20, 2009
Location: Colorado

Re: python hasattr custom device state

States are a dictionary and not an attribute:

Code: Select all
If ‘testAttr’ in dev.states:
  myVar = dev.states[‘testAttr’]


If you are looking for an attribute I personally use:
Code: Select all
If ‘onState’ in dir(dev):
   MyVar = dev.onState

My Modest Contributions to Indigo:

HomeKit Bridge | Device Extensions | Security Manager | LCD Creator | Room-O-Matic | Smart Dimmer | Scene Toggle | Powermiser | Homebridge Buddy

Check Them Out Here

Posted on
Thu Jul 05, 2018 5:19 am
mundmc offline
User avatar
Posts: 1060
Joined: Sep 14, 2012

Re: python hasattr custom device state

Colorado4Wheeler wrote:
States are a dictionary and not an attribute:

Code: Select all
If ‘testAttr’ in dev.states:
  myVar = dev.states[‘testAttr’]


If you are looking for an attribute I personally use:
Code: Select all
If ‘onState’ in dir(dev):
   MyVar = dev.onState

Mind blown. Thanks C4W.


Sent from my iPhone using Tapatalk

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 4 guests