Let token survive addon updates

This commit is contained in:
Dielee
2024-05-27 14:16:07 +02:00
parent 8bd0388690
commit 6306c9a4b3
4 changed files with 24 additions and 13 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
name: "Volvo2Mqtt"
description: "Volvo AAOS MQTT bridge"
version: "1.9.0"
version: "1.9.1"
slug: "volvo2mqtt"
init: false
url: "https://github.com/Dielee/volvo2mqtt"
+2 -2
View File
@@ -1,6 +1,6 @@
from config import settings
VERSION = "v1.9.0"
VERSION = "v1.9.1"
OAUTH_TOKEN_URL = "https://volvoid.eu.volvocars.com/as/token.oauth2"
OAUTH_AUTH_URL = "https://volvoid.eu.volvocars.com/as/authorization.oauth2"
@@ -139,5 +139,5 @@ supported_entities = [
]
old_entity_ids = ["months_to_service", "service_warning_trigger", "distance_to_empty"]
otp_max_loops = 15
otp_max_loops = 24
otp_mqtt_topic = "volvoAAOS2mqtt/otp_code"
+13 -5
View File
@@ -42,8 +42,16 @@ def get_volvo_app_version():
else:
return "5.37.0"
def save_to_json(data):
with open('.token', 'w', encoding='utf-8') as f:
def get_token_path():
token_path = ".token"
if os.environ.get("IS_HA_ADDON"):
check_existing_folder("/addons/volvo2mqtt/token/")
token_path = "/addons/volvo2mqtt/token/.token"
return token_path
def save_to_json(data, token_path):
with open(token_path, 'w', encoding='utf-8') as f:
json.dump(data, f, ensure_ascii=False, indent=4)
def get_icon_between(icon_list, state):
@@ -57,7 +65,7 @@ def get_icon_between(icon_list, state):
def setup_logging():
log_location = "volvo2mqtt.log"
if os.environ.get("IS_HA_ADDON"):
check_existing_folder()
check_existing_folder("/addons/volvo2mqtt/log/")
log_location = "/addons/volvo2mqtt/log/volvo2mqtt.log"
logging.Formatter.converter = lambda *args: datetime.now(tz=TZ).timetuple()
@@ -90,8 +98,8 @@ def setup_logging():
logger.setLevel(logging.ERROR)
def check_existing_folder():
Path("/addons/volvo2mqtt/log/").mkdir(parents=True, exist_ok=True)
def check_existing_folder(path):
Path(path).mkdir(parents=True, exist_ok=True)
def keys_exists(element, *keys):
+8 -5
View File
@@ -35,9 +35,11 @@ backend_status = ""
def authorize(renew_tokenfile=False):
global refresh_token
if os.path.isfile(".token") and not renew_tokenfile:
token_path = util.get_token_path()
if os.path.isfile(token_path) and not renew_tokenfile:
logging.info("Using login from token file")
f = open('.token')
f = open(token_path)
data = json.load(f)
refresh_token = data["refresh_token"]
refresh_auth()
@@ -91,7 +93,7 @@ def authorize(renew_tokenfile=False):
token_expires_at = datetime.now(util.TZ) + timedelta(seconds=(token_data["expires_in"] - 30))
refresh_token = token_data["refresh_token"]
util.save_to_json(token_data)
util.save_to_json(token_data, token_path)
else:
message = auth.json()
raise Exception(message["details"][0]["userMessage"])
@@ -149,7 +151,7 @@ def send_otp(auth_session, data):
logging.info("Waiting for otp code... Please check your mailbox and post your otp code to the following "
"mqtt topic \"volvoAAOS2mqtt/otp_code\". Retry " + str(i) + "/" + str(otp_max_loops))
time.sleep(8)
time.sleep(5)
if not mqtt.otp_code:
raise Exception ("No OTP found, exting...")
@@ -184,8 +186,9 @@ def refresh_auth():
return None
if auth.status_code == 200:
token_path = util.get_token_path()
data = auth.json()
util.save_to_json(data)
util.save_to_json(data, token_path)
session.headers.update({"authorization": "Bearer " + data["access_token"]})
global token_expires_at