1 """The Flick Electric integration."""
3 from datetime
import datetime
as dt
7 from pyflick
import FlickAPI
8 from pyflick.authentication
import AbstractFlickAuth
9 from pyflick.const
import DEFAULT_CLIENT_ID, DEFAULT_CLIENT_SECRET
23 from .const
import CONF_TOKEN_EXPIRY, DOMAIN
25 _LOGGER = logging.getLogger(__name__)
27 CONF_ID_TOKEN =
"id_token"
29 PLATFORMS = [Platform.SENSOR]
33 """Set up Flick Electric from a config entry."""
36 hass.data.setdefault(DOMAIN, {})
37 hass.data[DOMAIN][entry.entry_id] = FlickAPI(auth)
39 await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
45 """Unload a config entry."""
46 unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
48 hass.data[DOMAIN].pop(entry.entry_id)
53 """Implementation of AbstractFlickAuth based on a Home Assistant entity config."""
55 def __init__(self, hass: HomeAssistant, entry: ConfigEntry) ->
None:
56 """Flick authentication based on a Home Assistant entity config."""
57 super().
__init__(aiohttp_client.async_get_clientsession(hass))
64 CONF_TOKEN_EXPIRY
not in self.
_entry_entry.data
65 or CONF_ACCESS_TOKEN
not in self.
_entry_entry.data
70 if self.
_entry_entry.data[CONF_TOKEN_EXPIRY] <= dt.now().timestamp():
73 return self.
_entry_entry.data[CONF_ACCESS_TOKEN]
76 _LOGGER.debug(
"Fetching new access token")
78 token = await self.get_new_token(
79 username=self.
_entry_entry.data[CONF_USERNAME],
80 password=self.
_entry_entry.data[CONF_PASSWORD],
81 client_id=self.
_entry_entry.data.get(CONF_CLIENT_ID, DEFAULT_CLIENT_ID),
82 client_secret=self.
_entry_entry.data.get(
83 CONF_CLIENT_SECRET, DEFAULT_CLIENT_SECRET
87 _LOGGER.debug(
"New token: %s", token)
90 token_decoded = jwt.decode(
91 token[CONF_ID_TOKEN], options={
"verify_signature":
False}
94 self.
_hass_hass.config_entries.async_update_entry(
98 CONF_ACCESS_TOKEN: token,
99 CONF_TOKEN_EXPIRY: token_decoded[
"exp"],
104 """Get Access Token from HASS Storage."""
107 return token[CONF_ID_TOKEN]
def async_get_access_token(self)
def _get_entry_token(self)
None __init__(self, HomeAssistant hass, ConfigEntry entry)
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)