I don’t particularly need to derive from but I would like to be able to instantiate a class from one file into another file. Not sure if that’s a distinction without a difference.
Speaking of restarting HABApp… It is rather a pain to have to restart HABApp every time I add a new item in OH, especially since I also then have to tail -f another logfile. Have you considered having a magic item (you already have HABApp_Ping, why not a HABApp_Restart or HABApp_Refresh switch) which tells HABApp to re-initialize itself?
As for libraries and reloading, in OH2 with Jython, when using a personal library, it was standard practice to:
import personal.superbuttons
reload (personal.superbuttons)
…to ensure that that you got the latest version of the library, without having to restart anything else.
There are many, many functions that I will need to write and use for many different items.
Here’s one example of something that I need to be in a library.
The superbuttons library above references my own 433 MHz remote parser, which distills the raw code values rc-switch puts out. I use wall-mounted 433 MHz remotes everywhere – that is my primary day-to-control method.
So, pushing a remote button, rc-switch spits out 20f4a1
for every code it sees. One does not want to post that raw to MQTT (message rate), and in any case you’ve gotta distill it down somewhere, so I do that with my superbuttons library inside the esp8266 or esp32 that has the 433 MHz receiver.
Then I post the following to the remote receiver MQTT topic:
20f4a1*TALLY,1,0
20f4a1*SOLID,0,1
20f4a1*MEDIUMPRESS,0,3
20f4a1*LONGPRESS,0,7
20f4a1*RELEASE,1,7
20f4a1*DONE,1,7
If pressed three times in quick succession, the following gets posted:
20f4a1*TALLY,1,0
20f4a1*SOLID,0,1
20f4a1*RELEASE,1,1
20f4a1*TALLY,2,1
20f4a1*RELEASE,2,1
20f4a1*TALLY,3,1
20f4a1*RELEASE,3,1
20f4a1*DONE,3,1
This allows me to write whatever logic I want at the destination side, without the remote receiver needing to be aware of any remotes.
For example, if you wanted to have a separate action for one, two or three pushes, you’d simply trap the DONE message and look at the first parameter which is the number of distinct keypresses (this is determined by timing). If you wanted a self-repeating control while holding the button down, you’d simply trap TALLY and RELEASE while ignoring the parameters.
Anyway, the OH2 JSR223/Jython superbuttons counterpart simply contains the following:
class ParseEvent(object):
def __init__(self, payload):
self.valid = 0
self.code = 0
self.event = 0
self.count = 0
self.solid = 0
self.medium = 0
self.long = 0
self.verylong = 0
self.flags = 0
if payload.find("*") < 0: return
splitevent = payload.split("*")
self.code = int(splitevent[0],base=16)
splitdata = splitevent[1].split(",")
if len(splitdata)<3: return
self.event = splitdata[0]
self.count = int(splitdata[1])
self.flags = int(splitdata[2])
self.solid=1 if (self.flags & 1) else 0
self.medium=1 if (self.flags & 2) else 0
self.long=1 if (self.flags & 4) else 0
self.verylong=1 if (self.flags & 8) else 0
self.valid = 1
And then, here’s the rule that triggers on the 433 MHz receiver item (also OH2 Jython):
sb=superbuttons.ParseEvent(event.getEvent())
if not sb.valid: return
if sb.code == 0x19697d:
if sb.event == "SOLID":
toggle("BedsideLight")
And by the way, the toggle function is in another library:
from core.jsr223.scope import itemRegistry, OnOffType
def toggle(strItem):
item=itemRegistry.getItem(strItem)
item.send(OnOffType.OFF if item.getState()==OnOffType.ON else OnOffType.ON)
Where would those things go if not in libraries?