]> git.basschouten.com Git - openhab-addons.git/blob
e305d11753f903a6ed2222e5c5d47543d4446973
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.hue.internal.handler;
14
15 import static org.openhab.binding.hue.internal.HueBindingConstants.*;
16
17 import java.math.BigDecimal;
18 import java.time.Duration;
19 import java.time.Instant;
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.Comparator;
23 import java.util.HashMap;
24 import java.util.HashSet;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Objects;
28 import java.util.Set;
29 import java.util.TreeSet;
30 import java.util.concurrent.ConcurrentHashMap;
31 import java.util.concurrent.CopyOnWriteArrayList;
32 import java.util.concurrent.Future;
33 import java.util.concurrent.TimeUnit;
34 import java.util.stream.Collectors;
35 import java.util.stream.Stream;
36
37 import org.eclipse.jdt.annotation.NonNullByDefault;
38 import org.eclipse.jdt.annotation.Nullable;
39 import org.openhab.binding.hue.internal.action.DynamicsActions;
40 import org.openhab.binding.hue.internal.api.dto.clip2.Alerts;
41 import org.openhab.binding.hue.internal.api.dto.clip2.ColorXy;
42 import org.openhab.binding.hue.internal.api.dto.clip2.Dimming;
43 import org.openhab.binding.hue.internal.api.dto.clip2.Effects;
44 import org.openhab.binding.hue.internal.api.dto.clip2.Gamut2;
45 import org.openhab.binding.hue.internal.api.dto.clip2.MetaData;
46 import org.openhab.binding.hue.internal.api.dto.clip2.MirekSchema;
47 import org.openhab.binding.hue.internal.api.dto.clip2.ProductData;
48 import org.openhab.binding.hue.internal.api.dto.clip2.Resource;
49 import org.openhab.binding.hue.internal.api.dto.clip2.ResourceReference;
50 import org.openhab.binding.hue.internal.api.dto.clip2.Resources;
51 import org.openhab.binding.hue.internal.api.dto.clip2.TimedEffects;
52 import org.openhab.binding.hue.internal.api.dto.clip2.enums.ActionType;
53 import org.openhab.binding.hue.internal.api.dto.clip2.enums.EffectType;
54 import org.openhab.binding.hue.internal.api.dto.clip2.enums.ResourceType;
55 import org.openhab.binding.hue.internal.api.dto.clip2.enums.SceneRecallAction;
56 import org.openhab.binding.hue.internal.api.dto.clip2.enums.SmartSceneRecallAction;
57 import org.openhab.binding.hue.internal.api.dto.clip2.enums.ZigbeeStatus;
58 import org.openhab.binding.hue.internal.api.dto.clip2.helper.Setters;
59 import org.openhab.binding.hue.internal.config.Clip2ThingConfig;
60 import org.openhab.binding.hue.internal.exceptions.ApiException;
61 import org.openhab.binding.hue.internal.exceptions.AssetNotLoadedException;
62 import org.openhab.core.i18n.TimeZoneProvider;
63 import org.openhab.core.library.types.DateTimeType;
64 import org.openhab.core.library.types.DecimalType;
65 import org.openhab.core.library.types.HSBType;
66 import org.openhab.core.library.types.IncreaseDecreaseType;
67 import org.openhab.core.library.types.OnOffType;
68 import org.openhab.core.library.types.PercentType;
69 import org.openhab.core.library.types.QuantityType;
70 import org.openhab.core.library.types.StringType;
71 import org.openhab.core.library.unit.MetricPrefix;
72 import org.openhab.core.library.unit.Units;
73 import org.openhab.core.thing.Bridge;
74 import org.openhab.core.thing.Channel;
75 import org.openhab.core.thing.ChannelUID;
76 import org.openhab.core.thing.Thing;
77 import org.openhab.core.thing.ThingRegistry;
78 import org.openhab.core.thing.ThingStatus;
79 import org.openhab.core.thing.ThingStatusDetail;
80 import org.openhab.core.thing.ThingTypeUID;
81 import org.openhab.core.thing.ThingUID;
82 import org.openhab.core.thing.binding.BaseThingHandler;
83 import org.openhab.core.thing.binding.BridgeHandler;
84 import org.openhab.core.thing.binding.ThingHandlerService;
85 import org.openhab.core.thing.binding.builder.ChannelBuilder;
86 import org.openhab.core.thing.binding.builder.ThingBuilder;
87 import org.openhab.core.thing.link.ItemChannelLink;
88 import org.openhab.core.thing.link.ItemChannelLinkRegistry;
89 import org.openhab.core.thing.type.ChannelTypeUID;
90 import org.openhab.core.types.Command;
91 import org.openhab.core.types.RefreshType;
92 import org.openhab.core.types.State;
93 import org.openhab.core.types.StateOption;
94 import org.openhab.core.types.UnDefType;
95 import org.slf4j.Logger;
96 import org.slf4j.LoggerFactory;
97
98 /**
99  * Handler for things based on CLIP 2 'device', 'room', or 'zone resources.
100  *
101  * @author Andrew Fiddian-Green - Initial contribution.
102  */
103 @NonNullByDefault
104 public class Clip2ThingHandler extends BaseThingHandler {
105
106     public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Set.of(THING_TYPE_DEVICE, THING_TYPE_ROOM,
107             THING_TYPE_ZONE);
108
109     private static final Set<ResourceType> SUPPORTED_SCENE_TYPES = Set.of(ResourceType.SCENE, ResourceType.SMART_SCENE);
110
111     private static final Duration DYNAMICS_ACTIVE_WINDOW = Duration.ofSeconds(10);
112
113     private static final String LK_WISER_DIMMER_MODEL_ID = "LK Dimmer";
114
115     private final Logger logger = LoggerFactory.getLogger(Clip2ThingHandler.class);
116
117     /**
118      * A map of service Resources whose state contributes to the overall state of this thing. It is a map between the
119      * resource ID (string) and a Resource object containing the last known state. e.g. a DEVICE thing may support a
120      * LIGHT service whose Resource contributes to its overall state, or a ROOM or ZONE thing may support a
121      * GROUPED_LIGHT service whose Resource contributes to the its overall state.
122      */
123     private final Map<String, Resource> serviceContributorsCache = new ConcurrentHashMap<>();
124
125     /**
126      * A map of Resource IDs which are targets for commands to be sent. It is a map between the type of command
127      * (ResourcesType) and the resource ID to which the command shall be sent. e.g. a LIGHT 'on' command shall be sent
128      * to the respective LIGHT resource ID.
129      */
130     private final Map<ResourceType, String> commandResourceIds = new ConcurrentHashMap<>();
131
132     /**
133      * Button devices contain one or more physical buttons, each of which is represented by a BUTTON Resource with its
134      * own unique resource ID, and a respective controlId that indicates which button it is in the device. e.g. a dimmer
135      * pad has four buttons (controlId's 1..4) each represented by a BUTTON Resource with a unique resource ID. This is
136      * a map between the resource ID and its respective controlId.
137      */
138     private final Map<String, Integer> controlIds = new ConcurrentHashMap<>();
139
140     /**
141      * The set of channel IDs that are supported by this thing. e.g. an on/off light may support 'switch' and
142      * 'zigbeeStatus' channels, whereas a complex light may support 'switch', 'brightness', 'color', 'color temperature'
143      * and 'zigbeeStatus' channels.
144      */
145     private final Set<String> supportedChannelIdSet = new HashSet<>();
146
147     /**
148      * A map of scene IDs versus scene Resources for the scenes that contribute to and command this thing. It is a map
149      * between the resource ID (string) and a Resource object containing the scene's last known state.
150      */
151     private final Map<String, Resource> sceneContributorsCache = new ConcurrentHashMap<>();
152
153     /**
154      * A map of scene names versus scene Resources for the scenes that contribute to and command this thing. e.g. a
155      * command for a scene named 'Energize' shall be sent to the respective SCENE resource ID.
156      */
157     private final Map<String, Resource> sceneResourceEntries = new ConcurrentHashMap<>();
158
159     /**
160      * A list of API v1 thing channel UIDs that are linked to items. It is used in the process of replicating the
161      * Item/Channel links from a legacy v1 thing to this API v2 thing.
162      */
163     private final List<ChannelUID> legacyLinkedChannelUIDs = new CopyOnWriteArrayList<>();
164
165     private final ThingRegistry thingRegistry;
166     private final ItemChannelLinkRegistry itemChannelLinkRegistry;
167     private final Clip2StateDescriptionProvider stateDescriptionProvider;
168     private final TimeZoneProvider timeZoneProvider;
169
170     private String resourceId = "?";
171     private Resource thisResource;
172     private Duration dynamicsDuration = Duration.ZERO;
173     private Instant dynamicsExpireTime = Instant.MIN;
174     private Instant buttonGroupLastUpdated = Instant.MIN;
175
176     private boolean disposing;
177     private boolean hasConnectivityIssue;
178     private boolean updateSceneContributorsDone;
179     private boolean updateLightPropertiesDone;
180     private boolean updatePropertiesDone;
181     private boolean updateDependenciesDone;
182     private boolean applyOffTransitionWorkaround;
183
184     private @Nullable Future<?> alertResetTask;
185     private @Nullable Future<?> dynamicsResetTask;
186     private @Nullable Future<?> updateDependenciesTask;
187     private @Nullable Future<?> updateServiceContributorsTask;
188
189     public Clip2ThingHandler(Thing thing, Clip2StateDescriptionProvider stateDescriptionProvider,
190             TimeZoneProvider timeZoneProvider, ThingRegistry thingRegistry,
191             ItemChannelLinkRegistry itemChannelLinkRegistry) {
192         super(thing);
193
194         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
195         if (THING_TYPE_DEVICE.equals(thingTypeUID)) {
196             thisResource = new Resource(ResourceType.DEVICE);
197         } else if (THING_TYPE_ROOM.equals(thingTypeUID)) {
198             thisResource = new Resource(ResourceType.ROOM);
199         } else if (THING_TYPE_ZONE.equals(thingTypeUID)) {
200             thisResource = new Resource(ResourceType.ZONE);
201         } else {
202             throw new IllegalArgumentException("Wrong thing type " + thingTypeUID.getAsString());
203         }
204
205         this.thingRegistry = thingRegistry;
206         this.itemChannelLinkRegistry = itemChannelLinkRegistry;
207         this.stateDescriptionProvider = stateDescriptionProvider;
208         this.timeZoneProvider = timeZoneProvider;
209     }
210
211     /**
212      * Add a channel ID to the supportedChannelIdSet set. If the channel supports dynamics (timed transitions) then add
213      * the respective channel as well.
214      *
215      * @param channelId the channel ID to add.
216      */
217     private void addSupportedChannel(String channelId) {
218         if (!disposing && !updateDependenciesDone) {
219             synchronized (supportedChannelIdSet) {
220                 logger.debug("{} -> addSupportedChannel() '{}' added to supported channel set", resourceId, channelId);
221                 supportedChannelIdSet.add(channelId);
222                 if (DYNAMIC_CHANNELS.contains(channelId)) {
223                     clearDynamicsChannel();
224                 }
225             }
226         }
227     }
228
229     /**
230      * Cancel the given task.
231      *
232      * @param cancelTask the task to be cancelled (may be null)
233      * @param mayInterrupt allows cancel() to interrupt the thread.
234      */
235     private void cancelTask(@Nullable Future<?> cancelTask, boolean mayInterrupt) {
236         if (Objects.nonNull(cancelTask)) {
237             cancelTask.cancel(mayInterrupt);
238         }
239     }
240
241     /**
242      * Clear the dynamics channel parameters.
243      */
244     private void clearDynamicsChannel() {
245         dynamicsExpireTime = Instant.MIN;
246         dynamicsDuration = Duration.ZERO;
247         updateState(CHANNEL_2_DYNAMICS, new QuantityType<>(0, MetricPrefix.MILLI(Units.SECOND)), true);
248     }
249
250     @Override
251     public void dispose() {
252         logger.debug("{} -> dispose()", resourceId);
253         disposing = true;
254         cancelTask(alertResetTask, true);
255         cancelTask(dynamicsResetTask, true);
256         cancelTask(updateDependenciesTask, true);
257         cancelTask(updateServiceContributorsTask, true);
258         alertResetTask = null;
259         dynamicsResetTask = null;
260         updateDependenciesTask = null;
261         updateServiceContributorsTask = null;
262         legacyLinkedChannelUIDs.clear();
263         sceneContributorsCache.clear();
264         sceneResourceEntries.clear();
265         supportedChannelIdSet.clear();
266         commandResourceIds.clear();
267         serviceContributorsCache.clear();
268         controlIds.clear();
269     }
270
271     /**
272      * Get the bridge handler.
273      *
274      * @throws AssetNotLoadedException if the handler does not exist.
275      */
276     private Clip2BridgeHandler getBridgeHandler() throws AssetNotLoadedException {
277         Bridge bridge = getBridge();
278         if (Objects.nonNull(bridge)) {
279             BridgeHandler handler = bridge.getHandler();
280             if (handler instanceof Clip2BridgeHandler) {
281                 return (Clip2BridgeHandler) handler;
282             }
283         }
284         throw new AssetNotLoadedException("Bridge handler missing");
285     }
286
287     /**
288      * Do a double lookup to get the cached resource that matches the given ResourceType.
289      *
290      * @param resourceType the type to search for.
291      * @return the Resource, or null if not found.
292      */
293     private @Nullable Resource getCachedResource(ResourceType resourceType) {
294         String commandResourceId = commandResourceIds.get(resourceType);
295         return Objects.nonNull(commandResourceId) ? serviceContributorsCache.get(commandResourceId) : null;
296     }
297
298     /**
299      * Return a ResourceReference to this handler's resource.
300      *
301      * @return a ResourceReference instance.
302      */
303     public ResourceReference getResourceReference() {
304         return new ResourceReference().setId(resourceId).setType(thisResource.getType());
305     }
306
307     /**
308      * Register the 'DynamicsAction' service.
309      */
310     @Override
311     public Collection<Class<? extends ThingHandlerService>> getServices() {
312         return Set.of(DynamicsActions.class);
313     }
314
315     @Override
316     public void handleCommand(ChannelUID channelUID, Command commandParam) {
317         if (RefreshType.REFRESH.equals(commandParam)) {
318             if (thing.getStatus() == ThingStatus.ONLINE) {
319                 refreshAllChannels();
320             }
321             return;
322         }
323
324         Channel channel = thing.getChannel(channelUID);
325         if (channel == null) {
326             if (logger.isDebugEnabled()) {
327                 logger.debug("{} -> handleCommand() channelUID:{} does not exist", resourceId, channelUID);
328
329             } else {
330                 logger.warn("Command received for channel '{}' which is not in thing '{}'.", channelUID,
331                         thing.getUID());
332             }
333             return;
334         }
335
336         ResourceType lightResourceType = thisResource.getType() == ResourceType.DEVICE ? ResourceType.LIGHT
337                 : ResourceType.GROUPED_LIGHT;
338
339         Resource putResource = null;
340         String putResourceId = null;
341         Command command = commandParam;
342         String channelId = channelUID.getId();
343         Resource cache = getCachedResource(lightResourceType);
344
345         switch (channelId) {
346             case CHANNEL_2_ALERT:
347                 putResource = Setters.setAlert(new Resource(lightResourceType), command, cache);
348                 cancelTask(alertResetTask, false);
349                 alertResetTask = scheduler.schedule(
350                         () -> updateState(channelUID, new StringType(ActionType.NO_ACTION.name())), 10,
351                         TimeUnit.SECONDS);
352                 break;
353
354             case CHANNEL_2_EFFECT:
355                 putResource = Setters.setEffect(new Resource(lightResourceType), command, cache).setOnOff(OnOffType.ON);
356                 break;
357
358             case CHANNEL_2_COLOR_TEMP_PERCENT:
359                 if (command instanceof IncreaseDecreaseType) {
360                     if (Objects.nonNull(cache)) {
361                         State current = cache.getColorTemperaturePercentState();
362                         if (current instanceof PercentType) {
363                             int sign = IncreaseDecreaseType.INCREASE == command ? 1 : -1;
364                             int percent = ((PercentType) current).intValue() + (sign * (int) Resource.PERCENT_DELTA);
365                             command = new PercentType(Math.min(100, Math.max(0, percent)));
366                         }
367                     }
368                 } else if (command instanceof OnOffType) {
369                     command = OnOffType.OFF == command ? PercentType.ZERO : PercentType.HUNDRED;
370                 }
371                 putResource = Setters.setColorTemperaturePercent(new Resource(lightResourceType), command, cache);
372                 break;
373
374             case CHANNEL_2_COLOR_TEMP_ABSOLUTE:
375                 putResource = Setters.setColorTemperatureAbsolute(new Resource(lightResourceType), command, cache);
376                 break;
377
378             case CHANNEL_2_COLOR:
379                 putResource = new Resource(lightResourceType);
380                 if (command instanceof HSBType) {
381                     HSBType color = ((HSBType) command);
382                     putResource = Setters.setColorXy(putResource, color, cache);
383                     command = color.getBrightness();
384                 }
385                 // NB fall through for handling of brightness and switch related commands !!
386
387             case CHANNEL_2_BRIGHTNESS:
388                 putResource = Objects.nonNull(putResource) ? putResource : new Resource(lightResourceType);
389                 if (command instanceof IncreaseDecreaseType) {
390                     if (Objects.nonNull(cache)) {
391                         State current = cache.getBrightnessState();
392                         if (current instanceof PercentType) {
393                             int sign = IncreaseDecreaseType.INCREASE == command ? 1 : -1;
394                             double percent = ((PercentType) current).doubleValue() + (sign * Resource.PERCENT_DELTA);
395                             command = new PercentType(new BigDecimal(Math.min(100f, Math.max(0f, percent)),
396                                     Resource.PERCENT_MATH_CONTEXT));
397                         }
398                     }
399                 }
400                 if (command instanceof PercentType) {
401                     PercentType brightness = (PercentType) command;
402                     putResource = Setters.setDimming(putResource, brightness, cache);
403                     Double minDimLevel = Objects.nonNull(cache) ? cache.getMinimumDimmingLevel() : null;
404                     minDimLevel = Objects.nonNull(minDimLevel) ? minDimLevel : Dimming.DEFAULT_MINIMUM_DIMMIMG_LEVEL;
405                     command = OnOffType.from(brightness.doubleValue() >= minDimLevel);
406                 }
407                 // NB fall through for handling of switch related commands !!
408
409             case CHANNEL_2_SWITCH:
410                 putResource = Objects.nonNull(putResource) ? putResource : new Resource(lightResourceType);
411                 putResource.setOnOff(command);
412                 applyDeviceSpecificWorkArounds(command, putResource);
413                 break;
414
415             case CHANNEL_2_COLOR_XY_ONLY:
416                 putResource = Setters.setColorXy(new Resource(lightResourceType), command, cache);
417                 break;
418
419             case CHANNEL_2_DIMMING_ONLY:
420                 putResource = Setters.setDimming(new Resource(lightResourceType), command, cache);
421                 break;
422
423             case CHANNEL_2_ON_OFF_ONLY:
424                 putResource = new Resource(lightResourceType).setOnOff(command);
425                 applyDeviceSpecificWorkArounds(command, putResource);
426                 break;
427
428             case CHANNEL_2_TEMPERATURE_ENABLED:
429                 putResource = new Resource(ResourceType.TEMPERATURE).setEnabled(command);
430                 break;
431
432             case CHANNEL_2_MOTION_ENABLED:
433                 putResource = new Resource(ResourceType.MOTION).setEnabled(command);
434                 break;
435
436             case CHANNEL_2_LIGHT_LEVEL_ENABLED:
437                 putResource = new Resource(ResourceType.LIGHT_LEVEL).setEnabled(command);
438                 break;
439
440             case CHANNEL_2_SECURITY_CONTACT_ENABLED:
441                 putResource = new Resource(ResourceType.CONTACT).setEnabled(command);
442                 break;
443
444             case CHANNEL_2_SCENE:
445                 if (command instanceof StringType) {
446                     Resource scene = sceneResourceEntries.get(((StringType) command).toString());
447                     if (Objects.nonNull(scene)) {
448                         ResourceType putResourceType = scene.getType();
449                         putResource = new Resource(putResourceType);
450                         switch (putResourceType) {
451                             case SCENE:
452                                 putResource.setRecallAction(SceneRecallAction.ACTIVE);
453                                 break;
454                             case SMART_SCENE:
455                                 putResource.setRecallAction(SmartSceneRecallAction.ACTIVATE);
456                                 break;
457                             default:
458                                 logger.debug("{} -> handleCommand() type '{}' is not a supported scene type",
459                                         resourceId, putResourceType);
460                                 return;
461                         }
462                         putResourceId = scene.getId();
463                     }
464                 }
465                 break;
466
467             case CHANNEL_2_DYNAMICS:
468                 Duration clearAfter = Duration.ZERO;
469                 if (command instanceof QuantityType<?>) {
470                     QuantityType<?> durationMs = ((QuantityType<?>) command).toUnit(MetricPrefix.MILLI(Units.SECOND));
471                     if (Objects.nonNull(durationMs) && durationMs.longValue() > 0) {
472                         Duration duration = Duration.ofMillis(durationMs.longValue());
473                         dynamicsDuration = duration;
474                         dynamicsExpireTime = Instant.now().plus(DYNAMICS_ACTIVE_WINDOW);
475                         clearAfter = DYNAMICS_ACTIVE_WINDOW;
476                         logger.debug("{} -> handleCommand() dynamics setting {} valid for {}", resourceId, duration,
477                                 clearAfter);
478                     }
479                 }
480                 cancelTask(dynamicsResetTask, false);
481                 dynamicsResetTask = scheduler.schedule(() -> clearDynamicsChannel(), clearAfter.toMillis(),
482                         TimeUnit.MILLISECONDS);
483                 return;
484
485             default:
486                 if (logger.isDebugEnabled()) {
487                     logger.debug("{} -> handleCommand() channelUID:{} unknown", resourceId, channelUID);
488                 } else {
489                     logger.warn("Command received for unknown channel '{}'.", channelUID);
490                 }
491                 return;
492         }
493
494         if (putResource == null) {
495             if (logger.isDebugEnabled()) {
496                 logger.debug("{} -> handleCommand() command:{} not supported on channelUID:{}", resourceId, command,
497                         channelUID);
498             } else {
499                 logger.warn("Command '{}' is not supported on channel '{}'.", command, channelUID);
500             }
501             return;
502         }
503
504         putResourceId = Objects.nonNull(putResourceId) ? putResourceId : commandResourceIds.get(putResource.getType());
505         if (putResourceId == null) {
506             if (logger.isDebugEnabled()) {
507                 logger.debug(
508                         "{} -> handleCommand() channelUID:{}, command:{}, putResourceType:{} => missing resource ID",
509                         resourceId, channelUID, command, putResource.getType());
510             } else {
511                 logger.warn("Command '{}' for channel '{}' cannot be processed by thing '{}'.", command, channelUID,
512                         thing.getUID());
513             }
514             return;
515         }
516
517         if (DYNAMIC_CHANNELS.contains(channelId)) {
518             if (Instant.now().isBefore(dynamicsExpireTime) && !dynamicsDuration.isZero()
519                     && !dynamicsDuration.isNegative()) {
520                 if (ResourceType.SCENE == putResource.getType()) {
521                     putResource.setRecallDuration(dynamicsDuration);
522                 } else if (CHANNEL_2_EFFECT == channelId) {
523                     putResource.setTimedEffectsDuration(dynamicsDuration);
524                 } else {
525                     putResource.setDynamicsDuration(dynamicsDuration);
526                 }
527             }
528         }
529
530         putResource.setId(putResourceId);
531         logger.debug("{} -> handleCommand() put resource {}", resourceId, putResource);
532
533         try {
534             Resources resources = getBridgeHandler().putResource(putResource);
535             if (resources.hasErrors()) {
536                 logger.info("Command '{}' for thing '{}', channel '{}' succeeded with errors: {}", command,
537                         thing.getUID(), channelUID, String.join("; ", resources.getErrors()));
538             }
539         } catch (ApiException | AssetNotLoadedException e) {
540             if (logger.isDebugEnabled()) {
541                 logger.debug("{} -> handleCommand() error {}", resourceId, e.getMessage(), e);
542             } else {
543                 logger.warn("Command '{}' for thing '{}', channel '{}' failed with error '{}'.", command,
544                         thing.getUID(), channelUID, e.getMessage());
545             }
546         } catch (InterruptedException e) {
547         }
548     }
549
550     private void refreshAllChannels() {
551         if (!updateDependenciesDone) {
552             return;
553         }
554         cancelTask(updateServiceContributorsTask, false);
555         updateServiceContributorsTask = scheduler.schedule(() -> {
556             try {
557                 updateServiceContributors();
558             } catch (ApiException | AssetNotLoadedException e) {
559                 logger.debug("{} -> handleCommand() error {}", resourceId, e.getMessage(), e);
560             } catch (InterruptedException e) {
561             }
562         }, 3, TimeUnit.SECONDS);
563     }
564
565     /**
566      * Apply device specific work-arounds needed for given command.
567      *
568      * @param command the handled command.
569      * @param putResource the resource that will be adjusted if needed.
570      */
571     private void applyDeviceSpecificWorkArounds(Command command, Resource putResource) {
572         if (command == OnOffType.OFF && applyOffTransitionWorkaround) {
573             putResource.setDynamicsDuration(dynamicsDuration);
574         }
575     }
576
577     /**
578      * Handle a 'dynamics' command for the given channel ID for the given dynamics duration.
579      *
580      * @param channelId the ID of the target channel.
581      * @param command the new target state.
582      * @param duration the transition duration.
583      */
584     public synchronized void handleDynamicsCommand(String channelId, Command command, QuantityType<?> duration) {
585         if (DYNAMIC_CHANNELS.contains(channelId)) {
586             Channel dynamicsChannel = thing.getChannel(CHANNEL_2_DYNAMICS);
587             Channel targetChannel = thing.getChannel(channelId);
588             if (Objects.nonNull(dynamicsChannel) && Objects.nonNull(targetChannel)) {
589                 logger.debug("{} - handleDynamicsCommand() channelId:{}, command:{}, duration:{}", resourceId,
590                         channelId, command, duration);
591                 handleCommand(dynamicsChannel.getUID(), duration);
592                 handleCommand(targetChannel.getUID(), command);
593                 return;
594             }
595         }
596         logger.warn("Dynamics command '{}' for thing '{}', channel '{}' and duration'{}' failed.", command,
597                 thing.getUID(), channelId, duration);
598     }
599
600     @Override
601     public void initialize() {
602         Clip2ThingConfig config = getConfigAs(Clip2ThingConfig.class);
603
604         String resourceId = config.resourceId;
605         if (resourceId.isBlank()) {
606             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
607                     "@text/offline.api2.conf-error.resource-id-missing");
608             return;
609         }
610         thisResource.setId(resourceId);
611         this.resourceId = resourceId;
612         logger.debug("{} -> initialize()", resourceId);
613
614         updateThingFromLegacy();
615         updateStatus(ThingStatus.UNKNOWN);
616
617         dynamicsDuration = Duration.ZERO;
618         dynamicsExpireTime = Instant.MIN;
619
620         disposing = false;
621         hasConnectivityIssue = false;
622         updatePropertiesDone = false;
623         updateDependenciesDone = false;
624         updateLightPropertiesDone = false;
625         updateSceneContributorsDone = false;
626
627         Bridge bridge = getBridge();
628         if (Objects.nonNull(bridge)) {
629             BridgeHandler bridgeHandler = bridge.getHandler();
630             if (bridgeHandler instanceof Clip2BridgeHandler) {
631                 ((Clip2BridgeHandler) bridgeHandler).childInitialized();
632             }
633         }
634     }
635
636     /**
637      * Update the channel state depending on new resources sent from the bridge.
638      *
639      * @param resources a collection of Resource objects containing the new state.
640      */
641     public void onResources(Collection<Resource> resources) {
642         boolean sceneActivated = resources.stream().anyMatch(r -> sceneContributorsCache.containsKey(r.getId())
643                 && (r.getSceneActive().orElse(false) || r.getSmartSceneActive().orElse(false)));
644         for (Resource resource : resources) {
645             // Skip scene deactivation when we have also received a scene activation.
646             boolean updateChannels = !sceneActivated || !sceneContributorsCache.containsKey(resource.getId())
647                     || resource.getSceneActive().orElse(false) || resource.getSmartSceneActive().orElse(false);
648             onResource(resource, updateChannels);
649         }
650     }
651
652     /**
653      * Update the channel state depending on a new resource sent from the bridge.
654      *
655      * @param resource a Resource object containing the new state.
656      */
657     private void onResource(Resource resource) {
658         onResource(resource, true);
659     }
660
661     /**
662      * Update the channel state depending on a new resource sent from the bridge.
663      *
664      * @param resource a Resource object containing the new state.
665      * @param updateChannels update channels (otherwise only update cache/properties).
666      */
667     private void onResource(Resource resource, boolean updateChannels) {
668         if (disposing) {
669             return;
670         }
671         boolean resourceConsumed = false;
672         if (resourceId.equals(resource.getId())) {
673             if (resource.hasFullState()) {
674                 thisResource = resource;
675                 if (!updatePropertiesDone) {
676                     updateProperties(resource);
677                     resourceConsumed = updatePropertiesDone;
678                 }
679             }
680             if (!updateDependenciesDone) {
681                 resourceConsumed = true;
682                 cancelTask(updateDependenciesTask, false);
683                 updateDependenciesTask = scheduler.submit(() -> updateDependencies());
684             }
685         } else {
686             Resource cachedResource = getResourceFromCache(resource);
687             if (cachedResource != null) {
688                 Setters.setResource(resource, cachedResource);
689                 resourceConsumed = updateChannels && updateChannels(resource);
690                 putResourceToCache(resource);
691                 if (ResourceType.LIGHT == resource.getType() && !updateLightPropertiesDone) {
692                     updateLightProperties(resource);
693                 }
694             }
695         }
696         if (resourceConsumed) {
697             logger.debug("{} -> onResource() consumed resource {}", resourceId, resource);
698         }
699     }
700
701     private void putResourceToCache(Resource resource) {
702         if (SUPPORTED_SCENE_TYPES.contains(resource.getType())) {
703             sceneContributorsCache.put(resource.getId(), resource);
704         } else {
705             serviceContributorsCache.put(resource.getId(), resource);
706         }
707     }
708
709     private @Nullable Resource getResourceFromCache(Resource resource) {
710         return SUPPORTED_SCENE_TYPES.contains(resource.getType()) //
711                 ? sceneContributorsCache.get(resource.getId())
712                 : serviceContributorsCache.get(resource.getId());
713     }
714
715     /**
716      * Update the thing internal state depending on a full list of resources sent from the bridge. If the resourceType
717      * is SCENE then call updateScenes(), otherwise if the resource refers to this thing, consume it via onResource() as
718      * any other resource, or else if the resourceType nevertheless matches the thing type, set the thing state offline.
719      *
720      * @param resourceType the type of the resources in the list.
721      * @param fullResources the full list of resources of the given type.
722      */
723     public void onResourcesList(ResourceType resourceType, List<Resource> fullResources) {
724         if (resourceType == ResourceType.SCENE) {
725             updateSceneContributors(fullResources);
726         } else {
727             fullResources.stream().filter(r -> resourceId.equals(r.getId())).findAny()
728                     .ifPresentOrElse(r -> onResource(r), () -> {
729                         if (resourceType == thisResource.getType()) {
730                             logger.debug("{} -> onResourcesList() configuration error: unknown resourceId", resourceId);
731                             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.GONE,
732                                     "@text/offline.api2.gone.resource-id-unknown");
733                         }
734                     });
735         }
736     }
737
738     /**
739      * Process the incoming Resource to initialize the alert channel.
740      *
741      * @param resource a Resource possibly with an Alerts element.
742      */
743     private void updateAlertChannel(Resource resource) {
744         Alerts alerts = resource.getAlerts();
745         if (Objects.nonNull(alerts)) {
746             List<StateOption> stateOptions = alerts.getActionValues().stream().map(action -> action.name())
747                     .map(actionId -> new StateOption(actionId, actionId)).collect(Collectors.toList());
748             if (!stateOptions.isEmpty()) {
749                 stateDescriptionProvider.setStateOptions(new ChannelUID(thing.getUID(), CHANNEL_2_ALERT), stateOptions);
750                 logger.debug("{} -> updateAlerts() found {} associated alerts", resourceId, stateOptions.size());
751             }
752         }
753     }
754
755     /**
756      * If this v2 thing has a matching v1 legacy thing in the system, then for each channel in the v1 thing that
757      * corresponds to an equivalent channel in this v2 thing, and for all items that are linked to the v1 channel,
758      * create a new channel/item link between that item and the respective v2 channel in this thing.
759      */
760     private void updateChannelItemLinksFromLegacy() {
761         if (!disposing) {
762             legacyLinkedChannelUIDs.forEach(legacyLinkedChannelUID -> {
763                 String targetChannelId = REPLICATE_CHANNEL_ID_MAP.get(legacyLinkedChannelUID.getId());
764                 if (Objects.nonNull(targetChannelId)) {
765                     Channel targetChannel = thing.getChannel(targetChannelId);
766                     if (Objects.nonNull(targetChannel)) {
767                         ChannelUID uid = targetChannel.getUID();
768                         itemChannelLinkRegistry.getLinkedItems(legacyLinkedChannelUID).forEach(linkedItem -> {
769                             String item = linkedItem.getName();
770                             if (!itemChannelLinkRegistry.isLinked(item, uid)) {
771                                 if (logger.isDebugEnabled()) {
772                                     logger.debug(
773                                             "{} -> updateChannelItemLinksFromLegacy() item:{} linked to channel:{}",
774                                             resourceId, item, uid);
775                                 } else {
776                                     logger.info("Item '{}' linked to thing '{}' channel '{}'", item, thing.getUID(),
777                                             targetChannelId);
778                                 }
779                                 itemChannelLinkRegistry.add(new ItemChannelLink(item, uid));
780                             }
781                         });
782                     }
783                 }
784             });
785             legacyLinkedChannelUIDs.clear();
786         }
787     }
788
789     /**
790      * Set the active list of channels by removing any that had initially been created by the thing XML declaration, but
791      * which in fact did not have data returned from the bridge i.e. channels which are not in the supportedChannelIdSet
792      *
793      * Also warn if there are channels in the supportedChannelIdSet set which are not in the thing.
794      *
795      * Adjusts the channel list so that only the highest level channel is available in the normal channel list. If a
796      * light supports the color channel, then it's brightness and switch can be commanded via the 'B' part of the HSB
797      * channel value. And if it supports the brightness channel the switch can be controlled via the brightness. So we
798      * can remove these lower level channels from the normal channel list.
799      *
800      * For more advanced applications, it is necessary to orthogonally command the color xy parameter, dimming
801      * parameter, and/or on/off parameter independently. So we add corresponding advanced level 'CHANNEL_2_BLAH_ONLY'
802      * channels for that purpose. Since they are advanced level, normal users should normally not be confused by them,
803      * yet advanced users can use them nevertheless.
804      */
805     private void updateChannelList() {
806         if (!disposing) {
807             synchronized (supportedChannelIdSet) {
808                 logger.debug("{} -> updateChannelList()", resourceId);
809
810                 if (supportedChannelIdSet.contains(CHANNEL_2_COLOR)) {
811                     supportedChannelIdSet.add(CHANNEL_2_COLOR_XY_ONLY);
812                     //
813                     supportedChannelIdSet.remove(CHANNEL_2_BRIGHTNESS);
814                     supportedChannelIdSet.add(CHANNEL_2_DIMMING_ONLY);
815                     //
816                     supportedChannelIdSet.remove(CHANNEL_2_SWITCH);
817                     supportedChannelIdSet.add(CHANNEL_2_ON_OFF_ONLY);
818                 }
819                 if (supportedChannelIdSet.contains(CHANNEL_2_BRIGHTNESS)) {
820                     supportedChannelIdSet.add(CHANNEL_2_DIMMING_ONLY);
821                     //
822                     supportedChannelIdSet.remove(CHANNEL_2_SWITCH);
823                     supportedChannelIdSet.add(CHANNEL_2_ON_OFF_ONLY);
824                 }
825                 if (supportedChannelIdSet.contains(CHANNEL_2_SWITCH)) {
826                     supportedChannelIdSet.add(CHANNEL_2_ON_OFF_ONLY);
827                 }
828
829                 /*
830                  * This binding creates its dynamic list of channels by a 'subtractive' method i.e. the full set of
831                  * channels is initially created from the thing type xml, and then for any channels where UndfType.NULL
832                  * data is returned, the respective channel is removed from the full list. However in seldom cases
833                  * UndfType.NULL may wrongly be returned, so we should log a warning here just in case.
834                  */
835                 if (logger.isDebugEnabled()) {
836                     supportedChannelIdSet.stream().filter(channelId -> Objects.isNull(thing.getChannel(channelId)))
837                             .forEach(channelId -> logger.debug(
838                                     "{} -> updateChannelList() required channel '{}' missing", resourceId, channelId));
839                 } else {
840                     supportedChannelIdSet.stream().filter(channelId -> Objects.isNull(thing.getChannel(channelId)))
841                             .forEach(channelId -> logger.warn(
842                                     "Thing '{}' is missing required channel '{}'. Please recreate the thing!",
843                                     thing.getUID(), channelId));
844                 }
845
846                 // get list of unused channels
847                 List<Channel> unusedChannels = thing.getChannels().stream()
848                         .filter(channel -> !supportedChannelIdSet.contains(channel.getUID().getId()))
849                         .collect(Collectors.toList());
850
851                 // remove any unused channels
852                 if (!unusedChannels.isEmpty()) {
853                     if (logger.isDebugEnabled()) {
854                         unusedChannels.stream().map(channel -> channel.getUID().getId())
855                                 .forEach(channelId -> logger.debug(
856                                         "{} -> updateChannelList() removing unused channel '{}'", resourceId,
857                                         channelId));
858                     }
859                     updateThing(editThing().withoutChannels(unusedChannels).build());
860                 }
861             }
862         }
863     }
864
865     /**
866      * Update the state of the existing channels.
867      *
868      * @param resource the Resource containing the new channel state.
869      * @return true if the channel was found and updated.
870      */
871     private boolean updateChannels(Resource resource) {
872         logger.debug("{} -> updateChannels() from resource {}", resourceId, resource);
873         boolean fullUpdate = resource.hasFullState();
874         switch (resource.getType()) {
875             case BUTTON:
876                 if (fullUpdate) {
877                     addSupportedChannel(CHANNEL_2_BUTTON_LAST_EVENT);
878                     addSupportedChannel(CHANNEL_2_BUTTON_LAST_UPDATED);
879                     controlIds.put(resource.getId(), resource.getControlId());
880                 } else {
881                     State buttonState = resource.getButtonEventState(controlIds);
882                     updateState(CHANNEL_2_BUTTON_LAST_EVENT, buttonState, fullUpdate);
883                 }
884                 // Update channel from timestamp if last button pressed.
885                 State buttonLastUpdatedState = resource.getButtonLastUpdatedState(timeZoneProvider.getTimeZone());
886                 if (buttonLastUpdatedState instanceof DateTimeType) {
887                     Instant buttonLastUpdatedInstant = ((DateTimeType) buttonLastUpdatedState).getInstant();
888                     if (buttonLastUpdatedInstant.isAfter(buttonGroupLastUpdated)) {
889                         updateState(CHANNEL_2_BUTTON_LAST_UPDATED, buttonLastUpdatedState, fullUpdate);
890                         buttonGroupLastUpdated = buttonLastUpdatedInstant;
891                     }
892                 } else if (Instant.MIN.equals(buttonGroupLastUpdated)) {
893                     updateState(CHANNEL_2_BUTTON_LAST_UPDATED, buttonLastUpdatedState, fullUpdate);
894                 }
895                 break;
896
897             case DEVICE_POWER:
898                 updateState(CHANNEL_2_BATTERY_LEVEL, resource.getBatteryLevelState(), fullUpdate);
899                 updateState(CHANNEL_2_BATTERY_LOW, resource.getBatteryLowState(), fullUpdate);
900                 break;
901
902             case LIGHT:
903                 if (fullUpdate) {
904                     updateEffectChannel(resource);
905                 }
906                 updateState(CHANNEL_2_COLOR_TEMP_PERCENT, resource.getColorTemperaturePercentState(), fullUpdate);
907                 updateState(CHANNEL_2_COLOR_TEMP_ABSOLUTE, resource.getColorTemperatureAbsoluteState(), fullUpdate);
908                 updateState(CHANNEL_2_COLOR, resource.getColorState(), fullUpdate);
909                 updateState(CHANNEL_2_COLOR_XY_ONLY, resource.getColorXyState(), fullUpdate);
910                 updateState(CHANNEL_2_EFFECT, resource.getEffectState(), fullUpdate);
911                 // fall through for dimming and on/off related channels
912
913             case GROUPED_LIGHT:
914                 if (fullUpdate) {
915                     updateAlertChannel(resource);
916                 }
917                 updateState(CHANNEL_2_BRIGHTNESS, resource.getBrightnessState(), fullUpdate);
918                 updateState(CHANNEL_2_DIMMING_ONLY, resource.getDimmingState(), fullUpdate);
919                 updateState(CHANNEL_2_SWITCH, resource.getOnOffState(), fullUpdate);
920                 updateState(CHANNEL_2_ON_OFF_ONLY, resource.getOnOffState(), fullUpdate);
921                 updateState(CHANNEL_2_ALERT, resource.getAlertState(), fullUpdate);
922                 break;
923
924             case LIGHT_LEVEL:
925                 updateState(CHANNEL_2_LIGHT_LEVEL, resource.getLightLevelState(), fullUpdate);
926                 updateState(CHANNEL_2_LIGHT_LEVEL_LAST_UPDATED,
927                         resource.getLightLevelLastUpdatedState(timeZoneProvider.getTimeZone()), fullUpdate);
928                 updateState(CHANNEL_2_LIGHT_LEVEL_ENABLED, resource.getEnabledState(), fullUpdate);
929                 break;
930
931             case MOTION:
932             case CAMERA_MOTION:
933                 updateState(CHANNEL_2_MOTION, resource.getMotionState(), fullUpdate);
934                 updateState(CHANNEL_2_MOTION_LAST_UPDATED,
935                         resource.getMotionLastUpdatedState(timeZoneProvider.getTimeZone()), fullUpdate);
936                 updateState(CHANNEL_2_MOTION_ENABLED, resource.getEnabledState(), fullUpdate);
937                 break;
938
939             case RELATIVE_ROTARY:
940                 if (fullUpdate) {
941                     addSupportedChannel(CHANNEL_2_ROTARY_STEPS);
942                     addSupportedChannel(CHANNEL_2_ROTARY_STEPS_LAST_UPDATED);
943                 } else {
944                     updateState(CHANNEL_2_ROTARY_STEPS, resource.getRotaryStepsState(), fullUpdate);
945                 }
946                 updateState(CHANNEL_2_ROTARY_STEPS_LAST_UPDATED,
947                         resource.getRotaryStepsLastUpdatedState(timeZoneProvider.getTimeZone()), fullUpdate);
948                 break;
949
950             case TEMPERATURE:
951                 updateState(CHANNEL_2_TEMPERATURE, resource.getTemperatureState(), fullUpdate);
952                 updateState(CHANNEL_2_TEMPERATURE_LAST_UPDATED,
953                         resource.getTemperatureLastUpdatedState(timeZoneProvider.getTimeZone()), fullUpdate);
954                 updateState(CHANNEL_2_TEMPERATURE_ENABLED, resource.getEnabledState(), fullUpdate);
955                 break;
956
957             case ZIGBEE_CONNECTIVITY:
958                 updateConnectivityState(resource);
959                 break;
960
961             case SCENE:
962                 updateState(CHANNEL_2_SCENE, resource.getSceneState(), fullUpdate);
963                 break;
964
965             case CONTACT:
966                 updateState(CHANNEL_2_SECURITY_CONTACT, resource.getContactState(), fullUpdate);
967                 updateState(CHANNEL_2_SECURITY_CONTACT_LAST_UPDATED,
968                         resource.getContactLastUpdatedState(timeZoneProvider.getTimeZone()), fullUpdate);
969                 updateState(CHANNEL_2_SECURITY_CONTACT_ENABLED, resource.getEnabledState(), fullUpdate);
970                 break;
971
972             case TAMPER:
973                 updateState(CHANNEL_2_SECURITY_TAMPER, resource.getTamperState(), fullUpdate);
974                 updateState(CHANNEL_2_SECURITY_TAMPER_LAST_UPDATED,
975                         resource.getTamperLastUpdatedState(timeZoneProvider.getTimeZone()), fullUpdate);
976                 break;
977
978             case SMART_SCENE:
979                 updateState(CHANNEL_2_SCENE, resource.getSmartSceneState(), fullUpdate);
980                 break;
981
982             default:
983                 return false;
984         }
985         if (thisResource.getType() == ResourceType.DEVICE) {
986             updateState(CHANNEL_2_LAST_UPDATED, new DateTimeType(), fullUpdate);
987         }
988         return true;
989     }
990
991     /**
992      * Check the Zigbee connectivity and set the thing online status accordingly. If the thing is offline then set all
993      * its channel states to undefined, otherwise execute a refresh command to update channels to the latest current
994      * state.
995      *
996      * @param resource a Resource that potentially contains the Zigbee connectivity state.
997      */
998     private void updateConnectivityState(Resource resource) {
999         ZigbeeStatus zigbeeStatus = resource.getZigbeeStatus();
1000         if (Objects.nonNull(zigbeeStatus)) {
1001             logger.debug("{} -> updateConnectivityState() thingStatus:{}, zigbeeStatus:{}", resourceId,
1002                     thing.getStatus(), zigbeeStatus);
1003             hasConnectivityIssue = zigbeeStatus != ZigbeeStatus.CONNECTED;
1004             if (hasConnectivityIssue) {
1005                 if (thing.getStatusInfo().getStatusDetail() != ThingStatusDetail.COMMUNICATION_ERROR) {
1006                     updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.NONE,
1007                             "@text/offline.api2.comm-error.zigbee-connectivity-issue");
1008                     supportedChannelIdSet.forEach(channelId -> updateState(channelId, UnDefType.UNDEF));
1009                 }
1010             } else if (thing.getStatus() != ThingStatus.ONLINE) {
1011                 updateStatus(ThingStatus.ONLINE);
1012                 refreshAllChannels();
1013             }
1014         }
1015     }
1016
1017     /**
1018      * Get all resources needed for building the thing state. Build the forward / reverse contributor lookup maps. Set
1019      * up the final list of channels in the thing.
1020      */
1021     private synchronized void updateDependencies() {
1022         if (!disposing && !updateDependenciesDone) {
1023             logger.debug("{} -> updateDependencies()", resourceId);
1024             try {
1025                 if (!updatePropertiesDone) {
1026                     logger.debug("{} -> updateDependencies() properties not initialized", resourceId);
1027                     return;
1028                 }
1029                 if (!updateSceneContributorsDone && !updateSceneContributors()) {
1030                     logger.debug("{} -> updateDependencies() scenes not initialized", resourceId);
1031                     return;
1032                 }
1033                 updateLookups();
1034                 updateServiceContributors();
1035                 updateChannelList();
1036                 updateChannelItemLinksFromLegacy();
1037                 if (!hasConnectivityIssue) {
1038                     updateStatus(ThingStatus.ONLINE);
1039                 }
1040                 updateDependenciesDone = true;
1041             } catch (ApiException e) {
1042                 logger.debug("{} -> updateDependencies() {}", resourceId, e.getMessage(), e);
1043                 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
1044             } catch (AssetNotLoadedException e) {
1045                 logger.debug("{} -> updateDependencies() {}", resourceId, e.getMessage(), e);
1046                 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
1047                         "@text/offline.api2.conf-error.assets-not-loaded");
1048             } catch (InterruptedException e) {
1049             }
1050         }
1051     }
1052
1053     /**
1054      * Process the incoming Resource to initialize the fixed resp. timed effects channel.
1055      *
1056      * @param resource a Resource possibly containing a fixed and/or timed effects element.
1057      */
1058     public void updateEffectChannel(Resource resource) {
1059         Effects fixedEffects = resource.getFixedEffects();
1060         TimedEffects timedEffects = resource.getTimedEffects();
1061         List<StateOption> stateOptions = Stream
1062                 .concat(Objects.nonNull(fixedEffects) ? fixedEffects.getStatusValues().stream() : Stream.empty(),
1063                         Objects.nonNull(timedEffects) ? timedEffects.getStatusValues().stream() : Stream.empty())
1064                 .map(effect -> {
1065                     String effectName = EffectType.of(effect).name();
1066                     return new StateOption(effectName, effectName);
1067                 }).distinct().collect(Collectors.toList());
1068         if (!stateOptions.isEmpty()) {
1069             stateDescriptionProvider.setStateOptions(new ChannelUID(thing.getUID(), CHANNEL_2_EFFECT), stateOptions);
1070             logger.debug("{} -> updateEffects() found {} effects", resourceId, stateOptions.size());
1071         }
1072     }
1073
1074     /**
1075      * Update the light properties.
1076      *
1077      * @param resource a Resource object containing the property data.
1078      */
1079     private synchronized void updateLightProperties(Resource resource) {
1080         if (!disposing && !updateLightPropertiesDone) {
1081             logger.debug("{} -> updateLightProperties()", resourceId);
1082
1083             Dimming dimming = resource.getDimming();
1084             thing.setProperty(PROPERTY_DIMMING_RANGE, Objects.nonNull(dimming) ? dimming.toPropertyValue() : null);
1085
1086             MirekSchema mirekSchema = resource.getMirekSchema();
1087             thing.setProperty(PROPERTY_COLOR_TEMP_RANGE,
1088                     Objects.nonNull(mirekSchema) ? mirekSchema.toPropertyValue() : null);
1089
1090             ColorXy colorXy = resource.getColorXy();
1091             Gamut2 gamut = Objects.nonNull(colorXy) ? colorXy.getGamut2() : null;
1092             thing.setProperty(PROPERTY_COLOR_GAMUT, Objects.nonNull(gamut) ? gamut.toPropertyValue() : null);
1093
1094             updateLightPropertiesDone = true;
1095         }
1096     }
1097
1098     /**
1099      * Initialize the lookup maps of resources that contribute to the thing state.
1100      */
1101     private void updateLookups() {
1102         if (!disposing) {
1103             logger.debug("{} -> updateLookups()", resourceId);
1104             // get supported services
1105             List<ResourceReference> services = thisResource.getServiceReferences();
1106
1107             // add supported services to contributorsCache
1108             serviceContributorsCache.clear();
1109             serviceContributorsCache.putAll(services.stream()
1110                     .collect(Collectors.toMap(ResourceReference::getId, r -> new Resource(r.getType()))));
1111
1112             // add supported services to commandResourceIds
1113             commandResourceIds.clear();
1114             commandResourceIds.putAll(services.stream() // use a 'mergeFunction' to prevent duplicates
1115                     .collect(Collectors.toMap(ResourceReference::getType, ResourceReference::getId, (r1, r2) -> r1)));
1116         }
1117     }
1118
1119     /**
1120      * Update the primary device properties.
1121      *
1122      * @param resource a Resource object containing the property data.
1123      */
1124     private synchronized void updateProperties(Resource resource) {
1125         if (!disposing && !updatePropertiesDone) {
1126             logger.debug("{} -> updateProperties()", resourceId);
1127             Map<String, String> properties = new HashMap<>(thing.getProperties());
1128
1129             // resource data
1130             properties.put(PROPERTY_RESOURCE_TYPE, thisResource.getType().toString());
1131             properties.put(PROPERTY_RESOURCE_NAME, thisResource.getName());
1132
1133             // owner information
1134             ResourceReference owner = thisResource.getOwner();
1135             if (Objects.nonNull(owner)) {
1136                 String ownerId = owner.getId();
1137                 if (Objects.nonNull(ownerId)) {
1138                     properties.put(PROPERTY_OWNER, ownerId);
1139                 }
1140                 ResourceType ownerType = owner.getType();
1141                 properties.put(PROPERTY_OWNER_TYPE, ownerType.toString());
1142             }
1143
1144             // metadata
1145             MetaData metaData = thisResource.getMetaData();
1146             if (Objects.nonNull(metaData)) {
1147                 properties.put(PROPERTY_RESOURCE_ARCHETYPE, metaData.getArchetype().toString());
1148             }
1149
1150             // product data
1151             ProductData productData = thisResource.getProductData();
1152             if (Objects.nonNull(productData)) {
1153                 String modelId = productData.getModelId();
1154
1155                 // standard properties
1156                 properties.put(PROPERTY_RESOURCE_ID, resourceId);
1157                 properties.put(Thing.PROPERTY_MODEL_ID, modelId);
1158                 properties.put(Thing.PROPERTY_VENDOR, productData.getManufacturerName());
1159                 properties.put(Thing.PROPERTY_FIRMWARE_VERSION, productData.getSoftwareVersion());
1160                 String hardwarePlatformType = productData.getHardwarePlatformType();
1161                 if (Objects.nonNull(hardwarePlatformType)) {
1162                     properties.put(Thing.PROPERTY_HARDWARE_VERSION, hardwarePlatformType);
1163                 }
1164
1165                 // hue specific properties
1166                 properties.put(PROPERTY_PRODUCT_NAME, productData.getProductName());
1167                 properties.put(PROPERTY_PRODUCT_ARCHETYPE, productData.getProductArchetype().toString());
1168                 properties.put(PROPERTY_PRODUCT_CERTIFIED, productData.getCertified().toString());
1169
1170                 // Check device for needed work-arounds.
1171                 if (LK_WISER_DIMMER_MODEL_ID.equals(modelId)) {
1172                     // Apply transition time as a workaround for LK Wiser Dimmer firmware bug.
1173                     // Additional details here: https://techblog.vindvejr.dk/?p=455
1174                     applyOffTransitionWorkaround = true;
1175                     logger.debug("{} -> enabling work-around for turning off LK Wiser Dimmer", resourceId);
1176                 }
1177             }
1178
1179             thing.setProperties(properties);
1180             updatePropertiesDone = true;
1181         }
1182     }
1183
1184     /**
1185      * Execute an HTTP GET command to fetch the resources data for the referenced resource.
1186      *
1187      * @param reference to the required resource.
1188      * @throws ApiException if a communication error occurred.
1189      * @throws AssetNotLoadedException if one of the assets is not loaded.
1190      * @throws InterruptedException
1191      */
1192     private void updateResource(ResourceReference reference)
1193             throws ApiException, AssetNotLoadedException, InterruptedException {
1194         if (!disposing) {
1195             logger.debug("{} -> updateResource() from resource {}", resourceId, reference);
1196             getBridgeHandler().getResources(reference).getResources().stream()
1197                     .forEach(resource -> onResource(resource));
1198         }
1199     }
1200
1201     /**
1202      * Fetch the full list of normal resp. smart scenes from the bridge, and call
1203      * {@code updateSceneContributors(List<Resource> allScenes)}
1204      *
1205      * @throws ApiException if a communication error occurred.
1206      * @throws AssetNotLoadedException if one of the assets is not loaded.
1207      * @throws InterruptedException
1208      */
1209     public boolean updateSceneContributors() throws ApiException, AssetNotLoadedException, InterruptedException {
1210         if (!disposing && !updateSceneContributorsDone) {
1211             List<Resource> allScenes = new ArrayList<>();
1212             for (ResourceType type : SUPPORTED_SCENE_TYPES) {
1213                 allScenes.addAll(getBridgeHandler().getResources(new ResourceReference().setType(type)).getResources());
1214             }
1215             updateSceneContributors(allScenes);
1216         }
1217         return updateSceneContributorsDone;
1218     }
1219
1220     /**
1221      * Process the incoming list of normal resp. smart scene resources to find those which contribute to this thing. And
1222      * if there are any, include a scene channel in the supported channel list, and populate its respective state
1223      * options.
1224      *
1225      * @param allScenes the full list of normal resp. smart scene resources.
1226      */
1227     public synchronized boolean updateSceneContributors(List<Resource> allScenes) {
1228         if (!disposing && !updateSceneContributorsDone) {
1229             sceneContributorsCache.clear();
1230             sceneResourceEntries.clear();
1231
1232             ResourceReference thisReference = getResourceReference();
1233             Set<Resource> scenes = allScenes.stream().filter(s -> thisReference.equals(s.getGroup()))
1234                     .collect(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Resource::getName))));
1235
1236             if (!scenes.isEmpty()) {
1237                 sceneContributorsCache.putAll(scenes.stream().collect(Collectors.toMap(s -> s.getId(), s -> s)));
1238                 sceneResourceEntries.putAll(scenes.stream().collect(Collectors.toMap(s -> s.getName(), s -> s)));
1239
1240                 State state = scenes.stream().filter(s -> s.getSceneActive().orElse(false)).map(s -> s.getSceneState())
1241                         .findAny().orElse(UnDefType.UNDEF);
1242
1243                 // create scene channel if it is missing
1244                 if (getThing().getChannel(CHANNEL_2_SCENE) == null) {
1245                     updateThing(editThing()
1246                             .withChannel(ChannelBuilder.create(new ChannelUID(getThing().getUID(), CHANNEL_2_SCENE))
1247                                     .withType(new ChannelTypeUID(BINDING_ID, CHANNEL_TYPE_2_SCENE)).build())
1248                             .build());
1249                 }
1250
1251                 updateState(CHANNEL_2_SCENE, state, true);
1252
1253                 stateDescriptionProvider.setStateOptions(new ChannelUID(thing.getUID(), CHANNEL_2_SCENE), scenes
1254                         .stream().map(s -> s.getName()).map(n -> new StateOption(n, n)).collect(Collectors.toList()));
1255
1256                 logger.debug("{} -> updateSceneContributors() found {} normal resp. smart scenes", resourceId,
1257                         scenes.size());
1258             }
1259             updateSceneContributorsDone = true;
1260         }
1261         return updateSceneContributorsDone;
1262     }
1263
1264     /**
1265      * Execute a series of HTTP GET commands to fetch the resource data for all service resources that contribute to the
1266      * thing state.
1267      *
1268      * @throws ApiException if a communication error occurred.
1269      * @throws AssetNotLoadedException if one of the assets is not loaded.
1270      * @throws InterruptedException
1271      */
1272     private void updateServiceContributors() throws ApiException, AssetNotLoadedException, InterruptedException {
1273         if (!disposing) {
1274             logger.debug("{} -> updateServiceContributors() called for {} contributors", resourceId,
1275                     serviceContributorsCache.size());
1276             ResourceReference reference = new ResourceReference();
1277             for (var entry : serviceContributorsCache.entrySet()) {
1278                 updateResource(reference.setId(entry.getKey()).setType(entry.getValue().getType()));
1279             }
1280         }
1281     }
1282
1283     /**
1284      * Update the channel state, and if appropriate add the channel ID to the set of supportedChannelIds. Calls either
1285      * OH core updateState() or triggerChannel() methods depending on the channel kind.
1286      *
1287      * Note: the particular 'UnDefType.UNDEF' value of the state argument is used to specially indicate the undefined
1288      * state, but yet that its channel shall nevertheless continue to be present in the thing.
1289      *
1290      * @param channelID the id of the channel.
1291      * @param state the new state of the channel.
1292      * @param fullUpdate if true always update the channel, otherwise only update if state is not 'UNDEF'.
1293      */
1294     private void updateState(String channelID, State state, boolean fullUpdate) {
1295         boolean isDefined = state != UnDefType.NULL;
1296         Channel channel = thing.getChannel(channelID);
1297
1298         if ((fullUpdate || isDefined) && Objects.nonNull(channel)) {
1299             logger.debug("{} -> updateState() '{}' update with '{}' (fullUpdate:{}, isDefined:{})", resourceId,
1300                     channelID, state, fullUpdate, isDefined);
1301
1302             switch (channel.getKind()) {
1303                 case STATE:
1304                     updateState(channelID, state);
1305                     break;
1306
1307                 case TRIGGER:
1308                     if (state instanceof DecimalType) {
1309                         triggerChannel(channelID, String.valueOf(((DecimalType) state).intValue()));
1310                     }
1311             }
1312         }
1313         if (fullUpdate && isDefined) {
1314             addSupportedChannel(channelID);
1315         }
1316     }
1317
1318     /**
1319      * Check if a PROPERTY_LEGACY_THING_UID value was set by the discovery process, and if so, clone the legacy thing's
1320      * settings into this thing.
1321      */
1322     private void updateThingFromLegacy() {
1323         if (isInitialized()) {
1324             logger.warn("Cannot update thing '{}' from legacy thing since handler already initialized.",
1325                     thing.getUID());
1326             return;
1327         }
1328         Map<String, String> properties = thing.getProperties();
1329         String legacyThingUID = properties.get(PROPERTY_LEGACY_THING_UID);
1330         if (Objects.nonNull(legacyThingUID)) {
1331             Thing legacyThing = thingRegistry.get(new ThingUID(legacyThingUID));
1332             if (Objects.nonNull(legacyThing)) {
1333                 ThingBuilder editBuilder = editThing();
1334
1335                 String location = legacyThing.getLocation();
1336                 if (Objects.nonNull(location) && !location.isBlank()) {
1337                     editBuilder = editBuilder.withLocation(location);
1338                 }
1339
1340                 // save list of legacyLinkedChannelUIDs for use after channel list is initialised
1341                 legacyLinkedChannelUIDs.clear();
1342                 legacyLinkedChannelUIDs.addAll(legacyThing.getChannels().stream().map(Channel::getUID)
1343                         .filter(uid -> REPLICATE_CHANNEL_ID_MAP.containsKey(uid.getId())
1344                                 && itemChannelLinkRegistry.isLinked(uid))
1345                         .collect(Collectors.toList()));
1346
1347                 Map<String, String> newProperties = new HashMap<>(properties);
1348                 newProperties.remove(PROPERTY_LEGACY_THING_UID);
1349
1350                 updateThing(editBuilder.withProperties(newProperties).build());
1351             }
1352         }
1353     }
1354 }