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.homeconnect.internal.handler;
15 import static java.util.Collections.emptyList;
16 import static org.openhab.binding.homeconnect.internal.HomeConnectBindingConstants.*;
17 import static org.openhab.binding.homeconnect.internal.client.model.EventType.*;
18 import static org.openhab.core.library.unit.ImperialUnits.FAHRENHEIT;
19 import static org.openhab.core.library.unit.SIUnits.CELSIUS;
20 import static org.openhab.core.library.unit.Units.*;
21 import static org.openhab.core.thing.ThingStatus.*;
23 import java.time.Duration;
24 import java.util.List;
26 import java.util.Optional;
27 import java.util.concurrent.ConcurrentHashMap;
28 import java.util.concurrent.ScheduledFuture;
29 import java.util.concurrent.TimeUnit;
30 import java.util.concurrent.atomic.AtomicBoolean;
31 import java.util.function.Function;
32 import java.util.stream.Collectors;
34 import javax.measure.UnconvertibleException;
35 import javax.measure.Unit;
36 import javax.measure.quantity.Temperature;
38 import org.eclipse.jdt.annotation.NonNullByDefault;
39 import org.eclipse.jdt.annotation.Nullable;
40 import org.openhab.binding.homeconnect.internal.client.HomeConnectApiClient;
41 import org.openhab.binding.homeconnect.internal.client.HomeConnectEventSourceClient;
42 import org.openhab.binding.homeconnect.internal.client.exception.ApplianceOfflineException;
43 import org.openhab.binding.homeconnect.internal.client.exception.AuthorizationException;
44 import org.openhab.binding.homeconnect.internal.client.exception.CommunicationException;
45 import org.openhab.binding.homeconnect.internal.client.listener.HomeConnectEventListener;
46 import org.openhab.binding.homeconnect.internal.client.model.AvailableProgramOption;
47 import org.openhab.binding.homeconnect.internal.client.model.Data;
48 import org.openhab.binding.homeconnect.internal.client.model.Event;
49 import org.openhab.binding.homeconnect.internal.client.model.HomeAppliance;
50 import org.openhab.binding.homeconnect.internal.client.model.Option;
51 import org.openhab.binding.homeconnect.internal.client.model.Program;
52 import org.openhab.binding.homeconnect.internal.handler.cache.ExpiringStateMap;
53 import org.openhab.binding.homeconnect.internal.type.HomeConnectDynamicStateDescriptionProvider;
54 import org.openhab.core.auth.client.oauth2.OAuthException;
55 import org.openhab.core.library.types.DecimalType;
56 import org.openhab.core.library.types.HSBType;
57 import org.openhab.core.library.types.IncreaseDecreaseType;
58 import org.openhab.core.library.types.OnOffType;
59 import org.openhab.core.library.types.OpenClosedType;
60 import org.openhab.core.library.types.PercentType;
61 import org.openhab.core.library.types.QuantityType;
62 import org.openhab.core.library.types.StringType;
63 import org.openhab.core.library.unit.ImperialUnits;
64 import org.openhab.core.library.unit.SIUnits;
65 import org.openhab.core.thing.Bridge;
66 import org.openhab.core.thing.Channel;
67 import org.openhab.core.thing.ChannelUID;
68 import org.openhab.core.thing.Thing;
69 import org.openhab.core.thing.ThingStatusDetail;
70 import org.openhab.core.thing.ThingStatusInfo;
71 import org.openhab.core.thing.binding.BaseThingHandler;
72 import org.openhab.core.thing.binding.BridgeHandler;
73 import org.openhab.core.types.Command;
74 import org.openhab.core.types.RefreshType;
75 import org.openhab.core.types.StateOption;
76 import org.openhab.core.types.UnDefType;
77 import org.slf4j.Logger;
78 import org.slf4j.LoggerFactory;
81 * The {@link AbstractHomeConnectThingHandler} is responsible for handling commands, which are
82 * sent to one of the channels.
84 * @author Jonas Brüstel - Initial contribution
87 public abstract class AbstractHomeConnectThingHandler extends BaseThingHandler implements HomeConnectEventListener {
89 private static final int CACHE_TTL_SEC = 2;
90 private static final int OFFLINE_MONITOR_1_DELAY_MIN = 30;
91 private static final int OFFLINE_MONITOR_2_DELAY_MIN = 4;
92 private static final int EVENT_LISTENER_CONNECT_RETRY_DELAY_MIN = 10;
94 private @Nullable String operationState;
95 private @Nullable ScheduledFuture<?> reinitializationFuture1;
96 private @Nullable ScheduledFuture<?> reinitializationFuture2;
97 private @Nullable ScheduledFuture<?> reinitializationFuture3;
98 private boolean ignoreEventSourceClosedEvent;
100 private final ConcurrentHashMap<String, EventHandler> eventHandlers;
101 private final ConcurrentHashMap<String, ChannelUpdateHandler> channelUpdateHandlers;
102 private final HomeConnectDynamicStateDescriptionProvider dynamicStateDescriptionProvider;
103 private final ExpiringStateMap expiringStateMap;
104 private final AtomicBoolean accessible;
105 private final Logger logger = LoggerFactory.getLogger(AbstractHomeConnectThingHandler.class);
107 public AbstractHomeConnectThingHandler(Thing thing,
108 HomeConnectDynamicStateDescriptionProvider dynamicStateDescriptionProvider) {
110 eventHandlers = new ConcurrentHashMap<>();
111 channelUpdateHandlers = new ConcurrentHashMap<>();
112 this.dynamicStateDescriptionProvider = dynamicStateDescriptionProvider;
113 expiringStateMap = new ExpiringStateMap(Duration.ofSeconds(CACHE_TTL_SEC));
114 accessible = new AtomicBoolean(false);
116 configureEventHandlers(eventHandlers);
117 configureChannelUpdateHandlers(channelUpdateHandlers);
121 public void initialize() {
122 if (getBridgeHandler().isEmpty()) {
123 updateStatus(OFFLINE, ThingStatusDetail.BRIDGE_UNINITIALIZED);
124 accessible.set(false);
125 } else if (isBridgeOffline()) {
126 updateStatus(OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
127 accessible.set(false);
129 updateStatus(UNKNOWN);
130 scheduler.submit(() -> {
131 refreshThingStatus(); // set ONLINE / OFFLINE
132 updateSelectedProgramStateDescription();
134 registerEventListener();
135 scheduleOfflineMonitor1();
136 scheduleOfflineMonitor2();
142 public void dispose() {
143 stopRetryRegistering();
144 stopOfflineMonitor1();
145 stopOfflineMonitor2();
146 unregisterEventListener(true);
150 public void bridgeStatusChanged(ThingStatusInfo bridgeStatusInfo) {
151 logger.debug("Bridge status changed to {} ({}). haId={}", bridgeStatusInfo, getThingLabel(), getThingHaId());
155 private void reinitialize() {
156 logger.debug("Reinitialize thing handler ({}). haId={}", getThingLabel(), getThingHaId());
157 stopRetryRegistering();
158 stopOfflineMonitor1();
159 stopOfflineMonitor2();
160 unregisterEventListener();
165 * Handles a command for a given channel.
167 * This method is only called, if the thing has been initialized (status ONLINE/OFFLINE/UNKNOWN).
170 * @param channelUID the {@link ChannelUID} of the channel to which the command was sent
171 * @param command the {@link Command}
172 * @param apiClient the {@link HomeConnectApiClient}
173 * @throws CommunicationException communication problem
174 * @throws AuthorizationException authorization problem
175 * @throws ApplianceOfflineException appliance offline
177 protected void handleCommand(ChannelUID channelUID, Command command, HomeConnectApiClient apiClient)
178 throws CommunicationException, AuthorizationException, ApplianceOfflineException {
179 if (command instanceof RefreshType) {
180 updateChannel(channelUID);
181 } else if (command instanceof StringType && CHANNEL_BASIC_ACTIONS_STATE.equals(channelUID.getId())
182 && getBridgeHandler().isPresent()) {
183 updateState(channelUID, new StringType(""));
185 if (COMMAND_START.equalsIgnoreCase(command.toFullString())) {
186 HomeConnectBridgeHandler homeConnectBridgeHandler = getBridgeHandler().get();
187 // workaround for api bug
188 // if simulator, program options have to be passed along with the desired program
189 // if non simulator, some options throw a "SDK.Error.UnsupportedOption" error
190 if (homeConnectBridgeHandler.getConfiguration().isSimulator()) {
191 apiClient.startSelectedProgram(getThingHaId());
193 Program selectedProgram = apiClient.getSelectedProgram(getThingHaId());
194 if (selectedProgram != null) {
195 apiClient.startProgram(getThingHaId(), selectedProgram.getKey());
198 } else if (COMMAND_STOP.equalsIgnoreCase(command.toFullString())) {
199 apiClient.stopProgram(getThingHaId());
200 } else if (COMMAND_SELECTED.equalsIgnoreCase(command.toFullString())) {
201 apiClient.getSelectedProgram(getThingHaId());
203 logger.debug("Start custom program. command={} haId={}", command.toFullString(), getThingHaId());
204 apiClient.startCustomProgram(getThingHaId(), command.toFullString());
206 } else if (command instanceof StringType && CHANNEL_SELECTED_PROGRAM_STATE.equals(channelUID.getId())) {
207 apiClient.setSelectedProgram(getThingHaId(), command.toFullString());
212 public final void handleCommand(ChannelUID channelUID, Command command) {
213 var apiClient = getApiClient();
214 if ((isThingReadyToHandleCommand() || (this instanceof HomeConnectHoodHandler && isBridgeOnline()
215 && isThingAccessibleViaServerSentEvents())) && apiClient.isPresent()) {
216 logger.debug("Handle \"{}\" command ({}). haId={}", command, channelUID.getId(), getThingHaId());
218 handleCommand(channelUID, command, apiClient.get());
219 } catch (ApplianceOfflineException e) {
220 logger.debug("Could not handle command {}. Appliance offline. thing={}, haId={}, error={}",
221 command.toFullString(), getThingLabel(), getThingHaId(), e.getMessage());
222 updateStatus(OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR);
223 resetChannelsOnOfflineEvent();
224 resetProgramStateChannels(true);
225 } catch (CommunicationException e) {
226 logger.debug("Could not handle command {}. API communication problem! error={}, haId={}",
227 command.toFullString(), e.getMessage(), getThingHaId());
228 } catch (AuthorizationException e) {
229 logger.debug("Could not handle command {}. Authorization problem! error={}, haId={}",
230 command.toFullString(), e.getMessage(), getThingHaId());
232 handleAuthenticationError(e);
238 public void onEvent(Event event) {
239 if (DISCONNECTED.equals(event.getType())) {
240 logger.debug("Received DISCONNECTED event. Set {} to OFFLINE. haId={}", getThing().getLabel(),
242 updateStatus(OFFLINE);
243 resetChannelsOnOfflineEvent();
244 resetProgramStateChannels(true);
245 } else if (isThingOnline() && CONNECTED.equals(event.getType())) {
246 logger.debug("Received CONNECTED event. Update power state channel. haId={}", getThingHaId());
247 getThingChannel(CHANNEL_POWER_STATE).ifPresent(c -> updateChannel(c.getUID()));
248 } else if (isThingOffline() && !KEEP_ALIVE.equals(event.getType())) {
249 updateStatus(ONLINE);
250 logger.debug("Set {} to ONLINE and update channels. haId={}", getThing().getLabel(), getThingHaId());
254 String key = event.getKey();
255 if (EVENT_OPERATION_STATE.equals(key)) {
256 operationState = event.getValue() == null ? null : event.getValue();
259 if (key != null && eventHandlers.containsKey(key)) {
260 EventHandler eventHandler = eventHandlers.get(key);
261 if (eventHandler != null) {
262 eventHandler.handle(event);
266 accessible.set(true);
270 public void onClosed() {
271 if (ignoreEventSourceClosedEvent) {
272 logger.debug("Ignoring event source close event. thing={}, haId={}", getThing().getLabel(), getThingHaId());
274 unregisterEventListener();
275 refreshThingStatus();
276 registerEventListener();
281 public void onRateLimitReached() {
282 unregisterEventListener();
285 scheduleRetryRegistering();
289 * Register event listener.
291 protected void registerEventListener() {
292 if (isBridgeOnline() && isThingAccessibleViaServerSentEvents()) {
293 getEventSourceClient().ifPresent(client -> {
295 ignoreEventSourceClosedEvent = false;
296 client.registerEventListener(getThingHaId(), this);
297 } catch (CommunicationException | AuthorizationException e) {
298 logger.warn("Could not open event source connection. thing={}, haId={}, error={}", getThingLabel(),
299 getThingHaId(), e.getMessage());
306 * Unregister event listener.
308 protected void unregisterEventListener() {
309 unregisterEventListener(false);
312 private void unregisterEventListener(boolean immediate) {
313 getEventSourceClient().ifPresent(client -> {
314 ignoreEventSourceClosedEvent = true;
315 client.unregisterEventListener(this, immediate, false);
320 * Get {@link HomeConnectApiClient}.
322 * @return client instance
324 protected Optional<HomeConnectApiClient> getApiClient() {
325 return getBridgeHandler().map(HomeConnectBridgeHandler::getApiClient);
329 * Get {@link HomeConnectEventSourceClient}.
331 * @return client instance if present
333 protected Optional<HomeConnectEventSourceClient> getEventSourceClient() {
334 return getBridgeHandler().map(HomeConnectBridgeHandler::getEventSourceClient);
338 * Update state description of selected program (Fetch programs via API).
340 protected void updateSelectedProgramStateDescription() {
341 if (isBridgeOffline() || isThingOffline()) {
345 Optional<HomeConnectApiClient> apiClient = getApiClient();
346 if (apiClient.isPresent()) {
348 List<StateOption> stateOptions = apiClient.get().getPrograms(getThingHaId()).stream()
349 .map(p -> new StateOption(p.getKey(), mapStringType(p.getKey()))).collect(Collectors.toList());
351 getThingChannel(CHANNEL_SELECTED_PROGRAM_STATE).ifPresent(
352 channel -> dynamicStateDescriptionProvider.setStateOptions(channel.getUID(), stateOptions));
353 } catch (CommunicationException | ApplianceOfflineException | AuthorizationException e) {
354 logger.debug("Could not fetch available programs. thing={}, haId={}, error={}", getThingLabel(),
355 getThingHaId(), e.getMessage());
356 removeSelectedProgramStateDescription();
359 removeSelectedProgramStateDescription();
364 * Remove state description of selected program.
366 protected void removeSelectedProgramStateDescription() {
367 getThingChannel(CHANNEL_SELECTED_PROGRAM_STATE)
368 .ifPresent(channel -> dynamicStateDescriptionProvider.setStateOptions(channel.getUID(), emptyList()));
372 * Is thing ready to process commands. If bridge or thing itself is offline commands will be ignored.
374 * @return true if ready
376 protected boolean isThingReadyToHandleCommand() {
377 if (isBridgeOffline()) {
378 logger.debug("Bridge is OFFLINE. Ignore command. thing={}, haId={}", getThingLabel(), getThingHaId());
382 if (isThingOffline()) {
383 logger.debug("{} is OFFLINE. Ignore command. haId={}", getThing().getLabel(), getThingHaId());
391 * Checks if bridge is online and set.
393 * @return true if online
395 protected boolean isBridgeOnline() {
396 Bridge bridge = getBridge();
397 return bridge != null && ONLINE.equals(bridge.getStatus());
401 * Checks if bridge is offline or not set.
403 * @return true if offline
405 protected boolean isBridgeOffline() {
406 return !isBridgeOnline();
410 * Checks if thing is online.
412 * @return true if online
414 protected boolean isThingOnline() {
415 return ONLINE.equals(getThing().getStatus());
419 * Checks if thing is connected to the cloud and accessible via SSE.
421 * @return true if yes
423 public boolean isThingAccessibleViaServerSentEvents() {
424 return accessible.get();
428 * Checks if thing is offline.
430 * @return true if offline
432 protected boolean isThingOffline() {
433 return !isThingOnline();
437 * Get {@link HomeConnectBridgeHandler}.
439 * @return bridge handler
441 protected Optional<HomeConnectBridgeHandler> getBridgeHandler() {
442 Bridge bridge = getBridge();
443 if (bridge != null) {
444 BridgeHandler bridgeHandler = bridge.getHandler();
445 if (bridgeHandler instanceof HomeConnectBridgeHandler) {
446 return Optional.of((HomeConnectBridgeHandler) bridgeHandler);
449 return Optional.empty();
453 * Get thing channel by given channel id.
455 * @param channelId channel id
458 protected Optional<Channel> getThingChannel(String channelId) {
459 Channel channel = getThing().getChannel(channelId);
460 if (channel == null) {
461 return Optional.empty();
463 return Optional.of(channel);
468 * Configure channel update handlers. Classes which extend {@link AbstractHomeConnectThingHandler} must implement
469 * this class and add handlers.
471 * @param handlers channel update handlers
473 protected abstract void configureChannelUpdateHandlers(final Map<String, ChannelUpdateHandler> handlers);
476 * Configure event handlers. Classes which extend {@link AbstractHomeConnectThingHandler} must implement
477 * this class and add handlers.
479 * @param handlers Server-Sent-Event handlers
481 protected abstract void configureEventHandlers(final Map<String, EventHandler> handlers);
484 * Update all channels via API.
487 protected void updateChannels() {
488 if (isBridgeOffline()) {
489 logger.debug("Bridge handler not found or offline. Stopping update of channels. thing={}, haId={}",
490 getThingLabel(), getThingHaId());
491 } else if (isThingOffline()) {
492 logger.debug("{} offline. Stopping update of channels. haId={}", getThing().getLabel(), getThingHaId());
494 List<Channel> channels = getThing().getChannels();
495 for (Channel channel : channels) {
496 updateChannel(channel.getUID());
502 * Update Channel values via API.
504 * @param channelUID channel UID
506 protected void updateChannel(ChannelUID channelUID) {
507 if (!getApiClient().isPresent()) {
508 logger.error("Cannot update channel. No instance of api client found! thing={}, haId={}", getThingLabel(),
513 if (!isThingReadyToHandleCommand()) {
517 if ((isLinked(channelUID) || CHANNEL_OPERATION_STATE.equals(channelUID.getId())) // always update operation
519 && channelUpdateHandlers.containsKey(channelUID.getId())) {
521 ChannelUpdateHandler channelUpdateHandler = channelUpdateHandlers.get(channelUID.getId());
522 if (channelUpdateHandler != null) {
523 channelUpdateHandler.handle(channelUID, expiringStateMap);
525 } catch (ApplianceOfflineException e) {
527 "API communication problem while trying to update! Appliance offline. thing={}, haId={}, error={}",
528 getThingLabel(), getThingHaId(), e.getMessage());
529 updateStatus(OFFLINE);
530 resetChannelsOnOfflineEvent();
531 resetProgramStateChannels(true);
532 } catch (CommunicationException e) {
533 logger.debug("API communication problem while trying to update! thing={}, haId={}, error={}",
534 getThingLabel(), getThingHaId(), e.getMessage());
535 } catch (AuthorizationException e) {
536 logger.debug("Authentication problem while trying to update! thing={}, haId={}", getThingLabel(),
538 handleAuthenticationError(e);
544 * Reset program related channels.
546 * @param offline true if the device is considered as OFFLINE
548 protected void resetProgramStateChannels(boolean offline) {
549 logger.debug("Resetting active program channel states. thing={}, haId={}", getThingLabel(), getThingHaId());
553 * Reset all channels on OFFLINE event.
555 protected void resetChannelsOnOfflineEvent() {
556 logger.debug("Resetting channel states due to OFFLINE event. thing={}, haId={}", getThingLabel(),
558 getThingChannel(CHANNEL_POWER_STATE).ifPresent(channel -> updateState(channel.getUID(), OnOffType.OFF));
559 getThingChannel(CHANNEL_OPERATION_STATE).ifPresent(channel -> updateState(channel.getUID(), UnDefType.UNDEF));
560 getThingChannel(CHANNEL_DOOR_STATE).ifPresent(channel -> updateState(channel.getUID(), UnDefType.UNDEF));
561 getThingChannel(CHANNEL_LOCAL_CONTROL_ACTIVE_STATE)
562 .ifPresent(channel -> updateState(channel.getUID(), UnDefType.UNDEF));
563 getThingChannel(CHANNEL_REMOTE_CONTROL_ACTIVE_STATE)
564 .ifPresent(channel -> updateState(channel.getUID(), UnDefType.UNDEF));
565 getThingChannel(CHANNEL_REMOTE_START_ALLOWANCE_STATE)
566 .ifPresent(channel -> updateState(channel.getUID(), UnDefType.UNDEF));
567 getThingChannel(CHANNEL_SELECTED_PROGRAM_STATE)
568 .ifPresent(channel -> updateState(channel.getUID(), UnDefType.UNDEF));
572 * Map Home Connect key and value names to label.
573 * e.g. Dishcare.Dishwasher.Program.Eco50 --> Eco50 or BSH.Common.EnumType.OperationState.DelayedStart --> Delayed
577 * @return human readable label
579 protected String mapStringType(String type) {
580 int index = type.lastIndexOf(".");
581 if (index > 0 && type.length() > index) {
582 String sub = type.substring(index + 1);
583 StringBuilder sb = new StringBuilder();
584 for (String word : sub.split("(?<!(^|[A-Z]))(?=[A-Z])|(?<!^)(?=[A-Z][a-z])")) {
588 return sb.toString().trim();
594 * Map Home Connect stage value to label.
595 * e.g. Cooking.Hood.EnumType.IntensiveStage.IntensiveStage1 --> 1
598 * @return human readable label
600 protected String mapStageStringType(String stage) {
603 case STAGE_INTENSIVE_STAGE_OFF:
606 case STAGE_FAN_STAGE_01:
607 case STAGE_INTENSIVE_STAGE_1:
610 case STAGE_FAN_STAGE_02:
611 case STAGE_INTENSIVE_STAGE_2:
614 case STAGE_FAN_STAGE_03:
617 case STAGE_FAN_STAGE_04:
620 case STAGE_FAN_STAGE_05:
624 stage = mapStringType(stage);
631 * Map unit string (returned by home connect api) to Unit
633 * @param unit String eg. "°C"
636 protected Unit<Temperature> mapTemperature(@Nullable String unit) {
639 } else if (unit.endsWith("C")) {
647 * Map hex representation of color to HSB type.
649 * @param colorCode color code e.g. #001122
652 protected HSBType mapColor(String colorCode) {
653 HSBType color = HSBType.WHITE;
655 if (colorCode.length() == 7) {
656 int r = Integer.valueOf(colorCode.substring(1, 3), 16);
657 int g = Integer.valueOf(colorCode.substring(3, 5), 16);
658 int b = Integer.valueOf(colorCode.substring(5, 7), 16);
659 color = HSBType.fromRGB(r, g, b);
665 * Map HSB color type to hex representation.
667 * @param color HSB color
668 * @return color code e.g. #001122
670 protected String mapColor(HSBType color) {
671 String redValue = String.format("%02X", (int) (color.getRed().floatValue() * 2.55));
672 String greenValue = String.format("%02X", (int) (color.getGreen().floatValue() * 2.55));
673 String blueValue = String.format("%02X", (int) (color.getBlue().floatValue() * 2.55));
674 return "#" + redValue + greenValue + blueValue;
678 * Check bridge status and refresh connection status of thing accordingly.
680 protected void refreshThingStatus() {
681 Optional<HomeConnectApiClient> apiClient = getApiClient();
683 apiClient.ifPresent(client -> {
685 HomeAppliance homeAppliance = client.getHomeAppliance(getThingHaId());
686 if (!homeAppliance.isConnected()) {
687 updateStatus(OFFLINE);
689 updateStatus(ONLINE);
691 accessible.set(true);
692 } catch (CommunicationException e) {
694 "Update status to OFFLINE. Home Connect service is not reachable or a problem occurred! thing={}, haId={}, error={}.",
695 getThingLabel(), getThingHaId(), e.getMessage());
696 updateStatus(OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
697 "Home Connect service is not reachable or a problem occurred! (" + e.getMessage() + ").");
698 accessible.set(false);
699 } catch (AuthorizationException e) {
701 "Update status to OFFLINE. Home Connect service is not reachable or a problem occurred! thing={}, haId={}, error={}",
702 getThingLabel(), getThingHaId(), e.getMessage());
703 updateStatus(OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
704 "Home Connect service is not reachable or a problem occurred! (" + e.getMessage() + ").");
705 accessible.set(false);
706 handleAuthenticationError(e);
709 if (apiClient.isEmpty()) {
710 updateStatus(OFFLINE, ThingStatusDetail.BRIDGE_UNINITIALIZED);
711 accessible.set(false);
716 * Get home appliance id of Thing.
718 * @return home appliance id
720 public String getThingHaId() {
721 return getThing().getConfiguration().get(HA_ID).toString();
725 * Returns the human readable label for this thing.
727 * @return the human readable label
729 protected @Nullable String getThingLabel() {
730 return getThing().getLabel();
734 * Handle authentication exception.
736 protected void handleAuthenticationError(AuthorizationException exception) {
737 if (isBridgeOnline()) {
739 "Thing handler threw authentication exception --> clear credential storage thing={}, haId={} error={}",
740 getThingLabel(), getThingHaId(), exception.getMessage());
742 getBridgeHandler().ifPresent(homeConnectBridgeHandler -> {
744 homeConnectBridgeHandler.getOAuthClientService().remove();
745 homeConnectBridgeHandler.reinitialize();
746 } catch (OAuthException e) {
747 // client is already closed --> we can ignore it
754 * Get operation state of device.
756 * @return operation state string
758 protected @Nullable String getOperationState() {
759 return operationState;
762 protected EventHandler defaultElapsedProgramTimeEventHandler() {
763 return event -> getThingChannel(CHANNEL_ELAPSED_PROGRAM_TIME)
764 .ifPresent(channel -> updateState(channel.getUID(), new QuantityType<>(event.getValueAsInt(), SECOND)));
767 protected EventHandler defaultPowerStateEventHandler() {
769 getThingChannel(CHANNEL_POWER_STATE).ifPresent(
770 channel -> updateState(channel.getUID(), OnOffType.from(STATE_POWER_ON.equals(event.getValue()))));
772 if (STATE_POWER_ON.equals(event.getValue())) {
773 getThingChannel(CHANNEL_SELECTED_PROGRAM_STATE).ifPresent(c -> updateChannel(c.getUID()));
775 resetProgramStateChannels(true);
776 getThingChannel(CHANNEL_SELECTED_PROGRAM_STATE)
777 .ifPresent(c -> updateState(c.getUID(), UnDefType.UNDEF));
782 protected EventHandler defaultDoorStateEventHandler() {
783 return event -> getThingChannel(CHANNEL_DOOR_STATE).ifPresent(channel -> updateState(channel.getUID(),
784 STATE_DOOR_OPEN.equals(event.getValue()) ? OpenClosedType.OPEN : OpenClosedType.CLOSED));
787 protected EventHandler defaultOperationStateEventHandler() {
789 String value = event.getValue();
790 getThingChannel(CHANNEL_OPERATION_STATE).ifPresent(channel -> updateState(channel.getUID(),
791 value == null ? UnDefType.UNDEF : new StringType(mapStringType(value))));
793 if (STATE_OPERATION_FINISHED.equals(event.getValue())) {
794 getThingChannel(CHANNEL_PROGRAM_PROGRESS_STATE)
795 .ifPresent(c -> updateState(c.getUID(), new QuantityType<>(100, PERCENT)));
796 getThingChannel(CHANNEL_REMAINING_PROGRAM_TIME_STATE)
797 .ifPresent(c -> updateState(c.getUID(), new QuantityType<>(0, SECOND)));
798 } else if (STATE_OPERATION_RUN.equals(event.getValue())) {
799 getThingChannel(CHANNEL_PROGRAM_PROGRESS_STATE)
800 .ifPresent(c -> updateState(c.getUID(), new QuantityType<>(0, PERCENT)));
801 getThingChannel(CHANNEL_ACTIVE_PROGRAM_STATE).ifPresent(c -> updateChannel(c.getUID()));
802 } else if (STATE_OPERATION_READY.equals(event.getValue())) {
803 resetProgramStateChannels(false);
808 protected EventHandler defaultActiveProgramEventHandler() {
810 String value = event.getValue();
811 getThingChannel(CHANNEL_ACTIVE_PROGRAM_STATE).ifPresent(channel -> updateState(channel.getUID(),
812 value == null ? UnDefType.UNDEF : new StringType(mapStringType(value))));
813 if (event.getValue() == null) {
814 resetProgramStateChannels(false);
819 protected EventHandler defaultEventPresentStateEventHandler(String channelId) {
820 return event -> getThingChannel(channelId).ifPresent(channel -> updateState(channel.getUID(),
821 OnOffType.from(!STATE_EVENT_PRESENT_STATE_OFF.equals(event.getValue()))));
824 protected EventHandler defaultBooleanEventHandler(String channelId) {
825 return event -> getThingChannel(channelId)
826 .ifPresent(channel -> updateState(channel.getUID(), OnOffType.from(event.getValueAsBoolean())));
829 protected EventHandler defaultRemainingProgramTimeEventHandler() {
830 return event -> getThingChannel(CHANNEL_REMAINING_PROGRAM_TIME_STATE)
831 .ifPresent(channel -> updateState(channel.getUID(), new QuantityType<>(event.getValueAsInt(), SECOND)));
834 protected EventHandler defaultSelectedProgramStateEventHandler() {
835 return event -> getThingChannel(CHANNEL_SELECTED_PROGRAM_STATE)
836 .ifPresent(channel -> updateState(channel.getUID(),
837 event.getValue() == null ? UnDefType.UNDEF : new StringType(event.getValue())));
840 protected EventHandler defaultAmbientLightColorStateEventHandler() {
841 return event -> getThingChannel(CHANNEL_AMBIENT_LIGHT_COLOR_STATE)
842 .ifPresent(channel -> updateState(channel.getUID(),
843 event.getValue() == null ? UnDefType.UNDEF : new StringType(event.getValue())));
846 protected EventHandler defaultAmbientLightCustomColorStateEventHandler() {
847 return event -> getThingChannel(CHANNEL_AMBIENT_LIGHT_CUSTOM_COLOR_STATE).ifPresent(channel -> {
848 String value = event.getValue();
850 updateState(channel.getUID(), mapColor(value));
852 updateState(channel.getUID(), UnDefType.UNDEF);
857 protected EventHandler updateProgramOptionsAndSelectedProgramStateEventHandler() {
859 defaultSelectedProgramStateEventHandler().handle(event);
861 // update available program options
863 String programKey = event.getValue();
864 if (programKey != null) {
865 updateProgramOptionsStateDescriptions(programKey, null);
867 } catch (CommunicationException | ApplianceOfflineException | AuthorizationException e) {
868 logger.debug("Could not update program options. {}", e.getMessage());
873 protected EventHandler defaultPercentQuantityTypeEventHandler(String channelId) {
874 return event -> getThingChannel(channelId).ifPresent(
875 channel -> updateState(channel.getUID(), new QuantityType<>(event.getValueAsInt(), PERCENT)));
878 protected EventHandler defaultPercentHandler(String channelId) {
879 return event -> getThingChannel(channelId)
880 .ifPresent(channel -> updateState(channel.getUID(), new PercentType(event.getValueAsInt())));
883 protected ChannelUpdateHandler defaultDoorStateChannelUpdateHandler() {
884 return (channelUID, cache) -> updateState(channelUID, cache.putIfAbsentAndGet(channelUID, () -> {
885 Optional<HomeConnectApiClient> apiClient = getApiClient();
886 if (apiClient.isPresent()) {
887 Data data = apiClient.get().getDoorState(getThingHaId());
888 if (data.getValue() != null) {
889 return STATE_DOOR_OPEN.equals(data.getValue()) ? OpenClosedType.OPEN : OpenClosedType.CLOSED;
891 return UnDefType.UNDEF;
894 return UnDefType.UNDEF;
899 protected ChannelUpdateHandler defaultPowerStateChannelUpdateHandler() {
900 return (channelUID, cache) -> updateState(channelUID, cache.putIfAbsentAndGet(channelUID, () -> {
901 Optional<HomeConnectApiClient> apiClient = getApiClient();
902 if (apiClient.isPresent()) {
903 Data data = apiClient.get().getPowerState(getThingHaId());
904 if (data.getValue() != null) {
905 return OnOffType.from(STATE_POWER_ON.equals(data.getValue()));
907 return UnDefType.UNDEF;
910 return UnDefType.UNDEF;
915 protected ChannelUpdateHandler defaultAmbientLightChannelUpdateHandler() {
916 return (channelUID, cache) -> updateState(channelUID, cache.putIfAbsentAndGet(channelUID, () -> {
917 Optional<HomeConnectApiClient> apiClient = getApiClient();
918 if (apiClient.isPresent()) {
919 Data data = apiClient.get().getAmbientLightState(getThingHaId());
920 if (data.getValue() != null) {
921 boolean enabled = data.getValueAsBoolean();
924 Data brightnessData = apiClient.get().getAmbientLightBrightnessState(getThingHaId());
925 getThingChannel(CHANNEL_AMBIENT_LIGHT_BRIGHTNESS_STATE)
926 .ifPresent(channel -> updateState(channel.getUID(),
927 new PercentType(brightnessData.getValueAsInt())));
930 Data colorData = apiClient.get().getAmbientLightColorState(getThingHaId());
931 getThingChannel(CHANNEL_AMBIENT_LIGHT_COLOR_STATE).ifPresent(
932 channel -> updateState(channel.getUID(), new StringType(colorData.getValue())));
935 Data customColorData = apiClient.get().getAmbientLightCustomColorState(getThingHaId());
936 getThingChannel(CHANNEL_AMBIENT_LIGHT_CUSTOM_COLOR_STATE).ifPresent(channel -> {
937 String value = customColorData.getValue();
939 updateState(channel.getUID(), mapColor(value));
941 updateState(channel.getUID(), UnDefType.UNDEF);
946 return OnOffType.from(enabled);
948 return UnDefType.UNDEF;
951 return UnDefType.UNDEF;
956 protected ChannelUpdateHandler defaultNoOpUpdateHandler() {
957 return (channelUID, cache) -> updateState(channelUID, UnDefType.UNDEF);
960 protected ChannelUpdateHandler defaultOperationStateChannelUpdateHandler() {
961 return (channelUID, cache) -> updateState(channelUID, cache.putIfAbsentAndGet(channelUID, () -> {
962 Optional<HomeConnectApiClient> apiClient = getApiClient();
963 if (apiClient.isPresent()) {
964 Data data = apiClient.get().getOperationState(getThingHaId());
966 String value = data.getValue();
968 operationState = data.getValue();
969 return new StringType(mapStringType(value));
971 operationState = null;
972 return UnDefType.UNDEF;
975 return UnDefType.UNDEF;
980 protected ChannelUpdateHandler defaultRemoteControlActiveStateChannelUpdateHandler() {
981 return (channelUID, cache) -> updateState(channelUID, cache.putIfAbsentAndGet(channelUID, () -> {
982 Optional<HomeConnectApiClient> apiClient = getApiClient();
983 if (apiClient.isPresent()) {
984 return OnOffType.from(apiClient.get().isRemoteControlActive(getThingHaId()));
986 return OnOffType.OFF;
990 protected ChannelUpdateHandler defaultLocalControlActiveStateChannelUpdateHandler() {
991 return (channelUID, cache) -> updateState(channelUID, cache.putIfAbsentAndGet(channelUID, () -> {
992 Optional<HomeConnectApiClient> apiClient = getApiClient();
993 if (apiClient.isPresent()) {
994 return OnOffType.from(apiClient.get().isLocalControlActive(getThingHaId()));
996 return OnOffType.OFF;
1000 protected ChannelUpdateHandler defaultRemoteStartAllowanceChannelUpdateHandler() {
1001 return (channelUID, cache) -> updateState(channelUID, cache.putIfAbsentAndGet(channelUID, () -> {
1002 Optional<HomeConnectApiClient> apiClient = getApiClient();
1003 if (apiClient.isPresent()) {
1004 return OnOffType.from(apiClient.get().isRemoteControlStartAllowed(getThingHaId()));
1006 return OnOffType.OFF;
1010 protected ChannelUpdateHandler defaultSelectedProgramStateUpdateHandler() {
1011 return (channelUID, cache) -> updateState(channelUID, cache.putIfAbsentAndGet(channelUID, () -> {
1012 Optional<HomeConnectApiClient> apiClient = getApiClient();
1013 if (apiClient.isPresent()) {
1014 Program program = apiClient.get().getSelectedProgram(getThingHaId());
1015 if (program != null) {
1016 processProgramOptions(program.getOptions());
1017 return new StringType(program.getKey());
1019 return UnDefType.UNDEF;
1022 return UnDefType.UNDEF;
1026 protected ChannelUpdateHandler updateProgramOptionsStateDescriptionsAndSelectedProgramStateUpdateHandler() {
1027 return (channelUID, cache) -> updateState(channelUID, cache.putIfAbsentAndGet(channelUID, () -> {
1028 Optional<HomeConnectApiClient> apiClient = getApiClient();
1029 if (apiClient.isPresent()) {
1030 Program program = apiClient.get().getSelectedProgram(getThingHaId());
1032 if (program != null) {
1033 updateProgramOptionsStateDescriptions(program.getKey(), program.getOptions());
1034 processProgramOptions(program.getOptions());
1036 return new StringType(program.getKey());
1038 return UnDefType.UNDEF;
1041 return UnDefType.UNDEF;
1045 protected ChannelUpdateHandler defaultActiveProgramStateUpdateHandler() {
1046 return (channelUID, cache) -> updateState(channelUID, cache.putIfAbsentAndGet(channelUID, () -> {
1047 Optional<HomeConnectApiClient> apiClient = getApiClient();
1048 if (apiClient.isPresent()) {
1049 Program program = apiClient.get().getActiveProgram(getThingHaId());
1051 if (program != null) {
1052 processProgramOptions(program.getOptions());
1053 return new StringType(mapStringType(program.getKey()));
1055 resetProgramStateChannels(false);
1056 return UnDefType.UNDEF;
1059 return UnDefType.UNDEF;
1063 protected void handleTemperatureCommand(final ChannelUID channelUID, final Command command,
1064 final HomeConnectApiClient apiClient)
1065 throws CommunicationException, AuthorizationException, ApplianceOfflineException {
1066 if (command instanceof QuantityType) {
1067 QuantityType<?> quantity = (QuantityType<?>) command;
1073 if (quantity.getUnit().equals(SIUnits.CELSIUS) || quantity.getUnit().equals(ImperialUnits.FAHRENHEIT)) {
1074 unit = quantity.getUnit().toString();
1075 value = String.valueOf(quantity.intValue());
1077 logger.debug("Converting target temperature from {}{} to °C value. thing={}, haId={}",
1078 quantity.intValue(), quantity.getUnit().toString(), getThingLabel(), getThingHaId());
1080 var celsius = quantity.toUnit(SIUnits.CELSIUS);
1081 if (celsius == null) {
1082 logger.warn("Converting temperature to celsius failed! quantity={}", quantity);
1085 value = String.valueOf(celsius.intValue());
1087 logger.debug("Converted value {}{}", value, unit);
1090 if (value != null) {
1091 logger.debug("Set temperature to {} {}. thing={}, haId={}", value, unit, getThingLabel(),
1093 switch (channelUID.getId()) {
1094 case CHANNEL_REFRIGERATOR_SETPOINT_TEMPERATURE:
1095 apiClient.setFridgeSetpointTemperature(getThingHaId(), value, unit);
1096 case CHANNEL_FREEZER_SETPOINT_TEMPERATURE:
1097 apiClient.setFreezerSetpointTemperature(getThingHaId(), value, unit);
1099 case CHANNEL_SETPOINT_TEMPERATURE:
1100 apiClient.setProgramOptions(getThingHaId(), OPTION_SETPOINT_TEMPERATURE, value, unit, true,
1104 logger.debug("Unknown channel! Cannot set temperature. channelUID={}", channelUID);
1107 } catch (UnconvertibleException e) {
1108 logger.warn("Could not set temperature! haId={}, error={}", getThingHaId(), e.getMessage());
1113 protected void handleLightCommands(final ChannelUID channelUID, final Command command,
1114 final HomeConnectApiClient apiClient)
1115 throws CommunicationException, AuthorizationException, ApplianceOfflineException {
1116 switch (channelUID.getId()) {
1117 case CHANNEL_FUNCTIONAL_LIGHT_BRIGHTNESS_STATE:
1118 case CHANNEL_AMBIENT_LIGHT_BRIGHTNESS_STATE:
1119 // turn light on if turned off
1120 turnLightOn(channelUID, apiClient);
1122 int newBrightness = BRIGHTNESS_MIN;
1123 if (command instanceof OnOffType) {
1124 newBrightness = command == OnOffType.ON ? BRIGHTNESS_MAX : BRIGHTNESS_MIN;
1125 } else if (command instanceof IncreaseDecreaseType) {
1126 int currentBrightness = getCurrentBrightness(channelUID, apiClient);
1127 if (command.equals(IncreaseDecreaseType.INCREASE)) {
1128 newBrightness = currentBrightness + BRIGHTNESS_DIM_STEP;
1130 newBrightness = currentBrightness - BRIGHTNESS_DIM_STEP;
1132 } else if (command instanceof PercentType) {
1133 newBrightness = (int) Math.floor(((PercentType) command).doubleValue());
1134 } else if (command instanceof DecimalType) {
1135 newBrightness = ((DecimalType) command).intValue();
1138 // check in in range
1139 newBrightness = Math.min(Math.max(newBrightness, BRIGHTNESS_MIN), BRIGHTNESS_MAX);
1141 setLightBrightness(channelUID, apiClient, newBrightness);
1143 case CHANNEL_FUNCTIONAL_LIGHT_STATE:
1144 if (command instanceof OnOffType) {
1145 apiClient.setFunctionalLightState(getThingHaId(), OnOffType.ON.equals(command));
1148 case CHANNEL_AMBIENT_LIGHT_STATE:
1149 if (command instanceof OnOffType) {
1150 apiClient.setAmbientLightState(getThingHaId(), OnOffType.ON.equals(command));
1153 case CHANNEL_AMBIENT_LIGHT_COLOR_STATE:
1154 if (command instanceof StringType) {
1155 turnLightOn(channelUID, apiClient);
1156 apiClient.setAmbientLightColorState(getThingHaId(), command.toFullString());
1159 case CHANNEL_AMBIENT_LIGHT_CUSTOM_COLOR_STATE:
1160 turnLightOn(channelUID, apiClient);
1162 // make sure 'custom color' is set as color
1163 Data ambientLightColorState = apiClient.getAmbientLightColorState(getThingHaId());
1164 if (!STATE_AMBIENT_LIGHT_COLOR_CUSTOM_COLOR.equals(ambientLightColorState.getValue())) {
1165 apiClient.setAmbientLightColorState(getThingHaId(), STATE_AMBIENT_LIGHT_COLOR_CUSTOM_COLOR);
1168 if (command instanceof HSBType) {
1169 apiClient.setAmbientLightCustomColorState(getThingHaId(), mapColor((HSBType) command));
1170 } else if (command instanceof StringType) {
1171 apiClient.setAmbientLightCustomColorState(getThingHaId(), command.toFullString());
1177 protected void handlePowerCommand(final ChannelUID channelUID, final Command command,
1178 final HomeConnectApiClient apiClient, String stateNotOn)
1179 throws CommunicationException, AuthorizationException, ApplianceOfflineException {
1180 if (command instanceof OnOffType && CHANNEL_POWER_STATE.equals(channelUID.getId())) {
1181 apiClient.setPowerState(getThingHaId(), OnOffType.ON.equals(command) ? STATE_POWER_ON : stateNotOn);
1185 private int getCurrentBrightness(final ChannelUID channelUID, final HomeConnectApiClient apiClient)
1186 throws CommunicationException, AuthorizationException, ApplianceOfflineException {
1187 String id = channelUID.getId();
1188 if (CHANNEL_FUNCTIONAL_LIGHT_BRIGHTNESS_STATE.equals(id)) {
1189 return apiClient.getFunctionalLightBrightnessState(getThingHaId()).getValueAsInt();
1191 return apiClient.getAmbientLightBrightnessState(getThingHaId()).getValueAsInt();
1195 private void setLightBrightness(final ChannelUID channelUID, final HomeConnectApiClient apiClient, int value)
1196 throws CommunicationException, AuthorizationException, ApplianceOfflineException {
1197 switch (channelUID.getId()) {
1198 case CHANNEL_FUNCTIONAL_LIGHT_BRIGHTNESS_STATE:
1199 apiClient.setFunctionalLightBrightnessState(getThingHaId(), value);
1201 case CHANNEL_AMBIENT_LIGHT_BRIGHTNESS_STATE:
1202 apiClient.setAmbientLightBrightnessState(getThingHaId(), value);
1207 private void turnLightOn(final ChannelUID channelUID, final HomeConnectApiClient apiClient)
1208 throws CommunicationException, AuthorizationException, ApplianceOfflineException {
1209 switch (channelUID.getId()) {
1210 case CHANNEL_FUNCTIONAL_LIGHT_BRIGHTNESS_STATE:
1211 Data functionalLightState = apiClient.getFunctionalLightState(getThingHaId());
1212 if (!functionalLightState.getValueAsBoolean()) {
1213 apiClient.setFunctionalLightState(getThingHaId(), true);
1216 case CHANNEL_AMBIENT_LIGHT_CUSTOM_COLOR_STATE:
1217 case CHANNEL_AMBIENT_LIGHT_COLOR_STATE:
1218 case CHANNEL_AMBIENT_LIGHT_BRIGHTNESS_STATE:
1219 Data ambientLightState = apiClient.getAmbientLightState(getThingHaId());
1220 if (!ambientLightState.getValueAsBoolean()) {
1221 apiClient.setAmbientLightState(getThingHaId(), true);
1227 protected void processProgramOptions(List<Option> options) {
1228 options.forEach(option -> {
1229 String key = option.getKey();
1232 case OPTION_WASHER_TEMPERATURE:
1233 getThingChannel(CHANNEL_WASHER_TEMPERATURE)
1234 .ifPresent(channel -> updateState(channel.getUID(), new StringType(option.getValue())));
1236 case OPTION_WASHER_SPIN_SPEED:
1237 getThingChannel(CHANNEL_WASHER_SPIN_SPEED)
1238 .ifPresent(channel -> updateState(channel.getUID(), new StringType(option.getValue())));
1240 case OPTION_WASHER_IDOS_1_DOSING_LEVEL:
1241 getThingChannel(CHANNEL_WASHER_IDOS1)
1242 .ifPresent(channel -> updateState(channel.getUID(), new StringType(option.getValue())));
1244 case OPTION_WASHER_IDOS_2_DOSING_LEVEL:
1245 getThingChannel(CHANNEL_WASHER_IDOS2)
1246 .ifPresent(channel -> updateState(channel.getUID(), new StringType(option.getValue())));
1248 case OPTION_DRYER_DRYING_TARGET:
1249 getThingChannel(CHANNEL_DRYER_DRYING_TARGET)
1250 .ifPresent(channel -> updateState(channel.getUID(), new StringType(option.getValue())));
1252 case OPTION_HOOD_INTENSIVE_LEVEL:
1253 String hoodIntensiveLevelValue = option.getValue();
1254 if (hoodIntensiveLevelValue != null) {
1255 getThingChannel(CHANNEL_HOOD_INTENSIVE_LEVEL)
1256 .ifPresent(channel -> updateState(channel.getUID(),
1257 new StringType(mapStageStringType(hoodIntensiveLevelValue))));
1260 case OPTION_HOOD_VENTING_LEVEL:
1261 String hoodVentingLevel = option.getValue();
1262 if (hoodVentingLevel != null) {
1263 getThingChannel(CHANNEL_HOOD_VENTING_LEVEL)
1264 .ifPresent(channel -> updateState(channel.getUID(),
1265 new StringType(mapStageStringType(hoodVentingLevel))));
1268 case OPTION_SETPOINT_TEMPERATURE:
1269 getThingChannel(CHANNEL_SETPOINT_TEMPERATURE).ifPresent(channel -> updateState(channel.getUID(),
1270 new QuantityType<>(option.getValueAsInt(), mapTemperature(option.getUnit()))));
1272 case OPTION_DURATION:
1273 getThingChannel(CHANNEL_DURATION).ifPresent(channel -> updateState(channel.getUID(),
1274 new QuantityType<>(option.getValueAsInt(), SECOND)));
1276 case OPTION_FINISH_IN_RELATIVE:
1277 case OPTION_REMAINING_PROGRAM_TIME:
1278 getThingChannel(CHANNEL_REMAINING_PROGRAM_TIME_STATE)
1279 .ifPresent(channel -> updateState(channel.getUID(),
1280 new QuantityType<>(option.getValueAsInt(), SECOND)));
1282 case OPTION_ELAPSED_PROGRAM_TIME:
1283 getThingChannel(CHANNEL_ELAPSED_PROGRAM_TIME).ifPresent(channel -> updateState(channel.getUID(),
1284 new QuantityType<>(option.getValueAsInt(), SECOND)));
1286 case OPTION_PROGRAM_PROGRESS:
1287 getThingChannel(CHANNEL_PROGRAM_PROGRESS_STATE)
1288 .ifPresent(channel -> updateState(channel.getUID(),
1289 new QuantityType<>(option.getValueAsInt(), PERCENT)));
1296 protected String convertWasherTemperature(String value) {
1297 if (value.startsWith("LaundryCare.Washer.EnumType.Temperature.GC")) {
1298 return value.replace("LaundryCare.Washer.EnumType.Temperature.GC", "") + "°C";
1301 if (value.startsWith("LaundryCare.Washer.EnumType.Temperature.Ul")) {
1302 return mapStringType(value.replace("LaundryCare.Washer.EnumType.Temperature.Ul", ""));
1305 return mapStringType(value);
1308 protected String convertWasherSpinSpeed(String value) {
1309 if (value.startsWith("LaundryCare.Washer.EnumType.SpinSpeed.RPM")) {
1310 return value.replace("LaundryCare.Washer.EnumType.SpinSpeed.RPM", "") + " RPM";
1313 if (value.startsWith("LaundryCare.Washer.EnumType.SpinSpeed.Ul")) {
1314 return value.replace("LaundryCare.Washer.EnumType.SpinSpeed.Ul", "");
1317 return mapStringType(value);
1320 protected void updateProgramOptionsStateDescriptions(String programKey, @Nullable List<Option> optionsValues)
1321 throws CommunicationException, AuthorizationException, ApplianceOfflineException {
1322 Optional<HomeConnectApiClient> apiClient = getApiClient();
1323 if (apiClient.isPresent()) {
1324 List<AvailableProgramOption> availableProgramOptions = apiClient.get().getProgramOptions(getThingHaId(),
1327 Optional<Channel> channelSpinSpeed = getThingChannel(CHANNEL_WASHER_SPIN_SPEED);
1328 Optional<Channel> channelTemperature = getThingChannel(CHANNEL_WASHER_TEMPERATURE);
1329 Optional<Channel> channelDryingTarget = getThingChannel(CHANNEL_DRYER_DRYING_TARGET);
1331 if (availableProgramOptions.isEmpty()) {
1332 List<Option> options;
1333 if (optionsValues != null) {
1334 options = optionsValues;
1335 } else if (channelSpinSpeed.isPresent() || channelTemperature.isPresent()
1336 || channelDryingTarget.isPresent()) {
1337 Program program = apiClient.get().getSelectedProgram(getThingHaId());
1338 options = program != null ? program.getOptions() : emptyList();
1340 options = emptyList();
1343 channelSpinSpeed.ifPresent(channel -> dynamicStateDescriptionProvider.setStateOptions(channel.getUID(),
1345 .filter(option -> option.getKey() != null && option.getValue() != null
1346 && OPTION_WASHER_SPIN_SPEED.equals(option.getKey()))
1347 .map(option -> option.getValue())
1348 .map(value -> new StateOption(value == null ? "" : value,
1349 convertWasherSpinSpeed(value == null ? "" : value)))
1350 .collect(Collectors.toList())));
1352 .ifPresent(channel -> dynamicStateDescriptionProvider.setStateOptions(channel.getUID(),
1354 .filter(option -> option.getKey() != null && option.getValue() != null
1355 && OPTION_WASHER_TEMPERATURE.equals(option.getKey()))
1356 .map(option -> option.getValue())
1357 .map(value -> new StateOption(value == null ? "" : value,
1358 convertWasherTemperature(value == null ? "" : value)))
1359 .collect(Collectors.toList())));
1361 .ifPresent(channel -> dynamicStateDescriptionProvider.setStateOptions(channel.getUID(),
1363 .filter(option -> option.getKey() != null && option.getValue() != null
1364 && OPTION_DRYER_DRYING_TARGET.equals(option.getKey()))
1365 .map(option -> option.getValue())
1366 .map(value -> new StateOption(value == null ? "" : value,
1367 mapStringType(value == null ? "" : value)))
1368 .collect(Collectors.toList())));
1371 availableProgramOptions.forEach(option -> {
1372 switch (option.getKey()) {
1373 case OPTION_WASHER_SPIN_SPEED: {
1375 .ifPresent(channel -> dynamicStateDescriptionProvider.setStateOptions(channel.getUID(),
1376 createStateOptions(option, this::convertWasherSpinSpeed)));
1379 case OPTION_WASHER_TEMPERATURE: {
1381 .ifPresent(channel -> dynamicStateDescriptionProvider.setStateOptions(channel.getUID(),
1382 createStateOptions(option, this::convertWasherTemperature)));
1385 case OPTION_DRYER_DRYING_TARGET: {
1386 channelDryingTarget.ifPresent(channel -> dynamicStateDescriptionProvider
1387 .setStateOptions(channel.getUID(), createStateOptions(option, this::mapStringType)));
1395 protected HomeConnectDynamicStateDescriptionProvider getDynamicStateDescriptionProvider() {
1396 return dynamicStateDescriptionProvider;
1399 private List<StateOption> createStateOptions(AvailableProgramOption option,
1400 Function<String, String> stateConverter) {
1401 return option.getAllowedValues().stream().map(av -> new StateOption(av, stateConverter.apply(av)))
1402 .collect(Collectors.toList());
1405 private synchronized void scheduleOfflineMonitor1() {
1406 this.reinitializationFuture1 = scheduler.schedule(() -> {
1407 if (isBridgeOnline() && isThingOffline()) {
1408 logger.debug("Offline monitor 1: Check if thing is ONLINE. thing={}, haId={}", getThingLabel(),
1410 refreshThingStatus();
1411 if (isThingOnline()) {
1412 logger.debug("Offline monitor 1: Thing status changed to ONLINE. thing={}, haId={}",
1413 getThingLabel(), getThingHaId());
1416 scheduleOfflineMonitor1();
1419 scheduleOfflineMonitor1();
1421 }, AbstractHomeConnectThingHandler.OFFLINE_MONITOR_1_DELAY_MIN, TimeUnit.MINUTES);
1424 private synchronized void stopOfflineMonitor1() {
1425 ScheduledFuture<?> reinitializationFuture = this.reinitializationFuture1;
1426 if (reinitializationFuture != null) {
1427 reinitializationFuture.cancel(false);
1428 this.reinitializationFuture1 = null;
1432 private synchronized void scheduleOfflineMonitor2() {
1433 this.reinitializationFuture2 = scheduler.schedule(() -> {
1434 if (isBridgeOnline() && !accessible.get()) {
1435 logger.debug("Offline monitor 2: Check if thing is ONLINE. thing={}, haId={}", getThingLabel(),
1437 refreshThingStatus();
1438 if (isThingOnline()) {
1439 logger.debug("Offline monitor 2: Thing status changed to ONLINE. thing={}, haId={}",
1440 getThingLabel(), getThingHaId());
1443 scheduleOfflineMonitor2();
1446 scheduleOfflineMonitor2();
1448 }, AbstractHomeConnectThingHandler.OFFLINE_MONITOR_2_DELAY_MIN, TimeUnit.MINUTES);
1451 private synchronized void stopOfflineMonitor2() {
1452 ScheduledFuture<?> reinitializationFuture = this.reinitializationFuture2;
1453 if (reinitializationFuture != null) {
1454 reinitializationFuture.cancel(false);
1455 this.reinitializationFuture2 = null;
1459 private synchronized void scheduleRetryRegistering() {
1460 this.reinitializationFuture3 = scheduler.schedule(() -> {
1461 logger.debug("Try to register event listener again. haId={}", getThingHaId());
1462 unregisterEventListener();
1463 registerEventListener();
1464 }, AbstractHomeConnectThingHandler.EVENT_LISTENER_CONNECT_RETRY_DELAY_MIN, TimeUnit.MINUTES);
1467 private synchronized void stopRetryRegistering() {
1468 ScheduledFuture<?> reinitializationFuture = this.reinitializationFuture3;
1469 if (reinitializationFuture != null) {
1470 reinitializationFuture.cancel(true);
1471 this.reinitializationFuture3 = null;