c6f37493ca
###Fix # In preparation for pull request to homeassistant core, just corrected some spelling, sorted imports using isort and corrected some type hinting
69 lines
2.2 KiB
Python
69 lines
2.2 KiB
Python
"""Owlet integration coordinator class."""
|
|
from __future__ import annotations
|
|
|
|
from datetime import timedelta
|
|
import logging
|
|
|
|
from pyowletapi.exceptions import (
|
|
OwletAuthenticationError,
|
|
OwletConnectionError,
|
|
OwletError,
|
|
)
|
|
from pyowletapi.sock import Sock
|
|
|
|
from homeassistant.const import CONF_EMAIL
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.helpers.entity import DeviceInfo
|
|
from homeassistant.helpers.update_coordinator import (
|
|
ConfigEntryAuthFailed,
|
|
DataUpdateCoordinator,
|
|
UpdateFailed,
|
|
)
|
|
|
|
from .const import DOMAIN, MANUFACTURER
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
class OwletCoordinator(DataUpdateCoordinator):
|
|
"""Coordinator is responsible for querying the device at a specified route."""
|
|
|
|
def __init__(self, hass: HomeAssistant, sock: Sock, interval: int) -> None:
|
|
"""Initialise a custom coordinator."""
|
|
super().__init__(
|
|
hass,
|
|
_LOGGER,
|
|
name=DOMAIN,
|
|
update_interval=timedelta(seconds=interval),
|
|
)
|
|
assert self.config_entry is not None
|
|
self._device_unique_id = sock.serial
|
|
self._model = sock.model
|
|
self._sw_version = sock.sw_version
|
|
self._hw_version = sock.version
|
|
self.sock = sock
|
|
self.device_info = DeviceInfo(
|
|
identifiers={(DOMAIN, self._device_unique_id)},
|
|
name="Owlet Baby Care Sock",
|
|
manufacturer=MANUFACTURER,
|
|
model=self._model,
|
|
sw_version=self._sw_version,
|
|
hw_version=self._hw_version,
|
|
)
|
|
|
|
async def _async_update_data(self) -> None:
|
|
"""Fetch the data from the device."""
|
|
try:
|
|
properties = await self.sock.update_properties()
|
|
if properties["tokens"]:
|
|
self.hass.config_entries.async_update_entry(
|
|
self.config_entry,
|
|
data={**self.config_entry.data, **properties["tokens"]},
|
|
)
|
|
except OwletAuthenticationError as err:
|
|
raise ConfigEntryAuthFailed(
|
|
f"Authentication failed for {self.config_entry.data[CONF_EMAIL]}"
|
|
) from err
|
|
except (OwletError, OwletConnectionError) as err:
|
|
raise UpdateFailed(err) from err
|