YAML File-based Pages and Widgets

Inspired by this thread, I’ve started working on adding support for pages into our yaml file config:

CONF/yaml/testpage.yaml

version: 1

pages:
  testpage:
    config:
      label: yaml page
    component: oh-layout-page
    slots:
      default:
        - component: oh-block
          # config: {}
          slots:
            default:
              - component: oh-grid-row
                # config: {}
                slots:
                  default:
                    - component: oh-grid-col
                      # config: {}
                      slots:
                        default:
                          - component: oh-clock-card
                            # config: {}

Once this is in, we can add yaml file-based widgets too!

Yeah, the problem is that widgets are already YAML. They just describe a different structure than what “the new YAML format” does. The current widgets describe the actual object structure (directly translated from JSON), while “the new YAML format” is something undefined, where structure is altered so that (some) people think the structure is easier to handle. Thus, it’s not really possible to make “new YAML” anything without long discussions about what these “alternative structures” should be.

Thanks for bringing this up! The file-based yaml would be almost the same as the current marketplace widget, with the exception that:

  • It has no uid field. Instead, it turned into the key, so from:
uid: mywidget
<the rest is the same>

into:

version: 1
widgets:
  mywidget: # <- THIS IS THE UID in the file-based yaml
    <the rest is the same - but with 2 extra indentation levels>

That would be an easy solution, but I’m guessing that some people will want to turn everything that can possibly be changed from arrays to maps/objects, into maps/objects.

Keeping the “inner structure” the same would make it very easy to “convert” widgets, so I would definitely prefer that. Also, all the “turning arrays into maps” we do, must be undone again before the UI can actually use the widgets.

As I described above is already done.

  • Just convert the uid
  • Indent the rest

That is all. No other array to map conversions.

It works.

I’m confused. Of course it works, the “old YAML format” for rules and rule templates worked as well. It’s not about that, it’s about the idea that “using maps is more user-friendly”.

I’m confused as well. Are you suggesting that for this topic (file-based widget) something needs to be changed from what I have described above?

We had a working YAML system for rules and rule templates. The reason it had to be done “all over again” was to make it fit into “the spirit” of “the new YAML format”. The primary change was to turn arrays into maps where possible, in addition to shortening some terms (like config instead of configuration).

What I’m saying is that I expect that it will be demanded that a similar “transformation” takes place for pages and widgets. I would have preferred if the YAML reflected the actual structure of the data, the way the “old YAML” did, but that wasn’t the consensus. So, I assume that the same applies here.

This is speculation at this point, right?

At the moment, I’ve got a draft PR implementing it as I described

  • literal copy paste from existing widget yaml
  • add 2 extra indentation levels
  • convert uid to become the key under widgets section.

I don’t see any possible reason for converting any other “transformations” from arrays to maps to complicate this - but we’ll see how it goes. This probably won’t make it into 5.2 anyway, even though I live in hope.

Yes. As I’ve said, I prefer that the representation is as close as possible to the actual data structure, it just makes things easier on every level.

This whole issue was basically about figuring out what the “user friendlier” structure for rules and rule templates should look like:

OK that’s why I was confused. Let’s not continue with the speculation. It serves no productive purposes here, unless you are actually asking for this change, in which case feel free to do so in the PR review.

It was meant as a heads-up: It probably won’t be that simple.

I have no idea what all of this is about but I hope it doesn’t end up like home assistant yaml nightmare. :slightly_smiling_face:

Can you explain what the nightmare was so we can try to avoid it?

Every time I look at a home assistant youtube it just seems to be a big mess of yaml code. Every change they do you have to check the syntax and then restart home assistant as far as I could tell.
I haven’t really used home assistant but just by the videos is enough reason not to use it. It may have lots more support etc than openHab but I will stick with openHab. I don’t use the file options and ditched those in openHab 3 and also ditched DSL. I just use the UI and javascript.
I am not doing anything fancy so my system is fairly simple.

In my opinion, YAML is a terrible format, like all whitespace based formats. But, it has one good thing going for it, and that is the multiline strings. That makes it possible to embed scripts that are still readable. Except for that, not a lot of good to say about it from me.

That said, OH’s YAML format is versioned, so the idea is that it files won’t “break”. If breaking changes are done to the format, the version is bumped, which means that those using the old version will keep working as they did. That should avoid some of “the nightmare”.

Yes every time you change home assistant yaml, you have to restart home assistant. They don’t have an auto reloading system like Openhab.

I’ve adjusted the MainUI PR so that the YAML from the Code tab in the MainUI can be copy pasted into a file-based yaml and you’d only need to adjust the indentation level.

We can go one step further and make it the same as things/items: the code tab shows a full file syntax including version: 1, pages: - but I think I’ll leave that for a future PR since that’s another can of worms.

The same works for widgets too.

With the help of this pending add-on [yamlcomposer] YAML Composer Add-on for Enhanced YAML Preprocessing by jimtng · Pull Request #20305 · openhab/openhab-addons · GitHub one can even download a marketplace widget into a file, say, CONF/widgets/daikin_hvac.yaml, and then load it in the composer file like this without having to adjust any indentation.

widgets:
  daikin-controls: !include @widgets/daikin_hvac.yaml

This is because the syntax for the widget is exactly the same. When there is a new version of the widget on the marketplace, simply redownload / overwrite the widget file. The addon will automatically reload the yaml file too!

I considered also supporting remote inclusion (with local caching) in the addon, but not quite sure if it’s a good idea. In any case that’s a separate topic.

It’s a bit unpractical to have “version++” (and indentation) inserted when working with them, extra indentations makes YAML much harder to work with in the UI editor too. I think that a better option might be a button with “Export to YAML” (or whatever), and when you click that button, you get the “extras” included. So, if you just want to work with it, you work with it without the extras, if you want to store it to a file, you use the button.