Skip to content

Commit

Permalink
Added reauth flow, much better error handling on setup
Browse files Browse the repository at this point in the history
### Feature
* Now supports reauthentication if credentials have expired/no longer work
* Better error handling when configuring integration, will notify user of incorrect credentials

### Fix
* Removed Owlet specific constants, now using homeassistant generic constants
* On initialisation the integration would crash when trying to update the auth token, the integration would then have to be deleted and setup again
  • Loading branch information
ryanbdclark committed May 12, 2023
1 parent bb0e8cc commit ad699b4
Show file tree
Hide file tree
Showing 15 changed files with 243 additions and 62 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,5 @@ cython_debug/
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/

/custom_components/owlet.zip
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Changelog

<!--next-version-placeholder-->

## v1.5.0 (2023-05-12)
### Feature
* Now supports reauthentication if credentials have expired/no longer work
* Better error handling when configuring integration, will notify user of incorrect credentials

### Fix
* Removed Owlet specific constants, now using homeassistant generic constants
* On initialisation the integration would crash when trying to update the auth token, the integration would then have to be deleted and setup again
54 changes: 54 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Owlet Custom Integration

[![GitHub Release][releases-shield]][releases]
![GitHub all releases][download-all]
![GitHub release (latest by RyanClark123)][download-latest]
[![GitHub Activity][commits-shield]][commits]

[![License][license-shield]][license]

[![hacs][hacsbadge]][hacs]
[![Project Maintenance][maintenance-shield]][user_profile]

A custom component for the Owlet smart sock, currently this only supports the owlet smart sock 3.

If you have a smart sock 2 and would like to contribute then please do so.

## Installation

