11#!/usr/bin/env python3
2- from typing import Dict , TypedDict , Any
2+ import logging
3+ from typing import Dict , TypedDict , Any , Optional
34
45from modules .devices .batterx .batterx .config import BatterXBatSetup
56from modules .common .abstract_device import AbstractBat
89from modules .common .fault_state import ComponentInfo , FaultState
910from modules .common .simcount import SimCounter
1011from modules .common .store import get_bat_value_store
12+ from modules .common import req
13+
14+ log = logging .getLogger (__name__ )
1115
1216
1317class KwargsDict (TypedDict ):
1418 device_id : int
19+ ip_address : str
1520
1621
1722class BatterXBat (AbstractBat ):
@@ -21,9 +26,11 @@ def __init__(self, component_config: BatterXBatSetup, **kwargs: Any) -> None:
2126
2227 def initialize (self ) -> None :
2328 self .__device_id : int = self .kwargs ['device_id' ]
29+ self .__ip_address : str = self .kwargs ['ip_address' ]
2430 self .sim_counter = SimCounter (self .__device_id , self .component_config .id , prefix = "speicher" )
2531 self .store = get_bat_value_store (self .component_config .id )
2632 self .fault_state = FaultState (ComponentInfo .from_component_config (self .component_config ))
33+ self .last_mode = 'Undefined'
2734
2835 def update (self , resp : Dict ) -> None :
2936 power = resp ["1121" ]["1" ]
@@ -37,5 +44,57 @@ def update(self, resp: Dict) -> None:
3744 )
3845 self .store .set (bat_state )
3946
47+ def set_power_limit (self , power_limit : Optional [int ]) -> None :
48+ log .debug (f'last_mode: { self .last_mode } ' )
49+
50+ if power_limit is None :
51+ # Kein Powerlimit gefordert, externe Steuerung deaktivieren
52+ log .debug ("Keine Batteriesteuerung gefordert, deaktiviere externe Steuerung." )
53+ if self .last_mode is not None :
54+ # Battery Charge AC - OFF
55+ req .get_http_session ().get (
56+ f"http://{ self .__ip_address } /api.php?set=command&type=20738&text1=3&text2=0" ,
57+ timeout = 5
58+ )
59+ # Battery Discharging - ON
60+ req .get_http_session ().get (
61+ f"http://{ self .__ip_address } /api.php?set=command&type=20738&text1=4&text2=1" ,
62+ timeout = 5
63+ )
64+ self .last_mode = None
65+ elif power_limit <= 0 :
66+ # BatterX kann Entladung nur komplett sperren
67+ log .debug ("Aktive Batteriesteuerung angestoßen. Setze Entladesperre." )
68+ if self .last_mode != 'stop' :
69+ # Battery Charge AC - OFF
70+ req .get_http_session ().get (
71+ f"http://{ self .__ip_address } /api.php?set=command&type=20738&text1=3&text2=0" ,
72+ timeout = 5
73+ )
74+ # Battery Discharging - OFF
75+ req .get_http_session ().get (
76+ f"http://{ self .__ip_address } /api.php?set=command&type=20738&text1=4&text2=0" ,
77+ timeout = 5
78+ )
79+ self .last_mode = 'stop'
80+ else :
81+ # Aktive Ladung
82+ log .debug ("Aktive Batteriesteuerung angestoßen. Setze aktive Ladung." )
83+ if self .last_mode != 'charge' :
84+ # Battery Charge AC - ON
85+ req .get_http_session ().get (
86+ f"http://{ self .__ip_address } /api.php?set=command&type=20738&text1=3&text2=1" ,
87+ timeout = 5
88+ )
89+ # Battery Discharging - OFF
90+ req .get_http_session ().get (
91+ f"http://{ self .__ip_address } /api.php?set=command&type=20738&text1=4&text2=0" ,
92+ timeout = 5
93+ )
94+ self .last_mode = 'charge'
95+
96+ def power_limit_controllable (self ) -> bool :
97+ return True
98+
4099
41100component_descriptor = ComponentDescriptor (configuration_factory = BatterXBatSetup )
0 commit comments