So I thought I might turn the device dict into a string and parse out all the attributes, but that proved to be a bit too much work to be worthwhile. Instead, I decided to go with a list of desired attributes. I'm not sure that doing the attributes with a dictionary comprehension is the most efficient way, but it works and seems fast.
- Code: Select all
def deviceUpdated(self, origDev, newDev):
indigo.PluginBase.deviceUpdated(self, origDev, newDev)
if origDev.id in [697415992,]:
# Attribute changes
exclude_list = ['globalProps', 'lastChanged', 'lastSuccessfulComm', 'ownerProps', 'states']
attrib_list = [attr for attr in dir(origDev) if not callable(getattr(origDev, attr)) and '__' not in attr and attr not in exclude_list]
attrib_dict = {attrib: (getattr(origDev, attrib), getattr(newDev, attrib)) for attrib in attrib_list if getattr(origDev, attrib) != getattr(newDev, attrib)}
# Property changes
orig_props = dict(origDev.ownerProps)
new_props = dict(newDev.ownerProps)
props_dict = {key: (orig_props[key], new_props[key]) for key in orig_props if orig_props[key] != new_props[key]}
# State changes
state_dict = {key: (origDev.states[key], val) for key, val in newDev.states.iteritems() if key not in origDev.states or val != origDev.states[key]}
if len(attrib_dict) > 0 or len(state_dict) > 0 or len(props_dict) > 0:
indigo.server.log(u"\nDevice Changes: [{0}]\n{1:<8}{2}\n{3:<8}{4}\n{5:<8}{6}".format(newDev.name, 'Attr:', attrib_dict, 'Props', props_dict, 'States', state_dict))
This is the output when the device 'dave' is turned on (note that onState isn't the best example because the attribute and the state are redundant):
- Code: Select all
dave: Attribs: {'onState': (False, True)} States: {u'brightnessLevel': (0, 60), u'onOffState': (False, True)}
This is the output when the device 'dave' is renamed to 'Dave':
- Code: Select all
Dave: Attribs: {'name': (u'dave', u'Dave')} States: {}
This is the output when the device 'Dave' is disabled:
- Code: Select all
Dave: Attribs: {'enabled': (True, False)} States: {}
EDIT: updates to pull attributes from dir(origdey).
Edit 2: Updates to parse changes to device props.