HomeSeer / Plugin-SDK

Plugin development kit for the 4th major edition of the HomeSeer platform.
https://www.nuget.org/packages/HomeSeer-PluginSDK/
GNU Affero General Public License v3.0
21 stars 5 forks source link

HSPI.LoadSettingsFromIni not loading values from file #111

Closed weirdkid closed 3 years ago

weirdkid commented 4 years ago

Environment and System Config

Describe the issue Created a basic hello world plugin with a settings page where I can enter text into a field. Value is saved to ini file but never loaded on subsequent runs. I was able to trace the problem to the LoadSettingsFromIni method where it calls settingsPage.ToValueMap(). The valuemap is never populated. If you get a count of entries in the pageValueMap it's always 0, and I think that's because the valuemap is based on what is in the settingsPage, not what is in the savedSettings. Views are filtered out when they have no value, and of course they have no value before you load the INI.

But, you can get to the values in the saved settings another way. See my workaround below.

Steps to Reproduce Steps to reproduce the behavior: See above.

Expected behavior Values should be populated from the ini file


Screenshots N/A

Logs N/A

Additional info I've worked around the issue by overriding the LoadSettingsFromIni method. I'm not a C# programmer (first time trying to write a plugin too), so there could be things wrong with this that I am not aware of, but it works for me. I considered forking, modifying, and submitting a pull request, but the latest version of the SDK here on GitHub is older than what I am pulling down with NuGet.

protected new void LoadSettingsFromIni()
        {
            if (LogDebug)
            {
                Console.WriteLine("Loading settings from INI");
            }
            //Get the whole section for settings
            var savedSettings = HomeSeerSystem.GetIniSection(SettingsSectionName, SettingsFileName);
            //Loop through each settings page
            foreach (var settingsPage in Settings)
            {
                // This does not work
                //Build a map of settings on the page
                //var pageValueMap = settingsPage.ToValueMap();

                //Loop through each setting defined on the page
                // and see if there's a matching key in the savedSettings 
                foreach (var view in settingsPage.Views)
                {
                    //If there is a saved value for the setting
                    // Always true after the first time the plugin starts
                    if (savedSettings.ContainsKey(view.Id))
                    {
                        //Pull the saved value into memory
                        if (LogDebug)
                        {
                            Console.WriteLine("Updating view");
                        }
                        string savedValue = "";
                        savedSettings.TryGetValue(view.Id, out savedValue);
                        settingsPage.UpdateViewValueById(view.Id, savedValue);
                        //Go to the next setting
                        continue;
                    }

                    //Save the setting if there is no default value already saved
                    HomeSeerSystem.SaveINISetting(SettingsSectionName,
                                                  view.Id,
                                                  view.GetStringValue(),
                                                  SettingsFileName);
                }
            }
        }
skiingwiz commented 4 years ago

I think this is the same issue that I'm seeing. In my case I noticed that the ini settings were not loaded for some of my settings. It turns out that it's only those settings that don't set a default value. Adding a default value, even an empty string, causes the settings to be loaded successfully.

If I use PageFactory.WithInput("id", "name", ""); the setting loads fine. If I use page.WithInput("id", "name"); then the setting does not load. @weirdkid that's a potentially easier work-around for you, in that it doesn't involve modifying the SDK.

weirdkid commented 3 years ago

Thanks @skiingwiz -- setting a default for all settings fixed the issue for me too.