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

[senechome] code cleanup and rename of battery state to system state #9535

Closed
wants to merge 3 commits into from
Closed
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
12 changes: 6 additions & 6 deletions bundles/org.openhab.binding.senechome/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ The property `limitationTresholdValue` is used as threshold for channel `powerLi
| energyProduction | watt | Energy generated by your pv / inverter |
| batteryPower | watt | Energy processed by batterie itself, for example while charging |
| batteryFuelCharge | percent | Fuel charge of your battery (0 - 100%) |
| batteryState | | Text describing current action of battery (e.g. CHARGE) |
| batteryStateValue | | Value describing current action of battery (e.g. 14) |
| systemState | | Text describing current action of the senec home system (e.g. CHARGE) |
| systemStateValue | | Value describing current action of the senec home system (e.g. 14) |
| gridPower | watt | Grid power level, negative for supply, positive values for drawing power |
| gridPowerDraw | watt | Absolute power level of power draw, zero while supplying |
| gridPowerSupply | watt | Absolute power level of power supply, zero while drawing |
Expand Down Expand Up @@ -70,8 +70,8 @@ Number SenecHouseConsumption "Current power consumption [%d W]" <e
Number SenecEnergyProduction "Energy generated by pv [%d W]" <energy> { channel="senechome:senechome:pvbattery:energyProduction" }
Number SenecBatteryPower "Energy processed by battery [%d W]" <energy> { channel="senechome:senechome:pvbattery:batteryPower" }
Number SenecBatteryFuelCharge "State of Charge [%d %%]" <batterylevel> { channel="senechome:senechome:pvbattery:batteryFuelCharge" }
String SenecBatteryState "Current action [%s]" <text> { channel="senechome:senechome:pvbattery:batteryState" }
Number SenecBatteryStateValue "Current action [%d]" <text> { channel="senechome:senechome:pvbattery:batteryStateValue" }
String SenecSystemState "Current system state [%s]" <text> { channel="senechome:senechome:pvbattery:systemState" }
Number SenecSystemStateValue "Current system state [%d]" <text> { channel="senechome:senechome:pvbattery:systemStateValue" }
Number SenecGridPower "Grid power level [%d W]" <energy> { channel="senechome:senechome:pvbattery:gridPower" }
Number SenecGridPowerDraw "Power draw from grid [%d W]" <energy> { channel="senechome:senechome:pvbattery:gridPowerDraw" }
Number SenecGridPowerSupply "Power supply to grid [%d W]" <energy> { channel="senechome:senechome:pvbattery:gridPowerSupply" }
Expand Down Expand Up @@ -105,8 +105,8 @@ Text label="Power Grid"{
Default item=SenecEnergyProduction
Default item=SenecBatteryPower
Default item=SenecBatteryFuelCharge
Default item=SenecBatteryState
Default item=SenecBatteryStateValue
Default item=SenecSystemState
Default item=SenecSystemStateValue
Default item=SenecGridPower
Default item=SenecGridPowerDraw
Default item=SenecGridPowerSupply
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public class SenecHomeBindingConstants {

public static final String CHANNEL_SENEC_POWER_LIMITATION = "powerLimitation";
public static final String CHANNEL_SENEC_POWER_LIMITATION_STATE = "powerLimitationState";
public static final String CHANNEL_SENEC_BATTERY_STATE = "batteryState";
public static final String CHANNEL_SENEC_BATTERY_STATE_VALUE = "batteryStateValue";
public static final String CHANNEL_SENEC_SYSTEM_STATE = "systemState";
public static final String CHANNEL_SENEC_SYSTEM_STATE_VALUE = "systemStateValue";
public static final String CHANNEL_SENEC_POWER_CONSUMPTION = "houseConsumption";
public static final String CHANNEL_SENEC_ENERGY_PRODUCTION = "energyProduction";
public static final String CHANNEL_SENEC_BATTERY_POWER = "batteryPower";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import org.openhab.core.thing.ThingStatusDetail;
import org.openhab.core.thing.binding.BaseThingHandler;
import org.openhab.core.types.Command;
import org.openhab.core.types.State;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -118,6 +119,13 @@ private void refresh() {
refreshCache.getValue();
}

private void updateChannelState(String channelConstant, State channelState) {
Channel channel = getThing().getChannel(channelConstant);
if (channel != null) {
updateState(channel.getUID(), channelState);
}
}

public @Nullable Boolean refreshState() {
try {
SenecHomeResponse response = senecHomeApi.getStatistics();
Expand All @@ -136,141 +144,95 @@ private void refresh() {
(100 - pvLimitation.intValue()) <= config.limitationTresholdValue, config.limitationDuration);
}

Channel channelConsumption = getThing()
.getChannel(SenecHomeBindingConstants.CHANNEL_SENEC_POWER_CONSUMPTION);
if (channelConsumption != null) {
updateState(channelConsumption.getUID(),
new QuantityType<Power>(
getSenecValue(response.energy.homePowerConsumption).setScale(2, RoundingMode.HALF_UP),
Units.WATT));
Comment on lines -141 to -145
Copy link
Member

Choose a reason for hiding this comment

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

I don't know if it's relevant. But what changed here is that the creation of the QuantityType instance is always done even if channelConsumption is null. I don't know if that could result in errors, which could happen if the value cannot be evaluated if channelConsumption is null and in case that dependency does exist.
If this is an actual problem you could pass the QuantityType as Consumer. Like: () -> new QuantityType(... and then in the method it can be called when the parameter is not null.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hey,
I don't see the problem that you mention in the code. The Data is passed by getSenecValue() from the Senec Device. So if the channel is null or not has no influence on the creation of the QuantityType, only the .getUID() will not work on a null Object.

I actually don't understand the check for null, how can a channel be null?

Cheers

Copy link
Contributor

@Skinah Skinah Oct 2, 2021

Choose a reason for hiding this comment

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

getThing().getChannel(
The above can return NULL if the channel does not exist. However you have removed the use of this and instead used a constant instead which I don't see the issue in doing it this way. The only thing to comment on is you can use an import and change it to...

updateChannelState(CHANNEL_SENEC_POWER_CONSUMPTION, new QuantityType<Power>(.............

If you did a find replace on SenecHomeBindingConstants. it would be a quick change to make.

}
updateChannelState(SenecHomeBindingConstants.CHANNEL_SENEC_POWER_CONSUMPTION, new QuantityType<Power>(
getSenecValue(response.energy.homePowerConsumption).setScale(2, RoundingMode.HALF_UP), Units.WATT));

Channel channelEnergyProduction = getThing()
.getChannel(SenecHomeBindingConstants.CHANNEL_SENEC_ENERGY_PRODUCTION);
if (channelEnergyProduction != null) {
updateState(channelEnergyProduction.getUID(), new QuantityType<Power>(
getSenecValue(response.energy.inverterPowerGeneration).setScale(0, RoundingMode.HALF_UP),
Units.WATT));
}
updateChannelState(SenecHomeBindingConstants.CHANNEL_SENEC_ENERGY_PRODUCTION,
new QuantityType<Power>(
getSenecValue(response.energy.inverterPowerGeneration).setScale(0, RoundingMode.HALF_UP),
Units.WATT));

Channel channelBatteryPower = getThing().getChannel(SenecHomeBindingConstants.CHANNEL_SENEC_BATTERY_POWER);
if (channelBatteryPower != null) {
updateState(channelBatteryPower.getUID(), new QuantityType<Power>(
getSenecValue(response.energy.batteryPower).setScale(2, RoundingMode.HALF_UP), Units.WATT));
}
updateChannelState(SenecHomeBindingConstants.CHANNEL_SENEC_BATTERY_POWER, new QuantityType<Power>(
getSenecValue(response.energy.batteryPower).setScale(2, RoundingMode.HALF_UP), Units.WATT));

Channel channelBatteryFuelCharge = getThing()
.getChannel(SenecHomeBindingConstants.CHANNEL_SENEC_BATTERY_FUEL_CHARGE);
if (channelBatteryFuelCharge != null) {
updateState(channelBatteryFuelCharge.getUID(),
new QuantityType<Dimensionless>(
getSenecValue(response.energy.batteryFuelCharge).setScale(0, RoundingMode.HALF_UP),
Units.PERCENT));
}
updateChannelState(SenecHomeBindingConstants.CHANNEL_SENEC_BATTERY_FUEL_CHARGE,
new QuantityType<Dimensionless>(
getSenecValue(response.energy.batteryFuelCharge).setScale(0, RoundingMode.HALF_UP),
Units.PERCENT));

updateChannelState(SenecHomeBindingConstants.CHANNEL_SENEC_GRID_CURRENT_PH1,
new QuantityType<ElectricCurrent>(getSenecValue(response.grid.currentGridCurrentPerPhase[0])
.setScale(2, RoundingMode.HALF_UP), Units.AMPERE));

updateChannelState(SenecHomeBindingConstants.CHANNEL_SENEC_GRID_CURRENT_PH2,
new QuantityType<ElectricCurrent>(getSenecValue(response.grid.currentGridCurrentPerPhase[1])
.setScale(2, RoundingMode.HALF_UP), Units.AMPERE));

updateChannelState(SenecHomeBindingConstants.CHANNEL_SENEC_GRID_CURRENT_PH3,
new QuantityType<ElectricCurrent>(getSenecValue(response.grid.currentGridCurrentPerPhase[2])
.setScale(2, RoundingMode.HALF_UP), Units.AMPERE));

Channel channelGridCurrentPhase1 = getThing()
.getChannel(SenecHomeBindingConstants.CHANNEL_SENEC_GRID_CURRENT_PH1);
updateState(channelGridCurrentPhase1.getUID(), new QuantityType<ElectricCurrent>(
getSenecValue(response.grid.currentGridCurrentPerPhase[0]).setScale(2, RoundingMode.HALF_UP),
Units.AMPERE));

Channel channelGridCurrentPhase2 = getThing()
.getChannel(SenecHomeBindingConstants.CHANNEL_SENEC_GRID_CURRENT_PH2);
updateState(channelGridCurrentPhase2.getUID(), new QuantityType<ElectricCurrent>(
getSenecValue(response.grid.currentGridCurrentPerPhase[1]).setScale(2, RoundingMode.HALF_UP),
Units.AMPERE));

Channel channelGridCurrentPhase3 = getThing()
.getChannel(SenecHomeBindingConstants.CHANNEL_SENEC_GRID_CURRENT_PH3);
updateState(channelGridCurrentPhase3.getUID(), new QuantityType<ElectricCurrent>(
getSenecValue(response.grid.currentGridCurrentPerPhase[2]).setScale(2, RoundingMode.HALF_UP),
Units.AMPERE));

Channel channelGridPowerPhase1 = getThing()
.getChannel(SenecHomeBindingConstants.CHANNEL_SENEC_GRID_POWER_PH1);
updateState(channelGridPowerPhase1.getUID(),
updateChannelState(SenecHomeBindingConstants.CHANNEL_SENEC_GRID_POWER_PH1,
new QuantityType<Power>(
getSenecValue(response.grid.currentGridPowerPerPhase[0]).setScale(2, RoundingMode.HALF_UP),
Units.WATT));

Channel channelGridPowerPhase2 = getThing()
.getChannel(SenecHomeBindingConstants.CHANNEL_SENEC_GRID_POWER_PH2);
updateState(channelGridPowerPhase2.getUID(),
updateChannelState(SenecHomeBindingConstants.CHANNEL_SENEC_GRID_POWER_PH2,
new QuantityType<Power>(
getSenecValue(response.grid.currentGridPowerPerPhase[1]).setScale(2, RoundingMode.HALF_UP),
Units.WATT));

Channel channelGridPowerPhase3 = getThing()
.getChannel(SenecHomeBindingConstants.CHANNEL_SENEC_GRID_POWER_PH3);
updateState(channelGridPowerPhase3.getUID(),
updateChannelState(SenecHomeBindingConstants.CHANNEL_SENEC_GRID_POWER_PH3,
new QuantityType<Power>(
getSenecValue(response.grid.currentGridPowerPerPhase[2]).setScale(2, RoundingMode.HALF_UP),
Units.WATT));

Channel channelGridVoltagePhase1 = getThing()
.getChannel(SenecHomeBindingConstants.CHANNEL_SENEC_GRID_VOLTAGE_PH1);
updateState(channelGridVoltagePhase1.getUID(), new QuantityType<ElectricPotential>(
getSenecValue(response.grid.currentGridVoltagePerPhase[0]).setScale(2, RoundingMode.HALF_UP),
Units.VOLT));

Channel channelGridVoltagePhase2 = getThing()
.getChannel(SenecHomeBindingConstants.CHANNEL_SENEC_GRID_VOLTAGE_PH2);
updateState(channelGridVoltagePhase2.getUID(), new QuantityType<ElectricPotential>(
getSenecValue(response.grid.currentGridVoltagePerPhase[1]).setScale(2, RoundingMode.HALF_UP),
Units.VOLT));

Channel channelGridVoltagePhase3 = getThing()
.getChannel(SenecHomeBindingConstants.CHANNEL_SENEC_GRID_VOLTAGE_PH3);
updateState(channelGridVoltagePhase3.getUID(), new QuantityType<ElectricPotential>(
getSenecValue(response.grid.currentGridVoltagePerPhase[2]).setScale(2, RoundingMode.HALF_UP),
Units.VOLT));

Channel channelGridFrequency = getThing()
.getChannel(SenecHomeBindingConstants.CHANNEL_SENEC_GRID_FREQUENCY);
updateState(channelGridFrequency.getUID(), new QuantityType<Frequency>(
updateChannelState(SenecHomeBindingConstants.CHANNEL_SENEC_GRID_VOLTAGE_PH1,
new QuantityType<ElectricPotential>(getSenecValue(response.grid.currentGridVoltagePerPhase[0])
.setScale(2, RoundingMode.HALF_UP), Units.VOLT));

updateChannelState(SenecHomeBindingConstants.CHANNEL_SENEC_GRID_VOLTAGE_PH2,
new QuantityType<ElectricPotential>(getSenecValue(response.grid.currentGridVoltagePerPhase[1])
.setScale(2, RoundingMode.HALF_UP), Units.VOLT));

updateChannelState(SenecHomeBindingConstants.CHANNEL_SENEC_GRID_VOLTAGE_PH3,
new QuantityType<ElectricPotential>(getSenecValue(response.grid.currentGridVoltagePerPhase[2])
.setScale(2, RoundingMode.HALF_UP), Units.VOLT));

updateChannelState(SenecHomeBindingConstants.CHANNEL_SENEC_GRID_FREQUENCY, new QuantityType<Frequency>(
getSenecValue(response.grid.currentGridFrequency).setScale(2, RoundingMode.HALF_UP), Units.HERTZ));

Channel channelBatteryStateValue = getThing()
.getChannel(SenecHomeBindingConstants.CHANNEL_SENEC_BATTERY_STATE_VALUE);
updateState(channelBatteryStateValue.getUID(),
new DecimalType(getSenecValue(response.energy.batteryState).intValue()));
updateChannelState(SenecHomeBindingConstants.CHANNEL_SENEC_SYSTEM_STATE_VALUE,
new DecimalType(getSenecValue(response.energy.systemState).intValue()));

Channel channelLiveBatCharge = getThing()
.getChannel(SenecHomeBindingConstants.CHANNEL_SENEC_LIVE_BAT_CHARGE);
updateState(channelLiveBatCharge.getUID(),
updateChannelState(SenecHomeBindingConstants.CHANNEL_SENEC_LIVE_BAT_CHARGE,
new QuantityType<Energy>(
getSenecValue(response.statistics.liveBatCharge).setScale(2, RoundingMode.HALF_UP),
Units.WATT_HOUR));

Channel channelLiveBatDischarge = getThing()
.getChannel(SenecHomeBindingConstants.CHANNEL_SENEC_LIVE_BAT_DISCHARGE);
updateState(channelLiveBatDischarge.getUID(),
updateChannelState(SenecHomeBindingConstants.CHANNEL_SENEC_LIVE_BAT_DISCHARGE,
new QuantityType<Energy>(
getSenecValue(response.statistics.liveBatDischarge).setScale(2, RoundingMode.HALF_UP),
Units.WATT_HOUR));

Channel channelLiveGridImport = getThing()
.getChannel(SenecHomeBindingConstants.CHANNEL_SENEC_LIVE_GRID_IMPORT);
updateState(channelLiveGridImport.getUID(),
updateChannelState(SenecHomeBindingConstants.CHANNEL_SENEC_LIVE_GRID_IMPORT,
new QuantityType<Energy>(
getSenecValue(response.statistics.liveGridImport).setScale(2, RoundingMode.HALF_UP),
Units.WATT_HOUR));

Channel channelLiveGridExport = getThing()
.getChannel(SenecHomeBindingConstants.CHANNEL_SENEC_LIVE_GRID_EXPORT);
updateState(channelLiveGridExport.getUID(),
updateChannelState(SenecHomeBindingConstants.CHANNEL_SENEC_LIVE_GRID_EXPORT,
new QuantityType<Energy>(
getSenecValue(response.statistics.liveGridExport).setScale(2, RoundingMode.HALF_UP),
Units.WATT_HOUR));

Channel channelBatteryVoltage = getThing()
.getChannel(SenecHomeBindingConstants.CHANNEL_SENEC_BATTERY_VOLTAGE);
updateState(channelBatteryVoltage.getUID(), new QuantityType<ElectricPotential>(
getSenecValue(response.energy.batteryVoltage).setScale(2, RoundingMode.HALF_UP), Units.VOLT));
updateChannelState(SenecHomeBindingConstants.CHANNEL_SENEC_BATTERY_VOLTAGE,
new QuantityType<ElectricPotential>(
getSenecValue(response.energy.batteryVoltage).setScale(2, RoundingMode.HALF_UP),
Units.VOLT));

Channel channelBatteryState = getThing().getChannel(SenecHomeBindingConstants.CHANNEL_SENEC_BATTERY_STATE);
if (channelBatteryState != null) {
updateBatteryState(channelBatteryState, getSenecValue(response.energy.batteryState).intValue());
Channel channelSystemState = getThing().getChannel(SenecHomeBindingConstants.CHANNEL_SENEC_SYSTEM_STATE);
if (channelSystemState != null) {
updateSystemState(channelSystemState, getSenecValue(response.energy.systemState).intValue());
}

updateGridPowerValues(getSenecValue(response.grid.currentGridValue));
Expand Down Expand Up @@ -341,7 +303,7 @@ protected void updatePowerLimitationStatus(Channel channel, boolean status, int
updateState(channel.getUID(), status ? OnOffType.ON : OnOffType.OFF);
}

protected void updateBatteryState(Channel channel, int code) {
protected void updateSystemState(Channel channel, int code) {
updateState(channel.getUID(), new StringType(SenecBatteryStatus.descriptionFromCode(code)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ public class SenecHomeEnergy implements Serializable {
public @SerializedName("GUI_INVERTER_POWER") String inverterPowerGeneration;
public @SerializedName("GUI_BAT_DATA_POWER") String batteryPower;
public @SerializedName("GUI_BAT_DATA_FUEL_CHARGE") String batteryFuelCharge;
public @SerializedName("STAT_STATE") String batteryState;
public @SerializedName("STAT_STATE") String systemState;
public @SerializedName("GUI_BAT_DATA_VOLTAGE") String batteryVoltage;

@Override
public String toString() {
return "SenecHomeEnergy [homePowerConsumption=" + homePowerConsumption + ", inverterPowerGeneration="
+ inverterPowerGeneration + ", batteryPower=" + batteryPower + ", batteryFuelCharge="
+ batteryFuelCharge + ", batteryState=" + batteryState + ", batteryVoltage" + batteryVoltage + "]";
+ batteryFuelCharge + ", systemState=" + systemState + ", batteryVoltage" + batteryVoltage + "]";
}
}
Loading