4545import org .openhab .core .model .item .BindingConfigReader ;
4646import org .openhab .core .model .items .ItemModel ;
4747import org .openhab .core .model .items .ModelBinding ;
48- import org .openhab .core .model .items .ModelGroupFunction ;
49- import org .openhab .core .model .items .ModelGroupItem ;
5048import org .openhab .core .model .items .ModelItem ;
51- import org .openhab .core .model .items .ModelNormalItem ;
5249import org .openhab .core .types .StateDescriptionFragment ;
5350import org .openhab .core .types .StateDescriptionFragmentBuilder ;
5451import org .openhab .core .types .StateDescriptionFragmentProvider ;
@@ -222,48 +219,36 @@ private void processBindingConfigsFromModel(String modelName, EventType type) {
222219 }
223220
224221 private @ Nullable Item createItemFromModelItem (ModelItem modelItem ) {
225- Item item ;
226- if (modelItem instanceof ModelGroupItem modelGroupItem ) {
227- Item baseItem ;
228- try {
229- baseItem = createItemOfType (modelGroupItem .getType (), modelGroupItem .getName ());
230- } catch (IllegalArgumentException e ) {
231- logger .debug ("Error creating base item for group item '{}', item will be ignored: {}" ,
232- modelGroupItem .getName (), e .getMessage ());
233- return null ;
234- }
235- if (baseItem != null ) {
236- // if the user did not specify a function the first value of the enum in xtext (EQUAL) will be used
237- ModelGroupFunction function = modelGroupItem .getFunction ();
238- item = applyGroupFunction (baseItem , modelGroupItem , function );
222+ String itemType = modelItem .getType ();
223+
224+ try {
225+ String [] itemTypeSegments = itemType .split (ItemUtil .EXTENSION_SEPARATOR );
226+ String mainItemType = itemTypeSegments [0 ];
227+
228+ Item item = switch (mainItemType ) {
229+ case "Group" -> createGroupItem (modelItem , itemTypeSegments );
230+ default -> createItemOfType (itemType , modelItem .getName ());
231+ };
232+
233+ if (item instanceof ActiveItem activeItem ) {
234+ String label = modelItem .getLabel ();
235+ String format = extractFormat (label );
236+ if (format != null ) {
237+ label = label .substring (0 , label .indexOf ("[" )).trim ();
238+ stateDescriptionFragments .put (modelItem .getName (),
239+ StateDescriptionFragmentBuilder .create ().withPattern (format ).build ());
240+ } else {
241+ stateDescriptionFragments .remove (modelItem .getName ());
242+ }
243+ activeItem .setLabel (label );
244+ activeItem .setCategory (modelItem .getIcon ());
245+ assignTags (modelItem , activeItem );
246+ return item ;
239247 } else {
240- item = new GroupItem (modelGroupItem .getName ());
241- }
242- } else {
243- ModelNormalItem normalItem = (ModelNormalItem ) modelItem ;
244- try {
245- item = createItemOfType (normalItem .getType (), normalItem .getName ());
246- } catch (IllegalArgumentException e ) {
247- logger .debug ("Error creating item '{}', item will be ignored: {}" , normalItem .getName (),
248- e .getMessage ());
249248 return null ;
250249 }
251- }
252- if (item instanceof ActiveItem activeItem ) {
253- String label = modelItem .getLabel ();
254- String format = extractFormat (label );
255- if (format != null ) {
256- label = label .substring (0 , label .indexOf ("[" )).trim ();
257- stateDescriptionFragments .put (modelItem .getName (),
258- StateDescriptionFragmentBuilder .create ().withPattern (format ).build ());
259- } else {
260- stateDescriptionFragments .remove (modelItem .getName ());
261- }
262- activeItem .setLabel (label );
263- activeItem .setCategory (modelItem .getIcon ());
264- assignTags (modelItem , activeItem );
265- return item ;
266- } else {
250+ } catch (IllegalArgumentException e ) {
251+ logger .debug ("Error creating item '{}', item will be ignored: {}" , modelItem .getName (), e .getMessage ());
267252 return null ;
268253 }
269254 }
@@ -286,14 +271,14 @@ private void assignTags(ModelItem modelItem, ActiveItem item) {
286271 }
287272 }
288273
289- private GroupItem applyGroupFunction (Item baseItem , ModelGroupItem modelGroupItem , ModelGroupFunction function ) {
274+ private GroupItem applyGroupFunction (Item baseItem , ModelItem modelItem , String function ) {
290275 GroupFunctionDTO dto = new GroupFunctionDTO ();
291- dto .name = function . getName () ;
292- dto .params = modelGroupItem .getArgs ().toArray (new String [0 ]);
276+ dto .name = function ;
277+ dto .params = modelItem .getArgs ().toArray (new String [0 ]);
293278
294279 GroupFunction groupFunction = ItemDTOMapper .mapFunction (baseItem , dto );
295280
296- return new GroupItem (modelGroupItem .getName (), baseItem , groupFunction );
281+ return new GroupItem (modelItem .getName (), baseItem , groupFunction );
297282 }
298283
299284 private void dispatchBindingsPerItemType (String [] itemTypes ) {
@@ -497,6 +482,46 @@ private Map<String, Item> toItemMap(@Nullable Collection<Item> items) {
497482 return ret ;
498483 }
499484
485+ /**
486+ * Creates a new GroupItem based on the given ModelItem and item type segments.
487+ *
488+ * @param modelItem The ModelItem to create the GroupItem from.
489+ * @param itemTypeSegments The segments of the item type.
490+ * @return A new GroupItem or null if the item type is invalid.
491+ */
492+ private @ Nullable GroupItem createGroupItem (ModelItem modelItem , String [] itemTypeSegments ) {
493+ if (itemTypeSegments .length == 1 ) {
494+ // Just plain "Group" with no base type
495+ return new GroupItem (modelItem .getName ());
496+ }
497+
498+ String function = GroupFunction .DEFAULT ;
499+
500+ String baseItemType = switch (itemTypeSegments .length ) {
501+ case 2 -> itemTypeSegments [1 ];
502+ case 3 -> {
503+ // 3 segments could either be Group:Type:Function, or Group:Number:Dimension -> Find out which one it is
504+ if (!modelItem .getArgs ().isEmpty () || GroupFunction .VALID_FUNCTIONS .contains (itemTypeSegments [2 ])) {
505+ // It's Group:Type:Function because there are arguments or the third segment is a valid function
506+ function = itemTypeSegments [2 ];
507+ yield itemTypeSegments [1 ];
508+ } else {
509+ // Otherwise, it must be Group:Number:Dimension
510+ yield itemTypeSegments [1 ] + ItemUtil .EXTENSION_SEPARATOR + itemTypeSegments [2 ];
511+ }
512+ }
513+ case 4 -> {
514+ // 4 segments: "Group:Number:Dimension:Function"
515+ function = itemTypeSegments [3 ];
516+ yield itemTypeSegments [1 ] + ItemUtil .EXTENSION_SEPARATOR + itemTypeSegments [2 ];
517+ }
518+ default -> throw new IllegalArgumentException ("Invalid group item type: " + modelItem .getType ());
519+ };
520+
521+ Item baseItem = createItemOfType (baseItemType , modelItem .getName ());
522+ return applyGroupFunction (baseItem , modelItem , function );
523+ }
524+
500525 /**
501526 * Creates a new item of type {@code itemType} by utilizing an appropriate {@link ItemFactory}.
502527 *
0 commit comments