Skip to content

Commit

Permalink
Migrate august to use yalexs 5.2.0 (#119178)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco committed Jun 9, 2024
1 parent ff493a8 commit d9f1d40
Show file tree
Hide file tree
Showing 13 changed files with 65 additions and 535 deletions.
60 changes: 32 additions & 28 deletions homeassistant/components/august/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,37 @@
from datetime import datetime
from itertools import chain
import logging
from typing import Any
from typing import Any, cast

from aiohttp import ClientError, ClientResponseError
from path import Path
from yalexs.activity import ActivityTypes
from yalexs.const import DEFAULT_BRAND
from yalexs.doorbell import Doorbell, DoorbellDetail
from yalexs.exceptions import AugustApiAIOHTTPError
from yalexs.lock import Lock, LockDetail
from yalexs.manager.activity import ActivityStream
from yalexs.manager.const import CONF_BRAND
from yalexs.manager.exceptions import CannotConnect, InvalidAuth, RequireValidation
from yalexs.manager.gateway import Config as YaleXSConfig
from yalexs.manager.subscriber import SubscriberMixin
from yalexs.pubnub_activity import activities_from_pubnub_message
from yalexs.pubnub_async import AugustPubNub, async_create_pubnub
from yalexs_ble import YaleXSBLEDiscovery

from homeassistant.config_entries import SOURCE_INTEGRATION_DISCOVERY, ConfigEntry
from homeassistant.const import CONF_PASSWORD
from homeassistant.core import CALLBACK_TYPE, HomeAssistant, callback
from homeassistant.const import CONF_PASSWORD, EVENT_HOMEASSISTANT_STOP
from homeassistant.core import Event, HomeAssistant, callback
from homeassistant.exceptions import (
ConfigEntryAuthFailed,
ConfigEntryNotReady,
HomeAssistantError,
)
from homeassistant.helpers import device_registry as dr, discovery_flow
from homeassistant.util.async_ import create_eager_task

from .activity import ActivityStream
from .const import CONF_BRAND, DOMAIN, MIN_TIME_BETWEEN_DETAIL_UPDATES, PLATFORMS
from .exceptions import CannotConnect, InvalidAuth, RequireValidation
from .const import DOMAIN, MIN_TIME_BETWEEN_DETAIL_UPDATES, PLATFORMS
from .gateway import AugustGateway
from .subscriber import AugustSubscriberMixin
from .util import async_create_august_clientsession

_LOGGER = logging.getLogger(__name__)
Expand All @@ -52,10 +56,8 @@
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up August from a config entry."""
session = async_create_august_clientsession(hass)
august_gateway = AugustGateway(hass, session)

august_gateway = AugustGateway(Path(hass.config.config_dir), session)
try:
await august_gateway.async_setup(entry.data)
return await async_setup_august(hass, entry, august_gateway)
except (RequireValidation, InvalidAuth) as err:
raise ConfigEntryAuthFailed from err
Expand All @@ -67,14 +69,15 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:

async def async_unload_entry(hass: HomeAssistant, entry: AugustConfigEntry) -> bool:
"""Unload a config entry."""
entry.runtime_data.async_stop()
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)


async def async_setup_august(
hass: HomeAssistant, config_entry: AugustConfigEntry, august_gateway: AugustGateway
) -> bool:
"""Set up the August component."""
config = cast(YaleXSConfig, config_entry.data)
await august_gateway.async_setup(config)

if CONF_PASSWORD in config_entry.data:
# We no longer need to store passwords since we do not
Expand Down Expand Up @@ -116,7 +119,7 @@ def _async_trigger_ble_lock_discovery(
)


class AugustData(AugustSubscriberMixin):
class AugustData(SubscriberMixin):
"""August data object."""

def __init__(
Expand All @@ -126,17 +129,17 @@ def __init__(
august_gateway: AugustGateway,
) -> None:
"""Init August data object."""
super().__init__(hass, MIN_TIME_BETWEEN_DETAIL_UPDATES)
super().__init__(MIN_TIME_BETWEEN_DETAIL_UPDATES)
self._config_entry = config_entry
self._hass = hass
self._august_gateway = august_gateway
self.activity_stream: ActivityStream = None # type: ignore[assignment]
self.activity_stream: ActivityStream = None
self._api = august_gateway.api
self._device_detail_by_id: dict[str, LockDetail | DoorbellDetail] = {}
self._doorbells_by_id: dict[str, Doorbell] = {}
self._locks_by_id: dict[str, Lock] = {}
self._house_ids: set[str] = set()
self._pubnub_unsub: CALLBACK_TYPE | None = None
self._pubnub_unsub: Callable[[], Coroutine[Any, Any, None]] | None = None

@property
def brand(self) -> str:
Expand All @@ -148,13 +151,8 @@ async def async_setup(self) -> None:
token = self._august_gateway.access_token
# This used to be a gather but it was less reliable with august's recent api changes.
user_data = await self._api.async_get_user(token)
locks: list[Lock] = await self._api.async_get_operable_locks(token)
doorbells: list[Doorbell] = await self._api.async_get_doorbells(token)
if not doorbells:
doorbells = []
if not locks:
locks = []

locks: list[Lock] = await self._api.async_get_operable_locks(token) or []
doorbells: list[Doorbell] = await self._api.async_get_doorbells(token) or []
self._doorbells_by_id = {device.device_id: device for device in doorbells}
self._locks_by_id = {device.device_id: device for device in locks}
self._house_ids = {device.house_id for device in chain(locks, doorbells)}
Expand All @@ -175,9 +173,14 @@ async def async_setup(self) -> None:
pubnub.register_device(device)

self.activity_stream = ActivityStream(
self._hass, self._api, self._august_gateway, self._house_ids, pubnub
self._api, self._august_gateway, self._house_ids, pubnub
)
self._config_entry.async_on_unload(
self._hass.bus.async_listen(EVENT_HOMEASSISTANT_STOP, self.async_stop)
)
self._config_entry.async_on_unload(self.async_stop)
await self.activity_stream.async_setup()

pubnub.subscribe(self.async_pubnub_message)
self._pubnub_unsub = async_create_pubnub(
user_data["UserID"],
Expand All @@ -200,8 +203,10 @@ async def _async_initial_sync(self) -> None:
# awake when they come back online
for result in await asyncio.gather(
*[
self.async_status_async(
device_id, bool(detail.bridge and detail.bridge.hyper_bridge)
create_eager_task(
self.async_status_async(
device_id, bool(detail.bridge and detail.bridge.hyper_bridge)
)
)
for device_id, detail in self._device_detail_by_id.items()
if device_id in self._locks_by_id
Expand Down Expand Up @@ -231,11 +236,10 @@ def async_pubnub_message(
self.async_signal_device_id_update(device.device_id)
activity_stream.async_schedule_house_id_refresh(device.house_id)

@callback
def async_stop(self) -> None:
async def async_stop(self, event: Event | None = None) -> None:
"""Stop the subscriptions."""
if self._pubnub_unsub:
self._pubnub_unsub()
await self._pubnub_unsub()
self.activity_stream.async_stop()

@property
Expand Down
231 changes: 0 additions & 231 deletions homeassistant/components/august/activity.py

This file was deleted.

Loading

0 comments on commit d9f1d40

Please sign in to comment.