Closed jpmens closed 8 years ago
Additionally, it would be good to have support for TLS (needs a filename to a PEM-encoded CA certificate) as well as client authentication (username, password)
(Note: I opened new issues for Authentication #7 and TLS #8.)
@jpmens which kind of configuration file format do you think would suit best here? JSON, YAML, …?
JSON would have the advantage that you could reuse Marvin's Homie-esp8266 configuration file format, and that JSON is built-in to Python.
YAML looks neater and is easier to edit, but requires additional "batteries". If we want "editable" I think something parseable by ConfigParser would do the trick (e.g. "Ini").
Assuming that homie-python will be used by people who also use homie-esp8266, I'd go for Marvin's format.
@jpmens thanks for your thoughts! 👍
You can now pass on a configuration file (in JSON) to homie-python.
If I ever get around to doing this (although it's trivial), would you accept a PR which adds the possibility of setting device ID/name from an environment variable (HOMIE_DEVICEID
, say)? This would enable programs to use the same configuration file but override certain bits from the environment.
The configuration from file works, @jalmeroth, which is good.
What I consider an issue though, is that if the config cannot be opened (e.g. because the specified file doesn't exist), you fall back to default values, in particular host test.mosquitto.org
where my "device" would publish retained values to. This could be a privacy issue.
I think it would be better to fail completely if the JSON configuration cannot be opened.
@jpmens sure, I would accept your PR… but how about this:
self.deviceId = self.config.get("DEVICE_ID", (
os.environ["HOMIE_DEVICE_ID"]
if "HOMIE_DEVICE_ID" in os.environ
else "xxxxxxxx"))
Does this fulfill your needs?
That's along the lines of what I was thinking, yes. (Except that your code is much cleaner than mine would be. :-)
But isn't this easier?
self.deviceId = self.config.get("DEVICE_ID", os.getenv("HOMIE_DEVICE_ID", "xxxxxx"))
Added environment variables in 539dc15f610e8b02125618099fcae973d2915274
Just a thought: we could override each of the config values with variables in loadConfig
:
config = json.load(fp)
...
for key in config:
config[key] = os.getenv("HOMIE_" + key, config[key])
Hm, I personally don't use environment that much. What would be the use-case for this approach?
Being able to test by overriding from environment without having to provide a distinct configuration file.
I introduced an overwrite-function and defined the list pref_keys allowed to be overwritten. Does this fit?
This fits. Thank you.
I've resorted to this in order to adjust configuration parameters for testing:
I wonder if it would be sensible to be able to pass a filename to
homie.Homie()
for it to read the configuration from. Alternatively, maybe support configuration via environment variables?