1. Use [HACS](https://hacs.xyz/docs/setup/download), in `HACS > Integrations > Explore & Add Repositories` search for "Owlet". After adding this `https://github.com/RyanClark123/owlet` as a custom repository.
2. Restart Home Assistant.
3. [![Add Integration][add-integration-badge]][add-integration] or in the HA UI go to "Settings" -> "Devices & Services" then click "+" and search for "Owlet Smart Sock".


<!---->

## Usage

The `Owlet` integration offers integration with the Owlet Smart Sock cloud service. This provides sensors such as heart rate, oxygen saturation, charge percentage.

This integration provides the following entities:

- Binary sensors - charging status, high heart rate alert, low heart rate alert, high oxygen alert, low oxygen alert, low battery alert, lost power alert, sock diconnected alert, and sock status.
- Sensors - battery level, oxygen saturation, oxygen saturation 10 minute average, heart rate, battery time remaining, signal strength, and skin temperature.

## Options

- Seconds between polling - Number of seconds between each call for data from the owlet cloud service, default is 10 seconds.

---

[commits-shield]: https://img.shields.io/github/commit-activity/w/RyanClark123/owlet?style=for-the-badge
[commits]: https://github.com/RyanClark123/owlet/commits/main
[hacs]: https://github.com/hacs/integration
[hacsbadge]: https://img.shields.io/badge/HACS-Custom-orange.svg?style=for-the-badge
[license]: LICENSE
[license-shield]: https://img.shields.io/github/license/RyanClark123/owlet.svg?style=for-the-badge
[maintenance-shield]: https://img.shields.io/badge/maintainer-Ryan%20Clark%20%40RyanClark123-blue.svg?style=for-the-badge
[releases-shield]: https://img.shields.io/github/release/RyanClark123/owlet.svg?style=for-the-badge
[releases]: https://github.com/RyanClark123/owlet/releases
[user_profile]: https://github.com/RyanClark123
[download-all]: https://img.shields.io/github/downloads/RyanClark123/Owlet/total?style=for-the-badge
[download-latest]: https://img.shields.io/github/downloads/RyanClark123/Owlet/latest/total?style=for-the-badge
[add-integration]: https://my.home-assistant.io/redirect/config_flow_start?domain=owlet
[add-integration-badge]: https://my.home-assistant.io/badges/config_flow_start.svg
1 change: 1 addition & 0 deletions custom_components/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Custom Integrations for Home Assistant."""
41 changes: 22 additions & 19 deletions custom_components/owlet/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,22 @@

from pyowletapi.api import OwletAPI
from pyowletapi.sock import Sock
from pyowletapi.exceptions import OwletAuthenticationError, OwletDevicesError

from homeassistant.config_entries import ConfigEntry
from homeassistant.const import Platform
from pyowletapi.exceptions import OwletConnectionError

from homeassistant.config_entries import ConfigEntry, ConfigEntryAuthFailed
from homeassistant.const import (
Platform,
CONF_REGION,
CONF_USERNAME,
CONF_PASSWORD,
CONF_SCAN_INTERVAL,
CONF_API_TOKEN,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from .const import (
DOMAIN,
CONF_OWLET_REGION,
CONF_OWLET_USERNAME,
CONF_OWLET_PASSWORD,
CONF_OWLET_POLLINTERVAL,
CONF_OWLET_EXPIRY,
CONF_OWLET_TOKEN,
SUPPORTED_VERSIONS,
)
from .coordinator import OwletCoordinator
Expand All @@ -33,10 +35,10 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
hass.data.setdefault(DOMAIN, {})

owlet_api = OwletAPI(
entry.data[CONF_OWLET_REGION],
entry.data[CONF_OWLET_USERNAME],
entry.data[CONF_OWLET_PASSWORD],
entry.data[CONF_OWLET_TOKEN],
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),
)
Expand All @@ -45,20 +47,21 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
token = await owlet_api.authenticate()

if token:
entry.data[CONF_OWLET_TOKEN] = token[CONF_OWLET_TOKEN]
entry.data[CONF_OWLET_EXPIRY] = token[CONF_OWLET_EXPIRY]
hass.config_entries.async_update_entry(entry, data={**entry.data, **token})

socks = {
device["device"]["dsn"]: Sock(owlet_api, device["device"])
for device in await owlet_api.get_devices(SUPPORTED_VERSIONS)
}

except OwletAuthenticationError as err:
_LOGGER.error("Login failed %s", err)
return False
except OwletConnectionError as err:
_LOGGER.error("Credentials no longer valid, please setup owlet again")
raise ConfigEntryAuthFailed(
f"Credentials expired for {entry.data[CONF_USERNAME]}"
) from err

coordinators = [
OwletCoordinator(hass, sock, entry.options.get(CONF_OWLET_POLLINTERVAL))
OwletCoordinator(hass, sock, entry.options.get(CONF_SCAN_INTERVAL))
for sock in socks.values()
]

Expand Down
6 changes: 2 additions & 4 deletions custom_components/owlet/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,9 @@ def __init__(
sensor_description: OwletBinarySensorEntityDescription,
) -> None:
"""Initialize the binary sensor."""
self.entity_description = sensor_description
self._attr_unique_id = (
f"{coordinator.config_entry.entry_id}-{self.entity_description.name}"
)
super().__init__(coordinator)
self.entity_description = sensor_description
self._attr_unique_id = f"{self.sock.serial}-{self.entity_description.name}"

@property
def is_on(self) -> bool:
Expand Down
97 changes: 75 additions & 22 deletions custom_components/owlet/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,28 @@
from pyowletapi.api import OwletAPI
from pyowletapi.sock import Sock
from pyowletapi.exceptions import (
OwletConnectionError,
OwletAuthenticationError,
OwletDevicesError,
OwletEmailError,
OwletPasswordError,
)

import voluptuous as vol

from homeassistant import config_entries
from homeassistant import config_entries, exceptions
from homeassistant.data_entry_flow import FlowResult
from homeassistant.config_entries import ConfigEntry
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.core import callback
from homeassistant.const import (
CONF_REGION,
CONF_USERNAME,
CONF_PASSWORD,
CONF_SCAN_INTERVAL,
CONF_API_TOKEN,
)

from .const import (
DOMAIN,
CONF_OWLET_REGION,
CONF_OWLET_USERNAME,
CONF_OWLET_PASSWORD,
CONF_OWLET_POLLINTERVAL,
CONF_OWLET_TOKEN,
CONF_OWLET_EXPIRY,
POLLING_INTERVAL,
SUPPORTED_VERSIONS,
Expand Down Expand Up @@ -54,16 +56,17 @@ def __init__(self) -> None:
self._username: str
self._password: str
self._devices: dict[str, Sock]
self.reauth_entry: ConfigEntry | None = None

async def async_step_user(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
"""Handle the initial step."""
errors: dict[str, str] = {}
if user_input is not None:
self._region = user_input[CONF_OWLET_REGION]
self._username = user_input[CONF_OWLET_USERNAME]
self._password = user_input[CONF_OWLET_PASSWORD]
self._region = user_input[CONF_REGION]
self._username = user_input[CONF_USERNAME]
self._password = user_input[CONF_PASSWORD]

owlet_api = OwletAPI(
self._region,
Expand All @@ -82,21 +85,21 @@ async def async_step_user(
return self.async_create_entry(
title=self._username,
data={
CONF_OWLET_REGION: self._region,
CONF_OWLET_USERNAME: self._username,
CONF_OWLET_PASSWORD: self._password,
CONF_OWLET_TOKEN: token[CONF_OWLET_TOKEN],
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],
},
options={CONF_OWLET_POLLINTERVAL: POLLING_INTERVAL},
options={CONF_SCAN_INTERVAL: POLLING_INTERVAL},
)
except OwletDevicesError:
errors["base"] = "no_devices"

except OwletConnectionError:
errors["base"] = "cannot_connect"
except OwletAuthenticationError:
errors["base"] = "invalid_auth"
except OwletEmailError:
errors["base"] = "invalid_email"
except OwletPasswordError:
errors["base"] = "invalid_password"
except Exception: # pylint: disable=broad-except
_LOGGER.exception("Unexpected exception")
errors["base"] = "unknown"
Expand All @@ -111,6 +114,52 @@ def async_get_options_flow(config_entry):
"""Get the options flow for this handler."""
return OptionsFlowHandler(config_entry)

async def async_step_reauth(self, user_input=None):
"""Handle reauth"""
self.reauth_entry = self.hass.config_entries.async_get_entry(
self.context["entry_id"]
)
return await self.async_step_reauth_confirm()

async def async_step_reauth_confirm(self, user_input=None):
"""Dialog that informs the user that reauth is required"""
assert self.reauth_entry is not None
errors: dict[str, str] = {}

if user_input is not None:
entry_data = self.reauth_entry.data
owlet_api = OwletAPI(
entry_data[CONF_REGION],
entry_data[CONF_USERNAME],
user_input[CONF_PASSWORD],
session=async_get_clientsession(self.hass),
)
try:
token = await owlet_api.authenticate()
if token:
user_input[CONF_API_TOKEN] = token[CONF_API_TOKEN]
user_input[CONF_OWLET_EXPIRY] = token[CONF_OWLET_EXPIRY]
self.hass.config_entries.async_update_entry(
self.reauth_entry, data={**entry_data, **user_input}
)

await self.hass.config_entries.async_reload(self.reauth_entry.entry_id)

return self.async_abort(reason="reauth_successful")

except OwletEmailError:
errors["base"] = "invalid_email"
except OwletPasswordError:
errors["base"] = "invalid_password"
except Exception: # pylint: disable=broad-except
_LOGGER.exception("error reauthing")

return self.async_show_form(
step_id="reauth_confirm",
data_schema=vol.Schema({vol.Required(CONF_PASSWORD): str}),
errors=errors,
)


class OptionsFlowHandler(config_entries.OptionsFlow):
"""Handle a options flow for owlet"""
Expand All @@ -127,10 +176,14 @@ async def async_step_init(self, user_input=None):
schema = vol.Schema(
{
vol.Required(
CONF_OWLET_POLLINTERVAL,
default=self.config_entry.options.get(CONF_OWLET_POLLINTERVAL),
CONF_SCAN_INTERVAL,
default=self.config_entry.options.get(CONF_SCAN_INTERVAL),
): vol.All(vol.Coerce(int), vol.Range(min=10)),
}
)

return self.async_show_form(step_id="init", data_schema=schema)


class InvalidAuth(exceptions.HomeAssistantError):
"""Error to indiciate there is invalud auth"""
6 changes: 0 additions & 6 deletions custom_components/owlet/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@

DOMAIN = "owlet"

CONF_OWLET_REGION = "region"
CONF_OWLET_USERNAME = "username"
CONF_OWLET_PASSWORD = "password"
CONF_OWLET_DEVICES = "devices"
CONF_OWLET_POLLINTERVAL = "pollinterval"
CONF_OWLET_TOKEN = "token"
CONF_OWLET_EXPIRY = "expiry"

SUPPORTED_VERSIONS = [3]
Expand Down
4 changes: 2 additions & 2 deletions custom_components/owlet/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"homekit": {},
"iot_class": "cloud_polling",
"requirements": [
"pyowletapi==2023.5.18"
"pyowletapi==2023.5.20"
],
"version":"1.4.0"
"version":"1.5.0"
}
8 changes: 3 additions & 5 deletions custom_components/owlet/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,9 @@ def __init__(
sensor_description: OwletSensorEntityDescription,
) -> None:
"""Initialize the sensor."""
self.entity_description = sensor_description
self._attr_unique_id = (
f"{coordinator.config_entry.entry_id}-{self.entity_description.name}"
)
super().__init__(coordinator)
self.entity_description = sensor_description
self._attr_unique_id = f"{self.sock.serial}-{self.entity_description.name}"

@property
def native_value(self):
Expand All @@ -137,7 +135,7 @@ def native_value(self):
"battery_minutes",
"oxygen_saturation",
"skin_temperature",
"oxygen_10_av"
"oxygen_10_av",
]
and self.sock.properties["charging"]
):
Expand Down
9 changes: 8 additions & 1 deletion custom_components/owlet/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,18 @@
"username": "Email",
"password": "Password"
}
},
"reauth_confirm":{
"title": "Reauthentiaction required for Owlet",
"data":{
"password": "Password"
}
}
},
"error": {
"cannot_connect": "[%key:common::config_flow::error::cannot_connect%]",
"invalid_auth": "[%key:common::config_flow::error::invalid_auth%]",
"invalid_email": "Entered email address is incorrect",
"invalid_password": "Entered password is incorrect",
"unknown": "[%key:common::config_flow::error::unknown%]"
},
"abort": {
Expand Down
Loading

0 comments on commit ad699b4

Please sign in to comment.