diff --git a/homeassistant/components/flick_electric/__init__.py b/homeassistant/components/flick_electric/__init__.py index c29a476ca55..54eaf5a6917 100644 --- a/homeassistant/components/flick_electric/__init__.py +++ b/homeassistant/components/flick_electric/__init__.py @@ -1,7 +1,9 @@ """The Flick Electric integration.""" from datetime import datetime as dt +import logging +import jwt from pyflick import FlickAPI from pyflick.authentication import AbstractFlickAuth 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.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" @@ -69,6 +73,8 @@ class HassFlickAuth(AbstractFlickAuth): return self._entry.data[CONF_ACCESS_TOKEN] async def _update_token(self): + _LOGGER.debug("Fetching new access token") + token = await self.get_new_token( username=self._entry.data[CONF_USERNAME], 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 - expiry = dt.now().timestamp() + int(token[CONF_TOKEN_EXPIRES_IN] - 3600) + _LOGGER.debug("New token: %s", token) + + # 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._entry, data={ **self._entry.data, CONF_ACCESS_TOKEN: token, - CONF_TOKEN_EXPIRY: expiry, + CONF_TOKEN_EXPIRY: token_decoded["exp"], }, ) diff --git a/homeassistant/components/flick_electric/const.py b/homeassistant/components/flick_electric/const.py index e8365f37411..de1942096b5 100644 --- a/homeassistant/components/flick_electric/const.py +++ b/homeassistant/components/flick_electric/const.py @@ -2,7 +2,6 @@ DOMAIN = "flick_electric" -CONF_TOKEN_EXPIRES_IN = "expires_in" CONF_TOKEN_EXPIRY = "expires" ATTR_START_AT = "start_at" diff --git a/homeassistant/components/flick_electric/sensor.py b/homeassistant/components/flick_electric/sensor.py index d6a1bd59d8e..92bc81b5aa0 100644 --- a/homeassistant/components/flick_electric/sensor.py +++ b/homeassistant/components/flick_electric/sensor.py @@ -15,8 +15,6 @@ from homeassistant.util.dt import utcnow from .const import ATTR_COMPONENTS, ATTR_END_AT, ATTR_START_AT, DOMAIN _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) @@ -71,6 +69,8 @@ class FlickPricingSensor(SensorEntity): async with async_timeout.timeout(60): 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_END_AT] = self._price.end_at for component in self._price.components: