recent Domoticz.Configuration() API problem

Python and python framework

Moderator: leecollings

Post Reply
pipiche
Posts: 2034
Joined: Monday 02 April 2018 20:33
Target OS: Linux
Domoticz version:
Contact:

recent Domoticz.Configuration() API problem

Post by pipiche »

I have an issue with the recent added Domoticz.Configuration API in the Python Framework

from the doc: https://www.domoticz.com/wiki/Developin ... hon_plugin, the correct value type are dictionary. However it looks like the API doesn't survive to a Dictionary with a key equal to None !

Here is a piece of code to duplicate:

Code: Select all

config_item = { '_count': self.count , 'None_Value': None}
Domoticz.Log("config_item: %s" %str(config_item))
Domoticz.Log("Json config_item: %s" %json.dumps(config_item ))
setConfigItem( Value=config_item)
pipiche
Posts: 2034
Joined: Monday 02 April 2018 20:33
Target OS: Linux
Domoticz version:
Contact:

Re: recent Domoticz.Configuration() API problem

Post by pipiche »

For those whom might get to that issue ( having a dict with None value ), I have made a workaround by converting the None to Null when saving and from Null to None when retrieving.

I think the best would be to have Domoticz.Configuration() capable to store JSON datatype instead of dict, this will make the code in Domoticz simpler and rely on JSON Encoder/decoder

Code: Select all

def prepare_dict_for_storage( dict_items):
    Domoticz.Log("prepare_dict_for_storage")
    dict_str = json.dumps( dict_items )
    return json.loads( dict_str, object_pairs_hook=dict_None_to_Null )
    
def repair_dict_after_load( dict_items):
    dict_str = json.dumps( dict_items )
    return  json.loads( dict_str, object_pairs_hook=dict_Null_to_None)

def dict_None_to_Null( items ):
    result = {}
    for key, value in items:
        if value is None:
            Domoticz.Log("Updating %s: %s to %s:'Null'" %(key,value, key))
            value = "Null"
        result[key] = value
    return result

def dict_Null_to_None( items ):
    result = {}
    for key, value in items:
        if value == "Null":
            value = None
        result[key] = value
    return result   
    
pipiche
Posts: 2034
Joined: Monday 02 April 2018 20:33
Target OS: Linux
Domoticz version:
Contact:

Re: recent Domoticz.Configuration() API problem

Post by pipiche »

for information, I found an other limitation, which is that the new API doesn't work nicely with the full Dict type. for instance if you have any value inside your dictionary set to None, then it will be rejected !!!
Post Reply