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.daikin.internal.handler;
15 import java.util.Optional;
16 import java.util.concurrent.ScheduledFuture;
17 import java.util.concurrent.TimeUnit;
19 import javax.measure.quantity.Temperature;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.eclipse.jetty.client.HttpClient;
24 import org.openhab.binding.daikin.internal.DaikinBindingConstants;
25 import org.openhab.binding.daikin.internal.DaikinCommunicationException;
26 import org.openhab.binding.daikin.internal.DaikinCommunicationForbiddenException;
27 import org.openhab.binding.daikin.internal.DaikinDynamicStateDescriptionProvider;
28 import org.openhab.binding.daikin.internal.DaikinWebTargets;
29 import org.openhab.binding.daikin.internal.api.Enums.HomekitMode;
30 import org.openhab.binding.daikin.internal.config.DaikinConfiguration;
31 import org.openhab.core.library.types.DecimalType;
32 import org.openhab.core.library.types.OnOffType;
33 import org.openhab.core.library.types.QuantityType;
34 import org.openhab.core.library.types.StringType;
35 import org.openhab.core.library.unit.SIUnits;
36 import org.openhab.core.thing.ChannelUID;
37 import org.openhab.core.thing.Thing;
38 import org.openhab.core.thing.ThingStatus;
39 import org.openhab.core.thing.ThingStatusDetail;
40 import org.openhab.core.thing.binding.BaseThingHandler;
41 import org.openhab.core.types.Command;
42 import org.openhab.core.types.State;
43 import org.openhab.core.types.UnDefType;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
48 * Base class that handles common tasks with a Daikin air conditioning unit.
50 * @author Tim Waterhouse - Initial Contribution
51 * @author Paul Smedley <paul@smedley.id.au> - Modifications to support Airbase Controllers
52 * @author Jimmy Tanagra - Split handler classes, support Airside and DynamicStateDescription
56 public abstract class DaikinBaseHandler extends BaseThingHandler {
57 private final Logger logger = LoggerFactory.getLogger(DaikinBaseHandler.class);
59 private final @Nullable HttpClient httpClient;
61 private long refreshInterval;
63 protected @Nullable DaikinWebTargets webTargets;
64 private @Nullable ScheduledFuture<?> pollFuture;
65 protected final DaikinDynamicStateDescriptionProvider stateDescriptionProvider;
66 protected @Nullable DaikinConfiguration config;
67 private boolean uuidRegistrationAttempted = false;
69 // Abstract methods to be overridden by specific Daikin implementation class
70 protected abstract void pollStatus() throws DaikinCommunicationException;
72 protected abstract void changePower(boolean power) throws DaikinCommunicationException;
74 protected abstract void changeSetPoint(double newTemperature) throws DaikinCommunicationException;
76 protected abstract void changeMode(String mode) throws DaikinCommunicationException;
78 protected abstract void changeFanSpeed(String fanSpeed) throws DaikinCommunicationException;
80 // Power, Temp, Fan and Mode are handled in this base class. Override this to handle additional channels.
81 protected abstract boolean handleCommandInternal(ChannelUID channelUID, Command command)
82 throws DaikinCommunicationException;
84 protected abstract void registerUuid(@Nullable String key);
86 public DaikinBaseHandler(Thing thing, DaikinDynamicStateDescriptionProvider stateDescriptionProvider,
87 @Nullable HttpClient httpClient) {
89 this.stateDescriptionProvider = stateDescriptionProvider;
90 this.httpClient = httpClient;
94 public void handleCommand(ChannelUID channelUID, Command command) {
96 if (handleCommandInternal(channelUID, command)) {
99 switch (channelUID.getId()) {
100 case DaikinBindingConstants.CHANNEL_AC_POWER:
101 if (command instanceof OnOffType) {
102 changePower(((OnOffType) command).equals(OnOffType.ON));
106 case DaikinBindingConstants.CHANNEL_AC_TEMP:
107 if (changeSetPoint(command)) {
111 case DaikinBindingConstants.CHANNEL_AIRBASE_AC_FAN_SPEED:
112 case DaikinBindingConstants.CHANNEL_AC_FAN_SPEED:
113 if (command instanceof StringType) {
114 changeFanSpeed(((StringType) command).toString());
118 case DaikinBindingConstants.CHANNEL_AC_HOMEKITMODE:
119 if (command instanceof StringType) {
120 changeHomekitMode(command.toString());
124 case DaikinBindingConstants.CHANNEL_AC_MODE:
125 if (command instanceof StringType) {
126 changeMode(((StringType) command).toString());
131 logger.debug("Received command ({}) of wrong type for thing '{}' on channel {}", command,
132 thing.getUID().getAsString(), channelUID.getId());
133 } catch (DaikinCommunicationException e) {
134 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
139 public void initialize() {
140 logger.debug("Initializing Daikin AC Unit");
141 config = getConfigAs(DaikinConfiguration.class);
142 if (config.host == null) {
143 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Host address must be set");
145 if (config.uuid != null) {
146 config.uuid = config.uuid.replaceAll("\\s|-", "");
148 webTargets = new DaikinWebTargets(httpClient, config.host, config.secure, config.uuid);
149 refreshInterval = config.refresh;
155 public void handleRemoval() {
157 super.handleRemoval();
161 public void dispose() {
166 protected void schedulePoll() {
167 if (pollFuture != null) {
168 pollFuture.cancel(false);
170 logger.debug("Scheduling poll for 1s out, then every {} s", refreshInterval);
171 pollFuture = scheduler.scheduleWithFixedDelay(this::poll, 1, refreshInterval, TimeUnit.SECONDS);
174 protected synchronized void stopPoll() {
175 if (pollFuture != null && !pollFuture.isCancelled()) {
176 pollFuture.cancel(true);
181 private synchronized void poll() {
183 logger.trace("Polling for state");
185 if (getThing().getStatus() != ThingStatus.ONLINE) {
186 updateStatus(ThingStatus.ONLINE);
188 } catch (DaikinCommunicationForbiddenException e) {
189 if (!uuidRegistrationAttempted && config.key != null && config.uuid != null) {
190 logger.debug("poll: Attempting to register uuid {} with key {}", config.uuid, config.key);
191 registerUuid(config.key);
192 uuidRegistrationAttempted = true;
194 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
195 "Access denied. Check uuid/key.");
196 logger.warn("{} access denied by adapter. Check uuid/key.", thing.getUID());
198 } catch (DaikinCommunicationException e) {
199 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
203 protected void updateTemperatureChannel(String channel, Optional<Double> maybeTemperature) {
205 maybeTemperature.<State> map(t -> new QuantityType<>(t, SIUnits.CELSIUS)).orElse(UnDefType.UNDEF));
209 * @return true if the command was of an expected type, false otherwise
211 private boolean changeSetPoint(Command command) throws DaikinCommunicationException {
212 double newTemperature;
213 if (command instanceof DecimalType) {
214 newTemperature = ((DecimalType) command).doubleValue();
215 } else if (command instanceof QuantityType) {
216 newTemperature = ((QuantityType<Temperature>) command).toUnit(SIUnits.CELSIUS).doubleValue();
221 // Only half degree increments are allowed, all others are silently rejected by the A/C units
222 newTemperature = Math.round(newTemperature * 2) / 2.0;
223 changeSetPoint(newTemperature);
227 private void changeHomekitMode(String homekitmode) throws DaikinCommunicationException {
228 if (HomekitMode.OFF.getValue().equals(homekitmode)) {
232 if (HomekitMode.AUTO.getValue().equals(homekitmode)) {
234 } else if (HomekitMode.HEAT.getValue().equals(homekitmode)) {
236 } else if (HomekitMode.COOL.getValue().equals(homekitmode)) {