Python plugins: How to store additional information for a device

Python and python framework
Post Reply
User avatar
Dnpwwo
Posts: 767
Joined: Sunday 23 March 2014 10:00
Target OS: Raspberry Pi
Domoticz version: Beta
Location: Melbourne, Australia
Contact:

Python plugins: How to store additional information for a device

Post by Dnpwwo » Saturday 08 December 2018 4:49

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: 156
Joined: Monday 02 April 2018 20:33
Target OS: Raspberry Pi
Domoticz version: beta
Location: France
Contact:

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

Post by pipiche » Monday 10 December 2018 12:38

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: 156
Joined: Monday 02 April 2018 20:33
Target OS: Raspberry Pi
Domoticz version: beta
Location: France
Contact:

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

Post by pipiche » Monday 10 December 2018 12:48

Last it looks like on Domoticz V.4.10242

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

Generate : 'onStart' failed 'KeyError'.

User avatar
Dnpwwo
Posts: 767
Joined: Sunday 23 March 2014 10:00
Target OS: Raspberry Pi
Domoticz version: Beta
Location: Melbourne, Australia
Contact:

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

Post by Dnpwwo » Monday 10 December 2018 13:55

@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: 156
Joined: Monday 02 April 2018 20:33
Target OS: Raspberry Pi
Domoticz version: beta
Location: France
Contact:

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

Post by pipiche » Monday 10 December 2018 14:37

@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: 156
Joined: Monday 02 April 2018 20:33
Target OS: Raspberry Pi
Domoticz version: beta
Location: France
Contact:

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

Post by pipiche » Monday 10 December 2018 18:18

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 !

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest