Skip to content

Commit 5c42be4

Browse files
authored
Allow ThingHandlers to dynamically register services (#4719)
Signed-off-by: Cody Cutrer <[email protected]>
1 parent 2ec59cd commit 5c42be4

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

Diff for: bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/binding/BaseThingHandlerFactory.java

+17
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,23 @@ private void registerServices(Thing thing, ThingHandler thingHandler) {
161161
}
162162
}
163163

164+
/**
165+
* Registers a dynamic service for the given thing handler. The service must implement ThingHandlerService.
166+
*
167+
* @param thingHandler The thing handler requesting a service to be registered.
168+
* @param klass The service class to register.
169+
*/
170+
public void registerService(ThingHandler thingHandler, Class<? extends ThingHandlerService> klass) {
171+
ThingUID thingUID = thingHandler.getThing().getUID();
172+
if (!ThingHandlerService.class.isAssignableFrom(klass)) {
173+
logger.warn(
174+
"Should register service={} for thingUID={}, but it does not implement the interface ThingHandlerService.",
175+
klass.getCanonicalName(), thingUID);
176+
return;
177+
}
178+
registerThingHandlerService(thingUID, thingHandler, klass);
179+
}
180+
164181
private <T extends ThingHandlerService> void registerThingHandlerService(ThingUID thingUID,
165182
ThingHandler thingHandler, Class<T> c) {
166183
RegisteredThingHandlerService<T> registeredService;

0 commit comments

Comments
 (0)