diff --git a/custom_components/owlet/__init__.py b/custom_components/owlet/__init__.py index c482996..434bd29 100644 --- a/custom_components/owlet/__init__.py +++ b/custom_components/owlet/__init__.py @@ -5,7 +5,7 @@ import logging from pyowletapi.api import OwletAPI from pyowletapi.sock import Sock -from pyowletapi.exceptions import OwletConnectionError +from pyowletapi.exceptions import OwletAuthenticationError from homeassistant.config_entries import ConfigEntry, ConfigEntryAuthFailed from homeassistant.const import ( @@ -21,6 +21,7 @@ from homeassistant.helpers.aiohttp_client import async_get_clientsession from .const import ( DOMAIN, CONF_OWLET_EXPIRY, + CONF_OWLET_REFRESH, SUPPORTED_VERSIONS, ) from .coordinator import OwletCoordinator @@ -35,12 +36,11 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: hass.data.setdefault(DOMAIN, {}) owlet_api = OwletAPI( - entry.data[CONF_REGION], - entry.data[CONF_USERNAME], - entry.data[CONF_PASSWORD], - entry.data[CONF_API_TOKEN], - entry.data[CONF_OWLET_EXPIRY], - async_get_clientsession(hass), + region=entry.data[CONF_REGION], + token=entry.data[CONF_API_TOKEN], + expiry=entry.data[CONF_OWLET_EXPIRY], + refresh=entry.data[CONF_OWLET_REFRESH], + session=async_get_clientsession(hass), ) 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) } - except OwletConnectionError as err: + except OwletAuthenticationError as err: _LOGGER.error("Credentials no longer valid, please setup owlet again") raise ConfigEntryAuthFailed( f"Credentials expired for {entry.data[CONF_USERNAME]}" diff --git a/custom_components/owlet/config_flow.py b/custom_components/owlet/config_flow.py index 82b8314..9115a45 100644 --- a/custom_components/owlet/config_flow.py +++ b/custom_components/owlet/config_flow.py @@ -32,6 +32,7 @@ from .const import ( CONF_OWLET_EXPIRY, POLLING_INTERVAL, SUPPORTED_VERSIONS, + CONF_OWLET_REFRESH, ) _LOGGER = logging.getLogger(__name__) @@ -87,9 +88,9 @@ class OwletConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): data={ CONF_REGION: self._region, CONF_USERNAME: self._username, - CONF_PASSWORD: self._password, CONF_API_TOKEN: token[CONF_API_TOKEN], CONF_OWLET_EXPIRY: token[CONF_OWLET_EXPIRY], + CONF_OWLET_REFRESH: token[CONF_OWLET_REFRESH], }, options={CONF_SCAN_INTERVAL: POLLING_INTERVAL}, ) @@ -139,6 +140,7 @@ class OwletConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): if token: user_input[CONF_API_TOKEN] = token[CONF_API_TOKEN] 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.reauth_entry, data={**entry_data, **user_input} ) diff --git a/custom_components/owlet/const.py b/custom_components/owlet/const.py index 04f0f48..c91d26f 100644 --- a/custom_components/owlet/const.py +++ b/custom_components/owlet/const.py @@ -3,7 +3,8 @@ DOMAIN = "owlet" CONF_OWLET_EXPIRY = "expiry" +CONF_OWLET_REFRESH = "refresh" SUPPORTED_VERSIONS = [3] -POLLING_INTERVAL = 10 +POLLING_INTERVAL = 5 MANUFACTURER = "Owlet Baby Care" diff --git a/custom_components/owlet/coordinator.py b/custom_components/owlet/coordinator.py index 7975161..fd7fad6 100644 --- a/custom_components/owlet/coordinator.py +++ b/custom_components/owlet/coordinator.py @@ -5,16 +5,18 @@ from datetime import timedelta import logging 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.helpers.entity import DeviceInfo from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed +from homeassistant.const import CONF_API_TOKEN -from .const import ( - DOMAIN, - MANUFACTURER, -) +from .const import DOMAIN, MANUFACTURER, CONF_OWLET_EXPIRY, CONF_OWLET_REFRESH _LOGGER = logging.getLogger(__name__) @@ -49,5 +51,16 @@ class OwletCoordinator(DataUpdateCoordinator): """Fetch the data from the device.""" try: 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 diff --git a/custom_components/owlet/manifest.json b/custom_components/owlet/manifest.json index 45dc654..3ed5a51 100644 --- a/custom_components/owlet/manifest.json +++ b/custom_components/owlet/manifest.json @@ -10,7 +10,7 @@ "homekit": {}, "iot_class": "cloud_polling", "requirements": [ - "pyowletapi==2023.5.21" + "pyowletapi==2023.5.23" ], "version":"2023.5.1" }