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 java.util.Map.entry;
16 import static org.openhab.binding.miele.internal.MieleBindingConstants.*;
18 import java.lang.reflect.Method;
19 import java.text.SimpleDateFormat;
20 import java.util.Date;
22 import java.util.Map.Entry;
23 import java.util.TimeZone;
25 import org.openhab.binding.miele.internal.handler.MieleBridgeHandler.DeviceMetaData;
26 import org.openhab.core.library.types.DateTimeType;
27 import org.openhab.core.library.types.DecimalType;
28 import org.openhab.core.library.types.OnOffType;
29 import org.openhab.core.library.types.OpenClosedType;
30 import org.openhab.core.library.types.QuantityType;
31 import org.openhab.core.library.types.StringType;
32 import org.openhab.core.types.State;
33 import org.openhab.core.types.Type;
34 import org.openhab.core.types.UnDefType;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 import com.google.gson.JsonElement;
41 * The {@link ApplianceChannelSelector} for dishwashers
43 * @author Karel Goderis - Initial contribution
44 * @author Kai Kreuzer - Changed START_TIME to DateTimeType
45 * @author Jacob Laursen - Added power/water consumption channels, raw channels
47 public enum DishwasherChannelSelector implements ApplianceChannelSelector {
49 PRODUCT_TYPE("productTypeId", "productType", StringType.class, true, false),
50 DEVICE_TYPE("mieleDeviceType", "deviceType", StringType.class, true, false),
51 BRAND_ID("brandId", "brandId", StringType.class, true, false),
52 COMPANY_ID("companyId", "companyId", StringType.class, true, false),
53 STATE_TEXT(STATE_PROPERTY_NAME, STATE_TEXT_CHANNEL_ID, StringType.class, false, false),
54 STATE(null, STATE_CHANNEL_ID, DecimalType.class, false, false),
55 PROGRAM_TEXT(PROGRAM_ID_PROPERTY_NAME, PROGRAM_TEXT_CHANNEL_ID, StringType.class, false, false) {
57 public State getState(String s, DeviceMetaData dmd) {
58 State state = getTextState(s, dmd, programs, MISSING_PROGRAM_TEXT_PREFIX);
62 return super.getState(s, dmd);
65 PROGRAM(null, PROGRAM_CHANNEL_ID, DecimalType.class, false, false),
66 PROGRAM_PHASE_TEXT(PHASE_PROPERTY_NAME, PHASE_TEXT_CHANNEL_ID, StringType.class, false, false) {
68 public State getState(String s, DeviceMetaData dmd) {
69 State state = getTextState(s, dmd, phases, MISSING_PHASE_TEXT_PREFIX);
73 return super.getState(s, dmd);
76 PROGRAM_PHASE(RAW_PHASE_PROPERTY_NAME, PHASE_CHANNEL_ID, DecimalType.class, false, false),
77 START_TIME("startTime", "start", DateTimeType.class, false, false) {
79 public State getState(String s, DeviceMetaData dmd) {
80 Date date = new Date();
81 SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
82 dateFormatter.setTimeZone(TimeZone.getTimeZone("GMT+0"));
84 date.setTime(Long.valueOf(s) * 60000);
85 } catch (Exception e) {
88 return getState(dateFormatter.format(date));
91 DURATION("duration", "duration", DateTimeType.class, false, false) {
93 public State getState(String s, DeviceMetaData dmd) {
94 Date date = new Date();
95 SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
96 dateFormatter.setTimeZone(TimeZone.getTimeZone("GMT+0"));
98 date.setTime(Long.valueOf(s) * 60000);
99 } catch (Exception e) {
102 return getState(dateFormatter.format(date));
105 ELAPSED_TIME("elapsedTime", "elapsed", DateTimeType.class, false, false) {
107 public State getState(String s, DeviceMetaData dmd) {
108 Date date = new Date();
109 SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
110 dateFormatter.setTimeZone(TimeZone.getTimeZone("GMT+0"));
112 date.setTime(Long.valueOf(s) * 60000);
113 } catch (Exception e) {
116 return getState(dateFormatter.format(date));
119 FINISH_TIME("finishTime", "finish", DateTimeType.class, false, false) {
121 public State getState(String s, DeviceMetaData dmd) {
122 Date date = new Date();
123 SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
124 dateFormatter.setTimeZone(TimeZone.getTimeZone("GMT+0"));
126 date.setTime(Long.valueOf(s) * 60000);
127 } catch (Exception e) {
130 return getState(dateFormatter.format(date));
133 DOOR("signalDoor", "door", OpenClosedType.class, false, false) {
135 public State getState(String s, DeviceMetaData dmd) {
136 if ("true".equals(s)) {
137 return getState("OPEN");
140 if ("false".equals(s)) {
141 return getState("CLOSED");
144 return UnDefType.UNDEF;
147 SWITCH(null, "switch", OnOffType.class, false, false),
148 POWER_CONSUMPTION(EXTENDED_DEVICE_STATE_PROPERTY_NAME, POWER_CONSUMPTION_CHANNEL_ID, QuantityType.class, false,
150 WATER_CONSUMPTION(EXTENDED_DEVICE_STATE_PROPERTY_NAME, WATER_CONSUMPTION_CHANNEL_ID, QuantityType.class, false,
153 private final Logger logger = LoggerFactory.getLogger(DishwasherChannelSelector.class);
155 private final static Map<String, String> programs = Map.ofEntries(entry("26", "Pots & Pans"),
156 entry("27", "Clean Machine"), entry("28", "Economy"), entry("30", "Normal"), entry("32", "Sensor Wash"),
157 entry("34", "Energy Saver"), entry("35", "China & Crystal"), entry("36", "Extra Quiet"),
158 entry("37", "SaniWash"), entry("38", "QuickPowerWash"), entry("42", "Tall items"));
160 private final static Map<String, String> phases = Map.ofEntries(entry("2", "Pre-Wash"), entry("3", "Main Wash"),
161 entry("4", "Rinses"), entry("6", "Final rinse"), entry("7", "Drying"), entry("8", "Finished"));
163 private final String mieleID;
164 private final String channelID;
165 private final Class<? extends Type> typeClass;
166 private final boolean isProperty;
167 private final boolean isExtendedState;
169 DishwasherChannelSelector(String propertyID, String channelID, Class<? extends Type> typeClass, boolean isProperty,
170 boolean isExtendedState) {
171 this.mieleID = propertyID;
172 this.channelID = channelID;
173 this.typeClass = typeClass;
174 this.isProperty = isProperty;
175 this.isExtendedState = isExtendedState;
179 public String toString() {
184 public String getMieleID() {
189 public String getChannelID() {
194 public boolean isProperty() {
199 public boolean isExtendedState() {
200 return isExtendedState;
204 public State getState(String s, DeviceMetaData dmd) {
206 String localizedValue = getMieleEnum(s, dmd);
207 if (localizedValue == null) {
208 localizedValue = dmd.LocalizedValue;
210 if (localizedValue == null) {
214 return getState(localizedValue);
220 public State getState(String s) {
222 Method valueOf = typeClass.getMethod("valueOf", String.class);
223 State state = (State) valueOf.invoke(typeClass, s);
227 } catch (Exception e) {
228 logger.error("An exception occurred while converting '{}' into a State", s);
234 public State getTextState(String s, DeviceMetaData dmd, Map<String, String> valueMap, String prefix) {
236 return UnDefType.UNDEF;
239 if (dmd == null || dmd.LocalizedValue == null || dmd.LocalizedValue.startsWith(prefix)) {
240 String text = valueMap.get(s);
242 return getState(text);
244 if (dmd == null || dmd.LocalizedValue == null) {
245 return getState(prefix + s);
252 public String getMieleEnum(String s, DeviceMetaData dmd) {
253 if (dmd.MieleEnum != null) {
254 for (Entry<String, JsonElement> enumEntry : dmd.MieleEnum.entrySet()) {
255 if (enumEntry.getValue().getAsString().trim().equals(s.trim())) {
256 return enumEntry.getKey();