[rfxcom] Add ability to properly receive configured command ids, deprecated hard-coded guesses. ()

Currently, when a message is received the command will be determined only from the hard-coded set of ON/OFF commands, which means if I configure
a thing and attach it to a switch there is no guarantee that it will respond as expected to receive commands. This PR changes the message factory
to require either bytes (from the RFXcom device) or all the details required to make a transmissable message, including the confguration of the
associated Thing. At the moment, this is only used by lighting4 and raw types, but I expect it will be useful for others in the future.

The hard-coded ON/OFF commands in lighting4 are based on experimentation with different devices, and are not at all consistent. This PR deprecates the
use of those so that in a future release, Lighting4 devices will only work when configured with the correct command ids up front. This will resolve
inconsistent behaviour where devices that have been discovered may work correctly, work only for ON or OFF but not both, have ON and OFF the wrong
way around, turn ON one physical device and OFF another, or any combination of those possibilities.

Signed-off-by: James Hewitt <james.hewitt@uk.ibm.com>
This commit is contained in:
James Hewitt 2021-07-18 20:37:44 +01:00 committed by GitHub
parent fcc23f0ab3
commit 487dc0e49f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
72 changed files with 915 additions and 590 deletions
bundles/org.openhab.binding.rfxcom
README.md
src
main
test/java/org/openhab/binding/rfxcom/internal

