2 * Copyright (c) 2010-2022 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.TimeZone;
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.openhab.binding.miele.internal.DeviceUtil;
27 import org.openhab.binding.miele.internal.MieleTranslationProvider;
28 import org.openhab.binding.miele.internal.api.dto.DeviceMetaData;
29 import org.openhab.core.library.types.DateTimeType;
30 import org.openhab.core.library.types.DecimalType;
31 import org.openhab.core.library.types.OnOffType;
32 import org.openhab.core.library.types.OpenClosedType;
33 import org.openhab.core.library.types.StringType;
34 import org.openhab.core.types.State;
35 import org.openhab.core.types.Type;
36 import org.openhab.core.types.UnDefType;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
41 * The {@link ApplianceChannelSelector} for tumble dryers
43 * @author Karel Goderis - Initial contribution
44 * @author Kai Kreuzer - Changed START_TIME to DateTimeType
45 * @author Jacob Laursen - Added raw channels
48 public enum TumbleDryerChannelSelector implements ApplianceChannelSelector {
50 PRODUCT_TYPE("productTypeId", "productType", StringType.class, true),
51 DEVICE_TYPE("mieleDeviceType", "deviceType", StringType.class, true),
52 STATE_TEXT(STATE_PROPERTY_NAME, STATE_TEXT_CHANNEL_ID, StringType.class, false) {
54 public State getState(String s, @Nullable DeviceMetaData dmd, MieleTranslationProvider translationProvider) {
55 return DeviceUtil.getStateTextState(s, dmd, translationProvider);
58 STATE("", STATE_CHANNEL_ID, DecimalType.class, false),
59 PROGRAM_TEXT(PROGRAM_ID_PROPERTY_NAME, PROGRAM_TEXT_CHANNEL_ID, StringType.class, false) {
61 public State getState(String s, @Nullable DeviceMetaData dmd, MieleTranslationProvider translationProvider) {
62 return DeviceUtil.getTextState(s, dmd, translationProvider, PROGRAMS, MISSING_PROGRAM_TEXT_PREFIX,
63 MIELE_TUMBLE_DRYER_TEXT_PREFIX);
66 PROGRAM("", PROGRAM_CHANNEL_ID, DecimalType.class, false),
67 PROGRAMTYPE("programType", "type", StringType.class, false),
68 PROGRAM_PHASE_TEXT(PHASE_PROPERTY_NAME, PHASE_TEXT_CHANNEL_ID, StringType.class, false) {
70 public State getState(String s, @Nullable DeviceMetaData dmd, MieleTranslationProvider translationProvider) {
71 return DeviceUtil.getTextState(s, dmd, translationProvider, PHASES, MISSING_PHASE_TEXT_PREFIX,
72 MIELE_TUMBLE_DRYER_TEXT_PREFIX);
75 PROGRAM_PHASE(RAW_PHASE_PROPERTY_NAME, PHASE_CHANNEL_ID, DecimalType.class, false),
76 START_TIME("", START_CHANNEL_ID, DateTimeType.class, false),
77 END_TIME("", END_CHANNEL_ID, DateTimeType.class, false),
78 DURATION("duration", "duration", DateTimeType.class, false) {
80 public State getState(String s, @Nullable DeviceMetaData dmd, MieleTranslationProvider translationProvider) {
81 Date date = new Date();
82 SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
83 dateFormatter.setTimeZone(TimeZone.getTimeZone("GMT+0"));
85 date.setTime(Long.valueOf(s) * 60000);
86 } catch (Exception e) {
89 return getState(dateFormatter.format(date));
92 ELAPSED_TIME("elapsedTime", "elapsed", DateTimeType.class, false) {
94 public State getState(String s, @Nullable DeviceMetaData dmd, MieleTranslationProvider translationProvider) {
95 Date date = new Date();
96 SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
97 dateFormatter.setTimeZone(TimeZone.getTimeZone("GMT+0"));
99 date.setTime(Long.valueOf(s) * 60000);
100 } catch (Exception e) {
103 return getState(dateFormatter.format(date));
106 FINISH_TIME("", FINISH_CHANNEL_ID, DateTimeType.class, false),
107 DRYING_STEP("dryingStep", "step", DecimalType.class, false) {
109 public State getState(String s, @Nullable DeviceMetaData dmd, MieleTranslationProvider translationProvider) {
113 DOOR("signalDoor", "door", OpenClosedType.class, false) {
116 public State getState(String s, @Nullable DeviceMetaData dmd, MieleTranslationProvider translationProvider) {
117 if ("true".equals(s)) {
118 return getState("OPEN");
121 if ("false".equals(s)) {
122 return getState("CLOSED");
125 return UnDefType.UNDEF;
128 SWITCH("", "switch", OnOffType.class, false);
130 private final Logger logger = LoggerFactory.getLogger(TumbleDryerChannelSelector.class);
132 private static final Map<String, String> PROGRAMS = Map.ofEntries(entry("10", "automatic-plus"),
133 entry("20", "cottons"), entry("23", "cottons-hygiene"), entry("30", "minimum-iron"),
134 entry("31", "gentle-minimum-iron"), entry("40", "woollens-handcare"), entry("50", "delicates"),
135 entry("60", "warm-air"), entry("70", "cool-air"), entry("80", "express"), entry("90", "cottons-eco"),
136 entry("100", "gentle-smoothing"), entry("120", "proofing"), entry("130", "denim"),
137 entry("131", "gentle-denim"), entry("140", "shirts"), entry("141", "gentle-shirts"),
138 entry("150", "sportswear"), entry("160", "outerwear"), entry("170", "silks-handcare"),
139 entry("190", "standard-pillows"), entry("220", "basket-programme"), entry("240", "smoothing"),
140 entry("65000", "cottons-auto-load-control"), entry("65001", "minimum-iron-auto-load-control"));
142 private static final Map<String, String> PHASES = Map.ofEntries(entry("1", "programme-running"),
143 entry("2", "drying"), entry("3", "drying-machine-iron"), entry("4", "drying-hand-iron"),
144 entry("5", "drying-normal"), entry("6", "drying-normal-plus"), entry("7", "cooling-down"),
145 entry("8", "drying-hand-iron"), entry("10", "finished"));
147 private final String mieleID;
148 private final String channelID;
149 private final Class<? extends Type> typeClass;
150 private final boolean isProperty;
152 TumbleDryerChannelSelector(String propertyID, String channelID, Class<? extends Type> typeClass,
153 boolean isProperty) {
154 this.mieleID = propertyID;
155 this.channelID = channelID;
156 this.typeClass = typeClass;
157 this.isProperty = isProperty;
161 public String toString() {
166 public String getMieleID() {
171 public String getChannelID() {
176 public boolean isProperty() {
181 public boolean isExtendedState() {
186 public State getState(String s, @Nullable DeviceMetaData dmd, MieleTranslationProvider translationProvider) {
187 return this.getState(s, dmd);
191 public State getState(String s, @Nullable DeviceMetaData dmd) {
193 String localizedValue = dmd.getMieleEnum(s);
194 if (localizedValue == null) {
195 localizedValue = dmd.LocalizedValue;
197 if (localizedValue == null) {
201 return getState(localizedValue);
207 public State getState(String s) {
209 Method valueOf = typeClass.getMethod("valueOf", String.class);
210 State state = (State) valueOf.invoke(typeClass, s);
214 } catch (Exception e) {
215 logger.warn("An exception occurred while converting '{}' into a State", s);
218 return UnDefType.UNDEF;