Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve thing initialization and enable thing-type updates #3330

Merged
merged 11 commits into from
Feb 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -206,18 +206,14 @@ public static Object normalizeType(Object value, @Nullable ConfigDescriptionPara
* @param configuration the configuration to be normalized
* @param configDescriptions the configuration descriptions that should be applied (must not be empty).
* @return the normalized configuration or null if given configuration was null
* @throws IllegalArgumentExcetpion if given config description is null
* @throws IllegalArgumentException if given config description is null
*/
public static @Nullable Map<String, Object> normalizeTypes(@Nullable Map<String, Object> configuration,
public static Map<String, Object> normalizeTypes(Map<String, Object> configuration,
List<ConfigDescription> configDescriptions) {
if (configDescriptions.isEmpty()) {
throw new IllegalArgumentException("Config description must not be empty.");
}

if (configuration == null) {
return null;
}

Map<String, Object> convertedConfiguration = new HashMap<>();

Map<String, ConfigDescriptionParameter> configParams = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,8 @@ min_value_numeric_violated=The value must not be less than {0}.
pattern_violated=The value {0} does not match the pattern {1}.
options_violated=The value {0} does not match allowed parameter options. Allowed options are: {1}
multiple_limit_violated=Only {0} elements are allowed but {1} are provided.

bridge_not_configured=Configuring a bridge is mandatory.
type_description_missing=Type description for '{0}' not found also we checked the presence before.
config_description_missing=Config description for '{0}' not found also we checked the presence before.

49 changes: 49 additions & 0 deletions bundles/org.openhab.core.thing/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
<artifactId>org.openhab.core.io.console</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<version>2.3.3</version>
</dependency>
<dependency>
<groupId>org.openhab.core.bundles</groupId>
<artifactId>org.openhab.core.test</artifactId>
Expand All @@ -33,4 +38,48 @@
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.15.2</version>
<configuration>
<schemaDirectory>src/main/resources/xsd</schemaDirectory>
<noFileHeader>true</noFileHeader>
<locale>en</locale>
<episode>false</episode>
<extension>true</extension>
<args>
<arg>-Xxew</arg>
<arg>-Xxew:instantiate early</arg>
</args>
<plugins>
<plugin>
<groupId>com.github.jaxb-xew-plugin</groupId>
<artifactId>jaxb-xew-plugin</artifactId>
<version>1.10</version>
</plugin>
</plugins>
</configuration>
<dependencies>
<dependency>
<!-- Required for JDK 17 compatibility, see: /~https://github.com/highsource/maven-jaxb2-plugin/issues/207 -->
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.3.6</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>generate-jaxb-sources</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
@NonNullByDefault
public enum ThingStatusDetail {
NONE,
NOT_YET_READY,
HANDLER_MISSING_ERROR,
HANDLER_REGISTERING_ERROR,
HANDLER_INITIALIZING_ERROR,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
Expand Down Expand Up @@ -74,6 +76,19 @@ public static ThingBuilder create(ThingTypeUID thingTypeUID, ThingUID thingUID)
return new ThingBuilder(thingTypeUID, thingUID);
}

/**
* Create a new thing {@link ThingBuilder} for a copy of the given thing
*
* @param thing the {@link Thing} to create this builder from
* @return the created {@link ThingBuilder}
*
*/
public static ThingBuilder create(Thing thing) {
return ThingBuilder.create(thing.getThingTypeUID(), thing.getUID()).withBridge(thing.getBridgeUID())
.withChannels(thing.getChannels()).withConfiguration(thing.getConfiguration())
.withLabel(thing.getLabel()).withLocation(thing.getLocation()).withProperties(thing.getProperties());
}

/**
* Build the thing
*
Expand Down Expand Up @@ -200,6 +215,20 @@ public ThingBuilder withBridge(@Nullable ThingUID bridgeUID) {
return this;
}

/**
* Set / replace a single property for this thing
*
* @param key the key / name of the property
* @param value the value of the property
* @return the {@link ThingBuilder} itself
*/
public ThingBuilder withProperty(String key, String value) {
Map<String, String> oldProperties = Objects.requireNonNullElse(this.properties, Map.of());
Map<String, String> newProperties = new HashMap<>(oldProperties);
newProperties.put(key, value);
return withProperties(newProperties);
}

/**
* Set/replace the properties for this thing
*
Expand Down
Loading