Skip to content

Commit

Permalink
[various] Lamp handlers expose min/max Color Temperature in state des…
Browse files Browse the repository at this point in the history
…cription (#17641)

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

Signed-off-by: AndrewFG <[email protected]>
  • Loading branch information
andrewfg authored Nov 1, 2024
1 parent f4250b7 commit 2080c85
Show file tree
Hide file tree
Showing 10 changed files with 260 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand All @@ -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>
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@
<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>
</tags>
<state min="2000" max="9000" pattern="%.0f K"/>
</channel-type>


</thing:thing-descriptions>
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
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;

Expand All @@ -37,6 +39,12 @@
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) {
Expand All @@ -51,7 +59,7 @@ protected void activate(ComponentContext componentContext) {
@Override
protected @Nullable ThingHandler createHandler(Thing thing) {
if (supportsThingType(thing.getThingTypeUID())) {
return new LifxLightHandler(thing, channelFactory);
return new LifxLightHandler(thing, channelFactory, stateDescriptionProvider);
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
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;
Expand Down Expand Up @@ -74,6 +75,7 @@
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;

Expand All @@ -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;
Expand Down Expand Up @@ -180,11 +184,23 @@ private void updateColorChannels(@Nullable PowerState powerState, HSBK[] colors)
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);
}
Expand Down Expand Up @@ -249,9 +265,11 @@ private void updateZoneChannels(@Nullable PowerState powerState, HSBK[] colors)
}
}

public LifxLightHandler(Thing thing, LifxChannelFactory channelFactory) {
public LifxLightHandler(Thing thing, LifxChannelFactory channelFactory,
LifxStateDescriptionProvider stateDescriptionProvider) {
super(thing);
this.channelFactory = channelFactory;
this.stateDescriptionProvider = stateDescriptionProvider;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -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));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

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;
Expand Down Expand Up @@ -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
Expand All @@ -67,7 +71,8 @@ public boolean supportsThingType(ThingTypeUID thingTypeUID) {
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)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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));
}
}
}
Loading

0 comments on commit 2080c85

Please sign in to comment.