[various] Lamp handlers expose min/max Color Temperature in state description (#17641)

* Lamp handlers expose min/max Colour Temperature in state description
* add color temperature validit and range checks

Signed-off-by: AndrewFG <software@whitebear.ch>
Signed-off-by: Ciprian Pascu <contact@ciprianpascu.ro>
This commit is contained in:
Andrew Fiddian-Green 2024-11-01 23:07:20 +00:00 committed by Ciprian Pascu
parent 8bdedde8a0
commit fd85d1bad0
10 changed files with 260 additions and 30 deletions

View File

@ -241,4 +241,16 @@
<description>Controls the rollershutter and states its opening level in percent</description>
</channel-type>
<channel-type id="color-temperature-abs" advanced="true">
<item-type>Number:Temperature</item-type>
<label>Color Temperature</label>
<description>Controls the color temperature of the light in Kelvin</description>
<category>ColorLight</category>
<tags>
<tag>Control</tag>
<tag>ColorTemperature</tag>
</tags>
<state min="2700" max="6500" pattern="%.0f K"/>
</channel-type>
</thing:thing-descriptions>

View File

@ -16,11 +16,11 @@
<channels>
<channel id="color" typeId="system.color"/>
<channel id="color_temperature" typeId="system.color-temperature"/>
<channel id="color_temperature_abs" typeId="system.color-temperature-abs"/>
<channel id="color_temperature_abs" typeId="color-temperature-abs"/>
</channels>
<properties>
<property name="thingTypeVersion">1</property>
<property name="thingTypeVersion">2</property>
</properties>
<representation-property>ain</representation-property>
@ -418,11 +418,11 @@
<channels>
<channel id="color" typeId="system.color"/>
<channel id="color_temperature" typeId="system.color-temperature"/>
<channel id="color_temperature_abs" typeId="system.color-temperature-abs"/>
<channel id="color_temperature_abs" typeId="color-temperature-abs"/>
</channels>
<properties>
<property name="thingTypeVersion">1</property>
<property name="thingTypeVersion">2</property>
</properties>
<representation-property>ain</representation-property>

View File

@ -166,6 +166,11 @@
<type>system:color-temperature-abs</type>
</add-channel>
</instruction-set>
<instruction-set targetVersion="2">
<update-channel id="color_temperature_abs">
<type>avmfritz:color-temperature-abs</type>
</update-channel>
</instruction-set>
</thing-type>
<thing-type uid="avmfritz:HAN_FUN_COLOR_BULB">
@ -177,6 +182,11 @@
<type>system:color-temperature-abs</type>
</add-channel>
</instruction-set>
<instruction-set targetVersion="2">
<update-channel id="color_temperature_abs">
<type>avmfritz:color-temperature-abs</type>
</update-channel>
</instruction-set>
</thing-type>
</update:update-descriptions>

View File

@ -16,11 +16,12 @@
<config-description-ref uri="thing-type:govee:govee-light"/>
</thing-type>
<channel-type id="color-temperature-abs">
<channel-type id="color-temperature-abs" advanced="true">
<item-type>Number:Temperature</item-type>
<label>Absolute Color Temperature </label>
<label>Color Temperature</label>
<description>Controls the color temperature of the light in Kelvin</description>
<category>Temperature</category>
<category>ColorLight</category>
<tags>
<tag>Control</tag>
<tag>ColorTemperature</tag>
@ -28,5 +29,4 @@
<state min="2000" max="9000" pattern="%.0f K"/>
</channel-type>
</thing:thing-descriptions>

View File

@ -17,12 +17,14 @@ import static org.openhab.binding.lifx.internal.LifxBindingConstants.SUPPORTED_T
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.lifx.internal.handler.LifxLightHandler;
import org.openhab.binding.lifx.internal.handler.LifxStateDescriptionProvider;
import org.openhab.core.thing.Thing;
import org.openhab.core.thing.ThingTypeUID;
import org.openhab.core.thing.binding.BaseThingHandlerFactory;
import org.openhab.core.thing.binding.ThingHandler;
import org.openhab.core.thing.binding.ThingHandlerFactory;
import org.osgi.service.component.ComponentContext;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
@ -37,6 +39,12 @@ import org.osgi.service.component.annotations.Reference;
public class LifxHandlerFactory extends BaseThingHandlerFactory {
private @NonNullByDefault({}) LifxChannelFactory channelFactory;
private final LifxStateDescriptionProvider stateDescriptionProvider;
@Activate
public LifxHandlerFactory(@Reference LifxStateDescriptionProvider stateDescriptionProvider) {
this.stateDescriptionProvider = stateDescriptionProvider;
}
@Override
public boolean supportsThingType(ThingTypeUID thingTypeUID) {
@ -51,7 +59,7 @@ public class LifxHandlerFactory extends BaseThingHandlerFactory {
@Override
protected @Nullable ThingHandler createHandler(Thing thing) {
if (supportsThingType(thing.getThingTypeUID())) {
return new LifxLightHandler(thing, channelFactory);
return new LifxLightHandler(thing, channelFactory, stateDescriptionProvider);
}
return null;

View File

@ -42,6 +42,7 @@ import org.openhab.binding.lifx.internal.LifxLightState;
import org.openhab.binding.lifx.internal.LifxLightStateChanger;
import org.openhab.binding.lifx.internal.LifxProduct;
import org.openhab.binding.lifx.internal.LifxProduct.Features;
import org.openhab.binding.lifx.internal.LifxProduct.TemperatureRange;
import org.openhab.binding.lifx.internal.dto.Effect;
import org.openhab.binding.lifx.internal.dto.GetHevCycleRequest;
import org.openhab.binding.lifx.internal.dto.GetLightInfraredRequest;
@ -74,6 +75,7 @@ import org.openhab.core.thing.binding.BaseThingHandler;
import org.openhab.core.types.Command;
import org.openhab.core.types.RefreshType;
import org.openhab.core.types.State;
import org.openhab.core.types.UnDefType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -97,6 +99,8 @@ public class LifxLightHandler extends BaseThingHandler {
private static final Duration MAX_STATE_CHANGE_DURATION = Duration.ofSeconds(4);
private final LifxChannelFactory channelFactory;
private final LifxStateDescriptionProvider stateDescriptionProvider;
private @NonNullByDefault({}) Features features;
private Duration hevCycleDuration = Duration.ZERO;
@ -180,11 +184,23 @@ public class LifxLightHandler extends BaseThingHandler {
HSBK updateColor = nullSafeUpdateColor(powerState, color);
HSBType hsb = updateColor.getHSB();
State colorTemperatureState = UnDefType.UNDEF;
State colorTemperatureAbsoluteState = UnDefType.UNDEF;
TemperatureRange temperatureRange = features.getTemperatureRange();
if (temperatureRange.getRange() > 0) {
stateDescriptionProvider.setMinMaxKelvin(new ChannelUID(thing.getUID(), CHANNEL_ABS_TEMPERATURE),
temperatureRange.getMinimum(), temperatureRange.getMaximum());
colorTemperatureState = kelvinToPercentType(updateColor.getKelvin(), temperatureRange);
colorTemperatureAbsoluteState = QuantityType.valueOf(updateColor.getKelvin(), Units.KELVIN);
} else {
logger.warn("Thing {} invalid color temperature range {} .. {}", thing.getUID(),
temperatureRange.getMinimum(), temperatureRange.getMaximum());
}
updateStateIfChanged(CHANNEL_COLOR, hsb);
updateStateIfChanged(CHANNEL_BRIGHTNESS, hsb.getBrightness());
updateStateIfChanged(CHANNEL_TEMPERATURE,
kelvinToPercentType(updateColor.getKelvin(), features.getTemperatureRange()));
updateStateIfChanged(CHANNEL_ABS_TEMPERATURE, new QuantityType(updateColor.getKelvin(), Units.KELVIN));
updateStateIfChanged(CHANNEL_TEMPERATURE, colorTemperatureState);
updateStateIfChanged(CHANNEL_ABS_TEMPERATURE, colorTemperatureAbsoluteState);
updateZoneChannels(powerState, colors);
}
@ -249,9 +265,11 @@ public class LifxLightHandler extends BaseThingHandler {
}
}
public LifxLightHandler(Thing thing, LifxChannelFactory channelFactory) {
public LifxLightHandler(Thing thing, LifxChannelFactory channelFactory,
LifxStateDescriptionProvider stateDescriptionProvider) {
super(thing);
this.channelFactory = channelFactory;
this.stateDescriptionProvider = stateDescriptionProvider;
}
@Override

View File

@ -0,0 +1,84 @@
/**
* Copyright (c) 2010-2024 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.lifx.internal.handler;
import java.math.BigDecimal;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.events.EventPublisher;
import org.openhab.core.thing.Channel;
import org.openhab.core.thing.ChannelUID;
import org.openhab.core.thing.binding.BaseDynamicStateDescriptionProvider;
import org.openhab.core.thing.events.ThingEventFactory;
import org.openhab.core.thing.i18n.ChannelTypeI18nLocalizationService;
import org.openhab.core.thing.link.ItemChannelLinkRegistry;
import org.openhab.core.thing.type.DynamicStateDescriptionProvider;
import org.openhab.core.types.StateDescription;
import org.openhab.core.types.StateDescriptionFragment;
import org.openhab.core.types.StateDescriptionFragmentBuilder;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
/**
* The {@link LifxStateDescriptionProvider} provides dynamic state description minimum and maximum vales of color
* temperature channels whose capabilities are dynamically determined at runtime.
*
* @author Andrew Fiddian-Green - Initial contribution
*
*/
@NonNullByDefault
@Component(service = { DynamicStateDescriptionProvider.class, LifxStateDescriptionProvider.class })
public class LifxStateDescriptionProvider extends BaseDynamicStateDescriptionProvider {
private final Map<ChannelUID, StateDescriptionFragment> stateDescriptionFragments = new ConcurrentHashMap<>();
@Activate
public LifxStateDescriptionProvider(final @Reference EventPublisher eventPublisher,
final @Reference ItemChannelLinkRegistry itemChannelLinkRegistry,
final @Reference ChannelTypeI18nLocalizationService channelTypeI18nLocalizationService) {
this.eventPublisher = eventPublisher;
this.itemChannelLinkRegistry = itemChannelLinkRegistry;
this.channelTypeI18nLocalizationService = channelTypeI18nLocalizationService;
}
@Override
public @Nullable StateDescription getStateDescription(Channel channel, @Nullable StateDescription original,
@Nullable Locale locale) {
StateDescriptionFragment stateDescriptionFragment = stateDescriptionFragments.get(channel.getUID());
return stateDescriptionFragment != null ? stateDescriptionFragment.toStateDescription()
: super.getStateDescription(channel, original, locale);
}
/**
* Set the state description minimum and maximum values and pattern in Kelvin for the given channel UID
*/
public void setMinMaxKelvin(ChannelUID channelUID, long minKelvin, long maxKelvin) {
StateDescriptionFragment oldStateDescriptionFragment = stateDescriptionFragments.get(channelUID);
StateDescriptionFragment newStateDescriptionFragment = StateDescriptionFragmentBuilder.create()
.withMinimum(BigDecimal.valueOf(minKelvin)).withMaximum(BigDecimal.valueOf(maxKelvin))
.withStep(BigDecimal.valueOf(100)).withPattern("%.0f K").build();
if (!newStateDescriptionFragment.equals(oldStateDescriptionFragment)) {
stateDescriptionFragments.put(channelUID, newStateDescriptionFragment);
ItemChannelLinkRegistry itemChannelLinkRegistry = this.itemChannelLinkRegistry;
postEvent(ThingEventFactory.createChannelDescriptionChangedEvent(channelUID,
itemChannelLinkRegistry != null ? itemChannelLinkRegistry.getLinkedItemNames(channelUID) : Set.of(),
newStateDescriptionFragment, oldStateDescriptionFragment));
}
}
}

View File

@ -19,6 +19,7 @@ import java.util.stream.Stream;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.nanoleaf.internal.handler.NanoLeafStateDescriptionProvider;
import org.openhab.binding.nanoleaf.internal.handler.NanoleafControllerHandler;
import org.openhab.binding.nanoleaf.internal.handler.NanoleafPanelHandler;
import org.openhab.core.io.net.http.HttpClientFactory;
@ -51,10 +52,13 @@ public class NanoleafHandlerFactory extends BaseThingHandlerFactory {
private final Logger logger = LoggerFactory.getLogger(NanoleafHandlerFactory.class);
private final HttpClientFactory httpClientFactory;
private final NanoLeafStateDescriptionProvider nanoLeafStateDescriptionProvider;
@Activate
public NanoleafHandlerFactory(@Reference HttpClientFactory httpClientFactory) {
public NanoleafHandlerFactory(@Reference HttpClientFactory httpClientFactory,
@Reference NanoLeafStateDescriptionProvider nanoLeafStateDescriptionProvider) {
this.httpClientFactory = httpClientFactory;
this.nanoLeafStateDescriptionProvider = nanoLeafStateDescriptionProvider;
}
@Override
@ -67,7 +71,8 @@ public class NanoleafHandlerFactory extends BaseThingHandlerFactory {
protected ThingHandler createHandler(Thing thing) {
ThingTypeUID thingTypeUID = thing.getThingTypeUID();
if (NanoleafBindingConstants.THING_TYPE_CONTROLLER.equals(thingTypeUID)) {
NanoleafControllerHandler handler = new NanoleafControllerHandler((Bridge) thing, this.httpClientFactory);
NanoleafControllerHandler handler = new NanoleafControllerHandler((Bridge) thing, this.httpClientFactory,
this.nanoLeafStateDescriptionProvider);
logger.debug("Nanoleaf controller handler created.");
return handler;
} else if (NanoleafBindingConstants.THING_TYPE_LIGHT_PANEL.equals(thingTypeUID)) {

View File

@ -0,0 +1,84 @@
/**
* Copyright (c) 2010-2024 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.nanoleaf.internal.handler;
import java.math.BigDecimal;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.events.EventPublisher;
import org.openhab.core.thing.Channel;
import org.openhab.core.thing.ChannelUID;
import org.openhab.core.thing.binding.BaseDynamicStateDescriptionProvider;
import org.openhab.core.thing.events.ThingEventFactory;
import org.openhab.core.thing.i18n.ChannelTypeI18nLocalizationService;
import org.openhab.core.thing.link.ItemChannelLinkRegistry;
import org.openhab.core.thing.type.DynamicStateDescriptionProvider;
import org.openhab.core.types.StateDescription;
import org.openhab.core.types.StateDescriptionFragment;
import org.openhab.core.types.StateDescriptionFragmentBuilder;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
/**
* The {@link NanoLeafStateDescriptionProvider} provides dynamic state description minimum and maximum vales of color
* temperature channels whose capabilities are dynamically determined at runtime.
*
* @author Andrew Fiddian-Green - Initial contribution
*
*/
@NonNullByDefault
@Component(service = { DynamicStateDescriptionProvider.class, NanoLeafStateDescriptionProvider.class })
public class NanoLeafStateDescriptionProvider extends BaseDynamicStateDescriptionProvider {
private final Map<ChannelUID, StateDescriptionFragment> stateDescriptionFragments = new ConcurrentHashMap<>();
@Activate
public NanoLeafStateDescriptionProvider(final @Reference EventPublisher eventPublisher,
final @Reference ItemChannelLinkRegistry itemChannelLinkRegistry,
final @Reference ChannelTypeI18nLocalizationService channelTypeI18nLocalizationService) {
this.eventPublisher = eventPublisher;
this.itemChannelLinkRegistry = itemChannelLinkRegistry;
this.channelTypeI18nLocalizationService = channelTypeI18nLocalizationService;
}
@Override
public @Nullable StateDescription getStateDescription(Channel channel, @Nullable StateDescription original,
@Nullable Locale locale) {
StateDescriptionFragment stateDescriptionFragment = stateDescriptionFragments.get(channel.getUID());
return stateDescriptionFragment != null ? stateDescriptionFragment.toStateDescription()
: super.getStateDescription(channel, original, locale);
}
/**
* Set the state description minimum and maximum values and pattern in Kelvin for the given channel UID
*/
public void setMinMaxKelvin(ChannelUID channelUID, long minKelvin, long maxKelvin) {
StateDescriptionFragment oldStateDescriptionFragment = stateDescriptionFragments.get(channelUID);
StateDescriptionFragment newStateDescriptionFragment = StateDescriptionFragmentBuilder.create()
.withMinimum(BigDecimal.valueOf(minKelvin)).withMaximum(BigDecimal.valueOf(maxKelvin))
.withStep(BigDecimal.valueOf(100)).withPattern("%.0f K").build();
if (!newStateDescriptionFragment.equals(oldStateDescriptionFragment)) {
stateDescriptionFragments.put(channelUID, newStateDescriptionFragment);
ItemChannelLinkRegistry itemChannelLinkRegistry = this.itemChannelLinkRegistry;
postEvent(ThingEventFactory.createChannelDescriptionChangedEvent(channelUID,
itemChannelLinkRegistry != null ? itemChannelLinkRegistry.getLinkedItemNames(channelUID) : Set.of(),
newStateDescriptionFragment, oldStateDescriptionFragment));
}
}
}

View File

@ -94,6 +94,7 @@ import org.openhab.core.thing.binding.ThingHandlerService;
import org.openhab.core.thing.util.ThingWebClientUtil;
import org.openhab.core.types.Command;
import org.openhab.core.types.RefreshType;
import org.openhab.core.types.UnDefType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -118,6 +119,7 @@ public class NanoleafControllerHandler extends BaseBridgeHandler implements Nano
private final Logger logger = LoggerFactory.getLogger(NanoleafControllerHandler.class);
private final HttpClientFactory httpClientFactory;
private final HttpClient httpClient;
private final NanoLeafStateDescriptionProvider stateDescriptionProvider;
private @Nullable HttpClient httpClientSSETouchEvent;
private @Nullable Request sseTouchjobRequest;
@ -141,10 +143,12 @@ public class NanoleafControllerHandler extends BaseBridgeHandler implements Nano
private boolean touchJobRunning = false;
public NanoleafControllerHandler(Bridge bridge, HttpClientFactory httpClientFactory) {
public NanoleafControllerHandler(Bridge bridge, HttpClientFactory httpClientFactory,
NanoLeafStateDescriptionProvider nanoLeafStateDescriptionProvider) {
super(bridge);
this.httpClientFactory = httpClientFactory;
this.httpClient = httpClientFactory.getCommonHttpClient();
this.stateDescriptionProvider = nanoLeafStateDescriptionProvider;
}
private void initializeTouchHttpClient() {
@ -648,28 +652,33 @@ public class NanoleafControllerHandler extends BaseBridgeHandler implements Nano
OnOffType powerState = state.getOnOff();
org.openhab.core.types.State colorTemperatureState = UnDefType.UNDEF;
org.openhab.core.types.State colorTemperatureAbsoluteState = UnDefType.UNDEF;
Ct colorTemperature = state.getColorTemperature();
float colorTempPercent = 0.0F;
int hue;
int saturation;
if (colorTemperature != null) {
updateState(CHANNEL_COLOR_TEMPERATURE_ABS, new QuantityType(colorTemperature.getValue(), Units.KELVIN));
Integer min = colorTemperature.getMin();
hue = min == null ? 0 : min;
Integer max = colorTemperature.getMax();
saturation = max == null ? 0 : max;
colorTempPercent = (colorTemperature.getValue() - hue) / (saturation - hue)
* PercentType.HUNDRED.intValue();
int minKelvin = min == null ? 1000 : min;
int maxKelvin = max == null ? 10000 : max;
if (maxKelvin > minKelvin) {
stateDescriptionProvider.setMinMaxKelvin(new ChannelUID(thing.getUID(), CHANNEL_COLOR_TEMPERATURE_ABS),
minKelvin, maxKelvin);
colorTemperatureState = new PercentType(
Float.toString(100.0f * (colorTemperature.getValue() - minKelvin) / (maxKelvin - minKelvin)));
colorTemperatureAbsoluteState = QuantityType.valueOf(colorTemperature.getValue(), Units.KELVIN);
} else {
logger.warn("Thing {} invalid color temperature range {} .. {}", thing.getUID(), minKelvin, maxKelvin);
}
}
updateState(CHANNEL_COLOR_TEMPERATURE, colorTemperatureState);
updateState(CHANNEL_COLOR_TEMPERATURE_ABS, colorTemperatureAbsoluteState);
updateState(CHANNEL_COLOR_TEMPERATURE, new PercentType(Float.toString(colorTempPercent)));
updateState(CHANNEL_EFFECT, new StringType(controllerInfo.getEffects().getSelect()));
Hue stateHue = state.getHue();
hue = stateHue != null ? stateHue.getValue() : 0;
int hue = stateHue != null ? stateHue.getValue() : 0;
Sat stateSaturation = state.getSaturation();
saturation = stateSaturation != null ? stateSaturation.getValue() : 0;
int saturation = stateSaturation != null ? stateSaturation.getValue() : 0;
Brightness stateBrightness = state.getBrightness();
int brightness = stateBrightness != null ? stateBrightness.getValue() : 0;
@ -914,8 +923,8 @@ public class NanoleafControllerHandler extends BaseBridgeHandler implements Nano
IntegerState state = new Ct();
if (command instanceof DecimalType) {
state.setValue(((DecimalType) command).intValue());
} else if (command instanceof QuantityType) {
QuantityType<?> tempKelvin = ((QuantityType) command).toInvertibleUnit(Units.KELVIN);
} else if (command instanceof QuantityType<?> quantityType) {
QuantityType<?> tempKelvin = quantityType.toInvertibleUnit(Units.KELVIN);
if (tempKelvin == null) {
logger.warn("Cannot convert color temperature {} to Kelvin.", command);
return;