Skip to content

Commit

Permalink
[tplinksmarthome] Improve color temperature channel (#17780)
Browse files Browse the repository at this point in the history
* [tplinksmarthome] color temperature channel improvements

Signed-off-by: AndrewFG <software@whitebear.ch>
  • Loading branch information
andrewfg authored Nov 24, 2024
1 parent 8bc8961 commit ff10358
Show file tree
Hide file tree
Showing 20 changed files with 255 additions and 58 deletions.
2 changes: 1 addition & 1 deletion bundles/org.openhab.binding.tplinksmarthome/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ All devices support some of the following channels:
| switch | Switch | Power the device on or off. | EP10, EP25, EP40, HS100, HS103, HS105, HS107, HS110, HS200, HS210, HS300, KP100, KP105, KP115, KP200, KP303, KP400, KP401, KS230, RE270K, RE370K |
| brightness | Dimmer | Set the brightness of device or dimmer. | ES20M, HS220, KB100, KL50, KL60, KL110, KL120, KP405, LB100, LB110, LB120, LB200 |
| colorTemperature | Dimmer | Set the color temperature in percentage. | KB130, KL120, KL125, KL130, KL135, KL400, KL430, LB120, LB130, LB230 |
| colorTemperatureAbs | Number | Set the color temperature in Kelvin. | KB130, KL120, KL125, KL130, KL135, KL400, KL430, LB120, LB130, LB230 |
| colorTemperatureAbs | Number:Temperature | Set the color temperature in Kelvin. | KB130, KL120, KL125, KL130, KL135, KL400, KL430, LB120, LB130, LB230 |
| color | Color | Set the color of the light. | KB130, KL125, KL130, KL135, KL400, KL430, LB130, LB230 |
| power | Number:Power | Actual energy usage in Watt. | EP25, HS110, HS300, KLxxx, KP115, KP125, LBxxx, |
| eneryUsage | Number:Energy | Energy Usage in kWh. | EP25, HS110, HS300, KP115, KP125 |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
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.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

Expand All @@ -44,6 +45,12 @@
public class TPLinkSmartHomeHandlerFactory extends BaseThingHandlerFactory {

private @NonNullByDefault({}) TPLinkIpAddressService ipAddressService;
private final TPLinkStateDescriptionProvider stateDescriptionProvider;

@Activate
public TPLinkSmartHomeHandlerFactory(final @Reference TPLinkStateDescriptionProvider stateDescriptionProvider) {
this.stateDescriptionProvider = stateDescriptionProvider;
}

@Override
public boolean supportsThingType(final ThingTypeUID thingTypeUID) {
Expand Down Expand Up @@ -89,7 +96,7 @@ protected ThingHandler createHandler(final Thing thing) {
default:
return null;
}
return new SmartHomeHandler(thing, device, type, ipAddressService);
return new SmartHomeHandler(thing, device, type, ipAddressService, stateDescriptionProvider);
}

@Reference
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/**
* 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.tplinksmarthome.internal;

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 TPLinkStateDescriptionProvider} provides state descriptions for different color temperature light models.
*
* @author Andrew Fiddian-Green - Initial contribution
*
*/
@NonNullByDefault
@Component(service = { DynamicStateDescriptionProvider.class, TPLinkStateDescriptionProvider.class })
public class TPLinkStateDescriptionProvider extends BaseDynamicStateDescriptionProvider {

private final Map<ChannelUID, StateDescriptionFragment> stateDescriptionFragments = new ConcurrentHashMap<>();

@Activate
public TPLinkStateDescriptionProvider(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 @@ -12,13 +12,7 @@
*/
package org.openhab.binding.tplinksmarthome.internal.device;

import static org.openhab.binding.tplinksmarthome.internal.TPLinkSmartHomeBindingConstants.CHANNELS_BULB_SWITCH;
import static org.openhab.binding.tplinksmarthome.internal.TPLinkSmartHomeBindingConstants.CHANNEL_BRIGHTNESS;
import static org.openhab.binding.tplinksmarthome.internal.TPLinkSmartHomeBindingConstants.CHANNEL_COLOR;
import static org.openhab.binding.tplinksmarthome.internal.TPLinkSmartHomeBindingConstants.CHANNEL_COLOR_TEMPERATURE;
import static org.openhab.binding.tplinksmarthome.internal.TPLinkSmartHomeBindingConstants.CHANNEL_COLOR_TEMPERATURE_ABS;
import static org.openhab.binding.tplinksmarthome.internal.TPLinkSmartHomeBindingConstants.CHANNEL_ENERGY_POWER;
import static org.openhab.binding.tplinksmarthome.internal.TPLinkSmartHomeBindingConstants.CHANNEL_SWITCH;
import static org.openhab.binding.tplinksmarthome.internal.TPLinkSmartHomeBindingConstants.*;

import java.io.IOException;

Expand Down Expand Up @@ -163,4 +157,12 @@ private int convertKelvinToPercentage(final int colorTemperature) {
private int guardColorTemperature(final int colorTemperature) {
return Math.max(colorTempMin, Math.min(colorTempMax, colorTemperature));
}

public int getColorTempMin() {
return colorTempMin;
}

public int getColorTempMax() {
return colorTempMax;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@
*/
package org.openhab.binding.tplinksmarthome.internal.handler;

import static org.openhab.binding.tplinksmarthome.internal.TPLinkSmartHomeBindingConstants.CHANNEL_RSSI;
import static org.openhab.binding.tplinksmarthome.internal.TPLinkSmartHomeBindingConstants.CONFIG_DEVICE_ID;
import static org.openhab.binding.tplinksmarthome.internal.TPLinkSmartHomeBindingConstants.CONFIG_IP;
import static org.openhab.binding.tplinksmarthome.internal.TPLinkSmartHomeBindingConstants.FORCED_REFRESH_BOUNDERY_SECONDS;
import static org.openhab.binding.tplinksmarthome.internal.TPLinkSmartHomeBindingConstants.FORCED_REFRESH_BOUNDERY_SWITCHED_SECONDS;
import static org.openhab.binding.tplinksmarthome.internal.TPLinkSmartHomeBindingConstants.*;

import java.io.IOException;
import java.time.Duration;
Expand All @@ -33,6 +29,8 @@
import org.openhab.binding.tplinksmarthome.internal.TPLinkSmartHomeConfiguration;
import org.openhab.binding.tplinksmarthome.internal.TPLinkSmartHomeThingType;
import org.openhab.binding.tplinksmarthome.internal.TPLinkSmartHomeThingType.DeviceType;
import org.openhab.binding.tplinksmarthome.internal.TPLinkStateDescriptionProvider;
import org.openhab.binding.tplinksmarthome.internal.device.BulbDevice;
import org.openhab.binding.tplinksmarthome.internal.device.DeviceState;
import org.openhab.binding.tplinksmarthome.internal.device.SmartHomeDevice;
import org.openhab.core.cache.ExpiringCache;
Expand Down Expand Up @@ -69,6 +67,7 @@ public class SmartHomeHandler extends BaseThingHandler {
private final SmartHomeDevice smartHomeDevice;
private final TPLinkIpAddressService ipAddressService;
private final int forceRefreshThreshold;
private final TPLinkStateDescriptionProvider stateDescriptionProvider;

private @NonNullByDefault({}) TPLinkSmartHomeConfiguration configuration;
private @NonNullByDefault({}) Connection connection;
Expand All @@ -86,15 +85,18 @@ public class SmartHomeHandler extends BaseThingHandler {
* @param smartHomeDevice Specific Smart Home device handler
* @param type The device type
* @param ipAddressService Cache keeping track of ip addresses of tp link devices
* @param stateDescriptionProvider
*/
public SmartHomeHandler(final Thing thing, final SmartHomeDevice smartHomeDevice,
final TPLinkSmartHomeThingType type, final TPLinkIpAddressService ipAddressService) {
final TPLinkSmartHomeThingType type, final TPLinkIpAddressService ipAddressService,
final TPLinkStateDescriptionProvider stateDescriptionProvider) {
super(thing);
this.smartHomeDevice = smartHomeDevice;
this.ipAddressService = ipAddressService;
this.forceRefreshThreshold = type.getDeviceType() == DeviceType.SWITCH
|| type.getDeviceType() == DeviceType.DIMMER ? FORCED_REFRESH_BOUNDERY_SWITCHED_SECONDS
: FORCED_REFRESH_BOUNDERY_SECONDS;
this.stateDescriptionProvider = stateDescriptionProvider;
}

@Override
Expand Down Expand Up @@ -148,6 +150,10 @@ public void initialize() {
? new ExpiringCache<>(ONE_SECOND, this::forceCacheUpdate)
: cache;
updateStatus(ThingStatus.UNKNOWN);
if (smartHomeDevice instanceof BulbDevice bulb) {
stateDescriptionProvider.setMinMaxKelvin(new ChannelUID(thing.getUID(), CHANNEL_COLOR_TEMPERATURE_ABS),
bulb.getColorTempMin(), bulb.getColorTempMax());
}
// While config.xml defines refresh as min 1, this check is used to run a test that doesn't start refresh.
if (configuration.refresh > 0) {
startAutomaticRefresh(configuration);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,6 @@ channel-group-type.tplinksmarthome.switch-outlet.label = Outlet

# channel types

channel-type.tplinksmarthome.colorTemperatureAbs1.label = Color Temperature
channel-type.tplinksmarthome.colorTemperatureAbs1.description = This channel supports adjusting the color temperature from 2700K to 6500K.
channel-type.tplinksmarthome.colorTemperatureAbs2.label = Color Temperature
channel-type.tplinksmarthome.colorTemperatureAbs2.description = This channel supports adjusting the color temperature from 2500K to 9000K.
channel-type.tplinksmarthome.colorTemperatureAbs3.label = Color Temperature
channel-type.tplinksmarthome.colorTemperatureAbs3.description = This channel supports adjusting the color temperature from 2500K to 6500K.
channel-type.tplinksmarthome.current.label = Current
channel-type.tplinksmarthome.current.description = Actual current usage.
channel-type.tplinksmarthome.energy-usage.label = Energy Usage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@
<channels>
<channel id="color" typeId="system.color"/>
<channel id="colorTemperature" typeId="system.color-temperature"/>
<channel id="colorTemperatureAbs" typeId="colorTemperatureAbs2"/>
<channel id="colorTemperatureAbs" typeId="system.color-temperature-abs"/>
<channel id="rssi" typeId="rssi"/>
</channels>

<properties>
<property name="thingTypeVersion">1</property>
</properties>

<representation-property>deviceId</representation-property>

<config-description-ref uri="thing-type:tplinksmarthome:device-bulb"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@
<channels>
<channel id="brightness" typeId="system.brightness"/>
<channel id="colorTemperature" typeId="system.color-temperature"/>
<channel id="colorTemperatureAbs" typeId="colorTemperatureAbs1"/>
<channel id="colorTemperatureAbs" typeId="system.color-temperature-abs"/>
<channel id="power" typeId="power"/>
<channel id="rssi" typeId="rssi"/>
</channels>

<properties>
<property name="thingTypeVersion">1</property>
</properties>

<representation-property>deviceId</representation-property>

<config-description-ref uri="thing-type:tplinksmarthome:device-bulb"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@
<channels>
<channel id="color" typeId="system.color"/>
<channel id="colorTemperature" typeId="system.color-temperature"/>
<channel id="colorTemperatureAbs" typeId="colorTemperatureAbs3"/>
<channel id="colorTemperatureAbs" typeId="system.color-temperature-abs"/>
<channel id="power" typeId="power"/>
<channel id="rssi" typeId="rssi"/>
</channels>

<properties>
<property name="thingTypeVersion">1</property>
</properties>

<representation-property>deviceId</representation-property>

<config-description-ref uri="thing-type:tplinksmarthome:device-bulb"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@
<channels>
<channel id="color" typeId="system.color"/>
<channel id="colorTemperature" typeId="system.color-temperature"/>
<channel id="colorTemperatureAbs" typeId="colorTemperatureAbs2"/>
<channel id="colorTemperatureAbs" typeId="system.color-temperature-abs"/>
<channel id="power" typeId="power"/>
<channel id="rssi" typeId="rssi"/>
</channels>

<properties>
<property name="thingTypeVersion">1</property>
</properties>

<representation-property>deviceId</representation-property>

<config-description-ref uri="thing-type:tplinksmarthome:device-bulb"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@
<channels>
<channel id="color" typeId="system.color"/>
<channel id="colorTemperature" typeId="system.color-temperature"/>
<channel id="colorTemperatureAbs" typeId="colorTemperatureAbs3"/>
<channel id="colorTemperatureAbs" typeId="system.color-temperature-abs"/>
<channel id="power" typeId="power"/>
<channel id="rssi" typeId="rssi"/>
</channels>

<properties>
<property name="thingTypeVersion">1</property>
</properties>

<representation-property>deviceId</representation-property>

<config-description-ref uri="thing-type:tplinksmarthome:device-bulb"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@
<channels>
<channel id="color" typeId="system.color"/>
<channel id="colorTemperature" typeId="system.color-temperature"/>
<channel id="colorTemperatureAbs" typeId="colorTemperatureAbs2"/>
<channel id="colorTemperatureAbs" typeId="system.color-temperature-abs"/>
<channel id="power" typeId="power"/>
<channel id="rssi" typeId="rssi"/>
</channels>

<properties>
<property name="thingTypeVersion">1</property>
</properties>

<representation-property>deviceId</representation-property>

<config-description-ref uri="thing-type:tplinksmarthome:device-bulb"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@
<channels>
<channel id="color" typeId="system.color"/>
<channel id="colorTemperature" typeId="system.color-temperature"/>
<channel id="colorTemperatureAbs" typeId="colorTemperatureAbs2"/>
<channel id="colorTemperatureAbs" typeId="system.color-temperature-abs"/>
<channel id="power" typeId="power"/>
<channel id="rssi" typeId="rssi"/>
</channels>

<properties>
<property name="thingTypeVersion">1</property>
</properties>

<representation-property>deviceId</representation-property>

<config-description-ref uri="thing-type:tplinksmarthome:device-bulb"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@
<channels>
<channel id="brightness" typeId="system.brightness"/>
<channel id="colorTemperature" typeId="system.color-temperature"/>
<channel id="colorTemperatureAbs" typeId="colorTemperatureAbs1"/>
<channel id="colorTemperatureAbs" typeId="system.color-temperature-abs"/>
<channel id="power" typeId="power"/>
<channel id="rssi" typeId="rssi"/>
</channels>

<properties>
<property name="thingTypeVersion">1</property>
</properties>

<representation-property>deviceId</representation-property>

<config-description-ref uri="thing-type:tplinksmarthome:device-bulb"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@
<channels>
<channel id="color" typeId="system.color"/>
<channel id="colorTemperature" typeId="system.color-temperature"/>
<channel id="colorTemperatureAbs" typeId="colorTemperatureAbs2"/>
<channel id="colorTemperatureAbs" typeId="system.color-temperature-abs"/>
<channel id="power" typeId="power"/>
<channel id="rssi" typeId="rssi"/>
</channels>

<properties>
<property name="thingTypeVersion">1</property>
</properties>

<representation-property>deviceId</representation-property>

<config-description-ref uri="thing-type:tplinksmarthome:device-bulb"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@
<channels>
<channel id="color" typeId="system.color"/>
<channel id="colorTemperature" typeId="system.color-temperature"/>
<channel id="colorTemperatureAbs" typeId="colorTemperatureAbs2"/>
<channel id="colorTemperatureAbs" typeId="system.color-temperature-abs"/>
<channel id="power" typeId="power"/>
<channel id="rssi" typeId="rssi"/>
</channels>

<properties>
<property name="thingTypeVersion">1</property>
</properties>

<representation-property>deviceId</representation-property>

<config-description-ref uri="thing-type:tplinksmarthome:device-bulb"/>
Expand Down
Loading

0 comments on commit ff10358

Please sign in to comment.