Skip to content

Commit

Permalink
Added Sonnen battery API as an additional data source
Browse files Browse the repository at this point in the history
  • Loading branch information
wene37 committed Apr 30, 2023
1 parent ed5fc72 commit 461b048
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/SolarManager/SolarManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import json

from SolarEdge import SolarEdge
from Sonnen import Sonnen
from SolarManager.Elements.enums import ChargingState

from weconnect import weconnect, addressable
Expand Down Expand Up @@ -37,8 +38,19 @@ def __init__(
self.chargingChangeRequested = False

self.logger.info(f"Simulation mode: {self.simulationMode}")

self.solarEdge = SolarEdge.SolarEdge(configFileName)
dataSource = configParser.get("SolarManager", "DataSource")

match dataSource:
case "Sonnen":
self.logger.info("Using 'Sonnen' as data source.")
self.dataSource = Sonnen.Sonnen(configFileName)
case "SolarEdge":
self.logger.info("Using 'SolarEdge' as data source.")
self.dataSource = SolarEdge.SolarEdge(configFileName)
case _:
self.logger.error("The data source '{{DATA_SOURCE}}' does not exist. Set correct value in property 'DataSource' in the config file and restart the service.".replace("{{DATA_SOURCE}}", dataSource))
self.dataSource = None
return

self.logger.info("Initialize WeConnect")
self.weConnect = weconnect.WeConnect(username=username, password=password, updateAfterLogin=False, loginOnInit=False)
Expand Down Expand Up @@ -76,6 +88,10 @@ def onWeConnectEvent(self, element, flags):
def run(self) -> None:
self.logger.info("Run")

if self.dataSource == None:
self.logger.warn("The data source is not initialized.")
return

currentVehicleState = self.updateVehicle()
nickname = currentVehicleState.nickname.value

Expand All @@ -87,7 +103,7 @@ def run(self) -> None:
self.logger.info("Vehicle is not connected to or not locked at the plug.")
return

currentSolarState = self.solarEdge.get_current_state()
currentSolarState = self.dataSource.get_current_state()
self.logger.info(f"Current solar state: {json.dumps(currentSolarState)}")

if self.isCharging:
Expand Down
40 changes: 40 additions & 0 deletions src/Sonnen/Sonnen.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import logging
import requests
import configparser
import json

class Sonnen:
def __init__(
self,
configFileName: str
) -> None:

self.logger = logging.getLogger("Sonnen")

configParser = configparser.ConfigParser()
configParser.read(configFileName)

apiUrl = configParser.get("Sonnen", "ApiUrl")
ip = configParser.get("Sonnen", "IP")
port = configParser.get("Sonnen", "Port")

self.apiUrl = apiUrl.replace("{{IP}}", ip).replace("{{PORT}}", port)

def __del__(self) -> None:
self.logger.info("Del")

def get_current_state(self) -> None:
self.logger.info("Get current state")

response = requests.get(self.apiUrl)
jsonString = response.text

self.logger.debug(f"JSON: {jsonString}")

apiData = json.loads(jsonString)

result = dict()
result["loadToGridPower"] = apiData["GridFeedIn_W"] / 1000
result["batteryChargeLevel"] = apiData["USOC"]

return result
Empty file added src/Sonnen/__init__.py
Empty file.
6 changes: 6 additions & 0 deletions src/config.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@ MinBatteryLoad=90
SimulationMode=False
VIN=
VehicleNameSuffix=(SMC)
DataSource=SolarEdge

[SolarEdge]
ApiUrl=https://monitoringapi.solaredge.com/site/{{LOCATION_ID}}/currentPowerFlow.json?api_key={{API_KEY}}
LocationId=
ApiKey=

[Sonnen]
ApiUrl=http://{{IP}}:{{PORT}}/api/v1/status
IP=
Port=8080

[WeConnect]
Username=
Password=

0 comments on commit 461b048

Please sign in to comment.