Initial cleanup for Aladdin connect (#118777)

This commit is contained in:
Joost Lekkerkerker 2024-06-04 10:13:31 +02:00 committed by Franck Nijhof
parent 954e8ff9b3
commit c76b7a48d3
No known key found for this signature in database
GPG Key ID: D62583BA8AB11CA3
5 changed files with 42 additions and 45 deletions

View File

@ -5,49 +5,51 @@ from __future__ import annotations
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import Platform from homeassistant.const import Platform
from homeassistant.core import HomeAssistant 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 .api import AsyncConfigEntryAuth
from .const import CONFIG_FLOW_MINOR_VERSION, CONFIG_FLOW_VERSION
PLATFORMS: list[Platform] = [Platform.COVER] 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.""" """Set up Aladdin Connect Genie from a config entry."""
implementation = ( implementation = await async_get_config_entry_implementation(hass, entry)
await config_entry_oauth2_flow.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 = AsyncConfigEntryAuth(async_get_clientsession(hass), session)
entry.runtime_data = api.AsyncConfigEntryAuth(
aiohttp_client.async_get_clientsession(hass), session
)
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
return True 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.""" """Unload a config entry."""
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS) 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.""" """Migrate old config."""
if config_entry.version < CONFIG_FLOW_VERSION: if config_entry.version < 2:
config_entry.async_start_reauth(hass) config_entry.async_start_reauth(hass)
new_data = {**config_entry.data}
hass.config_entries.async_update_entry( hass.config_entries.async_update_entry(
config_entry, config_entry,
data=new_data, version=2,
version=CONFIG_FLOW_VERSION, minor_version=1,
minor_version=CONFIG_FLOW_MINOR_VERSION,
) )
return True return True

View File

@ -1,9 +1,11 @@
"""API for Aladdin Connect Genie bound to Home Assistant OAuth.""" """API for Aladdin Connect Genie bound to Home Assistant OAuth."""
from typing import cast
from aiohttp import ClientSession from aiohttp import ClientSession
from genie_partner_sdk.auth import Auth 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_URL = "https://twdvzuefzh.execute-api.us-east-2.amazonaws.com/v1"
API_KEY = "k6QaiQmcTm2zfaNns5L1Z8duBtJmhDOW8JawlCC3" API_KEY = "k6QaiQmcTm2zfaNns5L1Z8duBtJmhDOW8JawlCC3"
@ -15,7 +17,7 @@ class AsyncConfigEntryAuth(Auth): # type: ignore[misc]
def __init__( def __init__(
self, self,
websession: ClientSession, websession: ClientSession,
oauth_session: config_entry_oauth2_flow.OAuth2Session, oauth_session: OAuth2Session,
) -> None: ) -> None:
"""Initialize Aladdin Connect Genie auth.""" """Initialize Aladdin Connect Genie auth."""
super().__init__( super().__init__(
@ -25,7 +27,6 @@ class AsyncConfigEntryAuth(Auth): # type: ignore[misc]
async def async_get_access_token(self) -> str: async def async_get_access_token(self) -> str:
"""Return a valid access token.""" """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"])

View File

@ -7,19 +7,17 @@ from typing import Any
import voluptuous as vol import voluptuous as vol
from homeassistant.config_entries import ConfigEntry, ConfigFlowResult 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( class AladdinConnectOAuth2FlowHandler(AbstractOAuth2FlowHandler, domain=DOMAIN):
config_entry_oauth2_flow.AbstractOAuth2FlowHandler, domain=DOMAIN
):
"""Config flow to handle Aladdin Connect Genie OAuth2 authentication.""" """Config flow to handle Aladdin Connect Genie OAuth2 authentication."""
DOMAIN = DOMAIN DOMAIN = DOMAIN
VERSION = CONFIG_FLOW_VERSION VERSION = 2
MINOR_VERSION = CONFIG_FLOW_MINOR_VERSION MINOR_VERSION = 1
reauth_entry: ConfigEntry | None = None reauth_entry: ConfigEntry | None = None
@ -43,7 +41,7 @@ class OAuth2FlowHandler(
) )
return await self.async_step_user() 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.""" """Create an oauth config entry or update existing entry for reauth."""
if self.reauth_entry: if self.reauth_entry:
return self.async_update_reload_and_abort( return self.async_update_reload_and_abort(

View File

@ -1,14 +1,6 @@
"""Constants for the Aladdin Connect Genie integration.""" """Constants for the Aladdin Connect Genie integration."""
from typing import Final
from homeassistant.components.cover import CoverEntityFeature
DOMAIN = "aladdin_connect" DOMAIN = "aladdin_connect"
CONFIG_FLOW_VERSION = 2
CONFIG_FLOW_MINOR_VERSION = 1
OAUTH2_AUTHORIZE = "https://app.aladdinconnect.com/login.html" OAUTH2_AUTHORIZE = "https://app.aladdinconnect.com/login.html"
OAUTH2_TOKEN = "https://twdvzuefzh.execute-api.us-east-2.amazonaws.com/v1/oauth2/token" OAUTH2_TOKEN = "https://twdvzuefzh.execute-api.us-east-2.amazonaws.com/v1/oauth2/token"
SUPPORTED_FEATURES: Final = CoverEntityFeature.OPEN | CoverEntityFeature.CLOSE

View File

@ -5,7 +5,11 @@ from typing import Any
from genie_partner_sdk.client import AladdinConnectClient 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.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.exceptions import PlatformNotReady from homeassistant.exceptions import PlatformNotReady
@ -14,7 +18,7 @@ from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import api from . import api
from .const import DOMAIN, SUPPORTED_FEATURES from .const import DOMAIN
from .model import GarageDoor from .model import GarageDoor
SCAN_INTERVAL = timedelta(seconds=15) SCAN_INTERVAL = timedelta(seconds=15)
@ -75,7 +79,7 @@ class AladdinDevice(CoverEntity):
"""Representation of Aladdin Connect cover.""" """Representation of Aladdin Connect cover."""
_attr_device_class = CoverDeviceClass.GARAGE _attr_device_class = CoverDeviceClass.GARAGE
_attr_supported_features = SUPPORTED_FEATURES _attr_supported_features = CoverEntityFeature.OPEN | CoverEntityFeature.CLOSE
_attr_has_entity_name = True _attr_has_entity_name = True
_attr_name = None _attr_name = None