Fix flick_electric auth failures (#65274)

This commit is contained in:
Brynley McDonald 2022-01-31 17:21:15 +13:00 committed by Paulus Schoutsen
parent 252f5f6b35
commit 6b6bd381fd
3 changed files with 16 additions and 7 deletions

View File

@ -1,7 +1,9 @@
"""The Flick Electric integration.""" """The Flick Electric integration."""
from datetime import datetime as dt from datetime import datetime as dt
import logging
import jwt
from pyflick import FlickAPI from pyflick import FlickAPI
from pyflick.authentication import AbstractFlickAuth from pyflick.authentication import AbstractFlickAuth
from pyflick.const import DEFAULT_CLIENT_ID, DEFAULT_CLIENT_SECRET from pyflick.const import DEFAULT_CLIENT_ID, DEFAULT_CLIENT_SECRET
@ -18,7 +20,9 @@ from homeassistant.const import (
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import aiohttp_client from homeassistant.helpers import aiohttp_client
from .const import CONF_TOKEN_EXPIRES_IN, CONF_TOKEN_EXPIRY, DOMAIN from .const import CONF_TOKEN_EXPIRY, DOMAIN
_LOGGER = logging.getLogger(__name__)
CONF_ID_TOKEN = "id_token" CONF_ID_TOKEN = "id_token"
@ -69,6 +73,8 @@ class HassFlickAuth(AbstractFlickAuth):
return self._entry.data[CONF_ACCESS_TOKEN] return self._entry.data[CONF_ACCESS_TOKEN]
async def _update_token(self): async def _update_token(self):
_LOGGER.debug("Fetching new access token")
token = await self.get_new_token( token = await self.get_new_token(
username=self._entry.data[CONF_USERNAME], username=self._entry.data[CONF_USERNAME],
password=self._entry.data[CONF_PASSWORD], password=self._entry.data[CONF_PASSWORD],
@ -78,15 +84,19 @@ class HassFlickAuth(AbstractFlickAuth):
), ),
) )
# Reduce expiry by an hour to avoid API being called after expiry _LOGGER.debug("New token: %s", token)
expiry = dt.now().timestamp() + int(token[CONF_TOKEN_EXPIRES_IN] - 3600)
# Flick will send the same token, but expiry is relative - so grab it from the token
token_decoded = jwt.decode(
token[CONF_ID_TOKEN], options={"verify_signature": False}
)
self._hass.config_entries.async_update_entry( self._hass.config_entries.async_update_entry(
self._entry, self._entry,
data={ data={
**self._entry.data, **self._entry.data,
CONF_ACCESS_TOKEN: token, CONF_ACCESS_TOKEN: token,
CONF_TOKEN_EXPIRY: expiry, CONF_TOKEN_EXPIRY: token_decoded["exp"],
}, },
) )

View File

@ -2,7 +2,6 @@
DOMAIN = "flick_electric" DOMAIN = "flick_electric"
CONF_TOKEN_EXPIRES_IN = "expires_in"
CONF_TOKEN_EXPIRY = "expires" CONF_TOKEN_EXPIRY = "expires"
ATTR_START_AT = "start_at" ATTR_START_AT = "start_at"

View File

@ -15,8 +15,6 @@ from homeassistant.util.dt import utcnow
from .const import ATTR_COMPONENTS, ATTR_END_AT, ATTR_START_AT, DOMAIN from .const import ATTR_COMPONENTS, ATTR_END_AT, ATTR_START_AT, DOMAIN
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
_AUTH_URL = "https://api.flick.energy/identity/oauth/token"
_RESOURCE = "https://api.flick.energy/customer/mobile_provider/price"
SCAN_INTERVAL = timedelta(minutes=5) SCAN_INTERVAL = timedelta(minutes=5)
@ -71,6 +69,8 @@ class FlickPricingSensor(SensorEntity):
async with async_timeout.timeout(60): async with async_timeout.timeout(60):
self._price = await self._api.getPricing() self._price = await self._api.getPricing()
_LOGGER.debug("Pricing data: %s", self._price)
self._attributes[ATTR_START_AT] = self._price.start_at self._attributes[ATTR_START_AT] = self._price.start_at
self._attributes[ATTR_END_AT] = self._price.end_at self._attributes[ATTR_END_AT] = self._price.end_at
for component in self._price.components: for component in self._price.components: