11#!/usr/bin/env python3
2- from typing import Dict , Union
2+ import logging
3+ from typing import Dict , Union , Optional
34
45from dataclass_utils import dataclass_from_dict
56from modules .common import modbus
1415from modules .devices .sungrow .sungrow .version import Version
1516from modules .devices .sungrow .sungrow .firmware import Firmware
1617
18+ log = logging .getLogger (__name__ )
19+
1720
1821class SungrowBat (AbstractBat ):
1922 def __init__ (self ,
@@ -26,6 +29,7 @@ def __init__(self,
2629 self .sim_counter = SimCounter (self .device_config .id , self .component_config .id , prefix = "speicher" )
2730 self .store = get_bat_value_store (self .component_config .id )
2831 self .fault_state = FaultState (ComponentInfo .from_component_config (self .component_config ))
32+ self .last_mode = 'Undefined'
2933
3034 def update (self ) -> None :
3135 unit = self .device_config .configuration .modbus_id
@@ -64,5 +68,31 @@ def update(self) -> None:
6468 )
6569 self .store .set (bat_state )
6670
71+ def set_power_limit (self , power_limit : Optional [int ]) -> None :
72+ unit = self .device_config .configuration .modbus_id
73+ log .debug (f'last_mode: { self .last_mode } ' )
74+
75+ if power_limit is None :
76+ log .debug ("Keine Batteriesteuerung, Selbstregelung durch Wechselrichter" )
77+ if self .last_mode is not None :
78+ self .__tcp_client .write_registers (13049 , [0 ], data_type = ModbusDataType .UINT_16 , unit = unit )
79+ self .__tcp_client .write_registers (13050 , [0xCC ], data_type = ModbusDataType .UINT_16 , unit = unit )
80+ self .last_mode = None
81+ elif power_limit == 0 :
82+ log .debug ("Aktive Batteriesteuerung. Batterie wird auf Stop gesetzt und nicht entladen" )
83+ if self .last_mode != 'stop' :
84+ self .__tcp_client .write_registers (13049 , [2 ], data_type = ModbusDataType .UINT_16 , unit = unit )
85+ self .__tcp_client .write_registers (13050 , [0xCC ], data_type = ModbusDataType .UINT_16 , unit = unit )
86+ self .last_mode = 'stop'
87+ elif power_limit > 0 :
88+ log .debug (f"Aktive Batteriesteuerung. Batterie wird mit { power_limit } W entladen für den Hausverbrauch" )
89+ if self .last_mode != 'discharge' :
90+ self .__tcp_client .write_registers (13049 , [2 ], data_type = ModbusDataType .UINT_16 , unit = unit )
91+ self .__tcp_client .write_registers (13050 , [0xBB ], data_type = ModbusDataType .UINT_16 , unit = unit )
92+ self .last_mode = 'discharge'
93+ # Die maximale Entladeleistung begrenzen auf 5000W, maximaler Wertebereich Modbusregister.
94+ power_value = int (min (power_limit , 5000 ))
95+ self .__tcp_client .write_registers (13051 , [power_value ], data_type = ModbusDataType .UINT_16 , unit = unit )
96+
6797
6898component_descriptor = ComponentDescriptor (configuration_factory = SungrowBatSetup )
0 commit comments