Search in sources :

Example 1 with QuantityType

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;
}
Also used : ZWaveCommandClassTransactionPayload(org.openhab.binding.zwave.internal.protocol.transaction.ZWaveCommandClassTransactionPayload) QuantityType(org.openhab.core.library.types.QuantityType) ArrayList(java.util.ArrayList) DecimalType(org.openhab.core.library.types.DecimalType) ZWaveThermostatSetpointCommandClass(org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveThermostatSetpointCommandClass) BigDecimal(java.math.BigDecimal)

Example 2 with QuantityType

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);
}
Also used : QuantityType(org.openhab.core.library.types.QuantityType) DecimalType(org.openhab.core.library.types.DecimalType) ZWaveThermostatSetpointValueEvent(org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveThermostatSetpointCommandClass.ZWaveThermostatSetpointValueEvent) BigDecimal(java.math.BigDecimal)

Example 3 with QuantityType

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);
}
Also used : Command(org.openhab.core.types.Command) QuantityType(org.openhab.core.library.types.QuantityType) ChannelUID(org.openhab.core.thing.ChannelUID) DecimalType(org.openhab.core.library.types.DecimalType) Test(org.junit.jupiter.api.Test)

Example 4 with QuantityType

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());
    });
}
Also used : ThingBuilder(org.openhab.core.thing.binding.builder.ThingBuilder) Configuration(org.openhab.core.config.core.Configuration) SensitiveGroupConfiguration(org.openhab.binding.airquality.internal.config.SensitiveGroupConfiguration) AirQualityConfiguration(org.openhab.binding.airquality.internal.config.AirQualityConfiguration) HashMap(java.util.HashMap) QuantityType(org.openhab.core.library.types.QuantityType) Channel(org.openhab.core.thing.Channel) ArrayList(java.util.ArrayList) PointType(org.openhab.core.library.types.PointType)

Example 5 with QuantityType

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);
}
Also used : HotWaterDTO(org.openhab.binding.draytonwiser.internal.model.HotWaterDTO) QuantityType(org.openhab.core.library.types.QuantityType)

Aggregations

QuantityType (org.openhab.core.library.types.QuantityType)203 DecimalType (org.openhab.core.library.types.DecimalType)109 Test (org.junit.jupiter.api.Test)61 State (org.openhab.core.types.State)56 StringType (org.openhab.core.library.types.StringType)54 OnOffType (org.openhab.core.library.types.OnOffType)43 RefreshType (org.openhab.core.types.RefreshType)37 BigDecimal (java.math.BigDecimal)34 Temperature (javax.measure.quantity.Temperature)34 InOrder (org.mockito.InOrder)30 Nullable (org.eclipse.jdt.annotation.Nullable)29 ChannelUID (org.openhab.core.thing.ChannelUID)24 Command (org.openhab.core.types.Command)22 DateTimeType (org.openhab.core.library.types.DateTimeType)19 PercentType (org.openhab.core.library.types.PercentType)18 UnDefType (org.openhab.core.types.UnDefType)16 ProfileCallback (org.openhab.core.thing.profiles.ProfileCallback)14 IOException (java.io.IOException)13 Thing (org.openhab.core.thing.Thing)13 NumberItem (org.openhab.core.library.items.NumberItem)12