-
-
Notifications
You must be signed in to change notification settings - Fork 37.2k
Add Homecast integration #166675
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
robjampar
wants to merge
1
commit into
home-assistant:dev
Choose a base branch
from
robjampar:add-homecast-integration
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add Homecast integration #166675
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| """The Homecast integration.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from dataclasses import dataclass | ||
| import logging | ||
|
|
||
| from pyhomecast import HomecastClient, HomecastWebSocket | ||
|
|
||
| from homeassistant.components.application_credentials import AuthorizationServer | ||
| from homeassistant.config_entries import ConfigEntry | ||
| from homeassistant.const import CONF_ACCESS_TOKEN, Platform | ||
| from homeassistant.core import HomeAssistant | ||
| from homeassistant.exceptions import ( | ||
| ConfigEntryAuthFailed, | ||
| ConfigEntryNotReady, | ||
| OAuth2TokenRequestError, | ||
| OAuth2TokenRequestReauthError, | ||
| ) | ||
| from homeassistant.helpers.aiohttp_client import async_get_clientsession | ||
| from homeassistant.helpers.config_entry_oauth2_flow import ( | ||
| OAuth2Session, | ||
| async_get_config_entry_implementation, | ||
| ) | ||
|
|
||
| from .application_credentials import authorization_server_context | ||
| from .const import ( | ||
| API_BASE_URL, | ||
| CONF_API_URL, | ||
| CONF_MODE, | ||
| CONF_OAUTH_AUTHORIZE_URL, | ||
| CONF_OAUTH_TOKEN_URL, | ||
| DOMAIN as DOMAIN, | ||
| MODE_COMMUNITY, | ||
| OAUTH_AUTHORIZE_URL, | ||
| OAUTH_TOKEN_URL, | ||
| ) | ||
| from .coordinator import HomecastCoordinator | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
| PLATFORMS = [ | ||
| Platform.LIGHT, | ||
| ] | ||
|
|
||
|
|
||
| @dataclass | ||
| class HomecastData: | ||
| """Runtime data for a Homecast config entry.""" | ||
|
|
||
| coordinator: HomecastCoordinator | ||
| client: HomecastClient | ||
|
|
||
|
|
||
| type HomecastConfigEntry = ConfigEntry[HomecastData] | ||
|
|
||
|
|
||
| async def async_setup_entry(hass: HomeAssistant, entry: HomecastConfigEntry) -> bool: | ||
| """Set up Homecast from a config entry.""" | ||
| mode = entry.data.get(CONF_MODE) | ||
| api_url = entry.data.get(CONF_API_URL, API_BASE_URL) | ||
|
|
||
| authorize_url = entry.data.get(CONF_OAUTH_AUTHORIZE_URL, OAUTH_AUTHORIZE_URL) | ||
| token_url = entry.data.get(CONF_OAUTH_TOKEN_URL, OAUTH_TOKEN_URL) | ||
|
|
||
| with authorization_server_context( | ||
| AuthorizationServer(authorize_url=authorize_url, token_url=token_url) | ||
| ): | ||
| implementation = await async_get_config_entry_implementation(hass, entry) | ||
|
|
||
| session = OAuth2Session(hass, entry, implementation) | ||
|
|
||
| try: | ||
| await session.async_ensure_token_valid() | ||
| except OAuth2TokenRequestReauthError as err: | ||
| raise ConfigEntryAuthFailed from err | ||
| except OAuth2TokenRequestError as err: | ||
| raise ConfigEntryNotReady from err | ||
|
|
||
| http_session = async_get_clientsession(hass) | ||
| client = HomecastClient(session=http_session, api_url=api_url) | ||
| client.authenticate(session.token[CONF_ACCESS_TOKEN]) | ||
|
|
||
| device_id = f"ha_{entry.entry_id[:12]}" | ||
| ws = HomecastWebSocket( | ||
| session=http_session, | ||
| api_url=api_url, | ||
| device_id=device_id, | ||
| community=(mode == MODE_COMMUNITY), | ||
| ) | ||
|
|
||
| async def _refresh_token() -> str: | ||
| """Refresh the OAuth token and return the new access token.""" | ||
| await session.async_ensure_token_valid() | ||
| token = session.token[CONF_ACCESS_TOKEN] | ||
| client.authenticate(token) | ||
| if ws: | ||
| ws.set_token(token) | ||
| return token | ||
|
|
||
| coordinator = HomecastCoordinator( | ||
| hass, | ||
| entry, | ||
| client, | ||
| _refresh_token, | ||
| ws=ws, | ||
| initial_token=session.token[CONF_ACCESS_TOKEN], | ||
| ) | ||
|
|
||
| await coordinator.async_config_entry_first_refresh() | ||
|
|
||
| # Start WebSocket after initial state is available | ||
robjampar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| await coordinator.async_setup_websocket() | ||
|
|
||
| entry.runtime_data = HomecastData(coordinator=coordinator, client=client) | ||
| await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) | ||
| return True | ||
|
|
||
|
|
||
| async def async_unload_entry(hass: HomeAssistant, entry: HomecastConfigEntry) -> bool: | ||
| """Unload a Homecast config entry.""" | ||
| return await hass.config_entries.async_unload_platforms(entry, PLATFORMS) | ||
76 changes: 76 additions & 0 deletions
76
homeassistant/components/homecast/application_credentials.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| """OAuth application credentials for Homecast.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from contextlib import contextmanager | ||
| import contextvars | ||
|
|
||
| from homeassistant.components.application_credentials import ( | ||
| AuthorizationServer, | ||
| ClientCredential, | ||
| ) | ||
| from homeassistant.core import HomeAssistant | ||
| from homeassistant.helpers.config_entry_oauth2_flow import ( | ||
| LocalOAuth2ImplementationWithPkce, | ||
| ) | ||
|
|
||
| from .const import OAUTH_AUTHORIZE_URL, OAUTH_TOKEN_URL, SCOPES | ||
|
|
||
| # Context variable for dynamic OAuth server URLs (community mode). | ||
| # Set before calling into AbstractOAuth2FlowHandler so application_credentials | ||
| # resolves to the correct server (local community or cloud). | ||
| _server_context: contextvars.ContextVar[AuthorizationServer | None] = ( | ||
| contextvars.ContextVar("homecast_authorization_server", default=None) | ||
| ) | ||
|
|
||
|
|
||
| @contextmanager | ||
| def authorization_server_context(server: AuthorizationServer): | ||
| """Temporarily override the authorization server URLs.""" | ||
| token = _server_context.set(server) | ||
| try: | ||
| yield | ||
| finally: | ||
| _server_context.reset(token) | ||
|
|
||
|
|
||
| async def async_get_authorization_server(hass: HomeAssistant) -> AuthorizationServer: | ||
| """Return the Homecast authorization server. | ||
|
|
||
| Uses the context variable if set (community mode), otherwise cloud defaults. | ||
| """ | ||
| if (server := _server_context.get()) is not None: | ||
| return server | ||
| return AuthorizationServer( | ||
| authorize_url=OAUTH_AUTHORIZE_URL, | ||
| token_url=OAUTH_TOKEN_URL, | ||
| ) | ||
|
|
||
|
|
||
| async def async_get_auth_implementation( | ||
| hass: HomeAssistant, auth_domain: str, credential: ClientCredential | ||
| ) -> HomecastOAuth2Implementation: | ||
| """Return a custom auth implementation with PKCE.""" | ||
| server = _server_context.get() | ||
| authorize_url = server.authorize_url if server else OAUTH_AUTHORIZE_URL | ||
| token_url = server.token_url if server else OAUTH_TOKEN_URL | ||
|
|
||
| return HomecastOAuth2Implementation( | ||
| hass, | ||
| auth_domain, | ||
| credential.client_id, | ||
| authorize_url, | ||
| token_url, | ||
| credential.client_secret, | ||
| ) | ||
|
|
||
|
|
||
| class HomecastOAuth2Implementation(LocalOAuth2ImplementationWithPkce): | ||
| """Homecast OAuth2 implementation with PKCE (S256).""" | ||
|
|
||
| @property | ||
| def extra_authorize_data(self) -> dict: | ||
| """Extra data that needs to be appended to the authorize url.""" | ||
| return super().extra_authorize_data | { | ||
| "scope": SCOPES, | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.