I had a little bit of time to dig into this - it looks like ideally actually we should just get a genesis config setup for the opower integration that ships with HA - but I got this working with a temp fix…
Init a CookieJar()
Use it in the client sessions
Pull the csrf info from it
diff --git a/custom_components/genesisenergy/api.py b/custom_components/genesisenergy/api.py
index 474a031..7b51f64 100644
--- a/custom_components/genesisenergy/api.py
+++ b/custom_components/genesisenergy/api.py
@@ -36,6 +36,7 @@ class GenesisEnergyApi:
self._refresh_token = None
self._refresh_token_expires_in = 0
self._access_token_expires_in = 0
+ self._cookie_jar = aiohttp.CookieJar(quote_cookie=False)
def get_setting_json(self, page: str) -> Mapping[str, Any] | None:
"""Get the settings from json result."""
@@ -50,7 +51,7 @@ class GenesisEnergyApi:
"""Get the refresh token."""
_LOGGER.debug("API get_refresh_token")
- async with aiohttp.ClientSession() as session:
+ async with aiohttp.ClientSession(cookie_jar=self._cookie_jar) as session:
url = f"{self._url_token_base}/oauth2/v2.0/authorize"
scope = f'openid offline_access {self._client_id}'
params = {
@@ -94,7 +95,8 @@ class GenesisEnergyApi:
async with session.get(url, params=params) as response:
response_text = await response.text()
# Extract the new CSRF token from cookies because it changes here
- csrf_value = response.cookies.get('x-ms-cpim-csrf').value
+ cookies = self._cookie_jar[('auth.genesisenergy.co.nz', '')]
+ csrf_value = cookies.get('x-ms-cpim-csrf').value
csrf = csrf_value
payload = {
@@ -167,7 +169,7 @@ class GenesisEnergyApi:
"refresh_token": self._refresh_token,
}
- async with aiohttp.ClientSession() as session:
+ async with aiohttp.ClientSession(cookie_jar=self._cookie_jar) as session:
url = f"{self._url_token_base}/oauth2/v2.0/token"
async with session.post(url, data=token_data) as response:
if response.status == 200:
@@ -208,7 +210,7 @@ class GenesisEnergyApi:
'intervalType': "HOURLY"
}
- async with aiohttp.ClientSession() as session, \
+ async with aiohttp.ClientSession(cookie_jar=self._cookie_jar) as session, \
session.post(url, headers=headers, json=params) as response:
if response.status == 200:
data = await response.json()
@@ -220,7 +222,7 @@ class GenesisEnergyApi:
message = await response.text()
_LOGGER.error("Could not fetch consumption, error: %s", message)
return None
-
+
async def get_gas_data(self):
"""Get data from the API."""
@@ -252,7 +254,7 @@ class GenesisEnergyApi:
'intervalType': "HOURLY"
}
- async with aiohttp.ClientSession() as session, \
+ async with aiohttp.ClientSession(cookie_jar=self._cookie_jar) as session, \
session.get(url, headers=headers, params=params) as response:
if response.status == 200:
data = await response.json()
@@ -264,4 +266,4 @@ class GenesisEnergyApi:
message = await response.text()
_LOGGER.error("Could not fetch consumption, error: %s", message)
return None
-
+
Not entirely sure if it’s required to share the cookie jar with all the sessions, but will run this for a few days to make sure nothing crazy goes wrong and see if I can get it merged back into the git project.
Never contributed to a public git project before but I assume if I throw in a merge request that would be good @JoelBrenstrum ?