Import configurator (#64211)
Co-authored-by: epenet <epenet@users.noreply.github.com>pull/64374/head
parent
9fd704cabf
commit
81461832c3
|
@ -8,6 +8,7 @@ the user has submitted configuration information.
|
|||
"""
|
||||
from contextlib import suppress
|
||||
import functools as ft
|
||||
from typing import Any
|
||||
|
||||
from homeassistant.const import (
|
||||
ATTR_ENTITY_PICTURE,
|
||||
|
@ -60,7 +61,7 @@ def async_request_config(
|
|||
link_name=None,
|
||||
link_url=None,
|
||||
entity_picture=None,
|
||||
):
|
||||
) -> str:
|
||||
"""Create a new request for configuration.
|
||||
|
||||
Will return an ID to be used for sequent calls.
|
||||
|
@ -87,7 +88,7 @@ def async_request_config(
|
|||
|
||||
|
||||
@bind_hass
|
||||
def request_config(hass, *args, **kwargs):
|
||||
def request_config(hass: HomeAssistant, *args: Any, **kwargs: Any) -> str:
|
||||
"""Create a new request for configuration.
|
||||
|
||||
Will return an ID to be used for sequent calls.
|
||||
|
@ -106,7 +107,7 @@ def async_notify_errors(hass, request_id, error):
|
|||
|
||||
|
||||
@bind_hass
|
||||
def notify_errors(hass, request_id, error):
|
||||
def notify_errors(hass: HomeAssistant, request_id: str, error: str) -> None:
|
||||
"""Add errors to a config request."""
|
||||
return run_callback_threadsafe(
|
||||
hass.loop, async_notify_errors, hass, request_id, error
|
||||
|
@ -115,14 +116,14 @@ def notify_errors(hass, request_id, error):
|
|||
|
||||
@bind_hass
|
||||
@async_callback
|
||||
def async_request_done(hass, request_id):
|
||||
def async_request_done(hass: HomeAssistant, request_id: str) -> None:
|
||||
"""Mark a configuration request as done."""
|
||||
with suppress(KeyError): # If request_id does not exist
|
||||
hass.data[DATA_REQUESTS].pop(request_id).async_request_done(request_id)
|
||||
|
||||
|
||||
@bind_hass
|
||||
def request_done(hass, request_id):
|
||||
def request_done(hass: HomeAssistant, request_id: str) -> None:
|
||||
"""Mark a configuration request as done."""
|
||||
return run_callback_threadsafe(
|
||||
hass.loop, async_request_done, hass, request_id
|
||||
|
@ -149,7 +150,7 @@ class Configurator:
|
|||
@async_callback
|
||||
def async_request_config(
|
||||
self, name, callback, description, submit_caption, fields, entity_picture
|
||||
):
|
||||
) -> str:
|
||||
"""Set up a request for configuration."""
|
||||
entity_id = async_generate_entity_id(ENTITY_ID_FORMAT, name, hass=self.hass)
|
||||
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
"""Support for the Fitbit API."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import datetime
|
||||
|
@ -14,6 +13,7 @@ from fitbit.api import FitbitOauth2Client
|
|||
from oauthlib.oauth2.rfc6749.errors import MismatchingStateError, MissingTokenError
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components import configurator
|
||||
from homeassistant.components.http import HomeAssistantView
|
||||
from homeassistant.components.sensor import (
|
||||
PLATFORM_SCHEMA as PARENT_PLATFORM_SCHEMA,
|
||||
|
@ -83,7 +83,6 @@ def request_app_setup(
|
|||
discovery_info: DiscoveryInfoType | None = None,
|
||||
) -> None:
|
||||
"""Assist user with configuring the Fitbit dev application."""
|
||||
configurator = hass.components.configurator
|
||||
|
||||
def fitbit_configuration_callback(fields: list[dict[str, str]]) -> None:
|
||||
"""Handle configuration updates."""
|
||||
|
@ -91,11 +90,9 @@ def request_app_setup(
|
|||
if os.path.isfile(config_path):
|
||||
config_file = load_json(config_path)
|
||||
if config_file == DEFAULT_CONFIG:
|
||||
error_msg = (
|
||||
"You didn't correctly modify fitbit.conf",
|
||||
" please try again",
|
||||
)
|
||||
configurator.notify_errors(_CONFIGURING["fitbit"], error_msg)
|
||||
error_msg = "You didn't correctly modify fitbit.conf, please try again."
|
||||
|
||||
configurator.notify_errors(hass, _CONFIGURING["fitbit"], error_msg)
|
||||
else:
|
||||
setup_platform(hass, config, add_entities, discovery_info)
|
||||
else:
|
||||
|
@ -121,6 +118,7 @@ def request_app_setup(
|
|||
submit = "I have saved my Client ID and Client Secret into fitbit.conf."
|
||||
|
||||
_CONFIGURING["fitbit"] = configurator.request_config(
|
||||
hass,
|
||||
"Fitbit",
|
||||
fitbit_configuration_callback,
|
||||
description=description,
|
||||
|
@ -131,10 +129,9 @@ def request_app_setup(
|
|||
|
||||
def request_oauth_completion(hass: HomeAssistant) -> None:
|
||||
"""Request user complete Fitbit OAuth2 flow."""
|
||||
configurator = hass.components.configurator
|
||||
if "fitbit" in _CONFIGURING:
|
||||
configurator.notify_errors(
|
||||
_CONFIGURING["fitbit"], "Failed to register, please try again."
|
||||
hass, _CONFIGURING["fitbit"], "Failed to register, please try again."
|
||||
)
|
||||
|
||||
return
|
||||
|
@ -147,6 +144,7 @@ def request_oauth_completion(hass: HomeAssistant) -> None:
|
|||
description = f"Please authorize Fitbit by visiting {start_url}"
|
||||
|
||||
_CONFIGURING["fitbit"] = configurator.request_config(
|
||||
hass,
|
||||
"Fitbit",
|
||||
fitbit_configuration_callback,
|
||||
description=description,
|
||||
|
@ -175,7 +173,7 @@ def setup_platform(
|
|||
return
|
||||
|
||||
if "fitbit" in _CONFIGURING:
|
||||
hass.components.configurator.request_done(_CONFIGURING.pop("fitbit"))
|
||||
configurator.request_done(hass, _CONFIGURING.pop("fitbit"))
|
||||
|
||||
access_token: str | None = config_file.get(ATTR_ACCESS_TOKEN)
|
||||
refresh_token: str | None = config_file.get(ATTR_REFRESH_TOKEN)
|
||||
|
|
|
@ -10,6 +10,7 @@ from typing import Any
|
|||
import voluptuous as vol
|
||||
from websocket import _exceptions, create_connection
|
||||
|
||||
from homeassistant.components import configurator
|
||||
from homeassistant.components.media_player import PLATFORM_SCHEMA, MediaPlayerEntity
|
||||
from homeassistant.components.media_player.const import (
|
||||
MEDIA_TYPE_MUSIC,
|
||||
|
@ -65,10 +66,9 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|||
|
||||
def request_configuration(hass, config, url, add_entities_callback):
|
||||
"""Request configuration steps from the user."""
|
||||
configurator = hass.components.configurator
|
||||
if "gpmdp" in _CONFIGURING:
|
||||
configurator.notify_errors(
|
||||
_CONFIGURING["gpmdp"], "Failed to register, please try again."
|
||||
hass, _CONFIGURING["gpmdp"], "Failed to register, please try again."
|
||||
)
|
||||
|
||||
return
|
||||
|
@ -152,8 +152,7 @@ def setup_gpmdp(hass, config, code, add_entities):
|
|||
return
|
||||
|
||||
if "gpmdp" in _CONFIGURING:
|
||||
configurator = hass.components.configurator
|
||||
configurator.request_done(_CONFIGURING.pop("gpmdp"))
|
||||
configurator.request_done(hass, _CONFIGURING.pop("gpmdp"))
|
||||
|
||||
add_entities([GPMDP(name, url, code)], True)
|
||||
|
||||
|
|
|
@ -6,6 +6,7 @@ import os
|
|||
from rtmapi import Rtm, RtmRequestFailedException
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components import configurator
|
||||
from homeassistant.const import CONF_API_KEY, CONF_ID, CONF_NAME, CONF_TOKEN, STATE_OK
|
||||
from homeassistant.core import HomeAssistant, ServiceCall
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
|
@ -105,7 +106,6 @@ def _register_new_account(
|
|||
hass, account_name, api_key, shared_secret, stored_rtm_config, component
|
||||
):
|
||||
request_id = None
|
||||
configurator = hass.components.configurator
|
||||
api = Rtm(api_key, shared_secret, "write", None)
|
||||
url, frob = api.authenticate_desktop()
|
||||
_LOGGER.debug("Sent authentication request to server")
|
||||
|
@ -117,7 +117,7 @@ def _register_new_account(
|
|||
if api.token is None:
|
||||
_LOGGER.error("Failed to register, please try again")
|
||||
configurator.notify_errors(
|
||||
request_id, "Failed to register, please try again."
|
||||
hass, request_id, "Failed to register, please try again."
|
||||
)
|
||||
return
|
||||
|
||||
|
@ -134,9 +134,10 @@ def _register_new_account(
|
|||
component,
|
||||
)
|
||||
|
||||
configurator.request_done(request_id)
|
||||
configurator.request_done(hass, request_id)
|
||||
|
||||
request_id = configurator.async_request_config(
|
||||
hass,
|
||||
f"{DOMAIN} - {account_name}",
|
||||
callback=register_account_callback,
|
||||
description=(
|
||||
|
|
|
@ -8,6 +8,7 @@ import logging
|
|||
from pysabnzbd import SabnzbdApi, SabnzbdApiException
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components import configurator
|
||||
from homeassistant.components.discovery import SERVICE_SABNZBD
|
||||
from homeassistant.components.sensor import SensorEntityDescription
|
||||
from homeassistant.const import (
|
||||
|
@ -267,11 +268,10 @@ def async_setup_sabnzbd(hass, sab_api, config, name):
|
|||
def async_request_configuration(hass, config, host, web_root):
|
||||
"""Request configuration steps from the user."""
|
||||
|
||||
configurator = hass.components.configurator
|
||||
# We got an error if this method is called while we are configuring
|
||||
if host in _CONFIGURING:
|
||||
configurator.async_notify_errors(
|
||||
_CONFIGURING[host], "Failed to register, please try again."
|
||||
hass, _CONFIGURING[host], "Failed to register, please try again."
|
||||
)
|
||||
|
||||
return
|
||||
|
@ -291,7 +291,7 @@ def async_request_configuration(hass, config, host, web_root):
|
|||
conf[host] = {CONF_API_KEY: api_key}
|
||||
save_json(hass.config.path(CONFIG_FILE), conf)
|
||||
req_config = _CONFIGURING.pop(host)
|
||||
configurator.request_done(req_config)
|
||||
configurator.request_done(hass, req_config)
|
||||
|
||||
hass.async_add_job(success)
|
||||
async_setup_sabnzbd(hass, sab_api, config, config.get(CONF_NAME, DEFAULT_NAME))
|
||||
|
|
Loading…
Reference in New Issue