openhab-core icon indicating copy to clipboard operation
openhab-core copied to clipboard

Yaml/DSL Thing provider didn't check for existence of a Thing before adding one

Open jimtng opened this issue 6 months ago • 11 comments
trafficstars

The Yaml Thing provider doesn't check if a Thing with the same ID already exists in the Thing Registry (i.e. registered by another provider) before adding one to its "provider".

Steps:

  • Create a Thing in Main UI, e.g. astro:moon:test1
  • Create a yaml file and create the same UID (thus duplicating it)
version: 2
things:
  astro:moon:test1:
    config:
      geolocation: "1,2"

Save this file. This is the step where the problem occurred. It "thinks" that it had registered this UID when in fact it failed (or did create a duplicate uid?)

  • Edit the yaml file, and change the uid to astro:moon:test2 At this point it tried to remove the previous "test1" but failed with the following error

You'll see

Provider 'YamlThingProvider' is not allowed to remove element 'ThingImpl' with key 'astro:moon:test1' from the registry because it was added by provider 'ManagedThingProvider'.

It should've logged a warning and refuse to add an existing thing id in the first place.

Originally posted by @jimtng in https://github.com/openhab/openhab-core/issues/4793#issuecomment-2871160585

jimtng avatar May 13 '25 06:05 jimtng

@lolodomo, continuing here:

have to check the checks in thing registry but I agree it would be better to have a message when first adding a thing that already existed through the UI managed provider. It is the load of the updated YAML file that triggers the thing removal in the registry.

Is it different when you use DSL thing ? I assume it is not.

I just tried, the DSL suffered from the same problem. So I guess this isn't a specific issue to the Yaml Thing provider.

jimtng avatar May 13 '25 06:05 jimtng

Can you tell us if it is different when you use a DSL configuration file instead of a YAML file?

lolodomo avatar May 13 '25 06:05 lolodomo

So I guess this isn't a specific issue to the Yaml Thing provider.

Exactly so please adjust your issue title

This is like that probably since more than 10 years ;)

lolodomo avatar May 13 '25 06:05 lolodomo

The problem is not at thing provider level but at thing registry level.

lolodomo avatar May 13 '25 06:05 lolodomo

Each provider provides its things to the registry independtly of each other. This is the thing registry which then handles data provided by all providers.

So you can have things with same UID provided by the UI provider, the DSL provider and now the YAML provider. The thing registry will keep only one and should log warnings.

lolodomo avatar May 13 '25 06:05 lolodomo

The log is generated by the abstract registry so for any registry: https://github.com/openhab/openhab-core/blob/e9700f85ec844946926936075fdb8c093d5068eb/bundles/org.openhab.core/src/main/java/org/openhab/core/common/registry/AbstractRegistry.java#L252 This log makes sense.

There is also a log when adding something that already exists but it is at DEBUG level: https://github.com/openhab/openhab-core/blob/e9700f85ec844946926936075fdb8c093d5068eb/bundles/org.openhab.core/src/main/java/org/openhab/core/common/registry/AbstractRegistry.java#L195 So maybe we should increase level of this one (and check that the provider is different) and reduce level of the previous?

lolodomo avatar May 13 '25 06:05 lolodomo

if there are multiple Things, won't there be multiple instances of ThingHandler (created by the binding) for the same UID? I think we need to prevent that from happening

jimtng avatar May 13 '25 06:05 jimtng

There will be a thing handler only for things in the thing registry and you can't have duplicates in the registry.

lolodomo avatar May 13 '25 06:05 lolodomo

There will be a thing handler only for things in the thing registry and you can't have duplicates in the registry.

that's good. So the only problem is to warn users that their Thing in Yaml / DSL (or any other sources for that matter) isn't applied / loaded. Right now they wouldn't know that a duplicate exists during load time.

jimtng avatar May 13 '25 07:05 jimtng

Right now they wouldn't know that a duplicate exists during load time.

Yes because only DEBUG log.

lolodomo avatar May 13 '25 07:05 lolodomo

I removed my previous message as I made a mismatch between thing handler and thing handler factory.. The providers don't instantiate thing handlers, they just create things that can be finally added to the registry or ignored by the registry if the thing with same UID already exist in the registry.

lolodomo avatar May 13 '25 11:05 lolodomo