What about to update the access_token@ElGranLoky ? The refresh_token can be the same in the Xee server response, but the access_token is new everytime the token is refresh.
Up to now my solution is to run the refresh command (and also updating the secrets.yaml with the new access_token) everyday and restart Home Assistant. But this workaround is ugly.
It use the python api3 sdk from quentin
You need to install it before of course
Create or have a Xee dev accounts.
And then i use a simple cron that run on another container and publish through mqtt:
from xee import Xee
import xee.entities as xee_entities
import sys
import os
import pickle
import pytz
import json
import paho.mqtt.client as paho
broker="YOUR BROKER IP OR DNS NAME"
port=1883
def on_publish(client,userdata,result): #create function for callback
print("data published \n")
pass
client1= paho.Client("xee2mqtt") #create client object
client1.on_publish = on_publish #assign function to callback
client1.connect(broker,port) #establish connection
xee = Xee(client_id="YOUR_CLIENT_ID_FROM_XEE_DEV_ACCOUNT",
client_secret="YOUR_CLIENT_SECRET_FROM_XEE_DEV_ACCOUNT",
redirect_uri="http://localhost")
login_url = xee.get_authentication_url()+"&redirect_uri=http://localhost"
xee_config_file = os.path.join(os.path.dirname(__file__), 'xee.json')
try:
with open(xee_config_file, 'rb') as xee_token_file:
print ("Opening File")
token = pickle.load(xee_token_file)
print ("Getting user")
user ,error = xee.get_user(token.access_token)
print (error)
if error is not None :
print ("Error getting user, try refreshing with token_refresh from file")
print (error)
token,error = xee.get_token_from_refresh_token(token.refresh_token)
if error != None :
print (error)
sys.exit("refreshing token failed from refresh_token")
except:
print ("Error with file saved or no file saved")
print("Go to %s allow the app and copy your oauth_verifier" %login_url)
authorization_code = input('Please enter your authorization_code: ')
token,error = xee.get_token_from_code(authorization_code)
if error is not None :
print ("Error getting token from code")
print (error)
print ("Exiting")
sys.exit("refresh Error")
with open(xee_config_file, 'wb') as xee_token_file:
pickle.dump(token, xee_token_file)
cars, err = xee.get_cars(token.access_token)
for car in cars:
try:
client1.publish("/XEE/" + str(car.id) + "/carname/", str(car.name))
except:
print ("error publishing this value")
print (car)
Status ,error = xee.get_status(car.id,token.access_token)
if error is None:
for statu in Status:
if statu is not None:
for signals in statu:
try:
client1.publish("/XEE/" + str(car.id) + "/" + str(signals.name) + "", str(signals.value))
print (signals)
except:
print ("error publishing this value")
print (signals)
locations ,error = xee.get_locations(car.id,token.access_token,limit=1)
for location in locations:
try:
lat=location.latitude
lon=location.longitude
client1.publish("/XEE/" + str(car.id) + "/location/", json.dumps({"longitude": lon,"latitude": lat}))
print (location)
except:
print ("error publishing this value")
print (location)
Create a Xee2mqtt.py with this code
Launch once by hand and it will bring you to xee user account to allow access.
Then redirect you to a localhost page containing your authorization code.
Type it in the answer waiting from the cli.
It will create a json file containing the access token and the refresh token.
So you never have to look it up again, it will try refresh it automatically (works 99% of the time).
Create a cron that fire this script at the frequencies you wanted.
Benefits it list all the users cars and available signals alone
I hope to have time to make an hacs for this but i have more to do for know, like looking at plcbus again and need to learn how HASS is working.
If anyone has more notion on how to make hacs integration do not hesitate here are all the base to do it in pure python.
im trying but the api v3 dont work in the first execution… im tryng generate the code with the api v4 but is invalid…
help please!!
root@SuperDocker:/home# python3 Xee2mqtt.py
i’ve update the python xee sdk lib.
I also update my Xee2mqtt code so no more token problems.
When it expires it grab a new one automatically from refresh one’s.
All the explaination are in the Readme
Do not hesitate to ask, open ticket, PR etc.
Next step will be a complete hacs and hass compoents after that. (But it will take again a long time.)
Note also it makes a lot of print for the moment to debug…