Created
October 16, 2019 21:04
Config flow to link Home Assistant with Home Assistant
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
"""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