2 * Copyright (c) 2010-2020 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.tado.internal.handler;
15 import java.io.IOException;
16 import java.util.concurrent.ScheduledFuture;
17 import java.util.concurrent.TimeUnit;
19 import org.openhab.binding.tado.internal.TadoBindingConstants;
20 import org.openhab.binding.tado.internal.TadoBindingConstants.TemperatureUnit;
21 import org.openhab.binding.tado.internal.api.ApiException;
22 import org.openhab.binding.tado.internal.api.HomeApiFactory;
23 import org.openhab.binding.tado.internal.api.client.HomeApi;
24 import org.openhab.binding.tado.internal.api.model.HomeInfo;
25 import org.openhab.binding.tado.internal.api.model.User;
26 import org.openhab.binding.tado.internal.config.TadoHomeConfig;
27 import org.openhab.core.thing.Bridge;
28 import org.openhab.core.thing.ChannelUID;
29 import org.openhab.core.thing.ThingStatus;
30 import org.openhab.core.thing.ThingStatusDetail;
31 import org.openhab.core.thing.binding.BaseBridgeHandler;
32 import org.openhab.core.types.Command;
33 import org.openhab.core.types.State;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * The {@link TadoHomeHandler} is the bridge of all home-based things.
40 * @author Dennis Frommknecht - Initial contribution
42 public class TadoHomeHandler extends BaseBridgeHandler {
44 private Logger logger = LoggerFactory.getLogger(TadoHomeHandler.class);
46 private TadoHomeConfig configuration;
50 private TadoBatteryChecker batteryChecker;
52 private ScheduledFuture<?> initializationFuture;
54 public TadoHomeHandler(Bridge bridge) {
56 batteryChecker = new TadoBatteryChecker(this);
59 public TemperatureUnit getTemperatureUnit() {
60 String temperatureUnitStr = this.thing.getProperties().get(TadoBindingConstants.PROPERTY_HOME_TEMPERATURE_UNIT);
61 return TemperatureUnit.valueOf(temperatureUnitStr);
65 public void initialize() {
66 configuration = getConfigAs(TadoHomeConfig.class);
67 api = new HomeApiFactory().create(configuration.username, configuration.password);
69 if (this.initializationFuture == null || this.initializationFuture.isDone()) {
70 initializationFuture = scheduler.scheduleWithFixedDelay(this::initializeBridgeStatusAndPropertiesIfOffline,
71 0, 300, TimeUnit.SECONDS);
75 private void initializeBridgeStatusAndPropertiesIfOffline() {
76 Bridge bridge = getBridge();
77 if (bridge != null && bridge.getStatus() == ThingStatus.ONLINE) {
82 // Get user info to verify successful authentication and connection to server
83 User user = api.showUser();
85 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
86 "Cannot connect to server. Username and/or password might be invalid");
90 if (user.getHomes().isEmpty()) {
91 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
92 "User does not have access to any home");
96 homeId = user.getHomes().get(0).getId().longValue();
98 HomeInfo homeInfo = api.showHome(homeId);
99 TemperatureUnit temperatureUnit = org.openhab.binding.tado.internal.api.model.TemperatureUnit.FAHRENHEIT == homeInfo
100 .getTemperatureUnit() ? TemperatureUnit.FAHRENHEIT : TemperatureUnit.CELSIUS;
101 updateProperty(TadoBindingConstants.PROPERTY_HOME_TEMPERATURE_UNIT, temperatureUnit.name());
102 } catch (IOException | ApiException e) {
103 logger.debug("Error accessing tado server: {}", e.getMessage(), e);
104 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
105 "Could not connect to server due to " + e.getMessage());
109 updateStatus(ThingStatus.ONLINE);
113 public void dispose() {
115 if (this.initializationFuture != null || !this.initializationFuture.isDone()) {
116 this.initializationFuture.cancel(true);
117 this.initializationFuture = null;
121 public HomeApi getApi() {
125 public Long getHomeId() {
130 public void handleCommand(ChannelUID channelUID, Command command) {
131 // Nothing to do for a bridge
134 public State getBatteryLowAlarm(long zoneId) {
135 return batteryChecker.getBatteryLowAlarm(zoneId);