2 * Copyright (c) 2010-2021 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.miele.internal.handler;
15 import static org.openhab.binding.miele.internal.MieleBindingConstants.APPLIANCE_ID;
16 import static org.openhab.binding.miele.internal.MieleBindingConstants.MIELE_DEVICE_CLASS_DISHWASHER;
17 import static org.openhab.binding.miele.internal.MieleBindingConstants.POWER_CONSUMPTION_CHANNEL_ID;
18 import static org.openhab.binding.miele.internal.MieleBindingConstants.WATER_CONSUMPTION_CHANNEL_ID;
20 import java.math.BigDecimal;
22 import org.openhab.core.library.types.OnOffType;
23 import org.openhab.core.library.types.QuantityType;
24 import org.openhab.core.library.unit.Units;
25 import org.openhab.core.thing.ChannelUID;
26 import org.openhab.core.thing.Thing;
27 import org.openhab.core.types.Command;
28 import org.openhab.core.types.RefreshType;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 import com.google.gson.JsonElement;
35 * The {@link DishWasherHandler} is responsible for handling commands,
36 * which are sent to one of the channels
38 * @author Karel Goderis - Initial contribution
39 * @author Kai Kreuzer - fixed handling of REFRESH commands
40 * @author Martin Lepsy - fixed handling of empty JSON results
41 * @author Jacob Laursen - Fixed multicast and protocol support (ZigBee/LAN), added power/water consumption channels
43 public class DishWasherHandler extends MieleApplianceHandler<DishwasherChannelSelector>
44 implements ExtendedDeviceStateListener {
46 private static final int POWER_CONSUMPTION_BYTE_POSITION = 16;
47 private static final int WATER_CONSUMPTION_BYTE_POSITION = 18;
48 private static final int EXTENDED_STATE_MIN_SIZE_BYTES = 19;
50 private final Logger logger = LoggerFactory.getLogger(DishWasherHandler.class);
52 public DishWasherHandler(Thing thing) {
53 super(thing, DishwasherChannelSelector.class, MIELE_DEVICE_CLASS_DISHWASHER);
57 public void handleCommand(ChannelUID channelUID, Command command) {
58 super.handleCommand(channelUID, command);
60 String channelID = channelUID.getId();
61 String applianceId = (String) getThing().getConfiguration().getProperties().get(APPLIANCE_ID);
63 DishwasherChannelSelector selector = (DishwasherChannelSelector) getValueSelectorFromChannelID(channelID);
64 JsonElement result = null;
67 if (selector != null) {
70 if (command.equals(OnOffType.ON)) {
71 result = bridgeHandler.invokeOperation(applianceId, modelID, "start");
72 } else if (command.equals(OnOffType.OFF)) {
73 result = bridgeHandler.invokeOperation(applianceId, modelID, "stop");
78 if (!(command instanceof RefreshType)) {
79 logger.debug("{} is a read-only channel that does not accept commands",
80 selector.getChannelID());
86 if (result != null && isResultProcessable(result)) {
87 logger.debug("Result of operation is {}", result.getAsString());
89 } catch (IllegalArgumentException e) {
91 "An error occurred while trying to set the read-only variable associated with channel '{}' to '{}'",
92 channelID, command.toString());
96 public void onApplianceExtendedStateChanged(byte[] extendedDeviceState) {
97 if (extendedDeviceState.length < EXTENDED_STATE_MIN_SIZE_BYTES) {
98 logger.warn("Unexpected size of extended state: {}", extendedDeviceState);
102 BigDecimal kiloWattHoursTenths = BigDecimal
103 .valueOf(extendedDeviceState[POWER_CONSUMPTION_BYTE_POSITION] & 0xff);
104 var kiloWattHours = new QuantityType<>(kiloWattHoursTenths.divide(BigDecimal.valueOf(10)), Units.KILOWATT_HOUR);
105 updateExtendedState(POWER_CONSUMPTION_CHANNEL_ID, kiloWattHours);
107 BigDecimal decilitres = BigDecimal.valueOf(extendedDeviceState[WATER_CONSUMPTION_BYTE_POSITION] & 0xff);
108 var litres = new QuantityType<>(decilitres.divide(BigDecimal.valueOf(10)), Units.LITRE);
109 updateExtendedState(WATER_CONSUMPTION_CHANNEL_ID, litres);