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.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.HomePresence;
26 import org.openhab.binding.tado.internal.api.model.HomeState;
27 import org.openhab.binding.tado.internal.api.model.PresenceState;
28 import org.openhab.binding.tado.internal.api.model.User;
29 import org.openhab.binding.tado.internal.config.TadoHomeConfig;
30 import org.openhab.core.library.types.OnOffType;
31 import org.openhab.core.thing.Bridge;
32 import org.openhab.core.thing.ChannelUID;
33 import org.openhab.core.thing.ThingStatus;
34 import org.openhab.core.thing.ThingStatusDetail;
35 import org.openhab.core.thing.binding.BaseBridgeHandler;
36 import org.openhab.core.types.Command;
37 import org.openhab.core.types.RefreshType;
38 import org.openhab.core.types.State;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
43 * The {@link TadoHomeHandler} is the bridge of all home-based things.
45 * @author Dennis Frommknecht - Initial contribution
47 public class TadoHomeHandler extends BaseBridgeHandler {
49 private Logger logger = LoggerFactory.getLogger(TadoHomeHandler.class);
51 private TadoHomeConfig configuration;
55 private TadoBatteryChecker batteryChecker;
57 private ScheduledFuture<?> initializationFuture;
59 public TadoHomeHandler(Bridge bridge) {
61 batteryChecker = new TadoBatteryChecker(this);
64 public TemperatureUnit getTemperatureUnit() {
65 String temperatureUnitStr = this.thing.getProperties()
66 .getOrDefault(TadoBindingConstants.PROPERTY_HOME_TEMPERATURE_UNIT, "CELSIUS");
67 return TemperatureUnit.valueOf(temperatureUnitStr);
71 public void initialize() {
72 configuration = getConfigAs(TadoHomeConfig.class);
73 api = new HomeApiFactory().create(configuration.username, configuration.password);
75 if (this.initializationFuture == null || this.initializationFuture.isDone()) {
76 initializationFuture = scheduler.scheduleWithFixedDelay(this::initializeBridgeStatusAndPropertiesIfOffline,
77 0, 300, TimeUnit.SECONDS);
81 private void initializeBridgeStatusAndPropertiesIfOffline() {
82 Bridge bridge = getBridge();
83 if (bridge != null && bridge.getStatus() == ThingStatus.ONLINE) {
88 // Get user info to verify successful authentication and connection to server
89 User user = api.showUser();
91 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
92 "Cannot connect to server. Username and/or password might be invalid");
96 if (user.getHomes().isEmpty()) {
97 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
98 "User does not have access to any home");
102 homeId = user.getHomes().get(0).getId().longValue();
104 HomeInfo homeInfo = api.showHome(homeId);
105 TemperatureUnit temperatureUnit = org.openhab.binding.tado.internal.api.model.TemperatureUnit.FAHRENHEIT == homeInfo
106 .getTemperatureUnit() ? TemperatureUnit.FAHRENHEIT : TemperatureUnit.CELSIUS;
107 updateProperty(TadoBindingConstants.PROPERTY_HOME_TEMPERATURE_UNIT, temperatureUnit.name());
108 } catch (IOException | ApiException e) {
109 logger.debug("Error accessing tado server: {}", e.getMessage(), e);
110 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
111 "Could not connect to server due to " + e.getMessage());
115 updateStatus(ThingStatus.ONLINE);
119 public void dispose() {
121 if (this.initializationFuture != null || !this.initializationFuture.isDone()) {
122 this.initializationFuture.cancel(true);
123 this.initializationFuture = null;
127 public HomeApi getApi() {
131 public Long getHomeId() {
135 public HomeState getHomeState() throws IOException, ApiException {
136 HomeApi api = getApi();
137 return api != null ? api.homeState(getHomeId()) : null;
140 public void updateHomeState() {
142 updateState(TadoBindingConstants.CHANNEL_HOME_PRESENCE_MODE,
143 getHomeState().getPresence() == PresenceState.HOME ? OnOffType.ON : OnOffType.OFF);
144 } catch (IOException | ApiException e) {
145 logger.debug("Error accessing tado server: {}", e.getMessage(), e);
150 public void handleCommand(ChannelUID channelUID, Command command) {
151 String id = channelUID.getId();
153 if (command == RefreshType.REFRESH) {
159 case TadoBindingConstants.CHANNEL_HOME_PRESENCE_MODE:
160 HomePresence presence = new HomePresence();
161 presence.setHomePresence(command.toFullString().toUpperCase().equals("ON")
162 || command.toFullString().toUpperCase().equals("HOME") ? PresenceState.HOME
163 : PresenceState.AWAY);
165 api.updatePresenceLock(homeId, presence);
166 } catch (IOException | ApiException e) {
167 logger.warn("Error setting home presence: {}", e.getMessage(), e);
175 public State getBatteryLowAlarm(long zoneId) {
176 return batteryChecker.getBatteryLowAlarm(zoneId);