]> git.basschouten.com Git - openhab-addons.git/blob
54145b95c74a733360e2308915ea912e204e0730
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.tado.internal.handler;
14
15 import java.io.IOException;
16 import java.util.concurrent.ScheduledFuture;
17 import java.util.concurrent.TimeUnit;
18
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;
41
42 /**
43  * The {@link TadoHomeHandler} is the bridge of all home-based things.
44  *
45  * @author Dennis Frommknecht - Initial contribution
46  */
47 public class TadoHomeHandler extends BaseBridgeHandler {
48
49     private Logger logger = LoggerFactory.getLogger(TadoHomeHandler.class);
50
51     private TadoHomeConfig configuration;
52     private HomeApi api;
53     private Long homeId;
54
55     private TadoBatteryChecker batteryChecker;
56
57     private ScheduledFuture<?> initializationFuture;
58
59     public TadoHomeHandler(Bridge bridge) {
60         super(bridge);
61         batteryChecker = new TadoBatteryChecker(this);
62     }
63
64     public TemperatureUnit getTemperatureUnit() {
65         String temperatureUnitStr = this.thing.getProperties()
66                 .getOrDefault(TadoBindingConstants.PROPERTY_HOME_TEMPERATURE_UNIT, "CELSIUS");
67         return TemperatureUnit.valueOf(temperatureUnitStr);
68     }
69
70     @Override
71     public void initialize() {
72         configuration = getConfigAs(TadoHomeConfig.class);
73         api = new HomeApiFactory().create(configuration.username, configuration.password);
74
75         if (this.initializationFuture == null || this.initializationFuture.isDone()) {
76             initializationFuture = scheduler.scheduleWithFixedDelay(this::initializeBridgeStatusAndPropertiesIfOffline,
77                     0, 300, TimeUnit.SECONDS);
78         }
79     }
80
81     private void initializeBridgeStatusAndPropertiesIfOffline() {
82         Bridge bridge = getBridge();
83         if (bridge != null && bridge.getStatus() == ThingStatus.ONLINE) {
84             return;
85         }
86
87         try {
88             // Get user info to verify successful authentication and connection to server
89             User user = api.showUser();
90             if (user == null) {
91                 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
92                         "Cannot connect to server. Username and/or password might be invalid");
93                 return;
94             }
95
96             if (user.getHomes().isEmpty()) {
97                 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
98                         "User does not have access to any home");
99                 return;
100             }
101
102             homeId = user.getHomes().get(0).getId().longValue();
103
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());
112             return;
113         }
114
115         updateStatus(ThingStatus.ONLINE);
116     }
117
118     @Override
119     public void dispose() {
120         super.dispose();
121         if (this.initializationFuture != null || !this.initializationFuture.isDone()) {
122             this.initializationFuture.cancel(true);
123             this.initializationFuture = null;
124         }
125     }
126
127     public HomeApi getApi() {
128         return api;
129     }
130
131     public Long getHomeId() {
132         return homeId;
133     }
134
135     public HomeState getHomeState() throws IOException, ApiException {
136         HomeApi api = getApi();
137         return api != null ? api.homeState(getHomeId()) : null;
138     }
139
140     public void updateHomeState() {
141         try {
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);
146         }
147     }
148
149     @Override
150     public void handleCommand(ChannelUID channelUID, Command command) {
151         String id = channelUID.getId();
152
153         if (command == RefreshType.REFRESH) {
154             updateHomeState();
155             return;
156         }
157
158         switch (id) {
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);
164                 try {
165                     api.updatePresenceLock(homeId, presence);
166                 } catch (IOException | ApiException e) {
167                     logger.warn("Error setting home presence: {}", e.getMessage(), e);
168                 }
169
170                 break;
171
172         }
173     }
174
175     public State getBatteryLowAlarm(long zoneId) {
176         return batteryChecker.getBatteryLowAlarm(zoneId);
177     }
178 }