Integration now uses refresh token

-Makes use of pyowletapi 2023.5.23 to use the refresh token to reauthenticate, integration now no longer stores users password
-Default polling interval changed to 5 seconds to match owlet app
This commit is contained in:
RyanClark123
2023-05-16 14:05:26 +01:00
parent 8d9299a8f4
commit dc710a1783
5 changed files with 33 additions and 17 deletions
+8 -8
View File
@@ -5,7 +5,7 @@ import logging
from pyowletapi.api import OwletAPI from pyowletapi.api import OwletAPI
from pyowletapi.sock import Sock from pyowletapi.sock import Sock
from pyowletapi.exceptions import OwletConnectionError from pyowletapi.exceptions import OwletAuthenticationError
from homeassistant.config_entries import ConfigEntry, ConfigEntryAuthFailed from homeassistant.config_entries import ConfigEntry, ConfigEntryAuthFailed
from homeassistant.const import ( from homeassistant.const import (
@@ -21,6 +21,7 @@ from homeassistant.helpers.aiohttp_client import async_get_clientsession
from .const import ( from .const import (
DOMAIN, DOMAIN,
CONF_OWLET_EXPIRY, CONF_OWLET_EXPIRY,
CONF_OWLET_REFRESH,
SUPPORTED_VERSIONS, SUPPORTED_VERSIONS,
) )
from .coordinator import OwletCoordinator from .coordinator import OwletCoordinator
@@ -35,12 +36,11 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
hass.data.setdefault(DOMAIN, {}) hass.data.setdefault(DOMAIN, {})
owlet_api = OwletAPI( owlet_api = OwletAPI(
entry.data[CONF_REGION], region=entry.data[CONF_REGION],
entry.data[CONF_USERNAME], token=entry.data[CONF_API_TOKEN],
entry.data[CONF_PASSWORD], expiry=entry.data[CONF_OWLET_EXPIRY],
entry.data[CONF_API_TOKEN], refresh=entry.data[CONF_OWLET_REFRESH],
entry.data[CONF_OWLET_EXPIRY], session=async_get_clientsession(hass),
async_get_clientsession(hass),
) )
try: try:
@@ -54,7 +54,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
for device in await owlet_api.get_devices(SUPPORTED_VERSIONS) for device in await owlet_api.get_devices(SUPPORTED_VERSIONS)
} }
except OwletConnectionError as err: except OwletAuthenticationError as err:
_LOGGER.error("Credentials no longer valid, please setup owlet again") _LOGGER.error("Credentials no longer valid, please setup owlet again")
raise ConfigEntryAuthFailed( raise ConfigEntryAuthFailed(
f"Credentials expired for {entry.data[CONF_USERNAME]}" f"Credentials expired for {entry.data[CONF_USERNAME]}"
+3 -1
View File
@@ -32,6 +32,7 @@ from .const import (
CONF_OWLET_EXPIRY, CONF_OWLET_EXPIRY,
POLLING_INTERVAL, POLLING_INTERVAL,
SUPPORTED_VERSIONS, SUPPORTED_VERSIONS,
CONF_OWLET_REFRESH,
) )
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@@ -87,9 +88,9 @@ class OwletConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
data={ data={
CONF_REGION: self._region, CONF_REGION: self._region,
CONF_USERNAME: self._username, CONF_USERNAME: self._username,
CONF_PASSWORD: self._password,
CONF_API_TOKEN: token[CONF_API_TOKEN], CONF_API_TOKEN: token[CONF_API_TOKEN],
CONF_OWLET_EXPIRY: token[CONF_OWLET_EXPIRY], CONF_OWLET_EXPIRY: token[CONF_OWLET_EXPIRY],
CONF_OWLET_REFRESH: token[CONF_OWLET_REFRESH],
}, },
options={CONF_SCAN_INTERVAL: POLLING_INTERVAL}, options={CONF_SCAN_INTERVAL: POLLING_INTERVAL},
) )
@@ -139,6 +140,7 @@ class OwletConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
if token: if token:
user_input[CONF_API_TOKEN] = token[CONF_API_TOKEN] user_input[CONF_API_TOKEN] = token[CONF_API_TOKEN]
user_input[CONF_OWLET_EXPIRY] = token[CONF_OWLET_EXPIRY] user_input[CONF_OWLET_EXPIRY] = token[CONF_OWLET_EXPIRY]
user_input[CONF_OWLET_REFRESH] = token[CONF_OWLET_REFRESH]
self.hass.config_entries.async_update_entry( self.hass.config_entries.async_update_entry(
self.reauth_entry, data={**entry_data, **user_input} self.reauth_entry, data={**entry_data, **user_input}
) )
+2 -1
View File
@@ -3,7 +3,8 @@
DOMAIN = "owlet" DOMAIN = "owlet"
CONF_OWLET_EXPIRY = "expiry" CONF_OWLET_EXPIRY = "expiry"
CONF_OWLET_REFRESH = "refresh"
SUPPORTED_VERSIONS = [3] SUPPORTED_VERSIONS = [3]
POLLING_INTERVAL = 10 POLLING_INTERVAL = 5
MANUFACTURER = "Owlet Baby Care" MANUFACTURER = "Owlet Baby Care"
+19 -6
View File
@@ -5,16 +5,18 @@ from datetime import timedelta
import logging import logging
from pyowletapi.sock import Sock from pyowletapi.sock import Sock
from pyowletapi.exceptions import OwletError, OwletConnectionError from pyowletapi.exceptions import (
OwletError,
OwletConnectionError,
OwletAuthenticationError,
)
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import DeviceInfo from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
from homeassistant.const import CONF_API_TOKEN
from .const import ( from .const import DOMAIN, MANUFACTURER, CONF_OWLET_EXPIRY, CONF_OWLET_REFRESH
DOMAIN,
MANUFACTURER,
)
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@@ -49,5 +51,16 @@ class OwletCoordinator(DataUpdateCoordinator):
"""Fetch the data from the device.""" """Fetch the data from the device."""
try: try:
await self.sock.update_properties() await self.sock.update_properties()
except (OwletError, OwletConnectionError) as err: tokens = await self.sock.api.tokens_changed(
{
CONF_API_TOKEN: self.config_entry.data[CONF_API_TOKEN],
CONF_OWLET_EXPIRY: self.config_entry.data[CONF_OWLET_EXPIRY],
CONF_OWLET_REFRESH: self.config_entry.data[CONF_OWLET_REFRESH],
}
)
if tokens:
self.hass.config_entries.async_update_entry(
self.config_entry, data={**self.config_entry.data, **tokens}
)
except (OwletError, OwletConnectionError, OwletAuthenticationError) as err:
raise UpdateFailed(err) from err raise UpdateFailed(err) from err
+1 -1
View File
@@ -10,7 +10,7 @@
"homekit": {}, "homekit": {},
"iot_class": "cloud_polling", "iot_class": "cloud_polling",
"requirements": [ "requirements": [
"pyowletapi==2023.5.21" "pyowletapi==2023.5.23"
], ],
"version":"2023.5.1" "version":"2023.5.1"
} }