diff --git a/bundles/org.openhab.binding.homewizard/README.md b/bundles/org.openhab.binding.homewizard/README.md index 818f046225ca6..757c68c7370fd 100644 --- a/bundles/org.openhab.binding.homewizard/README.md +++ b/bundles/org.openhab.binding.homewizard/README.md @@ -1,9 +1,8 @@ # HomeWizard Binding The HomeWizard binding retrieves measurements from the HomeWizard Wi-Fi P1 meter. -The meter itself is attached to a DSMR Smart Meter and reads out the telegrams, which -it will forward to cloud storage. However, recently HomeWizard also added an interface -that can be queried locally. +The meter itself is attached to a DSMR Smart Meter and reads out the telegrams, which it will forward to cloud storage. +However, recently HomeWizard also added an interface that can be queried locally. This binding uses that local interface to make the measurements available. @@ -19,27 +18,40 @@ Auto discovery is not available for this binding. The P1 Meter thing can be configured through the web interface. -`Network Address (required)` +| Parameter | Required | Default | Description | +|--------------|----------|---------|---------------------------------------------------------------------------------------------------| +| ipAddress | * | | This specifies the IP address (or host name) where the meter can be found. | +| refreshDelay | | 5 | This specifies the interval in seconds used by the binding to read updated values from the meter. | -This should specify the IP address / host where the meter can be found. +Note that update rate of the P1 Meter itself depends on the frequency of the telegrams it receives from the Smart Meter. +For DSMR5 meters this is generally once per second, for older versions the frequency is much lower. -`Refresh Interval` +Example of configuration through a .thing file: -This specifies the interval in seconds used by the binding to read updated values from the meter. - -Note that update rate of the P1 Meter itself depends on the telegrams it receives from the Smart Meter. +``` +Thing homewizard:p1_wifi_meter:my_meter [ ipAddress="192.178.1.67", refreshDelay=5 ] +``` ## Channels -| Channel ID | Item Type | Description | -|-----------------------|---------------|--------------------------------------------------------------------------------------------| -| total_power_import_t1 | Number:Power | The most recently reported total imported power in KWh by counter 1 | -| total_power_import_t2 | Number:Power | The most recently reported total imported power in KWh by counter 2 | -| total_power_export_t1 | Number:Power | The most recently reported total exported power in KWh by counter 1 | -| total_power_export_t2 | Number:Power | The most recently reported total exported power in KWh by counter 2 | -| active_power | Number:Power | The current net total power in W. It will be below 0 if power is currently being exported. | -| active_power_l1 | Number:Power | The current net total power in W for phase 1. | -| active_power_l2 | Number:Power | The current net total power in W for phase 2. | -| active_power_l3 | Number:Power | The current net total power in W for phase 3. | -| total_gas | Number:Volume | The most recently reported total imported gas in m^3. | -| gas_timestamp | Number | The time stamp of the total_gas measurement. | +| Channel ID | Item Type | Description | +|------------------------|---------------|--------------------------------------------------------------------------------------------| +| total_energy_import_t1 | Number:Energy | The most recently reported total imported energy in kWh by counter 1. | +| total_energy_import_t2 | Number:Energy | The most recently reported total imported energy in kWh by counter 2. | +| total_energy_export_t1 | Number:Energy | The most recently reported total exported energy in kWh by counter 1. | +| total_energy_export_t2 | Number:Energy | The most recently reported total exported energy in kWh by counter 2. | +| active_power | Number:Power | The current net total power in W. It will be below 0 if power is currently being exported. | +| active_power_l1 | Number:Power | The current net total power in W for phase 1. | +| active_power_l2 | Number:Power | The current net total power in W for phase 2. | +| active_power_l3 | Number:Power | The current net total power in W for phase 3. | +| total_gas | Number:Volume | The most recently reported total imported gas in m^3. | +| gas_timestamp | DateTime | The time stamp of the total_gas measurement. | + + +Example of configuration through a .items file: + +``` +Number:Energy Energy_Import_T1 "Imported Energy T1 [%.0f kWh]" {channel="homewizard:p1_wifi_meter:my_meter:total_energy_import_t1" } +Number:Power Active_Power_L1 "Active Power Phase 1 [%.1f W]" {channel="homewizard:p1_wifi_meter:my_meter:active_power_l1" } +DateTime Gas_Update "Gas Update Time [%1$tH:%1$tM]" {channel="homewizard:p1_wifi_meter:my_meter:gas_timestamp" } +``` diff --git a/bundles/org.openhab.binding.homewizard/src/main/java/org/openhab/binding/homewizard/data/P1Payload.java b/bundles/org.openhab.binding.homewizard/src/main/java/org/openhab/binding/homewizard/data/P1Payload.java index b557c094e8ba6..f7c45d06d5ab8 100644 --- a/bundles/org.openhab.binding.homewizard/src/main/java/org/openhab/binding/homewizard/data/P1Payload.java +++ b/bundles/org.openhab.binding.homewizard/src/main/java/org/openhab/binding/homewizard/data/P1Payload.java @@ -14,6 +14,8 @@ import org.eclipse.jdt.annotation.NonNullByDefault; +import com.google.gson.annotations.SerializedName; + /** * Class that provides storage for the json object obtained from the P1 meter API * @@ -22,28 +24,34 @@ */ @NonNullByDefault public class P1Payload { - private int smr_version; - private String meter_model = ""; - private String wifi_ssid = ""; - private int wifi_strength; - private double total_power_import_t1_kwh; - private double total_power_import_t2_kwh; - private double total_power_export_t1_kwh; - private double total_power_export_t2_kwh; - private double active_power_w; - private double active_power_l1_w; - private double active_power_l2_w; - private double active_power_l3_w; - private double total_gas_m3; - private long gas_timestamp; + private int smrVersion = 0; + private String meterModel = ""; + private String wifiSsid = ""; + private int wifiStrength = 0; + + @SerializedName("total_power_import_t1_kwh") + private double totalEnergyImportT1Kwh; + @SerializedName("total_power_import_t2_kwh") + private double totalEnergyImportT2Kwh; + @SerializedName("total_power_export_t1_kwh") + private double totalEnergyExportT1Kwh; + @SerializedName("total_power_export_t2_kwh") + private double totalEnergyExportT2Kwh; + + private double activePowerW; + private double activePowerL1W; + private double activePowerL2W; + private double activePowerL3W; + private double totalGasM3; + private long gasTimestamp; /** * Getter for the smart meter version * * @return The most recent smart meter version obtained from the API */ - public int getSmr_version() { - return smr_version; + public int getSmrVersion() { + return smrVersion; } /** @@ -51,8 +59,8 @@ public int getSmr_version() { * * @param smrVersion The smart meter version to set */ - public void setSmr_version(int smrVersion) { - smr_version = smrVersion; + public void setSmrVersion(int smrVersion) { + this.smrVersion = smrVersion; } /** @@ -60,8 +68,8 @@ public void setSmr_version(int smrVersion) { * * @return meter model */ - public String getMeter_model() { - return meter_model; + public String getMeterModel() { + return meterModel; } /** @@ -69,8 +77,8 @@ public String getMeter_model() { * * @param meterModel meter model */ - public void setMeter_model(String meterModel) { - meter_model = meterModel; + public void setMeterModel(String meterModel) { + this.meterModel = meterModel; } /** @@ -78,8 +86,8 @@ public void setMeter_model(String meterModel) { * * @return the meter's wifi sid */ - public String getWifi_ssid() { - return wifi_ssid; + public String getWifiSsid() { + return wifiSsid; } /** @@ -87,8 +95,8 @@ public String getWifi_ssid() { * * @param wifiSsid wifi ssid */ - public void setWifi_ssid(String wifiSsid) { - wifi_ssid = wifiSsid; + public void setWifiSsid(String wifiSsid) { + this.wifiSsid = wifiSsid; } /** @@ -96,8 +104,8 @@ public void setWifi_ssid(String wifiSsid) { * * @return wifi rssi */ - public int getWifi_strength() { - return wifi_strength; + public int getWifiStrength() { + return wifiStrength; } /** @@ -105,80 +113,80 @@ public int getWifi_strength() { * * @param wifiStrength wifi rssi */ - public void setWifi_strength(int wifiStrength) { - wifi_strength = wifiStrength; + public void setWifiStrength(int wifiStrength) { + this.wifiStrength = wifiStrength; } /** - * Getter for the total imported power on counter 1 + * Getter for the total imported energy on counter 1 * - * @return total imported power on counter 1 + * @return total imported energy on counter 1 */ - public double getTotal_power_import_t1_kwh() { - return total_power_import_t1_kwh; + public double getTotalEnergyImportT1Kwh() { + return totalEnergyImportT1Kwh; } /** - * Setter for the total imported power on counter 1 + * Setter for the total imported energy on counter 1 * - * @param totalPowerImportT1Kwh total imported power on counter 1 + * @param totalEnergyImportT1Kwh total imported energy on counter 1 */ - public void setTotal_power_import_t1_kwh(double totalPowerImportT1Kwh) { - total_power_import_t1_kwh = totalPowerImportT1Kwh; + public void setTotalEnergyImportT1Kwh(double totalEnergyImportT1Kwh) { + this.totalEnergyImportT1Kwh = totalEnergyImportT1Kwh; } /** - * Getter for the total imported power on counter 2 + * Getter for the total imported energy on counter 2 * - * @return total imported power on counter 2 + * @return total imported energy on counter 2 */ - public double getTotal_power_import_t2_kwh() { - return total_power_import_t2_kwh; + public double getTotalEnergyImportT2Kwh() { + return totalEnergyImportT2Kwh; } /** - * Setter for the total imported power on counter 2 + * Setter for the total imported energy on counter 2 * - * @param totalPowerImportT2Kwh + * @param totalEnergyImportT2Kwh */ - public void setTotal_power_import_t2_kwh(double totalPowerImportT2Kwh) { - total_power_import_t2_kwh = totalPowerImportT2Kwh; + public void setTotalEnergyImportT2Kwh(double totalEnergyImportT2Kwh) { + this.totalEnergyImportT2Kwh = totalEnergyImportT2Kwh; } /** - * Getter for the total exported power on counter 1 + * Getter for the total exported energy on counter 1 * - * @return total exported power on counter 1 + * @return total exported energy on counter 1 */ - public double getTotal_power_export_t1_kwh() { - return total_power_export_t1_kwh; + public double getTotalEnergyExportT1Kwh() { + return totalEnergyExportT1Kwh; } /** - * Setter for the total exported power on counter 1 + * Setter for the total exported energy on counter 1 * - * @param totalPowerExportT1Kwh + * @param totalEnergyExportT1Kwh */ - public void setTotal_power_export_t1_kwh(double totalPowerExportT1Kwh) { - total_power_export_t1_kwh = totalPowerExportT1Kwh; + public void setTotalEnergyExportT1Kwh(double totalEnergyExportT1Kwh) { + this.totalEnergyExportT1Kwh = totalEnergyExportT1Kwh; } /** - * Getter for the total exported power on counter 2 + * Getter for the total exported energy on counter 2 * - * @return total exported power on counter 2 + * @return total exported energy on counter 2 */ - public double getTotal_power_export_t2_kwh() { - return total_power_export_t2_kwh; + public double getTotalEnergyExportT2Kwh() { + return totalEnergyExportT2Kwh; } /** - * Setter for the total exported power on counter 2 + * Setter for the total exported energy on counter 2 * - * @param totalPowerExportT2Kwh + * @param totalEnergyExportT2Kwh */ - public void setTotal_power_export_t2_kwh(double totalPowerExportT2Kwh) { - total_power_export_t2_kwh = totalPowerExportT2Kwh; + public void setTotalEnergyExportT2Kwh(double totalEnergyExportT2Kwh) { + this.totalEnergyExportT2Kwh = totalEnergyExportT2Kwh; } /** @@ -186,8 +194,8 @@ public void setTotal_power_export_t2_kwh(double totalPowerExportT2Kwh) { * * @return current active total power */ - public double getActive_power_w() { - return active_power_w; + public double getActivePowerW() { + return activePowerW; } /** @@ -195,8 +203,8 @@ public double getActive_power_w() { * * @param activePowerW */ - public void setActive_power_w(double activePowerW) { - active_power_w = activePowerW; + public void setActivePowerW(double activePowerW) { + this.activePowerW = activePowerW; } /** @@ -204,8 +212,8 @@ public void setActive_power_w(double activePowerW) { * * @return current active total power on phase 1 */ - public double getActive_power_l1_w() { - return active_power_l1_w; + public double getActivePowerL1W() { + return activePowerL1W; } /** @@ -213,8 +221,8 @@ public double getActive_power_l1_w() { * * @param activePowerL1W current active total power on phase 1 */ - public void setActive_power_l1_w(double activePowerL1W) { - active_power_l1_w = activePowerL1W; + public void setActivePowerL1W(double activePowerL1W) { + this.activePowerL1W = activePowerL1W; } /** @@ -222,8 +230,8 @@ public void setActive_power_l1_w(double activePowerL1W) { * * @return current active total power on phase 2 */ - public double getActive_power_l2_w() { - return active_power_l2_w; + public double getActivePowerL2W() { + return activePowerL2W; } /** @@ -231,8 +239,8 @@ public double getActive_power_l2_w() { * * @param activePowerL2W current active total power on phase 2 */ - public void setActive_power_l2_w(double activePowerL2W) { - active_power_l2_w = activePowerL2W; + public void setActivePowerL2W(double activePowerL2W) { + this.activePowerL2W = activePowerL2W; } /** @@ -240,8 +248,8 @@ public void setActive_power_l2_w(double activePowerL2W) { * * @return current active total power on phase 3 */ - public double getActive_power_l3_w() { - return active_power_l3_w; + public double getActivePowerL3W() { + return activePowerL3W; } /** @@ -249,8 +257,8 @@ public double getActive_power_l3_w() { * * @param activePowerL3W current active total power on phase 3 */ - public void setActive_power_l3_w(double activePowerL3W) { - active_power_l3_w = activePowerL3W; + public void setActivePowerL3W(double activePowerL3W) { + this.activePowerL3W = activePowerL3W; } /** @@ -258,8 +266,8 @@ public void setActive_power_l3_w(double activePowerL3W) { * * @return total imported gas volume */ - public double getTotal_gas_m3() { - return total_gas_m3; + public double getTotalGasM3() { + return totalGasM3; } /** @@ -267,8 +275,8 @@ public double getTotal_gas_m3() { * * @param totalGasM3 total imported gas volume */ - public void setTotal_gas_m3(double totalGasM3) { - total_gas_m3 = totalGasM3; + public void setTotalGasM3(double totalGasM3) { + this.totalGasM3 = totalGasM3; } /** @@ -276,8 +284,8 @@ public void setTotal_gas_m3(double totalGasM3) { * * @return time stamp of the last gas update */ - public long getGas_timestamp() { - return gas_timestamp; + public long getGasTimestamp() { + return gasTimestamp; } /** @@ -285,16 +293,16 @@ public long getGas_timestamp() { * * @param gasTimestamp time stamp of the last gas update */ - public void setGas_timestamp(long gasTimestamp) { - gas_timestamp = gasTimestamp; + public void setGasTimestamp(long gasTimestamp) { + this.gasTimestamp = gasTimestamp; } @Override public String toString() { return String.format("P1 [version: %d model: %s ssid: %s signal: %d" - + " imp1: %f imp2: %f exp1: %f exp2: %f active: %f active1: %f active2: %f active3: %f gas: %f timestamp: %d]", - smr_version, meter_model, wifi_ssid, wifi_strength, total_power_import_t1_kwh, - total_power_import_t2_kwh, total_power_export_t1_kwh, total_power_export_t2_kwh, active_power_w, - active_power_l1_w, active_power_l2_w, active_power_l3_w, total_gas_m3, gas_timestamp); + + " imp1: %f imp2: %f exp1: %f exp2: %f active: %f active1: %f active2: %f active3: %f gas: %f timestamp: %.0f]", + smrVersion, meterModel, wifiSsid, wifiStrength, totalEnergyImportT1Kwh, totalEnergyImportT2Kwh, + totalEnergyExportT1Kwh, totalEnergyExportT2Kwh, activePowerW, activePowerL1W, activePowerL2W, + activePowerL3W, totalGasM3, gasTimestamp); } } diff --git a/bundles/org.openhab.binding.homewizard/src/main/java/org/openhab/binding/homewizard/internal/HomeWizardBindingConstants.java b/bundles/org.openhab.binding.homewizard/src/main/java/org/openhab/binding/homewizard/internal/HomeWizardBindingConstants.java index 5b94f0a44fa67..266fabba0a995 100644 --- a/bundles/org.openhab.binding.homewizard/src/main/java/org/openhab/binding/homewizard/internal/HomeWizardBindingConstants.java +++ b/bundles/org.openhab.binding.homewizard/src/main/java/org/openhab/binding/homewizard/internal/HomeWizardBindingConstants.java @@ -30,10 +30,10 @@ public class HomeWizardBindingConstants { public static final ThingTypeUID THING_TYPE_P1_WIFI_METER = new ThingTypeUID(BINDING_ID, "p1_wifi_meter"); // List of all Channel ids - public static final String CHANNEL_POWER_IMPORT_T1 = "total_power_import_t1"; - public static final String CHANNEL_POWER_IMPORT_T2 = "total_power_import_t2"; - public static final String CHANNEL_POWER_EXPORT_T1 = "total_power_export_t1"; - public static final String CHANNEL_POWER_EXPORT_T2 = "total_power_export_t2"; + public static final String CHANNEL_ENERGY_IMPORT_T1 = "total_energy_import_t1"; + public static final String CHANNEL_ENERGY_IMPORT_T2 = "total_energy_import_t2"; + public static final String CHANNEL_ENERGY_EXPORT_T1 = "total_energy_export_t1"; + public static final String CHANNEL_ENERGY_EXPORT_T2 = "total_energy_export_t2"; public static final String CHANNEL_ACTIVE_POWER = "active_power"; public static final String CHANNEL_ACTIVE_POWER_L1 = "active_power_l1"; public static final String CHANNEL_ACTIVE_POWER_L2 = "active_power_l2"; diff --git a/bundles/org.openhab.binding.homewizard/src/main/java/org/openhab/binding/homewizard/internal/HomeWizardConfiguration.java b/bundles/org.openhab.binding.homewizard/src/main/java/org/openhab/binding/homewizard/internal/HomeWizardConfiguration.java index 1794e2e7d807e..0a2d7dc1aeaa7 100644 --- a/bundles/org.openhab.binding.homewizard/src/main/java/org/openhab/binding/homewizard/internal/HomeWizardConfiguration.java +++ b/bundles/org.openhab.binding.homewizard/src/main/java/org/openhab/binding/homewizard/internal/HomeWizardConfiguration.java @@ -22,18 +22,13 @@ @NonNullByDefault public class HomeWizardConfiguration { - public HomeWizardConfiguration() { - refreshDelay = 5; - ipAddress = ""; - } - /** * IP Address or host for the P1 Meter */ - public String ipAddress; + public String ipAddress = ""; /** * Refresh delay in seconds */ - public Integer refreshDelay; + public Integer refreshDelay = 5; } diff --git a/bundles/org.openhab.binding.homewizard/src/main/java/org/openhab/binding/homewizard/internal/HomeWizardHandler.java b/bundles/org.openhab.binding.homewizard/src/main/java/org/openhab/binding/homewizard/internal/HomeWizardHandler.java index aa1659ccf64dd..d321654494860 100644 --- a/bundles/org.openhab.binding.homewizard/src/main/java/org/openhab/binding/homewizard/internal/HomeWizardHandler.java +++ b/bundles/org.openhab.binding.homewizard/src/main/java/org/openhab/binding/homewizard/internal/HomeWizardHandler.java @@ -13,8 +13,6 @@ package org.openhab.binding.homewizard.internal; import java.io.IOException; -import java.math.BigDecimal; -import java.util.Map; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.Lock; @@ -24,7 +22,9 @@ import org.eclipse.jdt.annotation.Nullable; import org.openhab.binding.homewizard.data.P1Payload; import org.openhab.core.io.net.http.HttpUtil; +import org.openhab.core.library.types.DateTimeType; import org.openhab.core.library.types.QuantityType; +import org.openhab.core.library.unit.SIUnits; import org.openhab.core.library.unit.Units; import org.openhab.core.thing.ChannelUID; import org.openhab.core.thing.Thing; @@ -35,6 +35,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.google.gson.FieldNamingPolicy; import com.google.gson.Gson; import com.google.gson.GsonBuilder; @@ -55,11 +56,9 @@ public class HomeWizardHandler extends BaseThingHandler { private @Nullable ScheduledFuture pollingJob; private int currentRefreshDelay = 0; - private final GsonBuilder builder = new GsonBuilder(); - private final Gson gson = builder.create(); + private final Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES) + .create(); - private final Lock configurationLock = new ReentrantLock(); // to protected fields accessed in init/dispose AND the - // poller private String apiURL = ""; private String meterModel = ""; @@ -89,38 +88,10 @@ public void initialize() { config = getConfigAs(HomeWizardConfiguration.class); if (configure()) { - stopPolling(true); startPolling(); } } - /** - * Handle updates to the configuration gracefully - */ - @Override - public void handleConfigurationUpdate(Map configurationParameters) { - Object cfgObject = configurationParameters.get("refreshDelay"); - if (cfgObject != null) { - BigDecimal bd = (BigDecimal) cfgObject; - config.refreshDelay = bd.intValue(); - } - - cfgObject = configurationParameters.get("ipAddress"); - if (cfgObject != null) { - config.ipAddress = (String) cfgObject; - } - - if (configure()) { - // If the new configuration is proper, stop the poller if needed - stopPolling(true); - // Then start it again if it was stopped - startPolling(); - } else { - // Stop polling if the new config is invalid - stopPolling(false); - } - } - /** * Check the current configuration * @@ -128,19 +99,12 @@ public void handleConfigurationUpdate(Map configurationParameter */ private boolean configure() { if (config.ipAddress.trim().isEmpty()) { - // since it is marked as required, initialize() should not be called if this field is empty updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Missing ipAddress/host configuration"); return false; } else { updateStatus(ThingStatus.UNKNOWN); - - try { - configurationLock.lock(); - apiURL = String.format("http://%s/api/v1/data", config.ipAddress.trim()); - } finally { - configurationLock.unlock(); - } + apiURL = String.format("http://%s/api/v1/data", config.ipAddress.trim()); return true; } } @@ -150,24 +114,20 @@ private boolean configure() { */ @Override public void dispose() { - stopPolling(false); + stopPolling(); } /** * Stop the polling job - * - * @param onlyIfNeeded if true polling will only actually stop if a new refresh interval should be set */ - private void stopPolling(boolean onlyIfNeeded) { + private void stopPolling() { try { pollingJobLock.lock(); if (pollingJob != null && !pollingJob.isCancelled()) { - if (!onlyIfNeeded || config.refreshDelay != currentRefreshDelay) { - if (pollingJob != null) { - pollingJob.cancel(true); - } - pollingJob = null; + if (pollingJob != null) { + pollingJob.cancel(true); } + pollingJob = null; } } finally { pollingJobLock.unlock(); @@ -201,74 +161,87 @@ private void startPolling() { * The actual polling loop */ private void pollingCode() { - final String query; final String result; - // get a local copy of the configuration parameters try { - configurationLock.lock(); - query = apiURL; - } catch (Exception e) { - return; - } finally { - configurationLock.unlock(); - } - - try { - result = HttpUtil.executeUrl("GET", query, 750); + result = HttpUtil.executeUrl("GET", apiURL, 750); } catch (IOException e) { updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Unable to query P1 Meter"); return; } if (result.trim().isEmpty()) { - logger.warn("P1 Wi-Fi meter API at URI {} returned empty result", apiURL); updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "P1 Meter API returned empty status"); return; } P1Payload payload = gson.fromJson(result, P1Payload.class); - if (payload != null) { - if (payload.getMeter_model() == "") { - updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Results from API seem empty"); - return; - } + if (payload == null) { + updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, + "Unable to parse response from P1 meter"); + return; + } - updateStatus(ThingStatus.ONLINE); + if ("".equals(payload.getMeterModel())) { + updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Results from API are empty"); + return; + } - if (meterModel != payload.getMeter_model()) { - meterModel = payload.getMeter_model(); - updateProperty(HomeWizardBindingConstants.PROPERTY_METER_MODEL, meterModel); - } + updateStatus(ThingStatus.ONLINE); - if (meterVersion != payload.getSmr_version()) { - meterVersion = payload.getSmr_version(); - updateProperty(HomeWizardBindingConstants.PROPERTY_METER_VERSION, String.format("%d", meterVersion)); - } + if (!meterModel.equals(payload.getMeterModel())) { + meterModel = payload.getMeterModel(); + updateProperty(HomeWizardBindingConstants.PROPERTY_METER_MODEL, meterModel); + } - updateState(HomeWizardBindingConstants.CHANNEL_POWER_IMPORT_T1, - new QuantityType<>(payload.getTotal_power_import_t1_kwh(), Units.WATT)); - updateState(HomeWizardBindingConstants.CHANNEL_POWER_IMPORT_T2, - new QuantityType<>(payload.getTotal_power_import_t2_kwh(), Units.WATT)); - updateState(HomeWizardBindingConstants.CHANNEL_POWER_EXPORT_T1, - new QuantityType<>(payload.getTotal_power_export_t1_kwh(), Units.WATT)); - updateState(HomeWizardBindingConstants.CHANNEL_POWER_EXPORT_T2, - new QuantityType<>(payload.getTotal_power_export_t2_kwh(), Units.WATT)); - - updateState(HomeWizardBindingConstants.CHANNEL_ACTIVE_POWER, - new QuantityType<>(payload.getActive_power_w(), Units.WATT)); - updateState(HomeWizardBindingConstants.CHANNEL_ACTIVE_POWER_L1, - new QuantityType<>(payload.getActive_power_l1_w(), Units.WATT)); - updateState(HomeWizardBindingConstants.CHANNEL_ACTIVE_POWER_L2, - new QuantityType<>(payload.getActive_power_l2_w(), Units.WATT)); - updateState(HomeWizardBindingConstants.CHANNEL_ACTIVE_POWER_L3, - new QuantityType<>(payload.getActive_power_l3_w(), Units.WATT)); - - updateState(HomeWizardBindingConstants.CHANNEL_TOTAL_GAS, - new QuantityType<>(payload.getTotal_gas_m3(), Units.ONE)); // could convert, 1m^3 = 1000 liters - updateState(HomeWizardBindingConstants.CHANNEL_GAS_TIMESTAMP, - new QuantityType<>(payload.getGas_timestamp(), Units.SECOND)); + if (meterVersion != payload.getSmrVersion()) { + meterVersion = payload.getSmrVersion(); + updateProperty(HomeWizardBindingConstants.PROPERTY_METER_VERSION, String.format("%d", meterVersion)); } + + updateState(HomeWizardBindingConstants.CHANNEL_ENERGY_IMPORT_T1, + new QuantityType<>(payload.getTotalEnergyImportT1Kwh(), Units.KILOWATT_HOUR)); + updateState(HomeWizardBindingConstants.CHANNEL_ENERGY_IMPORT_T2, + new QuantityType<>(payload.getTotalEnergyImportT2Kwh(), Units.KILOWATT_HOUR)); + updateState(HomeWizardBindingConstants.CHANNEL_ENERGY_EXPORT_T1, + new QuantityType<>(payload.getTotalEnergyExportT1Kwh(), Units.KILOWATT_HOUR)); + updateState(HomeWizardBindingConstants.CHANNEL_ENERGY_EXPORT_T2, + new QuantityType<>(payload.getTotalEnergyExportT2Kwh(), Units.KILOWATT_HOUR)); + + updateState(HomeWizardBindingConstants.CHANNEL_ACTIVE_POWER, + new QuantityType<>(payload.getActivePowerW(), Units.WATT)); + updateState(HomeWizardBindingConstants.CHANNEL_ACTIVE_POWER_L1, + new QuantityType<>(payload.getActivePowerL1W(), Units.WATT)); + updateState(HomeWizardBindingConstants.CHANNEL_ACTIVE_POWER_L2, + new QuantityType<>(payload.getActivePowerL2W(), Units.WATT)); + updateState(HomeWizardBindingConstants.CHANNEL_ACTIVE_POWER_L3, + new QuantityType<>(payload.getActivePowerL3W(), Units.WATT)); + + updateState(HomeWizardBindingConstants.CHANNEL_TOTAL_GAS, + new QuantityType<>(payload.getTotalGasM3(), SIUnits.CUBIC_METRE)); + + // 210119164000 + long dtv = payload.getGasTimestamp(); + long seconds = dtv % 100; + + dtv /= 100; + long minutes = dtv % 100; + + dtv /= 100; + long hours = dtv % 100; + + dtv /= 100; + long day = dtv % 100; + + dtv /= 100; + long month = dtv % 100; + + dtv /= 100; + long year = dtv + 2000; // Where (When?) have I seen this before? + + DateTimeType dtt = DateTimeType + .valueOf(String.format("%04d-%02d-%02dT%02d:%02d:%02d", year, month, day, hours, minutes, seconds)); + updateState(HomeWizardBindingConstants.CHANNEL_GAS_TIMESTAMP, dtt); } } diff --git a/bundles/org.openhab.binding.homewizard/src/main/resources/OH-INF/thing/thing-types.xml b/bundles/org.openhab.binding.homewizard/src/main/resources/OH-INF/thing/thing-types.xml index e92602ea95845..bb7a6c20ec5c3 100644 --- a/bundles/org.openhab.binding.homewizard/src/main/resources/OH-INF/thing/thing-types.xml +++ b/bundles/org.openhab.binding.homewizard/src/main/resources/OH-INF/thing/thing-types.xml @@ -9,10 +9,10 @@ This thing provides the measurement data that is available through the http interface of the P1 Meter. - - - - + + + + @@ -40,36 +40,36 @@ - - Number:Power - - This channel provides the most recently reported total imported power in KWh by counter 1, most commonly + + Number:Energy + + This channel provides the most recently reported total imported energy in kWh by counter 1, most commonly used for import during the night or weekend. - - Number:Power - + + Number:Energy + - This channel provides the most recently reported total imported power in KWh by counter 2, most commonly + This channel provides the most recently reported total imported energy in kWh by counter 2, most commonly used for import during the day. - - Number:Power - + + Number:Energy + - This channel provides the most recently reported total exported power in KWh by counter 1, most commonly + This channel provides the most recently reported total exported energy in kWh by counter 1, most commonly used for export during the night or weekend. - - Number:Power - + + Number:Energy + - This channel provides the most recently reported total exported power in KWh by counter 2, most commonly + This channel provides the most recently reported total exported energy in kWh by counter 2, most commonly used for export during the day. @@ -121,8 +121,8 @@ - Number - + DateTime + This channel provides the time stamp of the total_gas measurement. diff --git a/bundles/org.openhab.binding.homewizard/src/test/java/org/openhab/binding/homewizard/P1PayloadTest.java b/bundles/org.openhab.binding.homewizard/src/test/java/org/openhab/binding/homewizard/P1PayloadTest.java deleted file mode 100644 index f5469b74f814a..0000000000000 --- a/bundles/org.openhab.binding.homewizard/src/test/java/org/openhab/binding/homewizard/P1PayloadTest.java +++ /dev/null @@ -1,153 +0,0 @@ -/** - * Copyright (c) 2010-2021 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.homewizard; - -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.closeTo; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.openhab.binding.homewizard.data.P1Payload; - -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; - -/** - * Tests {@link P1Payload}. - * - * @author Daniƫl van Os - Initial contribution - */ -public class P1PayloadTest { - - private P1Payload payload; - - @BeforeEach - public void setUp() { - payload = new P1Payload(); - } - - @Test - public void stringProperiesTest() { - String model = "Nice Model"; - String ssid = "FreeWifi"; - - payload.setMeter_model(model); - payload.setWifi_ssid(ssid); - - assertThat(payload.getMeter_model(), is(model)); - assertThat(payload.getWifi_ssid(), is(ssid)); - } - - @Test - public void intPropertiesTest() { - int version = 42; - int rssi = 1337; - - payload.setSmr_version(version); - payload.setWifi_strength(rssi); - - assertThat(payload.getSmr_version(), is(version)); - assertThat(payload.getWifi_strength(), is(rssi)); - } - - @Test - public void longPropertiesTest() { - long ts = 137137137; - payload.setGas_timestamp(ts); - assertThat(payload.getGas_timestamp(), is(ts)); - } - - @Test - public void doublePropertiesTest() { - double it1 = 1.337; - double it2 = 2.137; - double et1 = 3.3379; - double et2 = 4.1379; - double apt = 5.2734337; - double ap1 = 6.2746537; - double ap2 = 7.2735467; - double ap3 = 8.5642737; - double gas = 9.784352789435; - - payload.setTotal_power_import_t1_kwh(it1); - payload.setTotal_power_import_t2_kwh(it2); - payload.setTotal_power_export_t1_kwh(et1); - payload.setTotal_power_export_t2_kwh(et2); - - payload.setActive_power_w(apt); - payload.setActive_power_l1_w(ap1); - payload.setActive_power_l2_w(ap2); - payload.setActive_power_l3_w(ap3); - - payload.setTotal_gas_m3(gas); - - assertThat(payload.getTotal_power_import_t1_kwh(), is(it1)); - assertThat(payload.getTotal_power_import_t2_kwh(), is(it2)); - assertThat(payload.getTotal_power_export_t1_kwh(), is(et1)); - assertThat(payload.getTotal_power_export_t2_kwh(), is(et2)); - - assertThat(payload.getActive_power_w(), is(apt)); - assertThat(payload.getActive_power_l1_w(), is(ap1)); - assertThat(payload.getActive_power_l2_w(), is(ap2)); - assertThat(payload.getActive_power_l3_w(), is(ap3)); - - assertThat(payload.getTotal_gas_m3(), is(gas)); - } - - @Test - public void jsonTest() { - final GsonBuilder builder = new GsonBuilder(); - final Gson gson = builder.create(); - final double delta = 0.0005; - - int version = 42; - String model = "Nice Model"; - String ssid = "FreeWifi"; - int rssi = 1337; - double it1 = 1.337; - double it2 = 2.137; - double et1 = 3.3379; - double et2 = 4.1379; - double apt = 5.2734337; - double ap1 = 6.2746537; - double ap2 = 7.2735467; - double ap3 = 8.5642737; - double gas = 9.784352789435; - long ts = 137137137; - - String json = String.format("{\"smr_version\":%d,\"meter_model\":\"%s\",\"wifi_ssid\":\"%s\"," - + "\"wifi_strength\":%d,\"total_power_import_t1_kwh\":%.3f," - + "\"total_power_import_t2_kwh\":%.3f,\"total_power_export_t1_kwh\":%.3f," - + "\"total_power_export_t2_kwh\":%.3f,\"active_power_w\":%.3f,\"active_power_l1_w\":%.3f," - + "\"active_power_l2_w\":%.3f,\"active_power_l3_w\":%.3f,\"total_gas_m3\":%.3f,\"gas_timestamp\":%d}", - version, model, ssid, rssi, it1, it2, et1, et2, apt, ap1, ap2, ap3, gas, ts); - - payload = gson.fromJson(json, P1Payload.class); - - assertThat(payload.getMeter_model(), is(model)); - assertThat(payload.getWifi_ssid(), is(ssid)); - assertThat(payload.getSmr_version(), is(version)); - assertThat(payload.getWifi_strength(), is(rssi)); - assertThat(payload.getGas_timestamp(), is(ts)); - assertThat(payload.getTotal_power_import_t1_kwh(), closeTo(it1, delta)); - assertThat(payload.getTotal_power_import_t2_kwh(), closeTo(it2, delta)); - assertThat(payload.getTotal_power_export_t1_kwh(), closeTo(et1, delta)); - assertThat(payload.getTotal_power_export_t2_kwh(), closeTo(et2, delta)); - assertThat(payload.getActive_power_w(), closeTo(apt, delta)); - assertThat(payload.getActive_power_l1_w(), closeTo(ap1, delta)); - assertThat(payload.getActive_power_l2_w(), closeTo(ap2, delta)); - assertThat(payload.getActive_power_l3_w(), closeTo(ap3, delta)); - assertThat(payload.getTotal_gas_m3(), closeTo(gas, delta)); - } -}