2222from interfaces .evcc_interface import EvccInterface
2323from interfaces .eos_interface import EosInterface
2424from interfaces .price_interface import PriceInterface
25+ from interfaces .mqtt_interface import MqttInterface
2526
2627EOS_TGT_DURATION = 48
2728EOS_API_GET_PV_FORECAST = "https://api.akkudoktor.net/forecast"
@@ -119,6 +120,10 @@ def charging_state_callback(new_state):
119120 change_control_state ()
120121
121122
123+ mqtt_interface = MqttInterface (
124+ config_manager .config ["mqtt" ]
125+ )
126+
122127evcc_interface = EvccInterface (
123128 url = config_manager .config ["evcc" ]["url" ],
124129 update_interval = 10 ,
@@ -140,7 +145,7 @@ def charging_state_callback(new_state):
140145 config_manager .config .get ("battery" , {}).get ("url" , "" ),
141146 config_manager .config .get ("battery" , {}).get ("soc_sensor" , "" ),
142147 config_manager .config .get ("battery" , {}).get ("access_token" , "" ),
143- config_manager .config .get ("battery" , {})
148+ config_manager .config .get ("battery" , {}),
144149)
145150
146151price_interface = PriceInterface (
@@ -300,9 +305,7 @@ def get_pv_akku_data():
300305 "max_charge_power_w" : config_manager .config ["battery" ][
301306 "max_charge_power_w"
302307 ],
303- "initial_soc_percentage" : round (
304- battery_interface .get_current_soc ()
305- ),
308+ "initial_soc_percentage" : round (battery_interface .get_current_soc ()),
306309 "min_soc_percentage" : config_manager .config ["battery" ][
307310 "min_soc_percentage"
308311 ],
@@ -525,6 +528,13 @@ def run_optimization(self):
525528 ) as file :
526529 json .dump (json_optimize_input , file , indent = 4 )
527530
531+ mqtt_interface .update_publish_topics (
532+ {
533+ "optimization/state" : {
534+ "value" : self .get_current_state ()["request_state" ]
535+ }
536+ }
537+ )
528538 optimized_response = eos_interface .eos_set_optimize_request (
529539 json_optimize_input , config_manager .config ["eos" ]["timeout" ]
530540 )
@@ -560,6 +570,14 @@ def run_optimization(self):
560570 sleeptime = (next_eval - loop_now ).total_seconds ()
561571 minutes , seconds = divmod (sleeptime , 60 )
562572 self .__set_state_next_run (next_eval .astimezone (time_zone ).isoformat ())
573+ mqtt_interface .update_publish_topics (
574+ {
575+ "optimization/last_run" : {
576+ "value" : self .get_current_state ()["last_response_timestamp" ]},
577+ "optimization/next_run" : {
578+ "value" : self .get_current_state ()["next_run" ]},
579+ }
580+ )
563581 logger .info (
564582 "[Main] Next optimization at %s. Sleeping for %d min %.0f seconds\n " ,
565583 next_eval .strftime ("%H:%M:%S" ),
@@ -585,6 +603,19 @@ def setting_control_data(ac_charge_demand_rel, dc_charge_demand_rel, discharge_a
585603 base_control .set_current_ac_charge_demand (ac_charge_demand_rel )
586604 base_control .set_current_dc_charge_demand (dc_charge_demand_rel )
587605 base_control .set_current_discharge_allowed (bool (discharge_allowed ))
606+ mqtt_interface .update_publish_topics (
607+ {
608+ "control/eos_ac_charge_demand" : {
609+ "value" : base_control .get_current_ac_charge_demand ()
610+ },
611+ "control/eos_dc_charge_demand" : {
612+ "value" : base_control .get_current_dc_charge_demand ()
613+ },
614+ "control/eos_discharge_allowed" : {
615+ "value" : base_control .get_current_discharge_allowed ()
616+ }
617+ }
618+ )
588619 # set the current battery state of charge
589620 base_control .set_current_battery_soc (battery_interface .get_current_soc ())
590621 # getting the current charging state from evcc
@@ -616,6 +647,23 @@ def change_control_state():
616647 current_overall_state = base_control .get_current_overall_state_number ()
617648 current_overall_state_text = base_control .get_current_overall_state ()
618649
650+ mqtt_interface .update_publish_topics (
651+ {
652+ "control/overall_state" : {
653+ "value" : base_control .get_current_overall_state_number ()
654+ },
655+ "optimization/state" : {
656+ "value" : optimization_scheduler .get_current_state ()["request_state" ]
657+ },
658+ "control/override_remain_time" : { "value" : "01:00" },
659+ "battery/soc" : {"value" : battery_interface .get_current_soc ()},
660+ "battery/remaining_energy" : {
661+ "value" : battery_interface .get_current_usable_capacity ()
662+ },
663+ "status" : {"value" : "online" },
664+ }
665+ )
666+
619667 # Check if the overall state of the inverter was changed recently
620668 if base_control .was_overall_state_changed_recently (180 ):
621669 logger .debug ("[Main] Overall state changed recently" )
@@ -761,6 +809,7 @@ def get_controls():
761809 "evcc" : {
762810 "charging_state" : base_control .get_current_evcc_charging_state (),
763811 "charging_mode" : base_control .get_current_evcc_charging_mode (),
812+ "current_session" : evcc_interface .get_current_detail_data (),
764813 },
765814 "battery" : {
766815 "soc" : current_battery_soc ,
@@ -799,6 +848,7 @@ def get_controls():
799848 and inverter_interface is not None
800849 ):
801850 inverter_interface .shutdown ()
851+ mqtt_interface .shutdown ()
802852 evcc_interface .shutdown ()
803853 battery_interface .shutdown ()
804854 logger .info ("[Main] Server stopped" )
0 commit comments