@ -136,15 +136,10 @@ Bridge rfxcom:tcpbridge:sunflower [ host="sunflower", port=10001 ] {
## Thing Configuration
Available configuration parameters are:
| Applies to | Parameter Label | Parameter ID | Description | Required | Default |
|------------|-----------------|--------------|----------------------------------------------------------------------|----------|---------|
| All things | Device ID | deviceId | (Unique) id of the device, for example "100001.1", "B.8" or "286169" | true | |
| All things | Sub type | subType | Sub type, note that every thing-type has its own sub types | true | |
| Lighting4 | Pulse | pulse | Pulse length used by the device, only used when sending | false | 350 |
| Lighting4 | On command ID | onCommandId | Id of the command which should be send to turn the device ON | false | 1 |
| Lighting4 | Off command ID | offCommandId | Id of the command which should be send to turn the device OFF | false | 4 |
Configuration parameters are listed alongside each thing type. Most devices only require a deviceId and
a subType, but some things require additional configuration. The deviceId is used both when receiving and
transmitting messages, the subType is mainly used when sending messages, but it can vary between device
types.
## Channels
@ -690,7 +685,27 @@ A Lighting2 device
### lighting4 - RFXCOM Lighting4 Actuator
A Lighting4 device
A Lighting4 device. The specification for the PT2262 protocol includes 3 bytes for data. By
convention, the first 20 bits of this is used for deviceId, and the last 4 bits is used for
command, which gives us a total of 16 commands per device.
Depending on your device, you may have only one command, one pair of commands (on/off), or
any other multiple, for example, a set of 4 sockets with an on/off pair for each and an
additional pair for "all".
Different device manufactures using this protocol will use different schemes for their
commands, so to configure a thing using the lighting4 protocol, you must specify at least
one commandId in the thing configuration. If a device has multiple sets of commands, you
can configure multiple things with the same device id, but different commandIds.
Some devices will expect a specific pulse length. If required, that can also be specified
as a thing configuration parameter.
Previously, openHAB would attempt to guess at the meaning of a commandId if it was not
specified in the thing configuration based on devices seen in the wild. Due to the varying
nature of devices, this behaviour is deprecated and will be removed in a future openHAB
version. Until then, commands 1, 3, 5-13 and 15 are considered ON and 0, 2, 4 and 14 are
considered OFF when the `onCommandId` or `offCommandId` for a device is not specified.
#### Channels
@ -709,64 +724,46 @@ A Lighting4 device
* subType - Sub Type
* Specifies device sub type.
* PT2262 - PT2262
* PT2262 - PT2262
* pulse - Pulse length
* Pulse length of the device
* onCommandId - On command
* Specifies command to be send when ON must be transmitted
* 0 - OFF (value 0)
* 1 - ON (value 1)
* 2 - OFF (value 2)
* 3 - ON (value 3)
* 4 - OFF (value 4)
* 5 - ON (value 5)
* 6 - value 6
* 7 - ON (value 7)
* 8 - value 8
* 9 - ON (value 9)
* 10 - ON (value 10)
* 11 - ON (value 11)
* 12 - ON (value 12)
* 13 - value 13
* 14 - OFF (value 14)
* 15 - value 15
* Specifies command that represents ON for this device.
* offCommandId - Off command
* Specifies command to be send when OFF must be transmitted
* Specifies command that represents OFF for this device.
* 0 - OFF (value 0)
* 1 - ON (value 1)
* 2 - OFF (value 2)
* 3 - ON (value 3)
* 4 - OFF (value 4)
* 5 - ON (value 5)
* 6 - value 6
* 7 - ON (value 7)
* 8 - value 8
* 9 - ON (value 9)
* 10 - ON (value 10)
* 11 - ON (value 11)
* 12 - ON (value 12)
* 13 - value 13
* 14 - OFF (value 14)
* 15 - value 15
* openCommandId - Open command
* Specifies command that represents OPEN for this device.
* closedCommandId - Closed command
* Specifies command that represents CLOSED for this device.
#### Discovering commandId values
There are a number of ways to detect the commandId values for your device.
- You can turn on DEBUG messages for the rfxcom binding by adding the line
`<Logger level="DEBUG" name="org.openhab.binding.rfxcom"/>`
to your `log4j2.xml`. You will then be able to see the commandId in the log
file when you trigger the device.
- You can link a Number Item to the commandId channel. The item will be updated with the
detected commandId when you trigger the device.
- You can use RFXmngr to look at the data from the device. Use the last letter/number
of the hexadecimal "Code", and convert it from hexadecimal to decimal.
#### Examples
The support for lighting 4 in RFXCOM is less complete because a lot of different devices use the same chips and can not easily be distinguished.
So some extra configuration can be used for fine tuning the behavior of your Lighting4 devices.
When configuring, three extra fields are available, being the the pulse length, and a separate command id for both on and off.
If your item is auto-discovered normally the on or off command should be recognized properly.
For a USB attached RFXCOM on Windows the configuration could look like this (note that the `onCommandId`, `offCommandId` and `pulse` are all optional):
For a USB attached RFXCOM on Windows the configuration could look like this (note the `pulse` is optional):
```
Bridge rfxcom:bridge:238adf67 [ serialPort="COM4" ] {
Thing lighting4 17745 [deviceId="17745", subType="PT2262", onCommandId=7, offCommandId=4, pulse=800]
Thing lighting4 17745a [deviceId="17745", subType="PT2262", onCommandId=7, offCommandId=4]
Thing lighting4 17745b [deviceId="17745", subType="PT2262", onCommandId=10, offCommandId=2]
Thing lighting4 motion [deviceId="286169", subType="PT2262", onCommandId=9, pulse=392]
}
```
@ -774,30 +771,31 @@ Bridge rfxcom:bridge:238adf67 [ serialPort="COM4" ] {
Your items file could look like this:
```
Switch Switch {channel="rfxcom:lighting4:238adf67:17745:command"}
Number SwitchCommandId "Command ID [%d]" {channel="rfxcom:lighting4:238adf67:17745:commandId"}
Number SocketCommandId {channel="rfxcom:lighting4:238adf67:17745a:commandId"}
Switch SocketA {channel="rfxcom:lighting4:238adf67:17745a:command"}
Switch SocketB {channel="rfxcom:lighting4:238adf67:17745b:command"}
```
And if you want random actions on your relay you could for example do like this:
#### Known commandIds
```
rule "Set random relay variations"
when
System started or
Time cron "/20 * * * * ?"
then
SwitchCommandId.sendCommand((Math::random * 15.9).intValue)
end
```
These are some commandIds from the field that may match your devices.
#### Devices:
| Brand | What | Action | Command ID | Supported | Source |
|-------|---------------|-------------|------------|-----------|--------|
| Kerui | Motion Sensor | Motion | 10 | as ON | [#3103](https://github.com/openhab/openhab-addons/issues/3103) |
| Kerui | Door Contact | door open | 14 | as OFF | [#3103](https://github.com/openhab/openhab-addons/issues/3103) |
| Kerui | Door Contact | door closed | 7 | as ON | [#3103](https://github.com/openhab/openhab-addons/issues/3103) |
| Kerui | Door Contact | tamper | 7 | as ON | [#3103](https://github.com/openhab/openhab-addons/issues/3103) |
| Brand | What | Action | Command ID | Source |
|-------|---------------|-------------|------------|--------|
| Kerui | Motion Sensor | Motion | 10 | [#3103](https://github.com/openhab/openhab-addons/issues/3103) |
| Kerui | Door Contact | door open | 14 | [#3103](https://github.com/openhab/openhab-addons/issues/3103) |
| Kerui | Door Contact | door closed | 7 | [#3103](https://github.com/openhab/openhab-addons/issues/3103) |
| Kerui | Door Contact | tamper | 11 | [#3103](https://github.com/openhab/openhab-addons/issues/3103) |
| Energenie | 4 Socket Power Bar | Socket 1 on | 15 | [Community](https://community.openhab.org/t/rfxcom-looking-to-improve-lighting4-call-for-users/123674) |
| Energenie | 4 Socket Power Bar | Socket 1 off | 14 | [Community](https://community.openhab.org/t/rfxcom-looking-to-improve-lighting4-call-for-users/123674) |
| Energenie | 4 Socket Power Bar | Socket 2 on | 7 | [Community](https://community.openhab.org/t/rfxcom-looking-to-improve-lighting4-call-for-users/123674) |
| Energenie | 4 Socket Power Bar | Socket 2 off | 6 | [Community](https://community.openhab.org/t/rfxcom-looking-to-improve-lighting4-call-for-users/123674) |
| Energenie | 4 Socket Power Bar | Socket 3 on | 11 | [Community](https://community.openhab.org/t/rfxcom-looking-to-improve-lighting4-call-for-users/123674) |
| Energenie | 4 Socket Power Bar | Socket 3 off | 10 | [Community](https://community.openhab.org/t/rfxcom-looking-to-improve-lighting4-call-for-users/123674) |
| Energenie | 4 Socket Power Bar | Socket 4 on | 3 | [Community](https://community.openhab.org/t/rfxcom-looking-to-improve-lighting4-call-for-users/123674) |
| Energenie | 4 Socket Power Bar | Socket 4 off | 2 | [Community](https://community.openhab.org/t/rfxcom-looking-to-improve-lighting4-call-for-users/123674) |
| Energenie | 4 Socket Power Bar | All on | 13 | [Community](https://community.openhab.org/t/rfxcom-looking-to-improve-lighting4-call-for-users/123674) |
| Energenie | 4 Socket Power Bar | All off | 12 | [Community](https://community.openhab.org/t/rfxcom-looking-to-improve-lighting4-call-for-users/123674) |
### lighting5 - RFXCOM Lighting5 Actuator

@ -21,7 +21,11 @@ public class RFXComLighting4DeviceConfiguration extends RFXComGenericDeviceConfi
public static final String PULSE_LABEL = "pulse";
public static final String ON_COMMAND_ID_LABEL = "onCommandId";
public static final String OFF_COMMAND_ID_LABEL = "offCommandId";
public static final String OPEN_COMMAND_ID_LABEL = "openCommandId";
public static final String CLOSED_COMMAND_ID_LABEL = "closedCommandId";
public Integer pulse;
public Integer onCommandId;
public Integer offCommandId;
public Integer openCommandId;
public Integer closedCommandId;
}

@ -102,8 +102,10 @@ public class RFXComBridgeHandler extends BaseBridgeHandler {
RFXComBaseMessage msg = queue.peek();
try {
logger.debug("Transmitting message '{}'", msg);
byte[] data = msg.decodeMessage();
if (logger.isDebugEnabled()) {
logger.debug("Transmitting bytes '{}' for message '{}'", HexUtils.bytesToHex(data), msg);
}
connector.sendMessage(data);
break;
} catch (RFXComException rfxe) {

@ -95,10 +95,7 @@ public class RFXComHandler extends BaseThingHandler implements DeviceMessageList
PacketType packetType = RFXComMessageFactoryImpl
.convertPacketType(getThing().getThingTypeUID().getId().toUpperCase());
RFXComMessage msg = messageFactory.createMessage(packetType);
msg.setConfig(config);
msg.convertFromState(channelUID.getId(), command);
RFXComMessage msg = messageFactory.createMessage(packetType, config, channelUID, command);
bridgeHandler.sendMessage(msg);
} catch (RFXComMessageNotImplementedException e) {
@ -180,9 +177,9 @@ public class RFXComHandler extends BaseThingHandler implements DeviceMessageList
try {
if (config.matchesMessage(message)) {
String receivedId = PACKET_TYPE_THING_TYPE_UID_MAP.get(message.getPacketType()).getId();
logger.debug("Received message from bridge: {} message: {}", bridge, message);
if (receivedId.equals(getThing().getThingTypeUID().getId())) {
logger.debug("Message from bridge [{}] matches thing [{}] message: {}", bridge,
getThing().getUID().toString(), message);
updateStatus(ThingStatus.ONLINE);
for (Channel channel : getThing().getChannels()) {
@ -194,19 +191,21 @@ public class RFXComHandler extends BaseThingHandler implements DeviceMessageList
case CHANNEL_COMMAND:
case CHANNEL_CHIME_SOUND:
case CHANNEL_MOOD:
postNullableCommand(uid, message.convertToCommand(channelId, this));
postNullableCommand(uid, message.convertToCommand(channelId, config, this));
break;
case CHANNEL_LOW_BATTERY:
updateNullableState(uid,
isLowBattery(message.convertToState(CHANNEL_BATTERY_LEVEL, this)));
isLowBattery(message.convertToState(CHANNEL_BATTERY_LEVEL, config, this)));
break;
default:
updateNullableState(uid, message.convertToState(channelId, this));
updateNullableState(uid, message.convertToState(channelId, config, this));
break;
}
} catch (RFXComException e) {
} catch (RFXComInvalidStateException e) {
logger.trace("{} not configured for {}", channelId, message);
} catch (RFXComUnsupportedChannelException e) {
logger.trace("{} does not handle {}", channelId, message);
}
}

@ -15,7 +15,9 @@ package org.openhab.binding.rfxcom.internal.messages;
import static org.openhab.binding.rfxcom.internal.RFXComBindingConstants.*;
import static org.openhab.binding.rfxcom.internal.messages.ByteEnumUtil.fromByte;
import org.openhab.binding.rfxcom.internal.config.RFXComDeviceConfiguration;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComInvalidStateException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedChannelException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedValueException;
import org.openhab.binding.rfxcom.internal.handler.DeviceState;
@ -124,13 +126,14 @@ public class RFXComBBQTemperatureMessage extends RFXComBatteryDeviceMessage<RFXC
}
@Override
public State convertToState(String channelId, DeviceState deviceState) throws RFXComUnsupportedChannelException {
public State convertToState(String channelId, RFXComDeviceConfiguration config, DeviceState deviceState)
throws RFXComUnsupportedChannelException, RFXComInvalidStateException {
if (CHANNEL_FOOD_TEMPERATURE.equals(channelId)) {
return new DecimalType(foodTemperature);
} else if (CHANNEL_BBQ_TEMPERATURE.equals(channelId)) {
return new DecimalType(bbqTemperature);
} else {
return super.convertToState(channelId, deviceState);
return super.convertToState(channelId, config, deviceState);
}
}

@ -14,6 +14,8 @@ package org.openhab.binding.rfxcom.internal.messages;
import static org.openhab.binding.rfxcom.internal.RFXComBindingConstants.CHANNEL_BATTERY_LEVEL;
import org.openhab.binding.rfxcom.internal.config.RFXComDeviceConfiguration;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComInvalidStateException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedChannelException;
import org.openhab.binding.rfxcom.internal.handler.DeviceState;
import org.openhab.core.library.types.DecimalType;
@ -36,13 +38,14 @@ abstract class RFXComBatteryDeviceMessage<T> extends RFXComDeviceMessageImpl<T>
}
@Override
public State convertToState(String channelId, DeviceState deviceState) throws RFXComUnsupportedChannelException {
public State convertToState(String channelId, RFXComDeviceConfiguration config, DeviceState deviceState)
throws RFXComUnsupportedChannelException, RFXComInvalidStateException {
switch (channelId) {
case CHANNEL_BATTERY_LEVEL:
return convertBatteryLevelToSystemWideLevel(batteryLevel);
default:
return super.convertToState(channelId, deviceState);
return super.convertToState(channelId, config, deviceState);
}
}

@ -15,7 +15,9 @@ package org.openhab.binding.rfxcom.internal.messages;
import static org.openhab.binding.rfxcom.internal.RFXComBindingConstants.*;
import static org.openhab.binding.rfxcom.internal.messages.ByteEnumUtil.fromByte;
import org.openhab.binding.rfxcom.internal.config.RFXComDeviceConfiguration;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComInvalidStateException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedChannelException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedValueException;
import org.openhab.binding.rfxcom.internal.handler.DeviceState;
@ -174,11 +176,12 @@ public class RFXComBlinds1Message extends RFXComBatteryDeviceMessage<RFXComBlind
}
@Override
public State convertToState(String channelId, DeviceState deviceState) throws RFXComUnsupportedChannelException {
public State convertToState(String channelId, RFXComDeviceConfiguration config, DeviceState deviceState)
throws RFXComUnsupportedChannelException, RFXComInvalidStateException {
if (CHANNEL_COMMAND.equals(channelId)) {
return (command == Commands.CLOSE ? OpenClosedType.CLOSED : OpenClosedType.OPEN);
} else {
return super.convertToState(channelId, deviceState);
return super.convertToState(channelId, config, deviceState);
}
}

@ -15,7 +15,9 @@ package org.openhab.binding.rfxcom.internal.messages;
import static org.openhab.binding.rfxcom.internal.RFXComBindingConstants.CHANNEL_CHIME_SOUND;
import static org.openhab.binding.rfxcom.internal.messages.ByteEnumUtil.fromByte;
import org.openhab.binding.rfxcom.internal.config.RFXComDeviceConfiguration;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComInvalidStateException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedChannelException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedValueException;
import org.openhab.binding.rfxcom.internal.handler.DeviceState;
@ -133,11 +135,12 @@ public class RFXComChimeMessage extends RFXComDeviceMessageImpl<RFXComChimeMessa
}
@Override
public State convertToState(String channelId, DeviceState deviceState) throws RFXComUnsupportedChannelException {
public State convertToState(String channelId, RFXComDeviceConfiguration config, DeviceState deviceState)
throws RFXComUnsupportedChannelException, RFXComInvalidStateException {
if (CHANNEL_CHIME_SOUND.equals(channelId)) {
return new DecimalType(chimeSound);
} else {
return super.convertToState(channelId, deviceState);
return super.convertToState(channelId, config, deviceState);
}
}

@ -15,7 +15,9 @@ package org.openhab.binding.rfxcom.internal.messages;
import static org.openhab.binding.rfxcom.internal.RFXComBindingConstants.*;
import static org.openhab.binding.rfxcom.internal.messages.ByteEnumUtil.fromByte;
import org.openhab.binding.rfxcom.internal.config.RFXComDeviceConfiguration;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComInvalidStateException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedChannelException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedValueException;
import org.openhab.binding.rfxcom.internal.handler.DeviceState;
@ -136,7 +138,8 @@ public class RFXComCurrentEnergyMessage extends RFXComBatteryDeviceMessage<RFXCo
}
@Override
public State convertToState(String channelId, DeviceState deviceState) throws RFXComUnsupportedChannelException {
public State convertToState(String channelId, RFXComDeviceConfiguration config, DeviceState deviceState)
throws RFXComUnsupportedChannelException, RFXComInvalidStateException {
switch (channelId) {
case CHANNEL_CHANNEL1_AMPS:
return new DecimalType(channel1Amps);
@ -151,7 +154,7 @@ public class RFXComCurrentEnergyMessage extends RFXComBatteryDeviceMessage<RFXCo
return new DecimalType(totalUsage);
default:
return super.convertToState(channelId, deviceState);
return super.convertToState(channelId, config, deviceState);
}
}

@ -15,7 +15,9 @@ package org.openhab.binding.rfxcom.internal.messages;
import static org.openhab.binding.rfxcom.internal.RFXComBindingConstants.*;
import static org.openhab.binding.rfxcom.internal.messages.ByteEnumUtil.fromByte;
import org.openhab.binding.rfxcom.internal.config.RFXComDeviceConfiguration;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComInvalidStateException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedChannelException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedValueException;
import org.openhab.binding.rfxcom.internal.handler.DeviceState;
@ -125,7 +127,8 @@ public class RFXComCurrentMessage extends RFXComBatteryDeviceMessage<RFXComCurre
}
@Override
public State convertToState(String channelId, DeviceState deviceState) throws RFXComUnsupportedChannelException {
public State convertToState(String channelId, RFXComDeviceConfiguration config, DeviceState deviceState)
throws RFXComUnsupportedChannelException, RFXComInvalidStateException {
switch (channelId) {
case CHANNEL_CHANNEL1_AMPS:
return new DecimalType(channel1Amps);
@ -137,7 +140,7 @@ public class RFXComCurrentMessage extends RFXComBatteryDeviceMessage<RFXComCurre
return new DecimalType(channel3Amps);
default:
return super.convertToState(channelId, deviceState);
return super.convertToState(channelId, config, deviceState);
}
}

@ -15,7 +15,9 @@ package org.openhab.binding.rfxcom.internal.messages;
import static org.openhab.binding.rfxcom.internal.RFXComBindingConstants.*;
import static org.openhab.binding.rfxcom.internal.messages.ByteEnumUtil.fromByte;
import org.openhab.binding.rfxcom.internal.config.RFXComDeviceConfiguration;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComInvalidStateException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedChannelException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedValueException;
import org.openhab.binding.rfxcom.internal.handler.DeviceState;
@ -131,11 +133,12 @@ public class RFXComCurtain1Message extends RFXComBatteryDeviceMessage<RFXComCurt
}
@Override
public State convertToState(String channelId, DeviceState deviceState) throws RFXComUnsupportedChannelException {
public State convertToState(String channelId, RFXComDeviceConfiguration config, DeviceState deviceState)
throws RFXComUnsupportedChannelException, RFXComInvalidStateException {
if (channelId.equals(CHANNEL_COMMAND)) {
return (command == Commands.CLOSE ? OpenClosedType.CLOSED : OpenClosedType.OPEN);
} else {
return super.convertToState(channelId, deviceState);
return super.convertToState(channelId, config, deviceState);
}
}

@ -15,7 +15,9 @@ package org.openhab.binding.rfxcom.internal.messages;
import static org.openhab.binding.rfxcom.internal.RFXComBindingConstants.CHANNEL_DATE_TIME;
import static org.openhab.binding.rfxcom.internal.messages.ByteEnumUtil.fromByte;
import org.openhab.binding.rfxcom.internal.config.RFXComDeviceConfiguration;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComInvalidStateException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedChannelException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedValueException;
import org.openhab.binding.rfxcom.internal.handler.DeviceState;
@ -122,11 +124,12 @@ public class RFXComDateTimeMessage extends RFXComBatteryDeviceMessage<RFXComDate
}
@Override
public State convertToState(String channelId, DeviceState deviceState) throws RFXComUnsupportedChannelException {
public State convertToState(String channelId, RFXComDeviceConfiguration config, DeviceState deviceState)
throws RFXComUnsupportedChannelException, RFXComInvalidStateException {
if (channelId.equals(CHANNEL_DATE_TIME)) {
return new DateTimeType(dateTime);
} else {
return super.convertToState(channelId, deviceState);
return super.convertToState(channelId, config, deviceState);
}
}

@ -14,6 +14,7 @@ package org.openhab.binding.rfxcom.internal.messages;
import org.openhab.binding.rfxcom.internal.config.RFXComDeviceConfiguration;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComInvalidStateException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedChannelException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedValueException;
import org.openhab.binding.rfxcom.internal.handler.DeviceState;
@ -31,21 +32,27 @@ public interface RFXComDeviceMessage<T> extends RFXComMessage {
* Procedure for converting RFXCOM value to openHAB command.
*
* @param channelId id of the channel
* @param config Configuration of the thing being handled
* @param deviceState
* @return openHAB command.
* @throws RFXComUnsupportedChannelException if the channel is not supported
* @throws RFXComInvalidStateException if the channel is supported, but the device is not configured for the value
*/
Command convertToCommand(String channelId, DeviceState deviceState) throws RFXComUnsupportedChannelException;
Command convertToCommand(String channelId, RFXComDeviceConfiguration config, DeviceState deviceState)
throws RFXComUnsupportedChannelException, RFXComInvalidStateException;
/**
* Procedure for converting RFXCOM value to openHAB state.
*
* @param channelId id of the channel
* @param config configuration of the thing being handled
* @param deviceState
* @return openHAB state.
* @throws RFXComUnsupportedChannelException if the channel is not supported
* @throws RFXComInvalidStateException if the channel is supported, but the device is not configured for the value
*/
State convertToState(String channelId, DeviceState deviceState) throws RFXComUnsupportedChannelException;
State convertToState(String channelId, RFXComDeviceConfiguration config, DeviceState deviceState)
throws RFXComUnsupportedChannelException, RFXComInvalidStateException;
/**
* Procedure to get device id.

@ -17,6 +17,7 @@ import static org.openhab.binding.rfxcom.internal.RFXComBindingConstants.CHANNEL
import org.openhab.binding.rfxcom.internal.config.RFXComDeviceConfiguration;
import org.openhab.binding.rfxcom.internal.config.RFXComGenericDeviceConfiguration;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComInvalidStateException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedChannelException;
import org.openhab.binding.rfxcom.internal.handler.DeviceState;
import org.openhab.core.config.discovery.DiscoveryResultBuilder;
@ -48,13 +49,14 @@ abstract class RFXComDeviceMessageImpl<T> extends RFXComBaseMessage implements R
}
@Override
public Command convertToCommand(String channelId, DeviceState deviceState)
throws RFXComUnsupportedChannelException {
return (Command) convertToState(channelId, deviceState);
public Command convertToCommand(String channelId, RFXComDeviceConfiguration config, DeviceState deviceState)
throws RFXComUnsupportedChannelException, RFXComInvalidStateException {
return (Command) convertToState(channelId, config, deviceState);
}
@Override
public State convertToState(String channelId, DeviceState deviceState) throws RFXComUnsupportedChannelException {
public State convertToState(String channelId, RFXComDeviceConfiguration config, DeviceState deviceState)
throws RFXComUnsupportedChannelException, RFXComInvalidStateException {
switch (channelId) {
case CHANNEL_SIGNAL_LEVEL:
return convertSignalLevelToSystemWideLevel(signalLevel);

@ -15,7 +15,9 @@ package org.openhab.binding.rfxcom.internal.messages;
import static org.openhab.binding.rfxcom.internal.RFXComBindingConstants.*;
import static org.openhab.binding.rfxcom.internal.messages.ByteEnumUtil.fromByte;
import org.openhab.binding.rfxcom.internal.config.RFXComDeviceConfiguration;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComInvalidStateException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedChannelException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedValueException;
import org.openhab.binding.rfxcom.internal.handler.DeviceState;
@ -145,7 +147,8 @@ public class RFXComEnergyMessage extends RFXComBatteryDeviceMessage<RFXComEnergy
}
@Override
public State convertToState(String channelId, DeviceState deviceState) throws RFXComUnsupportedChannelException {
public State convertToState(String channelId, RFXComDeviceConfiguration config, DeviceState deviceState)
throws RFXComUnsupportedChannelException, RFXComInvalidStateException {
switch (channelId) {
case CHANNEL_INSTANT_POWER:
return new DecimalType(instantPower);
@ -160,7 +163,7 @@ public class RFXComEnergyMessage extends RFXComBatteryDeviceMessage<RFXComEnergy
return new DecimalType(totalAmpHour);
default:
return super.convertToState(channelId, deviceState);
return super.convertToState(channelId, config, deviceState);
}
}

@ -21,7 +21,9 @@ import java.util.Arrays;
import java.util.List;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.rfxcom.internal.config.RFXComDeviceConfiguration;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComInvalidStateException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedChannelException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedValueException;
import org.openhab.binding.rfxcom.internal.handler.DeviceState;
@ -263,7 +265,8 @@ public class RFXComFanMessage extends RFXComDeviceMessageImpl<RFXComFanMessage.S
}
@Override
public State convertToState(String channelId, DeviceState deviceState) throws RFXComUnsupportedChannelException {
public State convertToState(String channelId, RFXComDeviceConfiguration config, DeviceState deviceState)
throws RFXComUnsupportedChannelException, RFXComInvalidStateException {
switch (channelId) {
case CHANNEL_FAN_LIGHT:
return handleLightChannel();
@ -278,7 +281,7 @@ public class RFXComFanMessage extends RFXComDeviceMessageImpl<RFXComFanMessage.S
return handleCommandStringChannel();
default:
return super.convertToState(channelId, deviceState);
return super.convertToState(channelId, config, deviceState);
}
}

@ -15,7 +15,9 @@ package org.openhab.binding.rfxcom.internal.messages;
import static org.openhab.binding.rfxcom.internal.RFXComBindingConstants.CHANNEL_COMMAND;
import static org.openhab.binding.rfxcom.internal.messages.ByteEnumUtil.fromByte;
import org.openhab.binding.rfxcom.internal.config.RFXComDeviceConfiguration;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComInvalidStateException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedChannelException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedValueException;
import org.openhab.binding.rfxcom.internal.handler.DeviceState;
@ -125,11 +127,12 @@ public class RFXComHomeConfortMessage extends RFXComDeviceMessageImpl<RFXComHome
}
@Override
public State convertToState(String channelId, DeviceState deviceState) throws RFXComUnsupportedChannelException {
public State convertToState(String channelId, RFXComDeviceConfiguration config, DeviceState deviceState)
throws RFXComUnsupportedChannelException, RFXComInvalidStateException {
if (channelId.equals(CHANNEL_COMMAND)) {
return (command == Commands.OFF || command == Commands.GROUP_OFF ? OnOffType.OFF : OnOffType.ON);
} else {
return super.convertToState(channelId, deviceState);
return super.convertToState(channelId, config, deviceState);
}
}

@ -15,7 +15,9 @@ package org.openhab.binding.rfxcom.internal.messages;
import static org.openhab.binding.rfxcom.internal.RFXComBindingConstants.*;
import static org.openhab.binding.rfxcom.internal.messages.ByteEnumUtil.fromByte;
import org.openhab.binding.rfxcom.internal.config.RFXComDeviceConfiguration;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComInvalidStateException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedChannelException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedValueException;
import org.openhab.binding.rfxcom.internal.handler.DeviceState;
@ -129,7 +131,8 @@ public class RFXComHumidityMessage extends RFXComBatteryDeviceMessage<RFXComHumi
}
@Override
public State convertToState(String channelId, DeviceState deviceState) throws RFXComUnsupportedChannelException {
public State convertToState(String channelId, RFXComDeviceConfiguration config, DeviceState deviceState)
throws RFXComUnsupportedChannelException, RFXComInvalidStateException {
switch (channelId) {
case CHANNEL_HUMIDITY:
return new DecimalType(humidity);
@ -138,7 +141,7 @@ public class RFXComHumidityMessage extends RFXComBatteryDeviceMessage<RFXComHumi
return new StringType(humidityStatus.toString());
default:
return super.convertToState(channelId, deviceState);
return super.convertToState(channelId, config, deviceState);
}
}

@ -271,7 +271,7 @@ public class RFXComInterfaceMessage extends RFXComBaseMessage {
transceiverType = TransceiverType._UNKNOWN;
logger.warn(
"The transceiver type reported ({}) isn't known to the RFXCom binding. Please raise an issue at https://github.com/openhab/openhab-addons/ to have it included.",
data[5]);
Byte.toUnsignedInt(data[5]));
}
hardwareVersion1 = data[11];

@ -14,7 +14,9 @@ package org.openhab.binding.rfxcom.internal.messages;
import static org.openhab.binding.rfxcom.internal.RFXComBindingConstants.*;
import org.openhab.binding.rfxcom.internal.config.RFXComDeviceConfiguration;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComInvalidStateException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedChannelException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedValueException;
import org.openhab.binding.rfxcom.internal.handler.DeviceState;
@ -126,11 +128,11 @@ public class RFXComLighting1Message extends RFXComDeviceMessageImpl<RFXComLighti
// the message unless the last X<n> ON they saw was for them. So we
// redirect an incoming broadcast DIM/BRIGHT to the correct item
// based on the last X<n> we saw or sent.
unitCode = lastUnit[(int) houseCode - (int) 'A'];
unitCode = lastUnit[houseCode - 'A'];
} else {
unitCode = data[5];
if (command == Commands.ON) {
lastUnit[(int) houseCode - (int) 'A'] = unitCode;
lastUnit[houseCode - 'A'] = unitCode;
}
}
@ -162,8 +164,8 @@ public class RFXComLighting1Message extends RFXComDeviceMessageImpl<RFXComLighti
}
@Override
public Command convertToCommand(String channelId, DeviceState deviceState)
throws RFXComUnsupportedChannelException {
public Command convertToCommand(String channelId, RFXComDeviceConfiguration config, DeviceState deviceState)
throws RFXComUnsupportedChannelException, RFXComInvalidStateException {
switch (channelId) {
case CHANNEL_COMMAND:
switch (command) {
@ -190,11 +192,13 @@ public class RFXComLighting1Message extends RFXComDeviceMessageImpl<RFXComLighti
}
default:
return super.convertToCommand(channelId, deviceState);
return super.convertToCommand(channelId, config, deviceState);
}
}
public State convertToState(String channelId, DeviceState deviceState) throws RFXComUnsupportedChannelException {
@Override
public State convertToState(String channelId, RFXComDeviceConfiguration config, DeviceState deviceState)
throws RFXComUnsupportedChannelException {
switch (channelId) {
case CHANNEL_COMMAND:
switch (command) {

@ -20,7 +20,9 @@ import static org.openhab.binding.rfxcom.internal.messages.RFXComLighting2Messag
import java.math.BigDecimal;
import java.math.RoundingMode;
import org.openhab.binding.rfxcom.internal.config.RFXComDeviceConfiguration;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComInvalidStateException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedChannelException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedValueException;
import org.openhab.binding.rfxcom.internal.handler.DeviceState;
@ -173,7 +175,8 @@ public class RFXComLighting2Message extends RFXComDeviceMessageImpl<RFXComLighti
}
@Override
public State convertToState(String channelId, DeviceState deviceState) throws RFXComUnsupportedChannelException {
public State convertToState(String channelId, RFXComDeviceConfiguration config, DeviceState deviceState)
throws RFXComUnsupportedChannelException, RFXComInvalidStateException {
switch (channelId) {
case CHANNEL_DIMMING_LEVEL:
return RFXComLighting2Message.getPercentTypeFromDimLevel(dimmingLevel);
@ -211,7 +214,7 @@ public class RFXComLighting2Message extends RFXComDeviceMessageImpl<RFXComLighti
}
default:
return super.convertToState(channelId, deviceState);
return super.convertToState(channelId, config, deviceState);
}
}

@ -13,12 +13,17 @@
package org.openhab.binding.rfxcom.internal.messages;
import static org.openhab.binding.rfxcom.internal.RFXComBindingConstants.*;
import static org.openhab.binding.rfxcom.internal.config.RFXComLighting4DeviceConfiguration.*;
import static org.openhab.binding.rfxcom.internal.messages.ByteEnumUtil.fromByte;
import static org.openhab.binding.rfxcom.internal.config.RFXComLighting4DeviceConfiguration.PULSE_LABEL;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.openhab.binding.rfxcom.internal.config.RFXComDeviceConfiguration;
import org.openhab.binding.rfxcom.internal.config.RFXComLighting4DeviceConfiguration;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComInvalidStateException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedChannelException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedValueException;
import org.openhab.binding.rfxcom.internal.handler.DeviceState;
@ -28,8 +33,6 @@ import org.openhab.core.library.types.OnOffType;
import org.openhab.core.library.types.OpenClosedType;
import org.openhab.core.types.State;
import org.openhab.core.types.Type;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* RFXCOM data class for lighting4 message.
@ -57,14 +60,10 @@ import org.slf4j.LoggerFactory;
* @author Alessandro Ballini (ITA) - Initial contribution
* @author Pauli Anttila - Migrated to OH2
* @author Martin van Wingerden - Extended support for more complex PT2262 devices
* @author James Hewitt - Use the thing config to identify what incoming commandIds map to
* @author James Hewitt - Deprecate using previously discovered commandIds because they are unreliable
*/
public class RFXComLighting4Message extends RFXComDeviceMessageImpl<RFXComLighting4Message.SubType> {
// this logger is used from a static context, so is static as well
private static final Logger LOGGER = LoggerFactory.getLogger(RFXComLighting4Message.class);
private static final byte DEFAULT_OFF_COMMAND_ID = Commands.OFF_4.toByte();
private static final byte DEFAULT_ON_COMMAND_ID = Commands.ON_1.toByte();
public enum SubType implements ByteEnumWrapper {
PT2262(0);
@ -80,62 +79,21 @@ public class RFXComLighting4Message extends RFXComDeviceMessageImpl<RFXComLighti
}
}
public enum Commands implements ByteEnumWrapper {
OFF_0(0, false),
ON_1(1, true),
OFF_2(2, false),
ON_3(3, true),
OFF_4(4, false),
ON_5(5, true),
ON_6(6, true),
ON_7(7, true),
ON_8(8, true),
ON_9(9, true),
ON_10(10, true),
ON_11(11, true),
ON_12(12, true),
OFF_14(14, false),
ON_15(15, true),
UNKNOWN(-1, false);
private final int command;
private final boolean on;
Commands(int command, boolean on) {
this.command = command;
this.on = on;
}
@Override
public byte toByte() {
return (byte) command;
}
public boolean isOn() {
return on;
}
public static Commands fromByte(int input) {
for (Commands c : Commands.values()) {
if (c.command == input) {
return c;
}
}
LOGGER.info(
"A not completely supported command with value {} was received, we can send it but please report "
+ "it as an issue including what the command means, this helps to extend the binding with better support.",
input);
return UNKNOWN;
}
}
// These are historical behaviour, are deprecated, and will be removed in a future openHAB release.
@Deprecated
private static final byte DEFAULT_OFF_COMMAND_ID = 4;
@Deprecated
private static final byte DEFAULT_ON_COMMAND_ID = 1;
@Deprecated
private Set<Integer> ON_COMMAND_IDS = Stream.of(1, 3, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15)
.collect(Collectors.toCollection(HashSet::new));
private SubType subType;
private int sensorId;
private int pulse;
private Commands command;
private int commandId;
private int offCommandId;
private int onCommandId;
private RFXComLighting4DeviceConfiguration config;
public RFXComLighting4Message() {
super(PacketType.LIGHTING4);
@ -152,7 +110,7 @@ public class RFXComLighting4Message extends RFXComDeviceMessageImpl<RFXComLighti
str += super.toString();
str += ", Sub type = " + subType;
str += ", Device Id = " + getDeviceId();
str += ", Command = " + command + "(" + commandId + ")";
str += ", Command Id = " + commandId;
str += ", Pulse = " + pulse;
return str;
@ -162,13 +120,10 @@ public class RFXComLighting4Message extends RFXComDeviceMessageImpl<RFXComLighti
public void encodeMessage(byte[] data) throws RFXComException {
super.encodeMessage(data);
subType = fromByte(SubType.class, super.subType);
subType = ByteEnumUtil.fromByte(SubType.class, super.subType);
sensorId = (data[4] & 0xFF) << 12 | (data[5] & 0xFF) << 4 | (data[6] & 0xF0) >> 4;
commandId = (data[6] & 0x0F);
command = Commands.fromByte(commandId);
onCommandId = command.isOn() ? commandId : DEFAULT_ON_COMMAND_ID;
offCommandId = command.isOn() ? DEFAULT_OFF_COMMAND_ID : commandId;
pulse = (data[7] & 0xFF) << 8 | (data[8] & 0xFF);
@ -205,20 +160,45 @@ public class RFXComLighting4Message extends RFXComDeviceMessageImpl<RFXComLighti
}
@Override
public State convertToState(String channelId, DeviceState deviceState) throws RFXComUnsupportedChannelException {
public State convertToState(String channelId, RFXComDeviceConfiguration configuration, DeviceState deviceState)
throws RFXComUnsupportedChannelException, RFXComInvalidStateException {
RFXComLighting4DeviceConfiguration config = (RFXComLighting4DeviceConfiguration) configuration;
switch (channelId) {
case CHANNEL_COMMAND:
case CHANNEL_MOTION:
return command.isOn() ? OnOffType.ON : OnOffType.OFF;
if (config.onCommandId != null && commandId == config.onCommandId) {
return OnOffType.ON;
}
if (config.offCommandId != null && commandId == config.offCommandId) {
return OnOffType.OFF;
}
// Deprecated if statement - to be removed in a future release
if (config.onCommandId == null && config.offCommandId == null) {
return ON_COMMAND_IDS.contains(commandId) ? OnOffType.ON : OnOffType.OFF;
}
throw new RFXComInvalidStateException(channelId, Integer.toString(commandId),
"Device not configured for received commandId");
case CHANNEL_CONTACT:
return command.isOn() ? OpenClosedType.OPEN : OpenClosedType.CLOSED;
if (config.openCommandId != null && commandId == config.openCommandId) {
return OpenClosedType.OPEN;
}
if (config.closedCommandId != null && commandId == config.closedCommandId) {
return OpenClosedType.CLOSED;
}
// Deprecated if statement - to be removed in a future release
if (config.onCommandId == null && config.offCommandId == null) {
return ON_COMMAND_IDS.contains(commandId) ? OpenClosedType.OPEN : OpenClosedType.CLOSED;
}
throw new RFXComInvalidStateException(channelId, Integer.toString(commandId),
"Device not configured for received commandId");
case CHANNEL_COMMAND_ID:
return new DecimalType(commandId);
default:
return super.convertToState(channelId, deviceState);
return super.convertToState(channelId, config, deviceState);
}
}
@ -233,30 +213,70 @@ public class RFXComLighting4Message extends RFXComDeviceMessageImpl<RFXComLighti
}
@Override
public void convertFromState(String channelId, Type type) throws RFXComUnsupportedChannelException {
public void convertFromState(String channelId, Type type)
throws RFXComUnsupportedChannelException, RFXComInvalidStateException {
switch (channelId) {
case CHANNEL_COMMAND:
if (type instanceof OnOffType) {
command = Commands.fromByte(type == OnOffType.ON ? onCommandId : offCommandId);
commandId = command.toByte();
if (type == OnOffType.ON) {
if (config.onCommandId != null) {
commandId = config.onCommandId;
} else {
// Deprecated - to throw RFXComInvalidStateException in a future release, see contact
// channel
commandId = DEFAULT_ON_COMMAND_ID;
}
}
if (type == OnOffType.OFF) {
if (config.offCommandId != null) {
commandId = config.offCommandId;
} else {
// Deprecated - to throw RFXComInvalidStateException in a future release, see contact
// channel
commandId = DEFAULT_OFF_COMMAND_ID;
}
}
} else {
throw new RFXComUnsupportedChannelException("Channel " + channelId + " does not accept " + type);
throw new RFXComInvalidStateException(channelId, type.toString(),
"Channel only supports OnOffType");
}
break;
case CHANNEL_CONTACT:
if (type instanceof OpenClosedType) {
if (type == OpenClosedType.OPEN) {
if (config.openCommandId != null) {
commandId = config.openCommandId;
} else {
throw new RFXComInvalidStateException(channelId, type.toString(),
"openCommandId not configured for this device");
}
}
if (type == OpenClosedType.CLOSED) {
if (config.closedCommandId != null) {
commandId = config.closedCommandId;
} else {
throw new RFXComInvalidStateException(channelId, type.toString(),
"closedCommandId not configured for this device");
}
}
} else {
throw new RFXComInvalidStateException(channelId, type.toString(),
"Channel only supports OpenClosedType");
}
break;
case CHANNEL_COMMAND_ID:
if (type instanceof DecimalType) {
commandId = ((DecimalType) type).toBigDecimal().byteValue();
command = Commands.fromByte(commandId);
commandId = (byte) ((DecimalType) type).intValue();
} else {
throw new RFXComUnsupportedChannelException("Channel " + channelId + " does not accept " + type);
throw new RFXComInvalidStateException(channelId, type.toString(),
"Channel only supports DecimalType");
}
break;
default:
throw new RFXComUnsupportedChannelException("Channel " + channelId + " is not relevant here");
throw new RFXComUnsupportedChannelException("Channel " + channelId + " is not supported by Lighting4");
}
}
@ -269,23 +289,12 @@ public class RFXComLighting4Message extends RFXComDeviceMessageImpl<RFXComLighti
public void addDevicePropertiesTo(DiscoveryResultBuilder discoveryResultBuilder) throws RFXComException {
super.addDevicePropertiesTo(discoveryResultBuilder);
discoveryResultBuilder.withProperty(PULSE_LABEL, pulse);
discoveryResultBuilder.withProperty(ON_COMMAND_ID_LABEL, onCommandId);
discoveryResultBuilder.withProperty(OFF_COMMAND_ID_LABEL, offCommandId);
}
@Override
public void setConfig(RFXComDeviceConfiguration config) throws RFXComException {
RFXComLighting4DeviceConfiguration lighting4Config = (RFXComLighting4DeviceConfiguration) config;
super.setConfig(lighting4Config);
this.pulse = lighting4Config.pulse != null ? lighting4Config.pulse : 350;
this.onCommandId = valueOrDefault(lighting4Config.onCommandId, DEFAULT_ON_COMMAND_ID);
this.offCommandId = valueOrDefault(lighting4Config.offCommandId, DEFAULT_OFF_COMMAND_ID);
}
private int valueOrDefault(Integer commandId, byte defaultValue) {
if (commandId != null) {
return commandId;
}
return defaultValue;
super.setConfig(config);
this.config = (RFXComLighting4DeviceConfiguration) config;
this.pulse = this.config.pulse != null ? this.config.pulse : 350;
}
}

@ -21,7 +21,9 @@ import java.math.RoundingMode;
import java.util.Arrays;
import java.util.List;
import org.openhab.binding.rfxcom.internal.config.RFXComDeviceConfiguration;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComInvalidStateException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedChannelException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedValueException;
import org.openhab.binding.rfxcom.internal.handler.DeviceState;
@ -234,7 +236,8 @@ public class RFXComLighting5Message extends RFXComDeviceMessageImpl<RFXComLighti
}
@Override
public State convertToState(String channelId, DeviceState deviceState) throws RFXComUnsupportedChannelException {
public State convertToState(String channelId, RFXComDeviceConfiguration config, DeviceState deviceState)
throws RFXComUnsupportedChannelException, RFXComInvalidStateException {
switch (channelId) {
case CHANNEL_MOOD:
switch (command) {
@ -292,7 +295,7 @@ public class RFXComLighting5Message extends RFXComDeviceMessageImpl<RFXComLighti
}
default:
return super.convertToState(channelId, deviceState);
return super.convertToState(channelId, config, deviceState);
}
}

@ -15,7 +15,9 @@ package org.openhab.binding.rfxcom.internal.messages;
import static org.openhab.binding.rfxcom.internal.RFXComBindingConstants.*;
import static org.openhab.binding.rfxcom.internal.messages.ByteEnumUtil.fromByte;
import org.openhab.binding.rfxcom.internal.config.RFXComDeviceConfiguration;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComInvalidStateException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedChannelException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedValueException;
import org.openhab.binding.rfxcom.internal.handler.DeviceState;
@ -134,7 +136,8 @@ public class RFXComLighting6Message extends RFXComDeviceMessageImpl<RFXComLighti
}
@Override
public State convertToState(String channelId, DeviceState deviceState) throws RFXComUnsupportedChannelException {
public State convertToState(String channelId, RFXComDeviceConfiguration config, DeviceState deviceState)
throws RFXComUnsupportedChannelException, RFXComInvalidStateException {
switch (channelId) {
case CHANNEL_COMMAND:
switch (command) {
@ -165,7 +168,7 @@ public class RFXComLighting6Message extends RFXComDeviceMessageImpl<RFXComLighti
}
default:
return super.convertToState(channelId, deviceState);
return super.convertToState(channelId, config, deviceState);
}
}

@ -12,8 +12,11 @@
*/
package org.openhab.binding.rfxcom.internal.messages;
import org.openhab.binding.rfxcom.internal.config.RFXComDeviceConfiguration;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
import org.openhab.binding.rfxcom.internal.messages.RFXComBaseMessage.PacketType;
import org.openhab.core.thing.ChannelUID;
import org.openhab.core.types.Command;
/**
* Factory to create RFXCom messages from either bytes delivered by the RFXCom device
@ -21,9 +24,11 @@ import org.openhab.binding.rfxcom.internal.messages.RFXComBaseMessage.PacketType
*
* @author Pauli Anttila - Initial contribution
* @author James Hewitt-Thomas - Convert to interface to allow dependency injection
* @author James Hewitt-Thomas - Switch to making messages for a specific command
*/
public interface RFXComMessageFactory {
public RFXComMessage createMessage(PacketType packetType) throws RFXComException;
public RFXComMessage createMessage(PacketType packetType, RFXComDeviceConfiguration config, ChannelUID channelUID,
Command command) throws RFXComException;
public RFXComMessage createMessage(byte[] packet) throws RFXComException;
}

@ -18,9 +18,12 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.openhab.binding.rfxcom.internal.config.RFXComDeviceConfiguration;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComMessageNotImplementedException;
import org.openhab.binding.rfxcom.internal.messages.RFXComBaseMessage.PacketType;
import org.openhab.core.thing.ChannelUID;
import org.openhab.core.types.Command;
/**
* Factory to create RFXCom messages from either bytes delivered by the RFXCom device
@ -98,13 +101,17 @@ public enum RFXComMessageFactoryImpl implements RFXComMessageFactory {
* Create message for transmission from the packet type associated with the thing.
*/
@Override
public RFXComMessage createMessage(PacketType packetType) throws RFXComException {
public RFXComMessage createMessage(PacketType packetType, RFXComDeviceConfiguration config, ChannelUID channelUID,
Command command) throws RFXComException {
try {
Class<? extends RFXComMessage> cl = MESSAGE_CLASSES.get(packetType);
if (cl == null) {
throw new RFXComMessageNotImplementedException("Message " + packetType + " not implemented");
}
return cl.getDeclaredConstructor().newInstance();
RFXComMessage msg = cl.getDeclaredConstructor().newInstance();
msg.setConfig(config);
msg.convertFromState(channelUID.getId(), command);
return msg;
} catch (ReflectiveOperationException e) {
throw new RFXComException(e);
}

@ -18,7 +18,9 @@ import static org.openhab.binding.rfxcom.internal.messages.ByteEnumUtil.fromByte
import java.math.BigDecimal;
import org.openhab.binding.rfxcom.internal.config.RFXComDeviceConfiguration;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComInvalidStateException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedChannelException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedValueException;
import org.openhab.binding.rfxcom.internal.handler.DeviceState;
@ -201,7 +203,8 @@ public class RFXComRFXSensorMessage extends RFXComDeviceMessageImpl<RFXComRFXSen
}
@Override
public State convertToState(String channelId, DeviceState deviceState) throws RFXComUnsupportedChannelException {
public State convertToState(String channelId, RFXComDeviceConfiguration config, DeviceState deviceState)
throws RFXComUnsupportedChannelException, RFXComInvalidStateException {
switch (channelId) {
case CHANNEL_TEMPERATURE:
return subType == SubType.TEMPERATURE ? getTemperature() : null;
@ -219,7 +222,7 @@ public class RFXComRFXSensorMessage extends RFXComDeviceMessageImpl<RFXComRFXSen
return subType == SubType.A_D ? handlePressure(deviceState) : null;
default:
return super.convertToState(channelId, deviceState);
return super.convertToState(channelId, config, deviceState);
}
}

@ -15,7 +15,9 @@ package org.openhab.binding.rfxcom.internal.messages;
import static org.openhab.binding.rfxcom.internal.RFXComBindingConstants.*;
import static org.openhab.binding.rfxcom.internal.messages.ByteEnumUtil.fromByte;
import org.openhab.binding.rfxcom.internal.config.RFXComDeviceConfiguration;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComInvalidStateException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedChannelException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedValueException;
import org.openhab.binding.rfxcom.internal.handler.DeviceState;
@ -135,7 +137,8 @@ public class RFXComRainMessage extends RFXComBatteryDeviceMessage<RFXComRainMess
}
@Override
public State convertToState(String channelId, DeviceState deviceState) throws RFXComUnsupportedChannelException {
public State convertToState(String channelId, RFXComDeviceConfiguration config, DeviceState deviceState)
throws RFXComUnsupportedChannelException, RFXComInvalidStateException {
switch (channelId) {
case CHANNEL_RAIN_RATE:
return new DecimalType(rainRate);
@ -144,7 +147,7 @@ public class RFXComRainMessage extends RFXComBatteryDeviceMessage<RFXComRainMess
return new DecimalType(rainTotal);
default:
return super.convertToState(channelId, deviceState);
return super.convertToState(channelId, config, deviceState);
}
}

@ -142,7 +142,8 @@ public class RFXComRawMessage extends RFXComDeviceMessageImpl<RFXComRawMessage.S
}
@Override
public State convertToState(String channelId, DeviceState deviceState) throws RFXComUnsupportedChannelException {
public State convertToState(String channelId, RFXComDeviceConfiguration config, DeviceState deviceState)
throws RFXComUnsupportedChannelException {
switch (channelId) {
case CHANNEL_RAW_MESSAGE:
return new StringType(HexUtils.bytesToHex(rawMessage));

@ -15,7 +15,9 @@ package org.openhab.binding.rfxcom.internal.messages;
import static org.openhab.binding.rfxcom.internal.RFXComBindingConstants.*;
import static org.openhab.binding.rfxcom.internal.messages.ByteEnumUtil.fromByte;
import org.openhab.binding.rfxcom.internal.config.RFXComDeviceConfiguration;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComInvalidStateException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedChannelException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedValueException;
import org.openhab.binding.rfxcom.internal.handler.DeviceState;
@ -134,13 +136,14 @@ public class RFXComRfyMessage extends RFXComDeviceMessageImpl<RFXComRfyMessage.S
}
@Override
public State convertToState(String channelId, DeviceState deviceState) throws RFXComUnsupportedChannelException {
public State convertToState(String channelId, RFXComDeviceConfiguration config, DeviceState deviceState)
throws RFXComUnsupportedChannelException, RFXComInvalidStateException {
switch (channelId) {
case CHANNEL_COMMAND:
return (command == Commands.DOWN ? OpenClosedType.CLOSED : OpenClosedType.OPEN);
default:
return super.convertToState(channelId, deviceState);
return super.convertToState(channelId, config, deviceState);
}
}

@ -15,7 +15,9 @@ package org.openhab.binding.rfxcom.internal.messages;
import static org.openhab.binding.rfxcom.internal.RFXComBindingConstants.*;
import static org.openhab.binding.rfxcom.internal.messages.ByteEnumUtil.fromByte;
import org.openhab.binding.rfxcom.internal.config.RFXComDeviceConfiguration;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComInvalidStateException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedChannelException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedValueException;
import org.openhab.binding.rfxcom.internal.handler.DeviceState;
@ -233,7 +235,8 @@ public class RFXComSecurity1Message extends RFXComBatteryDeviceMessage<RFXComSec
}
@Override
public State convertToState(String channelId, DeviceState deviceState) throws RFXComUnsupportedChannelException {
public State convertToState(String channelId, RFXComDeviceConfiguration config, DeviceState deviceState)
throws RFXComUnsupportedChannelException, RFXComInvalidStateException {
switch (channelId) {
case CHANNEL_MOTION:
switch (status) {
@ -263,7 +266,7 @@ public class RFXComSecurity1Message extends RFXComBatteryDeviceMessage<RFXComSec
return new StringType(status.toString());
default:
return super.convertToState(channelId, deviceState);
return super.convertToState(channelId, config, deviceState);
}
}

@ -17,7 +17,9 @@ import static org.openhab.binding.rfxcom.internal.messages.ByteEnumUtil.fromByte
import java.util.Arrays;
import org.openhab.binding.rfxcom.internal.config.RFXComDeviceConfiguration;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComInvalidStateException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedChannelException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedValueException;
import org.openhab.binding.rfxcom.internal.handler.DeviceState;
@ -115,7 +117,8 @@ public class RFXComSecurity2Message extends RFXComBatteryDeviceMessage<RFXComSec
}
@Override
public State convertToState(String channelId, DeviceState deviceState) throws RFXComUnsupportedChannelException {
public State convertToState(String channelId, RFXComDeviceConfiguration config, DeviceState deviceState)
throws RFXComUnsupportedChannelException, RFXComInvalidStateException {
switch (channelId) {
case CHANNEL_CONTACT:
return ((buttonStatus & BUTTON_0_BIT) == 0) ? OpenClosedType.CLOSED : OpenClosedType.OPEN;
@ -130,7 +133,7 @@ public class RFXComSecurity2Message extends RFXComBatteryDeviceMessage<RFXComSec
return ((buttonStatus & BUTTON_3_BIT) == 0) ? OpenClosedType.CLOSED : OpenClosedType.OPEN;
default:
return super.convertToState(channelId, deviceState);
return super.convertToState(channelId, config, deviceState);
}
}

@ -15,7 +15,9 @@ package org.openhab.binding.rfxcom.internal.messages;
import static org.openhab.binding.rfxcom.internal.RFXComBindingConstants.*;
import static org.openhab.binding.rfxcom.internal.messages.ByteEnumUtil.fromByte;
import org.openhab.binding.rfxcom.internal.config.RFXComDeviceConfiguration;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComInvalidStateException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedChannelException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedValueException;
import org.openhab.binding.rfxcom.internal.handler.DeviceState;
@ -178,7 +180,8 @@ public class RFXComTemperatureHumidityBarometricMessage
}
@Override
public State convertToState(String channelId, DeviceState deviceState) throws RFXComUnsupportedChannelException {
public State convertToState(String channelId, RFXComDeviceConfiguration config, DeviceState deviceState)
throws RFXComUnsupportedChannelException, RFXComInvalidStateException {
switch (channelId) {
case CHANNEL_TEMPERATURE:
return new DecimalType(temperature);
@ -196,7 +199,7 @@ public class RFXComTemperatureHumidityBarometricMessage
return new StringType(forecastStatus.toString());
default:
return super.convertToState(channelId, deviceState);
return super.convertToState(channelId, config, deviceState);
}
}

@ -15,7 +15,9 @@ package org.openhab.binding.rfxcom.internal.messages;
import static org.openhab.binding.rfxcom.internal.RFXComBindingConstants.*;
import static org.openhab.binding.rfxcom.internal.messages.ByteEnumUtil.fromByte;
import org.openhab.binding.rfxcom.internal.config.RFXComDeviceConfiguration;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComInvalidStateException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedChannelException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedValueException;
import org.openhab.binding.rfxcom.internal.handler.DeviceState;
@ -158,7 +160,8 @@ public class RFXComTemperatureHumidityMessage
}
@Override
public State convertToState(String channelId, DeviceState deviceState) throws RFXComUnsupportedChannelException {
public State convertToState(String channelId, RFXComDeviceConfiguration config, DeviceState deviceState)
throws RFXComUnsupportedChannelException, RFXComInvalidStateException {
switch (channelId) {
case CHANNEL_TEMPERATURE:
return new DecimalType(temperature);
@ -170,7 +173,7 @@ public class RFXComTemperatureHumidityMessage
return new StringType(humidityStatus.toString());
default:
return super.convertToState(channelId, deviceState);
return super.convertToState(channelId, config, deviceState);
}
}

@ -15,7 +15,9 @@ package org.openhab.binding.rfxcom.internal.messages;
import static org.openhab.binding.rfxcom.internal.RFXComBindingConstants.CHANNEL_TEMPERATURE;
import static org.openhab.binding.rfxcom.internal.messages.ByteEnumUtil.fromByte;
import org.openhab.binding.rfxcom.internal.config.RFXComDeviceConfiguration;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComInvalidStateException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedChannelException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedValueException;
import org.openhab.binding.rfxcom.internal.handler.DeviceState;
@ -126,13 +128,14 @@ public class RFXComTemperatureMessage extends RFXComBatteryDeviceMessage<RFXComT
}
@Override
public State convertToState(String channelId, DeviceState deviceState) throws RFXComUnsupportedChannelException {
public State convertToState(String channelId, RFXComDeviceConfiguration config, DeviceState deviceState)
throws RFXComUnsupportedChannelException, RFXComInvalidStateException {
switch (channelId) {
case CHANNEL_TEMPERATURE:
return new DecimalType(temperature);
default:
return super.convertToState(channelId, deviceState);
return super.convertToState(channelId, config, deviceState);
}
}

@ -15,7 +15,9 @@ package org.openhab.binding.rfxcom.internal.messages;
import static org.openhab.binding.rfxcom.internal.RFXComBindingConstants.*;
import static org.openhab.binding.rfxcom.internal.messages.ByteEnumUtil.fromByte;
import org.openhab.binding.rfxcom.internal.config.RFXComDeviceConfiguration;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComInvalidStateException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedChannelException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedValueException;
import org.openhab.binding.rfxcom.internal.handler.DeviceState;
@ -120,7 +122,8 @@ public class RFXComTemperatureRainMessage extends RFXComBatteryDeviceMessage<RFX
}
@Override
public State convertToState(String channelId, DeviceState deviceState) throws RFXComUnsupportedChannelException {
public State convertToState(String channelId, RFXComDeviceConfiguration config, DeviceState deviceState)
throws RFXComUnsupportedChannelException, RFXComInvalidStateException {
switch (channelId) {
case CHANNEL_TEMPERATURE:
return new DecimalType(temperature);
@ -129,7 +132,7 @@ public class RFXComTemperatureRainMessage extends RFXComBatteryDeviceMessage<RFX
return new DecimalType(rainTotal);
default:
return super.convertToState(channelId, deviceState);
return super.convertToState(channelId, config, deviceState);
}
}

@ -15,7 +15,9 @@ package org.openhab.binding.rfxcom.internal.messages;
import static org.openhab.binding.rfxcom.internal.RFXComBindingConstants.*;
import static org.openhab.binding.rfxcom.internal.messages.ByteEnumUtil.fromByte;
import org.openhab.binding.rfxcom.internal.config.RFXComDeviceConfiguration;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComInvalidStateException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedChannelException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedValueException;
import org.openhab.binding.rfxcom.internal.handler.DeviceState;
@ -155,7 +157,8 @@ public class RFXComThermostat1Message extends RFXComDeviceMessageImpl<RFXComTher
}
@Override
public State convertToState(String channelId, DeviceState deviceState) throws RFXComUnsupportedChannelException {
public State convertToState(String channelId, RFXComDeviceConfiguration config, DeviceState deviceState)
throws RFXComUnsupportedChannelException, RFXComInvalidStateException {
switch (channelId) {
case CHANNEL_TEMPERATURE:
return new DecimalType(temperature);
@ -174,7 +177,7 @@ public class RFXComThermostat1Message extends RFXComDeviceMessageImpl<RFXComTher
}
default:
return super.convertToState(channelId, deviceState);
return super.convertToState(channelId, config, deviceState);
}
}

@ -19,7 +19,9 @@ import static org.openhab.binding.rfxcom.internal.messages.RFXComThermostat3Mess
import java.util.Arrays;
import java.util.List;
import org.openhab.binding.rfxcom.internal.config.RFXComDeviceConfiguration;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComInvalidStateException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedChannelException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedValueException;
import org.openhab.binding.rfxcom.internal.handler.DeviceState;
@ -152,7 +154,8 @@ public class RFXComThermostat3Message extends RFXComDeviceMessageImpl<RFXComTher
}
@Override
public State convertToState(String channelId, DeviceState deviceState) throws RFXComUnsupportedChannelException {
public State convertToState(String channelId, RFXComDeviceConfiguration config, DeviceState deviceState)
throws RFXComUnsupportedChannelException, RFXComInvalidStateException {
switch (channelId) {
case CHANNEL_COMMAND:
switch (command) {
@ -202,7 +205,7 @@ public class RFXComThermostat3Message extends RFXComDeviceMessageImpl<RFXComTher
return command == null ? UnDefType.UNDEF : StringType.valueOf(command.toString());
default:
return super.convertToState(channelId, deviceState);
return super.convertToState(channelId, config, deviceState);
}
}

@ -15,7 +15,9 @@ package org.openhab.binding.rfxcom.internal.messages;
import static org.openhab.binding.rfxcom.internal.RFXComBindingConstants.*;
import static org.openhab.binding.rfxcom.internal.messages.ByteEnumUtil.fromByte;
import org.openhab.binding.rfxcom.internal.config.RFXComDeviceConfiguration;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComInvalidStateException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedChannelException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedValueException;
import org.openhab.binding.rfxcom.internal.handler.DeviceState;
@ -125,7 +127,8 @@ public class RFXComUVMessage extends RFXComBatteryDeviceMessage<RFXComUVMessage.
}
@Override
public State convertToState(String channelId, DeviceState deviceState) throws RFXComUnsupportedChannelException {
public State convertToState(String channelId, RFXComDeviceConfiguration config, DeviceState deviceState)
throws RFXComUnsupportedChannelException, RFXComInvalidStateException {
switch (channelId) {
case CHANNEL_UV:
return new DecimalType(uv);
@ -134,7 +137,7 @@ public class RFXComUVMessage extends RFXComBatteryDeviceMessage<RFXComUVMessage.
return (subType == SubType.UV3 ? new DecimalType(temperature) : UnDefType.UNDEF);
default:
return super.convertToState(channelId, deviceState);
return super.convertToState(channelId, config, deviceState);
}
}

@ -17,6 +17,7 @@ import static org.openhab.binding.rfxcom.internal.messages.RFXComBaseMessage.Pac
import java.util.Arrays;
import org.openhab.binding.rfxcom.internal.config.RFXComDeviceConfiguration;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComMessageTooLongException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedChannelException;
@ -140,7 +141,8 @@ public class RFXComUndecodedRFMessage extends RFXComDeviceMessageImpl<RFXComUnde
}
@Override
public State convertToState(String channelId, DeviceState deviceState) throws RFXComUnsupportedChannelException {
public State convertToState(String channelId, RFXComDeviceConfiguration config, DeviceState deviceState)
throws RFXComUnsupportedChannelException {
switch (channelId) {
case CHANNEL_RAW_MESSAGE:
return new StringType(HexUtils.bytesToHex(rawMessage));

@ -15,7 +15,9 @@ package org.openhab.binding.rfxcom.internal.messages;
import static org.openhab.binding.rfxcom.internal.RFXComBindingConstants.*;
import static org.openhab.binding.rfxcom.internal.messages.ByteEnumUtil.fromByte;
import org.openhab.binding.rfxcom.internal.config.RFXComDeviceConfiguration;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComInvalidStateException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedChannelException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedValueException;
import org.openhab.binding.rfxcom.internal.handler.DeviceState;
@ -168,7 +170,8 @@ public class RFXComWindMessage extends RFXComBatteryDeviceMessage<RFXComWindMess
}
@Override
public State convertToState(String channelId, DeviceState deviceState) throws RFXComUnsupportedChannelException {
public State convertToState(String channelId, RFXComDeviceConfiguration config, DeviceState deviceState)
throws RFXComUnsupportedChannelException, RFXComInvalidStateException {
switch (channelId) {
case CHANNEL_WIND_DIRECTION:
return new DecimalType(windDirection);
@ -186,7 +189,7 @@ public class RFXComWindMessage extends RFXComBatteryDeviceMessage<RFXComWindMess
return new DecimalType(chillTemperature);
default:
return super.convertToState(channelId, deviceState);
return super.convertToState(channelId, config, deviceState);
}
}

@ -38,51 +38,31 @@
<description>Pulse length of the device</description>
<default>350</default>
</parameter>
<parameter name="onCommandId" type="integer" required="true">
<parameter name="onCommandId" type="integer" required="false" min="0" max="15">
<label>On Command</label>
<description>Specifies command to be send when ON must be transmitted</description>
<options>
<option value="0">OFF (value 0)</option>
<option value="1">ON (value 1)</option>
<option value="2">OFF (value 2)</option>
<option value="3">ON (value 3)</option>
<option value="4">OFF (value 4)</option>
<option value="5">ON (value 5)</option>
<option value="6">value 5</option>
<option value="7">ON (value 7)</option>
<option value="8">value 8</option>
<option value="9">ON (value 9)</option>
<option value="10">ON (value 10)</option>
<option value="11">ON (value 11)</option>
<option value="12">ON (value 12)</option>
<option value="13">value 13</option>
<option value="14">OFF (value 14)</option>
<option value="15">value 15</option>
</options>
<default>1</default>
<description>Specifies command that represents ON for this device. If not
specified, will treat 1, 3, 5-13 and 15 as
ON commands for receiving message, and will send 1 as an ON
command. This behaviour is deprecated. In a future
version, if this is not specified, the on command will
not be available.</description>
</parameter>
<parameter name="offCommandId" type="integer" required="true">
<parameter name="offCommandId" type="integer" required="false" min="0" max="15">
<label>Off Command</label>
<description>Specifies command to be send when OFF must be transmitted</description>
<options>
<option value="0">OFF (value 0)</option>
<option value="1">ON (value 1)</option>
<option value="2">OFF (value 2)</option>
<option value="3">ON (value 3)</option>
<option value="4">OFF (value 4)</option>
<option value="5">ON (value 5)</option>
<option value="6">value 5</option>
<option value="7">ON (value 7)</option>
<option value="8">value 8</option>
<option value="9">ON (value 9)</option>
<option value="10">ON (value 10)</option>
<option value="11">ON (value 11)</option>
<option value="12">ON (value 12)</option>
<option value="13">value 13</option>
<option value="14">OFF (value 14)</option>
<option value="15">value 15</option>
</options>
<default>4</default>
<description>Specifies command that represents OFF for this device. For historical reasons, if not
specified, will
treat 0, 2, 4 and 14 as OFF commands for receiving message, and will send 4 as an OFF
command. This behaviour is
deprecated. In a future version, if this is not specified, the off command will
not be available.</description>
</parameter>
<parameter name="openCommandId" type="integer" required="false" min="0" max="15">
<label>Open Command</label>
<description>Specifies command that represents OPEN for this device.</description>
</parameter>
<parameter name="closedCommandId" type="integer" required="false" min="0" max="15">
<label>Closed Command</label>
<description>Specifies command that represents CLOSED for this device.</description>
</parameter>
</config-description>
</thing-type>

@ -0,0 +1,54 @@
/**
* Copyright (c) 2010-2021 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.rfxcom.internal;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.binding.rfxcom.internal.config.RFXComDeviceConfiguration;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
import org.openhab.binding.rfxcom.internal.messages.MockDeviceState;
import org.openhab.binding.rfxcom.internal.messages.RFXComBaseMessage.PacketType;
import org.openhab.binding.rfxcom.internal.messages.RFXComDeviceMessage;
import org.openhab.binding.rfxcom.internal.messages.RFXComMessage;
import org.openhab.core.library.types.DecimalType;
import org.openhab.core.thing.ChannelUID;
import org.openhab.core.thing.ThingTypeUID;
import org.openhab.core.thing.ThingUID;
/**
* Helper class for testing the RFXCom-binding
*
* @author Martin van Wingerden - Initial contribution
*/
@NonNullByDefault
public class RFXComTestHelper {
static final public ThingUID bridgeUID = new ThingUID("rfxcom", "tcpbridge", "rfxtrx0");
static final public ThingUID thingUID = new ThingUID("rfxcom", bridgeUID, "mocked");
static final public ThingTypeUID thingTypeUID = new ThingTypeUID("rfxcom", "raw");
static final public ChannelUID commandChannelUID = new ChannelUID(thingUID, RFXComBindingConstants.CHANNEL_COMMAND);
static public void basicBoundaryCheck(PacketType packetType, RFXComMessage message) throws RFXComException {
// This is a place where its easy to make mistakes in coding, and can result in errors, normally
// array bounds errors
byte[] binaryMessage = message.decodeMessage();
assertEquals(binaryMessage[0], binaryMessage.length - 1, "Wrong packet length");
assertEquals(packetType.toByte(), binaryMessage[1], "Wrong packet type");
}
static public int getActualIntValue(RFXComDeviceMessage msg, RFXComDeviceConfiguration config, String channelId)
throws RFXComException {
return ((DecimalType) msg.convertToState(channelId, config, new MockDeviceState())).intValue();
}
}

@ -15,6 +15,7 @@ package org.openhab.binding.rfxcom.internal.handler;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
import static org.openhab.binding.rfxcom.internal.RFXComTestHelper.*;
import java.util.Map;
@ -41,8 +42,6 @@ import org.openhab.core.thing.Thing;
import org.openhab.core.thing.ThingStatus;
import org.openhab.core.thing.ThingStatusDetail;
import org.openhab.core.thing.ThingStatusInfo;
import org.openhab.core.thing.ThingTypeUID;
import org.openhab.core.thing.ThingUID;
import org.openhab.core.thing.binding.ThingHandlerCallback;
import org.openhab.core.types.Command;
@ -54,10 +53,6 @@ import org.openhab.core.types.Command;
@ExtendWith(MockitoExtension.class)
public class RFXComHandlerTest {
static ThingUID bridgeUID = new ThingUID("rfxcom", "tcpbridge", "rfxtrx0");
static ThingUID thingUID = new ThingUID("rfxcom", bridgeUID, "mocked");
static ThingTypeUID thingTypeUID = new ThingTypeUID("rfxcom", "raw");
@Mock
Bridge bridge;
@ -114,10 +109,10 @@ public class RFXComHandlerTest {
private RFXComGenericDeviceConfiguration sendMessageToGetConfig(String channel, Command command)
throws RFXComException {
when(messageFactory.createMessage(any(PacketType.class))).thenReturn(message);
ChannelUID cuid = new ChannelUID(thing.getUID(), channel);
handler.handleCommand(cuid, command);
verify(message).setConfig(deviceConfigurationCaptor.capture());
verify(messageFactory).createMessage(any(PacketType.class), deviceConfigurationCaptor.capture(), eq(cuid),
eq(command));
return deviceConfigurationCaptor.getValue();
}

@ -29,6 +29,6 @@ public class RFXComBarometricMessageTest {
@Test
public void checkNotImplemented() {
assertThrows(RFXComMessageNotImplementedException.class,
() -> RFXComMessageFactoryImpl.INSTANCE.createMessage(PacketType.BAROMETRIC));
() -> RFXComMessageFactoryImpl.INSTANCE.createMessage(PacketType.BAROMETRIC, null, null, null));
}
}

@ -29,6 +29,6 @@ public class RFXComCamera1MessageTest {
@Test
public void checkNotImplemented() {
assertThrows(RFXComMessageNotImplementedException.class,
() -> RFXComMessageFactoryImpl.INSTANCE.createMessage(PacketType.CAMERA1));
() -> RFXComMessageFactoryImpl.INSTANCE.createMessage(PacketType.CAMERA1, null, null, null));
}
}

@ -12,11 +12,17 @@
*/
package org.openhab.binding.rfxcom.internal.messages;
import static org.openhab.binding.rfxcom.internal.RFXComTestHelper.thingUID;
import static org.openhab.binding.rfxcom.internal.messages.RFXComBaseMessage.PacketType.CURTAIN1;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.jupiter.api.Test;
import org.openhab.binding.rfxcom.internal.RFXComBindingConstants;
import org.openhab.binding.rfxcom.internal.RFXComTestHelper;
import org.openhab.binding.rfxcom.internal.config.RFXComGenericDeviceConfiguration;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
import org.openhab.core.library.types.OpenClosedType;
import org.openhab.core.thing.ChannelUID;
/**
* Test for RFXCom-binding
@ -25,18 +31,23 @@ import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
*/
@NonNullByDefault
public class RFXComCurtain1MessageTest {
private static ChannelUID shutterChannelUID = new ChannelUID(thingUID, RFXComBindingConstants.CHANNEL_SHUTTER);
private static RFXComGenericDeviceConfiguration config = new RFXComGenericDeviceConfiguration();
static {
config.deviceId = "1.2";
config.subType = RFXComCurtain1Message.SubType.HARRISON.toString();
}
@Test
public void checkForSupportTest() throws RFXComException {
RFXComMessageFactoryImpl.INSTANCE.createMessage(CURTAIN1);
RFXComMessageFactoryImpl.INSTANCE.createMessage(CURTAIN1, config, shutterChannelUID, OpenClosedType.OPEN);
}
@Test
public void basicBoundaryCheck() throws RFXComException {
RFXComCurtain1Message message = (RFXComCurtain1Message) RFXComMessageFactoryImpl.INSTANCE
.createMessage(CURTAIN1);
message.subType = RFXComCurtain1Message.SubType.HARRISON;
message.command = RFXComCurtain1Message.Commands.OPEN;
.createMessage(CURTAIN1, config, shutterChannelUID, OpenClosedType.OPEN);
RFXComTestHelper.basicBoundaryCheck(CURTAIN1, message);
}

@ -17,6 +17,8 @@ import static org.openhab.binding.rfxcom.internal.RFXComBindingConstants.*;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.jupiter.api.Test;
import org.openhab.binding.rfxcom.internal.RFXComTestHelper;
import org.openhab.binding.rfxcom.internal.config.RFXComGenericDeviceConfiguration;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
import org.openhab.core.library.types.DateTimeType;
import org.openhab.core.util.HexUtils;
@ -30,6 +32,10 @@ import org.openhab.core.util.HexUtils;
public class RFXComDateTimeMessageTest {
@Test
public void testSomeMessages() throws RFXComException {
RFXComGenericDeviceConfiguration config = new RFXComGenericDeviceConfiguration();
config.deviceId = "47360";
config.subType = RFXComDateTimeMessage.SubType.RTGR328N.toString();
String hexMessage = "0D580117B90003041D030D150A69";
byte[] message = HexUtils.hexToBytes(hexMessage);
RFXComDateTimeMessage msg = (RFXComDateTimeMessage) RFXComMessageFactoryImpl.INSTANCE.createMessage(message);
@ -37,10 +43,10 @@ public class RFXComDateTimeMessageTest {
assertEquals(23, (short) (msg.seqNbr & 0xFF), "Seq Number");
assertEquals("47360", msg.getDeviceId(), "Sensor Id");
assertEquals("2003-04-29T13:21:10", msg.dateTime, "Date time");
assertEquals(2, RFXComTestHelper.getActualIntValue(msg, CHANNEL_SIGNAL_LEVEL), "Signal Level");
assertEquals(2, RFXComTestHelper.getActualIntValue(msg, config, CHANNEL_SIGNAL_LEVEL), "Signal Level");
assertEquals(DateTimeType.valueOf("2003-04-29T13:21:10"),
msg.convertToState(CHANNEL_DATE_TIME, new MockDeviceState()), "Converted value");
msg.convertToState(CHANNEL_DATE_TIME, config, new MockDeviceState()), "Converted value");
byte[] decoded = msg.decodeMessage();

@ -29,6 +29,6 @@ public class RFXComEdisioTest {
@Test
public void checkNotImplemented() {
assertThrows(RFXComMessageNotImplementedException.class,
() -> RFXComMessageFactoryImpl.INSTANCE.createMessage(EDISIO));
() -> RFXComMessageFactoryImpl.INSTANCE.createMessage(EDISIO, null, null, null));
}
}

@ -30,6 +30,6 @@ public class RFXComFS20MessageTest {
@Test
public void checkNotImplemented() {
assertThrows(RFXComMessageNotImplementedException.class,
() -> RFXComMessageFactoryImpl.INSTANCE.createMessage(PacketType.FS20));
() -> RFXComMessageFactoryImpl.INSTANCE.createMessage(PacketType.FS20, null, null, null));
}
}

@ -14,15 +14,20 @@ package org.openhab.binding.rfxcom.internal.messages;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.openhab.binding.rfxcom.internal.RFXComBindingConstants.*;
import static org.openhab.binding.rfxcom.internal.RFXComTestHelper.thingUID;
import static org.openhab.binding.rfxcom.internal.messages.RFXComBaseMessage.PacketType.FAN;
import static org.openhab.binding.rfxcom.internal.messages.RFXComFanMessage.SubType.CASAFAN;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.junit.jupiter.api.Test;
import org.openhab.binding.rfxcom.internal.RFXComBindingConstants;
import org.openhab.binding.rfxcom.internal.RFXComTestHelper;
import org.openhab.binding.rfxcom.internal.config.RFXComGenericDeviceConfiguration;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
import org.openhab.core.library.types.OnOffType;
import org.openhab.core.library.types.StringType;
import org.openhab.core.thing.ChannelUID;
import org.openhab.core.types.State;
import org.openhab.core.types.UnDefType;
import org.openhab.core.util.HexUtils;
@ -34,19 +39,26 @@ import org.openhab.core.util.HexUtils;
*/
@NonNullByDefault
public class RFXComFanMessageTest {
private static RFXComGenericDeviceConfiguration config = new RFXComGenericDeviceConfiguration();
private static ChannelUID fanSpeedChannelUID = new ChannelUID(thingUID, RFXComBindingConstants.CHANNEL_FAN_SPEED);
private static StringType fanSpeedOff = StringType.valueOf("OFF");
static {
config.deviceId = "5428224";
config.subType = RFXComFanMessage.SubType.CASAFAN.toString();
}
private static final MockDeviceState DEVICE_STATE = new MockDeviceState();
@Test
public void checkForSupportTest() throws RFXComException {
RFXComMessageFactoryImpl.INSTANCE.createMessage(FAN);
RFXComMessageFactoryImpl.INSTANCE.createMessage(FAN, config, fanSpeedChannelUID, fanSpeedOff);
}
@Test
public void basicBoundaryCheck() throws RFXComException {
RFXComFanMessage message = (RFXComFanMessage) RFXComMessageFactoryImpl.INSTANCE.createMessage(FAN);
message.setSubType(RFXComFanMessage.SubType.CASAFAN);
message.convertFromState(CHANNEL_FAN_SPEED, StringType.valueOf("OFF"));
RFXComFanMessage message = (RFXComFanMessage) RFXComMessageFactoryImpl.INSTANCE.createMessage(FAN, config,
fanSpeedChannelUID, fanSpeedOff);
RFXComTestHelper.basicBoundaryCheck(FAN, message);
}
@ -60,9 +72,9 @@ public class RFXComFanMessageTest {
assertEquals(deviceId, msg.getDeviceId(), "Sensor Id");
assertEquals(signalLevel, msg.signalLevel, "Signal Level");
assertEquals(expectedCommand, msg.convertToState(CHANNEL_COMMAND, DEVICE_STATE));
assertEquals(expectedLightCommand, msg.convertToState(CHANNEL_FAN_LIGHT, DEVICE_STATE));
assertEquals(expectedFanSpeed, msg.convertToState(CHANNEL_FAN_SPEED, DEVICE_STATE));
assertEquals(expectedCommand, msg.convertToState(CHANNEL_COMMAND, config, DEVICE_STATE));
assertEquals(expectedLightCommand, msg.convertToState(CHANNEL_FAN_LIGHT, config, DEVICE_STATE));
assertEquals(expectedFanSpeed, msg.convertToState(CHANNEL_FAN_SPEED, config, DEVICE_STATE));
assertEquals(packetType, msg.getPacketType());
@ -139,10 +151,10 @@ public class RFXComFanMessageTest {
private static void assertValues(RFXComFanMessage msg, @Nullable OnOffType expectedCommand,
State expectedLightCommand, @Nullable State expectedFanSpeed, RFXComBaseMessage.PacketType packetType,
State expectedCommandString) throws RFXComException {
assertEquals(expectedCommand, msg.convertToState(CHANNEL_COMMAND, DEVICE_STATE));
assertEquals(expectedLightCommand, msg.convertToState(CHANNEL_FAN_LIGHT, DEVICE_STATE));
assertEquals(expectedFanSpeed, msg.convertToState(CHANNEL_FAN_SPEED, DEVICE_STATE));
assertEquals(expectedCommandString, msg.convertToState(CHANNEL_COMMAND_STRING, DEVICE_STATE));
assertEquals(expectedCommand, msg.convertToState(CHANNEL_COMMAND, config, DEVICE_STATE));
assertEquals(expectedLightCommand, msg.convertToState(CHANNEL_FAN_LIGHT, config, DEVICE_STATE));
assertEquals(expectedFanSpeed, msg.convertToState(CHANNEL_FAN_SPEED, config, DEVICE_STATE));
assertEquals(expectedCommandString, msg.convertToState(CHANNEL_COMMAND_STRING, config, DEVICE_STATE));
assertEquals(packetType, msg.getPacketType());
}
}

@ -30,6 +30,6 @@ public class RFXComGasMessageTest {
@Test
public void checkNotImplemented() {
assertThrows(RFXComMessageNotImplementedException.class,
() -> RFXComMessageFactoryImpl.INSTANCE.createMessage(PacketType.GAS));
() -> RFXComMessageFactoryImpl.INSTANCE.createMessage(PacketType.GAS, null, null, null));
}
}

@ -13,13 +13,16 @@
package org.openhab.binding.rfxcom.internal.messages;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.openhab.binding.rfxcom.internal.RFXComTestHelper.commandChannelUID;
import static org.openhab.binding.rfxcom.internal.messages.RFXComBaseMessage.PacketType.HOME_CONFORT;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.jupiter.api.Test;
import org.openhab.binding.rfxcom.internal.config.RFXComGenericDeviceConfiguration;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
import org.openhab.binding.rfxcom.internal.messages.RFXComHomeConfortMessage.Commands;
import org.openhab.binding.rfxcom.internal.messages.RFXComHomeConfortMessage.SubType;
import org.openhab.core.library.types.OnOffType;
import org.openhab.core.types.Command;
import org.openhab.core.util.HexUtils;
/**
@ -29,19 +32,21 @@ import org.openhab.core.util.HexUtils;
* @author Mike Jagdis - added message handling and real test
*/
@NonNullByDefault
public class RFXComHomeConfortTest {
private void testMessage(SubType subType, Commands command, String deviceId, String data) throws RFXComException {
public class RFXComHomeConfortMessageTest {
private void testMessage(SubType subType, Command command, String deviceId, String data) throws RFXComException {
RFXComGenericDeviceConfiguration config = new RFXComGenericDeviceConfiguration();
config.deviceId = deviceId;
config.subType = subType.toString();
RFXComHomeConfortMessage message = (RFXComHomeConfortMessage) RFXComMessageFactoryImpl.INSTANCE
.createMessage(HOME_CONFORT);
message.setSubType(subType);
message.command = command;
message.setDeviceId(deviceId);
.createMessage(HOME_CONFORT, config, commandChannelUID, command);
assertArrayEquals(HexUtils.hexToBytes(data), message.decodeMessage());
}
@Test
public void testMessage1() throws RFXComException {
testMessage(SubType.TEL_010, Commands.GROUP_ON, "1118739.A.4", "0C1B0000111213410403000000");
testMessage(SubType.TEL_010, OnOffType.ON, "1118739.A.4", "0C1B0000111213410401000000");
}
}

@ -30,6 +30,6 @@ public class RFXComIOLinesMessageTest {
@Test
public void checkNotImplemented() {
assertThrows(RFXComMessageNotImplementedException.class,
() -> RFXComMessageFactoryImpl.INSTANCE.createMessage(PacketType.IO_LINES));
() -> RFXComMessageFactoryImpl.INSTANCE.createMessage(PacketType.IO_LINES, null, null, null));
}
}

@ -17,6 +17,7 @@ import static org.openhab.binding.rfxcom.internal.RFXComBindingConstants.*;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.jupiter.api.Test;
import org.openhab.binding.rfxcom.internal.config.RFXComGenericDeviceConfiguration;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedChannelException;
import org.openhab.binding.rfxcom.internal.messages.RFXComLighting1Message.Commands;
@ -33,6 +34,12 @@ import org.openhab.core.util.HexUtils;
@NonNullByDefault
public class RFXComLighting1MessageTest {
private final MockDeviceState deviceState = new MockDeviceState();
private static final RFXComGenericDeviceConfiguration config = new RFXComGenericDeviceConfiguration();
static {
config.deviceId = "A.1";
config.subType = RFXComLighting1Message.SubType.ARC.toString();
}
private void testMessage(String hexMsg, RFXComLighting1Message.SubType subType, int seqNbr, String deviceId,
byte signalLevel, Commands command, String commandString) throws RFXComException {
@ -43,7 +50,11 @@ public class RFXComLighting1MessageTest {
assertEquals(deviceId, msg.getDeviceId(), "Sensor Id");
assertEquals(signalLevel, msg.signalLevel, "Signal Level");
assertEquals(command, msg.command, "Command");
assertEquals(commandString, msg.convertToState(CHANNEL_COMMAND_STRING, deviceState).toString(),
RFXComGenericDeviceConfiguration config = new RFXComGenericDeviceConfiguration();
config.deviceId = deviceId;
config.subType = subType.toString();
assertEquals(commandString, msg.convertToState(CHANNEL_COMMAND_STRING, config, deviceState).toString(),
"Command String");
byte[] decoded = msg.decodeMessage();
@ -72,9 +83,9 @@ public class RFXComLighting1MessageTest {
msg.convertFromState(CHANNEL_COMMAND_STRING, StringType.valueOf("OFF"));
assertEquals(OnOffType.OFF, msg.convertToState(CHANNEL_COMMAND, deviceState));
assertEquals(OpenClosedType.CLOSED, msg.convertToState(CHANNEL_CONTACT, deviceState));
assertEquals(StringType.valueOf("OFF"), msg.convertToState(CHANNEL_COMMAND_STRING, deviceState));
assertEquals(OnOffType.OFF, msg.convertToState(CHANNEL_COMMAND, config, deviceState));
assertEquals(OpenClosedType.CLOSED, msg.convertToState(CHANNEL_CONTACT, config, deviceState));
assertEquals(StringType.valueOf("OFF"), msg.convertToState(CHANNEL_COMMAND_STRING, config, deviceState));
}
@Test
@ -83,9 +94,9 @@ public class RFXComLighting1MessageTest {
msg.convertFromState(CHANNEL_COMMAND_STRING, StringType.valueOf("chime"));
assertEquals(OnOffType.ON, msg.convertToState(CHANNEL_COMMAND, deviceState));
assertEquals(OpenClosedType.OPEN, msg.convertToState(CHANNEL_CONTACT, deviceState));
assertEquals(StringType.valueOf("CHIME"), msg.convertToState(CHANNEL_COMMAND_STRING, deviceState));
assertEquals(OnOffType.ON, msg.convertToState(CHANNEL_COMMAND, config, deviceState));
assertEquals(OpenClosedType.OPEN, msg.convertToState(CHANNEL_CONTACT, config, deviceState));
assertEquals(StringType.valueOf("CHIME"), msg.convertToState(CHANNEL_COMMAND_STRING, config, deviceState));
}
@Test
@ -94,9 +105,9 @@ public class RFXComLighting1MessageTest {
msg.convertFromState(CHANNEL_COMMAND_STRING, StringType.valueOf("bright"));
assertEquals(OnOffType.ON, msg.convertToState(CHANNEL_COMMAND, deviceState));
assertEquals(OpenClosedType.OPEN, msg.convertToState(CHANNEL_CONTACT, deviceState));
assertEquals(StringType.valueOf("BRIGHT"), msg.convertToState(CHANNEL_COMMAND_STRING, deviceState));
assertEquals(OnOffType.ON, msg.convertToState(CHANNEL_COMMAND, config, deviceState));
assertEquals(OpenClosedType.OPEN, msg.convertToState(CHANNEL_CONTACT, config, deviceState));
assertEquals(StringType.valueOf("BRIGHT"), msg.convertToState(CHANNEL_COMMAND_STRING, config, deviceState));
}
@Test
@ -105,8 +116,8 @@ public class RFXComLighting1MessageTest {
msg.convertFromState(CHANNEL_COMMAND_STRING, StringType.valueOf("dim"));
assertEquals(OnOffType.OFF, msg.convertToState(CHANNEL_COMMAND, deviceState));
assertEquals(OpenClosedType.CLOSED, msg.convertToState(CHANNEL_CONTACT, deviceState));
assertEquals(StringType.valueOf("DIM"), msg.convertToState(CHANNEL_COMMAND_STRING, deviceState));
assertEquals(OnOffType.OFF, msg.convertToState(CHANNEL_COMMAND, config, deviceState));
assertEquals(OpenClosedType.CLOSED, msg.convertToState(CHANNEL_CONTACT, config, deviceState));
assertEquals(StringType.valueOf("DIM"), msg.convertToState(CHANNEL_COMMAND_STRING, config, deviceState));
}
}

@ -31,6 +31,6 @@ public class RFXComLighting3MessageTest {
public void checkNotImplemented() {
// TODO Note that this message is supported in the 1.9 binding
assertThrows(RFXComMessageNotImplementedException.class,
() -> RFXComMessageFactoryImpl.INSTANCE.createMessage(PacketType.LIGHTING3));
() -> RFXComMessageFactoryImpl.INSTANCE.createMessage(PacketType.LIGHTING3, null, null, null));
}
}

@ -12,21 +12,31 @@
*/
package org.openhab.binding.rfxcom.internal.messages;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.*;
import static org.openhab.binding.rfxcom.internal.RFXComBindingConstants.*;
import static org.openhab.binding.rfxcom.internal.RFXComTestHelper.*;
import static org.openhab.binding.rfxcom.internal.messages.RFXComBaseMessage.PacketType.LIGHTING4;
import static org.openhab.binding.rfxcom.internal.messages.RFXComLighting4Message.Commands.*;
import static org.openhab.binding.rfxcom.internal.messages.RFXComLighting4Message.SubType.PT2262;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.junit.jupiter.api.Test;
import org.openhab.binding.rfxcom.internal.RFXComTestHelper;
import org.openhab.binding.rfxcom.internal.config.RFXComDeviceConfiguration;
import org.openhab.binding.rfxcom.internal.config.RFXComLighting4DeviceConfiguration;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComInvalidStateException;
import org.openhab.binding.rfxcom.internal.messages.RFXComBaseMessage.PacketType;
import org.openhab.core.config.discovery.DiscoveryResultBuilder;
import org.openhab.core.library.types.OnOffType;
import org.openhab.core.library.types.OpenClosedType;
import org.openhab.core.thing.ChannelUID;
import org.openhab.core.thing.ThingUID;
import org.openhab.core.types.Command;
import org.openhab.core.util.HexUtils;
/**
@ -36,17 +46,33 @@ import org.openhab.core.util.HexUtils;
*/
@NonNullByDefault
public class RFXComLighting4MessageTest {
static public final ChannelUID contactChannelUID = new ChannelUID(thingUID, CHANNEL_CONTACT);
static public void checkDiscoveryResult(RFXComDeviceMessage<RFXComLighting4Message.SubType> msg, String deviceId,
@Nullable Integer pulse, String subType) throws RFXComException {
String thingUID = "homeduino:rfxcom:fssfsd:thing";
DiscoveryResultBuilder builder = DiscoveryResultBuilder.create(new ThingUID(thingUID));
// check whether the pulse is stored
msg.addDevicePropertiesTo(builder);
Map<String, Object> properties = builder.build().getProperties();
assertEquals(deviceId, properties.get("deviceId"), "Device Id");
assertEquals(subType, properties.get("subType"), "Sub type");
if (pulse != null) {
assertEquals(pulse, properties.get("pulse"), "Pulse");
}
}
@Test
public void basicBoundaryCheck() throws RFXComException {
RFXComLighting4Message message = (RFXComLighting4Message) RFXComMessageFactoryImpl.INSTANCE
.createMessage(LIGHTING4);
RFXComLighting4DeviceConfiguration config = new RFXComLighting4DeviceConfiguration();
config.deviceId = "90000";
config.subType = "PT2262";
config.pulse = 300;
message.setConfig(config);
message.convertFromState(CHANNEL_COMMAND, OnOffType.ON);
RFXComLighting4Message message = (RFXComLighting4Message) RFXComMessageFactoryImpl.INSTANCE
.createMessage(LIGHTING4, config, commandChannelUID, OnOffType.ON);
byte[] binaryMessage = message.decodeMessage();
RFXComLighting4Message msg = (RFXComLighting4Message) RFXComMessageFactoryImpl.INSTANCE
@ -55,56 +81,59 @@ public class RFXComLighting4MessageTest {
assertEquals("90000", msg.getDeviceId(), "Sensor Id");
}
private void testMessage(String hexMsg, RFXComLighting4Message.SubType subType, String deviceId,
@Nullable Integer pulse, RFXComLighting4Message.Commands command, @Nullable Integer seqNbr, int signalLevel,
int offCommand, int onCommand) throws RFXComException {
testMessage(hexMsg, subType, deviceId, pulse, command.toByte(), seqNbr, signalLevel, offCommand, onCommand);
}
private void testMessageWithoutCommandIds(String hexMsg, RFXComLighting4Message.SubType subType, String deviceId,
@Nullable Integer pulse, int commandByte, @Nullable Integer seqNbr, int signalLevel, Command command)
throws RFXComException {
// These tests rely on the deprecated behaviour of a "known" set of ON/OFF values and will
// be removed in a later release to be replaced with test that check we throw an exception
// if the config isn't specified (see the open/closed tests).
RFXComLighting4DeviceConfiguration config = new RFXComLighting4DeviceConfiguration();
config.deviceId = deviceId;
config.subType = subType.toString();
private void testMessage(String hexMsg, RFXComLighting4Message.SubType subType, String deviceId,
@Nullable Integer pulse, byte commandByte, @Nullable Integer seqNbr, int signalLevel, int offCommand,
int onCommand) throws RFXComException {
RFXComLighting4Message msg = (RFXComLighting4Message) RFXComMessageFactoryImpl.INSTANCE
.createMessage(HexUtils.hexToBytes(hexMsg));
assertEquals(deviceId, msg.getDeviceId(), "Sensor Id");
assertEquals(commandByte, RFXComTestHelper.getActualIntValue(msg, CHANNEL_COMMAND_ID), "Command");
assertEquals(commandByte, RFXComTestHelper.getActualIntValue(msg, config, CHANNEL_COMMAND_ID), "Command");
if (seqNbr != null) {
assertEquals(seqNbr.shortValue(), (short) (msg.seqNbr & 0xFF), "Seq Number");
}
assertEquals(signalLevel, RFXComTestHelper.getActualIntValue(msg, CHANNEL_SIGNAL_LEVEL), "Signal Level");
assertEquals(signalLevel, RFXComTestHelper.getActualIntValue(msg, config, CHANNEL_SIGNAL_LEVEL),
"Signal Level");
assertEquals(command, msg.convertToCommand(CHANNEL_COMMAND, config, null));
byte[] decoded = msg.decodeMessage();
assertEquals(hexMsg, HexUtils.bytesToHex(decoded), "Message converted back");
RFXComTestHelper.checkDiscoveryResult(msg, deviceId, pulse, subType.toString(), offCommand, onCommand);
checkDiscoveryResult(msg, deviceId, pulse, subType.toString());
}
@Test
public void testSomeMessages() throws RFXComException {
testMessage("091300E1D8AD59018F70", PT2262, "887509", 399, ON_9, 225, 2, 4, 9);
testMessage("0913005FA9A9C901A170", PT2262, "694940", 417, ON_9, 95, 2, 4, 9);
testMessage("091300021D155C01E960", PT2262, "119125", 489, ON_12, 2, 2, 4, 12);
testMessage("091300D345DD99018C50", PT2262, "286169", 396, ON_9, 211, 2, 4, 9);
testMessage("09130035D149A2017750", PT2262, "857242", 375, OFF_2, 53, 2, 2, 1);
testMessage("0913000B4E462A012280", PT2262, "320610", 290, ON_10, 11, 3, 4, 10);
testMessage("09130009232D2E013970", PT2262, "144082", 313, OFF_14, 9, 2, 14, 1);
testMessage("091300CA0F8D2801AA70", PT2262, "63698", 426, ON_8, 202, 2, 4, 8);
testMessageWithoutCommandIds("091300E1D8AD59018F70", PT2262, "887509", 399, 9, 225, 2, OnOffType.ON);
testMessageWithoutCommandIds("0913005FA9A9C901A170", PT2262, "694940", 417, 9, 95, 2, OnOffType.ON);
testMessageWithoutCommandIds("091300021D155C01E960", PT2262, "119125", 489, 12, 2, 2, OnOffType.ON);
testMessageWithoutCommandIds("091300D345DD99018C50", PT2262, "286169", 396, 9, 211, 2, OnOffType.ON);
testMessageWithoutCommandIds("09130035D149A2017750", PT2262, "857242", 375, 2, 53, 2, OnOffType.OFF);
testMessageWithoutCommandIds("0913000B4E462A012280", PT2262, "320610", 290, 10, 11, 3, OnOffType.ON);
testMessageWithoutCommandIds("09130009232D2E013970", PT2262, "144082", 313, 14, 9, 2, OnOffType.OFF);
testMessageWithoutCommandIds("091300CA0F8D2801AA70", PT2262, "63698", 426, 8, 202, 2, OnOffType.ON);
}
@Test
public void testSomeAlarmRemote() throws RFXComException {
testMessage("0913004A0D8998016E60", PT2262, "55449", 366, ON_8, 74, 2, 4, 8);
testMessageWithoutCommandIds("0913004A0D8998016E60", PT2262, "55449", 366, 8, 74, 2, OnOffType.ON);
}
@Test
public void testCheapPirSensor() throws RFXComException {
testMessage("091300EF505FC6019670", PT2262, "329212", 406, ON_6, 239, 2, 4, 6);
testMessageWithoutCommandIds("091300EF505FC6019670", PT2262, "329212", 406, 6, 239, 2, OnOffType.ON);
}
@Test
public void testSomeConradMessages() throws RFXComException {
testMessage("0913003554545401A150", PT2262, "345413", 417, OFF_4, 53, 2, 4, 1);
testMessageWithoutCommandIds("0913003554545401A150", PT2262, "345413", 417, 4, 53, 2, OnOffType.OFF);
}
@Test
@ -113,7 +142,7 @@ public class RFXComLighting4MessageTest {
"0913004C044551013780", "0913004E044551013780");
for (String message : onMessages) {
testMessage(message, PT2262, "17493", null, ON_1, null, 3, 4, 1);
testMessageWithoutCommandIds(message, PT2262, "17493", null, 1, null, 3, OnOffType.ON);
}
List<String> offMessages = Arrays.asList("09130051044554013980", "09130053044554013680", "09130055044554013680",
@ -121,7 +150,149 @@ public class RFXComLighting4MessageTest {
"09130060044554013980", "09130062044554013680", "09130064044554013280");
for (String message : offMessages) {
testMessage(message, PT2262, "17493", null, OFF_4, null, 3, 4, 1);
testMessageWithoutCommandIds(message, PT2262, "17493", null, 4, null, 3, OnOffType.OFF);
}
}
private void testRxWithConfig(String hexMsg, RFXComDeviceConfiguration config,
RFXComLighting4Message.SubType subType, String deviceId, @Nullable Integer pulse, int commandByte,
@Nullable Integer seqNbr, int signalLevel, ChannelUID channelUID, Command command) throws RFXComException {
RFXComLighting4Message msg = (RFXComLighting4Message) RFXComMessageFactoryImpl.INSTANCE
.createMessage(HexUtils.hexToBytes(hexMsg));
assertEquals(deviceId, msg.getDeviceId(), "Sensor Id");
assertEquals(commandByte, RFXComTestHelper.getActualIntValue(msg, config, CHANNEL_COMMAND_ID), "Command");
if (seqNbr != null) {
assertEquals(seqNbr.shortValue(), (short) (msg.seqNbr & 0xFF), "Seq Number");
}
assertEquals(signalLevel, RFXComTestHelper.getActualIntValue(msg, config, CHANNEL_SIGNAL_LEVEL),
"Signal Level");
assertEquals(command, msg.convertToCommand(channelUID.getId(), config, null));
byte[] decoded = msg.decodeMessage();
assertEquals(hexMsg, HexUtils.bytesToHex(decoded), "Message converted back");
checkDiscoveryResult(msg, deviceId, pulse, subType.toString());
}
@Test
public void testRxWithFullConfig() throws RFXComException {
RFXComLighting4DeviceConfiguration config = new RFXComLighting4DeviceConfiguration();
config.deviceId = "12345";
config.subType = PT2262.toString();
config.onCommandId = 0xA;
config.offCommandId = 0xB;
config.openCommandId = 0xC;
config.closedCommandId = 0xD;
testRxWithConfig("0913003503039A01A150", config, PT2262, "12345", 417, 0xA, 53, 2, commandChannelUID,
OnOffType.ON);
testRxWithConfig("0913003503039B01A150", config, PT2262, "12345", 417, 0xB, 53, 2, commandChannelUID,
OnOffType.OFF);
testRxWithConfig("0913003503039C01A150", config, PT2262, "12345", 417, 0xC, 53, 2, contactChannelUID,
OpenClosedType.OPEN);
testRxWithConfig("0913003503039D01A150", config, PT2262, "12345", 417, 0xD, 53, 2, contactChannelUID,
OpenClosedType.CLOSED);
}
@Test
public void testRxWithPartialConfig() throws RFXComException {
RFXComLighting4DeviceConfiguration config = new RFXComLighting4DeviceConfiguration();
config.deviceId = "12345";
config.subType = PT2262.toString();
config.onCommandId = 0xA;
config.openCommandId = 0xC;
testRxWithConfig("0913003503039A01A150", config, PT2262, "12345", 417, 0xA, 53, 2, commandChannelUID,
OnOffType.ON);
assertThrows(RFXComInvalidStateException.class, () -> testRxWithConfig("0913003503039B01A150", config, PT2262,
"12345", 417, 0xB, 53, 2, commandChannelUID, OnOffType.OFF));
testRxWithConfig("0913003503039C01A150", config, PT2262, "12345", 417, 0xC, 53, 2, contactChannelUID,
OpenClosedType.OPEN);
assertThrows(RFXComInvalidStateException.class, () -> testRxWithConfig("0913003503039D01A150", config, PT2262,
"12345", 417, 0xD, 53, 2, contactChannelUID, OpenClosedType.CLOSED));
}
@Test
public void testRxWithNoConfig() throws RFXComException {
RFXComLighting4DeviceConfiguration config = new RFXComLighting4DeviceConfiguration();
config.deviceId = "12345";
config.subType = PT2262.toString();
// These will fall back on deprecated behaviour, but should all be assertThrows in the future.
testRxWithConfig("0913003503039A01A150", config, PT2262, "12345", 417, 0xA, 53, 2, commandChannelUID,
OnOffType.ON);
testRxWithConfig("0913003503039B01A150", config, PT2262, "12345", 417, 0xB, 53, 2, commandChannelUID,
OnOffType.ON);
testRxWithConfig("0913003503039C01A150", config, PT2262, "12345", 417, 0xC, 53, 2, contactChannelUID,
OpenClosedType.OPEN);
testRxWithConfig("0913003503039D01A150", config, PT2262, "12345", 417, 0xD, 53, 2, contactChannelUID,
OpenClosedType.OPEN);
}
private void testTxWithConfig(RFXComDeviceConfiguration config, ChannelUID channelUID, Command command,
RFXComLighting4Message.SubType subType, String deviceId, @Nullable Integer pulse, int commandByte,
String hexMsg) throws RFXComException {
RFXComLighting4Message msg = (RFXComLighting4Message) RFXComMessageFactoryImpl.INSTANCE
.createMessage(PacketType.LIGHTING4, config, channelUID, command);
assertEquals(deviceId, msg.getDeviceId(), "Sensor Id");
assertEquals(commandByte, RFXComTestHelper.getActualIntValue(msg, config, CHANNEL_COMMAND_ID), "Command");
assertEquals(0, msg.seqNbr & 0xFF, "Seq Number");
assertEquals(0, RFXComTestHelper.getActualIntValue(msg, config, CHANNEL_SIGNAL_LEVEL), "Signal Level");
assertEquals(hexMsg, HexUtils.bytesToHex(msg.decodeMessage()), "Message bytes");
}
@Test
void testTxWithFullConfig() throws RFXComException {
RFXComLighting4DeviceConfiguration config = new RFXComLighting4DeviceConfiguration();
config.deviceId = "703696";
config.subType = PT2262.toString();
config.onCommandId = 0xA;
config.offCommandId = 0xB;
config.openCommandId = 0xC;
config.closedCommandId = 0xD;
config.pulse = 417;
testTxWithConfig(config, commandChannelUID, OnOffType.ON, PT2262, "703696", 417, 0xA, "09130000ABCD0A01A100");
testTxWithConfig(config, commandChannelUID, OnOffType.OFF, PT2262, "703696", 417, 0xB, "09130000ABCD0B01A100");
testTxWithConfig(config, contactChannelUID, OpenClosedType.OPEN, PT2262, "703696", 417, 0xC,
"09130000ABCD0C01A100");
testTxWithConfig(config, contactChannelUID, OpenClosedType.CLOSED, PT2262, "703696", 417, 0xD,
"09130000ABCD0D01A100");
}
@Test
void testTxWithPartialConfig() throws RFXComException {
RFXComLighting4DeviceConfiguration config = new RFXComLighting4DeviceConfiguration();
config.deviceId = "703696";
config.subType = PT2262.toString();
config.onCommandId = 0xA;
config.openCommandId = 0xC;
config.pulse = 417;
testTxWithConfig(config, commandChannelUID, OnOffType.ON, PT2262, "703696", 417, 0xA, "09130000ABCD0A01A100");
// Falls back on deprecated behaviour, but should be assertThrows in the future.
testTxWithConfig(config, commandChannelUID, OnOffType.OFF, PT2262, "703696", 417, 0x4, "09130000ABCD0401A100");
testTxWithConfig(config, contactChannelUID, OpenClosedType.OPEN, PT2262, "703696", 417, 0xC,
"09130000ABCD0C01A100");
assertThrows(RFXComInvalidStateException.class, () -> testTxWithConfig(config, contactChannelUID,
OpenClosedType.CLOSED, PT2262, "703696", 417, 0xD, "??"));
}
@Test
void testTxWithNoConfig() throws RFXComException {
RFXComLighting4DeviceConfiguration config = new RFXComLighting4DeviceConfiguration();
config.deviceId = "703696";
config.subType = PT2262.toString();
config.pulse = 417;
// Falls back on deprecated behaviour, but should be assertThrows in the future.
testTxWithConfig(config, commandChannelUID, OnOffType.ON, PT2262, "703696", 417, 0x1, "09130000ABCD0101A100");
// Falls back on deprecated behaviour, but should be assertThrows in the future.
testTxWithConfig(config, commandChannelUID, OnOffType.OFF, PT2262, "703696", 417, 0x4, "09130000ABCD0401A100");
assertThrows(RFXComInvalidStateException.class, () -> testTxWithConfig(config, contactChannelUID,
OpenClosedType.OPEN, PT2262, "703696", 417, 0xC, "??"));
assertThrows(RFXComInvalidStateException.class, () -> testTxWithConfig(config, contactChannelUID,
OpenClosedType.CLOSED, PT2262, "703696", 417, 0xD, "??"));
}
}

@ -13,15 +13,16 @@
package org.openhab.binding.rfxcom.internal.messages;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.openhab.binding.rfxcom.internal.RFXComBindingConstants.*;
import static org.openhab.binding.rfxcom.internal.RFXComBindingConstants.CHANNEL_COMMAND_STRING;
import static org.openhab.binding.rfxcom.internal.RFXComTestHelper.commandChannelUID;
import static org.openhab.binding.rfxcom.internal.messages.RFXComBaseMessage.PacketType.LIGHTING5;
import static org.openhab.binding.rfxcom.internal.messages.RFXComLighting5Message.Commands.*;
import static org.openhab.binding.rfxcom.internal.messages.RFXComLighting5Message.SubType.IT;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.jupiter.api.Test;
import org.openhab.binding.rfxcom.internal.RFXComTestHelper;
import org.openhab.binding.rfxcom.internal.config.RFXComGenericDeviceConfiguration;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedChannelException;
import org.openhab.core.library.types.OnOffType;
import org.openhab.core.library.types.StringType;
import org.openhab.core.util.HexUtils;
@ -34,30 +35,33 @@ import org.openhab.core.util.HexUtils;
@NonNullByDefault
public class RFXComLighting5MessageTest {
private final MockDeviceState deviceState = new MockDeviceState();
private static final RFXComGenericDeviceConfiguration config = new RFXComGenericDeviceConfiguration();
static {
config.deviceId = "2061.1";
config.subType = RFXComLighting5Message.SubType.IT.toString();
}
@Test
public void convertFromStateItMessage() throws RFXComException {
RFXComDeviceMessage itMessageObject = (RFXComDeviceMessage) RFXComMessageFactoryImpl.INSTANCE
.createMessage(LIGHTING5);
itMessageObject.setDeviceId("2061.1");
itMessageObject.setSubType(IT);
itMessageObject.convertFromState(CHANNEL_COMMAND, OnOffType.ON);
RFXComMessage itMessageObject = RFXComMessageFactoryImpl.INSTANCE.createMessage(LIGHTING5, config,
commandChannelUID, OnOffType.ON);
byte[] message = itMessageObject.decodeMessage();
String hexMessage = HexUtils.bytesToHex(message);
assertEquals("0A140F0000080D01010000", hexMessage, "Message is not as expected");
RFXComLighting5Message msg = (RFXComLighting5Message) RFXComMessageFactoryImpl.INSTANCE.createMessage(message);
assertEquals(IT, msg.subType, "SubType");
assertEquals(RFXComLighting5Message.SubType.IT, msg.subType, "SubType");
assertEquals("2061.1", msg.getDeviceId(), "Sensor Id");
assertEquals(ON, msg.command, "Command");
}
@Test
public void basicBoundaryCheck() throws RFXComException {
RFXComLighting5Message message = (RFXComLighting5Message) RFXComMessageFactoryImpl.INSTANCE
.createMessage(LIGHTING5);
RFXComGenericDeviceConfiguration config = new RFXComGenericDeviceConfiguration();
config.deviceId = "206.1";
config.subType = RFXComLighting5Message.SubType.LIGHTWAVERF.toString();
message.subType = RFXComLighting5Message.SubType.LIGHTWAVERF;
message.command = ON;
RFXComLighting5Message message = (RFXComLighting5Message) RFXComMessageFactoryImpl.INSTANCE
.createMessage(LIGHTING5, config, commandChannelUID, OnOffType.ON);
RFXComTestHelper.basicBoundaryCheck(LIGHTING5, message);
}
@ -65,29 +69,30 @@ public class RFXComLighting5MessageTest {
// TODO please add more tests for different messages
@Test
public void testStringCommandOpenRelay() throws RFXComUnsupportedChannelException {
public void testStringCommandOpenRelay() throws RFXComException {
RFXComLighting5Message msg = new RFXComLighting5Message();
msg.convertFromState(CHANNEL_COMMAND_STRING, StringType.valueOf("OPEN_RELAY"));
assertEquals(StringType.valueOf("OPEN_RELAY"), msg.convertToState(CHANNEL_COMMAND_STRING, deviceState));
assertEquals(StringType.valueOf("OPEN_RELAY"), msg.convertToState(CHANNEL_COMMAND_STRING, config, deviceState));
assertEquals(OPEN_RELAY, msg.command);
}
@Test
public void testStringCommandCloseRelay() throws RFXComUnsupportedChannelException {
public void testStringCommandCloseRelay() throws RFXComException {
RFXComLighting5Message msg = new RFXComLighting5Message();
msg.convertFromState(CHANNEL_COMMAND_STRING, StringType.valueOf("CLOSE_RELAY"));
assertEquals(StringType.valueOf("CLOSE_RELAY"), msg.convertToState(CHANNEL_COMMAND_STRING, deviceState));
assertEquals(StringType.valueOf("CLOSE_RELAY"),
msg.convertToState(CHANNEL_COMMAND_STRING, config, deviceState));
assertEquals(CLOSE_RELAY, msg.command);
}
@Test
public void testStringCommandStopRelay() throws RFXComUnsupportedChannelException {
public void testStringCommandStopRelay() throws RFXComException {
RFXComLighting5Message msg = new RFXComLighting5Message();
msg.convertFromState(CHANNEL_COMMAND_STRING, StringType.valueOf("STOP_RELAY"));
assertEquals(StringType.valueOf("STOP_RELAY"), msg.convertToState(CHANNEL_COMMAND_STRING, deviceState));
assertEquals(StringType.valueOf("STOP_RELAY"), msg.convertToState(CHANNEL_COMMAND_STRING, config, deviceState));
assertEquals(STOP_RELAY, msg.command);
}
}

@ -28,7 +28,7 @@ public class RFXComPowerMessageTest {
@Test
public void checkNotImplemented() {
// TODO Note that this message is supported in the 1.9 binding
assertThrows(RFXComMessageNotImplementedException.class,
() -> RFXComMessageFactoryImpl.INSTANCE.createMessage(RFXComBaseMessage.PacketType.POWER));
assertThrows(RFXComMessageNotImplementedException.class, () -> RFXComMessageFactoryImpl.INSTANCE
.createMessage(RFXComBaseMessage.PacketType.POWER, null, null, null));
}
}

@ -30,6 +30,6 @@ public class RFXComRFXMeterMessageTest {
@Test
public void checkNotImplemented() {
assertThrows(RFXComMessageNotImplementedException.class,
() -> RFXComMessageFactoryImpl.INSTANCE.createMessage(PacketType.RFXMETER));
() -> RFXComMessageFactoryImpl.INSTANCE.createMessage(PacketType.RFXMETER, null, null, null));
}
}

@ -89,7 +89,7 @@ public class RFXComRFXSensorMessageTest {
private @Nullable Double getChannelAsDouble(String channelId, RFXComRFXSensorMessage msg, DeviceState deviceState)
throws RFXComException {
return getStateAsDouble(msg.convertToState(channelId, deviceState));
return getStateAsDouble(msg.convertToState(channelId, null, deviceState));
}
private @Nullable Double getStateAsDouble(State state) {

@ -29,6 +29,6 @@ public class RFXComRadiator1MessageTest {
@Test
public void checkNotImplemented() {
assertThrows(RFXComMessageNotImplementedException.class,
() -> RFXComMessageFactoryImpl.INSTANCE.createMessage(RADIATOR1));
() -> RFXComMessageFactoryImpl.INSTANCE.createMessage(RADIATOR1, null, null, null));
}
}

@ -13,18 +13,18 @@
package org.openhab.binding.rfxcom.internal.messages;
import static org.junit.jupiter.api.Assertions.*;
import static org.openhab.binding.rfxcom.internal.RFXComTestHelper.commandChannelUID;
import java.nio.ByteBuffer;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.jupiter.api.Test;
import org.openhab.binding.rfxcom.internal.RFXComBindingConstants;
import org.openhab.binding.rfxcom.internal.config.RFXComRawDeviceConfiguration;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComInvalidStateException;
import org.openhab.binding.rfxcom.internal.messages.RFXComBaseMessage.PacketType;
import org.openhab.core.library.types.OnOffType;
import org.openhab.core.types.Type;
import org.openhab.core.types.Command;
import org.openhab.core.util.HexUtils;
/**
@ -34,7 +34,6 @@ import org.openhab.core.util.HexUtils;
*/
@NonNullByDefault
public class RFXComRawMessageTest {
private void testMessageRx(String hexMsg, RFXComRawMessage.SubType subType, int seqNbr, int repeat, String pulses)
throws RFXComException {
final RFXComRawMessage msg = (RFXComRawMessage) RFXComMessageFactoryImpl.INSTANCE
@ -53,11 +52,10 @@ public class RFXComRawMessageTest {
testMessageRx("087F0027051356ECC0", RFXComRawMessage.SubType.RAW_PACKET1, 0x27, 5, "1356ECC0");
}
private void testMessageTx(RFXComRawDeviceConfiguration config, Type command, String hexMsg)
private void testMessageTx(RFXComRawDeviceConfiguration config, Command command, String hexMsg)
throws RFXComException {
RFXComRawMessage msg = (RFXComRawMessage) RFXComMessageFactoryImpl.INSTANCE.createMessage(PacketType.RAW);
msg.setConfig(config);
msg.convertFromState(RFXComBindingConstants.CHANNEL_COMMAND, command);
RFXComRawMessage msg = (RFXComRawMessage) RFXComMessageFactoryImpl.INSTANCE.createMessage(PacketType.RAW,
config, commandChannelUID, command);
byte[] decoded = msg.decodeMessage();
assertEquals(hexMsg, HexUtils.bytesToHex(decoded), "Transmitted message");

@ -29,6 +29,6 @@ public class RFXComRemoteControlMessageTest {
@Test
public void checkNotImplemented() {
assertThrows(RFXComMessageNotImplementedException.class,
() -> RFXComMessageFactoryImpl.INSTANCE.createMessage(PacketType.REMOTE_CONTROL));
() -> RFXComMessageFactoryImpl.INSTANCE.createMessage(PacketType.REMOTE_CONTROL, null, null, null));
}
}

@ -13,13 +13,21 @@
package org.openhab.binding.rfxcom.internal.messages;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.openhab.binding.rfxcom.internal.RFXComTestHelper.thingUID;
import static org.openhab.binding.rfxcom.internal.messages.RFXComBaseMessage.PacketType.RFY;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.jupiter.api.Test;
import org.openhab.binding.rfxcom.internal.RFXComBindingConstants;
import org.openhab.binding.rfxcom.internal.RFXComTestHelper;
import org.openhab.binding.rfxcom.internal.config.RFXComGenericDeviceConfiguration;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
import org.openhab.binding.rfxcom.internal.messages.RFXComRfyMessage.Commands;
import org.openhab.binding.rfxcom.internal.messages.RFXComRfyMessage.SubType;
import org.openhab.core.library.types.IncreaseDecreaseType;
import org.openhab.core.library.types.OpenClosedType;
import org.openhab.core.library.types.UpDownType;
import org.openhab.core.thing.ChannelUID;
import org.openhab.core.types.Command;
import org.openhab.core.util.HexUtils;
/**
@ -29,33 +37,40 @@ import org.openhab.core.util.HexUtils;
*/
@NonNullByDefault
public class RFXComRfyMessageTest {
private static ChannelUID shutterChannelUID = new ChannelUID(thingUID, RFXComBindingConstants.CHANNEL_SHUTTER);
private static ChannelUID venetionBlindChannelUID = new ChannelUID(thingUID,
RFXComBindingConstants.CHANNEL_VENETIAN_BLIND);
@Test
public void basicBoundaryCheck() throws RFXComException {
RFXComRfyMessage message = (RFXComRfyMessage) RFXComMessageFactoryImpl.INSTANCE.createMessage(RFY);
RFXComGenericDeviceConfiguration config = new RFXComGenericDeviceConfiguration();
config.deviceId = "1.1";
config.subType = SubType.RFY.toString();
message.subType = SubType.RFY;
message.command = Commands.UP;
RFXComRfyMessage message = (RFXComRfyMessage) RFXComMessageFactoryImpl.INSTANCE.createMessage(RFY, config,
shutterChannelUID, UpDownType.UP);
RFXComTestHelper.basicBoundaryCheck(RFY, message);
}
private void testMessage(SubType subType, Commands command, String deviceId, String data) throws RFXComException {
RFXComRfyMessage message = (RFXComRfyMessage) RFXComMessageFactoryImpl.INSTANCE.createMessage(RFY);
message.setSubType(subType);
message.command = command;
message.setDeviceId(deviceId);
private void testMessage(SubType subType, Command command, String deviceId, String data) throws RFXComException {
RFXComGenericDeviceConfiguration config = new RFXComGenericDeviceConfiguration();
config.deviceId = "66051.4";
config.subType = subType.toString();
RFXComRfyMessage message = (RFXComRfyMessage) RFXComMessageFactoryImpl.INSTANCE.createMessage(RFY, config,
venetionBlindChannelUID, command);
assertArrayEquals(HexUtils.hexToBytes(data), message.decodeMessage());
}
@Test
public void testMessage1() throws RFXComException {
testMessage(SubType.RFY, Commands.UP_SHORT, "66051.4", "0C1A0000010203040F00000000");
testMessage(SubType.RFY, OpenClosedType.OPEN, "66051.4", "0C1A0000010203040F00000000");
}
@Test
public void testMessage2() throws RFXComException {
testMessage(SubType.ASA, Commands.DOWN_LONG, "66051.4", "0C1A0300010203041200000000");
testMessage(SubType.ASA, IncreaseDecreaseType.INCREASE, "66051.4", "0C1A0300010203041200000000");
}
}

@ -1,63 +0,0 @@
/**
* Copyright (c) 2010-2021 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.rfxcom.internal.messages;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.Map;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
import org.openhab.binding.rfxcom.internal.messages.RFXComBaseMessage.PacketType;
import org.openhab.core.config.discovery.DiscoveryResultBuilder;
import org.openhab.core.library.types.DecimalType;
import org.openhab.core.thing.ThingUID;
/**
* Helper class for testing the RFXCom-binding
*
* @author Martin van Wingerden - Initial contribution
*/
@NonNullByDefault
class RFXComTestHelper {
static void basicBoundaryCheck(PacketType packetType, RFXComMessage message) throws RFXComException {
// This is a place where its easy to make mistakes in coding, and can result in errors, normally
// array bounds errors
byte[] binaryMessage = message.decodeMessage();
assertEquals(binaryMessage[0], binaryMessage.length - 1, "Wrong packet length");
assertEquals(packetType.toByte(), binaryMessage[1], "Wrong packet type");
}
static void checkDiscoveryResult(RFXComDeviceMessage msg, String deviceId, @Nullable Integer pulse, String subType,
int offCommand, int onCommand) throws RFXComException {
String thingUID = "homeduino:rfxcom:fssfsd:thing";
DiscoveryResultBuilder builder = DiscoveryResultBuilder.create(new ThingUID(thingUID));
// check whether the pulse is stored
msg.addDevicePropertiesTo(builder);
Map<String, Object> properties = builder.build().getProperties();
assertEquals(deviceId, properties.get("deviceId"), "Device Id");
assertEquals(subType, properties.get("subType"), "Sub type");
if (pulse != null) {
assertEquals(pulse, properties.get("pulse"), "Pulse");
}
assertEquals(onCommand, properties.get("onCommandId"), "On command");
assertEquals(offCommand, properties.get("offCommandId"), "Off command");
}
static int getActualIntValue(RFXComDeviceMessage msg, String channelId) throws RFXComException {
return ((DecimalType) msg.convertToState(channelId, new MockDeviceState())).intValue();
}
}

@ -30,6 +30,6 @@ public class RFXComThermostat2MessageTest {
public void checkNotImplemented() {
// TODO Note that this message is supported in the 1.9 binding
assertThrows(RFXComMessageNotImplementedException.class,
() -> RFXComMessageFactoryImpl.INSTANCE.createMessage(PacketType.THERMOSTAT2));
() -> RFXComMessageFactoryImpl.INSTANCE.createMessage(PacketType.THERMOSTAT2, null, null, null));
}
}

@ -14,14 +14,16 @@ package org.openhab.binding.rfxcom.internal.messages;
import static org.junit.jupiter.api.Assertions.*;
import static org.openhab.binding.rfxcom.internal.RFXComBindingConstants.*;
import static org.openhab.binding.rfxcom.internal.RFXComTestHelper.commandChannelUID;
import static org.openhab.binding.rfxcom.internal.messages.RFXComBaseMessage.PacketType.THERMOSTAT3;
import static org.openhab.binding.rfxcom.internal.messages.RFXComThermostat3Message.SubType.*;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.junit.jupiter.api.Test;
import org.openhab.binding.rfxcom.internal.RFXComTestHelper;
import org.openhab.binding.rfxcom.internal.config.RFXComGenericDeviceConfiguration;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedChannelException;
import org.openhab.core.library.types.OnOffType;
import org.openhab.core.library.types.StopMoveType;
import org.openhab.core.library.types.StringType;
@ -37,20 +39,24 @@ import org.openhab.core.util.HexUtils;
*/
@NonNullByDefault
public class RFXComThermostat3MessageTest {
private static RFXComGenericDeviceConfiguration config = new RFXComGenericDeviceConfiguration();
static {
config.deviceId = "106411";
config.subType = RFXComThermostat3Message.SubType.MERTIK__G6R_H4S_TRANSMIT_ONLY.toString();
}
private final MockDeviceState deviceState = new MockDeviceState();
@Test
public void checkForSupportTest() throws RFXComException {
RFXComMessageFactoryImpl.INSTANCE.createMessage(THERMOSTAT3);
RFXComMessageFactoryImpl.INSTANCE.createMessage(THERMOSTAT3, config, commandChannelUID, OnOffType.ON);
}
@Test
public void basicBoundaryCheck() throws RFXComException {
RFXComThermostat3Message message = (RFXComThermostat3Message) RFXComMessageFactoryImpl.INSTANCE
.createMessage(THERMOSTAT3);
message.subType = RFXComThermostat3Message.SubType.MERTIK__G6R_H4S_TRANSMIT_ONLY;
message.command = RFXComThermostat3Message.Commands.ON;
.createMessage(THERMOSTAT3, config, commandChannelUID, OnOffType.ON);
RFXComTestHelper.basicBoundaryCheck(THERMOSTAT3, message);
}
@ -77,213 +83,212 @@ public class RFXComThermostat3MessageTest {
assertEquals(command, msg.command, CHANNEL_COMMAND);
assertEquals(signalLevel, msg.signalLevel, "Signal Level");
assertEquals(commandChannel, msg.convertToState(CHANNEL_COMMAND, deviceState));
assertEquals(secondCommandChannel, msg.convertToState(CHANNEL_COMMAND_SECOND, deviceState));
assertEquals(controlChannel, msg.convertToState(CHANNEL_CONTROL, deviceState));
assertEquals(commandStringChannel, msg.convertToState(CHANNEL_COMMAND_STRING, deviceState));
assertEquals(commandChannel, msg.convertToState(CHANNEL_COMMAND, config, deviceState));
assertEquals(secondCommandChannel, msg.convertToState(CHANNEL_COMMAND_SECOND, config, deviceState));
assertEquals(controlChannel, msg.convertToState(CHANNEL_CONTROL, config, deviceState));
assertEquals(commandStringChannel, msg.convertToState(CHANNEL_COMMAND_STRING, config, deviceState));
byte[] decoded = msg.decodeMessage();
assertEquals(hexMessage, HexUtils.bytesToHex(decoded), "Message converted back");
}
// TODO please add tests for real messages
@Test
public void testCommandChannelOn() throws RFXComUnsupportedChannelException {
public void testCommandChannelOn() throws RFXComException {
RFXComThermostat3Message msg = new RFXComThermostat3Message();
msg.convertFromState(CHANNEL_COMMAND, OnOffType.ON);
assertEquals(OnOffType.ON, msg.convertToState(CHANNEL_COMMAND, deviceState));
assertEquals(OnOffType.ON, msg.convertToState(CHANNEL_CONTROL, deviceState));
assertEquals(StringType.valueOf("ON"), msg.convertToState(CHANNEL_COMMAND_STRING, deviceState));
assertNull(msg.convertToState(CHANNEL_COMMAND_SECOND, deviceState));
assertEquals(OnOffType.ON, msg.convertToState(CHANNEL_COMMAND, config, deviceState));
assertEquals(OnOffType.ON, msg.convertToState(CHANNEL_CONTROL, config, deviceState));
assertEquals(StringType.valueOf("ON"), msg.convertToState(CHANNEL_COMMAND_STRING, config, deviceState));
assertNull(msg.convertToState(CHANNEL_COMMAND_SECOND, config, deviceState));
}
@Test
public void testCommandChannelOff() throws RFXComUnsupportedChannelException {
public void testCommandChannelOff() throws RFXComException {
RFXComThermostat3Message msg = new RFXComThermostat3Message();
msg.convertFromState(CHANNEL_COMMAND, OnOffType.OFF);
assertEquals(OnOffType.OFF, msg.convertToState(CHANNEL_COMMAND, deviceState));
assertEquals(OnOffType.OFF, msg.convertToState(CHANNEL_CONTROL, deviceState));
assertNull(msg.convertToState(CHANNEL_COMMAND_SECOND, deviceState));
assertEquals(StringType.valueOf("OFF"), msg.convertToState(CHANNEL_COMMAND_STRING, deviceState));
assertEquals(OnOffType.OFF, msg.convertToState(CHANNEL_COMMAND, config, deviceState));
assertEquals(OnOffType.OFF, msg.convertToState(CHANNEL_CONTROL, config, deviceState));
assertNull(msg.convertToState(CHANNEL_COMMAND_SECOND, config, deviceState));
assertEquals(StringType.valueOf("OFF"), msg.convertToState(CHANNEL_COMMAND_STRING, config, deviceState));
}
@Test
public void testSecondCommandChannelOn() throws RFXComUnsupportedChannelException {
public void testSecondCommandChannelOn() throws RFXComException {
RFXComThermostat3Message msg = new RFXComThermostat3Message();
msg.convertFromState(CHANNEL_COMMAND_SECOND, OnOffType.ON);
assertNull(msg.convertToState(CHANNEL_COMMAND, deviceState));
assertEquals(OnOffType.ON, msg.convertToState(CHANNEL_COMMAND_SECOND, deviceState));
assertNull(msg.convertToState(CHANNEL_CONTROL, deviceState));
assertEquals(StringType.valueOf("SECOND_ON"), msg.convertToState(CHANNEL_COMMAND_STRING, deviceState));
assertNull(msg.convertToState(CHANNEL_COMMAND, config, deviceState));
assertEquals(OnOffType.ON, msg.convertToState(CHANNEL_COMMAND_SECOND, config, deviceState));
assertNull(msg.convertToState(CHANNEL_CONTROL, config, deviceState));
assertEquals(StringType.valueOf("SECOND_ON"), msg.convertToState(CHANNEL_COMMAND_STRING, config, deviceState));
}
@Test
public void testSecondCommandChannelOff() throws RFXComUnsupportedChannelException {
public void testSecondCommandChannelOff() throws RFXComException {
RFXComThermostat3Message msg = new RFXComThermostat3Message();
msg.convertFromState(CHANNEL_COMMAND_SECOND, OnOffType.OFF);
assertNull(msg.convertToState(CHANNEL_COMMAND, deviceState));
assertEquals(OnOffType.OFF, msg.convertToState(CHANNEL_COMMAND_SECOND, deviceState));
assertNull(msg.convertToState(CHANNEL_CONTROL, deviceState));
assertEquals(StringType.valueOf("SECOND_OFF"), msg.convertToState(CHANNEL_COMMAND_STRING, deviceState));
assertNull(msg.convertToState(CHANNEL_COMMAND, config, deviceState));
assertEquals(OnOffType.OFF, msg.convertToState(CHANNEL_COMMAND_SECOND, config, deviceState));
assertNull(msg.convertToState(CHANNEL_CONTROL, config, deviceState));
assertEquals(StringType.valueOf("SECOND_OFF"), msg.convertToState(CHANNEL_COMMAND_STRING, config, deviceState));
}
@Test
public void testControlUp() throws RFXComUnsupportedChannelException {
public void testControlUp() throws RFXComException {
RFXComThermostat3Message msg = new RFXComThermostat3Message();
msg.convertFromState(CHANNEL_CONTROL, UpDownType.UP);
assertEquals(OnOffType.ON, msg.convertToState(CHANNEL_COMMAND, deviceState));
assertEquals(UpDownType.UP, msg.convertToState(CHANNEL_CONTROL, deviceState));
assertEquals(StringType.valueOf("UP"), msg.convertToState(CHANNEL_COMMAND_STRING, deviceState));
assertEquals(OnOffType.ON, msg.convertToState(CHANNEL_COMMAND, config, deviceState));
assertEquals(UpDownType.UP, msg.convertToState(CHANNEL_CONTROL, config, deviceState));
assertEquals(StringType.valueOf("UP"), msg.convertToState(CHANNEL_COMMAND_STRING, config, deviceState));
assertNull(msg.convertToState(CHANNEL_COMMAND_SECOND, deviceState));
assertNull(msg.convertToState(CHANNEL_COMMAND_SECOND, config, deviceState));
}
@Test
public void testControlDown() throws RFXComUnsupportedChannelException {
public void testControlDown() throws RFXComException {
RFXComThermostat3Message msg = new RFXComThermostat3Message();
msg.convertFromState(CHANNEL_CONTROL, UpDownType.DOWN);
assertEquals(UnDefType.UNDEF, msg.convertToState(CHANNEL_COMMAND, deviceState));
assertEquals(UpDownType.DOWN, msg.convertToState(CHANNEL_CONTROL, deviceState));
assertEquals(StringType.valueOf("DOWN"), msg.convertToState(CHANNEL_COMMAND_STRING, deviceState));
assertEquals(UnDefType.UNDEF, msg.convertToState(CHANNEL_COMMAND, config, deviceState));
assertEquals(UpDownType.DOWN, msg.convertToState(CHANNEL_CONTROL, config, deviceState));
assertEquals(StringType.valueOf("DOWN"), msg.convertToState(CHANNEL_COMMAND_STRING, config, deviceState));
assertNull(msg.convertToState(CHANNEL_COMMAND_SECOND, deviceState));
assertNull(msg.convertToState(CHANNEL_COMMAND_SECOND, config, deviceState));
}
@Test
public void testControlStop() throws RFXComUnsupportedChannelException {
public void testControlStop() throws RFXComException {
RFXComThermostat3Message msg = new RFXComThermostat3Message();
msg.convertFromState(CHANNEL_CONTROL, StopMoveType.STOP);
assertEquals(UnDefType.UNDEF, msg.convertToState(CHANNEL_COMMAND, deviceState));
assertEquals(StringType.valueOf("STOP"), msg.convertToState(CHANNEL_COMMAND_STRING, deviceState));
assertEquals(UnDefType.UNDEF, msg.convertToState(CHANNEL_COMMAND, config, deviceState));
assertEquals(StringType.valueOf("STOP"), msg.convertToState(CHANNEL_COMMAND_STRING, config, deviceState));
assertNull(msg.convertToState(CHANNEL_CONTROL, deviceState));
assertNull(msg.convertToState(CHANNEL_COMMAND_SECOND, deviceState));
assertNull(msg.convertToState(CHANNEL_CONTROL, config, deviceState));
assertNull(msg.convertToState(CHANNEL_COMMAND_SECOND, config, deviceState));
}
@Test
public void testCommandStringOff() throws RFXComUnsupportedChannelException {
public void testCommandStringOff() throws RFXComException {
RFXComThermostat3Message msg = new RFXComThermostat3Message();
msg.convertFromState(CHANNEL_COMMAND_STRING, StringType.valueOf("OFF"));
assertEquals(OnOffType.OFF, msg.convertToState(CHANNEL_COMMAND, deviceState));
assertEquals(OnOffType.OFF, msg.convertToState(CHANNEL_CONTROL, deviceState));
assertEquals(StringType.valueOf("OFF"), msg.convertToState(CHANNEL_COMMAND_STRING, deviceState));
assertNull(msg.convertToState(CHANNEL_COMMAND_SECOND, deviceState));
assertEquals(OnOffType.OFF, msg.convertToState(CHANNEL_COMMAND, config, deviceState));
assertEquals(OnOffType.OFF, msg.convertToState(CHANNEL_CONTROL, config, deviceState));
assertEquals(StringType.valueOf("OFF"), msg.convertToState(CHANNEL_COMMAND_STRING, config, deviceState));
assertNull(msg.convertToState(CHANNEL_COMMAND_SECOND, config, deviceState));
}
@Test
public void testCommandStringOn() throws RFXComUnsupportedChannelException {
public void testCommandStringOn() throws RFXComException {
RFXComThermostat3Message msg = new RFXComThermostat3Message();
msg.convertFromState(CHANNEL_COMMAND_STRING, StringType.valueOf("On"));
assertEquals(OnOffType.ON, msg.convertToState(CHANNEL_COMMAND, deviceState));
assertEquals(OnOffType.ON, msg.convertToState(CHANNEL_CONTROL, deviceState));
assertEquals(StringType.valueOf("ON"), msg.convertToState(CHANNEL_COMMAND_STRING, deviceState));
assertNull(msg.convertToState(CHANNEL_COMMAND_SECOND, deviceState));
assertEquals(OnOffType.ON, msg.convertToState(CHANNEL_COMMAND, config, deviceState));
assertEquals(OnOffType.ON, msg.convertToState(CHANNEL_CONTROL, config, deviceState));
assertEquals(StringType.valueOf("ON"), msg.convertToState(CHANNEL_COMMAND_STRING, config, deviceState));
assertNull(msg.convertToState(CHANNEL_COMMAND_SECOND, config, deviceState));
}
@Test
public void testCommandStringUp() throws RFXComUnsupportedChannelException {
public void testCommandStringUp() throws RFXComException {
RFXComThermostat3Message msg = new RFXComThermostat3Message();
msg.convertFromState(CHANNEL_COMMAND_STRING, StringType.valueOf("UP"));
assertEquals(OnOffType.ON, msg.convertToState(CHANNEL_COMMAND, deviceState));
assertEquals(UpDownType.UP, msg.convertToState(CHANNEL_CONTROL, deviceState));
assertEquals(StringType.valueOf("UP"), msg.convertToState(CHANNEL_COMMAND_STRING, deviceState));
assertEquals(OnOffType.ON, msg.convertToState(CHANNEL_COMMAND, config, deviceState));
assertEquals(UpDownType.UP, msg.convertToState(CHANNEL_CONTROL, config, deviceState));
assertEquals(StringType.valueOf("UP"), msg.convertToState(CHANNEL_COMMAND_STRING, config, deviceState));
assertNull(msg.convertToState(CHANNEL_COMMAND_SECOND, deviceState));
assertNull(msg.convertToState(CHANNEL_COMMAND_SECOND, config, deviceState));
}
@Test
public void testCommandStringDown() throws RFXComUnsupportedChannelException {
public void testCommandStringDown() throws RFXComException {
RFXComThermostat3Message msg = new RFXComThermostat3Message();
msg.convertFromState(CHANNEL_COMMAND_STRING, StringType.valueOf("down"));
assertEquals(UnDefType.UNDEF, msg.convertToState(CHANNEL_COMMAND, deviceState));
assertEquals(UpDownType.DOWN, msg.convertToState(CHANNEL_CONTROL, deviceState));
assertEquals(StringType.valueOf("DOWN"), msg.convertToState(CHANNEL_COMMAND_STRING, deviceState));
assertNull(msg.convertToState(CHANNEL_COMMAND_SECOND, deviceState));
assertEquals(UnDefType.UNDEF, msg.convertToState(CHANNEL_COMMAND, config, deviceState));
assertEquals(UpDownType.DOWN, msg.convertToState(CHANNEL_CONTROL, config, deviceState));
assertEquals(StringType.valueOf("DOWN"), msg.convertToState(CHANNEL_COMMAND_STRING, config, deviceState));
assertNull(msg.convertToState(CHANNEL_COMMAND_SECOND, config, deviceState));
}
@Test
public void testCommandStringRunUp() throws RFXComUnsupportedChannelException {
public void testCommandStringRunUp() throws RFXComException {
RFXComThermostat3Message msg = new RFXComThermostat3Message();
msg.convertFromState(CHANNEL_COMMAND_STRING, StringType.valueOf("RUN_UP"));
assertEquals(OnOffType.ON, msg.convertToState(CHANNEL_COMMAND, deviceState));
assertEquals(UpDownType.UP, msg.convertToState(CHANNEL_CONTROL, deviceState));
assertEquals(StringType.valueOf("RUN_UP"), msg.convertToState(CHANNEL_COMMAND_STRING, deviceState));
assertNull(msg.convertToState(CHANNEL_COMMAND_SECOND, deviceState));
assertEquals(OnOffType.ON, msg.convertToState(CHANNEL_COMMAND, config, deviceState));
assertEquals(UpDownType.UP, msg.convertToState(CHANNEL_CONTROL, config, deviceState));
assertEquals(StringType.valueOf("RUN_UP"), msg.convertToState(CHANNEL_COMMAND_STRING, config, deviceState));
assertNull(msg.convertToState(CHANNEL_COMMAND_SECOND, config, deviceState));
}
@Test
public void testCommandStringRunDown() throws RFXComUnsupportedChannelException {
public void testCommandStringRunDown() throws RFXComException {
RFXComThermostat3Message msg = new RFXComThermostat3Message();
msg.convertFromState(CHANNEL_COMMAND_STRING, StringType.valueOf("RUN_DOWN"));
assertEquals(OnOffType.OFF, msg.convertToState(CHANNEL_COMMAND, deviceState));
assertEquals(UpDownType.DOWN, msg.convertToState(CHANNEL_CONTROL, deviceState));
assertEquals(StringType.valueOf("RUN_DOWN"), msg.convertToState(CHANNEL_COMMAND_STRING, deviceState));
assertNull(msg.convertToState(CHANNEL_COMMAND_SECOND, deviceState));
assertEquals(OnOffType.OFF, msg.convertToState(CHANNEL_COMMAND, config, deviceState));
assertEquals(UpDownType.DOWN, msg.convertToState(CHANNEL_CONTROL, config, deviceState));
assertEquals(StringType.valueOf("RUN_DOWN"), msg.convertToState(CHANNEL_COMMAND_STRING, config, deviceState));
assertNull(msg.convertToState(CHANNEL_COMMAND_SECOND, config, deviceState));
}
@Test
public void testCommandStringStop() throws RFXComUnsupportedChannelException {
public void testCommandStringStop() throws RFXComException {
RFXComThermostat3Message msg = new RFXComThermostat3Message();
msg.convertFromState(CHANNEL_COMMAND_STRING, StringType.valueOf("STOP"));
assertEquals(UnDefType.UNDEF, msg.convertToState(CHANNEL_COMMAND, deviceState));
assertEquals(StringType.valueOf("STOP"), msg.convertToState(CHANNEL_COMMAND_STRING, deviceState));
assertEquals(UnDefType.UNDEF, msg.convertToState(CHANNEL_COMMAND, config, deviceState));
assertEquals(StringType.valueOf("STOP"), msg.convertToState(CHANNEL_COMMAND_STRING, config, deviceState));
assertNull(msg.convertToState(CHANNEL_CONTROL, deviceState));
assertNull(msg.convertToState(CHANNEL_COMMAND_SECOND, deviceState));
assertNull(msg.convertToState(CHANNEL_CONTROL, config, deviceState));
assertNull(msg.convertToState(CHANNEL_COMMAND_SECOND, config, deviceState));
}
@Test
public void testCommandStringSecondOn() throws RFXComUnsupportedChannelException {
public void testCommandStringSecondOn() throws RFXComException {
RFXComThermostat3Message msg = new RFXComThermostat3Message();
msg.convertFromState(CHANNEL_COMMAND_STRING, StringType.valueOf("SECOND_ON"));
assertEquals(OnOffType.ON, msg.convertToState(CHANNEL_COMMAND_SECOND, deviceState));
assertEquals(StringType.valueOf("SECOND_ON"), msg.convertToState(CHANNEL_COMMAND_STRING, deviceState));
assertEquals(OnOffType.ON, msg.convertToState(CHANNEL_COMMAND_SECOND, config, deviceState));
assertEquals(StringType.valueOf("SECOND_ON"), msg.convertToState(CHANNEL_COMMAND_STRING, config, deviceState));
assertNull(msg.convertToState(CHANNEL_COMMAND, deviceState));
assertNull(msg.convertToState(CHANNEL_CONTROL, deviceState));
assertNull(msg.convertToState(CHANNEL_COMMAND, config, deviceState));
assertNull(msg.convertToState(CHANNEL_CONTROL, config, deviceState));
}
@Test
public void testCommandStringSecondOff() throws RFXComUnsupportedChannelException {
public void testCommandStringSecondOff() throws RFXComException {
RFXComThermostat3Message msg = new RFXComThermostat3Message();
msg.convertFromState(CHANNEL_COMMAND_STRING, StringType.valueOf("SECOND_OFF"));
assertEquals(OnOffType.OFF, msg.convertToState(CHANNEL_COMMAND_SECOND, deviceState));
assertEquals(StringType.valueOf("SECOND_OFF"), msg.convertToState(CHANNEL_COMMAND_STRING, deviceState));
assertEquals(OnOffType.OFF, msg.convertToState(CHANNEL_COMMAND_SECOND, config, deviceState));
assertEquals(StringType.valueOf("SECOND_OFF"), msg.convertToState(CHANNEL_COMMAND_STRING, config, deviceState));
assertNull(msg.convertToState(CHANNEL_COMMAND, deviceState));
assertNull(msg.convertToState(CHANNEL_CONTROL, deviceState));
assertNull(msg.convertToState(CHANNEL_COMMAND, config, deviceState));
assertNull(msg.convertToState(CHANNEL_CONTROL, config, deviceState));
}
}

@ -29,6 +29,6 @@ public class RFXComThermostat4MessageTest {
@Test
public void checkNotImplemented() {
assertThrows(RFXComMessageNotImplementedException.class,
() -> RFXComMessageFactoryImpl.INSTANCE.createMessage(THERMOSTAT4));
() -> RFXComMessageFactoryImpl.INSTANCE.createMessage(THERMOSTAT4, null, null, null));
}
}

@ -18,7 +18,6 @@ import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.jupiter.api.Test;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
import org.openhab.binding.rfxcom.internal.exceptions.RFXComMessageTooLongException;
import org.openhab.binding.rfxcom.internal.messages.RFXComBaseMessage.PacketType;
import org.openhab.core.util.HexUtils;
/**
@ -51,12 +50,9 @@ public class RFXComUndecodedRFMessageTest {
@Test
public void testLongMessage() throws RFXComException {
RFXComUndecodedRFMessage msg = (RFXComUndecodedRFMessage) RFXComMessageFactoryImpl.INSTANCE
.createMessage(PacketType.UNDECODED_RF_MESSAGE);
msg.subType = RFXComUndecodedRFMessage.SubType.ARC;
msg.seqNbr = 1;
msg.rawPayload = HexUtils.hexToBytes("000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F2021");
assertThrows(RFXComMessageTooLongException.class, () -> msg.decodeMessage());
assertThrows(RFXComMessageTooLongException.class,
() -> testMessage("25030101000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F2021",
RFXComUndecodedRFMessage.SubType.ARC, 0x01,
"000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F2021"));
}
}

@ -30,6 +30,6 @@ public class RFXComWaterMessageTest {
@Test
public void checkNotImplemented() {
assertThrows(RFXComMessageNotImplementedException.class,
() -> RFXComMessageFactoryImpl.INSTANCE.createMessage(PacketType.WATER));
() -> RFXComMessageFactoryImpl.INSTANCE.createMessage(PacketType.WATER, null, null, null));
}
}

@ -30,6 +30,6 @@ public class RFXComWeightMessageTest {
public void checkNotImplemented() {
// TODO Note that this message is supported in the 1.9 binding
assertThrows(RFXComMessageNotImplementedException.class,
() -> RFXComMessageFactoryImpl.INSTANCE.createMessage(PacketType.WEIGHT));
() -> RFXComMessageFactoryImpl.INSTANCE.createMessage(PacketType.WEIGHT, null, null, null));
}
}