Skip to content

Commit

Permalink
Merge branch '4.4.0_rc1' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
acockburn committed Mar 31, 2023
2 parents 54c3f8a + d4a2a09 commit ab20741
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"request": "launch",
"module": "appdaemon",
"justMyCode": true,
"args": "-c /home/hass/appdaemon/dev_test --toml"
"args": "-c /home/hass/ad_config/dev_test --toml"
},
]
}
41 changes: 22 additions & 19 deletions appdaemon/plugins/hass/hassplugin.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import asyncio
import datetime
import json
import os
import ssl
import websocket
import traceback
from copy import deepcopy
from typing import Union
from urllib.parse import quote, urlencode

import aiohttp
import pytz
import websocket
from deepdiff import DeepDiff
from copy import deepcopy
import datetime
from urllib.parse import quote
from urllib.parse import urlencode
from typing import Union

import appdaemon.utils as utils
from appdaemon.appdaemon import AppDaemon
Expand Down Expand Up @@ -55,10 +56,12 @@ def __init__(self, ad: AppDaemon, name, args):
self.cert_path = args.get("cert_path")
self.cert_verify = args.get("cert_verify")
self.commtype = args.get("commtype", "WS")
self.ha_key = args.get("ha_key")

# Remove trailing slash if present
self.ha_url = args.get("ha_url", "").rstrip("/")
# Fixes for supervised
self.ha_key = args.get("ha_key", os.environ.get("SUPERVISOR_TOKEN"))
self.ha_url = args.get("ha_url", "http://supervisor/core").rstrip("/")
# End fixes for supervised

self.namespace = args.get("namespace", "default")
self.plugin_startup_conditions = args.get("plugin_startup_conditions", {})
self.retry_secs = int(args.get("retry_secs", 5))
Expand Down Expand Up @@ -148,7 +151,7 @@ def session(self):
headers["x-ha-access"] = self.ha_key

self._session = aiohttp.ClientSession(
base_url=self.ha_url,
# base_url=self.ha_url,
connector=conn,
headers=headers,
json_serialize=utils.convert_json,
Expand Down Expand Up @@ -490,7 +493,7 @@ async def set_plugin_state(self, namespace, entity_id, **kwargs):
# if we get a request for not our namespace something has gone very wrong
assert namespace == self.namespace

api_url = "/api/states/{}".format(entity_id)
api_url = f"{self.ha_url}/api/states/{entity_id}"

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

else:
api_url = "/api/services/{}/{}".format(domain, service)
api_url = f"{self.ha_url}/api/services/{domain}/{service}"

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

# Build the url
# /api/history/period/<start_time>?filter_entity_id=<entity_id>&end_time=<end_time>
apiurl = "/api/history/period"
apiurl = f"{self.ha_url}/api/history/period"

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

async def get_hass_state(self, entity_id=None):
if entity_id is None:
api_url = "/api/states"
api_url = f"{self.ha_url}/api/states"
else:
api_url = "/api/states/{}".format(entity_id)
api_url = f"{self.ha_url}/api/states/{entity_id}"
self.logger.debug("get_ha_state: url is %s", api_url)
r = await self.session.get(api_url)
if r.status == 200 or r.status == 201:
Expand Down Expand Up @@ -731,7 +734,7 @@ def validate_tz(self, meta):
async def get_hass_config(self):
try:
self.logger.debug("get_ha_config()")
api_url = "/api/config"
api_url = f"{self.ha_url}/api/config"
self.logger.debug("get_ha_config: url is %s", api_url)
r = await self.session.get(api_url)
r.raise_for_status()
Expand All @@ -754,7 +757,7 @@ async def get_hass_services(self) -> dict:
try:
self.logger.debug("get_hass_services()")

api_url = "/api/services"
api_url = f"{self.ha_url}/api/services"
self.logger.debug("get_hass_services: url is %s", api_url)
r = await self.session.get(api_url)

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

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

api_url = "/api/states/{}".format(entity_id)
api_url = f"{self.ha_url}/api/states/{entity_id}"

try:
r = await self.session.delete(api_url)
Expand Down

0 comments on commit ab20741

Please sign in to comment.