2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.hue.internal.api.dto.clip2;
15 import java.math.BigDecimal;
16 import java.math.MathContext;
17 import java.math.RoundingMode;
18 import java.time.Duration;
19 import java.time.Instant;
20 import java.time.ZoneId;
21 import java.time.ZonedDateTime;
22 import java.util.List;
24 import java.util.Objects;
25 import java.util.Optional;
27 import org.eclipse.jdt.annotation.NonNullByDefault;
28 import org.eclipse.jdt.annotation.Nullable;
29 import org.openhab.binding.hue.internal.api.dto.clip2.enums.ActionType;
30 import org.openhab.binding.hue.internal.api.dto.clip2.enums.ButtonEventType;
31 import org.openhab.binding.hue.internal.api.dto.clip2.enums.ContactStateType;
32 import org.openhab.binding.hue.internal.api.dto.clip2.enums.EffectType;
33 import org.openhab.binding.hue.internal.api.dto.clip2.enums.ResourceType;
34 import org.openhab.binding.hue.internal.api.dto.clip2.enums.SceneRecallAction;
35 import org.openhab.binding.hue.internal.api.dto.clip2.enums.SmartSceneRecallAction;
36 import org.openhab.binding.hue.internal.api.dto.clip2.enums.SmartSceneState;
37 import org.openhab.binding.hue.internal.api.dto.clip2.enums.TamperStateType;
38 import org.openhab.binding.hue.internal.api.dto.clip2.enums.ZigbeeStatus;
39 import org.openhab.binding.hue.internal.exceptions.DTOPresentButEmptyException;
40 import org.openhab.core.library.types.DateTimeType;
41 import org.openhab.core.library.types.DecimalType;
42 import org.openhab.core.library.types.HSBType;
43 import org.openhab.core.library.types.OnOffType;
44 import org.openhab.core.library.types.OpenClosedType;
45 import org.openhab.core.library.types.PercentType;
46 import org.openhab.core.library.types.QuantityType;
47 import org.openhab.core.library.types.StringType;
48 import org.openhab.core.library.unit.SIUnits;
49 import org.openhab.core.library.unit.Units;
50 import org.openhab.core.types.Command;
51 import org.openhab.core.types.State;
52 import org.openhab.core.types.UnDefType;
53 import org.openhab.core.util.ColorUtil;
54 import org.openhab.core.util.ColorUtil.Gamut;
56 import com.google.gson.JsonElement;
57 import com.google.gson.JsonObject;
58 import com.google.gson.annotations.SerializedName;
61 * Complete Resource information DTO for CLIP 2.
63 * Note: all fields are @Nullable because some cases do not (must not) use them.
65 * @author Andrew Fiddian-Green - Initial contribution
68 public class Resource {
70 public static final double PERCENT_DELTA = 30f;
71 public static final MathContext PERCENT_MATH_CONTEXT = new MathContext(4, RoundingMode.HALF_UP);
74 * The SSE event mechanism sends resources in a sparse (skeleton) format that only includes state fields whose
75 * values have changed. A sparse resource does not contain the full state of the resource. And the absence of any
76 * field from such a resource does not indicate that the field value is UNDEF, but rather that the value is the same
77 * as what it was previously set to by the last non-sparse resource.
79 private transient boolean hasSparseData;
81 private @Nullable String type;
82 private @Nullable String id;
83 private @Nullable @SerializedName("bridge_id") String bridgeId;
84 private @Nullable @SerializedName("id_v1") String idV1;
85 private @Nullable ResourceReference owner;
86 private @Nullable MetaData metadata;
87 private @Nullable @SerializedName("product_data") ProductData productData;
88 private @Nullable List<ResourceReference> services;
89 private @Nullable OnState on;
90 private @Nullable Dimming dimming;
91 private @Nullable @SerializedName("color_temperature") ColorTemperature colorTemperature;
92 private @Nullable ColorXy color;
93 private @Nullable Alerts alert;
94 private @Nullable Effects effects;
95 private @Nullable @SerializedName("timed_effects") TimedEffects timedEffects;
96 private @Nullable ResourceReference group;
97 private @Nullable List<ActionEntry> actions;
98 private @Nullable Recall recall;
99 private @Nullable Boolean enabled;
100 private @Nullable LightLevel light;
101 private @Nullable Button button;
102 private @Nullable Temperature temperature;
103 private @Nullable Motion motion;
104 private @Nullable @SerializedName("power_state") Power powerState;
105 private @Nullable @SerializedName("relative_rotary") RelativeRotary relativeRotary;
106 private @Nullable List<ResourceReference> children;
107 private @Nullable JsonElement status;
108 private @Nullable Dynamics dynamics;
109 private @Nullable @SerializedName("contact_report") ContactReport contactReport;
110 private @Nullable @SerializedName("tamper_reports") List<TamperReport> tamperReports;
111 private @Nullable String state;
116 * @param resourceType
118 public Resource(@Nullable ResourceType resourceType) {
119 if (Objects.nonNull(resourceType)) {
120 setType(resourceType);
125 * Check if <code>light</code> or <code>grouped_light</code> resource contains any
126 * relevant fields to process according to its type.
128 * As an example, {@link #colorTemperature} is relevant for a <code>light</code>
129 * resource because it's needed for updating the color-temperature channels.
131 * @return true is resource contains any relevant field
133 public boolean hasAnyRelevantField() {
134 return switch (getType()) {
135 // https://developers.meethue.com/develop/hue-api-v2/api-reference/#resource_light_get
136 case LIGHT -> hasHSBField() || colorTemperature != null || dynamics != null || effects != null
137 || timedEffects != null;
138 // https://developers.meethue.com/develop/hue-api-v2/api-reference/#resource_grouped_light_get
139 case GROUPED_LIGHT -> on != null || dimming != null || alert != null;
140 default -> throw new IllegalStateException(type + " is not supported by hasAnyRelevantField()");
145 * Check if resource contains any field which is needed to represent an HSB value
146 * (<code>on</code>, <code>dimming</code> or <code>color</code>).
148 * @return true if resource has any HSB field
150 public boolean hasHSBField() {
151 return on != null || dimming != null || color != null;
154 public @Nullable List<ActionEntry> getActions() {
158 public @Nullable Alerts getAlerts() {
162 public State getAlertState() {
163 Alerts alerts = this.alert;
164 if (Objects.nonNull(alerts)) {
165 if (!alerts.getActionValues().isEmpty()) {
166 ActionType alertType = alerts.getAction();
167 if (Objects.nonNull(alertType)) {
168 return new StringType(alertType.name());
170 return new StringType(ActionType.NO_ACTION.name());
173 return UnDefType.NULL;
176 public String getArchetype() {
177 MetaData metaData = getMetaData();
178 if (Objects.nonNull(metaData)) {
179 return metaData.getArchetype().toString();
181 return getType().toString();
184 public State getBatteryLevelState() {
185 Power powerState = this.powerState;
186 return Objects.nonNull(powerState) ? powerState.getBatteryLevelState() : UnDefType.NULL;
189 public State getBatteryLowState() {
190 Power powerState = this.powerState;
191 return Objects.nonNull(powerState) ? powerState.getBatteryLowState() : UnDefType.NULL;
194 public @Nullable String getBridgeId() {
195 String bridgeId = this.bridgeId;
196 return Objects.isNull(bridgeId) || bridgeId.isBlank() ? null : bridgeId;
200 * Get the brightness as a PercentType. If off the brightness is 0, otherwise use dimming value.
202 * @return a PercentType with the dimming state, or UNDEF, or NULL
204 public State getBrightnessState() {
205 Dimming dimming = this.dimming;
206 if (Objects.nonNull(dimming)) {
208 // if off the brightness is 0, otherwise it is dimming value
209 OnState on = this.on;
210 double brightness = Objects.nonNull(on) && !on.isOn() ? 0f
211 : Math.max(0f, Math.min(100f, dimming.getBrightness()));
212 return new PercentType(new BigDecimal(brightness, PERCENT_MATH_CONTEXT));
213 } catch (DTOPresentButEmptyException e) {
214 return UnDefType.UNDEF; // indicates the DTO is present but its inner fields are missing
217 return UnDefType.NULL;
220 public @Nullable Button getButton() {
225 * Get the state corresponding to a button's last event value multiplied by the controlId found for it in the given
226 * controlIds map. States are decimal values formatted like '1002' where the first digit is the button's controlId
227 * and the last digit is the ordinal value of the button's last event.
229 * @param controlIds the map of control ids to be referenced.
232 public State getButtonEventState(Map<String, Integer> controlIds) {
233 Button button = this.button;
234 if (button == null) {
235 return UnDefType.NULL;
237 ButtonEventType event;
238 ButtonReport buttonReport = button.getButtonReport();
239 if (buttonReport == null) {
240 event = button.getLastEvent();
242 event = buttonReport.getLastEvent();
245 return UnDefType.NULL;
247 return new DecimalType((controlIds.getOrDefault(getId(), 0).intValue() * 1000) + event.ordinal());
250 public State getButtonLastUpdatedState(ZoneId zoneId) {
251 Button button = this.button;
252 if (button == null) {
253 return UnDefType.NULL;
255 ButtonReport buttonReport = button.getButtonReport();
256 if (buttonReport == null) {
257 return UnDefType.UNDEF;
259 Instant lastChanged = buttonReport.getLastChanged();
260 if (Instant.EPOCH.equals(lastChanged)) {
261 return UnDefType.UNDEF;
263 return new DateTimeType(ZonedDateTime.ofInstant(lastChanged, zoneId));
266 public List<ResourceReference> getChildren() {
267 List<ResourceReference> children = this.children;
268 return Objects.nonNull(children) ? children : List.of();
272 * Get the color as an HSBType. This returns an HSB that is based on an amalgamation of the color xy, dimming, and
273 * on/off JSON elements. It takes its 'H' and 'S' parts from the 'ColorXy' JSON element, and its 'B' part from the
274 * on/off resp. dimming JSON elements. If off the B part is 0, otherwise it is the dimming element value. Note: this
275 * method is only to be used on cached state DTOs which already have a defined color gamut.
277 * @return an HSBType containing the current color and brightness level, or UNDEF or NULL.
279 public State getColorState() {
280 ColorXy color = this.color;
281 if (Objects.nonNull(color)) {
283 Gamut gamut = color.getGamut();
284 gamut = Objects.nonNull(gamut) ? gamut : ColorUtil.DEFAULT_GAMUT;
285 HSBType hsb = ColorUtil.xyToHsb(color.getXY(), gamut);
286 OnState on = this.on;
287 Dimming dimming = this.dimming;
288 double brightness = Objects.nonNull(on) && !on.isOn() ? 0
289 : Objects.nonNull(dimming) ? Math.max(0, Math.min(100, dimming.getBrightness())) : 50;
290 return new HSBType(hsb.getHue(), hsb.getSaturation(),
291 new PercentType(new BigDecimal(brightness, PERCENT_MATH_CONTEXT)));
292 } catch (DTOPresentButEmptyException e) {
293 return UnDefType.UNDEF; // indicates the DTO is present but its inner fields are missing
296 return UnDefType.NULL;
299 public @Nullable ColorTemperature getColorTemperature() {
300 return colorTemperature;
303 public State getColorTemperatureAbsoluteState() {
304 ColorTemperature colorTemp = colorTemperature;
305 if (Objects.nonNull(colorTemp)) {
307 QuantityType<?> colorTemperature = colorTemp.getAbsolute();
308 if (Objects.nonNull(colorTemperature)) {
309 return colorTemperature;
311 } catch (DTOPresentButEmptyException e) {
312 return UnDefType.UNDEF; // indicates the DTO is present but its inner fields are missing
315 return UnDefType.NULL;
319 * Get the colour temperature in percent. Note: this method is only to be used on cached state DTOs which already
320 * have a defined mirek schema.
322 * @return a PercentType with the colour temperature percentage.
324 public State getColorTemperaturePercentState() {
325 ColorTemperature colorTemperature = this.colorTemperature;
326 if (Objects.nonNull(colorTemperature)) {
328 Double percent = colorTemperature.getPercent();
329 if (Objects.nonNull(percent)) {
330 return new PercentType(new BigDecimal(percent, PERCENT_MATH_CONTEXT));
332 } catch (DTOPresentButEmptyException e) {
333 return UnDefType.UNDEF; // indicates the DTO is present but its inner fields are missing
336 return UnDefType.NULL;
339 public @Nullable ColorXy getColorXy() {
344 * Return an HSB where the HS part is derived from the color xy JSON element (only), so the B part is 100%
346 * @return an HSBType.
348 public State getColorXyState() {
349 ColorXy color = this.color;
350 if (Objects.nonNull(color)) {
352 Gamut gamut = color.getGamut();
353 gamut = Objects.nonNull(gamut) ? gamut : ColorUtil.DEFAULT_GAMUT;
354 HSBType hsb = ColorUtil.xyToHsb(color.getXY(), gamut);
355 return new HSBType(hsb.getHue(), hsb.getSaturation(), PercentType.HUNDRED);
356 } catch (DTOPresentButEmptyException e) {
357 return UnDefType.UNDEF; // indicates the DTO is present but its inner fields are missing
360 return UnDefType.NULL;
363 public State getContactLastUpdatedState(ZoneId zoneId) {
364 ContactReport contactReport = this.contactReport;
365 return Objects.nonNull(contactReport)
366 ? new DateTimeType(ZonedDateTime.ofInstant(contactReport.getLastChanged(), zoneId))
370 public State getContactState() {
371 ContactReport contactReport = this.contactReport;
372 return Objects.isNull(contactReport) ? UnDefType.NULL
373 : ContactStateType.CONTACT == contactReport.getContactState() ? OpenClosedType.CLOSED
374 : OpenClosedType.OPEN;
377 public int getControlId() {
378 MetaData metadata = this.metadata;
379 return Objects.nonNull(metadata) ? metadata.getControlId() : 0;
382 public @Nullable Dimming getDimming() {
387 * Return a PercentType which is derived from the dimming JSON element (only).
389 * @return a PercentType.
391 public State getDimmingState() {
392 Dimming dimming = this.dimming;
393 if (Objects.nonNull(dimming)) {
395 double dimmingValue = Math.max(0f, Math.min(100f, dimming.getBrightness()));
396 return new PercentType(new BigDecimal(dimmingValue, PERCENT_MATH_CONTEXT));
397 } catch (DTOPresentButEmptyException e) {
398 return UnDefType.UNDEF; // indicates the DTO is present but its inner fields are missing
401 return UnDefType.NULL;
404 public @Nullable Effects getFixedEffects() {
409 * Get the amalgamated effect state. The result may be either from an 'effects' field or from a 'timedEffects'
410 * field. If both fields are missing it returns UnDefType.NULL, otherwise if either field is present and has an
411 * active value (other than EffectType.NO_EFFECT) it returns a StringType of the name of the respective active
412 * effect; and if none of the above apply, it returns a StringType of 'NO_EFFECT'.
414 * @return either a StringType value or UnDefType.NULL
416 public State getEffectState() {
417 Effects effects = this.effects;
418 TimedEffects timedEffects = this.timedEffects;
419 if (Objects.isNull(effects) && Objects.isNull(timedEffects)) {
420 return UnDefType.NULL;
422 EffectType effect = Objects.nonNull(effects) ? effects.getStatus() : null;
423 if (Objects.nonNull(effect) && effect != EffectType.NO_EFFECT) {
424 return new StringType(effect.name());
426 EffectType timedEffect = Objects.nonNull(timedEffects) ? timedEffects.getStatus() : null;
427 if (Objects.nonNull(timedEffect) && timedEffect != EffectType.NO_EFFECT) {
428 return new StringType(timedEffect.name());
430 return new StringType(EffectType.NO_EFFECT.name());
433 public @Nullable Boolean getEnabled() {
437 public State getEnabledState() {
438 Boolean enabled = this.enabled;
439 return Objects.nonNull(enabled) ? OnOffType.from(enabled.booleanValue()) : UnDefType.NULL;
442 public @Nullable Gamut getGamut() {
443 ColorXy color = this.color;
444 return Objects.nonNull(color) ? color.getGamut() : null;
447 public @Nullable ResourceReference getGroup() {
451 public String getId() {
453 return Objects.nonNull(id) ? id : "";
456 public String getIdV1() {
457 String idV1 = this.idV1;
458 return Objects.nonNull(idV1) ? idV1 : "";
461 public @Nullable LightLevel getLightLevel() {
465 public State getLightLevelState() {
466 LightLevel lightLevel = this.light;
467 if (lightLevel == null) {
468 return UnDefType.NULL;
470 LightLevelReport lightLevelReport = lightLevel.getLightLevelReport();
471 if (lightLevelReport == null) {
472 return lightLevel.getLightLevelState();
474 return new QuantityType<>(Math.pow(10f, (double) lightLevelReport.getLightLevel() / 10000f) - 1f, Units.LUX);
477 public State getLightLevelLastUpdatedState(ZoneId zoneId) {
478 LightLevel lightLevel = this.light;
479 if (lightLevel == null) {
480 return UnDefType.NULL;
482 LightLevelReport lightLevelReport = lightLevel.getLightLevelReport();
483 if (lightLevelReport == null) {
484 return UnDefType.UNDEF;
486 Instant lastChanged = lightLevelReport.getLastChanged();
487 if (Instant.EPOCH.equals(lastChanged)) {
488 return UnDefType.UNDEF;
490 return new DateTimeType(ZonedDateTime.ofInstant(lastChanged, zoneId));
493 public @Nullable MetaData getMetaData() {
497 public @Nullable Double getMinimumDimmingLevel() {
498 Dimming dimming = this.dimming;
499 return Objects.nonNull(dimming) ? dimming.getMinimumDimmingLevel() : null;
502 public @Nullable MirekSchema getMirekSchema() {
503 ColorTemperature colorTemp = this.colorTemperature;
504 return Objects.nonNull(colorTemp) ? colorTemp.getMirekSchema() : null;
507 public @Nullable Motion getMotion() {
511 public State getMotionState() {
512 Motion motion = this.motion;
513 if (motion == null) {
514 return UnDefType.NULL;
516 MotionReport motionReport = motion.getMotionReport();
517 if (motionReport == null) {
518 return motion.getMotionState();
520 return OnOffType.from(motionReport.isMotion());
523 public State getMotionLastUpdatedState(ZoneId zoneId) {
524 Motion motion = this.motion;
525 if (motion == null) {
526 return UnDefType.NULL;
528 MotionReport motionReport = motion.getMotionReport();
529 if (motionReport == null) {
530 return UnDefType.UNDEF;
532 Instant lastChanged = motionReport.getLastChanged();
533 if (Instant.EPOCH.equals(lastChanged)) {
534 return UnDefType.UNDEF;
536 return new DateTimeType(ZonedDateTime.ofInstant(lastChanged, zoneId));
539 public State getMotionValidState() {
540 Motion motion = this.motion;
541 return Objects.nonNull(motion) ? motion.getMotionValidState() : UnDefType.NULL;
544 public String getName() {
545 MetaData metaData = getMetaData();
546 if (Objects.nonNull(metaData)) {
547 String name = metaData.getName();
548 if (Objects.nonNull(name)) {
552 return getType().toString();
556 * Return the state of the On/Off element (only).
558 public State getOnOffState() {
560 OnState on = this.on;
561 return Objects.nonNull(on) ? OnOffType.from(on.isOn()) : UnDefType.NULL;
562 } catch (DTOPresentButEmptyException e) {
563 return UnDefType.UNDEF; // indicates the DTO is present but its inner fields are missing
567 public @Nullable OnState getOnState() {
571 public @Nullable ResourceReference getOwner() {
575 public @Nullable Power getPowerState() {
579 public @Nullable ProductData getProductData() {
583 public String getProductName() {
584 ProductData productData = getProductData();
585 if (Objects.nonNull(productData)) {
586 return productData.getProductName();
588 return getType().toString();
591 public @Nullable Recall getRecall() {
595 public @Nullable RelativeRotary getRelativeRotary() {
596 return relativeRotary;
599 public State getRotaryStepsState() {
600 RelativeRotary relativeRotary = this.relativeRotary;
601 if (relativeRotary == null) {
602 return UnDefType.NULL;
604 RotaryReport rotaryReport = relativeRotary.getRotaryReport();
605 if (rotaryReport == null) {
606 return relativeRotary.getStepsState();
608 Rotation rotation = rotaryReport.getRotation();
609 if (rotation == null) {
610 return UnDefType.NULL;
612 return rotation.getStepsState();
615 public State getRotaryStepsLastUpdatedState(ZoneId zoneId) {
616 RelativeRotary relativeRotary = this.relativeRotary;
617 if (relativeRotary == null) {
618 return UnDefType.NULL;
620 RotaryReport rotaryReport = relativeRotary.getRotaryReport();
621 if (rotaryReport == null) {
622 return UnDefType.UNDEF;
624 Instant lastChanged = rotaryReport.getLastChanged();
625 if (Instant.EPOCH.equals(lastChanged)) {
626 return UnDefType.UNDEF;
628 return new DateTimeType(ZonedDateTime.ofInstant(lastChanged, zoneId));
632 * Check if the scene resource contains a 'status.active' element. If such an element is present, returns a Boolean
633 * Optional whose value depends on the value of that element, or an empty Optional if it is not.
635 * @return true, false, or empty.
637 public Optional<Boolean> getSceneActive() {
638 if (ResourceType.SCENE == getType()) {
639 JsonElement status = this.status;
640 if (Objects.nonNull(status) && status.isJsonObject()) {
641 JsonElement active = ((JsonObject) status).get("active");
642 if (Objects.nonNull(active) && active.isJsonPrimitive()) {
643 return Optional.of(!"inactive".equalsIgnoreCase(active.getAsString()));
647 return Optional.empty();
651 * If the getSceneActive() optional result is empty return 'UnDefType.NULL'. Otherwise if the optional result is
652 * present and 'true' (i.e. the scene is active) return the scene name. Or finally (the optional result is present
653 * and 'false') return 'UnDefType.UNDEF'.
655 * @return either 'UnDefType.NULL', a StringType containing the (active) scene name, or 'UnDefType.UNDEF'.
657 public State getSceneState() {
658 return getSceneActive().map(a -> a ? new StringType(getName()) : UnDefType.UNDEF).orElse(UnDefType.NULL);
662 * Check if the smart scene resource contains a 'state' element. If such an element is present, returns a Boolean
663 * Optional whose value depends on the value of that element, or an empty Optional if it is not.
665 * @return true, false, or empty.
667 public Optional<Boolean> getSmartSceneActive() {
668 if (ResourceType.SMART_SCENE == getType()) {
669 String state = this.state;
670 if (Objects.nonNull(state)) {
671 return Optional.of(SmartSceneState.ACTIVE == SmartSceneState.of(state));
674 return Optional.empty();
678 * If the getSmartSceneActive() optional result is empty return 'UnDefType.NULL'. Otherwise if the optional result
679 * is present and 'true' (i.e. the scene is active) return the smart scene name. Or finally (the optional result is
680 * present and 'false') return 'UnDefType.UNDEF'.
682 * @return either 'UnDefType.NULL', a StringType containing the (active) scene name, or 'UnDefType.UNDEF'.
684 public State getSmartSceneState() {
685 return getSmartSceneActive().map(a -> a ? new StringType(getName()) : UnDefType.UNDEF).orElse(UnDefType.NULL);
688 public List<ResourceReference> getServiceReferences() {
689 List<ResourceReference> services = this.services;
690 return Objects.nonNull(services) ? services : List.of();
693 public JsonObject getStatus() {
694 JsonElement status = this.status;
695 if (Objects.nonNull(status) && status.isJsonObject()) {
696 return status.getAsJsonObject();
698 return new JsonObject();
701 public State getTamperLastUpdatedState(ZoneId zoneId) {
702 TamperReport report = getTamperReportsLatest();
703 return Objects.nonNull(report) ? new DateTimeType(ZonedDateTime.ofInstant(report.getLastChanged(), zoneId))
708 * The the Hue bridge could return its raw list of tamper reports in any order, so sort the list (latest entry
709 * first) according to the respective 'changed' instant and return the first entry i.e. the latest changed entry.
711 * @return the latest changed tamper report
713 private @Nullable TamperReport getTamperReportsLatest() {
714 List<TamperReport> reports = this.tamperReports;
715 return Objects.nonNull(reports)
716 ? reports.stream().sorted((e1, e2) -> e2.getLastChanged().compareTo(e1.getLastChanged())).findFirst()
721 public State getTamperState() {
722 TamperReport report = getTamperReportsLatest();
723 return Objects.nonNull(report)
724 ? TamperStateType.TAMPERED == report.getTamperState() ? OpenClosedType.OPEN : OpenClosedType.CLOSED
728 public @Nullable Temperature getTemperature() {
732 public State getTemperatureState() {
733 Temperature temperature = this.temperature;
734 if (temperature == null) {
735 return UnDefType.NULL;
737 TemperatureReport temperatureReport = temperature.getTemperatureReport();
738 if (temperatureReport == null) {
739 return temperature.getTemperatureState();
741 return new QuantityType<>(temperatureReport.getTemperature(), SIUnits.CELSIUS);
744 public State getTemperatureLastUpdatedState(ZoneId zoneId) {
745 Temperature temperature = this.temperature;
746 if (temperature == null) {
747 return UnDefType.NULL;
749 TemperatureReport temperatureReport = temperature.getTemperatureReport();
750 if (temperatureReport == null) {
751 return UnDefType.UNDEF;
753 Instant lastChanged = temperatureReport.getLastChanged();
754 if (Instant.EPOCH.equals(lastChanged)) {
755 return UnDefType.UNDEF;
757 return new DateTimeType(ZonedDateTime.ofInstant(lastChanged, zoneId));
760 public State getTemperatureValidState() {
761 Temperature temperature = this.temperature;
762 return Objects.nonNull(temperature) ? temperature.getTemperatureValidState() : UnDefType.NULL;
765 public @Nullable TimedEffects getTimedEffects() {
769 public ResourceType getType() {
770 return ResourceType.of(type);
773 public State getZigbeeState() {
774 ZigbeeStatus zigbeeStatus = getZigbeeStatus();
775 return Objects.nonNull(zigbeeStatus) ? new StringType(zigbeeStatus.toString()) : UnDefType.NULL;
778 public @Nullable ZigbeeStatus getZigbeeStatus() {
779 JsonElement status = this.status;
780 if (Objects.nonNull(status) && status.isJsonPrimitive()) {
781 return ZigbeeStatus.of(status.getAsString());
786 public boolean hasFullState() {
787 return !hasSparseData;
791 * Mark that the resource has sparse data.
793 * @return this instance.
795 public Resource markAsSparse() {
796 hasSparseData = true;
800 public Resource setAlerts(Alerts alert) {
805 public Resource setColorTemperature(ColorTemperature colorTemperature) {
806 this.colorTemperature = colorTemperature;
810 public Resource setColorXy(@Nullable ColorXy color) {
815 public Resource setContactReport(ContactReport contactReport) {
816 this.contactReport = contactReport;
820 public Resource setDimming(@Nullable Dimming dimming) {
821 this.dimming = dimming;
825 public Resource setDynamicsDuration(Duration duration) {
826 dynamics = new Dynamics().setDuration(duration);
830 public Resource setFixedEffects(Effects effect) {
831 this.effects = effect;
835 public Resource setEnabled(Command command) {
836 if (command instanceof OnOffType) {
837 this.enabled = ((OnOffType) command) == OnOffType.ON;
842 public Resource setId(String id) {
847 public Resource setMetadata(MetaData metadata) {
848 this.metadata = metadata;
852 public Resource setMirekSchema(@Nullable MirekSchema schema) {
853 ColorTemperature colorTemperature = this.colorTemperature;
854 if (Objects.nonNull(colorTemperature)) {
855 colorTemperature.setMirekSchema(schema);
861 * Set the on/off JSON element (only).
863 * @param command an OnOffTypee command value.
864 * @return this resource instance.
866 public Resource setOnOff(Command command) {
867 if (command instanceof OnOffType) {
868 OnOffType onOff = (OnOffType) command;
869 OnState on = this.on;
870 on = Objects.nonNull(on) ? on : new OnState();
871 on.setOn(OnOffType.ON.equals(onOff));
877 public void setOnState(@Nullable OnState on) {
881 public Resource setRecallAction(SceneRecallAction recallAction) {
882 Recall recall = this.recall;
883 this.recall = ((Objects.nonNull(recall) ? recall : new Recall())).setAction(recallAction);
887 public Resource setRecallAction(SmartSceneRecallAction recallAction) {
888 Recall recall = this.recall;
889 this.recall = ((Objects.nonNull(recall) ? recall : new Recall())).setAction(recallAction);
893 public Resource setRecallDuration(Duration recallDuration) {
894 Recall recall = this.recall;
895 this.recall = ((Objects.nonNull(recall) ? recall : new Recall())).setDuration(recallDuration);
899 public Resource setTamperReports(List<TamperReport> tamperReports) {
900 this.tamperReports = tamperReports;
904 public Resource setTimedEffects(TimedEffects timedEffects) {
905 this.timedEffects = timedEffects;
909 public Resource setTimedEffectsDuration(Duration dynamicsDuration) {
910 TimedEffects timedEffects = this.timedEffects;
911 if (Objects.nonNull(timedEffects)) {
912 timedEffects.setDuration(dynamicsDuration);
917 public Resource setType(ResourceType resourceType) {
918 this.type = resourceType.name().toLowerCase();
923 public String toString() {
925 return String.format("id:%s, type:%s", Objects.nonNull(id) ? id : "?" + " ".repeat(35),
926 getType().name().toLowerCase());