Skip to content

Commit

Permalink
Compare active entity (#2019)
Browse files Browse the repository at this point in the history
* Compare active entity

#1994

* [pre-commit.ci lite] apply automatic fixes

* Update compare.md

---------

Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
  • Loading branch information
springfall2008 and pre-commit-ci-lite[bot] authored Feb 15, 2025
1 parent 4c298ac commit 7aecdbf
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 15 deletions.
7 changes: 7 additions & 0 deletions apps/predbat/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@
"default": False,
"restore": False,
},
{
"name": "compare_active",
"friendly_name": "Predbat Compare Active",
"type": "switch",
"default": False,
"restore": False,
},
{
"name": "pv_metric10_weight",
"friendly_name": "Metric 10 Weight",
Expand Down
19 changes: 11 additions & 8 deletions apps/predbat/predbat.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,6 @@ def reset(self):
self.ge_cloud_direct = None
self.CONFIG_ITEMS = copy.deepcopy(CONFIG_ITEMS)
self.comparison = None
self.compare_tariffs = False
self.predheat = None
self.predbat_mode = "Monitor"
self.soc_kwh_history = {}
Expand Down Expand Up @@ -768,13 +767,17 @@ def update_pred(self, scheduled=True):
self.expose_config("active", False)
self.save_current_config()

if ((scheduled and self.minutes_now < RUN_EVERY) or self.compare_tariffs) and self.comparison:
# Compare tariffs either when triggered or daily at midnight
self.comparison.run_all()
self.compare_tariffs = False
if self.comparison:
if (scheduled and self.minutes_now < RUN_EVERY) or self.get_arg("compare_active", False):
# Compare tariffs either when triggered or daily at midnight
self.expose_config("compare_active", True)
self.comparison.run_all()
self.expose_config("compare_active", False)
else:
# Otherwise just update HA sensors to prevent then expiring
self.comparison.publish_only()
else:
# Otherwise just update HA sensors to prevent then expiring
self.comparison.publish_only()
self.expose_config("compare_active", False)

async def async_download_predbat_version(self, version):
"""
Expand Down Expand Up @@ -958,7 +961,7 @@ def update_time_loop(self, cb_args):
Called every 15 seconds
"""
self.check_entity_refresh()
if (self.update_pending or self.compare_tariffs) and not self.prediction_started:
if self.update_pending and not self.prediction_started:
self.prediction_started = True
self.ha_interface.update_states()
self.load_user_config()
Expand Down
12 changes: 9 additions & 3 deletions apps/predbat/unit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3076,6 +3076,7 @@ def run_test_web_if(my_predbat):
"""
failed = 0
print("**** Running web interface test ****\n")
ha = my_predbat.ha_interface
my_predbat.web_interface = WebInterface(my_predbat)
my_predbat.web_interface_task = my_predbat.create_task(my_predbat.web_interface.start())

Expand All @@ -3090,15 +3091,20 @@ def run_test_web_if(my_predbat):

# Perform a post to /compare page with data for form 'compareform' value 'run'
print("**** Running test: Fetch page /compare with post")

address = "http://127.0.0.1:5052/compare"
my_predbat.compare_tariffs = False
data = {"run": "run"}
res = requests.post(address, data=data)
if res.status_code != 200:
print("ERROR: Failed to post to pagepage {} got status {} value {}".format(address, res.status_code, res.text))
failed = 1
if not my_predbat.compare_tariffs:
print("ERROR: Compare tariffs not set")
time.sleep(0.1)
# Get service data
entity_id = "switch.predbat_compare_active"
result = ha.get_state(entity_id)

if result != "on":
print("ERROR: Compare tariffs not triggered - expected {} got {}".format("on", result))
failed = 1

my_predbat.web_interface.abort = True
Expand Down
10 changes: 7 additions & 3 deletions apps/predbat/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -962,11 +962,15 @@ async def html_compare_post(self, request):
"""
Handle post request for html compare
"""

postdata = await request.post()
for pitem in postdata:
if pitem == "run":
self.base.compare_tariffs = True
self.log("Starting compare from web page")
service_data = {}
service_data["domain"] = "switch"
service_data["service"] = "turn_on"
service_data["service_data"] = {"entity_id": "switch.{}_compare_active".format(self.base.prefix)}
await self.base.trigger_callback(service_data)

return await self.html_compare(request)

Expand Down Expand Up @@ -995,7 +999,7 @@ async def html_compare(self, request):

text += "<body>\n"
text += '<form class="form-inline" action="./compare" method="post" enctype="multipart/form-data" id="compareform">\n'
active = self.base.compare_tariffs
active = self.base.get_arg("compare_active", False)

if not active:
text += '<button type="submit" form="compareform" value="run">Compare now</button>\n'
Expand Down
4 changes: 3 additions & 1 deletion docs/compare.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ If you do not set an import or export rate for a particular tariff then your exi
By default, the comparison will be run at Midnight every night and saved for the entire day.
You can view the comparison on the Predbat web interface under the 'Compare' tab and also manually trigger a new comparison by hitting the 'Run' button.
You can view the comparison on the Predbat web interface under the 'Compare' tab and also manually trigger a new comparison by hitting the 'Run' button or by turning on **switch.predbat_compare_active**.
When a compare is running **switch.predbat_compare_active** will be turned off, otherwise it will be off.
Predbat will highlight which tariff may be the best cost-wise for the next 24-hour period based on the plan optimisation metric you have defined. The metric
includes the value of the contents of your battery and iBoost that has been diverted during this period.
Expand Down

0 comments on commit 7aecdbf

Please sign in to comment.