use of org.openhab.core.library.types.QuantityType in project org.openhab.binding.zwave by openhab.
the class ZWaveThermostatSetpointConverter method receiveCommand.
@Override
public List<ZWaveCommandClassTransactionPayload> receiveCommand(ZWaveThingChannel channel, ZWaveNode node, Command command) {
String scaleString = channel.getArguments().get("config_scale");
String setpointType = channel.getArguments().get("type");
int scale = 0;
if (scaleString != null) {
scale = Integer.parseInt(scaleString);
}
logger.debug("NODE {}: Thermostat command received for {}", node.getNodeId(), command.toString());
BigDecimal value;
if (command instanceof QuantityType) {
QuantityType<?> quantity = (QuantityType<?>) command;
if (quantity.getUnit() == SIUnits.CELSIUS) {
scale = 0;
} else if (quantity.getUnit() == ImperialUnits.FAHRENHEIT) {
scale = 1;
}
value = quantity.toBigDecimal();
} else if (command instanceof DecimalType) {
value = ((DecimalType) command).toBigDecimal();
} else {
logger.debug("NODE {}: Thermostat command received with unsupported type {}", node.getNodeId(), command.getClass().getSimpleName());
return null;
}
ZWaveThermostatSetpointCommandClass commandClass = (ZWaveThermostatSetpointCommandClass) node.resolveCommandClass(ZWaveCommandClass.CommandClass.COMMAND_CLASS_THERMOSTAT_SETPOINT, channel.getEndpoint());
ZWaveCommandClassTransactionPayload serialMessage;
if (setpointType != null) {
serialMessage = node.encapsulate(commandClass.setMessage(scale, SetpointType.valueOf(setpointType), value), channel.getEndpoint());
} else {
serialMessage = node.encapsulate(commandClass.setMessage(scale, value), channel.getEndpoint());
}
if (serialMessage == null) {
logger.warn("NODE {}: Generating message failed for command class = {}, endpoint = {}", node.getNodeId(), commandClass.getCommandClass(), channel.getEndpoint());
return null;
}
logger.debug("NODE {}: Sending Message: {}", node.getNodeId(), serialMessage);
List<ZWaveCommandClassTransactionPayload> messages = new ArrayList<ZWaveCommandClassTransactionPayload>();
messages.add(serialMessage);
// Request an update so that OH knows when the setpoint has changed.
if (setpointType != null) {
serialMessage = node.encapsulate(commandClass.getMessage(SetpointType.valueOf(setpointType)), channel.getEndpoint());
} else {
serialMessage = node.encapsulate(commandClass.getValueMessage(), channel.getEndpoint());
}
if (serialMessage != null) {
messages.add(serialMessage);
}
return messages;
}
use of org.openhab.core.library.types.QuantityType in project org.openhab.binding.zwave by openhab.
the class ZWaveThermostatSetpointConverter method handleEvent.
@Override
public State handleEvent(ZWaveThingChannel channel, ZWaveCommandClassValueEvent event) {
String setpointType = channel.getArguments().get("type");
ZWaveThermostatSetpointValueEvent setpointEvent = (ZWaveThermostatSetpointValueEvent) event;
// Don't trigger event if this item is bound to another setpoint type
if (setpointType != null && SetpointType.valueOf(setpointType) != setpointEvent.getSetpointType()) {
return null;
}
BigDecimal value = (BigDecimal) event.getValue();
switch(setpointEvent.getScale()) {
case 0:
return new QuantityType<>(value, SIUnits.CELSIUS);
case 1:
return new QuantityType<>(value, ImperialUnits.FAHRENHEIT);
default:
logger.debug("NODE {}: Unknown temperature scale {}", event.getNodeId(), setpointEvent.getScale());
break;
}
return new DecimalType(value);
}
use of org.openhab.core.library.types.QuantityType in project org.openhab.binding.zwave by openhab.
the class ZWaveThingHandlerTest method testConvertToDataType.
@Test
public void testConvertToDataType() {
ZWaveThingHandler sut = new ZWaveThingHandlerForTest(null);
ChannelUID channelUID = new ChannelUID("channel:for:a:test");
Command command = new QuantityType<>("22 °C");
Command result = sut.convertCommandToDataType(channelUID, ZWaveThingChannel.DataType.DecimalType, command, ZWaveThingChannel.DataType.QuantityType);
assertTrue(result instanceof DecimalType);
}
use of org.openhab.core.library.types.QuantityType in project openhab-addons by openhab.
the class AirQualityStationHandler method discoverAttributes.
private void discoverAttributes() {
getAirQualityData().ifPresent(data -> {
// Update thing properties
Map<String, String> properties = new HashMap<>();
properties.put(ATTRIBUTIONS, data.getAttributions());
PointType serverLocation = locationProvider.getLocation();
if (serverLocation != null) {
PointType stationLocation = new PointType(data.getCity().getGeo());
double distance = serverLocation.distanceFrom(stationLocation).doubleValue();
properties.put(DISTANCE, new QuantityType<>(distance / 1000, KILO(SIUnits.METRE)).toString());
}
// Search and remove missing pollutant channels
List<Channel> channels = new ArrayList<>(getThing().getChannels());
Stream.of(Pollutant.values()).forEach(pollutant -> {
String groupName = pollutant.name().toLowerCase();
double value = data.getIaqiValue(pollutant);
channels.removeIf(channel -> value == -1 && groupName.equals(channel.getUID().getGroupId()));
});
// Update thing configuration
Configuration config = editConfiguration();
config.put(AirQualityConfiguration.STATION_ID, data.getStationId());
ThingBuilder thingBuilder = editThing();
thingBuilder.withChannels(channels).withConfiguration(config).withProperties(properties).withLocation(data.getCity().getName());
updateThing(thingBuilder.build());
});
}
use of org.openhab.core.library.types.QuantityType in project openhab-addons by openhab.
the class HotWaterHandler method getBoostRemainingState.
private State getBoostRemainingState() {
if (getData().hotWater.size() >= 1) {
final HotWaterDTO firstChannel = getData().hotWater.get(0);
final Integer overrideTimeout = firstChannel.getOverrideTimeoutUnixTime();
if (overrideTimeout != null && !"NONE".equalsIgnoreCase(firstChannel.getOverrideType())) {
return new QuantityType<Time>(overrideTimeout - (System.currentTimeMillis() / 1000L), Units.SECOND);
}
}
return new QuantityType<Time>(0, Units.SECOND);
}
Aggregations