Skip to content

Commit 8d3976c

Browse files
committed
Removed the INVERTER_BATTERY_CAPACITY environment variable and updated the inverter code to fetch battery capacity directly from the SEMSPortal API.
1 parent a0b4397 commit 8d3976c

File tree

4 files changed

+29
-6
lines changed

4 files changed

+29
-6
lines changed

.env.example

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ POWER_USAGE_FACTOR=
1010
TIBBER_API_TOKEN=
1111

1212
INVERTER_HOSTNAME=
13-
INVERTER_BATTERY_CAPACITY=
1413
INVERTER_TARGET_MIN_STATE_OF_CHARGE=
1514

1615
SEMSPORTAL_USERNAME=

README.md

-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@ When the current time is a price minimum, the program wakes up and does the foll
131131
| `POWER_USAGE_FACTOR` | The amount of power used during the day vs during the night as a percentage value. E.g. if the value is set to `0.6` the program assumes you use 60 % of the daily power usage between 6 AM and 6 PM and 40 % between 6 PM and 6 AM. | `0.6` | A decimal number between `0` and `1`, typically between `0.5` and `0.8` |
132132
| `TIBBER_API_TOKEN` | The token to crawl the Tibber API. See https://developer.tibber.com/docs/guides/calling-api for more information. | - | A string, example: `my-secret-token` |
133133
| `INVERTER_HOSTNAME` | The hostname or IP of the inverter. | - | [`inverter.mydomain.com`, `192.168.5.10`, ...] |
134-
| `INVERTER_BATTERY_CAPACITY` | The capacity of the battery in watt hours without any separators. | - | A number, typically between `3000` and `15000` |
135134
| `INVERTER_TARGET_MIN_STATE_OF_CHARGE` | The state of charge the battery shall have when reaching the next minimum as a buffer. | `20` | A number between `0` and `100`, typically between `0` and `40` |
136135
| `SEMSPORTAL_USERNAME` | The username to login into the SEMSPortal. | - | A string, example: `[email protected]` |
137136
| `SEMSPORTAL_PASSWORD` | The password to login into the SEMSPortal. | - | A string, example: `my-secret-password` |

source/inverter.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from environment_variable_getter import EnvironmentVariableGetter
66
from goodwe.et import OperationMode
77
from logger import LoggerMixin
8+
from sems_portal_api_handler import SemsPortalApiHandler
89

910

1011
class Inverter(LoggerMixin):
@@ -14,7 +15,8 @@ def __init__(self):
1415
self.device = None
1516

1617
self.hostname = EnvironmentVariableGetter.get("INVERTER_HOSTNAME")
17-
self.battery_capacity = EnergyAmount(float(EnvironmentVariableGetter.get("INVERTER_BATTERY_CAPACITY")))
18+
sems_portal_api_handler = SemsPortalApiHandler()
19+
self.battery_capacity = sems_portal_api_handler.get_battery_capacity()
1820

1921
def connect(self) -> None:
2022
"""

source/sems_portal_api_handler.py

+26-3
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,30 @@ def _get_value_of_line_by_line_index_and_time_key(lines: dict, line_index: int,
332332
"""
333333
return int([line for line in lines[line_index]["xy"] if line["x"] == time_key][0]["y"])
334334

335+
def get_battery_capacity(self) -> EnergyAmount:
336+
"""
337+
Retrieves the battery capacity from the SEMSPORTAL API.
338+
339+
Returns:
340+
EnergyAmount: The battery capacity retrieved from the SEMSPORTAL API.
341+
"""
342+
self.login()
343+
344+
self.log.debug("Crawling the SEMSPORTAL API for the capacity of the battery...")
345+
346+
url = "https://eu.semsportal.com/api/v3/PowerStation/GetPlantDetailByPowerstationId"
347+
headers = {
348+
"Content-Type": "application/json",
349+
"Token": f'{{"version":"v2.1.0","client":"ios","language":"en", "timestamp": "{self.timestamp}", "uid": "{self.user_id}", "token": "{self.token}"}}',
350+
}
351+
payload = {
352+
"powerStationId": EnvironmentVariableGetter.get("SEMSPORTAL_POWERSTATION_ID"),
353+
}
354+
355+
response = requests.post(url, headers=headers, json=payload, timeout=20)
356+
response.raise_for_status()
357+
response = response.json()
358+
359+
self.log.trace(f"Retrieved data: {response}")
335360

336-
if __name__ == "__main__":
337-
sems_portal_api_handler = SemsPortalApiHandler()
338-
sems_portal_api_handler.write_values_to_database()
361+
return EnergyAmount.from_kilo_watt_hours(response["data"]["info"]["battery_capacity"])

0 commit comments

Comments
 (0)