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.POWER_CONSUMPTION_CHANNEL_ID;
17 import static org.openhab.binding.miele.internal.MieleBindingConstants.PROTOCOL_PROPERTY_NAME;
18 import static org.openhab.binding.miele.internal.MieleBindingConstants.WATER_CONSUMPTION_CHANNEL_ID;
20 import java.math.BigDecimal;
22 import org.openhab.binding.miele.internal.FullyQualifiedApplianceIdentifier;
23 import org.openhab.core.library.types.OnOffType;
24 import org.openhab.core.library.types.QuantityType;
25 import org.openhab.core.library.unit.Units;
26 import org.openhab.core.thing.ChannelUID;
27 import org.openhab.core.thing.Thing;
28 import org.openhab.core.types.Command;
29 import org.openhab.core.types.RefreshType;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 import com.google.gson.JsonElement;
36 * The {@link DishWasherHandler} is responsible for handling commands,
37 * which are sent to one of the channels
39 * @author Karel Goderis - Initial contribution
40 * @author Kai Kreuzer - fixed handling of REFRESH commands
41 * @author Martin Lepsy - fixed handling of empty JSON results
42 * @author Jacob Laursen - Fixed multicast and protocol support (ZigBee/LAN), added power/water consumption channels
44 public class DishWasherHandler extends MieleApplianceHandler<DishwasherChannelSelector>
45 implements ExtendedDeviceStateListener {
47 private static final int POWER_CONSUMPTION_BYTE_POSITION = 16;
48 private static final int WATER_CONSUMPTION_BYTE_POSITION = 18;
49 private static final int EXTENDED_STATE_SIZE_BYTES = 24;
51 private final Logger logger = LoggerFactory.getLogger(DishWasherHandler.class);
53 public DishWasherHandler(Thing thing) {
54 super(thing, DishwasherChannelSelector.class, "Dishwasher");
58 public void handleCommand(ChannelUID channelUID, Command command) {
59 super.handleCommand(channelUID, command);
61 String channelID = channelUID.getId();
62 String applianceId = (String) getThing().getConfiguration().getProperties().get(APPLIANCE_ID);
63 String protocol = getThing().getProperties().get(PROTOCOL_PROPERTY_NAME);
64 var applianceIdentifier = new FullyQualifiedApplianceIdentifier(applianceId, protocol);
66 DishwasherChannelSelector selector = (DishwasherChannelSelector) getValueSelectorFromChannelID(channelID);
67 JsonElement result = null;
70 if (selector != null) {
73 if (command.equals(OnOffType.ON)) {
74 result = bridgeHandler.invokeOperation(applianceIdentifier, modelID, "start");
75 } else if (command.equals(OnOffType.OFF)) {
76 result = bridgeHandler.invokeOperation(applianceIdentifier, modelID, "stop");
81 if (!(command instanceof RefreshType)) {
82 logger.debug("{} is a read-only channel that does not accept commands",
83 selector.getChannelID());
89 if (isResultProcessable(result)) {
90 logger.debug("Result of operation is {}", result.getAsString());
92 } catch (IllegalArgumentException e) {
94 "An error occurred while trying to set the read-only variable associated with channel '{}' to '{}'",
95 channelID, command.toString());
99 public void onApplianceExtendedStateChanged(byte[] extendedDeviceState) {
100 if (extendedDeviceState.length != EXTENDED_STATE_SIZE_BYTES) {
101 logger.error("Unexpected size of extended state: {}", extendedDeviceState);
105 BigDecimal kiloWattHoursTenths = BigDecimal
106 .valueOf(extendedDeviceState[POWER_CONSUMPTION_BYTE_POSITION] & 0xff);
107 var kiloWattHours = new QuantityType<>(kiloWattHoursTenths.divide(BigDecimal.valueOf(10)), Units.KILOWATT_HOUR);
108 updateExtendedState(POWER_CONSUMPTION_CHANNEL_ID, kiloWattHours);
110 BigDecimal decilitres = BigDecimal.valueOf(extendedDeviceState[WATER_CONSUMPTION_BYTE_POSITION] & 0xff);
111 var litres = new QuantityType<>(decilitres.divide(BigDecimal.valueOf(10)), Units.LITRE);
112 updateExtendedState(WATER_CONSUMPTION_CHANNEL_ID, litres);