11"""Stromer module for Home Assistant Core."""
22
3- __version__ = "0.0.4 "
3+ __version__ = "0.0.6 "
44
55import json
66import logging
@@ -19,6 +19,7 @@ def __init__(self, username, password, client_id, client_secret, timeout=60):
1919 self .bike = {}
2020 self .status = {}
2121 self .position = {}
22+ self .base_url = "https://api3.stromer-portal.ch"
2223
2324 self ._timeout = timeout
2425 self ._username = username
@@ -37,7 +38,7 @@ async def stromer_connect(self):
3738 aio_timeout = aiohttp .ClientTimeout (total = self ._timeout )
3839 self ._websession = aiohttp .ClientSession (timeout = aio_timeout )
3940
40- # Retrieve access token
41+ # Retrieve authorization token
4142 await self .stromer_get_code ()
4243 # LOGGER.debug("Stromer code: {}".format(self._code))
4344
@@ -58,6 +59,7 @@ async def stromer_update(self):
5859 attempts = 0
5960 while attempts < 5 :
6061 attempts += 1
62+ await self .stromer_get_access_token ()
6163 try :
6264 LOGGER .debug ("Stromer attempt: {}/5" .format (attempts ))
6365 self .bike = await self .stromer_call_api (endpoint = "bike/" )
@@ -83,8 +85,7 @@ async def stromer_update(self):
8385 LOGGER .debug ("Stromer retry: {}/5" .format (attempts ))
8486
8587 async def stromer_get_code (self ):
86- base_url = "https://api3.stromer-portal.ch"
87- url = f"{ base_url } /users/login/"
88+ url = f"{ self .base_url } /users/login/"
8889 res = await self ._websession .get (url )
8990 cookie = res .headers .get ("Set-Cookie" )
9091 pattern = "=(.*?);"
@@ -110,13 +111,13 @@ async def stromer_get_code(self):
110111 url , data = data , headers = dict (Referer = url ), allow_redirects = False
111112 )
112113 next_loc = res .headers .get ("Location" )
113- next_url = f"{ base_url } { next_loc } "
114+ next_url = f"{ self . base_url } { next_loc } "
114115 res = await self ._websession .get (next_url , allow_redirects = False )
115116 self ._code = res .headers .get ("Location" )
116117 self ._code = self ._code .split ("=" )[1 ]
117118
118119 async def stromer_get_access_token (self ):
119- url = "https://api3.stromer-portal.ch /o/token/"
120+ url = f" { self . base_url } /o/token/"
120121 data = {
121122 "grant_type" : "authorization_code" ,
122123 "client_id" : self ._client_id ,
@@ -130,7 +131,7 @@ async def stromer_get_access_token(self):
130131 self ._token = token ["access_token" ]
131132
132133 async def stromer_call_api (self , endpoint , data = {}):
133- url = f"https://api3.stromer-portal.ch /rapi/mobile/v2/{ endpoint } "
134+ url = f"{ self . base_url } /rapi/mobile/v2/{ endpoint } "
134135 headers = {"Authorization" : f"Bearer { self ._token } " }
135136 # LOGGER.debug("token %s" % self._token)
136137 res = await self ._websession .get (url , headers = headers , data = {})
0 commit comments