Skip to content

Commit 596b481

Browse files
authored
Merge pull request #5 from CoMPaTech/updatefix
Ensure entity state updating
2 parents b914fb7 + bbd7f1c commit 596b481

File tree

5 files changed

+17
-10
lines changed

5 files changed

+17
-10
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ Even though available does not mean it's stable yet, the HA part is solid but th
3030

3131
# Changelog
3232

33+
## APR 2022 [0.0.7]
34+
- Fix sensory updates
35+
- Potentially fix token expiration
36+
3337
## MAR 2022 [0.0.4]
3438
- Initial release
3539
- Creates a device for your bike in Home Assistant

custom_components/stromer/binary_sensor.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ def __init__(
7575
self._idx = idx
7676
self._ent = data[0]
7777
self._data = data[1]
78+
self._coordinator = coordinator
7879

7980
device_id = coordinator.data.bike_id
8081

@@ -85,4 +86,4 @@ def __init__(
8586
@property
8687
def is_on(self) -> bool | None:
8788
"""Return true if the binary sensor is on."""
88-
return self._data
89+
return self._coordinator.bike.bikedata.get(self._ent)

custom_components/stromer/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"domain": "stromer",
33
"name": "Stromer e-bike",
4-
"version": "0.0.5",
4+
"version": "0.0.7",
55
"documentation": "https://github.com/CoMPaTech/stromer",
66
"requirements": [],
77
"codeowners": ["@CoMPaTech"],

custom_components/stromer/sensor.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ def __init__(
164164
self._idx = idx
165165
self._ent = data[0]
166166
self._data = data[1]
167+
self._coordinator = coordinator
167168

168169
device_id = coordinator.data.bike_id
169170

@@ -174,4 +175,4 @@ def __init__(
174175
@property
175176
def state(self):
176177
"""Return the state of the sensor."""
177-
return self._data
178+
return self._coordinator.bike.bikedata.get(self._ent)

custom_components/stromer/stromer.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Stromer module for Home Assistant Core."""
22

3-
__version__ = "0.0.4"
3+
__version__ = "0.0.6"
44

55
import json
66
import 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

Comments
 (0)