device.updated, run concurrent, subscribe to changes, other?

Posted on
Fri Jan 29, 2021 2:13 pm
whmoorejr offline
User avatar
Posts: 762
Joined: Jan 15, 2013
Location: Houston, TX

device.updated, run concurrent, subscribe to changes, other?

Trying to figure out which way to go with a plugin. It's functioning as expected, but I want to take it a step further...
a "device" in my plugin has empty states that are filled with either text, a variableID or statekey from another device. As an example, the device "Bill's Office" has a statekey, "mainLight" which reflects the state of the actual light in my office.

Currently, I can create an action that will populate Bill's Office:mainLight with On or Off. It will update the plugin whenever the action is called because the statekey only sees the string substitute of "%%d:937130354:onOffState%%".

I would like it to stay updated, but don't want to kill indigo with too much stuff in a run concurrent thread (Which I've never written before... baby steps. )

My thought is to add some metadata lines with the code that updates the states where "newValue" is the entered text (not substituted)
Code: Select all
newProps = thisRoomDev.pluginProps
      metaDataItem = "meta_"+requestedState
      newProps[metaDataItem] = newValue
      thisRoomDev.replacePluginPropsOnServer(newProps)
Then a update mechanism that runs through all the plugin devices and re-populates the states from the stored metadata line which would keep them up updated so when I turn my office light on, the plugin that reflect that state will update... something like...
Code: Select all
def runconcurrent bla bla bla(stuff):
      xList =[]
      for dev in indigo.devices.iter(filter="com.whmoorejr.my-rooms"):
         accentLightMD = self.substitute(dev.pluginProps[meta_accentLight])
         mainLightMD = self.substitute(dev.pluginProps[meta_mainLight])
         otherDeviceMD = self.substitute(dev.pluginProps[meta_otherDevice])

         key_value_list = [
            {'key':'accentLight', 'value':accentLightMD},
            {'key':'mainLight', 'value':mainLightMD},
            {'key':'otherDevice', 'value':otherDeviceMD}
         ]
         dev.updateStateOnServer(key_value_list)

My questions are...
Will this work?
Is there a better way?
Can you "self.substitute(dev.pluginProps...."?

Last thing... some states I would expect to change all the time... Lights, Motion Sensors, etc. Some will probably never change, roomName, floorLevel, buildingName. Either way, even though this could be updating stuff continually in the background, if that's too taxing on the server, the other option is to create an "Update all states" action that could be put on a control page so as you flip through rooms, you could click a button to get an updated status.... that wouldn't be ideal, but better than nothing if it shouldn't be a continual update thing.

Bill
My Plugin: My People

Posted on
Fri Jan 29, 2021 3:06 pm
jay (support) offline
Site Admin
User avatar
Posts: 18201
Joined: Mar 19, 2008
Location: Austin, Texas

Re: device.updated, run concurrent, subscribe to changes, ot

You need to subscribe to device (and variable?) changes, such that whenever there is a device change you can see (via calls to the deviceUpdated method that you implement) if it's one that's represented in one of your plugin's devices and update the state(s) accordingly.

