Skip to content

Commit

Permalink
Fix Broken core API when using HA URL (Docker) (#1371)
Browse files Browse the repository at this point in the history
* Broken core API when using HA URL

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

* Update apps-yaml.md

* Update apps-yaml.md

* Update apps-yaml.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 Aug 16, 2024
1 parent a720c9a commit a6c4ab2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 18 deletions.
17 changes: 8 additions & 9 deletions apps/predbat/ha.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def __init__(self, base):
"""
Initialize the interface to Home Assistant.
"""
self.ha_url = base.args.get("ha_url", "http://supervisor")
self.ha_url = base.args.get("ha_url", "http://supervisor/core")
self.ha_key = base.args.get("ha_key", os.environ.get("SUPERVISOR_TOKEN", None))
self.websocket_active = False

Expand All @@ -43,7 +43,7 @@ def __init__(self, base):
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:
check = self.api_call("/core/api/")
check = self.api_call("/api/")
if not check:
self.log("Warn: Unable to connect directly to Home Assistant at {}, please check your configuration of ha_url/ha_key".format(self.ha_url))
self.ha_key = None
Expand All @@ -53,7 +53,6 @@ def __init__(self, base):
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))

Expand All @@ -76,7 +75,7 @@ async def socketLoop(self):
self.log("Info: Web socket stopping")
break

url = "{}/core/api/websocket".format(self.ha_url)
url = "{}/api/websocket".format(self.ha_url)
self.log("Info: Start socket for url {}".format(url))
async with ClientSession() as session:
try:
Expand Down Expand Up @@ -192,7 +191,7 @@ def update_state(self, entity_id):
"""
if not self.ha_key:
return
item = self.api_call("/core/api/states/{}".format(entity_id))
item = self.api_call("/api/states/{}".format(entity_id))
if item:
self.update_state_item(item)

Expand All @@ -212,7 +211,7 @@ def update_states(self):
"""
if not self.ha_key:
return
res = self.api_call("/core/api/states")
res = self.api_call("/api/states")
if res:
self.state_data = {}
for item in res:
Expand All @@ -232,7 +231,7 @@ def get_history(self, sensor, now, days=30):

start = now - timedelta(days=days)
end = now
res = self.api_call("/core/api/history/period/{}".format(start.strftime(TIME_FORMAT_HA)), {"filter_entity_id": sensor, "end_time": end.strftime(TIME_FORMAT_HA)})
res = self.api_call("/api/history/period/{}".format(start.strftime(TIME_FORMAT_HA)), {"filter_entity_id": sensor, "end_time": end.strftime(TIME_FORMAT_HA)})
return res

def set_state(self, entity_id, state, attributes={}):
Expand All @@ -248,7 +247,7 @@ def set_state(self, entity_id, state, attributes={}):
data = {"state": state}
if attributes:
data["attributes"] = attributes
self.api_call("/core/api/states/{}".format(entity_id), data, post=True)
self.api_call("/api/states/{}".format(entity_id), data, post=True)
self.update_state(entity_id)

def call_service(self, service, **kwargs):
Expand All @@ -261,7 +260,7 @@ def call_service(self, service, **kwargs):
data = {}
for key in kwargs:
data[key] = kwargs[key]
self.api_call("/core/api/services/{}".format(service), data, post=True)
self.api_call("/api/services/{}".format(service), data, post=True)

def api_call(self, endpoint, data_in=None, post=False):
"""
Expand Down
23 changes: 14 additions & 9 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.4"
THIS_VERSION = "v8.3.5"
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 @@ -756,7 +756,7 @@ def cache_get_url(self, url, params, max_age=8 * 60):
try:
data = r.json()
except requests.exceptions.JSONDecodeError as e:
self.log("Warn: Error downloading data from URL {}, error {} code {}".format(url, e, r.status_code))
self.log("Warn: Error downloading data from URL {}, error {} code {} data was {}".format(url, e, r.status_code, r.text))
if data:
self.log("Warn: Error downloading data from URL {}, using cached data age {} minutes".format(url, self.dp1(age_minutes)))
else:
Expand Down Expand Up @@ -822,13 +822,18 @@ def download_solcast_data(self):
# self.solcast_api_used += data.get("daily_limit_consumed", None)
# self.log("Solcast API limit {} used {}".format(self.solcast_api_limit, self.solcast_api_used))

url = f"{host}/rooftop_sites"
data = self.cache_get_url(url, params, max_age=max_age)
if not data:
self.log("Warn: Solcast sites could not be downloaded, check your Solcast cloud settings")
continue

sites = data.get("sites", [])
site_config = self.get_arg("solcast_sites", [])
if site_config:
sites = []
for site in site_config:
sites.append({"resource_id": site})
else:
url = f"{host}/rooftop_sites"
data = self.cache_get_url(url, params, max_age=max_age)
if not data:
self.log("Warn: Solcast sites could not be downloaded, try setting solcast_sites in apps.yaml instead")
continue
sites = data.get("sites", [])

for site in sites:
resource_id = site.get("resource_id", None)
Expand Down
8 changes: 8 additions & 0 deletions docs/apps-yaml.md
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,14 @@ solcast_api_key: 'xxxx'
solcast_poll_hours: 8
```

Note that by default the solcast API will be used to download all sites (up to 2 for hobby accounts), if you want to override this set your sites manually using
**solcast_sites** as an array of site IDs:

```yaml
solcast_sites:
- 'xxxx'
```

If you have more than 2 array orientations and thus more than one Solcast API key, enter each key in a list, i.e.:

```yaml
Expand Down

0 comments on commit a6c4ab2

Please sign in to comment.