Skip to content

Commit

Permalink
Add support for QuantityType
Browse files Browse the repository at this point in the history
Signed-off-by: Laurent Garnier <lg.hc@free.fr>
  • Loading branch information
lolodomo committed Oct 2, 2024
1 parent b20bf5d commit 47e8144
Show file tree
Hide file tree
Showing 9 changed files with 196 additions and 122 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import org.openhab.core.config.core.Configuration;
import org.openhab.core.config.core.dto.ConfigDescriptionDTOMapper;
import org.openhab.core.config.core.dto.ConfigDescriptionParameterDTO;
import org.openhab.core.i18n.UnitProvider;
import org.openhab.core.io.rest.LocaleService;
import org.openhab.core.io.rest.RESTConstants;
import org.openhab.core.io.rest.RESTResource;
Expand Down Expand Up @@ -99,15 +100,17 @@ public class ThingActionsResource implements RESTResource {
private final Logger logger = LoggerFactory.getLogger(ThingActionsResource.class);

private final LocaleService localeService;
private final UnitProvider unitProvider;
private final ModuleTypeRegistry moduleTypeRegistry;

Map<ThingUID, Map<String, ThingActions>> thingActionsMap = new ConcurrentHashMap<>();
private List<ModuleHandlerFactory> moduleHandlerFactories = new ArrayList<>();

@Activate
public ThingActionsResource(@Reference LocaleService localeService,
public ThingActionsResource(@Reference LocaleService localeService, @Reference UnitProvider unitProvider,
@Reference ModuleTypeRegistry moduleTypeRegistry) {
this.localeService = localeService;
this.unitProvider = unitProvider;
this.moduleTypeRegistry = moduleTypeRegistry;
}

Expand Down Expand Up @@ -181,7 +184,7 @@ public Response getActions(@PathParam("thingUID") @Parameter(description = "thin
}

List<ConfigDescriptionParameter> inputParameters = ActionInputsToConfigDescriptionParameters
.map(actionType.getInputs());
.map(actionType.getInputs(), unitProvider);
if (inputParameters == null) {
logger.info("Thing action {} has an input with an unsupported type, hiding it in the UI.",
actionType.getUID());
Expand Down Expand Up @@ -240,7 +243,8 @@ public Response executeThingAction(@PathParam("thingUID") @Parameter(description

try {
Map<String, Object> returnValue = Objects.requireNonNullElse(
handler.execute(SerialisedInputsToActionInputs.map(actionType, actionInputs)), Map.of());
handler.execute(SerialisedInputsToActionInputs.map(actionType, actionInputs, unitProvider)),
Map.of());
moduleHandlerFactory.ungetHandler(action, ruleUID, handler);
return Response.ok(returnValue).build();
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public AnnotationActionHandler(Action module, ActionType mt, Method method, Obje
// fallback to configuration as this is where the UI stores the input values
if (value == null) {
value = SerialisedInputsToActionInputs.map(moduleType, moduleType.getInputs().get(i),
module.getConfiguration().get(inputAnnotation.name()));
module.getConfiguration().get(inputAnnotation.name()), null);
}
args.add(i, value);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.openhab.core.automation.type.ModuleType;
import org.openhab.core.automation.type.ModuleTypeProvider;
import org.openhab.core.common.registry.ProviderChangeListener;
import org.openhab.core.i18n.UnitProvider;
import org.osgi.framework.Bundle;
import org.osgi.framework.FrameworkUtil;
import org.osgi.service.component.annotations.Activate;
Expand All @@ -62,10 +63,13 @@ public class AnnotatedActionModuleTypeProvider extends BaseModuleHandlerFactory
private final AnnotationActionModuleTypeHelper helper = new AnnotationActionModuleTypeHelper();

private final ModuleTypeI18nService moduleTypeI18nService;
private final UnitProvider unitProvider;

@Activate
public AnnotatedActionModuleTypeProvider(final @Reference ModuleTypeI18nService moduleTypeI18nService) {
public AnnotatedActionModuleTypeProvider(final @Reference ModuleTypeI18nService moduleTypeI18nService,
final @Reference UnitProvider unitProvider) {
this.moduleTypeI18nService = moduleTypeI18nService;
this.unitProvider = unitProvider;
}

@Override
Expand All @@ -88,7 +92,7 @@ public void removeProviderChangeListener(ProviderChangeListener<ModuleType> list
public Collection<ModuleType> getAll() {
Collection<ModuleType> moduleTypes = new ArrayList<>();
for (String moduleUID : moduleInformation.keySet()) {
ModuleType mt = helper.buildModuleType(moduleUID, moduleInformation);
ModuleType mt = helper.buildModuleType(moduleUID, moduleInformation, unitProvider);
if (mt != null) {
moduleTypes.add(mt);
}
Expand Down Expand Up @@ -121,7 +125,7 @@ public <T extends ModuleType> Collection<T> getModuleTypes(@Nullable Locale loca
ModuleInformation mi = mis.iterator().next();

Bundle bundle = FrameworkUtil.getBundle(mi.getActionProvider().getClass());
ModuleType mt = helper.buildModuleType(uid, moduleInformation);
ModuleType mt = helper.buildModuleType(uid, moduleInformation, unitProvider);
return moduleTypeI18nService.getModuleTypePerLocale(mt, locale, bundle);
}
return null;
Expand All @@ -138,7 +142,7 @@ public void addActionProvider(AnnotatedActions actionProvider, Map<String, Objec

ModuleType oldType = null;
if (moduleInformation.containsKey(mi.getUID())) {
oldType = helper.buildModuleType(mi.getUID(), moduleInformation);
oldType = helper.buildModuleType(mi.getUID(), moduleInformation, unitProvider);
Set<ModuleInformation> availableModuleConfigs = moduleInformation.get(mi.getUID());
availableModuleConfigs.add(mi);
} else {
Expand All @@ -147,7 +151,7 @@ public void addActionProvider(AnnotatedActions actionProvider, Map<String, Objec
moduleInformation.put(mi.getUID(), configs);
}

ModuleType mt = helper.buildModuleType(mi.getUID(), moduleInformation);
ModuleType mt = helper.buildModuleType(mi.getUID(), moduleInformation, unitProvider);
if (mt != null) {
for (ProviderChangeListener<ModuleType> l : changeListeners) {
if (oldType != null) {
Expand All @@ -172,13 +176,13 @@ public void removeActionProvider(AnnotatedActions actionProvider, Map<String, Ob
Set<ModuleInformation> availableModuleConfigs = moduleInformation.get(mi.getUID());
if (availableModuleConfigs != null) {
if (availableModuleConfigs.size() > 1) {
oldType = helper.buildModuleType(mi.getUID(), moduleInformation);
oldType = helper.buildModuleType(mi.getUID(), moduleInformation, unitProvider);
availableModuleConfigs.remove(mi);
} else {
moduleInformation.remove(mi.getUID());
}

ModuleType mt = helper.buildModuleType(mi.getUID(), moduleInformation);
ModuleType mt = helper.buildModuleType(mi.getUID(), moduleInformation, unitProvider);
// localize moduletype -> remove from map
if (mt != null) {
for (ProviderChangeListener<ModuleType> l : changeListeners) {
Expand Down Expand Up @@ -214,7 +218,8 @@ public Collection<String> getTypes() {
ModuleInformation finalMI = helper.getModuleInformationForIdentifier(actionModule, moduleInformation,
false);
if (finalMI != null) {
ActionType moduleType = helper.buildModuleType(module.getTypeUID(), moduleInformation);
ActionType moduleType = helper.buildModuleType(module.getTypeUID(), moduleInformation,
unitProvider);
if (moduleType == null) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import org.openhab.core.config.core.ConfigDescriptionParameterBuilder;
import org.openhab.core.config.core.Configuration;
import org.openhab.core.config.core.ParameterOption;
import org.openhab.core.i18n.UnitProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -145,7 +146,8 @@ private List<Output> getOutputsFromAction(Method method) {
return outputs;
}

public @Nullable ActionType buildModuleType(String uid, Map<String, Set<ModuleInformation>> moduleInformation) {
public @Nullable ActionType buildModuleType(String uid, Map<String, Set<ModuleInformation>> moduleInformation,
UnitProvider unitProvider) {
Set<ModuleInformation> mis = moduleInformation.get(uid);
List<ConfigDescriptionParameter> configDescriptions = new ArrayList<>();

Expand Down Expand Up @@ -174,7 +176,7 @@ private List<Output> getOutputsFromAction(Method method) {
if (kind == ActionModuleKind.THING) {
// we have a Thing module, so we have to map the inputs to config description parameters for the UI
List<ConfigDescriptionParameter> inputConfigDescriptions = ActionInputsToConfigDescriptionParameters
.map(mi.getInputs());
.map(mi.getInputs(), unitProvider);
if (inputConfigDescriptions != null) {
// all inputs have a supported type
configDescriptions.addAll(inputConfigDescriptions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.openhab.core.automation.type.ModuleType;
import org.openhab.core.automation.type.ModuleTypeProvider;
import org.openhab.core.common.registry.ProviderChangeListener;
import org.openhab.core.i18n.UnitProvider;
import org.openhab.core.thing.binding.ThingActions;
import org.openhab.core.thing.binding.ThingActionsScope;
import org.openhab.core.thing.binding.ThingHandler;
Expand Down Expand Up @@ -66,10 +67,13 @@ public class AnnotatedThingActionModuleTypeProvider extends BaseModuleHandlerFac
private final AnnotationActionModuleTypeHelper helper = new AnnotationActionModuleTypeHelper();

private final ModuleTypeI18nService moduleTypeI18nService;
private final UnitProvider unitProvider;

@Activate
public AnnotatedThingActionModuleTypeProvider(final @Reference ModuleTypeI18nService moduleTypeI18nService) {
public AnnotatedThingActionModuleTypeProvider(final @Reference ModuleTypeI18nService moduleTypeI18nService,
final @Reference UnitProvider unitProvider) {
this.moduleTypeI18nService = moduleTypeI18nService;
this.unitProvider = unitProvider;
}

@Override
Expand All @@ -92,7 +96,7 @@ public void removeProviderChangeListener(ProviderChangeListener<ModuleType> list
public Collection<ModuleType> getAll() {
Collection<ModuleType> moduleTypes = new ArrayList<>();
for (String moduleUID : moduleInformation.keySet()) {
ModuleType mt = helper.buildModuleType(moduleUID, moduleInformation);
ModuleType mt = helper.buildModuleType(moduleUID, moduleInformation, unitProvider);
if (mt != null) {
moduleTypes.add(mt);
}
Expand Down Expand Up @@ -125,7 +129,7 @@ public <T extends ModuleType> Collection<T> getModuleTypes(@Nullable Locale loca
ModuleInformation mi = mis.iterator().next();

Bundle bundle = FrameworkUtil.getBundle(mi.getActionProvider().getClass());
ModuleType mt = helper.buildModuleType(uid, moduleInformation);
ModuleType mt = helper.buildModuleType(uid, moduleInformation, unitProvider);
return moduleTypeI18nService.getModuleTypePerLocale(mt, locale, bundle);
}
return null;
Expand All @@ -145,7 +149,7 @@ public void addAnnotatedThingActions(ThingActions annotatedThingActions) {

ModuleType oldType = null;
if (moduleInformation.containsKey(mi.getUID())) {
oldType = helper.buildModuleType(mi.getUID(), moduleInformation);
oldType = helper.buildModuleType(mi.getUID(), moduleInformation, unitProvider);
Set<ModuleInformation> availableModuleConfigs = moduleInformation.get(mi.getUID());
availableModuleConfigs.add(mi);
} else {
Expand All @@ -154,7 +158,7 @@ public void addAnnotatedThingActions(ThingActions annotatedThingActions) {
moduleInformation.put(mi.getUID(), configs);
}

ModuleType mt = helper.buildModuleType(mi.getUID(), moduleInformation);
ModuleType mt = helper.buildModuleType(mi.getUID(), moduleInformation, unitProvider);
if (mt != null) {
for (ProviderChangeListener<ModuleType> l : changeListeners) {
if (oldType != null) {
Expand Down Expand Up @@ -184,14 +188,14 @@ public void removeAnnotatedThingActions(ThingActions annotatedThingActions) {

Set<ModuleInformation> availableModuleConfigs = moduleInformation.get(mi.getUID());
if (availableModuleConfigs != null) {
ModuleType oldType = helper.buildModuleType(mi.getUID(), moduleInformation);
ModuleType oldType = helper.buildModuleType(mi.getUID(), moduleInformation, unitProvider);
if (availableModuleConfigs.size() > 1) {
availableModuleConfigs.remove(mi);
} else {
moduleInformation.remove(mi.getUID());
}

ModuleType mt = helper.buildModuleType(mi.getUID(), moduleInformation);
ModuleType mt = helper.buildModuleType(mi.getUID(), moduleInformation, unitProvider);
// localize moduletype -> remove from map
if (oldType != null) {
for (ProviderChangeListener<ModuleType> l : changeListeners) {
Expand Down Expand Up @@ -231,7 +235,8 @@ public Collection<String> getTypes() {
ModuleInformation finalMI = helper.getModuleInformationForIdentifier(actionModule, moduleInformation,
true);
if (finalMI != null) {
ActionType moduleType = helper.buildModuleType(module.getTypeUID(), moduleInformation);
ActionType moduleType = helper.buildModuleType(module.getTypeUID(), moduleInformation,
unitProvider);
if (moduleType == null) {
return null;
}
Expand Down
Loading

0 comments on commit 47e8144

Please sign in to comment.