Allow ThingHandlers to dynamically register services (#4719)

Signed-off-by: Cody Cutrer <cody@cutrer.us>
pull/4732/head
Cody Cutrer 2025-04-20 00:52:23 -06:00 committed by GitHub
parent 2ec59cdebf
commit 5c42be4861
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 17 additions and 0 deletions

View File

@ -161,6 +161,23 @@ public abstract class BaseThingHandlerFactory implements ThingHandlerFactory {
}
}
/**
* Registers a dynamic service for the given thing handler. The service must implement ThingHandlerService.
*
* @param thingHandler The thing handler requesting a service to be registered.
* @param klass The service class to register.
*/
public void registerService(ThingHandler thingHandler, Class<? extends ThingHandlerService> klass) {
ThingUID thingUID = thingHandler.getThing().getUID();
if (!ThingHandlerService.class.isAssignableFrom(klass)) {
logger.warn(
"Should register service={} for thingUID={}, but it does not implement the interface ThingHandlerService.",
klass.getCanonicalName(), thingUID);
return;
}
registerThingHandlerService(thingUID, thingHandler, klass);
}
private <T extends ThingHandlerService> void registerThingHandlerService(ThingUID thingUID,
ThingHandler thingHandler, Class<T> c) {
RegisteredThingHandlerService<T> registeredService;