Here's the rough approach I think I'd take:

  1. When deviceStartComm is called for one of your devices, you inspect the device to discover which other devices/variables are used by it, and you save it off in a map.
  2. You define a deviceUpdated method which inspects the device ID to see if it's one that's used by one of your devices, and if so you do the appropriate updates (do the same for variableUpdated if you're watching variable changes).
  3. When deviceStopComm is called for one of your devices, you remove any linked devices/variables from the map so you don't continue to update them.
  4. For completeness, you'd want to also implement deviceDeleted so that whenever a device that's referenced by one or more of your device is deleted you can update that device accordingly (same for variables). Otherwise you might (depending on implementation details) get some odd errors when there's a broken dependency.

This is (roughly) the approach that the Virtual Device interface takes specifically for the device group type - whenever a device in the group gets updated, we watch for that change and do the appropriate updates to our group device.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Sat Jan 30, 2021 8:56 pm
whmoorejr offline
User avatar
Posts: 762
Joined: Jan 15, 2013
Location: Houston, TX

Re: device.updated, run concurrent, subscribe to changes, ot

jay (support) wrote:
This is (roughly) the approach that the Virtual Device interface takes specifically for the device group type - whenever a device in the group gets updated, we watch for that change and do the appropriate updates to our group device.


Edit... How do I look at the Virtual Device plugin? It seems to have a plugin.so file instead of a plugin.py file. Since it's a built in plugin, is it a different kind of animal? FYI, if you change the .so to .py and open it in BBEdit, it's a red&white version of the matrix.
Screen Shot 2021-01-30 at 9.03.24 PM.png
Screen Shot 2021-01-30 at 9.03.24 PM.png (491.42 KiB) Viewed 2458 times


I've been picking apart Karl's Home-Away plugin because it does some magical stuff with outside state keys and variables as well.

As I begin to understand what I'm looking at when I pop the hood of a plugin, it makes it easier to figure out what I need to take note of, make a copy of, etc.

Thanks again Jay for pointing me in the right direction.

Bill
My Plugin: My People

Posted on
Sun Jan 31, 2021 12:43 am
kw123 offline
User avatar
Posts: 8335
Joined: May 12, 2013
Location: Dallas, TX

Re: device.updated, run concurrent, subscribe to changes, ot

.so files are “compiled” py files.
The normal text of a regular py file is gone.

So you can’t really view them.


Sent from my iPhone using Tapatalk

Posted on
Sun Jan 31, 2021 6:37 am
DaveL17 offline
User avatar
Posts: 6744
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: device.updated, run concurrent, subscribe to changes, ot

Have a look at the Plugin Developer Documenter plugin.

https://forums.indigodomo.com/viewtopic.php?f=59&t=14169

This is its reason for being. :D

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Sun Jan 31, 2021 6:39 am
DaveL17 offline
User avatar
Posts: 6744
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: device.updated, run concurrent, subscribe to changes, ot

Apparently, I linked to an old thread. The plugin is now on GitHub.

https://github.com/RogueProeliator/IndigoPlugins-Plugin-Developer-Documenter/releases

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Sun Jan 31, 2021 10:36 am
whmoorejr offline
User avatar
Posts: 762
Joined: Jan 15, 2013
Location: Houston, TX

Re: device.updated, run concurrent, subscribe to changes, ot

DaveL17 wrote:
Have a look at the Plugin Developer Documenter plugin.
This is its reason for being. :D


I've got the plugin... but that opens up another question...

What is the best method for a testing platform? I currently test on my main indigo machine. I haven't broken anything, so that's good... but it is kinda a pain constantly screen sharing with the headless mini running indigo. The other suck part is that I have a very busy house. My log traffic is insane during the day especially on "Virtual" school days.

When I hear plugin developers talking about testing, are you guys running indigo on a separate machine with a fresh license or are you just creating a separate database on your main indigo platform and test in there (but wouldn't that shut down the 'real' database).

So, is there a Demo/Testing best practice?


Other random/general question: When you .... borrow, bits from someone else's plugin or from someone that helped you on the forum, is there a preferred or standard way of publicly thanking them? Like a ### From Dave, next to the tidbit in the .py file or "Thanks to .... " on the config GUI? As someone that is very new to this, I'm unfamiliar with developer etiquette.

Bill
My Plugin: My People

Posted on
Sun Jan 31, 2021 12:07 pm
DaveL17 offline
User avatar
Posts: 6744
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: device.updated, run concurrent, subscribe to changes, ot

Your first question is better answered by Matt or Jay.

Regarding the use of others' code, IMO, it's kind of a case-by-case thing. Many developers include a license in their code which lays out how it can (or can't) be used. Code posted to Stack Overflow is governed by its terms of use--in this case, code posted there is freely usable by anyone for any purpose. Best approach, if you're unsure, is to ask. When I use others' code whole-cloth, I try to follow their terms as best I can (including licenses in my code, etc.) and I also acknowledge them in comments in my code. That way, if someone borrows from me, they'll know that they may be borrowing from someone else, too. If I'm using others' code just to learn how to so something, I consider that research and I'll often include a url or a citation to say thanks. Beyond that, I'd say it's up to you to judge how much someone helped you and how you'd like to recognize them.

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Sun Jan 31, 2021 12:12 pm
kw123 offline
User avatar
Posts: 8335
Joined: May 12, 2013
Location: Dallas, TX

Re: device.updated, run concurrent, subscribe to changes, ot

I agree w dave. And if you borrow a lot, some of the developers have their PayPal account in their indigo store published.

But a well meant “thank you” post here in the discussion forum goes a long way. From my point of view it’s better than a PayPal donation.

Karl


Sent from my iPhone using Tapatalk

Posted on
Sun Jan 31, 2021 12:24 pm
FlyingDiver offline
User avatar
Posts: 7190
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: device.updated, run concurrent, subscribe to changes, ot

whmoorejr wrote:
When I hear plugin developers talking about testing, are you guys running indigo on a separate machine with a fresh license or are you just creating a separate database on your main indigo platform and test in there (but wouldn't that shut down the 'real' database).


I have a separate Indigo install on my desktop Mac, with it's own license, that I use for development. It's only when I'm testing something that's a hardwire connection to the main Indigo server that I resort to screen sharing. Most of my plugins are IP connected (local or cloud), so it's rare that I need to actually test stuff on the production system.

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

Posted on
Sun Jan 31, 2021 2:12 pm
whmoorejr offline
User avatar
Posts: 762
Joined: Jan 15, 2013
Location: Houston, TX

Re: device.updated, run concurrent, subscribe to changes, ot

DaveL17 wrote:
a license in their code
Crap, I didn't even think about that!.... luckily, I've only been using stuff people have "given" me on the forum or I look at other plugins to figure out how to format stuff.... Dave, I haven't used much from your plugins, but I used quite a few of your scripts and several of the graphics you've contributed on my control pages.

Example:

Peter's
howartp wrote:
:)
ZWave Lock/Code Manger Plugin: Great example of formatting the actions dialogue using separators and the uiPath tag.
or from his Clock Display plugin, they way the states are organized in the Device.xml file is very streamlined

or Karl's
kw123 wrote:
:)
the way he used buttons and lists to get device information and statekey information from the user to the py file. (But it's in an events.xml file and I'm not sure when/how "events" are called... but I put some of that stuff in my actions.xml)

Sorry for the smiley faces above... my hack version of tagging people on my post.

FlyingDiver wrote:
I have a separate Indigo install on my desktop Mac, with it's own license, that I use for development.
I figured that would be the best way to go. It's been so long since I had an empty indigo database, it will be a little exciting.

The plugin I'm working on (My Rooms) and the one I wrote (My People), compared to the plugins that have been published by your guys.... mine are ridiculously simplistic. Mine don't have anywhere near the logic or complexity and I'm years away from having that kind of skill. I'm not trying to bash on myself, the intent of my plugins aren't meant to be that. Mine are meant to be a way to harness the logic from the plugin's that you all have created and consolidate it in a user defined way. One Room device, for example, might have state keys populated from 10 different plugins. Like piBeacon, fingscan, homeAway, occupatum, to populate the occupancy field of a room device or a person device. SMTPd plugin, Foscam, Security Spy, etc., to populate the cctv MotionOn field of a room device. . Airfoil, Roku, Plex, Sharp, etc., to populate the different audio/video states of aRoom device, etc. Lock/Code manager to tell "My People" to change the "Now Showing" room to "lock/code manger:userid == aPerson:userid". FindFriendsMini to populate aPerson:Location....

My plugins don't really Do anything. My analogy is this... control pages are a wall. Your plugins are art. My plugins are just a different style of frame to display the art on the wall.

Bill
My Plugin: My People

Posted on
Mon Feb 01, 2021 11:23 am
jay (support) offline
Site Admin
User avatar
Posts: 18201
Joined: Mar 19, 2008
Location: Austin, Texas

Re: device.updated, run concurrent, subscribe to changes, ot

Yeah, some of our plugins use compiled code for a variety of reasons. Only some of those that we include in the installer do that - everything on our Github repo is full source. We've been gradually moving plugins to Github that we once installed with Indigo, though the Virtual Devices interface won't likely be one of those, at least not in the near term.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 3 guests