1
1
import asyncio
2
+ import datetime
2
3
import json
4
+ import os
3
5
import ssl
4
- import websocket
5
6
import traceback
7
+ from copy import deepcopy
8
+ from typing import Union
9
+ from urllib .parse import quote , urlencode
10
+
6
11
import aiohttp
7
12
import pytz
13
+ import websocket
8
14
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
14
15
15
16
import appdaemon .utils as utils
16
17
from appdaemon .appdaemon import AppDaemon
@@ -55,10 +56,12 @@ def __init__(self, ad: AppDaemon, name, args):
55
56
self .cert_path = args .get ("cert_path" )
56
57
self .cert_verify = args .get ("cert_verify" )
57
58
self .commtype = args .get ("commtype" , "WS" )
58
- self .ha_key = args .get ("ha_key" )
59
59
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
+
62
65
self .namespace = args .get ("namespace" , "default" )
63
66
self .plugin_startup_conditions = args .get ("plugin_startup_conditions" , {})
64
67
self .retry_secs = int (args .get ("retry_secs" , 5 ))
@@ -148,7 +151,7 @@ def session(self):
148
151
headers ["x-ha-access" ] = self .ha_key
149
152
150
153
self ._session = aiohttp .ClientSession (
151
- base_url = self .ha_url ,
154
+ # base_url=self.ha_url,
152
155
connector = conn ,
153
156
headers = headers ,
154
157
json_serialize = utils .convert_json ,
@@ -490,7 +493,7 @@ async def set_plugin_state(self, namespace, entity_id, **kwargs):
490
493
# if we get a request for not our namespace something has gone very wrong
491
494
assert namespace == self .namespace
492
495
493
- api_url = " /api/states/{}" . format ( entity_id )
496
+ api_url = f" { self . ha_url } /api/states/{ entity_id } "
494
497
495
498
try :
496
499
r = await self .session .post (api_url , json = kwargs )
@@ -548,7 +551,7 @@ async def call_plugin_service(self, namespace, domain, service, data):
548
551
return await self .get_history (** data )
549
552
550
553
else :
551
- api_url = " /api/services/{}/{}" . format ( domain , service )
554
+ api_url = f" { self . ha_url } /api/services/{ domain } /{ service } "
552
555
553
556
try :
554
557
r = await self .session .post (api_url , json = data )
@@ -668,7 +671,7 @@ def as_datetime(args, key):
668
671
669
672
# Build the url
670
673
# /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"
672
675
673
676
if start_time :
674
677
apiurl += "/" + utils .dt_to_str (start_time .replace (microsecond = 0 ), self .AD .tz )
@@ -684,9 +687,9 @@ def as_datetime(args, key):
684
687
685
688
async def get_hass_state (self , entity_id = None ):
686
689
if entity_id is None :
687
- api_url = " /api/states"
690
+ api_url = f" { self . ha_url } /api/states"
688
691
else :
689
- api_url = " /api/states/{}" . format ( entity_id )
692
+ api_url = f" { self . ha_url } /api/states/{ entity_id } "
690
693
self .logger .debug ("get_ha_state: url is %s" , api_url )
691
694
r = await self .session .get (api_url )
692
695
if r .status == 200 or r .status == 201 :
@@ -731,7 +734,7 @@ def validate_tz(self, meta):
731
734
async def get_hass_config (self ):
732
735
try :
733
736
self .logger .debug ("get_ha_config()" )
734
- api_url = " /api/config"
737
+ api_url = f" { self . ha_url } /api/config"
735
738
self .logger .debug ("get_ha_config: url is %s" , api_url )
736
739
r = await self .session .get (api_url )
737
740
r .raise_for_status ()
@@ -754,7 +757,7 @@ async def get_hass_services(self) -> dict:
754
757
try :
755
758
self .logger .debug ("get_hass_services()" )
756
759
757
- api_url = " /api/services"
760
+ api_url = f" { self . ha_url } /api/services"
758
761
self .logger .debug ("get_hass_services: url is %s" , api_url )
759
762
r = await self .session .get (api_url )
760
763
@@ -865,7 +868,7 @@ async def fire_plugin_event(self, event, namespace, **kwargs):
865
868
assert namespace == self .namespace
866
869
867
870
event_clean = quote (event , safe = "" )
868
- api_url = " /api/events/{}" . format ( event_clean )
871
+ api_url = f" { self . ha_url } /api/events/{ event_clean } "
869
872
try :
870
873
r = await self .session .post (api_url , json = kwargs )
871
874
r .raise_for_status ()
@@ -894,7 +897,7 @@ async def remove_entity(self, namespace, entity_id):
894
897
# if we get a request for not our namespace something has gone very wrong
895
898
assert namespace == self .namespace
896
899
897
- api_url = " /api/states/{}" . format ( entity_id )
900
+ api_url = f" { self . ha_url } /api/states/{ entity_id } "
898
901
899
902
try :
900
903
r = await self .session .delete (api_url )
0 commit comments