jalmeroth / homie-python

A Python-implementation of the homie v2 convention.
https://github.com/marvinroger/homie
54 stars 15 forks source link

Load config from JSON/dict #37

Closed fabiosoft closed 6 years ago

fabiosoft commented 6 years ago

I think it cloud be really helpful to load settings from a json or dictionary object and not only from file on local filesystem.

I have different settings in remote database and it's hard to save each of them on disk locally just to load once.

jalmeroth commented 6 years ago

Hi @fabiosoft, thanks for your suggestion. So how do you imagine this to be implemented?

p.s. Pull-Requests are always welcome ;)

fabiosoft commented 6 years ago

Something like this:

config = {
    "DEVICE_ID": "xxxxxxxx",
    "DEVICE_NAME": "xxxxxxxx",
}
Homie = homie.Homie(config)
jalmeroth commented 6 years ago

While I think this would be a useful addition, this is currently not possible. You might try to use environment variable as a workaround HOMIE_DEVICE_ID and HOMIE_ DEVICE_NAME.

fabiosoft commented 6 years ago

That's my use case: I have a lot of instances of "Homie" on same machine (different threads) simulating sensor so they should have different ID and NAME.

bodiroga commented 6 years ago

Hi @jalmeroth!

I'm also interested in this feature and I will try to implement it and submit a PR if you agree. But I'm not sure what will be the way to proceed.

What do you think about changing the default constructor of the Homie class to accept a json object as @fabiosoft suggested? We could make the "_loadConfig(self, configFile)" function public to simplify the transition from the previous situation. Something like:

import homie
config = homie.loadConfig(configFile)
Homie = homie.Homie(config)

All the needed functions are already implemented, it's just a matter of changing the instantiation order :+1:

BTW, what's the current state of the master branch compared to the dev branch? The dev branch is the one that implements the Homie V2 convention? And the master branch follows the 1.5 version? Do you have any plan to merge both branches?

Many thanks for you help and for the work you have done!

Best regards,

Aitor

jalmeroth commented 6 years ago

Hi @bodiroga,

thanks for your suggestion. Please go ahead and submit this change, I will be happy to merge your PR. 👍

Yes the dev-branch was targeted to comply with homie convention 2.0.1, but as we are facing a 3.0.0 release now, I would rather skip 2.0.1 and go for version 3. What do you guys think?

Best regards, Jan