]> git.basschouten.com Git - openhab-addons.git/blob
523211210e3936fb40d0d69b01ca98a135c3c183
[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.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;
36
37 /**
38  * The {@link TadoHomeHandler} is the bridge of all home-based things.
39  *
40  * @author Dennis Frommknecht - Initial contribution
41  */
42 public class TadoHomeHandler extends BaseBridgeHandler {
43
44     private Logger logger = LoggerFactory.getLogger(TadoHomeHandler.class);
45
46     private TadoHomeConfig configuration;
47     private HomeApi api;
48     private Long homeId;
49
50     private TadoBatteryChecker batteryChecker;
51
52     private ScheduledFuture<?> initializationFuture;
53
54     public TadoHomeHandler(Bridge bridge) {
55         super(bridge);
56         batteryChecker = new TadoBatteryChecker(this);
57     }
58
59     public TemperatureUnit getTemperatureUnit() {
60         String temperatureUnitStr = this.thing.getProperties().get(TadoBindingConstants.PROPERTY_HOME_TEMPERATURE_UNIT);
61         return TemperatureUnit.valueOf(temperatureUnitStr);
62     }
63
64     @Override
65     public void initialize() {
66         configuration = getConfigAs(TadoHomeConfig.class);
67         api = new HomeApiFactory().create(configuration.username, configuration.password);
68
69         if (this.initializationFuture == null || this.initializationFuture.isDone()) {
70             initializationFuture = scheduler.scheduleWithFixedDelay(this::initializeBridgeStatusAndPropertiesIfOffline,
71                     0, 300, TimeUnit.SECONDS);
72         }
73     }
74
75     private void initializeBridgeStatusAndPropertiesIfOffline() {
76         Bridge bridge = getBridge();
77         if (bridge != null && bridge.getStatus() == ThingStatus.ONLINE) {
78             return;
79         }
80
81         try {
82             // Get user info to verify successful authentication and connection to server
83             User user = api.showUser();
84             if (user == null) {
85                 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
86                         "Cannot connect to server. Username and/or password might be invalid");
87                 return;
88             }
89
90             if (user.getHomes().isEmpty()) {
91                 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
92                         "User does not have access to any home");
93                 return;
94             }
95
96             homeId = user.getHomes().get(0).getId().longValue();
97
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());
106             return;
107         }
108
109         updateStatus(ThingStatus.ONLINE);
110     }
111
112     @Override
113     public void dispose() {
114         super.dispose();
115         if (this.initializationFuture != null || !this.initializationFuture.isDone()) {
116             this.initializationFuture.cancel(true);
117             this.initializationFuture = null;
118         }
119     }
120
121     public HomeApi getApi() {
122         return api;
123     }
124
125     public Long getHomeId() {
126         return homeId;
127     }
128
129     @Override
130     public void handleCommand(ChannelUID channelUID, Command command) {
131         // Nothing to do for a bridge
132     }
133
134     public State getBatteryLowAlarm(long zoneId) {
135         return batteryChecker.getBatteryLowAlarm(zoneId);
136     }
137 }