Skip to content

Instantly share code, notes, and snippets.

@balloob
Created October 16, 2019 21:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save balloob/e2abc99f0036eed4f308f19d36be9ac9 to your computer and use it in GitHub Desktop.
Save balloob/e2abc99f0036eed4f308f19d36be9ac9 to your computer and use it in GitHub Desktop.
Config flow to link Home Assistant with Home Assistant
"""Config flow to connect with Home Assistant."""
import logging
import voluptuous as vol
from homeassistant.helpers import config_entry_oauth2_flow
from .const import DOMAIN
class HassConfigFlow(config_entry_oauth2_flow.AbstractOAuth2FlowHandler, domain=DOMAIN):
"""Implementation of the Hass OAuth2 config flow."""
DOMAIN = DOMAIN
@property
def logger(self) -> logging.Logger:
"""Return logger."""
return logging.getLogger(__name__)
def __init__(self):
"""Initialize the Hass config flow."""
super().__init__()
self.hass_url = None
async def async_step_user(self, user_input=None):
"""Handle a flow started by a user."""
if user_input:
self.hass_url = user_input["hass_url"]
HassConfigFlow.register_local_implementation(
self.hass,
config_entry_oauth2_flow.LocalOAuth2Implementation(
self.hass,
DOMAIN,
self.hass.config.api.base_url,
None,
f"{self.hass_url}/auth/authorize",
f"{self.hass_url}/auth/token",
),
)
return await self.async_step_pick_implementation()
return self.async_show_form(
step_id="user", data_schema=vol.Schema({"hass_url": str})
)
async def async_oauth_create_entry(self, data):
"""Create an entry for the flow.
Ok to override if you want to provide extra info.
"""
# Fetch config of other HASS instance
resp = await config_entry_oauth2_flow.async_oauth2_request(
self.hass, data["token"], "get", f"{self.hass_url}/api/config"
)
config = await resp.json()
# Store client ID so auth keeps working if we change base url
data["client_id"] = self.hass.config.api.base_url
data["hass_url"] = self.hass_url
return self.async_create_entry(title=config["location_name"], data=data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment