Skip to content

Commit

Permalink
Correcting schedule handling of energy home
Browse files Browse the repository at this point in the history
Correcting setpoint handling for rooms

Signed-off-by: clinique <gael@lhopital.org>
  • Loading branch information
clinique committed Feb 24, 2022
1 parent b7a0b1a commit 6526f69
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.netatmo.internal.api.data.ModuleType;
import org.openhab.binding.netatmo.internal.api.data.NetatmoConstants.SetpointMode;
import org.openhab.core.library.types.OnOffType;
import org.openhab.core.types.State;
import org.openhab.core.types.UnDefType;

/**
* The {@link NARoom} holds temperature data for a given room.
Expand All @@ -27,8 +30,8 @@
*/
@NonNullByDefault
public class NARoom extends NAThing implements NetatmoModule {
private boolean anticipating;
private boolean openWindow;
private @Nullable OnOffType anticipating;
private @Nullable OnOffType openWindow;
private int heatingPowerRequest;
private double thermMeasuredTemperature;
private @Nullable ZonedDateTime thermSetpointStartTime;
Expand All @@ -39,8 +42,9 @@ public class NARoom extends NAThing implements NetatmoModule {
/**
* @return the anticipating
*/
public boolean isAnticipating() {
return anticipating;
public State isAnticipating() {
OnOffType status = anticipating;
return status != null ? status : UnDefType.NULL;
}

/**
Expand All @@ -53,8 +57,9 @@ public int getHeatingPowerRequest() {
/**
* @return the openWindow
*/
public boolean isOpenWindow() {
return openWindow;
public State isOpenWindow() {
OnOffType status = openWindow;
return status != null ? status : UnDefType.NULL;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,12 @@
@NonNullByDefault
public class CameraHandler extends NetatmoHandler {
private final Logger logger = LoggerFactory.getLogger(CameraHandler.class);
private final NetatmoDescriptionProvider descriptionProvider;
protected final CameraChannelHelper cameraHelper;
private Optional<SecurityCapability> securityCap = Optional.empty();

public CameraHandler(Bridge bridge, List<AbstractChannelHelper> channelHelpers, ApiBridge apiBridge,
NetatmoDescriptionProvider descriptionProvider, NetatmoServlet webhookServlet) {
super(bridge, channelHelpers, apiBridge, descriptionProvider, webhookServlet);
this.descriptionProvider = descriptionProvider;
this.cameraHelper = (CameraChannelHelper) channelHelpers.stream().filter(c -> c instanceof CameraChannelHelper)
.findFirst().orElseThrow(() -> new IllegalArgumentException(
"CameraHandler must have a CameraChannelHelper, file a bug."));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public void setNewData(NAObject newData) {
securityCap = Optional.of(new SecurityCapability(getThing(), apiBridge, getId()));
}
if (propHelper.hasFeature(FeatureArea.ENERGY) && energyCap.isEmpty()) {
energyCap = Optional.of(new EnergyCapability(getThing(), apiBridge, getId()));
energyCap = Optional.of(new EnergyCapability(getThing(), apiBridge, descriptionProvider, getId()));
}

List<Channel> channelsToRemove = new ArrayList<>();
Expand All @@ -142,10 +142,11 @@ public void setNewData(NAObject newData) {

@Override
public void handleCommand(ChannelUID channelUID, Command command) {
// TODO : ce n'est pas super optimal parceque si la command a été traitée par l'un des cap il n'y a pas lieu de
// continuer.
energyCap.ifPresent(cap -> handleCommand(channelUID, command));
securityCap.ifPresent(cap -> handleCommand(channelUID, command));
// TODO : ce n'est pas super optimal : si command a été traitée par l'un des cap il n'y a pas lieu de continuer.
String channelID = channelUID.getIdWithoutGroup();
energyCap.ifPresent(cap -> cap.handleCommand(channelID, command));
// Security does not currently handle any command
// securityCap.ifPresent(cap -> cap.handleCommand(channelUID, command));
super.handleCommand(channelUID, command);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public abstract class NetatmoHandler extends BaseBridgeHandler implements Connec
protected final ApiBridge apiBridge;
protected final PropertyHelper propertyHelper;
protected NetatmoServlet webhookServlet;
protected final NetatmoDescriptionProvider descriptionProvider;

public NetatmoHandler(Bridge bridge, List<AbstractChannelHelper> channelHelpers, ApiBridge apiBridge,
NetatmoDescriptionProvider descriptionProvider, NetatmoServlet webhookServlet) {
Expand All @@ -77,6 +78,7 @@ public NetatmoHandler(Bridge bridge, List<AbstractChannelHelper> channelHelpers,
this.channelHelpers = channelHelpers;
this.propertyHelper = getPropertyHelper();
this.webhookServlet = webhookServlet;
this.descriptionProvider = descriptionProvider;
}

protected abstract PropertyHelper getPropertyHelper();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,11 @@
*/
@NonNullByDefault
public class PersonHandler extends NetatmoHandler {
private final NetatmoDescriptionProvider descriptionProvider;
private Optional<SecurityCapability> securityCap = Optional.empty();

public PersonHandler(Bridge bridge, List<AbstractChannelHelper> channelHelpers, ApiBridge apiBridge,
NetatmoDescriptionProvider descriptionProvider, NetatmoServlet webhookServlet) {
super(bridge, channelHelpers, apiBridge, descriptionProvider, webhookServlet);
this.descriptionProvider = descriptionProvider;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,12 @@
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.binding.netatmo.internal.action.RoomActions;
import org.openhab.binding.netatmo.internal.api.ApiBridge;
import org.openhab.binding.netatmo.internal.api.data.NetatmoConstants.MeasureClass;
import org.openhab.binding.netatmo.internal.api.data.NetatmoConstants.SetpointMode;
import org.openhab.binding.netatmo.internal.handler.capability.EnergyCapability;
import org.openhab.binding.netatmo.internal.handler.channelhelper.AbstractChannelHelper;
import org.openhab.binding.netatmo.internal.handler.propertyhelper.PropertyHelper;
import org.openhab.binding.netatmo.internal.providers.NetatmoDescriptionProvider;
Expand All @@ -49,7 +47,6 @@
@NonNullByDefault
public class RoomHandler extends NetatmoHandler {
private final Logger logger = LoggerFactory.getLogger(RoomHandler.class);
private Optional<EnergyCapability> energyCap = Optional.empty();

public RoomHandler(Bridge bridge, List<AbstractChannelHelper> channelHelpers, ApiBridge apiBridge,
NetatmoDescriptionProvider descriptionProvider, NetatmoServlet webhookServlet) {
Expand All @@ -61,49 +58,44 @@ protected PropertyHelper getPropertyHelper() {
return new PropertyHelper(getThing());
}

@Override
public void initialize() {
super.initialize();
NetatmoHandler bridgeHandler = getBridgeHandler();
if (bridgeHandler instanceof HomeHandler) {
HomeHandler homeHandler = (HomeHandler) bridgeHandler;
energyCap = homeHandler.getEnergyCap();
}
}

@Override
public void handleCommand(ChannelUID channelUID, Command command) {
if (command instanceof RefreshType) {
super.handleCommand(channelUID, command);
} else {
String channelName = channelUID.getIdWithoutGroup();
String groupName = channelUID.getGroupId();
if (channelName.equals(CHANNEL_SETPOINT_MODE)) {
SetpointMode targetMode = SetpointMode.valueOf(command.toString());
if (targetMode == SetpointMode.MANUAL) {
logger.info("Switch to 'Manual' is done by setting a setpoint temp, command ignored");
} else {
energyCap.ifPresent(cap -> cap.callSetRoomThermMode(getId(), targetMode));
}
} else if (GROUP_TH_SETPOINT.equals(groupName) && channelName.equals(CHANNEL_VALUE)) {
QuantityType<?> quantity = commandToQuantity(command, MeasureClass.INTERIOR_TEMPERATURE);
if (quantity != null) {
energyCap.ifPresent(cap -> cap.callSetRoomThermTemp(getId(), quantity.doubleValue()));
updateState(channelUID, quantity);
} else {
logger.warn("Incorrect command '{}' on channel '{}'", command, channelName);
NetatmoHandler bridgeHandler = getBridgeHandler();
if (bridgeHandler instanceof HomeHandler) {
HomeHandler homeHandler = (HomeHandler) bridgeHandler;
if (channelName.equals(CHANNEL_SETPOINT_MODE)) {
SetpointMode targetMode = SetpointMode.valueOf(command.toString());
if (targetMode == SetpointMode.MANUAL) {
logger.info("Switch to 'Manual' is done by setting a setpoint temp, command ignored");
} else {
homeHandler.getEnergyCap().ifPresent(cap -> cap.callSetRoomThermMode(getId(), targetMode));
}
} else if (GROUP_TH_SETPOINT.equals(groupName) && channelName.equals(CHANNEL_VALUE)) {
QuantityType<?> quantity = commandToQuantity(command, MeasureClass.INTERIOR_TEMPERATURE);
if (quantity != null) {
homeHandler.getEnergyCap()
.ifPresent(cap -> cap.callSetRoomThermTemp(getId(), quantity.doubleValue()));
updateState(channelUID, quantity);
} else {
logger.warn("Incorrect command '{}' on channel '{}'", command, channelName);
}
}

}
}
}

// public int getSetpointDefaultDuration() {
// return getHomeHandler().map(h -> h.energy.getSetpointDefaultDuration()).orElse(120);
// return 0;
// }

public void thingActionCallSetRoomThermTemp(double temperature, long endtime, SetpointMode mode) {
energyCap.ifPresent(cap -> cap.callSetRoomThermTemp(getId(), temperature, endtime, mode));
NetatmoHandler bridgeHandler = getBridgeHandler();
if (bridgeHandler instanceof HomeHandler) {
HomeHandler homeHandler = (HomeHandler) bridgeHandler;
homeHandler.getEnergyCap().ifPresent(cap -> cap.callSetRoomThermTemp(getId(), temperature, endtime, mode));
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import static org.openhab.binding.netatmo.internal.NetatmoBindingConstants.*;
import static org.openhab.binding.netatmo.internal.utils.NetatmoCalendarUtils.setpointEndTimeFromNow;

import java.util.stream.Collectors;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.binding.netatmo.internal.api.ApiBridge;
import org.openhab.binding.netatmo.internal.api.EnergyApi;
Expand All @@ -27,8 +29,11 @@
import org.openhab.binding.netatmo.internal.api.dto.NAHomeStatusModule;
import org.openhab.binding.netatmo.internal.api.dto.NARoom;
import org.openhab.binding.netatmo.internal.deserialization.NAObjectMap;
import org.openhab.binding.netatmo.internal.providers.NetatmoDescriptionProvider;
import org.openhab.core.thing.Bridge;
import org.openhab.core.thing.ChannelUID;
import org.openhab.core.types.Command;
import org.openhab.core.types.StateOption;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -44,10 +49,13 @@ public class EnergyCapability extends Capability<EnergyApi> {

private int setPointDefaultDuration = -1;
private final String homeId;
private final NetatmoDescriptionProvider descriptionProvider;

public EnergyCapability(Bridge bridge, ApiBridge apiBridge, String homeId) {
public EnergyCapability(Bridge bridge, ApiBridge apiBridge, NetatmoDescriptionProvider descriptionProvider,
String homeId) {
super(bridge, apiBridge.getRestManager(EnergyApi.class));
this.homeId = homeId;
this.descriptionProvider = descriptionProvider;
}

public void updateHomeData(NAHomeData homeData) {
Expand All @@ -63,6 +71,10 @@ public void updateHomeData(NAHomeData homeData) {
handler.setNewData(moduleData);
}
});
descriptionProvider.setStateOptions(new ChannelUID(bridge.getUID(), GROUP_HOME_ENERGY, CHANNEL_PLANNING),
homeData.getThermSchedules().stream().map(p -> new StateOption(p.getId(), p.getName()))
.collect(Collectors.toList()));
setPointDefaultDuration = homeData.getThermSetpointDefaultDuration();
}

public void updateHomeStatus(HomeStatus homeStatus) {
Expand All @@ -84,19 +96,14 @@ public void updateHomeStatus(HomeStatus homeStatus) {
});
}

// TODO : ensure this is called by HomeHandler
public void setNewData(NAHomeData home) {
setPointDefaultDuration = home.getThermSetpointDefaultDuration();
}

public int getSetpointDefaultDuration() {
return setPointDefaultDuration;
}

public void callSetRoomThermTemp(String roomId, double temperature) {
try {
api.setRoomThermpoint(homeId, roomId, SetpointMode.MANUAL, setpointEndTimeFromNow(setPointDefaultDuration),
temperature);
temperature > 30 ? 30 : temperature);
expireData();
} catch (NetatmoException e) {
logger.warn("Error setting room target temperature '{}' : {}", roomId, e.getMessage());
Expand All @@ -122,8 +129,7 @@ public void callSetRoomThermTemp(String roomId, double temperature, long endtime
}
}

// TODO from UCDetector: Method "EnergyCapability.handleCommand(String,Command)" has 0 references
public void handleCommand(String channelName, Command command) { // NO_UCD (unused code)
public void handleCommand(String channelName, Command command) {
try {
switch (channelName) {
case CHANNEL_PLANNING:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import org.openhab.binding.netatmo.internal.api.dto.NARoom;
import org.openhab.binding.netatmo.internal.api.dto.NAThing;
import org.openhab.core.config.core.Configuration;
import org.openhab.core.library.types.OpenClosedType;
import org.openhab.core.library.unit.Units;
import org.openhab.core.types.State;

Expand All @@ -44,9 +43,9 @@ public RoomChannelHelper() {
NARoom room = (NARoom) naThing;
switch (channelId) {
case CHANNEL_ROOM_WINDOW_OPEN:
return (room.isOpenWindow() ? OpenClosedType.OPEN : OpenClosedType.CLOSED);
return room.isOpenWindow();
case CHANNEL_ANTICIPATING:
return (room.isAnticipating() ? OpenClosedType.OPEN : OpenClosedType.CLOSED);
return room.isAnticipating();
case CHANNEL_ROOM_HEATING_POWER:
return toQuantityType(room.getHeatingPowerRequest(), Units.PERCENT);
case CHANNEL_VALUE:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@
<label>Setpoint</label>
<description>Thermostat temperature setpoint.</description>
<category>Temperature</category>
<tags>
<tag>Setpoint</tag>
<tag>Temperature</tag>
</tags>
<state pattern="%.1f %unit%" readOnly="false" min="7" max="30" step="0.5"/>
</channel-type>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@
<channel-group-type id="room-properties">
<label>Room status</label>
<channels>
<channel id="window-open" typeId="contact">
<channel id="window-open" typeId="switch-ro">
<label>Window Status</label>
</channel>
<channel id="anticipating" typeId="contact">
<channel id="anticipating" typeId="switch-ro">
<label>Anticipated heating</label>
</channel>
<channel id="heating-power-request" typeId="room-heating-percent">
Expand Down

0 comments on commit 6526f69

Please sign in to comment.