Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[evcc] Fix missing grid readings after evcc breaking change in 0.133.0 #18199

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,7 @@
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.evcc.internal.api.EvccAPI;
import org.openhab.binding.evcc.internal.api.EvccApiException;
import org.openhab.binding.evcc.internal.api.dto.Battery;
import org.openhab.binding.evcc.internal.api.dto.Loadpoint;
import org.openhab.binding.evcc.internal.api.dto.PV;
import org.openhab.binding.evcc.internal.api.dto.Plan;
import org.openhab.binding.evcc.internal.api.dto.Result;
import org.openhab.binding.evcc.internal.api.dto.Vehicle;
import org.openhab.binding.evcc.internal.api.dto.*;
import org.openhab.core.i18n.TimeZoneProvider;
import org.openhab.core.library.CoreItemFactory;
import org.openhab.core.library.types.DateTimeType;
Expand Down Expand Up @@ -420,7 +415,7 @@ private void refresh() {
updateStatus(ThingStatus.ONLINE);
Battery[] batteries = result.getBattery();
batteryConfigured = ((batteries != null) && (batteries.length > 0));
gridConfigured = (result.getGridPower() != null);
gridConfigured = result.getGridConfigured();
PV[] pvs = result.getPV();
pvConfigured = ((pvs != null) && (pvs.length > 0));
createChannelsGeneral();
Expand Down Expand Up @@ -712,7 +707,12 @@ private void updateChannelsGeneral() {
}
boolean gridConfigured = this.gridConfigured;
if (gridConfigured) {
// handling gridPower prior to changes in evcc version 0.133.0
float gridPower = ((result.getGridPower() == null) ? 0.0f : result.getGridPower());
Grid grid = result.getGrid();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Grid grid = result.getGrid();
// handling gridPower since evcc version 0.133.0
Grid grid = result.getGrid();

if (grid != null) {
gridPower = ((grid.getPower() == null) ? 0.0f : grid.getPower());
}
channel = new ChannelUID(uid, CHANNEL_GROUP_ID_GENERAL, CHANNEL_GRID_POWER);
updateState(channel, new QuantityType<>(gridPower, Units.WATT));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (c) 2010-2025 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.evcc.internal.api.dto;

import com.google.gson.annotations.SerializedName;

/**
* This class represents the grid response (/api/state).
* This DTO was written for evcc version 0.133.0
*
* @author Daniel Kötting - Initial contribution
*/
public class Grid {
// Data types from https://github.com/evcc-io/evcc/blob/master/api/api.go
// and from https://docs.evcc.io/docs/reference/configuration/messaging/#msg

@SerializedName("currents")
private float[] currents;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this also work if no currenty are provided in the JSON response?


@SerializedName("energy")
private float energy;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the evcc demo, it might be possible that this value is not available, which would make deserialization fail. Can you please switch to Float?


@SerializedName("power")
private Float power;

/**
* @return grid's currents
*/
public float[] getCurrents() {
return currents;
}

/**
* @return grid's energy
*/
public float getEnergy() {
return energy;
}

/**
* @return grid's power or {@code null} if not available
*/
public Float getPower() {
return power;
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assuming my comments are correct (can you please verify?), I would add some API compatibility notes to the changed values.
Please also update the class JavaDoc for the evcc version compatibility.

Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ public class Result {
@SerializedName("batteryMode")
private String batteryMode;

@SerializedName("gridCurrents")
private float[] gridCurrents;

@SerializedName("gridEnergy")
private float gridEnergy;

@SerializedName("gridPower")
private Float gridPower;

@SerializedName("grid")
private Grid grid;

@SerializedName("gridConfigured")
private boolean gridConfigured;

@SerializedName("homePower")
private float homePower;

Expand Down Expand Up @@ -165,24 +165,24 @@ public String getBatteryMode() {
}

/**
* @return grid's currents
* @return gridPower (before evcc version 0.133.0)
*/
public float[] getGridCurrents() {
return gridCurrents;
public Float getGridPower() {
return gridPower;
}

/**
* @return grid's energy
* @return all grid related values
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @return all grid related values
* @return all grid related values (since evcc version 0.133.0)

*/
public float getGridEnergy() {
return gridEnergy;
public Grid getGrid() {
return grid;
}

/**
* @return grid's power or {@code null} if not available
* @return is grid configured
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @return is grid configured
* @return whether grid is configured (since evcc version 0.133.0)

*/
public Float getGridPower() {
return gridPower;
public boolean getGridConfigured() {
return gridConfigured;
}

/**
Expand Down