From b2f29ec26370e6351f15150b4f055041c1aca1b9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Odd=20Str=C3=A5b=C3=B8?= <oddstr13@openshell.no>
Date: Sat, 20 Nov 2021 09:12:21 +0100
Subject: [PATCH] Add calculated power values for Battery, Panel and Load.
 Should give better resolution than the integer panel and load numbers.

---
 consumers/mqtt.py | 15 +++++++++++++++
 solar_ble.py      | 11 ++++++++++-
 solar_types.py    |  4 ++++
 3 files changed, 29 insertions(+), 1 deletion(-)

diff --git a/consumers/mqtt.py b/consumers/mqtt.py
index 70e7dc2..57b6d8f 100644
--- a/consumers/mqtt.py
+++ b/consumers/mqtt.py
@@ -94,6 +94,21 @@ MAP_VALUES: Dict[DataName, Dict[str, Any]] = {
     },
     DataName.PANEL_POWER: {"unit": "W", "type": "power", "state_class": "measurement"},
     # DataName.LOAD_ENABLED: {},
+    DataName.CALCULATED_BATTERY_POWER: {
+        "unit": "W",
+        "type": "power",
+        "state_class": "measurement",
+    },
+    DataName.CALCULATED_PANEL_POWER: {
+        "unit": "W",
+        "type": "power",
+        "state_class": "measurement",
+    },
+    DataName.CALCULATED_LOAD_POWER: {
+        "unit": "W",
+        "type": "power",
+        "state_class": "measurement",
+    },
 }
 
 
diff --git a/solar_ble.py b/solar_ble.py
index 3f4ccf4..a0cf72b 100755
--- a/solar_ble.py
+++ b/solar_ble.py
@@ -12,7 +12,7 @@ from bluepy import btle
 from libscrc import modbus
 
 from feasycom_ble import BTLEUart
-from solar_types import DATA_BATTERY_STATE, HISTORICAL_DATA, DataItem
+from solar_types import DATA_BATTERY_STATE, HISTORICAL_DATA, DataItem, DataName
 from test_config import get_config, get_consumers
 
 MAC = "DC:0D:30:9C:61:BA"
@@ -403,6 +403,15 @@ if __name__ == "__main__":
                         if per_voltages(now):
                             data = try_read_parse(dev, 0x0100, 11, parse_battery_state)
                             if data:
+                                data[DataName.CALCULATED_BATTERY_POWER] = data.get(
+                                    DataName.BATTERY_VOLTAGE, 0
+                                ) * data.get(DataName.BATTERY_CURRENT)
+                                data[DataName.CALCULATED_PANEL_POWER] = data.get(
+                                    DataName.PANEL_VOLTAGE, 0
+                                ) * data.get(DataName.PANEL_CURRENT)
+                                data[DataName.CALCULATED_LOAD_POWER] = data.get(
+                                    DataName.LOAD_VOLTAGE, 0
+                                ) * data.get(DataName.LOAD_CURRENT)
                                 log(data)
                                 for consumer in consumers:
                                     consumer.write(data)
diff --git a/solar_types.py b/solar_types.py
index 2b36573..8fdcb83 100644
--- a/solar_types.py
+++ b/solar_types.py
@@ -36,6 +36,10 @@ class DataName(str, Enum):
     TOTAL_PRODUCTION_ENERGY = "total_production_energy"
     TOTAL_CONSUMPTION_ENERGY = "total_consumption_energy"
 
+    CALCULATED_BATTERY_POWER = "calculated_battery_power"
+    CALCULATED_PANEL_POWER = "calculated_panel_power"
+    CALCULATED_LOAD_POWER = "calculated_load_power"
+
     def __repr__(self):
         return repr(self.value)