Skip to content

Commit

Permalink
Merge pull request #6 from CoMPaTech/updatefix
Browse files Browse the repository at this point in the history
Add v4
  • Loading branch information
CoMPaTech authored Apr 19, 2022
2 parents 596b481 + 240feca commit 9143126
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 19 deletions.
2 changes: 1 addition & 1 deletion custom_components/stromer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
username = entry.data[CONF_USERNAME]
password = entry.data[CONF_PASSWORD]
client_id = entry.data[CONF_CLIENT_ID]
client_secret = entry.data[CONF_CLIENT_SECRET]
client_secret = entry.data.get(CONF_CLIENT_SECRET, None)

# Initialize connection to stromer
stromer = Stromer(username, password, client_id, client_secret)
Expand Down
2 changes: 1 addition & 1 deletion custom_components/stromer/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,4 @@ def __init__(
@property
def is_on(self) -> bool | None:
"""Return true if the binary sensor is on."""
return self._coordinator.bike.bikedata.get(self._ent)
return self._coordinator.data.bikedata.get(self._ent)
4 changes: 2 additions & 2 deletions custom_components/stromer/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
vol.Required(CONF_USERNAME): str,
vol.Required(CONF_PASSWORD): str,
vol.Required(CONF_CLIENT_ID): str,
vol.Required(CONF_CLIENT_SECRET): str,
vol.Optional(CONF_CLIENT_SECRET): str,
}
)

Expand All @@ -29,7 +29,7 @@ async def validate_input(hass: HomeAssistant, data: dict[str, Any]) -> dict[str,
username = data[CONF_USERNAME]
password = data[CONF_PASSWORD]
client_id = data[CONF_CLIENT_ID]
client_secret = data[CONF_CLIENT_SECRET]
client_secret = data.get(CONF_CLIENT_SECRET, None)

# Initialize connection to stromer
stromer = Stromer(username, password, client_id, client_secret)
Expand Down
2 changes: 1 addition & 1 deletion custom_components/stromer/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,4 @@ def __init__(
@property
def state(self):
"""Return the state of the sensor."""
return self._coordinator.bike.bikedata.get(self._ent)
return self._coordinator.data.bikedata.get(self._ent)
2 changes: 1 addition & 1 deletion custom_components/stromer/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"password": "Password",
"username" : "Username",
"client_id": "Client ID",
"client_secret": "Client Secret"
"client_secret": "Client Secret (optional, not needed if you client id starts with 4P"
}
}
},
Expand Down
40 changes: 29 additions & 11 deletions custom_components/stromer/stromer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Stromer module for Home Assistant Core."""

__version__ = "0.0.6"
__version__ = "0.0.7"

import json
import logging
Expand All @@ -19,6 +19,10 @@ def __init__(self, username, password, client_id, client_secret, timeout=60):
self.bike = {}
self.status = {}
self.position = {}

self._api_version = 'v4'
if client_secret:
self._api_version = 'v3'
self.base_url = "https://api3.stromer-portal.ch"

self._timeout = timeout
Expand Down Expand Up @@ -57,11 +61,13 @@ async def stromer_connect(self):

async def stromer_update(self):
attempts = 0
while attempts < 5:
while attempts < 10:
if attempts == 5:
LOGGER.info("Reconnecting to Stromer API")
await self.stromer_connect()
attempts += 1
await self.stromer_get_access_token()
try:
LOGGER.debug("Stromer attempt: {}/5".format(attempts))
LOGGER.debug("Stromer attempt: {}/10".format(attempts))
self.bike = await self.stromer_call_api(endpoint="bike/")
LOGGER.debug("Stromer bike: {}".format(self.bike))

Expand All @@ -82,10 +88,12 @@ async def stromer_update(self):

except Exception as e:
LOGGER.error("Stromer error: api call failed: {}".format(e))
LOGGER.debug("Stromer retry: {}/5".format(attempts))
LOGGER.debug("Stromer retry: {}/10".format(attempts))

async def stromer_get_code(self):
url = f"{self.base_url}/users/login/"
url = f"{self.base_url}/mobile/v4/login/"
if self._api_version == 'v3':
url = f"{self.base_url}/users/login/"
res = await self._websession.get(url)
cookie = res.headers.get("Set-Cookie")
pattern = "=(.*?);"
Expand All @@ -104,9 +112,12 @@ async def stromer_get_code(self):
"password": self._password,
"username": self._username,
"csrfmiddlewaretoken": csrftoken,
"next": "/o/authorize/?" + qs,
"next": "/mobile/v4/o/authorize/?" + qs
}

if self._api_version == 'v3':
data["next"]= "/o/authorize/?" + qs

res = await self._websession.post(
url, data=data, headers=dict(Referer=url), allow_redirects=False
)
Expand All @@ -117,21 +128,28 @@ async def stromer_get_code(self):
self._code = self._code.split("=")[1]

async def stromer_get_access_token(self):
url = f"{self.base_url}/o/token/"
url = f"{self.base_url}/mobile/v4/o/token/"
data = {
"grant_type": "authorization_code",
"client_id": self._client_id,
"client_secret": self._client_secret,
"code": self._code,
"redirect_uri": "stromerauth://auth",
"redirect_uri": "stromer://auth",
}

if self._api_version == 'v3':
url = f"{self.base_url}/o/token/"
data["client_secret"] = self._client_secret
data["redirect_uri"] = "stromerauth://auth"

res = await self._websession.post(url, data=data)
token = json.loads(await res.text())
self._token = token["access_token"]

async def stromer_call_api(self, endpoint, data={}):
url = f"{self.base_url}/rapi/mobile/v2/{endpoint}"
url = f"{self.base_url}/rapi/mobile/v4.1/{endpoint}"
if self._api_version == 'v3':
url = f"{self.base_url}/rapi/mobile/v2/{endpoint}"

headers = {"Authorization": f"Bearer {self._token}"}
# LOGGER.debug("token %s" % self._token)
res = await self._websession.get(url, headers=headers, data={})
Expand Down
2 changes: 1 addition & 1 deletion custom_components/stromer/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"password": "Password",
"username" : "Username",
"client_id": "Client ID",
"client_secret": "Client Secret"
"client_secret": "Client Secret (optional, not needed if your Client ID starts with 4P"
}
}
},
Expand Down
2 changes: 1 addition & 1 deletion custom_components/stromer/translations/nl.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"password": "Wachtwoord",
"username" : "Gebruikersnaam",
"client_id": "Client ID",
"client_secret": "Client Secret"
"client_secret": "Client Secret (optioneel, niet nodig als je client id met 4P begint"
}
}
},
Expand Down

0 comments on commit 9143126

Please sign in to comment.