From 6ba7c1d12e42082f6ecb1109b9890b0c06d7b679 Mon Sep 17 00:00:00 2001 From: lolodomo Date: Thu, 25 Jun 2020 22:41:25 +0200 Subject: [PATCH] [netatmo] Null annotations Part 1 (#8012) Related to #7913 Signed-off-by: Laurent Garnier Signed-off-by: CSchlipp --- .../internal/NetatmoHandlerFactory.java | 4 +- .../internal/channelhelper/BatteryHelper.java | 8 +- .../internal/channelhelper/RadioHelper.java | 8 +- .../config/NetatmoBridgeConfiguration.java | 26 +++-- .../NetatmoModuleDiscoveryService.java | 11 +- .../handler/NetatmoBridgeHandler.java | 104 +++++++++++------- .../internal/handler/NetatmoDataListener.java | 3 + .../handler/NetatmoDeviceHandler.java | 3 +- .../internal/thermostat/NATherm1Handler.java | 35 ++++-- .../welcome/NAWelcomePersonHandler.java | 49 ++++++--- 10 files changed, 158 insertions(+), 93 deletions(-) diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/NetatmoHandlerFactory.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/NetatmoHandlerFactory.java index b1d948b83ec0d..41fab7e8aff7c 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/NetatmoHandlerFactory.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/NetatmoHandlerFactory.java @@ -64,8 +64,8 @@ @Component(service = ThingHandlerFactory.class, configurationPid = "binding.netatmo") public class NetatmoHandlerFactory extends BaseThingHandlerFactory { private final Logger logger = LoggerFactory.getLogger(NetatmoHandlerFactory.class); - private final Map> discoveryServiceRegs = new HashMap<>(); - private final Map> webHookServiceRegs = new HashMap<>(); + private final Map> discoveryServiceRegs = new HashMap<>(); + private final Map> webHookServiceRegs = new HashMap<>(); private final HttpService httpService; private final NATherm1StateDescriptionProvider stateDescriptionProvider; private final TimeZoneProvider timeZoneProvider; diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/channelhelper/BatteryHelper.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/channelhelper/BatteryHelper.java index 2c3f68859106d..dbcc1f82706db 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/channelhelper/BatteryHelper.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/channelhelper/BatteryHelper.java @@ -20,6 +20,8 @@ import java.util.List; import java.util.Optional; +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; import org.eclipse.smarthome.core.library.types.OnOffType; import org.eclipse.smarthome.core.types.State; import org.eclipse.smarthome.core.types.UnDefType; @@ -34,11 +36,12 @@ * @author Gaël L'hopital - Initial contribution * */ +@NonNullByDefault public class BatteryHelper { - private Logger logger = LoggerFactory.getLogger(BatteryHelper.class); + private final Logger logger = LoggerFactory.getLogger(BatteryHelper.class); private int batteryLow; - private Object module; + private @Nullable Object module; public BatteryHelper(String batteryLevels) { List thresholds = Arrays.asList(batteryLevels.split(",")); @@ -50,6 +53,7 @@ public void setModule(Object module) { } public Optional getNAThingProperty(String channelId) { + Object module = this.module; if (module != null) { try { if (CHANNEL_BATTERY_LEVEL.equalsIgnoreCase(channelId) diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/channelhelper/RadioHelper.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/channelhelper/RadioHelper.java index f5d27283a937d..b4113fcde697b 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/channelhelper/RadioHelper.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/channelhelper/RadioHelper.java @@ -21,6 +21,8 @@ import java.util.stream.Collectors; import java.util.stream.Stream; +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; import org.eclipse.smarthome.core.library.types.DecimalType; import org.eclipse.smarthome.core.types.State; import org.eclipse.smarthome.core.types.UnDefType; @@ -34,10 +36,11 @@ * @author Gaël L'hopital - Initial contribution * */ +@NonNullByDefault public class RadioHelper { - private Logger logger = LoggerFactory.getLogger(RadioHelper.class); + private final Logger logger = LoggerFactory.getLogger(RadioHelper.class); private final List signalThresholds; - private Object module; + private @Nullable Object module; public RadioHelper(String signalLevels) { signalThresholds = Stream.of(signalLevels.split(",")).map(Integer::parseInt).collect(Collectors.toList()); @@ -58,6 +61,7 @@ public void setModule(Object module) { } public Optional getNAThingProperty(String channelId) { + Object module = this.module; if (module != null) { try { switch (channelId) { diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/config/NetatmoBridgeConfiguration.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/config/NetatmoBridgeConfiguration.java index bcf5f1ba32915..de4b64846a699 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/config/NetatmoBridgeConfiguration.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/config/NetatmoBridgeConfiguration.java @@ -12,22 +12,26 @@ */ package org.openhab.binding.netatmo.internal.config; +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; + /** * The {@link NetatmoBridgeConfiguration} is responsible for holding * configuration informations needed to access Netatmo API * * @author Gaël L'hopital - Initial contribution */ +@NonNullByDefault public class NetatmoBridgeConfiguration { - public String clientId; - public String clientSecret; - public String username; - public String password; - public Boolean readStation; - public Boolean readThermostat; - public Boolean readHealthyHomeCoach; - public Boolean readWelcome; - public Boolean readPresence; - public String webHookUrl; - public Integer reconnectInterval; + public @Nullable String clientId; + public @Nullable String clientSecret; + public @Nullable String username; + public @Nullable String password; + public boolean readStation = true; + public boolean readThermostat = false; + public boolean readHealthyHomeCoach = false; + public boolean readWelcome = false; + public boolean readPresence = false; + public @Nullable String webHookUrl; + public int reconnectInterval = 5400; } diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/discovery/NetatmoModuleDiscoveryService.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/discovery/NetatmoModuleDiscoveryService.java index 1d8eb8c694103..46fc8bcdd6ebf 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/discovery/NetatmoModuleDiscoveryService.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/discovery/NetatmoModuleDiscoveryService.java @@ -17,7 +17,7 @@ import java.util.HashMap; import java.util.Map; -import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.eclipse.smarthome.config.discovery.AbstractDiscoveryService; import org.eclipse.smarthome.config.discovery.DiscoveryResult; @@ -45,18 +45,18 @@ * @author Ing. Peter Weiss - Welcome camera implementation * */ +@NonNullByDefault public class NetatmoModuleDiscoveryService extends AbstractDiscoveryService implements NetatmoDataListener { private static final int SEARCH_TIME = 2; - @NonNull private final NetatmoBridgeHandler netatmoBridgeHandler; - public NetatmoModuleDiscoveryService(@NonNull NetatmoBridgeHandler netatmoBridgeHandler) { + public NetatmoModuleDiscoveryService(NetatmoBridgeHandler netatmoBridgeHandler) { super(SUPPORTED_DEVICE_THING_TYPES_UIDS, SEARCH_TIME); this.netatmoBridgeHandler = netatmoBridgeHandler; } @Override - public void activate(@Nullable Map<@NonNull String, @Nullable Object> configProperties) { + public void activate(@Nullable Map configProperties) { super.activate(configProperties); netatmoBridgeHandler.registerDataListener(this); } @@ -157,7 +157,8 @@ private void discoverWelcomeHome(NAWelcomeHome home) { } } - private void onDeviceAddedInternal(String id, String parentId, String type, String name, Integer firmwareVersion) { + private void onDeviceAddedInternal(String id, @Nullable String parentId, String type, String name, + @Nullable Integer firmwareVersion) { ThingUID thingUID = findThingUID(type, id); Map properties = new HashMap<>(); diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/NetatmoBridgeHandler.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/NetatmoBridgeHandler.java index 3b0d63df00641..7c81f16b44b0e 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/NetatmoBridgeHandler.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/NetatmoBridgeHandler.java @@ -27,8 +27,8 @@ import java.util.stream.Stream; import org.apache.oltu.oauth2.client.request.OAuthClientRequest; -import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; import org.eclipse.smarthome.core.thing.Bridge; import org.eclipse.smarthome.core.thing.Channel; import org.eclipse.smarthome.core.thing.ChannelUID; @@ -73,16 +73,16 @@ * @author Rob Nielsen - Added day, week, and month measurements to the weather station and modules * */ +@NonNullByDefault public class NetatmoBridgeHandler extends BaseBridgeHandler { - private Logger logger = LoggerFactory.getLogger(NetatmoBridgeHandler.class); + private final Logger logger = LoggerFactory.getLogger(NetatmoBridgeHandler.class); - public NetatmoBridgeConfiguration configuration; - private ScheduledFuture refreshJob; - private APIMap apiMap; - private WelcomeWebHookServlet webHookServlet; + public NetatmoBridgeConfiguration configuration = new NetatmoBridgeConfiguration(); + private @Nullable ScheduledFuture refreshJob; + private @Nullable APIMap apiMap; + private @Nullable WelcomeWebHookServlet webHookServlet; private List dataListeners = new CopyOnWriteArrayList<>(); - @NonNullByDefault private class APIMap extends HashMap, Object> { private static final long serialVersionUID = -2024031764691952343L; private ApiClient apiClient; @@ -101,7 +101,7 @@ public Object get(Class apiClass) { } } - public NetatmoBridgeHandler(@NonNull Bridge bridge, WelcomeWebHookServlet webHookServlet) { + public NetatmoBridgeHandler(Bridge bridge, @Nullable WelcomeWebHookServlet webHookServlet) { super(bridge); this.webHookServlet = webHookServlet; } @@ -116,11 +116,13 @@ public void initialize() { private void connectionSucceed() { updateStatus(ThingStatus.ONLINE); + WelcomeWebHookServlet webHookServlet = this.webHookServlet; String webHookURI = getWebHookURI(); - if (webHookURI != null) { + WelcomeApi welcomeApi = getWelcomeApi(); + if (welcomeApi != null && webHookServlet != null && webHookURI != null) { webHookServlet.activate(this); logger.debug("Setting up Netatmo Welcome WebHook"); - getWelcomeApi().addwebhook(webHookURI, WEBHOOK_APP); + welcomeApi.addwebhook(webHookURI, WEBHOOK_APP); } } @@ -223,71 +225,88 @@ public void handleCommand(ChannelUID channelUID, Command command) { logger.debug("Netatmo Bridge is read-only and does not handle commands"); } - public PartnerApi getPartnerApi() { - return (PartnerApi) apiMap.get(PartnerApi.class); + public @Nullable PartnerApi getPartnerApi() { + APIMap apiMap = this.apiMap; + return apiMap != null ? (PartnerApi) apiMap.get(PartnerApi.class) : null; } - private StationApi getStationApi() { - return (StationApi) apiMap.get(StationApi.class); + private @Nullable StationApi getStationApi() { + APIMap apiMap = this.apiMap; + return apiMap != null ? (StationApi) apiMap.get(StationApi.class) : null; } - private HealthyhomecoachApi getHomeCoachApi() { - return (HealthyhomecoachApi) apiMap.get(HealthyhomecoachApi.class); + private @Nullable HealthyhomecoachApi getHomeCoachApi() { + APIMap apiMap = this.apiMap; + return apiMap != null ? (HealthyhomecoachApi) apiMap.get(HealthyhomecoachApi.class) : null; } - public ThermostatApi getThermostatApi() { - return (ThermostatApi) apiMap.get(ThermostatApi.class); + public @Nullable ThermostatApi getThermostatApi() { + APIMap apiMap = this.apiMap; + return apiMap != null ? (ThermostatApi) apiMap.get(ThermostatApi.class) : null; } - public WelcomeApi getWelcomeApi() { - return (WelcomeApi) apiMap.get(WelcomeApi.class); + public @Nullable WelcomeApi getWelcomeApi() { + APIMap apiMap = this.apiMap; + return apiMap != null ? (WelcomeApi) apiMap.get(WelcomeApi.class) : null; } @Override public void dispose() { logger.debug("Running dispose()"); - if (getWebHookURI() != null) { + WelcomeWebHookServlet webHookServlet = this.webHookServlet; + WelcomeApi welcomeApi = getWelcomeApi(); + if (welcomeApi != null && webHookServlet != null && getWebHookURI() != null) { logger.debug("Releasing Netatmo Welcome WebHook"); webHookServlet.deactivate(); - getWelcomeApi().dropwebhook(WEBHOOK_APP); + welcomeApi.dropwebhook(WEBHOOK_APP); } - if (refreshJob != null && !refreshJob.isCancelled()) { - refreshJob.cancel(true); + ScheduledFuture job = refreshJob; + if (job != null) { + job.cancel(true); refreshJob = null; } } - public NAStationDataBody getStationsDataBody(String equipmentId) { - NAStationDataBody data = getStationApi().getstationsdata(equipmentId, false).getBody(); + public @Nullable NAStationDataBody getStationsDataBody(@Nullable String equipmentId) { + StationApi stationApi = getStationApi(); + NAStationDataBody data = stationApi == null ? null : stationApi.getstationsdata(equipmentId, false).getBody(); updateStatus(ThingStatus.ONLINE); return data; } - public List getStationMeasureResponses(String equipmentId, String moduleId, String scale, - List<@NonNull String> types) { - List data = getStationApi() - .getmeasure(equipmentId, scale, new CSVParams(types), moduleId, null, "last", 1, true, false).getBody(); + public List getStationMeasureResponses(String equipmentId, @Nullable String moduleId, String scale, + List types) { + StationApi stationApi = getStationApi(); + List data = stationApi == null ? null + : stationApi + .getmeasure(equipmentId, scale, new CSVParams(types), moduleId, null, "last", 1, true, false) + .getBody(); updateStatus(ThingStatus.ONLINE); - NAMeasureBodyElem element = data.get(0); + NAMeasureBodyElem element = (data != null && data.size() > 0) ? data.get(0) : null; return element != null ? element.getValue().get(0) : Collections.emptyList(); } - public NAHealthyHomeCoachDataBody getHomecoachDataBody(String equipmentId) { - NAHealthyHomeCoachDataBody data = getHomeCoachApi().gethomecoachsdata(equipmentId).getBody(); + public @Nullable NAHealthyHomeCoachDataBody getHomecoachDataBody(@Nullable String equipmentId) { + HealthyhomecoachApi healthyhomecoachApi = getHomeCoachApi(); + NAHealthyHomeCoachDataBody data = healthyhomecoachApi == null ? null + : healthyhomecoachApi.gethomecoachsdata(equipmentId).getBody(); updateStatus(ThingStatus.ONLINE); return data; } - public NAThermostatDataBody getThermostatsDataBody(String equipmentId) { - NAThermostatDataBody data = getThermostatApi().getthermostatsdata(equipmentId).getBody(); + public @Nullable NAThermostatDataBody getThermostatsDataBody(@Nullable String equipmentId) { + ThermostatApi thermostatApi = getThermostatApi(); + NAThermostatDataBody data = thermostatApi == null ? null + : thermostatApi.getthermostatsdata(equipmentId).getBody(); updateStatus(ThingStatus.ONLINE); return data; } - public NAWelcomeHomeData getWelcomeDataBody(String homeId) { - NAWelcomeHomeData data = getWelcomeApi().gethomedata(homeId, null).getBody(); + public @Nullable NAWelcomeHomeData getWelcomeDataBody(@Nullable String homeId) { + WelcomeApi welcomeApi = getWelcomeApi(); + NAWelcomeHomeData data = welcomeApi == null ? null : welcomeApi.gethomedata(homeId, null).getBody(); updateStatus(ThingStatus.ONLINE); return data; } @@ -297,7 +316,7 @@ public NAWelcomeHomeData getWelcomeDataBody(String homeId) { * * @return Url of the picture or UnDefType.UNDEF */ - public String getPictureUrl(String id, String key) { + public String getPictureUrl(@Nullable String id, @Nullable String key) { StringBuilder ret = new StringBuilder(); if (id != null && key != null) { ret.append(WELCOME_PICTURE_URL).append("?").append(WELCOME_PICTURE_IMAGEID).append("=").append(id) @@ -306,7 +325,7 @@ public String getPictureUrl(String id, String key) { return ret.toString(); } - public Optional findNAThing(String searchedId) { + public Optional findNAThing(@Nullable String searchedId) { List things = getThing().getThings(); Stream naHandlers = things.stream().map(Thing::getHandler) .filter(AbstractNetatmoThingHandler.class::isInstance).map(AbstractNetatmoThingHandler.class::cast) @@ -351,8 +370,9 @@ public void webHookEvent(NAWebhookCameraEvent event) { } } - private String getWebHookURI() { + private @Nullable String getWebHookURI() { String webHookURI = null; + WelcomeWebHookServlet webHookServlet = this.webHookServlet; if (configuration.webHookUrl != null && (configuration.readWelcome || configuration.readPresence) && webHookServlet != null) { webHookURI = configuration.webHookUrl + webHookServlet.getPath(); @@ -360,11 +380,11 @@ private String getWebHookURI() { return webHookURI; } - public boolean registerDataListener(@NonNull NetatmoDataListener dataListener) { + public boolean registerDataListener(NetatmoDataListener dataListener) { return dataListeners.add(dataListener); } - public boolean unregisterDataListener(@NonNull NetatmoDataListener dataListener) { + public boolean unregisterDataListener(NetatmoDataListener dataListener) { return dataListeners.remove(dataListener); } diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/NetatmoDataListener.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/NetatmoDataListener.java index f030750e28d81..08e1d861fe400 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/NetatmoDataListener.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/NetatmoDataListener.java @@ -12,12 +12,15 @@ */ package org.openhab.binding.netatmo.internal.handler; +import org.eclipse.jdt.annotation.NonNullByDefault; + /** * The {@link NetatmoDataListener} allows receiving notification when any netatmo device thing handler * is getting refreshed data from the netatmo server. * * @author Laurent Garnier - Initial contribution */ +@NonNullByDefault public interface NetatmoDataListener { /** diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/NetatmoDeviceHandler.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/NetatmoDeviceHandler.java index ff274376ad233..ab31d0d807891 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/NetatmoDeviceHandler.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/NetatmoDeviceHandler.java @@ -123,7 +123,8 @@ protected void updateChannels() { if (newDeviceReading != null) { updateStatus(ThingStatus.ONLINE); logger.debug("Successfully updated device {} readings! Now updating channels", getId()); - this.device = newDeviceReading; + DEVICE device = newDeviceReading; + this.device = device; updateProperties(device); Integer dataTimeStamp = getDataTimestamp(); if (dataTimeStamp != null) { diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/thermostat/NATherm1Handler.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/thermostat/NATherm1Handler.java index 2b3effafd8199..bc14a079d1ea1 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/thermostat/NATherm1Handler.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/thermostat/NATherm1Handler.java @@ -26,6 +26,7 @@ import javax.measure.quantity.Temperature; import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.jdt.annotation.Nullable; import org.eclipse.smarthome.core.i18n.TimeZoneProvider; import org.eclipse.smarthome.core.library.types.OnOffType; import org.eclipse.smarthome.core.library.types.QuantityType; @@ -40,6 +41,7 @@ import org.eclipse.smarthome.core.types.StateOption; import org.eclipse.smarthome.core.types.UnDefType; import org.openhab.binding.netatmo.internal.NATherm1StateDescriptionProvider; +import org.openhab.binding.netatmo.internal.handler.NetatmoBridgeHandler; import org.openhab.binding.netatmo.internal.handler.NetatmoModuleHandler; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -74,14 +76,21 @@ protected void updateProperties(NAThermostat moduleData) { updateProperties(moduleData.getFirmware(), moduleData.getType()); } + private @Nullable ThermostatApi getThermostatApi() { + NetatmoBridgeHandler bridgeHandler = getBridgeHandler(); + return bridgeHandler == null ? null : bridgeHandler.getThermostatApi(); + } + @Override public void updateChannels(Object moduleObject) { if (isRefreshRequired()) { measurableChannels.getAsCsv().ifPresent(csvParams -> { - ThermostatApi thermostatApi = getBridgeHandler().getThermostatApi(); - NAMeasureResponse measures = thermostatApi.getmeasure(getParentId(), "max", csvParams, getId(), null, - null, 1, true, true); - measurableChannels.setMeasures(measures); + ThermostatApi thermostatApi = getThermostatApi(); + if (thermostatApi != null) { + NAMeasureResponse measures = thermostatApi.getmeasure(getParentId(), "max", csvParams, getId(), + null, null, 1, true, true); + measurableChannels.setMeasures(measures); + } }); setRefreshRequired(false); } @@ -277,10 +286,12 @@ public void handleCommand(ChannelUID channelUID, Command command) { break; } case CHANNEL_PLANNING: { - getBridgeHandler().getThermostatApi().switchschedule(getParentId(), getId(), - command.toString()); - updateState(channelUID, new StringType(command.toString())); - invalidateParentCacheAndRefresh(); + ThermostatApi thermostatApi = getThermostatApi(); + if (thermostatApi != null) { + thermostatApi.switchschedule(getParentId(), getId(), command.toString()); + updateState(channelUID, new StringType(command.toString())); + invalidateParentCacheAndRefresh(); + } } } } catch (Exception e) { @@ -290,9 +301,11 @@ public void handleCommand(ChannelUID channelUID, Command command) { } private void pushSetpointUpdate(String target_mode, Integer setpointEndtime, Float setpointTemp) { - getBridgeHandler().getThermostatApi().setthermpoint(getParentId(), getId(), target_mode, setpointEndtime, - setpointTemp); - invalidateParentCacheAndRefresh(); + ThermostatApi thermostatApi = getThermostatApi(); + if (thermostatApi != null) { + thermostatApi.setthermpoint(getParentId(), getId(), target_mode, setpointEndtime, setpointTemp); + invalidateParentCacheAndRefresh(); + } } private int getSetpointEndTime() { diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/welcome/NAWelcomePersonHandler.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/welcome/NAWelcomePersonHandler.java index 7797134ce97d8..dd7bbc53a8454 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/welcome/NAWelcomePersonHandler.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/welcome/NAWelcomePersonHandler.java @@ -18,6 +18,7 @@ import java.util.List; import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.jdt.annotation.Nullable; import org.eclipse.smarthome.core.i18n.TimeZoneProvider; import org.eclipse.smarthome.core.library.types.OnOffType; import org.eclipse.smarthome.core.thing.ChannelUID; @@ -26,6 +27,7 @@ import org.eclipse.smarthome.core.types.State; import org.eclipse.smarthome.core.types.UnDefType; import org.eclipse.smarthome.io.net.http.HttpUtil; +import org.openhab.binding.netatmo.internal.handler.NetatmoBridgeHandler; import org.openhab.binding.netatmo.internal.handler.NetatmoModuleHandler; import io.swagger.client.api.WelcomeApi; @@ -48,20 +50,27 @@ public NAWelcomePersonHandler(@NonNull Thing thing, final TimeZoneProvider timeZ super(thing, timeZoneProvider); } + private @Nullable WelcomeApi getWelcomeApi() { + NetatmoBridgeHandler bridgeHandler = getBridgeHandler(); + return bridgeHandler == null ? null : bridgeHandler.getWelcomeApi(); + } + @Override public void updateChannels(Object module) { if (isRefreshRequired()) { - WelcomeApi welcomeApi = getBridgeHandler().getWelcomeApi(); - NAWelcomeEventResponse eventResponse = welcomeApi.getlasteventof(getParentId(), getId(), 10); + WelcomeApi welcomeApi = getWelcomeApi(); + if (welcomeApi != null) { + NAWelcomeEventResponse eventResponse = welcomeApi.getlasteventof(getParentId(), getId(), 10); - // Search the last event for this person - List rawEventList = eventResponse.getBody().getEventsList(); - rawEventList.forEach(event -> { - if (event.getPersonId() != null && event.getPersonId().equalsIgnoreCase(getId()) - && (lastEvent == null || lastEvent.getTime() < event.getTime())) { - lastEvent = event; - } - }); + // Search the last event for this person + List rawEventList = eventResponse.getBody().getEventsList(); + rawEventList.forEach(event -> { + if (event.getPersonId() != null && event.getPersonId().equalsIgnoreCase(getId()) + && (lastEvent == null || lastEvent.getTime() < event.getTime())) { + lastEvent = event; + } + }); + } setRefreshRequired(false); } @@ -70,6 +79,7 @@ public void updateChannels(Object module) { @Override protected State getNAThingProperty(@NonNull String channelId) { + NAWelcomePerson module = this.module; switch (channelId) { case CHANNEL_WELCOME_PERSON_LASTSEEN: return module != null ? toDateTimeType(module.getLastSeen(), timeZoneProvider.getTimeZone()) @@ -96,17 +106,20 @@ protected State getNAThingProperty(@NonNull String channelId) { } private String getLastEventURL() { - if (lastEvent != null && lastEvent.getSnapshot() != null) { - return getBridgeHandler().getPictureUrl(lastEvent.getSnapshot().getId(), lastEvent.getSnapshot().getKey()); + NetatmoBridgeHandler bridgeHandler = getBridgeHandler(); + if (bridgeHandler != null && lastEvent != null && lastEvent.getSnapshot() != null) { + return bridgeHandler.getPictureUrl(lastEvent.getSnapshot().getId(), lastEvent.getSnapshot().getKey()); } return null; } private String getAvatarURL() { - if (avatarURL == null && module != null) { + NetatmoBridgeHandler bridgeHandler = getBridgeHandler(); + NAWelcomePerson module = this.module; + if (bridgeHandler != null && avatarURL == null && module != null) { NAWelcomeFace face = module.getFace(); if (face != null) { - avatarURL = getBridgeHandler().getPictureUrl(face.getId(), face.getKey()); + avatarURL = bridgeHandler.getPictureUrl(face.getId(), face.getKey()); } } return avatarURL; @@ -115,11 +128,13 @@ private String getAvatarURL() { @Override public void handleCommand(ChannelUID channelUID, Command command) { super.handleCommand(channelUID, command); - if ((command instanceof OnOffType) && (CHANNEL_WELCOME_PERSON_ATHOME.equalsIgnoreCase(channelUID.getId()))) { + WelcomeApi welcomeApi = getWelcomeApi(); + if (welcomeApi != null && (command instanceof OnOffType) + && (CHANNEL_WELCOME_PERSON_ATHOME.equalsIgnoreCase(channelUID.getId()))) { if ((OnOffType) command == OnOffType.OFF) { - getBridgeHandler().getWelcomeApi().setpersonsaway(getParentId(), getId()); + welcomeApi.setpersonsaway(getParentId(), getId()); } else { - getBridgeHandler().getWelcomeApi().setpersonshome(getParentId(), "[\"" + getId() + "\"]"); + welcomeApi.setpersonshome(getParentId(), "[\"" + getId() + "\"]"); } invalidateParentCacheAndRefresh(); }