2 * Copyright (c) 2010-2023 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.*;
17 import java.math.BigDecimal;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.miele.internal.api.dto.DeviceProperty;
21 import org.openhab.binding.miele.internal.exceptions.MieleRpcException;
22 import org.openhab.core.i18n.LocaleProvider;
23 import org.openhab.core.i18n.TimeZoneProvider;
24 import org.openhab.core.i18n.TranslationProvider;
25 import org.openhab.core.library.types.OnOffType;
26 import org.openhab.core.library.types.QuantityType;
27 import org.openhab.core.library.unit.Units;
28 import org.openhab.core.thing.ChannelUID;
29 import org.openhab.core.thing.Thing;
30 import org.openhab.core.types.Command;
31 import org.openhab.core.types.RefreshType;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 import com.google.gson.JsonElement;
38 * The {@link DishwasherHandler} is responsible for handling commands,
39 * which are sent to one of the channels
41 * @author Karel Goderis - Initial contribution
42 * @author Kai Kreuzer - fixed handling of REFRESH commands
43 * @author Martin Lepsy - fixed handling of empty JSON results
44 * @author Jacob Laursen - Fixed multicast and protocol support (ZigBee/LAN), added power/water consumption channels
47 public class DishwasherHandler extends MieleApplianceHandler<DishwasherChannelSelector>
48 implements ExtendedDeviceStateListener {
50 private static final int ENERGY_CONSUMPTION_BYTE_POSITION = 16;
51 private static final int WATER_CONSUMPTION_BYTE_POSITION = 18;
52 private static final int EXTENDED_STATE_MIN_SIZE_BYTES = 19;
54 private final Logger logger = LoggerFactory.getLogger(DishwasherHandler.class);
56 public DishwasherHandler(Thing thing, TranslationProvider i18nProvider, LocaleProvider localeProvider,
57 TimeZoneProvider timeZoneProvider) {
58 super(thing, i18nProvider, localeProvider, timeZoneProvider, DishwasherChannelSelector.class,
59 MIELE_DEVICE_CLASS_DISHWASHER);
63 public void handleCommand(ChannelUID channelUID, Command command) {
64 super.handleCommand(channelUID, command);
66 String channelID = channelUID.getId();
67 String applianceId = this.applianceId;
68 if (applianceId == null) {
69 logger.warn("Command '{}' failed, appliance id is unknown", command);
73 DishwasherChannelSelector selector = (DishwasherChannelSelector) getValueSelectorFromChannelID(channelID);
74 JsonElement result = null;
79 MieleBridgeHandler bridgeHandler = getMieleBridgeHandler();
80 if (bridgeHandler == null) {
81 logger.warn("Command '{}' failed, missing bridge handler", command);
84 if (command.equals(OnOffType.ON)) {
85 result = bridgeHandler.invokeOperation(applianceId, modelID, "start");
86 } else if (command.equals(OnOffType.OFF)) {
87 result = bridgeHandler.invokeOperation(applianceId, modelID, "stop");
92 if (!(command instanceof RefreshType)) {
93 logger.debug("{} is a read-only channel that does not accept commands",
94 selector.getChannelID());
99 if (result != null && isResultProcessable(result)) {
100 logger.debug("Result of operation is {}", result.getAsString());
102 } catch (IllegalArgumentException e) {
104 "An error occurred while trying to set the read-only variable associated with channel '{}' to '{}'",
105 channelID, command.toString());
106 } catch (MieleRpcException e) {
107 Throwable cause = e.getCause();
109 logger.warn("An error occurred while trying to invoke operation: {}", e.getMessage());
111 logger.warn("An error occurred while trying to invoke operation: {} -> {}", e.getMessage(),
118 public void onAppliancePropertyChanged(DeviceProperty dp) {
119 super.onAppliancePropertyChanged(dp);
120 updateSwitchStartStopFromState(dp);
124 public void onApplianceExtendedStateChanged(byte[] extendedDeviceState) {
125 if (extendedDeviceState.length < EXTENDED_STATE_MIN_SIZE_BYTES) {
126 logger.debug("Insufficient extended state data to extract consumption values: {}", extendedDeviceState);
130 BigDecimal kiloWattHoursTenths = BigDecimal
131 .valueOf(extendedDeviceState[ENERGY_CONSUMPTION_BYTE_POSITION] & 0xff);
132 var kiloWattHours = new QuantityType<>(kiloWattHoursTenths.divide(BigDecimal.valueOf(10)), Units.KILOWATT_HOUR);
133 updateExtendedState(ENERGY_CONSUMPTION_CHANNEL_ID, kiloWattHours);
135 BigDecimal decilitres = BigDecimal.valueOf(extendedDeviceState[WATER_CONSUMPTION_BYTE_POSITION] & 0xff);
136 var litres = new QuantityType<>(decilitres.divide(BigDecimal.valueOf(10)), Units.LITRE);
137 updateExtendedState(WATER_CONSUMPTION_CHANNEL_ID, litres);