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.api.ApiException;
21 import org.openhab.binding.tado.internal.api.model.MobileDevice;
22 import org.openhab.binding.tado.internal.config.TadoMobileDeviceConfig;
23 import org.openhab.core.library.types.OnOffType;
24 import org.openhab.core.thing.Bridge;
25 import org.openhab.core.thing.ChannelUID;
26 import org.openhab.core.thing.Thing;
27 import org.openhab.core.thing.ThingStatus;
28 import org.openhab.core.thing.ThingStatusDetail;
29 import org.openhab.core.thing.ThingStatusInfo;
30 import org.openhab.core.types.Command;
31 import org.openhab.core.types.RefreshType;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * The {@link TadoMobileDeviceHandler} is responsible for handling commands of mobile devices and update their state.
38 * @author Dennis Frommknecht - Initial contribution
40 public class TadoMobileDeviceHandler extends BaseHomeThingHandler {
42 private Logger logger = LoggerFactory.getLogger(TadoMobileDeviceHandler.class);
44 private TadoMobileDeviceConfig configuration;
45 private ScheduledFuture<?> refreshTimer;
47 public TadoMobileDeviceHandler(Thing thing) {
52 public void handleCommand(ChannelUID channelUID, Command command) {
53 if (command == RefreshType.REFRESH) {
54 logger.debug("Refreshing {}", channelUID);
57 logger.warn("This Thing is read-only and can only handle REFRESH command");
62 public void initialize() {
63 configuration = getConfigAs(TadoMobileDeviceConfig.class);
65 if (configuration.refreshInterval <= 0) {
66 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Refresh interval of zone "
67 + configuration.id + " of home " + getHomeId() + " must be greater than zero");
71 Bridge bridge = getBridge();
73 bridgeStatusChanged(bridge.getStatusInfo());
78 public void bridgeStatusChanged(ThingStatusInfo bridgeStatusInfo) {
79 if (bridgeStatusInfo.getStatus() == ThingStatus.ONLINE) {
81 MobileDevice device = getMobileDevice();
82 updateProperty(TadoBindingConstants.PROPERTY_MOBILE_DEVICE_NAME, device.getName());
84 if (!device.getSettings().isGeoTrackingEnabled()) {
85 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
86 "Geotracking is disabled on mobile device " + device.getName());
89 } catch (IOException | ApiException e) {
90 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
91 "Could not connect to server due to " + e.getMessage());
92 cancelScheduledStateUpdate();
96 scheduleZoneStateUpdate();
97 updateStatus(ThingStatus.ONLINE);
99 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
100 cancelScheduledStateUpdate();
104 private void updateState() {
106 MobileDevice device = getMobileDevice();
107 updateState(TadoBindingConstants.CHANNEL_MOBILE_DEVICE_AT_HOME,
108 device.getLocation().isAtHome() ? OnOffType.ON : OnOffType.OFF);
109 } catch (IOException | ApiException e) {
110 logger.debug("Status update of mobile device with id {} failed: {}", configuration.id, e.getMessage());
114 private MobileDevice getMobileDevice() throws IOException, ApiException {
115 MobileDevice device = null;
118 device = getApi().listMobileDevices(getHomeId()).stream().filter(m -> m.getId() == configuration.id)
119 .findFirst().orElse(null);
120 } catch (IOException | ApiException e) {
121 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
122 "Could not connect to server due to " + e.getMessage());
126 if (device == null) {
127 String message = "Mobile device with id " + configuration.id + " unknown or does not belong to home "
129 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, message);
130 throw new IOException(message);
133 onSuccessfulOperation();
137 private void scheduleZoneStateUpdate() {
138 if (refreshTimer == null || refreshTimer.isCancelled()) {
139 refreshTimer = scheduler.scheduleWithFixedDelay(this::updateState, 5, configuration.refreshInterval,
144 private void cancelScheduledStateUpdate() {
145 if (refreshTimer != null) {
146 refreshTimer.cancel(false);