mirror of
https://github.com/openhab/openhab-addons.git
synced 2025-01-25 14:55:55 +01:00
[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:
parent
8bdedde8a0
commit
fd85d1bad0
@ -241,4 +241,16 @@
|
|||||||
<description>Controls the rollershutter and states its opening level in percent</description>
|
<description>Controls the rollershutter and states its opening level in percent</description>
|
||||||
</channel-type>
|
</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>
|
</thing:thing-descriptions>
|
||||||
|
@ -16,11 +16,11 @@
|
|||||||
<channels>
|
<channels>
|
||||||
<channel id="color" typeId="system.color"/>
|
<channel id="color" typeId="system.color"/>
|
||||||
<channel id="color_temperature" typeId="system.color-temperature"/>
|
<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>
|
</channels>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<property name="thingTypeVersion">1</property>
|
<property name="thingTypeVersion">2</property>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<representation-property>ain</representation-property>
|
<representation-property>ain</representation-property>
|
||||||
@ -418,11 +418,11 @@
|
|||||||
<channels>
|
<channels>
|
||||||
<channel id="color" typeId="system.color"/>
|
<channel id="color" typeId="system.color"/>
|
||||||
<channel id="color_temperature" typeId="system.color-temperature"/>
|
<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>
|
</channels>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<property name="thingTypeVersion">1</property>
|
<property name="thingTypeVersion">2</property>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<representation-property>ain</representation-property>
|
<representation-property>ain</representation-property>
|
||||||
|
@ -166,6 +166,11 @@
|
|||||||
<type>system:color-temperature-abs</type>
|
<type>system:color-temperature-abs</type>
|
||||||
</add-channel>
|
</add-channel>
|
||||||
</instruction-set>
|
</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>
|
||||||
|
|
||||||
<thing-type uid="avmfritz:HAN_FUN_COLOR_BULB">
|
<thing-type uid="avmfritz:HAN_FUN_COLOR_BULB">
|
||||||
@ -177,6 +182,11 @@
|
|||||||
<type>system:color-temperature-abs</type>
|
<type>system:color-temperature-abs</type>
|
||||||
</add-channel>
|
</add-channel>
|
||||||
</instruction-set>
|
</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>
|
||||||
|
|
||||||
</update:update-descriptions>
|
</update:update-descriptions>
|
||||||
|
@ -16,11 +16,12 @@
|
|||||||
<config-description-ref uri="thing-type:govee:govee-light"/>
|
<config-description-ref uri="thing-type:govee:govee-light"/>
|
||||||
</thing-type>
|
</thing-type>
|
||||||
|
|
||||||
<channel-type id="color-temperature-abs">
|
|
||||||
|
<channel-type id="color-temperature-abs" advanced="true">
|
||||||
<item-type>Number:Temperature</item-type>
|
<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>
|
<description>Controls the color temperature of the light in Kelvin</description>
|
||||||
<category>Temperature</category>
|
<category>ColorLight</category>
|
||||||
<tags>
|
<tags>
|
||||||
<tag>Control</tag>
|
<tag>Control</tag>
|
||||||
<tag>ColorTemperature</tag>
|
<tag>ColorTemperature</tag>
|
||||||
@ -28,5 +29,4 @@
|
|||||||
<state min="2000" max="9000" pattern="%.0f K"/>
|
<state min="2000" max="9000" pattern="%.0f K"/>
|
||||||
</channel-type>
|
</channel-type>
|
||||||
|
|
||||||
|
|
||||||
</thing:thing-descriptions>
|
</thing:thing-descriptions>
|
||||||
|
@ -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.NonNullByDefault;
|
||||||
import org.eclipse.jdt.annotation.Nullable;
|
import org.eclipse.jdt.annotation.Nullable;
|
||||||
import org.openhab.binding.lifx.internal.handler.LifxLightHandler;
|
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.Thing;
|
||||||
import org.openhab.core.thing.ThingTypeUID;
|
import org.openhab.core.thing.ThingTypeUID;
|
||||||
import org.openhab.core.thing.binding.BaseThingHandlerFactory;
|
import org.openhab.core.thing.binding.BaseThingHandlerFactory;
|
||||||
import org.openhab.core.thing.binding.ThingHandler;
|
import org.openhab.core.thing.binding.ThingHandler;
|
||||||
import org.openhab.core.thing.binding.ThingHandlerFactory;
|
import org.openhab.core.thing.binding.ThingHandlerFactory;
|
||||||
import org.osgi.service.component.ComponentContext;
|
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.Component;
|
||||||
import org.osgi.service.component.annotations.Reference;
|
import org.osgi.service.component.annotations.Reference;
|
||||||
|
|
||||||
@ -37,6 +39,12 @@ import org.osgi.service.component.annotations.Reference;
|
|||||||
public class LifxHandlerFactory extends BaseThingHandlerFactory {
|
public class LifxHandlerFactory extends BaseThingHandlerFactory {
|
||||||
|
|
||||||
private @NonNullByDefault({}) LifxChannelFactory channelFactory;
|
private @NonNullByDefault({}) LifxChannelFactory channelFactory;
|
||||||
|
private final LifxStateDescriptionProvider stateDescriptionProvider;
|
||||||
|
|
||||||
|
@Activate
|
||||||
|
public LifxHandlerFactory(@Reference LifxStateDescriptionProvider stateDescriptionProvider) {
|
||||||
|
this.stateDescriptionProvider = stateDescriptionProvider;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean supportsThingType(ThingTypeUID thingTypeUID) {
|
public boolean supportsThingType(ThingTypeUID thingTypeUID) {
|
||||||
@ -51,7 +59,7 @@ public class LifxHandlerFactory extends BaseThingHandlerFactory {
|
|||||||
@Override
|
@Override
|
||||||
protected @Nullable ThingHandler createHandler(Thing thing) {
|
protected @Nullable ThingHandler createHandler(Thing thing) {
|
||||||
if (supportsThingType(thing.getThingTypeUID())) {
|
if (supportsThingType(thing.getThingTypeUID())) {
|
||||||
return new LifxLightHandler(thing, channelFactory);
|
return new LifxLightHandler(thing, channelFactory, stateDescriptionProvider);
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
|
@ -42,6 +42,7 @@ import org.openhab.binding.lifx.internal.LifxLightState;
|
|||||||
import org.openhab.binding.lifx.internal.LifxLightStateChanger;
|
import org.openhab.binding.lifx.internal.LifxLightStateChanger;
|
||||||
import org.openhab.binding.lifx.internal.LifxProduct;
|
import org.openhab.binding.lifx.internal.LifxProduct;
|
||||||
import org.openhab.binding.lifx.internal.LifxProduct.Features;
|
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.Effect;
|
||||||
import org.openhab.binding.lifx.internal.dto.GetHevCycleRequest;
|
import org.openhab.binding.lifx.internal.dto.GetHevCycleRequest;
|
||||||
import org.openhab.binding.lifx.internal.dto.GetLightInfraredRequest;
|
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.Command;
|
||||||
import org.openhab.core.types.RefreshType;
|
import org.openhab.core.types.RefreshType;
|
||||||
import org.openhab.core.types.State;
|
import org.openhab.core.types.State;
|
||||||
|
import org.openhab.core.types.UnDefType;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
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 static final Duration MAX_STATE_CHANGE_DURATION = Duration.ofSeconds(4);
|
||||||
|
|
||||||
private final LifxChannelFactory channelFactory;
|
private final LifxChannelFactory channelFactory;
|
||||||
|
private final LifxStateDescriptionProvider stateDescriptionProvider;
|
||||||
|
|
||||||
private @NonNullByDefault({}) Features features;
|
private @NonNullByDefault({}) Features features;
|
||||||
|
|
||||||
private Duration hevCycleDuration = Duration.ZERO;
|
private Duration hevCycleDuration = Duration.ZERO;
|
||||||
@ -180,11 +184,23 @@ public class LifxLightHandler extends BaseThingHandler {
|
|||||||
HSBK updateColor = nullSafeUpdateColor(powerState, color);
|
HSBK updateColor = nullSafeUpdateColor(powerState, color);
|
||||||
HSBType hsb = updateColor.getHSB();
|
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_COLOR, hsb);
|
||||||
updateStateIfChanged(CHANNEL_BRIGHTNESS, hsb.getBrightness());
|
updateStateIfChanged(CHANNEL_BRIGHTNESS, hsb.getBrightness());
|
||||||
updateStateIfChanged(CHANNEL_TEMPERATURE,
|
updateStateIfChanged(CHANNEL_TEMPERATURE, colorTemperatureState);
|
||||||
kelvinToPercentType(updateColor.getKelvin(), features.getTemperatureRange()));
|
updateStateIfChanged(CHANNEL_ABS_TEMPERATURE, colorTemperatureAbsoluteState);
|
||||||
updateStateIfChanged(CHANNEL_ABS_TEMPERATURE, new QuantityType(updateColor.getKelvin(), Units.KELVIN));
|
|
||||||
|
|
||||||
updateZoneChannels(powerState, colors);
|
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);
|
super(thing);
|
||||||
this.channelFactory = channelFactory;
|
this.channelFactory = channelFactory;
|
||||||
|
this.stateDescriptionProvider = stateDescriptionProvider;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -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));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -19,6 +19,7 @@ import java.util.stream.Stream;
|
|||||||
|
|
||||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||||
import org.eclipse.jdt.annotation.Nullable;
|
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.NanoleafControllerHandler;
|
||||||
import org.openhab.binding.nanoleaf.internal.handler.NanoleafPanelHandler;
|
import org.openhab.binding.nanoleaf.internal.handler.NanoleafPanelHandler;
|
||||||
import org.openhab.core.io.net.http.HttpClientFactory;
|
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 Logger logger = LoggerFactory.getLogger(NanoleafHandlerFactory.class);
|
||||||
private final HttpClientFactory httpClientFactory;
|
private final HttpClientFactory httpClientFactory;
|
||||||
|
private final NanoLeafStateDescriptionProvider nanoLeafStateDescriptionProvider;
|
||||||
|
|
||||||
@Activate
|
@Activate
|
||||||
public NanoleafHandlerFactory(@Reference HttpClientFactory httpClientFactory) {
|
public NanoleafHandlerFactory(@Reference HttpClientFactory httpClientFactory,
|
||||||
|
@Reference NanoLeafStateDescriptionProvider nanoLeafStateDescriptionProvider) {
|
||||||
this.httpClientFactory = httpClientFactory;
|
this.httpClientFactory = httpClientFactory;
|
||||||
|
this.nanoLeafStateDescriptionProvider = nanoLeafStateDescriptionProvider;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -67,7 +71,8 @@ public class NanoleafHandlerFactory extends BaseThingHandlerFactory {
|
|||||||
protected ThingHandler createHandler(Thing thing) {
|
protected ThingHandler createHandler(Thing thing) {
|
||||||
ThingTypeUID thingTypeUID = thing.getThingTypeUID();
|
ThingTypeUID thingTypeUID = thing.getThingTypeUID();
|
||||||
if (NanoleafBindingConstants.THING_TYPE_CONTROLLER.equals(thingTypeUID)) {
|
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.");
|
logger.debug("Nanoleaf controller handler created.");
|
||||||
return handler;
|
return handler;
|
||||||
} else if (NanoleafBindingConstants.THING_TYPE_LIGHT_PANEL.equals(thingTypeUID)) {
|
} else if (NanoleafBindingConstants.THING_TYPE_LIGHT_PANEL.equals(thingTypeUID)) {
|
||||||
|
@ -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));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -94,6 +94,7 @@ import org.openhab.core.thing.binding.ThingHandlerService;
|
|||||||
import org.openhab.core.thing.util.ThingWebClientUtil;
|
import org.openhab.core.thing.util.ThingWebClientUtil;
|
||||||
import org.openhab.core.types.Command;
|
import org.openhab.core.types.Command;
|
||||||
import org.openhab.core.types.RefreshType;
|
import org.openhab.core.types.RefreshType;
|
||||||
|
import org.openhab.core.types.UnDefType;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
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 Logger logger = LoggerFactory.getLogger(NanoleafControllerHandler.class);
|
||||||
private final HttpClientFactory httpClientFactory;
|
private final HttpClientFactory httpClientFactory;
|
||||||
private final HttpClient httpClient;
|
private final HttpClient httpClient;
|
||||||
|
private final NanoLeafStateDescriptionProvider stateDescriptionProvider;
|
||||||
|
|
||||||
private @Nullable HttpClient httpClientSSETouchEvent;
|
private @Nullable HttpClient httpClientSSETouchEvent;
|
||||||
private @Nullable Request sseTouchjobRequest;
|
private @Nullable Request sseTouchjobRequest;
|
||||||
@ -141,10 +143,12 @@ public class NanoleafControllerHandler extends BaseBridgeHandler implements Nano
|
|||||||
|
|
||||||
private boolean touchJobRunning = false;
|
private boolean touchJobRunning = false;
|
||||||
|
|
||||||
public NanoleafControllerHandler(Bridge bridge, HttpClientFactory httpClientFactory) {
|
public NanoleafControllerHandler(Bridge bridge, HttpClientFactory httpClientFactory,
|
||||||
|
NanoLeafStateDescriptionProvider nanoLeafStateDescriptionProvider) {
|
||||||
super(bridge);
|
super(bridge);
|
||||||
this.httpClientFactory = httpClientFactory;
|
this.httpClientFactory = httpClientFactory;
|
||||||
this.httpClient = httpClientFactory.getCommonHttpClient();
|
this.httpClient = httpClientFactory.getCommonHttpClient();
|
||||||
|
this.stateDescriptionProvider = nanoLeafStateDescriptionProvider;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initializeTouchHttpClient() {
|
private void initializeTouchHttpClient() {
|
||||||
@ -648,28 +652,33 @@ public class NanoleafControllerHandler extends BaseBridgeHandler implements Nano
|
|||||||
|
|
||||||
OnOffType powerState = state.getOnOff();
|
OnOffType powerState = state.getOnOff();
|
||||||
|
|
||||||
|
org.openhab.core.types.State colorTemperatureState = UnDefType.UNDEF;
|
||||||
|
org.openhab.core.types.State colorTemperatureAbsoluteState = UnDefType.UNDEF;
|
||||||
Ct colorTemperature = state.getColorTemperature();
|
Ct colorTemperature = state.getColorTemperature();
|
||||||
|
|
||||||
float colorTempPercent = 0.0F;
|
|
||||||
int hue;
|
|
||||||
int saturation;
|
|
||||||
if (colorTemperature != null) {
|
if (colorTemperature != null) {
|
||||||
updateState(CHANNEL_COLOR_TEMPERATURE_ABS, new QuantityType(colorTemperature.getValue(), Units.KELVIN));
|
|
||||||
Integer min = colorTemperature.getMin();
|
Integer min = colorTemperature.getMin();
|
||||||
hue = min == null ? 0 : min;
|
|
||||||
Integer max = colorTemperature.getMax();
|
Integer max = colorTemperature.getMax();
|
||||||
saturation = max == null ? 0 : max;
|
int minKelvin = min == null ? 1000 : min;
|
||||||
colorTempPercent = (colorTemperature.getValue() - hue) / (saturation - hue)
|
int maxKelvin = max == null ? 10000 : max;
|
||||||
* PercentType.HUNDRED.intValue();
|
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()));
|
updateState(CHANNEL_EFFECT, new StringType(controllerInfo.getEffects().getSelect()));
|
||||||
Hue stateHue = state.getHue();
|
Hue stateHue = state.getHue();
|
||||||
hue = stateHue != null ? stateHue.getValue() : 0;
|
int hue = stateHue != null ? stateHue.getValue() : 0;
|
||||||
|
|
||||||
Sat stateSaturation = state.getSaturation();
|
Sat stateSaturation = state.getSaturation();
|
||||||
saturation = stateSaturation != null ? stateSaturation.getValue() : 0;
|
int saturation = stateSaturation != null ? stateSaturation.getValue() : 0;
|
||||||
|
|
||||||
Brightness stateBrightness = state.getBrightness();
|
Brightness stateBrightness = state.getBrightness();
|
||||||
int brightness = stateBrightness != null ? stateBrightness.getValue() : 0;
|
int brightness = stateBrightness != null ? stateBrightness.getValue() : 0;
|
||||||
@ -914,8 +923,8 @@ public class NanoleafControllerHandler extends BaseBridgeHandler implements Nano
|
|||||||
IntegerState state = new Ct();
|
IntegerState state = new Ct();
|
||||||
if (command instanceof DecimalType) {
|
if (command instanceof DecimalType) {
|
||||||
state.setValue(((DecimalType) command).intValue());
|
state.setValue(((DecimalType) command).intValue());
|
||||||
} else if (command instanceof QuantityType) {
|
} else if (command instanceof QuantityType<?> quantityType) {
|
||||||
QuantityType<?> tempKelvin = ((QuantityType) command).toInvertibleUnit(Units.KELVIN);
|
QuantityType<?> tempKelvin = quantityType.toInvertibleUnit(Units.KELVIN);
|
||||||
if (tempKelvin == null) {
|
if (tempKelvin == null) {
|
||||||
logger.warn("Cannot convert color temperature {} to Kelvin.", command);
|
logger.warn("Cannot convert color temperature {} to Kelvin.", command);
|
||||||
return;
|
return;
|
||||||
|
Loading…
Reference in New Issue
Block a user