Page 1 of 1

Python plugins: How to store additional information for a device

Posted: Saturday 08 December 2018 3:49
by Dnpwwo
An update on writing additional data to the database, particularly for fields for devices. If you are running the most up to date beta this code works (in this case adding a field called 'UUID'):

Code: Select all

        import sqlite3
        dbConn = None
        dbConn = sqlite3.connect(Parameters["StartupFolder"]+'domoticz.db')
        # Ensure that extra column has exists in DeviceStatus table (create if not)
        try:
            dbCursor = dbConn.execute("ALTER TABLE DeviceStatus ADD COLUMN UUID VARCHAR(36)")
            dbConn.commit()
            Domoticz.Log("DeviceStatus table definition updated successfully with UUID column.")
        except Exception as e:
            if (dbConn != None): dbConn.rollback()
            Domoticz.Debug("DeviceStatus table definition NOT updated, UUID column probably exists.")
            Domoticz.Debug("Exception: '"+str(e)+"'")
I noticed that a number of plugin authors seem to be using Domoticz variables to hold additional data, I would think that this approach would be neater.

Re: Python plugins: How to store additional information for a device

Posted: Monday 10 December 2018 11:38
by pipiche
I would be very careful with such approach, as at any point of time Domoticz can decide to change the domoticz.db schema and add a conflicting column.

So eventually this workaround could be acceptable for creating a new Table, but still Domoticz could also create a conflicting table.

So for me the end solution is the Python Plugin Framework providing in a native way such capability. In such, this will also pre-define a naming convention to avoid any further conflicts.

Re: Python plugins: How to store additional information for a device

Posted: Monday 10 December 2018 11:48
by pipiche
Last it looks like on Domoticz V.4.10242

Domoticz.Status("Startup Folder: %s" %Parameters["StartupFolder"])

Generate : 'onStart' failed 'KeyError'.

Re: Python plugins: How to store additional information for a device

Posted: Monday 10 December 2018 12:55
by Dnpwwo
@pipiche,

StartupFolder was added 2 days ago (https://github.com/domoticz/domoticz/co ... 28d145960b) so you need to be on very recent beta.

If you are worried about name clashes you can always choose names decorated with 'plugin' or 'pipiche'. 'UUID' may be reused but 'chromecastUUID' is unlikely to be.

Re: Python plugins: How to store additional information for a device

Posted: Monday 10 December 2018 13:37
by pipiche
@Dnpwwo, I'm doing the compilation right now ;-)

Do you have the possibility to extend the parameters list expose to the Framework ?
It would be indeed very beneficial to get access to parameters like those available
- in the settings-Other tab
- Like those available on the System Tab (like Hardware/Devices),

Thanks for the work you are doing

Re: Python plugins: How to store additional information for a device

Posted: Monday 10 December 2018 17:18
by pipiche
I do confirm, after re-compilation it works,

So I'll be using the following code in the plugin in order to make sure that

Code: Select all

        if Parameters["DomoticzVersion"] >= '4.10267':
               %Parameters["StartupFolder"] is available !

Re: Python plugins: How to store additional information for a device

Posted: Friday 11 January 2019 16:01
by pipiche
pipiche wrote: Monday 10 December 2018 11:48 Domoticz.Status("Startup Folder: %s" %Parameters["StartupFolder"])
@dnpwwo I feel that StartupFolder might not be the right path, when you start domoticz like I do.

Code: Select all

 /usr/share/domoticz/beta-domoticz/domoticz -www 8080 -sslwww 8443 -sslcert /var/lib/domoticz/server_cert.pem -sslpass -sslmethod -ssloptions -ssldhparam -wwwroot /var/lib/domoticz/www -dbase /var/lib/domoticz/domoticz.db -userdata /var/lib/domoticz/ -debuglevel normal -loglevel normal,status,error -log /var/tmp/domoticz.log
So it might be great if the Python Plugin Framework could provide the same Parameters as the one use by Domoticz:

StartupFolder
VarFolder
WWWrootFolder
DatabaseFolder
UserdataFolder

Re: Python plugins: How to store additional information for a device

Posted: Tuesday 15 January 2019 11:32
by Dnpwwo
@pipiche,

I've added:

Code: Select all

		ADD_STRING_TO_DICT(pParamsDict, "UserDataFolder", szUserDataFolder);
		ADD_STRING_TO_DICT(pParamsDict, "WebRoot", szWebRoot);
		ADD_STRING_TO_DICT(pParamsDict, "Database", dbasefile);
in addition to:

Code: Select all

		ADD_STRING_TO_DICT(pParamsDict, "StartupFolder", szStartupFolder);
these are the standard Domoticz global variables that are available to internally to C++ developers. From reading the start up code I believe they have default values that are over written by command line parameters if supplied so they should be correct.

I believe they should be used in combination, for example the database is normally in the Domoticz Startup folder so the 2 values (szStartupFolder + dbasefile) need to be combined

Re: Python plugins: How to store additional information for a device

Posted: Tuesday 15 January 2019 11:49
by pipiche
Great news. Will retreive and recompile Domoticz...

Re: Python plugins: How to store additional information for a device

Posted: Tuesday 15 January 2019 14:55
by pipiche
Sound good, but not for WWWRoot

The szWebRoot looks to be the URL root. which is defined in the domoticz.conf file
# Web root path (for instance mydomoticz, Domoticz url will become https://127.0.0.1:8080/mydomoticz/)

What I was looking is szWWWFolder

and an other way which is -wwwroot and in that case, the settings is store in szWWWFolder

I have submitted: https://github.com/domoticz/domoticz/pull/2971