'Unit testing' scripts

I’m getting the hang of ‚the IPS way‘ after playing with setting up my Fibaro RGBW dimmer and implementing some of its advanced features. Now I want to move on to step 2.

I have a fairly complicated algorithm for determining if and how bright the lights in the hallway of the floor with the bedrooms should be on. This algorithm depends on the status of a movement sensor, whether it’s before or after sunset/dawn, the current time, the average over the last 10 days of a measured variable, a wall switch and the status of a manual override through the mobile frontend.

Normally I would make a matrix of the states of all variables, fill in what their status should be, write a bunch of unit tests to test all those states, then implement the algorithm.

However with IPS this is not as straightforward - I need to be able to simulate events, different times of day, etc. in my mocks.

It would seem that this should be somehow possible running IPS in a virtual machine and using IPSLibrary. I imagine that the developers of IPS have their own unit tests, too. Is there an overview, best practice guide, advice or whatever available on how to best do this? Or should I just make up my own methodology?

You have all the power which PHP brings you. So just encapsulate the algorithm as functions, test them in isolation with any desired values and afterwards use them with the real values, which you get through GetValue.


Hmm yes that makes sense.

However, is there a way to have ‚global‘ functions? I.e. functions that are available from every script? Or do I need to use the hack described in the manual (with the IPS_GetScript() and include(), and ‚pass‘ arguments by setting them as global variables) ?