oklona / Miele-MQTT

A very simple script to read data from Miele@home cloud services, and publish using Mosquitto MQTT
21 stars 10 forks source link

Move OAuth procedure away from wizard #30

Open christianTF opened 3 years ago

christianTF commented 3 years ago

Currently, the OAuth procedure to get code, access_token and refresh_token sits in the middle of the prompted configuration wizard. To automate the creation of an initial config file, these steps should take place automatically on script start, if a config file is present but these variables are empty. Otherwise, it is not possible to automatically fill a working config file.

Background: I think about integrating your program into a LoxBerry Plugin (https://www.loxwiki.eu/pages/viewpage.action?pageId=27100273). Required settings would be set in a webif, others like broker settings will be used from LoxBerrys configuration, and together the initial miele-config2.php configuration file would be created by the LoxBerry plugin code without wizard.

For this to work, the initial OAuth stuff needs to be done on starting of the script automatically, if variables are empty.

A change on this would be very appreciated!

cu, Christian

Loxfreek commented 3 years ago

Christian, could this lead to my problem if I have changed script file and now it's not anymore running automatically on my LoxBerry?

christianTF commented 3 years ago

Christian, could this lead to my problem if I have changed script file and now it's not anymore running automatically on my LoxBerry?

No, that’s not your issue.

oklona commented 3 years ago

Hi! I am just back from vacation. I am a bit unsure of what you really mean here? Are you saying that if config file is present, but OAuth parameters are missing, we should only ask for authentication? Could you tell me more about what would be your "dream scenario" for this?

christianTF commented 3 years ago

I‘ll upload my updated script in here, for you to compare.

My „dream“ scenario would be, that this https://github.com/oklona/Miele-MQTT/blob/039361347840372e35b3d184df6e9da3ed0033b3/miele-MQTT.php#L191 and this https://github.com/oklona/Miele-MQTT/blob/039361347840372e35b3d184df6e9da3ed0033b3/miele-MQTT.php#L223 requests are not part of the wizard.

Instead, if code or the tokens are missing in the config on script Start, they are automatically fetched and added to the config, and on success, the scripts goesvto normal processing.

I‘ve already a working implementation of this based on your Code, but it really needed a lot of scrambling around because of the way how the config is read and written. It was a quick-and-dirty try (more dirty as quick…). I will upload in here, but I think you might get 😱 if you see this….

oklona commented 3 years ago

Ok. I am absolutely willing to look at this. -But how would you like the "wizard" to look, then? Would you like the wizard not to ask for codes, username and password, and only request that separately, if needed? Would you want to have command line parameters to supply this information? (Which would then need to be handled securely in some way.) I definitely agree that doing the OAuth authentication and retrieving information in the same procedure is not the best way to do things, so these should probably be separated into different procedures anyways. (This started as just a simple script for my dishwasher that I wanted to share, so I didn't really put much effort into architecture.)

My immediate suggestion is this:

This way, the config process would be way more flexible. Another way would (of course) be to actually create a small separate script that will just create the config file for your plugin. I don't know Loxberry (is that what it was called), so I basically have no idea what such a plugin would look like.

christianTF commented 3 years ago

miele-MQTT.php.txt

The modification defines two new functions oauth_userlogin and gettokens, and I added $config global for some existing functions to get and set params from the config. To get a clue how the functions and if conditions are executed, I added debugging code (if($debug){print("-->". __FUNCTION__ . PHP_EOL);}) as well.

For you, to get a feeling, how LoxBerry and a LoxBerry plugin looks like, I add a screenshot of the Nuki SmartLock plugin.

LoxBerry is a easy-to-use gateway to Loxone Smarthome devices, prepared as Debian-Light image that users install on their Raspberry's (VMs are available too). All settings is done through the webbrowser. Plugins make use of the provided UI, SDK and settings from the core LoxBerry system, to configure scripts to run in the background or by web requests from Loxone's central unit (e.g. send Weather data to the Loxone central unit; or connect devices like heating to Loxone, where LoxBerry acts as a gateway from device interfaces to Loxones software interfaces).

We have placed MQTT as a core connection standard on LoxBerry, therefore many plugins are using MQTT for communication.

In a potential 'Miele plugin', MQTT broker settings are directly fetched from the LoxBerry-Core settings, also language and country settings. The user would have to provide his user/pass, client id and client secret in a webinterface provided by the plugin. On Save in the webif, my plugin would create/update the config file and restart the miele-MQTT.php script. Users of the plugin would never get in touch with the miele-MQTT.php wizard.

Regards Christian

PS: Sorry, needed to remove my screenshot, as with token anyone would be possible to open my main door 😉

oklona commented 3 years ago

Thanks. I have looked through your file and compared it to mine. I will try to make it more the way I mentioned in my last post, hopefully later this week.

christianTF commented 3 years ago

LoxBerry Screenshots:

LoxBerry System Settings image

LoxBerry installed plugins (I have many, as I develop some of them) image

LoxBerry MQTT Gateway This are the MQTT Gateway settings, that interfaces MQTT messages to Loxone http/udp (the only that Loxone understands), and from Loxone udp to MQTT. image image

oklona commented 3 years ago

Nice :-) -And then I assume, if you create a Miele-plugin, you will create a similar web form to fill in information for it? (Probably pick up broker information from the central storage of LoxBerry.) So the big question is how you plan to move information from a "plugin configuration webform" to Miele-config2.php. The simplest for you might actually be to create an interface similar to this: https://home.hasslers.net/teslaapi/ (This is for Tesla API tokens, and I don't know if the service is trustworthy.) and then actually add the auth_token and refresh_token to the config file through your web form.

I will still seek to split the functions within my script, though, but as it seems you will need a configuration page anyways, doing it this way would probably be the best.

christianTF commented 3 years ago

I will do the WebIf in PHP, giving me the possibility to directly include your config file to show current values. Other plugins use a json config, or ini-style, or are directly called with command line parameters. In fact, it may be the easiest way to do the initial authentication directly via AJAX in the WebIf, and write the result to your Config file.

Is this ok to implement your script to a LoxBerry Plugin? Issues may increase! 5000 LoxBerrys are out there, many of them have Miele devices 🙂

oklona commented 3 years ago

Of course it is OK. My hope was always that someone would find this useful :-) If there are too many issues, I might be slow to fix, but that just means someone lese will need to try to find solutions. The most difficult parts are when there are issues with devices that I don't own (which is "most devices"). -Since then, I need to rely on other people to provide information and help me debug.

