Skip to content

Commit

Permalink
Fix ha_key crash if not using addon (#1369)
Browse files Browse the repository at this point in the history
* Fix ha_key crash if not using addon

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

---------

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 Aug 16, 2024
1 parent fa9e6ad commit 57fd6f0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
16 changes: 16 additions & 0 deletions apps/predbat/ha.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ def __init__(self, base):
self.base = base
self.log = base.log
self.state_data = {}
self.slug = None

if not self.ha_key:
self.log("Warn: ha_key or SUPERVISOR_TOKEN not found, you can set ha_url/ha_key in apps.yaml. Will use direct HA API")
else:
Expand All @@ -47,10 +49,24 @@ def __init__(self, base):
self.ha_key = None
else:
self.log("Info: Connected to Home Assistant at {}".format(self.ha_url))

res = self.api_call("/addons/self/info")
if res:
# get add-on slug name which is the actual directory name under /addon_configs that /config is mounted to
self.config_root_p = "/addon_configs/" + res["data"]["slug"]
self.slug = res["data"]["slug"]
self.log("Info: Add-on slug is {}".format(self.slug))

self.base.create_task(self.socketLoop())
self.websocket_active = True
self.log("Info: Web Socket task started")

def get_slug(self):
"""
Get the add-on slug.
"""
return self.slug

async def socketLoop(self):
"""
Web socket loop for HA interface
Expand Down
14 changes: 6 additions & 8 deletions apps/predbat/predbat.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import asyncio
import json

THIS_VERSION = "v8.3.3"
THIS_VERSION = "v8.3.4"
PREDBAT_FILES = ["predbat.py", "config.py", "prediction.py", "utils.py", "inverter.py", "ha.py", "download.py", "unit_test.py"]
from download import predbat_update_move, predbat_update_download, check_install

Expand Down Expand Up @@ -10855,15 +10855,13 @@ def initialize(self):
try:
self.reset()
self.ha_interface = HAInterface(self)
self.config_root_p = self.config_root

# Get add-on info by making API call to HA supervisor
res = self.ha_interface.api_call("/addons/self/info")

if res:
# get add-on slug name which is the actual directory name under /addon_configs that /config is mounted to
# Printable config root
self.config_root_p = self.config_root
slug = self.ha_interface.get_slug()
if slug:
# and use slug name to determine printable config_root pathname when writing debug info to the log file
self.config_root_p = "/addon_configs/" + res["data"]["slug"]
self.config_root_p = "/addon_configs/" + slug

self.log("Config root is {} and printable config_root_p is now {}".format(self.config_root, self.config_root_p))

Expand Down

0 comments on commit 57fd6f0

Please sign in to comment.