Skip to content

Commit d4a2a09

Browse files
committed
Fixes for Supervised HASS
1 parent 588008e commit d4a2a09

File tree

2 files changed

+23
-20
lines changed

2 files changed

+23
-20
lines changed

.vscode/launch.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"request": "launch",
1111
"module": "appdaemon",
1212
"justMyCode": true,
13-
"args": "-c /home/hass/appdaemon/dev_test --toml"
13+
"args": "-c /home/hass/ad_config/dev_test --toml"
1414
},
1515
]
1616
}

appdaemon/plugins/hass/hassplugin.py

+22-19
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import asyncio
2+
import datetime
23
import json
4+
import os
35
import ssl
4-
import websocket
56
import traceback
7+
from copy import deepcopy
8+
from typing import Union
9+
from urllib.parse import quote, urlencode
10+
611
import aiohttp
712
import pytz
13+
import websocket
814
from deepdiff import DeepDiff
9-
from copy import deepcopy
10-
import datetime
11-
from urllib.parse import quote
12-
from urllib.parse import urlencode
13-
from typing import Union
1415

1516
import appdaemon.utils as utils
1617
from appdaemon.appdaemon import AppDaemon
@@ -55,10 +56,12 @@ def __init__(self, ad: AppDaemon, name, args):
5556
self.cert_path = args.get("cert_path")
5657
self.cert_verify = args.get("cert_verify")
5758
self.commtype = args.get("commtype", "WS")
58-
self.ha_key = args.get("ha_key")
5959

60-
# Remove trailing slash if present
61-
self.ha_url = args.get("ha_url", "").rstrip("/")
60+
# Fixes for supervised
61+
self.ha_key = args.get("ha_key", os.environ.get("SUPERVISOR_TOKEN"))
62+
self.ha_url = args.get("ha_url", "http://supervisor/core").rstrip("/")
63+
# End fixes for supervised
64+
6265
self.namespace = args.get("namespace", "default")
6366
self.plugin_startup_conditions = args.get("plugin_startup_conditions", {})
6467
self.retry_secs = int(args.get("retry_secs", 5))
@@ -148,7 +151,7 @@ def session(self):
148151
headers["x-ha-access"] = self.ha_key
149152

150153
self._session = aiohttp.ClientSession(
151-
base_url=self.ha_url,
154+
# base_url=self.ha_url,
152155
connector=conn,
153156
headers=headers,
154157
json_serialize=utils.convert_json,
@@ -490,7 +493,7 @@ async def set_plugin_state(self, namespace, entity_id, **kwargs):
490493
# if we get a request for not our namespace something has gone very wrong
491494
assert namespace == self.namespace
492495

493-
api_url = "/api/states/{}".format(entity_id)
496+
api_url = f"{self.ha_url}/api/states/{entity_id}"
494497

495498
try:
496499
r = await self.session.post(api_url, json=kwargs)
@@ -548,7 +551,7 @@ async def call_plugin_service(self, namespace, domain, service, data):
548551
return await self.get_history(**data)
549552

550553
else:
551-
api_url = "/api/services/{}/{}".format(domain, service)
554+
api_url = f"{self.ha_url}/api/services/{domain}/{service}"
552555

553556
try:
554557
r = await self.session.post(api_url, json=data)
@@ -668,7 +671,7 @@ def as_datetime(args, key):
668671

669672
# Build the url
670673
# /api/history/period/<start_time>?filter_entity_id=<entity_id>&end_time=<end_time>
671-
apiurl = "/api/history/period"
674+
apiurl = f"{self.ha_url}/api/history/period"
672675

673676
if start_time:
674677
apiurl += "/" + utils.dt_to_str(start_time.replace(microsecond=0), self.AD.tz)
@@ -684,9 +687,9 @@ def as_datetime(args, key):
684687

685688
async def get_hass_state(self, entity_id=None):
686689
if entity_id is None:
687-
api_url = "/api/states"
690+
api_url = f"{self.ha_url}/api/states"
688691
else:
689-
api_url = "/api/states/{}".format(entity_id)
692+
api_url = f"{self.ha_url}/api/states/{entity_id}"
690693
self.logger.debug("get_ha_state: url is %s", api_url)
691694
r = await self.session.get(api_url)
692695
if r.status == 200 or r.status == 201:
@@ -731,7 +734,7 @@ def validate_tz(self, meta):
731734
async def get_hass_config(self):
732735
try:
733736
self.logger.debug("get_ha_config()")
734-
api_url = "/api/config"
737+
api_url = f"{self.ha_url}/api/config"
735738
self.logger.debug("get_ha_config: url is %s", api_url)
736739
r = await self.session.get(api_url)
737740
r.raise_for_status()
@@ -754,7 +757,7 @@ async def get_hass_services(self) -> dict:
754757
try:
755758
self.logger.debug("get_hass_services()")
756759

757-
api_url = "/api/services"
760+
api_url = f"{self.ha_url}/api/services"
758761
self.logger.debug("get_hass_services: url is %s", api_url)
759762
r = await self.session.get(api_url)
760763

@@ -865,7 +868,7 @@ async def fire_plugin_event(self, event, namespace, **kwargs):
865868
assert namespace == self.namespace
866869

867870
event_clean = quote(event, safe="")
868-
api_url = "/api/events/{}".format(event_clean)
871+
api_url = f"{self.ha_url}/api/events/{event_clean}"
869872
try:
870873
r = await self.session.post(api_url, json=kwargs)
871874
r.raise_for_status()
@@ -894,7 +897,7 @@ async def remove_entity(self, namespace, entity_id):
894897
# if we get a request for not our namespace something has gone very wrong
895898
assert namespace == self.namespace
896899

897-
api_url = "/api/states/{}".format(entity_id)
900+
api_url = f"{self.ha_url}/api/states/{entity_id}"
898901

899902
try:
900903
r = await self.session.delete(api_url)

0 commit comments

Comments
 (0)