Skip to content

Commit

Permalink
[tuya] Improve number channel handling (#561)
Browse files Browse the repository at this point in the history
Signed-off-by: Jan N. Klug <[email protected]>
  • Loading branch information
J-N-K committed Jan 21, 2024
1 parent 315858c commit fd65f7c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class ChannelConfiguration {
public int dp2 = 0;
public int min = Integer.MIN_VALUE;
public int max = Integer.MAX_VALUE;
public boolean sendAsString = false;
public boolean reversed = false;
public String range = "";
public String irCode = "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ public void processDeviceStatus(Map<Integer, Object> deviceStatus) {
private void processChannelStatus(Integer dp, Object value) {
String channelId = dpToChannelId.get(dp);
if (channelId != null) {

ChannelConfiguration configuration = channelIdToConfiguration.get(channelId);
ChannelTypeUID channelTypeUID = channelIdToChannelTypeUID.get(channelId);

Expand Down Expand Up @@ -180,6 +179,8 @@ private void processChannelStatus(Integer dp, Object value) {
&& CHANNEL_TYPE_UID_NUMBER.equals(channelTypeUID)) {
updateState(channelId, new DecimalType((double) value));
return;
} else if (value instanceof String string && CHANNEL_TYPE_UID_NUMBER.equals(channelTypeUID)) {
updateState(channelId, new DecimalType(string));
} else if (Boolean.class.isAssignableFrom(value.getClass())
&& CHANNEL_TYPE_UID_SWITCH.equals(channelTypeUID)) {
updateState(channelId, OnOffType.from((boolean) value));
Expand Down Expand Up @@ -324,12 +325,12 @@ public void handleCommand(ChannelUID channelUID, Command command) {
}
}
} else if (CHANNEL_TYPE_UID_STRING.equals(channelTypeUID)) {
if (command instanceof StringType) {
commandRequest.put(configuration.dp, command.toString());
}
commandRequest.put(configuration.dp, command.toString());
} else if (CHANNEL_TYPE_UID_NUMBER.equals(channelTypeUID)) {
if (command instanceof DecimalType) {
commandRequest.put(configuration.dp, ((DecimalType) command).intValue());
if (command instanceof DecimalType decimalType) {
commandRequest.put(configuration.dp,
configuration.sendAsString ? String.format("%d", decimalType.intValue())
: decimalType.intValue());
}
} else if (CHANNEL_TYPE_UID_SWITCH.equals(channelTypeUID)) {
if (command instanceof OnOffType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
</parameter>
<parameter name="localKey" type="text" required="true">
<label>Device Local Key</label>
<context>password</context>
</parameter>
<parameter name="productId" type="text" required="true">
<label>Product ID</label>
Expand Down Expand Up @@ -174,6 +175,12 @@
<parameter name="max" type="integer">
<label>Maximum</label>
</parameter>
<parameter name="sendAdString" type="boolean">
<label>Send As String</label>
<description>Send the value as string instead of number.</description>
<default>false</default>
<advanced>true</advanced>
</parameter>
</config-description>
</channel-type>

Expand Down

0 comments on commit fd65f7c

Please sign in to comment.