I’m working on creating a ruleset with Jython (which I like a lot by the way, unit tests finally give me assurance that it will do what it is supposed to do before my wife goes crazy).
The one thing I came across are persistence extensions in jython - they seemd to be working in Openhab 1.x with the python module name PersistenceExtensions, but they don’t seem to be there in openhab2.1 anymore.
Does anyone have more information on this?
I am aware that I could work around this by creating periodic triggers in XText that would just populate another item with the historic values I am looking for in jython.
I need to get my company’s OK first to release my code, this will take a couple of more weeks, but then I’ll definitely publish some documentation.
As for now, my strategy looks like this: Write a class in “pure” python that contains all the algorithmic and logical work, that class knows nothing about openhab itself.
Write all unit tests for this class.
In the Rules Class, use the openhab items.
High level code example (using steve-bates classes):
scriptExtension.importPreset("RuleSimple")
scriptExtension.importPreset("RuleSupport")
import unittest
class Blinds():
def __init__(self, temerpature):
self.temperature = temperature
...
def DoSomething():
# some really complicated blind control here, with variables only, no real openhab items (e. g. pure python)
...
class BlindUnitTest(unittest.TestCase):
def test_cold_day(self):
blind = Blinds(-14)
r = blind.DoSomething()
self.assertEqual(100, r.position)
class BlindsRule(SimpleRule):
...
def execute(self, module, input):
# instantiate the Blinds class and just fill in the items
blind_window_south = Blinds(itemRegistry.getItem('Temperature').getState().floatValue())
# get the return value and run postUpdate() on it.
r = blind_window_south.DoSomething()
events.postUpdate(itemRegistry.getItem('Blind_Window_Sout_Position'), r.position)