Skip to content

Commit

Permalink
Initial cleanup for Aladdin connect (#118777)
Browse files Browse the repository at this point in the history
  • Loading branch information
joostlek authored and frenck committed Jun 4, 2024
1 parent 954e8ff commit c76b7a4
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 45 deletions.
44 changes: 23 additions & 21 deletions homeassistant/components/aladdin_connect/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,49 +5,51 @@
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers import aiohttp_client, config_entry_oauth2_flow
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.config_entry_oauth2_flow import (
OAuth2Session,
async_get_config_entry_implementation,
)

from . import api
from .const import CONFIG_FLOW_MINOR_VERSION, CONFIG_FLOW_VERSION
from .api import AsyncConfigEntryAuth

PLATFORMS: list[Platform] = [Platform.COVER]

type AladdinConnectConfigEntry = ConfigEntry[AsyncConfigEntryAuth]

async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:

async def async_setup_entry(
hass: HomeAssistant, entry: AladdinConnectConfigEntry
) -> bool:
"""Set up Aladdin Connect Genie from a config entry."""
implementation = (
await config_entry_oauth2_flow.async_get_config_entry_implementation(
hass, entry
)
)
implementation = await async_get_config_entry_implementation(hass, entry)

session = config_entry_oauth2_flow.OAuth2Session(hass, entry, implementation)
session = OAuth2Session(hass, entry, implementation)

# If using an aiohttp-based API lib
entry.runtime_data = api.AsyncConfigEntryAuth(
aiohttp_client.async_get_clientsession(hass), session
)
entry.runtime_data = AsyncConfigEntryAuth(async_get_clientsession(hass), session)

await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)

return True


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


async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
async def async_migrate_entry(
hass: HomeAssistant, config_entry: AladdinConnectConfigEntry
) -> bool:
"""Migrate old config."""
if config_entry.version < CONFIG_FLOW_VERSION:
if config_entry.version < 2:
config_entry.async_start_reauth(hass)
new_data = {**config_entry.data}
hass.config_entries.async_update_entry(
config_entry,
data=new_data,
version=CONFIG_FLOW_VERSION,
minor_version=CONFIG_FLOW_MINOR_VERSION,
version=2,
minor_version=1,
)

return True
11 changes: 6 additions & 5 deletions homeassistant/components/aladdin_connect/api.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
"""API for Aladdin Connect Genie bound to Home Assistant OAuth."""

from typing import cast

from aiohttp import ClientSession
from genie_partner_sdk.auth import Auth

from homeassistant.helpers import config_entry_oauth2_flow
from homeassistant.helpers.config_entry_oauth2_flow import OAuth2Session

API_URL = "https://twdvzuefzh.execute-api.us-east-2.amazonaws.com/v1"
API_KEY = "k6QaiQmcTm2zfaNns5L1Z8duBtJmhDOW8JawlCC3"
Expand All @@ -15,7 +17,7 @@ class AsyncConfigEntryAuth(Auth): # type: ignore[misc]
def __init__(
self,
websession: ClientSession,
oauth_session: config_entry_oauth2_flow.OAuth2Session,
oauth_session: OAuth2Session,
) -> None:
"""Initialize Aladdin Connect Genie auth."""
super().__init__(
Expand All @@ -25,7 +27,6 @@ def __init__(

async def async_get_access_token(self) -> str:
"""Return a valid access token."""
if not self._oauth_session.valid_token:
await self._oauth_session.async_ensure_token_valid()
await self._oauth_session.async_ensure_token_valid()

return str(self._oauth_session.token["access_token"])
return cast(str, self._oauth_session.token["access_token"])
14 changes: 6 additions & 8 deletions homeassistant/components/aladdin_connect/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,17 @@
import voluptuous as vol

from homeassistant.config_entries import ConfigEntry, ConfigFlowResult
from homeassistant.helpers import config_entry_oauth2_flow
from homeassistant.helpers.config_entry_oauth2_flow import AbstractOAuth2FlowHandler

from .const import CONFIG_FLOW_MINOR_VERSION, CONFIG_FLOW_VERSION, DOMAIN
from .const import DOMAIN


class OAuth2FlowHandler(
config_entry_oauth2_flow.AbstractOAuth2FlowHandler, domain=DOMAIN
):
class AladdinConnectOAuth2FlowHandler(AbstractOAuth2FlowHandler, domain=DOMAIN):
"""Config flow to handle Aladdin Connect Genie OAuth2 authentication."""

DOMAIN = DOMAIN
VERSION = CONFIG_FLOW_VERSION
MINOR_VERSION = CONFIG_FLOW_MINOR_VERSION
VERSION = 2
MINOR_VERSION = 1

reauth_entry: ConfigEntry | None = None

Expand All @@ -43,7 +41,7 @@ async def async_step_reauth_confirm(
)
return await self.async_step_user()

async def async_oauth_create_entry(self, data: dict) -> ConfigFlowResult:
async def async_oauth_create_entry(self, data: dict[str, Any]) -> ConfigFlowResult:
"""Create an oauth config entry or update existing entry for reauth."""
if self.reauth_entry:
return self.async_update_reload_and_abort(
Expand Down
8 changes: 0 additions & 8 deletions homeassistant/components/aladdin_connect/const.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
"""Constants for the Aladdin Connect Genie integration."""

from typing import Final

from homeassistant.components.cover import CoverEntityFeature

DOMAIN = "aladdin_connect"
CONFIG_FLOW_VERSION = 2
CONFIG_FLOW_MINOR_VERSION = 1

OAUTH2_AUTHORIZE = "https://app.aladdinconnect.com/login.html"
OAUTH2_TOKEN = "https://twdvzuefzh.execute-api.us-east-2.amazonaws.com/v1/oauth2/token"

SUPPORTED_FEATURES: Final = CoverEntityFeature.OPEN | CoverEntityFeature.CLOSE
10 changes: 7 additions & 3 deletions homeassistant/components/aladdin_connect/cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@

from genie_partner_sdk.client import AladdinConnectClient

from homeassistant.components.cover import CoverDeviceClass, CoverEntity
from homeassistant.components.cover import (
CoverDeviceClass,
CoverEntity,
CoverEntityFeature,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import PlatformNotReady
Expand All @@ -14,7 +18,7 @@
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from . import api
from .const import DOMAIN, SUPPORTED_FEATURES
from .const import DOMAIN
from .model import GarageDoor

SCAN_INTERVAL = timedelta(seconds=15)
Expand Down Expand Up @@ -75,7 +79,7 @@ class AladdinDevice(CoverEntity):
"""Representation of Aladdin Connect cover."""

_attr_device_class = CoverDeviceClass.GARAGE
_attr_supported_features = SUPPORTED_FEATURES
_attr_supported_features = CoverEntityFeature.OPEN | CoverEntityFeature.CLOSE
_attr_has_entity_name = True
_attr_name = None

Expand Down

0 comments on commit c76b7a4

Please sign in to comment.