Skip to content

Commit 091a6d8

Browse files
committed
implement power control
1 parent 1b19490 commit 091a6d8

1 file changed

Lines changed: 46 additions & 2 deletions

File tree

  • packages/modules/devices/sonnen/sonnenbatterie

packages/modules/devices/sonnen/sonnenbatterie/bat.py

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ def __update_variant_0(self) -> BatState:
4747
soc=battery_soc
4848
)
4949

50-
def __read_variant_1(self, api: str = "v1"):
50+
def __read_variant_1(self, api: str = "v1", target: str = "status") -> Dict:
5151
return req.get_http_session().get(
52-
"http://" + self.__device_address + "/api/" + api + "/status",
52+
f"http://{self.__device_address}/api/{api}/{target}",
5353
timeout=5,
5454
headers={"Auth-Token": self.__api_v2_token} if api == "v2" else None
5555
).json()
@@ -105,6 +105,32 @@ def __update_variant_1(self, api: str = "v1") -> BatState:
105105
exported=exported
106106
)
107107

108+
def __get_json_api_v2_configurations(self) -> Dict:
109+
if self.__device_variant != 3:
110+
raise ValueError("JSON API v2 wird nur für Variante 3 unterstützt!")
111+
return self.__read_variant_1("v2", "configurations")
112+
113+
def __set_json_api_v2_configurations(self, configuration: Dict) -> None:
114+
if self.__device_variant != 3:
115+
raise ValueError("JSON API v2 wird nur für Variante 3 unterstützt!")
116+
req.get_http_session().put(
117+
f"http://{self.__device_address}/api/v2/configurations",
118+
json=configuration,
119+
headers={"Auth-Token": self.__api_v2_token}
120+
)
121+
122+
def __set_json_api_v2_setpoint(self, power_limit: int) -> None:
123+
if self.__device_variant != 3:
124+
raise ValueError("JSON API v2 wird nur für Variante 3 unterstützt!")
125+
command = "charge"
126+
if power_limit < 0:
127+
command = "discharge"
128+
power_limit = -power_limit
129+
req.get_http_session().post(
130+
f"http://{self.__device_address}/api/v2/setpoint/{command}/{power_limit}",
131+
headers={"Auth-Token": self.__api_v2_token, "Content-Type": "application/json"}
132+
)
133+
108134
def __read_variant_2_element(self, element: str) -> str:
109135
response = req.get_http_session().get(
110136
'http://' + self.__device_address + ':7979/rest/devices/battery/' + element,
@@ -137,5 +163,23 @@ def update(self) -> None:
137163
raise ValueError("Unbekannte Variante: " + str(self.__device_variant))
138164
self.store.set(state)
139165

166+
def set_power_limit(self, power_limit: Optional[int]) -> None:
167+
if self.__device_variant != 3:
168+
raise ValueError("Leistungsvorgabe wird nur für Variante 3 unterstützt!")
169+
operating_mode = self.__get_json_api_v2_configurations()["EM_OperatingMode"]
170+
log.debug(f"Betriebsmodus: aktuell: {operating_mode}")
171+
if power_limit is None:
172+
# Keine Leistungsvorgabe, Betriebsmodus "Eigenverbrauch" aktivieren
173+
if operating_mode == "1":
174+
log.debug("Keine Leistungsvorgabe, aktiviere normale Steuerung durch den Speicher")
175+
self.__set_json_api_v2_configurations({"EM_OperatingMode": "2"})
176+
else:
177+
# Leistungsvorgabe, Betriebsmodus "Manuell" aktivieren
178+
if operating_mode == "2":
179+
log.debug(f"Leistungsvorgabe: {power_limit}, aktiviere manuelle Steuerung durch openWB")
180+
self.__set_json_api_v2_configurations({"EM_OperatingMode": "1"})
181+
log.debug(f"Setze Leistungsvorgabe auf: {power_limit}")
182+
self.__set_json_api_v2_setpoint(power_limit)
183+
140184

141185
component_descriptor = ComponentDescriptor(configuration_factory=SonnenbatterieBatSetup)

0 commit comments

Comments
 (0)