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

[zigbee] Add a color temperature absolute channel #868

Merged
merged 5 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ public class ZigBeeBindingConstants {
public static final String CHANNEL_LABEL_COLOR_TEMPERATURE = "Color Temperature";
public static final ChannelTypeUID CHANNEL_COLOR_TEMPERATURE = SYSTEM_COLOR_TEMPERATURE.getUID();

public static final String CHANNEL_NAME_COLOR_TEMPERATURE_ABS = "colortemperature_abs";
public static final ChannelTypeUID CHANNEL_COLOR_TEMPERATURE_ABS = SYSTEM_COLOR_TEMPERATURE_ABS.getUID();

public static final String CHANNEL_NAME_ILLUMINANCE_VALUE = "illuminance";
public static final String CHANNEL_LABEL_ILLUMINANCE_VALUE = "Illuminance";
public static final ChannelTypeUID CHANNEL_ILLUMINANCE_VALUE = new ChannelTypeUID("zigbee:measurement_illuminance");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1149,4 +1149,8 @@ public boolean isDeviceInitialized() {
public ZigBeeNode getNode() {
return coordinatorHandler.getNode(nodeIeeeAddress);
}

public void putStateDescription(ChannelUID channelUID, StateDescription stateDescription) {
andrewfg marked this conversation as resolved.
Show resolved Hide resolved
stateDescriptions.put(channelUID, stateDescription);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ public class ZigBeeConverterColorTemperature extends ZigBeeBaseChannelConverter

private Logger logger = LoggerFactory.getLogger(ZigBeeConverterColorTemperature.class);

private ZclColorControlCluster clusterColorControl;
protected ZclColorControlCluster clusterColorControl;
private ZclOnOffCluster clusterOnOff;

private double kelvinMin;
private double kelvinMax;
protected double kelvinMin;
protected double kelvinMax;
private double kelvinRange;

// Default range of 2000K to 6500K
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/**
* 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.zigbee.internal.converter;

import java.math.BigDecimal;

import org.openhab.binding.zigbee.ZigBeeBindingConstants;
import org.openhab.binding.zigbee.handler.ZigBeeCoordinatorHandler;
import org.openhab.core.library.types.QuantityType;
import org.openhab.core.library.unit.Units;
import org.openhab.core.thing.Channel;
import org.openhab.core.thing.ThingUID;
import org.openhab.core.thing.binding.builder.ChannelBuilder;
import org.openhab.core.types.Command;
import org.openhab.core.types.StateDescriptionFragmentBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.zsmartsystems.zigbee.IeeeAddress;
import com.zsmartsystems.zigbee.ZigBeeEndpoint;
import com.zsmartsystems.zigbee.zcl.ZclAttribute;
import com.zsmartsystems.zigbee.zcl.clusters.ZclColorControlCluster;
import com.zsmartsystems.zigbee.zcl.clusters.colorcontrol.MoveToColorTemperatureCommand;
import com.zsmartsystems.zigbee.zcl.protocol.ZclClusterType;

import tech.units.indriya.unit.UnitDimension;

/**
* Channel converter for color temperature, converting between the color control cluster and a Kelvin
* QuantityType<Temperature> channel.
*
* @author Andrew Fiddian-Green - Initial Contribution
*/
public class ZigBeeConverterColorTemperatureAbs extends ZigBeeConverterColorTemperature {

private Logger logger = LoggerFactory.getLogger(ZigBeeConverterColorTemperatureAbs.class);

private boolean updateStateDescriptionDone;

@Override
public void initialize(Channel channel, ZigBeeCoordinatorHandler coordinator, IeeeAddress address, int endpointId) {
super.initialize(channel, coordinator, address, endpointId);
updateStateDescriptionDone = false;
}

@Override
public void handleCommand(final Command command) {
if (command instanceof QuantityType<?> quantity && UnitDimension.TEMPERATURE == quantity.getDimension()) {
QuantityType<?> kelvin = quantity.toInvertibleUnit(Units.KELVIN);
if (kelvin != null) {
logger.debug("handleCommand() channel {} to {}", channelUID, kelvin);
andrewfg marked this conversation as resolved.
Show resolved Hide resolved
MoveToColorTemperatureCommand zclCommand = new MoveToColorTemperatureCommand(
1000000 / kelvin.intValue(), 10);
monitorCommandResponse(command, clusterColorControl.sendCommand(zclCommand));
}
} else {
super.handleCommand(command);
}
}

@Override
public Channel getChannel(ThingUID thingUID, ZigBeeEndpoint endpoint) {
return super.getChannel(thingUID, endpoint) == null ? null
: ChannelBuilder
.create(createChannelUID(thingUID, endpoint,
ZigBeeBindingConstants.CHANNEL_NAME_COLOR_TEMPERATURE_ABS),
ZigBeeBindingConstants.ITEM_TYPE_NUMBER_TEMPERATURE)
.withType(ZigBeeBindingConstants.CHANNEL_COLOR_TEMPERATURE_ABS)
.withLabel(ZigBeeBindingConstants.CHANNEL_LABEL_COLOR_TEMPERATURE)
.withProperties(createProperties(endpoint)).build();
}

@Override
public void attributeUpdated(ZclAttribute attribute, Object val) {
if (attribute.getClusterType() == ZclClusterType.COLOR_CONTROL
&& attribute.getId() == ZclColorControlCluster.ATTR_COLORTEMPERATURE && val instanceof Integer mired) {
updateStateDescription();
updateChannelState(QuantityType.valueOf(1000000 / mired, Units.KELVIN));
} else {
super.attributeUpdated(attribute, val);
andrewfg marked this conversation as resolved.
Show resolved Hide resolved
}
}

/**
* Set the state description minimum and maximum values and pattern in Kelvin for the given channel UID
*/
private void updateStateDescription() {
if (!updateStateDescriptionDone) {
updateStateDescriptionDone = true;
logger.debug("updateStateDescription() {} state min/max = {}/{} Kelvin", channelUID, kelvinMin, kelvinMax);
andrewfg marked this conversation as resolved.
Show resolved Hide resolved
thing.putStateDescription(channelUID,
StateDescriptionFragmentBuilder.create().withMinimum(BigDecimal.valueOf(kelvinMin))
.withMaximum(BigDecimal.valueOf(kelvinMax)).withStep(BigDecimal.valueOf(100))
.withPattern("%.0f K").build().toStateDescription());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public ZigBeeDefaultChannelConverterProvider() {
// Add all the converters into the map...
channelMap.put(ZigBeeBindingConstants.CHANNEL_COLOR_COLOR, ZigBeeConverterColorColor.class);
channelMap.put(ZigBeeBindingConstants.CHANNEL_COLOR_TEMPERATURE, ZigBeeConverterColorTemperature.class);
channelMap.put(ZigBeeBindingConstants.CHANNEL_COLOR_TEMPERATURE_ABS, ZigBeeConverterColorTemperatureAbs.class);
channelMap.put(ZigBeeBindingConstants.CHANNEL_DOORLOCK_STATE, ZigBeeConverterDoorLock.class);
channelMap.put(ZigBeeBindingConstants.CHANNEL_ELECTRICAL_ACTIVEPOWER, ZigBeeConverterMeasurementPower.class);
channelMap.put(ZigBeeBindingConstants.CHANNEL_HUMIDITY_VALUE, ZigBeeConverterRelativeHumidity.class);
Expand Down