@@ -123,9 +123,9 @@ def send_discord_notification(message):
123123 except requests .RequestException as e :
124124 logging .error (f"Error sending Discord notification: { e } " )
125125 print (f"Error sending Discord notification: { e } " )
126-
126+
127127def get_last_state ():
128- """Reads the last state and charging start time from the state file."""
128+ """Reads the last state, charging start time, power, and notified flag from the state file."""
129129 try :
130130 with open (STATE_FILE , "r" ) as f :
131131 data = f .read ().strip ()
@@ -134,23 +134,24 @@ def get_last_state():
134134 try :
135135 timestamp = float (parts [1 ]) if parts [1 ] != "None" else 0.0
136136 power = float (parts [2 ]) if parts [2 ] != "None" else 0.0
137- return "charging" , timestamp , power
137+ notified = parts [3 ] == "1" if len (parts ) > 3 else False
138+ return "charging" , timestamp , power , notified
138139 except ValueError as e :
139140 logging .error (f"Value Error: { e } " )
140141 print (f"Value Error: { e } " )
141- return "idle" , None , None # Reset to idle if parsing fails
142- return data , None , None # "idle"
142+ return "idle" , None , None , False
143+ return data , None , None , False
143144 except FileNotFoundError as e :
144145 with open (STATE_FILE , "w" ) as f :
145146 f .write ("idle" )
146- return "idle" , None , None
147-
148- def save_last_state (state , charging_power = 0.0 ):
149- """Saves the current state and timestamp if charging starts ."""
147+ return "idle" , None , None , False
148+
149+ def save_last_state (state , charging_power = 0.0 , notified = False ):
150+ """Saves the current state, timestamp, power, and notified flag ."""
150151 with open (STATE_FILE , "w" ) as f :
151152 if state == "charging" :
152153 charging_power = charging_power if charging_power is not None else 0.0 # Ensure valid number
153- f .write (f"charging:{ time .time ()} :{ charging_power :.2f} " ) # Store timestamp + power
154+ f .write (f"charging:{ time .time ()} :{ charging_power :.2f} : { int ( notified ) } " ) # Store timestamp + power + notified
154155 else :
155156 f .write ("idle" ) # Reset if charging stops
156157
@@ -166,7 +167,7 @@ def main():
166167
167168 if charging_rate is not None :
168169 new_state = "charging" if charging_rate >= 1.0 else "idle"
169- last_state , start_time , stored_power = get_last_state ()
170+ last_state , start_time , stored_power , notified = get_last_state ()
170171
171172 timestamp = german_timestamp ()
172173
@@ -179,7 +180,7 @@ def main():
179180 print (f"📢 Sending Discord Notification: { message } " )
180181 logging .info (f"📢 Sending Discord Notification: { message } " )
181182 send_discord_notification (message )
182- save_last_state (new_state , charging_rate ) # Store start time & power
183+ save_last_state (new_state , charging_rate , notified = False ) # Store start time & power
183184 else :
184185 message = f"🔋 { timestamp } : charging stopped."
185186 print (f"📢 Sending Discord Notification: { message } " )
@@ -188,19 +189,19 @@ def main():
188189 save_last_state (new_state ) # Reset state
189190
190191 # If charging, check if 5 minutes have passed
191- elif new_state == "charging" and start_time is not None :
192+ elif new_state == "charging" and start_time is not None and not notified :
192193 elapsed_time = time .time () - start_time
193194 print (f"⏳ Elapsed Charging Time: { elapsed_time :.2f} seconds" )
194195 logging .info (f"⏳ Elapsed Charging Time: { elapsed_time :.2f} seconds" )
195196
196- if elapsed_time >= 300 and elapsed_time < 359 : # 300 seconds = 5 minutes
197+ if elapsed_time >= 300 : # 300 seconds = 5 minutes
197198 latest_charging_rate , _ = fetch_charging_status (driver ) # Fetch latest power
198199 message = f"⏳ charging power: { latest_charging_rate :.2f} kW"
199200 print (f"📢 Sending Discord Notification: { message } " )
200201 logging .info (f"📢 Sending Discord Notification: { message } " )
201202 send_discord_notification (message )
202- save_last_state ("charging" )
203-
203+ save_last_state ("charging" , latest_charging_rate , notified = True )
204+
204205 # If charging stopped, send a separate consumption message
205206 if last_state == "charging" and new_state == "idle" and consumed_energy_wh is not None :
206207 formatted_energy = format_energy (consumed_energy_wh )
0 commit comments