Python plugins: How to store additional information for a device

Python and python framework

Moderator: leecollings

Post Reply
User avatar
Dnpwwo
Posts: 820
Joined: Sunday 23 March 2014 9:00
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Melbourne, Australia
Contact:

Python plugins: How to store additional information for a device

Post 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.
The reasonable man adapts himself to the world; the unreasonable one persists to adapt the world to himself. Therefore all progress depends on the unreasonable man. George Bernard Shaw
pipiche
Posts: 2035
Joined: Monday 02 April 2018 20:33
Target OS: Linux
Domoticz version:
Contact:

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

Post 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.
pipiche
Posts: 2035
Joined: Monday 02 April 2018 20:33
Target OS: Linux
Domoticz version:
Contact:

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

Post by pipiche »

Last it looks like on Domoticz V.4.10242

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

Generate : 'onStart' failed 'KeyError'.
User avatar
Dnpwwo
Posts: 820
Joined: Sunday 23 March 2014 9:00
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Melbourne, Australia
Contact:

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

Post 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.
The reasonable man adapts himself to the world; the unreasonable one persists to adapt the world to himself. Therefore all progress depends on the unreasonable man. George Bernard Shaw
pipiche
Posts: 2035
Joined: Monday 02 April 2018 20:33
Target OS: Linux
Domoticz version:
Contact:

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

Post 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
pipiche
Posts: 2035
Joined: Monday 02 April 2018 20:33
Target OS: Linux
Domoticz version:
Contact:

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

Post 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 !
pipiche
Posts: 2035
Joined: Monday 02 April 2018 20:33
Target OS: Linux
Domoticz version:
Contact:

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

Post 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
User avatar
Dnpwwo
Posts: 820
Joined: Sunday 23 March 2014 9:00
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Melbourne, Australia
Contact:

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

Post 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
The reasonable man adapts himself to the world; the unreasonable one persists to adapt the world to himself. Therefore all progress depends on the unreasonable man. George Bernard Shaw
pipiche
Posts: 2035
Joined: Monday 02 April 2018 20:33
Target OS: Linux
Domoticz version:
Contact:

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

Post by pipiche »

Great news. Will retreive and recompile Domoticz...
pipiche
Posts: 2035
Joined: Monday 02 April 2018 20:33
Target OS: Linux
Domoticz version:
Contact:

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

Post 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
Post Reply