]> git.basschouten.com Git - openhab-addons.git/blob
555d762caa8010324425d6a09a7e472f71bf6fa4
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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().get(TadoBindingConstants.PROPERTY_HOME_TEMPERATURE_UNIT);
66         return TemperatureUnit.valueOf(temperatureUnitStr);
67     }
68
69     @Override
70     public void initialize() {
71         configuration = getConfigAs(TadoHomeConfig.class);
72         api = new HomeApiFactory().create(configuration.username, configuration.password);
73
74         if (this.initializationFuture == null || this.initializationFuture.isDone()) {
75             initializationFuture = scheduler.scheduleWithFixedDelay(this::initializeBridgeStatusAndPropertiesIfOffline,
76                     0, 300, TimeUnit.SECONDS);
77         }
78     }
79
80     private void initializeBridgeStatusAndPropertiesIfOffline() {
81         Bridge bridge = getBridge();
82         if (bridge != null && bridge.getStatus() == ThingStatus.ONLINE) {
83             return;
84         }
85
86         try {
87             // Get user info to verify successful authentication and connection to server
88             User user = api.showUser();
89             if (user == null) {
90                 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
91                         "Cannot connect to server. Username and/or password might be invalid");
92                 return;
93             }
94
95             if (user.getHomes().isEmpty()) {
96                 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
97                         "User does not have access to any home");
98                 return;
99             }
100
101             homeId = user.getHomes().get(0).getId().longValue();
102
103             HomeInfo homeInfo = api.showHome(homeId);
104             TemperatureUnit temperatureUnit = org.openhab.binding.tado.internal.api.model.TemperatureUnit.FAHRENHEIT == homeInfo
105                     .getTemperatureUnit() ? TemperatureUnit.FAHRENHEIT : TemperatureUnit.CELSIUS;
106             updateProperty(TadoBindingConstants.PROPERTY_HOME_TEMPERATURE_UNIT, temperatureUnit.name());
107         } catch (IOException | ApiException e) {
108             logger.debug("Error accessing tado server: {}", e.getMessage(), e);
109             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
110                     "Could not connect to server due to " + e.getMessage());
111             return;
112         }
113
114         updateStatus(ThingStatus.ONLINE);
115     }
116
117     @Override
118     public void dispose() {
119         super.dispose();
120         if (this.initializationFuture != null || !this.initializationFuture.isDone()) {
121             this.initializationFuture.cancel(true);
122             this.initializationFuture = null;
123         }
124     }
125
126     public HomeApi getApi() {
127         return api;
128     }
129
130     public Long getHomeId() {
131         return homeId;
132     }
133
134     public HomeState getHomeState() throws IOException, ApiException {
135         HomeApi api = getApi();
136         return api != null ? api.homeState(getHomeId()) : null;
137     }
138
139     public void updateHomeState() {
140         try {
141             updateState(TadoBindingConstants.CHANNEL_HOME_PRESENCE_MODE,
142                     getHomeState().getPresence() == PresenceState.HOME ? OnOffType.ON : OnOffType.OFF);
143         } catch (IOException | ApiException e) {
144             logger.debug("Error accessing tado server: {}", e.getMessage(), e);
145         }
146     }
147
148     @Override
149     public void handleCommand(ChannelUID channelUID, Command command) {
150         String id = channelUID.getId();
151
152         if (command == RefreshType.REFRESH) {
153             updateHomeState();
154             return;
155         }
156
157         switch (id) {
158             case TadoBindingConstants.CHANNEL_HOME_PRESENCE_MODE:
159                 HomePresence presence = new HomePresence();
160                 presence.setHomePresence(command.toFullString().toUpperCase().equals("ON")
161                         || command.toFullString().toUpperCase().equals("HOME") ? PresenceState.HOME
162                                 : PresenceState.AWAY);
163                 try {
164                     api.updatePresenceLock(homeId, presence);
165                 } catch (IOException | ApiException e) {
166                     logger.warn("Error setting home presence: {}", e.getMessage(), e);
167                 }
168
169                 break;
170
171         }
172     }
173
174     public State getBatteryLowAlarm(long zoneId) {
175         return batteryChecker.getBatteryLowAlarm(zoneId);
176     }
177 }