christianTF commented 3 years ago

I will create Debug/Dump buttons in the WebIf to simplify getting a meaningful output. It will also take a while to create a Plugin, as most effort is error handling (how to validate and tell the user what he has done wrong). LoxBerry users usually aren't IT Pro's. I think the combination of your script together with a LoxBerry Plugin will be a killer feature.

oklona commented 3 years ago

Cool! That is a great idea! Sounds great! I assume LoxBerry requires the user to own a Loxone Miniserver or similar?

christianTF commented 3 years ago

You don't need a Loxone Miniserver for LoxBerry, but plugins mostly are made to receive or transmit data to a Loxone Miniserver.

This is a Virtual Box image I've created (LoxBerry 2.2.0.1). https://download.loxberry.de/testing/images/virtualbox/Loxberry-vm-virtualbox-2.2.0.2-buster.7z LoxBerry is web-based from your PCs browser - no UI on the machine itself.

After inital boot, connect to http://loxberry and skip the wizard, wait some time (10 mins), as Debian will automatically do security updates in the background. Without using the wizard, SSH and web user is loxberry / PW loxberry; root also pass loxberry (direct putty login for root is disabled; logon with loxberry and su). Default SecurePIN is 0000.

Install the plugin MQTT Gateway, that is required for other plugins that are using MQTT.

https://www.loxwiki.eu/pages/viewpage.action?pageId=27100376 https://www.loxwiki.eu/display/LOXBERRY/First+steps https://www.loxwiki.eu/pages/viewpage.action?pageId=27100429