]> git.basschouten.com Git - openhab-addons.git/commitdiff
[tibber] Enhancement, added channel and channel corrections due to API changes (...
authorStian Kjoglum <47720690+kjoglum@users.noreply.github.com>
Thu, 14 Jan 2021 00:35:03 +0000 (01:35 +0100)
committerGitHub <noreply@github.com>
Thu, 14 Jan 2021 00:35:03 +0000 (16:35 -0800)
Signed-off-by: Stian Kjoglum <stiankj@online.no>
bundles/org.openhab.binding.tibber/README.md
bundles/org.openhab.binding.tibber/src/main/java/org/openhab/binding/tibber/internal/TibberBindingConstants.java
bundles/org.openhab.binding.tibber/src/main/java/org/openhab/binding/tibber/internal/handler/TibberHandler.java
bundles/org.openhab.binding.tibber/src/main/java/org/openhab/binding/tibber/internal/handler/TibberPriceConsumptionHandler.java
bundles/org.openhab.binding.tibber/src/main/resources/OH-INF/thing/thing-types.xml

index 2e71ca04f9b4ef2aa9ef489d3c2f45a4278aa723..cd25861c47e1cc69ab6abaf2f786f5c521a231f7 100644 (file)
@@ -23,6 +23,7 @@ Tibber Default:
 |--------------------|-----------------------------------------|-----------|
 | Current Total      | Current Total Price (energy + tax)      | True      |
 | Starts At          | Current Price Timestamp                 | True      |
+| Current Level      | Current Price Level                     | True      |
 | Daily Cost         | Daily Cost (last/previous day)          | True      |
 | Daily Consumption  | Daily Consumption (last/previous day)   | True      |
 | Daily From         | Timestamp (daily from)                  | True      |
@@ -104,6 +105,7 @@ demo.items:
 ```
 Number:Dimensionless       TibberAPICurrentTotal                 "Current Total Price [%.2f NOK]"            {channel="tibber:tibberapi:7cfae492:current_total"}
 DateTime                   TibberAPICurrentStartsAt              "Timestamp - Current Price"                 {channel="tibber:tibberapi:7cfae492:current_startsAt"}
+String                     TibberAPICurrentLevel                 "Price Level"                               {channel="tibber:tibberapi:7cfae492:current_level"}
 DateTime                   TibberAPIDailyFrom                    "Timestamp - Daily From"                    {channel="tibber:tibberapi:7cfae492:daily_from"}
 DateTime                   TibberAPIDailyTo                      "Timestamp - Daily To"                      {channel="tibber:tibberapi:7cfae492:daily_to"}
 Number:Dimensionless       TibberAPIDailyCost                    "Total Daily Cost [%.2f NOK]"               {channel="tibber:tibberapi:7cfae492:daily_cost"}
@@ -113,22 +115,22 @@ DateTime                   TibberAPIHourlyTo                     "Timestamp - Ho
 Number:Dimensionless       TibberAPIHourlyCost                   "Total Hourly Cost [%.2f NOK]"              {channel="tibber:tibberapi:7cfae492:hourly_cost"}
 Number:Energy              TibberAPIHourlyConsumption            "Total Hourly Consumption [%.2f kWh]"       {channel="tibber:tibberapi:7cfae492:hourly_consumption"}
 DateTime                   TibberAPILiveTimestamp                "Timestamp - Live Measurement"              {channel="tibber:tibberapi:7cfae492:live_timestamp"}
-Number:Power               TibberAPILivePower                    "Live Power Consumption [W]"                {channel="tibber:tibberapi:7cfae492:live_power"}
+Number:Power               TibberAPILivePower                    "Live Power Consumption [%.0f W]"           {channel="tibber:tibberapi:7cfae492:live_power"}
 Number:Energy              TibberAPILiveLastMeterConsumption     "Last Meter Consumption [%.2f kWh]"         {channel="tibber:tibberapi:7cfae492:live_lastMeterConsumption"}
 Number:Energy              TibberAPILiveAccumulatedConsumption   "Accumulated Consumption [%.2f kWh]"        {channel="tibber:tibberapi:7cfae492:live_accumulatedConsumption"}
 Number:Dimensionless       TibberAPILiveAccumulatedCost          "Accumulated Cost [%.2f NOK]"               {channel="tibber:tibberapi:7cfae492:live_accumulatedCost"}
 String                     TibberAPILiveCurrency                 "Currency"                                  {channel="tibber:tibberapi:7cfae492:live_currency"}
-Number:Power               TibberAPILiveMinPower                 "Min Power Consumption [W]"                 {channel="tibber:tibberapi:7cfae492:live_minPower"}
-Number:Power               TibberAPILiveAveragePower             "Average Power Consumption [W]"             {channel="tibber:tibberapi:7cfae492:live_averagePower"}
-Number:Power               TibberAPILiveMaxPower                 "Max Power Consumption [W]"                 {channel="tibber:tibberapi:7cfae492:live_maxPower"}
-Number:ElectricPotential   TibberAPILiveVoltage1                 "Live Voltage Phase 1 [V]"                  {channel="tibber:tibberapi:7cfae492:live_voltage1"}
-Number:ElectricPotential   TibberAPILiveVoltage2                 "Live Voltage Phase 2 [V]"                  {channel="tibber:tibberapi:7cfae492:live_voltage2"}
-Number:ElectricPotential   TibberAPILiveVoltage3                 "Live Voltage Phase 3 [V]"                  {channel="tibber:tibberapi:7cfae492:live_voltage3"}
-Number:ElectricCurrent     TibberAPILiveCurrent1                 "Live Current Phase 1 [A]"                  {channel="tibber:tibberapi:7cfae492:live_current1"}
-Number:ElectricCurrent     TibberAPILiveCurrent2                 "Live Current Phase 2 [A]"                  {channel="tibber:tibberapi:7cfae492:live_current2"}
-Number:ElectricCurrent     TibberAPILiveCurrent3                 "Live Current Phase 3 [A]"                  {channel="tibber:tibberapi:7cfae492:live_current3"}
-Number:Power               TibberAPILivePowerProduction          "Live Power Production [W]"                 {channel="tibber:tibberapi:7cfae492:live_powerProduction"}
-Number:Power               TibberAPILiveMinPowerproduction       "Min Power Production [W]"                  {channel="tibber:tibberapi:7cfae492:live_minPowerproduction"}
-Number:Power               TibberAPILiveMaxPowerproduction       "Max Power Production [W]"                  {channel="tibber:tibberapi:7cfae492:live_maxPowerproduction"}
+Number:Power               TibberAPILiveMinPower                 "Min Power Consumption [%.0f W]"            {channel="tibber:tibberapi:7cfae492:live_minPower"}
+Number:Power               TibberAPILiveAveragePower             "Average Power Consumption [%.0f W]"             {channel="tibber:tibberapi:7cfae492:live_averagePower"}
+Number:Power               TibberAPILiveMaxPower                 "Max Power Consumption [%.0f W]"            {channel="tibber:tibberapi:7cfae492:live_maxPower"}
+Number:ElectricPotential   TibberAPILiveVoltage1                 "Live Voltage Phase 1 [%.0 V]"              {channel="tibber:tibberapi:7cfae492:live_voltage1"}
+Number:ElectricPotential   TibberAPILiveVoltage2                 "Live Voltage Phase 2 [%.0 V]"              {channel="tibber:tibberapi:7cfae492:live_voltage2"}
+Number:ElectricPotential   TibberAPILiveVoltage3                 "Live Voltage Phase 3 [%.0 V]"              {channel="tibber:tibberapi:7cfae492:live_voltage3"}
+Number:ElectricCurrent     TibberAPILiveCurrent1                 "Live Current Phase 1 [%.1 A]"              {channel="tibber:tibberapi:7cfae492:live_current1"}
+Number:ElectricCurrent     TibberAPILiveCurrent2                 "Live Current Phase 2 [%.1 A]"              {channel="tibber:tibberapi:7cfae492:live_current2"}
+Number:ElectricCurrent     TibberAPILiveCurrent3                 "Live Current Phase 3 [%.1 A]"              {channel="tibber:tibberapi:7cfae492:live_current3"}
+Number:Power               TibberAPILivePowerProduction          "Live Power Production [%.0f W]"                 {channel="tibber:tibberapi:7cfae492:live_powerProduction"}
+Number:Power               TibberAPILiveMinPowerproduction       "Min Power Production [%.0f W]"                  {channel="tibber:tibberapi:7cfae492:live_minPowerproduction"}
+Number:Power               TibberAPILiveMaxPowerproduction       "Max Power Production [%.0f W]"                  {channel="tibber:tibberapi:7cfae492:live_maxPowerproduction"}
 Number:Energy              TibberAPILiveAccumulatedProduction    "Accumulated Production [%.2f kWh]"         {channel="tibber:tibberapi:7cfae492:live_accumulatedProduction"}
 ```
index 1b74ac06d149fd1b21cbde97165c06de91d4c11d..206cbd90d3270159f4c20dfe6e9b466e1c1e4521 100644 (file)
@@ -43,6 +43,7 @@ public class TibberBindingConstants {
     // List of all Channel ids - Used for queries
     public static final String CURRENT_TOTAL = "current_total";
     public static final String CURRENT_STARTSAT = "current_startsAt";
+    public static final String CURRENT_LEVEL = "current_level";
     public static final String DAILY_FROM = "daily_from";
     public static final String DAILY_TO = "daily_to";
     public static final String DAILY_COST = "daily_cost";
index 8c5fe16f13f1f026deb19d1fbeafd29e61a756cf..2e45e801498f5119f4ea31bf75bc46723b91a632 100644 (file)
@@ -102,6 +102,7 @@ public class TibberHandler extends BaseThingHandler {
     }
 
     public void getTibberParameters() {
+        String response = "";
         try {
             httpHeader.put("cache-control", "no-cache");
             httpHeader.put("content-type", JSON_CONTENT_TYPE);
@@ -109,12 +110,10 @@ public class TibberHandler extends BaseThingHandler {
 
             TibberPriceConsumptionHandler tibberQuery = new TibberPriceConsumptionHandler();
             InputStream connectionStream = tibberQuery.connectionInputStream(tibberConfig.getHomeid());
-            String response = HttpUtil.executeUrl("POST", BASE_URL, httpHeader, connectionStream, null,
-                    REQUEST_TIMEOUT);
+            response = HttpUtil.executeUrl("POST", BASE_URL, httpHeader, connectionStream, null, REQUEST_TIMEOUT);
 
             if (!response.contains("error") && !response.contains("<html>")) {
                 updateStatus(ThingStatus.ONLINE);
-
                 getURLInput(BASE_URL);
 
                 InputStream inputStream = tibberQuery.getRealtimeInputStream(tibberConfig.getHomeid());
@@ -141,10 +140,11 @@ public class TibberHandler extends BaseThingHandler {
     }
 
     public void getURLInput(String url) throws IOException {
+        String jsonResponse = "";
         TibberPriceConsumptionHandler tibberQuery = new TibberPriceConsumptionHandler();
 
         InputStream inputStream = tibberQuery.getInputStream(tibberConfig.getHomeid());
-        String jsonResponse = HttpUtil.executeUrl("POST", url, httpHeader, inputStream, null, REQUEST_TIMEOUT);
+        jsonResponse = HttpUtil.executeUrl("POST", url, httpHeader, inputStream, null, REQUEST_TIMEOUT);
         logger.debug("API response: {}", jsonResponse);
 
         if (!jsonResponse.contains("error") && !jsonResponse.contains("<html>")) {
@@ -163,13 +163,15 @@ public class TibberHandler extends BaseThingHandler {
                     updateState(CURRENT_TOTAL, new DecimalType(myObject.get("total").toString()));
                     String timestamp = myObject.get("startsAt").toString().substring(1, 20);
                     updateState(CURRENT_STARTSAT, new DateTimeType(timestamp));
+                    updateState(CURRENT_LEVEL, new StringType(myObject.get("level").toString()));
 
                 } catch (JsonSyntaxException e) {
                     updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
                             "Error communicating with Tibber API: " + e.getMessage());
                 }
             }
-            if (jsonResponse.contains("daily")) {
+            if (jsonResponse.contains("daily") && !jsonResponse.contains("\"daily\":{\"nodes\":[]")
+                    && !jsonResponse.contains("\"daily\":null")) {
                 try {
                     JsonObject myObject = (JsonObject) object.getAsJsonObject("data").getAsJsonObject("viewer")
                             .getAsJsonObject("home").getAsJsonObject("daily").getAsJsonArray("nodes").get(0);
@@ -188,7 +190,8 @@ public class TibberHandler extends BaseThingHandler {
                             "Error communicating with Tibber API: " + e.getMessage());
                 }
             }
-            if (jsonResponse.contains("hourly")) {
+            if (jsonResponse.contains("hourly") && !jsonResponse.contains("\"hourly\":{\"nodes\":[]")
+                    && !jsonResponse.contains("\"hourly\":null")) {
                 try {
                     JsonObject myObject = (JsonObject) object.getAsJsonObject("data").getAsJsonObject("viewer")
                             .getAsJsonObject("home").getAsJsonObject("hourly").getAsJsonArray("nodes").get(0);
@@ -257,7 +260,7 @@ public class TibberHandler extends BaseThingHandler {
                 updateState(channelID, new QuantityType<>(new BigDecimal(channelValue), Units.WATT));
             } else if (channelID.contains("voltage")) {
                 updateState(channelID, new QuantityType<>(new BigDecimal(channelValue), Units.VOLT));
-            } else if (channelID.contains("live_current")) {
+            } else if (channelID.contains("current")) {
                 updateState(channelID, new QuantityType<>(new BigDecimal(channelValue), Units.AMPERE));
             } else {
                 updateState(channelID, new DecimalType(channelValue));
@@ -307,6 +310,7 @@ public class TibberHandler extends BaseThingHandler {
             WebSocketClient client = this.client;
             if (client == null) {
                 client = new WebSocketClient(sslContextFactory, websocketExecutor);
+                client.setMaxIdleTimeout(600 * 1000);
                 this.client = client;
             }
 
@@ -352,7 +356,7 @@ public class TibberHandler extends BaseThingHandler {
             } catch (IOException e) {
                 logger.warn("Websocket Close Exception: {}", e.getMessage());
             }
-            session.close(0, "Tibber websocket disposed");
+            session.close();
             this.session = null;
             this.socket = null;
         }
@@ -470,14 +474,14 @@ public class TibberHandler extends BaseThingHandler {
                 if (myObject.has("voltagePhase3")) {
                     updateChannel(LIVE_VOLTAGE3, myObject.get("voltagePhase3").toString());
                 }
-                if (myObject.has("currentPhase1")) {
-                    updateChannel(LIVE_CURRENT1, myObject.get("currentPhase1").toString());
+                if (myObject.has("currentL1")) {
+                    updateChannel(LIVE_CURRENT1, myObject.get("currentL1").toString());
                 }
-                if (myObject.has("currentPhase2")) {
-                    updateChannel(LIVE_CURRENT2, myObject.get("currentPhase2").toString());
+                if (myObject.has("currentL2")) {
+                    updateChannel(LIVE_CURRENT2, myObject.get("currentL2").toString());
                 }
-                if (myObject.has("currentPhase3")) {
-                    updateChannel(LIVE_CURRENT3, myObject.get("currentPhase3").toString());
+                if (myObject.has("currentL3")) {
+                    updateChannel(LIVE_CURRENT3, myObject.get("currentL3").toString());
                 }
                 if (myObject.has("powerProduction")) {
                     updateChannel(LIVE_POWERPRODUCTION, myObject.get("powerProduction").toString());
@@ -508,7 +512,7 @@ public class TibberHandler extends BaseThingHandler {
             String query = "{\"id\":\"1\",\"type\":\"start\",\"payload\":{\"variables\":{},\"extensions\":{},\"operationName\":null,\"query\":\"subscription {\\n liveMeasurement(homeId:\\\""
                     + tibberConfig.getHomeid()
                     + "\\\") {\\n timestamp\\n power\\n lastMeterConsumption\\n accumulatedConsumption\\n accumulatedCost\\n currency\\n minPower\\n averagePower\\n maxPower\\n"
-                    + "voltagePhase1\\n voltagePhase2\\n voltagePhase3\\n currentPhase1\\n currentPhase2\\n currentPhase3\\n powerProduction\\n accumulatedProduction\\n minPowerProduction\\n maxPowerProduction\\n }\\n }\\n\"}}";
+                    + "voltagePhase1\\n voltagePhase2\\n voltagePhase3\\n currentL1\\n currentL2\\n currentL3\\n powerProduction\\n accumulatedProduction\\n minPowerProduction\\n maxPowerProduction\\n }\\n }\\n\"}}";
             try {
                 TibberWebSocketListener socket = TibberHandler.this.socket;
                 if (socket != null) {
index ec977721c6b2cf670ba6e81ca229301a3b94baae..dd487eb82ed9cfe8778ce26e04339afaa66e4edc 100644 (file)
@@ -33,7 +33,7 @@ public class TibberPriceConsumptionHandler {
 
     public InputStream getInputStream(String homeId) {
         String Query = "{\"query\": \"{viewer {home (id: \\\"" + homeId
-                + "\\\") {currentSubscription {priceInfo {current {total startsAt }}} daily: consumption(resolution: DAILY, last: 1) {nodes {from to cost unitPrice consumption consumptionUnit}} hourly: consumption(resolution: HOURLY, last: 1) {nodes {from to cost unitPrice consumption consumptionUnit}}}}}\"}";
+                + "\\\") {currentSubscription {priceInfo {current {total startsAt level }}} daily: consumption(resolution: DAILY, last: 1) {nodes {from to cost unitPrice consumption consumptionUnit}} hourly: consumption(resolution: HOURLY, last: 1) {nodes {from to cost unitPrice consumption consumptionUnit}}}}}\"}";
         return new ByteArrayInputStream(Query.getBytes(StandardCharsets.UTF_8));
     }
 
index 0a7bd7ebca0e56a41fcd5a9cd2474ea77cf09ae3..ef320c4d10d9fa4ec32337192e27c957301b29bc 100644 (file)
@@ -10,6 +10,7 @@
                <channels>
                        <channel id="current_total" typeId="price"/>
                        <channel id="current_startsAt" typeId="timestamp"/>
+                       <channel id="current_level" typeId="level"/>
                        <channel id="daily_from" typeId="timestamp"/>
                        <channel id="daily_to" typeId="timestamp"/>
                        <channel id="daily_cost" typeId="cost"/>
                <description>Total Price: Energy + Tax</description>
                <state pattern="%.3f"></state>
        </channel-type>
+       <channel-type id="level">
+               <item-type>String</item-type>
+               <label>Price Level</label>
+               <description>Current price level</description>
+       </channel-type>
        <channel-type id="cost">
                <item-type>Number:Dimensionless</item-type>
                <label>Total Cost</label>