Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[avmfritz] Add light blub color temperature support #14373

Merged
merged 12 commits into from
May 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 35 additions & 33 deletions bundles/org.openhab.binding.avmfritz/README.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ public class AVMFritzBindingConstants {
public static final String CHANNEL_LAST_CHANGE = "last_change";
public static final String CHANNEL_ROLLERSHUTTER = "rollershutter";
public static final String CHANNEL_COLOR = "color";
public static final String CHANNEL_COLORTEMPERATURE = "color_temperature";
public static final String CHANNEL_COLORTEMPERATURE_ABS = "color_temperature_abs";
public static final String CHANNEL_BRIGHTNESS = "brightness";
public static final String CHANNEL_ON_OFF = "on_off";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,50 @@
*/
package org.openhab.binding.avmfritz.internal.handler;

import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.*;
import static org.openhab.binding.avmfritz.internal.dto.HeatingModel.*;
import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.BINDING_ID;
import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.CHANNEL_ACTUALTEMP;
import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.CHANNEL_BATTERY;
import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.CHANNEL_BATTERY_LOW;
import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.CHANNEL_BRIGHTNESS;
import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.CHANNEL_COLOR;
import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.CHANNEL_COLORTEMPERATURE;
import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.CHANNEL_COLORTEMPERATURE_ABS;
import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.CHANNEL_COMFORTTEMP;
import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.CHANNEL_CONTACT_STATE;
import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.CHANNEL_DEVICE_LOCKED;
import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.CHANNEL_ECOTEMP;
import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.CHANNEL_ENERGY;
import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.CHANNEL_HUMIDITY;
import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.CHANNEL_LAST_CHANGE;
import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.CHANNEL_LOCKED;
import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.CHANNEL_MODE;
import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.CHANNEL_NEXTTEMP;
import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.CHANNEL_NEXT_CHANGE;
import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.CHANNEL_OBSTRUCTION_ALARM;
import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.CHANNEL_ON_OFF;
import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.CHANNEL_OUTLET;
import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.CHANNEL_POWER;
import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.CHANNEL_RADIATOR_MODE;
import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.CHANNEL_ROLLERSHUTTER;
import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.CHANNEL_SETTEMP;
import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.CHANNEL_TEMPERATURE;
import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.CHANNEL_TEMPERATURE_ALARM;
import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.CHANNEL_VOLTAGE;
import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.CONFIG_CHANNEL_TEMP_OFFSET;
import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.MODE_BOOST;
import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.MODE_COMFORT;
import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.MODE_ECO;
import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.MODE_OFF;
import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.MODE_ON;
import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.MODE_UNKNOWN;
import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.MODE_WINDOW_OPEN;
import static org.openhab.binding.avmfritz.internal.dto.HeatingModel.TEMP_FRITZ_MAX;
import static org.openhab.binding.avmfritz.internal.dto.HeatingModel.TEMP_FRITZ_OFF;
import static org.openhab.binding.avmfritz.internal.dto.HeatingModel.TEMP_FRITZ_ON;
import static org.openhab.binding.avmfritz.internal.dto.HeatingModel.TEMP_FRITZ_UNDEFINED;
import static org.openhab.binding.avmfritz.internal.dto.HeatingModel.fromCelsius;
import static org.openhab.binding.avmfritz.internal.dto.HeatingModel.normalizeCelsius;
import static org.openhab.binding.avmfritz.internal.dto.HeatingModel.toCelsius;

import java.math.BigDecimal;
import java.time.Instant;
Expand Down Expand Up @@ -82,6 +124,8 @@
* @author Christoph Weitkamp - Added support for AVM FRITZ!DECT 300 and Comet DECT
* @author Christoph Weitkamp - Added support for groups
* @author Ulrich Mertin - Added support for HAN-FUN blinds
* @author Christoph Sommer - Added support for color temperature
* @author Tobias Lange - Added abs color temperature and fixed on/off behavior of light blub
*/
@NonNullByDefault
public abstract class AVMFritzBaseThingHandler extends BaseThingHandler implements FritzAhaStatusListener {
Expand Down Expand Up @@ -161,7 +205,8 @@ public void onDeviceUpdated(ThingUID thingUID, AVMFritzBaseModel device) {
if (deviceModel.isHANFUNBlinds()) {
updateLevelControl(deviceModel.getLevelControlModel());
} else if (deviceModel.isColorLight()) {
updateColorLight(deviceModel.getColorControlModel(), deviceModel.getLevelControlModel());
updateColorLight(deviceModel.getColorControlModel(), deviceModel.getLevelControlModel(),
deviceModel.getSimpleOnOffUnit());
} else if (deviceModel.isDimmableLight() && !deviceModel.isHANFUNBlinds()) {
updateDimmableLight(deviceModel.getLevelControlModel());
} else if (deviceModel.isHANFUNUnit() && deviceModel.isHANFUNOnOff()) {
Expand Down Expand Up @@ -218,12 +263,28 @@ private void updateDimmableLight(@Nullable LevelControlModel levelControlModel)
}

private void updateColorLight(@Nullable ColorControlModel colorControlModel,
@Nullable LevelControlModel levelControlModel) {
if (colorControlModel != null && levelControlModel != null) {
@Nullable LevelControlModel levelControlModel, @Nullable SimpleOnOffModel simpleOnOff) {
if (colorControlModel != null && levelControlModel != null && simpleOnOff != null) {
DecimalType hue = new DecimalType(colorControlModel.hue);
PercentType saturation = ColorControlModel.toPercent(colorControlModel.saturation);
PercentType brightness = new PercentType(levelControlModel.getLevelPercentage());
PercentType brightness;
if (simpleOnOff.state) {
vich-667 marked this conversation as resolved.
Show resolved Hide resolved
brightness = new PercentType(levelControlModel.getLevelPercentage());
} else {
brightness = PercentType.ZERO;
}
updateThingChannelState(CHANNEL_COLOR, new HSBType(hue, saturation, brightness));

if (colorControlModel.currentMode == 4) {
int temperature = colorControlModel.temperature;
int tempMired = 1000000 / temperature;
int tempMinMired = 1000000 / 2700;
int tempMaxMired = 1000000 / 6500;
int pct = (tempMired - tempMinMired) * 100 / (tempMaxMired - tempMinMired);
updateThingChannelState(CHANNEL_COLORTEMPERATURE, new PercentType(pct));
updateThingChannelState(CHANNEL_COLORTEMPERATURE_ABS,
new QuantityType<>(BigDecimal.valueOf(temperature), Units.KELVIN));
}
}
}

Expand Down Expand Up @@ -454,7 +515,41 @@ public void handleCommand(ChannelUID channelUID, Command command) {
}
}
if (brightness != null) {
fritzBox.setLevelPercentage(ain, brightness);
if (brightness.equals(BigDecimal.ZERO)) {
fritzBox.setSwitch(ain, false);
} else {
fritzBox.setSwitch(ain, true);
fritzBox.setLevelPercentage(ain, brightness);
}
}
break;
case CHANNEL_COLORTEMPERATURE:
BigDecimal colorTemperaturePct = null;
if (command instanceof PercentType) {
colorTemperaturePct = ((PercentType) command).toBigDecimal();
}
if (colorTemperaturePct != null) {
int pct = colorTemperaturePct.intValue();
// AHA-HTTP-Inteface docu say that the values typically between 2700K and 6500K
int tempMinMired = 1000000 / 2700;
int tempMaxMired = 1000000 / 6500;
int tempScaledMired = tempMinMired + ((tempMaxMired - tempMinMired) * pct / 100);
int tempKelvin = 1000000 / tempScaledMired;
fritzBox.setColorTemperature(ain, tempKelvin, 0);
}
break;
case CHANNEL_COLORTEMPERATURE_ABS:
BigDecimal colorTemperature = null;
if (command instanceof QuantityType) {
QuantityType<?> convertedCommand = ((QuantityType<?>) command).toInvertibleUnit(Units.KELVIN);
if (convertedCommand != null) {
colorTemperature = convertedCommand.toBigDecimal();
}
} else if (command instanceof DecimalType) {
colorTemperature = ((DecimalType) command).toBigDecimal();
}
if (colorTemperature != null) {
fritzBox.setColorTemperature(ain, colorTemperature.intValue(), 0);
}
break;
case CHANNEL_SETTEMP:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.openhab.binding.avmfritz.internal.hardware.callbacks.FritzAhaSetBlindTargetCallback;
import org.openhab.binding.avmfritz.internal.hardware.callbacks.FritzAhaSetBlindTargetCallback.BlindCommand;
import org.openhab.binding.avmfritz.internal.hardware.callbacks.FritzAhaSetColorCallback;
import org.openhab.binding.avmfritz.internal.hardware.callbacks.FritzAhaSetColorTemperatureCallback;
import org.openhab.binding.avmfritz.internal.hardware.callbacks.FritzAhaSetHeatingModeCallback;
import org.openhab.binding.avmfritz.internal.hardware.callbacks.FritzAhaSetHeatingTemperatureCallback;
import org.openhab.binding.avmfritz.internal.hardware.callbacks.FritzAhaSetLevelPercentageCallback;
Expand All @@ -53,6 +54,7 @@
* DECT
* @author Christoph Weitkamp - Added support for groups
* @author Ulrich Mertin - Added support for HAN-FUN blinds
* @author Christoph Sommer - Added support for color temperature
*/
@NonNullByDefault
public class FritzAhaWebInterface {
Expand Down Expand Up @@ -340,6 +342,12 @@ public FritzAhaContentExchange setUnmappedHueAndSaturation(String ain, int hue,
return asyncGet(callback);
}

public FritzAhaContentExchange setColorTemperature(String ain, int temperature, int duration) {
FritzAhaSetColorTemperatureCallback callback = new FritzAhaSetColorTemperatureCallback(this, ain, temperature,
duration);
return asyncGet(callback);
}

public FritzAhaContentExchange setBlind(String ain, BlindCommand command) {
FritzAhaSetBlindTargetCallback callback = new FritzAhaSetBlindTargetCallback(this, ain, command);
return asyncGet(callback);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* Copyright (c) 2010-2023 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.avmfritz.internal.hardware.callbacks;

import static org.eclipse.jetty.http.HttpMethod.GET;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.binding.avmfritz.internal.hardware.FritzAhaWebInterface;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Callback implementation for updating colortemperature. Supports reauthorization
*
* @author Christoph Sommer - Initial contribution
*/
@NonNullByDefault
public class FritzAhaSetColorTemperatureCallback extends FritzAhaReauthCallback {

private final Logger logger = LoggerFactory.getLogger(FritzAhaSetColorTemperatureCallback.class);

private final String ain;

/**
* Constructor
*
* @param webIface Interface to FRITZ!Box
* @param ain AIN of the device that should be switched
* @param temperature Color Temperature in Kelvin (typ.: 2700 to 6500)
* @param duration Duration of the change in 100ms. 0 immediately.
*/
public FritzAhaSetColorTemperatureCallback(FritzAhaWebInterface webIface, String ain, int temperature,
int duration) {
super(WEBSERVICE_PATH,
"switchcmd=setcolortemperature&temperature=" + temperature + "&duration=" + duration + "&ain=" + ain,
webIface, GET, 1);
this.ain = ain;
}

@Override
public void execute(int status, String response) {
super.execute(status, response);
if (isValidRequest()) {
logger.debug("Received response '{}' for item '{}'", response, ain);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,14 @@

<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"/>
lolodomo marked this conversation as resolved.
Show resolved Hide resolved
</channels>

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

<representation-property>ain</representation-property>

<config-description-ref uri="thing-type:avmfritz:fritzdevice"/>
Expand Down Expand Up @@ -411,8 +417,14 @@

<channels>
<channel id="color" typeId="system.color"/>
<channel id="color_temperature" typeId="system.color-temperature"/>
vich-667 marked this conversation as resolved.
Show resolved Hide resolved
<channel id="color_temperature_abs" typeId="system.color-temperature-abs"/>
</channels>

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

<representation-property>ain</representation-property>

<config-description-ref uri="thing-type:avmfritz:fritzdevice"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,26 @@
</instruction-set>
</thing-type>

<thing-type uid="avmfritz:FRITZ_DECT_500">
<instruction-set targetVersion="1">
<add-channel id="color_temperature">
<type>system:color-temperature</type>
</add-channel>
<add-channel id="color_temperature_abs">
<type>system:color-temperature-abs</type>
</add-channel>
</instruction-set>
</thing-type>

<thing-type uid="avmfritz:HAN_FUN_COLOR_BULB">
<instruction-set targetVersion="1">
<add-channel id="color_temperature">
<type>system:color-temperature</type>
</add-channel>
<add-channel id="color_temperature_abs">
<type>system:color-temperature-abs</type>
</add-channel>
</instruction-set>
</thing-type>

</update:update-descriptions>