Read system settings

I tried to find it through the forum, but could not so I’m asking here. How can i get the system settings of openHAB, like unit settings and choosen currency provider inside a binding?

You can use low level api - see ConfigurationAdmin. PID you looking for is org.openhab.i18n.
This is configuration object used by I18nService which supplies LocaleProvider implementation.

Thanks, do you know of any example in any binding that implements this? So I can have a look at that…

Not sure what your goal is, getting configuration is one thing, making sense out of it is another.

You can have a look at hdpowerview binding. It makes use of TranslationProvider and LocaleProvider, without necessity to read configuration of these.

For units you can use org.openhab.core.i18n.UnitProvider#getMeasurementSystem.
For currencies you try to use org.openhab.core.internal.library.unit.CurrencyService, however it does not provide a direct way to detect system currency. For that you can use org.openhab.core.internal.library.unit.LocaleBasedCurrencyProvider.

In principle any of above interfaces can be sourced through @Reference annotations at ThingHandlerFactory and pushed to your ThingHandler implementations.

Thanks! My goal is to check if there are set a default currency provider in the system, and the name of that currency provider. So I have tried with LocaleBasedCurrencyProvider.

Injecting it in my handlerfactory-class. It seems like I’ve run into a dependency-problem…?

When i try to compile and run I get this error:

[ERROR] Failed to execute goal org.apache.karaf.tooling:karaf-maven-plugin:4.4.6:verify (karaf-feature-verification) on project org.openhab.binding.entsoe: Feature resolution failed for [openhab-binding-entsoe/4.3.0.SNAPSHOT]
[ERROR] Message: Unable to resolve root: missing requirement [root] osgi.identity; osgi.identity=openhab-binding-entsoe; type=karaf.feature; version=4.3.0.SNAPSHOT; filter:="(&(osgi.identity=openhab-binding-entsoe)(type=karaf.feature)(version>=4.3.0.SNAPSHOT))" [caused by: Unable to resolve openhab-binding-entsoe/4.3.0.SNAPSHOT: missing requirement [openhab-binding-entsoe/4.3.0.SNAPSHOT] osgi.identity; osgi.identity=org.openhab.binding.entsoe; type=osgi.bundle; version="[4.3.0.202408240957,4.3.0.202408240957]"; resolution:=mandatory [caused by: Unable to resolve org.openhab.binding.entsoe/4.3.0.202408240957: missing requirement [org.openhab.binding.entsoe/4.3.0.202408240957] osgi.wiring.package; filter:="(osgi.wiring.package=org.openhab.core.internal.library.unit)"]]

So I tried to add dependencies like this to pom.xml:

  <dependencies>
    <dependency>
      <groupId>org.openhab.core</groupId>
      <artifactId>org.openhab.core.library</artifactId>
      <version>4.3.0.SNAPSHOT</version>
    </dependency>
  </dependencies>

That results in this error:

[ERROR] Failed to execute goal on project org.openhab.binding.entsoe: Could not resolve dependencies for project org.openhab.addons.bundles:org.openhab.binding.entsoe:jar:4.3.0-SNAPSHOT: The following artifacts could not be resolved: org.openhab.core:org.openhab.core.library:jar:4.3.0.SNAPSHOT (absent): org.openhab.core:org.openhab.core.library:jar:4.3.0.SNAPSHOT was not found in https://openhab.jfrog.io/openhab/libs-snapshot during a previous attempt. This failure was cached in the local repository and resolution is not reattempted until the update interval of openhab-snapshot has elapsed or updates are forced -> [Help 1]

I have tried reading up in the forum and addon examples but not being able to find a solution unfortunately.

This provider remains in internal (non exported) package, so you can’t use it directly.

Instead you need to use wiring:

@Reference(
  cardinality=ReferenceCardinality.OPTIONAL,
  target = "(service.pid=org.openhab.localebasedcurrency)" // or
  target = "(component.name=org.openhab.core.internal.library.unit.LocaleBasedCurrencyProvider)"
)
CurrencyProvider currencyProvider;

The cardinality attribute makes this reference optional, so your service will not wait for this element to become available. You can remove this attribute, then this dependency will be mandatory.

Thanks, tried, still get this error:

[ERROR] Failed to execute goal org.apache.karaf.tooling:karaf-maven-plugin:4.4.6:verify (karaf-feature-verification) on project org.openhab.binding.entsoe: Feature resolution failed for [openhab-binding-entsoe/4.3.0.SNAPSHOT]
[ERROR] Message: Unable to resolve root: missing requirement [root] osgi.identity; osgi.identity=openhab-binding-entsoe; type=karaf.feature; version=4.3.0.SNAPSHOT; filter:="(&(osgi.identity=openhab-binding-entsoe)(type=karaf.feature)(version>=4.3.0.SNAPSHOT))" [caused by: Unable to resolve openhab-binding-entsoe/4.3.0.SNAPSHOT: missing requirement [openhab-binding-entsoe/4.3.0.SNAPSHOT] osgi.identity; osgi.identity=org.openhab.binding.entsoe; type=osgi.bundle; version="[4.3.0.202408241033,4.3.0.202408241033]"; resolution:=mandatory [caused by: Unable to resolve org.openhab.binding.entsoe/4.3.0.202408241033: missing requirement [org.openhab.binding.entsoe/4.3.0.202408241033] osgi.wiring.package; filter:="(osgi.wiring.package=org.openhab.core.internal.library.unit)"]]

Here is that part of my handlerfactory-class:

@NonNullByDefault
@Component(configurationPid = "binding.entsoe", service = ThingHandlerFactory.class)
public class entsoeHandlerFactory extends BaseThingHandlerFactory {
    private LocaleBasedCurrencyProvider currencyProvider;

    @Activate
    public entsoeHandlerFactory(
            @Reference(cardinality = ReferenceCardinality.OPTIONAL, target = "(service.pid=org.openhab.localebasedcurrency)") LocaleBasedCurrencyProvider currencyProvider) {
        this.currencyProvider = currencyProvider;
    }

What could be the issue?

Look if source code have used or unused imports of org.openhab.core.internal.library.unit. You have some reference which causes failure. In order to pass feature verification your bundle need to rely only on public packages.

Change
@Reference(cardinality = ReferenceCardinality.OPTIONAL, target = "(service.pid=org.openhab.localebasedcurrency)") LocaleBasedCurrencyProvider currencyProvider) {
To
@Reference(cardinality = ReferenceCardinality.OPTIONAL, target = "(service.pid=org.openhab.localebasedcurrency)") CurrencyProvider currencyProvider) {

The getBaseCurrency method is defined at interface level, so you don’t need to rely on a specific type.

Brilliant, that worked, thanks a lot! :smiley:

1 Like