Skip to content

Commit

Permalink
[sbus] apply suggested changes
Browse files Browse the repository at this point in the history
Signed-off-by: Ciprian Pascu <[email protected]>
  • Loading branch information
Ciprian Pascu committed Jan 12, 2025
1 parent fe10c83 commit b2935be
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,10 @@ private BindingConstants() {
public static final String CHANNEL_BLUE = "blue";
public static final String CHANNEL_WHITE = "white";
public static final String CHANNEL_COLOR = "color";

// Channel Types
public static final String CHANNEL_TYPE_COLOR = "color-channel";
public static final String CHANNEL_TYPE_SWITCH = "switch-channel";
public static final String CHANNEL_TYPE_DIMMER = "dimmer-channel";
public static final String CHANNEL_TYPE_PAIRED = "paired-channel";
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package org.openhab.binding.sbus.handler;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.binding.sbus.BindingConstants;
import org.openhab.binding.sbus.handler.config.SbusChannelConfig;
import org.openhab.binding.sbus.handler.config.SbusDeviceConfig;
import org.openhab.core.library.types.HSBType;
Expand Down Expand Up @@ -138,22 +139,21 @@ protected void initializeChannels() {
continue;
}
String channelTypeId = channelTypeUID.getId();
if ("color-channel".equals(channelTypeId)) {
if (BindingConstants.CHANNEL_TYPE_COLOR.equals(channelTypeId)) {
if (channelConfig.channelNumber <= 0) {
logger.warn("Channel {} has invalid channel number configuration", channel.getUID());
}
}
if ("switch-channel".equals(channelTypeId)) {
if (BindingConstants.CHANNEL_TYPE_SWITCH.equals(channelTypeId)) {
switchChannelCount++;
if (channelConfig.channelNumber <= 0) {
logger.warn("Channel {} has invalid channel number configuration", channel.getUID());
}
}
}
if (switchChannelCount > 1) {
logger.error("Only one switch channel is allowed for RGBW thing {}", getThing().getUID());
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
"Only one switch channel is allowed");
"Only one switch channel is allowed for RGBW thing " + getThing().getUID());
return;
}
}
Expand All @@ -179,15 +179,15 @@ protected void pollDevice() {
String channelTypeId = channelTypeUID.getId();
SbusChannelConfig channelConfig = channel.getConfiguration().as(SbusChannelConfig.class);

if ("color-channel".equals(channelTypeId)) {
if (BindingConstants.CHANNEL_TYPE_COLOR.equals(channelTypeId)) {
// Read RGBW values for this channel
int[] rgbwValues = adapter.readRgbw(config.subnetId, config.id, channelConfig.channelNumber);
if (rgbwValues.length >= 4) {
// Convert RGBW to HSB using our custom conversion
HSBType hsbType = rgbwToHsb(rgbwValues);
updateState(channel.getUID(), hsbType);
}
} else if ("switch-channel".equals(channelTypeId)) {
} else if (BindingConstants.CHANNEL_TYPE_SWITCH.equals(channelTypeId)) {
// Read status channels for switch states
int[] statuses = adapter.readStatusChannels(config.subnetId, config.id);

Expand All @@ -207,7 +207,6 @@ protected void pollDevice() {
public void handleCommand(ChannelUID channelUID, Command command) {
final SbusService adapter = super.sbusAdapter;
if (adapter == null) {
logger.warn("Sbus adapter not initialized");
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Sbus adapter not initialized");
return;
}
Expand All @@ -224,22 +223,23 @@ public void handleCommand(ChannelUID channelUID, Command command) {
SbusDeviceConfig config = getConfigAs(SbusDeviceConfig.class);
SbusChannelConfig channelConfig = channel.getConfiguration().as(SbusChannelConfig.class);

if ("color-channel".equals(channelTypeId) && command instanceof HSBType hsbCommand) {
if (BindingConstants.CHANNEL_TYPE_COLOR.equals(channelTypeId)
&& command instanceof HSBType hsbCommand) {
// Handle color command
int[] rgbw = hsbToRgbw(hsbCommand);
adapter.writeRgbw(config.subnetId, config.id, channelConfig.channelNumber, rgbw[0], rgbw[1],
rgbw[2], rgbw[3]);
updateState(channelUID, hsbCommand);
} else if ("switch-channel".equals(channelTypeId) && command instanceof OnOffType onOffCommand) {
} else if (BindingConstants.CHANNEL_TYPE_SWITCH.equals(channelTypeId)
&& command instanceof OnOffType onOffCommand) {
// Handle switch command
boolean isOn = onOffCommand == OnOffType.ON;
adapter.writeSingleChannel(config.subnetId, config.id, channelConfig.channelNumber, isOn ? 100 : 0,
-1);
updateState(channelUID, isOn ? OnOffType.ON : OnOffType.OFF);
updateState(channelUID, OnOffType.from(isOn));
}
}
} catch (Exception e) {
logger.error("Error handling command", e);
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Error sending command to device");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package org.openhab.binding.sbus.handler;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.binding.sbus.BindingConstants;
import org.openhab.binding.sbus.handler.config.SbusChannelConfig;
import org.openhab.binding.sbus.handler.config.SbusDeviceConfig;
import org.openhab.core.library.types.OnOffType;
Expand Down Expand Up @@ -79,11 +80,11 @@ protected void pollDevice() {
// 100 when on, something else when off
boolean isActive = statuses[channelConfig.channelNumber - 1] == 0x64;

if ("switch-channel".equals(channelTypeId)) {
updateState(channel.getUID(), isActive ? OnOffType.ON : OnOffType.OFF);
} else if ("dimmer-channel".equals(channelTypeId)) {
if (BindingConstants.CHANNEL_TYPE_SWITCH.equals(channelTypeId)) {
updateState(channel.getUID(), OnOffType.from(isActive));
} else if (BindingConstants.CHANNEL_TYPE_DIMMER.equals(channelTypeId)) {
updateState(channel.getUID(), new PercentType(statuses[channelConfig.channelNumber - 1]));
} else if ("paired-channel".equals(channelTypeId)) {
} else if (BindingConstants.CHANNEL_TYPE_PAIRED.equals(channelTypeId)) {
updateState(channel.getUID(), isActive ? OpenClosedType.OPEN : OpenClosedType.CLOSED);
}
}
Expand All @@ -97,7 +98,6 @@ protected void pollDevice() {
public void handleCommand(ChannelUID channelUID, Command command) {
final SbusService adapter = super.sbusAdapter;
if (adapter == null) {
logger.warn("Sbus adapter not initialized");
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Sbus adapter not initialized");
return;
}
Expand All @@ -113,16 +113,16 @@ public void handleCommand(ChannelUID channelUID, Command command) {

SbusDeviceConfig config = getConfigAs(SbusDeviceConfig.class);

if (command instanceof OnOffType) {
handleOnOffCommand((OnOffType) command, config, channelConfig, channelUID, adapter);
} else if (command instanceof PercentType) {
handlePercentCommand((PercentType) command, config, channelConfig, channelUID, adapter);
} else if (command instanceof OpenClosedType) {
handleOpenClosedCommand((OpenClosedType) command, config, channelConfig, channelUID, adapter);
if (command instanceof OnOffType onOffCommand) {
handleOnOffCommand(onOffCommand, config, channelConfig, channelUID, adapter);
} else if (command instanceof PercentType percentCommand) {
handlePercentCommand(percentCommand, config, channelConfig, channelUID, adapter);
} else if (command instanceof OpenClosedType openClosedCommand) {
handleOpenClosedCommand(openClosedCommand, config, channelConfig, channelUID, adapter);
}
}
} catch (Exception e) {
logger.error("Error handling command", e);
logger.warn("Error handling command", e);
}
}

Expand All @@ -131,7 +131,7 @@ private void handleOnOffCommand(OnOffType command, SbusDeviceConfig config, Sbus
boolean isOn = command == OnOffType.ON;
adapter.writeSingleChannel(config.subnetId, config.id, channelConfig.channelNumber, isOn ? 100 : 0,
channelConfig.timer);
updateState(channelUID, isOn ? OnOffType.ON : OnOffType.OFF);
updateState(channelUID, OnOffType.from(isOn));
}

private void handlePercentCommand(PercentType command, SbusDeviceConfig config, SbusChannelConfig channelConfig,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,61 +18,6 @@
<description>Port of the slave</description>
<default>6000</default>
</parameter>

<parameter name="enableDiscovery" type="boolean">
<label>Discovery Enabled</label>
<description>When enabled we try to find a device specific handler. Turn this on if you're using one of the
supported devices.</description>
<default>false</default>
</parameter>

<parameter name="rtuEncoded" type="boolean">
<label>RTU Encoding</label>
<description>Use RTU Encoding over IP</description>
<default>false</default>
</parameter>

<!-- connection handling -->
<parameter name="timeBetweenTransactionsMillis" type="integer" min="0" unit="ms">
<label>Time Between Transactions</label>
<description>How long to delay we must have at minimum between two consecutive Sbus transactions. In milliseconds.
</description>
<default>60</default>
</parameter>
<parameter name="timeBetweenReconnectMillis" type="integer" min="0" unit="ms">
<label>Time Between Reconnections</label>
<description>How long to wait to before trying to establish a new connection after the previous one has been
disconnected. In milliseconds.</description>
<default>0</default>
<advanced>true</advanced>
</parameter>
<parameter name="connectMaxTries" type="integer" min="1">
<label>Maximum Connection Tries</label>
<description>How many times we try to establish the connection. Should be at least 1.</description>
<default>1</default>
<advanced>true</advanced>
</parameter>
<parameter name="afterConnectionDelayMillis" type="integer" min="0" unit="ms">
<label>Connection warm-up time</label>
<description>Connection warm-up time. Additional time which is spent on preparing connection which should be spent
waiting while end device is getting ready to answer first sbus call. In milliseconds.</description>
<default>0</default>
<advanced>true</advanced>
</parameter>
<parameter name="reconnectAfterMillis" type="integer" min="0" unit="ms">
<label>Reconnect Again After</label>
<description>The connection is kept open at least the time specified here. Value of zero means that connection is
disconnected after every Sbus transaction. In milliseconds.</description>
<default>0</default>
<advanced>true</advanced>
</parameter>
<parameter name="connectTimeoutMillis" type="integer" min="0" unit="ms">
<label>Timeout for Establishing the Connection</label>
<description>The maximum time that is waited when establishing the connection. Value of zero means that system/OS
default is respected. In milliseconds.</description>
<default>10000</default>
<advanced>true</advanced>
</parameter>
</config-description>
</bridge-type>
</thing:thing-descriptions>

0 comments on commit b2935be

Please sign in to comment.