Skip to content

Commit

Permalink
[netatmo] Take in account errored modules/devices for Security/Energy…
Browse files Browse the repository at this point in the history
… areas (openhab#15587)

* Netatmo binding has to take in account errored modules/devices provided by HomeStatus api.

---------

Signed-off-by: clinique <gael@lhopital.org>
Signed-off-by: querdenker2k <querdenker2k@gmx.de>
  • Loading branch information
clinique authored and querdenker2k committed Oct 29, 2023
1 parent 9fcd5df commit 56aaac8
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import static org.openhab.binding.netatmo.internal.api.data.NetatmoConstants.*;

import java.util.Collection;
import java.util.Optional;
import java.util.Set;

import javax.ws.rs.core.UriBuilder;
Expand All @@ -25,7 +26,6 @@
import org.openhab.binding.netatmo.internal.api.data.NetatmoConstants.FeatureArea;
import org.openhab.binding.netatmo.internal.api.dto.HomeData;
import org.openhab.binding.netatmo.internal.api.dto.NAHomeStatus;
import org.openhab.binding.netatmo.internal.api.dto.NAHomeStatus.HomeStatus;
import org.openhab.binding.netatmo.internal.api.dto.NAHomeStatus.NAHomeStatusResponse;
import org.openhab.binding.netatmo.internal.handler.ApiBridgeHandler;

Expand All @@ -43,12 +43,10 @@ public HomeApi(ApiBridgeHandler apiClient) {
super(apiClient, FeatureArea.NONE);
}

public @Nullable HomeStatus getHomeStatus(String homeId) throws NetatmoException {
public Optional<NAHomeStatus> getHomeStatus(String homeId) throws NetatmoException {
UriBuilder uriBuilder = getApiUriBuilder(SUB_PATH_HOMESTATUS, PARAM_HOME_ID, homeId);

NAHomeStatusResponse response = get(uriBuilder, NAHomeStatusResponse.class);
NAHomeStatus body = response.getBody();
return body != null ? body.getHomeStatus().orElse(null) : null;
return Optional.ofNullable(get(uriBuilder, NAHomeStatusResponse.class).getBody());
}

public @Nullable HomeData getHomeData(String homeId) throws NetatmoException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -431,4 +431,27 @@ public enum ServiceError {
@SerializedName("41")
DEVICE_IS_UNREACHABLE
}

public enum HomeStatusError {
@SerializedName("1")
UNKNOWN_ERROR("homestatus-unknown-error"),
@SerializedName("2")
INTERNAL_ERROR("homestatus-internal-error"),
@SerializedName("3")
PARSER_ERROR("homestatus-parser-error"),
@SerializedName("4")
COMMAND_UNKNOWN_NODE_MODULE_ERROR("homestatus-command-unknown"),
@SerializedName("5")
COMMAND_INVALID_PARAMS("homestatus-invalid-params"),
@SerializedName("6")
UNREACHABLE("device-not-connected"),
UNKNOWN("deserialization-unknow");

// Associated error message that can be found in properties files
public final String message;

HomeStatusError(String message) {
this.message = message;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Copyright (c) 2010-2023 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.netatmo.internal.api.dto;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.binding.netatmo.internal.api.data.NetatmoConstants.HomeStatusError;

/**
* The {@link NAError} class is the base class for errors returned by the Netatmo API
* regarding a given thing.
*
* @author Gaël L'hopital - Initial contribution
*
*/
@NonNullByDefault
public class NAError extends NAObject {
private HomeStatusError code = HomeStatusError.UNKNOWN;

public HomeStatusError getCode() {
return code;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
package org.openhab.binding.netatmo.internal.api.dto;

import java.util.List;
import java.util.Optional;

import org.eclipse.jdt.annotation.NonNullByDefault;
Expand Down Expand Up @@ -56,8 +57,13 @@ public NAObjectMap<HomeStatusPerson> getPersons() {
}

private @Nullable HomeStatus home;
private List<NAError> errors = List.of();

public Optional<HomeStatus> getHomeStatus() {
return Optional.ofNullable(home);
}

public List<NAError> getErrors() {
return errors;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.openhab.binding.netatmo.internal.api.dto.HomeData;
import org.openhab.binding.netatmo.internal.api.dto.HomeEvent;
import org.openhab.binding.netatmo.internal.api.dto.HomeStatusModule;
import org.openhab.binding.netatmo.internal.api.dto.NAError;
import org.openhab.binding.netatmo.internal.api.dto.NAHomeStatus.HomeStatus;
import org.openhab.binding.netatmo.internal.api.dto.NAMain;
import org.openhab.binding.netatmo.internal.api.dto.NAObject;
Expand Down Expand Up @@ -63,32 +64,37 @@ public class Capability {

public final @Nullable String setNewData(NAObject newData) {
beforeNewData();
if (newData instanceof HomeData homeData) {
updateHomeData(homeData);
}
if (newData instanceof HomeStatus homeStatus) {
updateHomeStatus(homeStatus);
}
if (newData instanceof HomeStatusModule homeStatusModule) {
updateHomeStatusModule(homeStatusModule);
}
if (newData instanceof NAError error) {
updateErrors(error);
} else {
if (newData instanceof HomeData homeData) {
updateHomeData(homeData);
}
if (newData instanceof HomeStatus homeStatus) {
updateHomeStatus(homeStatus);
}
if (newData instanceof HomeStatusModule homeStatusModule) {
updateHomeStatusModule(homeStatusModule);
}

if (newData instanceof HomeEvent homeEvent) {
updateHomeEvent(homeEvent);
} else if (newData instanceof WebhookEvent webhookEvent && webhookEvent.getEventType().validFor(moduleType)) {
updateWebhookEvent(webhookEvent);
} else if (newData instanceof Event event) {
updateEvent(event);
}
if (newData instanceof HomeEvent homeEvent) {
updateHomeEvent(homeEvent);
} else if (newData instanceof WebhookEvent webhookEvent
&& webhookEvent.getEventType().validFor(moduleType)) {
updateWebhookEvent(webhookEvent);
} else if (newData instanceof Event event) {
updateEvent(event);
}

if (newData instanceof NAThing naThing) {
updateNAThing(naThing);
}
if (newData instanceof NAMain naMain) {
updateNAMain(naMain);
}
if (newData instanceof Device device) {
updateNADevice(device);
if (newData instanceof NAThing naThing) {
updateNAThing(naThing);
}
if (newData instanceof NAMain naMain) {
updateNAMain(naMain);
}
if (newData instanceof Device device) {
updateNADevice(device);
}
}
afterNewData(newData);
return statusReason;
Expand Down Expand Up @@ -152,6 +158,10 @@ protected void updateNADevice(Device newData) {
// do nothing by default, can be overridden by subclasses
}

protected void updateErrors(NAError error) {
// do nothing by default, can be overridden by subclasses
}

public void initialize() {
// do nothing by default, can be overridden by subclasses
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.netatmo.internal.api.dto.NAError;
import org.openhab.binding.netatmo.internal.api.dto.NAObject;
import org.openhab.binding.netatmo.internal.handler.CommonInterface;
import org.openhab.binding.netatmo.internal.handler.channelhelper.ChannelHelper;
Expand All @@ -25,6 +26,7 @@

/**
* {@link ChannelHelperCapability} give the capability to dispatch incoming data across the channel helpers.
* This capability is common to all things
*
* @author Gaël L'hopital - Initial contribution
*
Expand Down Expand Up @@ -56,4 +58,11 @@ public void afterNewData(@Nullable NAObject newData) {
}
});
}

@Override
protected void updateErrors(NAError error) {
if (error.getId().equals(handler.getId())) {
statusReason = "@text/%s".formatted(error.getCode().message);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import org.openhab.binding.netatmo.internal.api.data.NetatmoConstants.FeatureArea;
import org.openhab.binding.netatmo.internal.api.dto.HomeData;
import org.openhab.binding.netatmo.internal.api.dto.Location;
import org.openhab.binding.netatmo.internal.api.dto.NAHomeStatus.HomeStatus;
import org.openhab.binding.netatmo.internal.api.dto.NAError;
import org.openhab.binding.netatmo.internal.api.dto.NAObject;
import org.openhab.binding.netatmo.internal.config.HomeConfiguration;
import org.openhab.binding.netatmo.internal.handler.CommonInterface;
Expand Down Expand Up @@ -105,6 +105,12 @@ private boolean hasArea(FeatureArea searched) {
return featureAreas.contains(searched);
}

@Override
protected void updateErrors(NAError error) {
handler.getActiveChildren().stream().filter(handler -> handler.getId().equals(error.getId())).findFirst()
.ifPresent(handler -> handler.setNewData(error));
}

@Override
protected List<NAObject> updateReadings(HomeApi api) {
List<NAObject> result = new ArrayList<>();
Expand All @@ -115,10 +121,11 @@ protected List<NAObject> updateReadings(HomeApi api) {
result.add(homeData);
featureAreas.addAll(homeData.getFeatures());
}
HomeStatus homeStatus = api.getHomeStatus(id);
if (homeStatus != null) {
result.add(homeStatus);
}

api.getHomeStatus(id).ifPresent(body -> {
body.getHomeStatus().ifPresent(homeStatus -> result.add(homeStatus));
result.addAll(body.getErrors());
});
} catch (NetatmoException e) {
logger.warn("Error getting Home informations : {}", e.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ private void addEventIfKnownObject(HomeEvent homeEvent, @Nullable String objectI
return;
}
handler.getActiveChildren(FeatureArea.SECURITY).filter(child -> child.getId().equals(objectId))
.forEach(child -> child.setNewData(homeEvent.ignoringForThingUpdate()));
.forEach(child -> child.setNewData(homeEvent));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,13 @@ status-bridge-offline = Bridge is not connected to Netatmo API
device-not-connected = Thing is not reachable
data-over-limit = Data seems quite old
request-time-out = Request timed out - will attempt reconnection later
deserialization-unknow = Deserialization lead to an unknown code

homestatus-unknown-error = Unknown error
homestatus-internal-error = Internal error
homestatus-parser-error = Parser error
homestatus-command-unknown = Command unknown node module error
homestatus-invalid-params = Command invalid params

# actions

Expand Down

0 comments on commit 56aaac8

Please sign in to comment.