Remove YAML import from coinbase (#80084)

This commit is contained in:
epenet 2022-10-26 17:14:31 +02:00 committed by GitHub
parent a1a0284e81
commit 3eb574edca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 13 additions and 193 deletions

View File

@ -6,14 +6,12 @@ import logging
from coinbase.wallet.client import Client
from coinbase.wallet.error import AuthenticationError
import voluptuous as vol
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_API_KEY, CONF_API_TOKEN, Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry, issue_registry as ir
from homeassistant.helpers import entity_registry
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.typing import ConfigType
from homeassistant.util import Throttle
from .const import (
@ -22,7 +20,6 @@ from .const import (
CONF_CURRENCIES,
CONF_EXCHANGE_BASE,
CONF_EXCHANGE_RATES,
CONF_YAML_API_TOKEN,
DOMAIN,
)
@ -32,46 +29,7 @@ PLATFORMS = [Platform.SENSOR]
MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=1)
CONFIG_SCHEMA = vol.Schema(
cv.deprecated(DOMAIN),
{
DOMAIN: vol.Schema(
{
vol.Required(CONF_API_KEY): cv.string,
vol.Required(CONF_YAML_API_TOKEN): cv.string,
vol.Optional(CONF_CURRENCIES): vol.All(cv.ensure_list, [cv.string]),
vol.Optional(CONF_EXCHANGE_RATES, default=[]): vol.All(
cv.ensure_list, [cv.string]
),
},
)
},
extra=vol.ALLOW_EXTRA,
)
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Set up the Coinbase component."""
if DOMAIN not in config:
return True
ir.async_create_issue(
hass,
DOMAIN,
"remove_yaml",
breaks_in_ha_version="2022.12.0",
is_fixable=False,
severity=ir.IssueSeverity.WARNING,
translation_key="removed_yaml",
)
hass.async_create_task(
hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_IMPORT},
data=config[DOMAIN],
)
)
return True
CONFIG_SCHEMA = cv.removed(DOMAIN, raise_if_present=False)
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:

View File

@ -2,6 +2,7 @@
from __future__ import annotations
import logging
from typing import Any
from coinbase.wallet.client import Client
from coinbase.wallet.error import AuthenticationError
@ -10,6 +11,7 @@ import voluptuous as vol
from homeassistant import config_entries, core, exceptions
from homeassistant.const import CONF_API_KEY, CONF_API_TOKEN
from homeassistant.core import callback
from homeassistant.data_entry_flow import FlowResult
import homeassistant.helpers.config_validation as cv
from . import get_accounts
@ -23,8 +25,6 @@ from .const import (
CONF_EXCHANGE_PRECISION,
CONF_EXCHANGE_PRECISION_DEFAULT,
CONF_EXCHANGE_RATES,
CONF_OPTIONS,
CONF_YAML_API_TOKEN,
DOMAIN,
RATES,
WALLETS,
@ -104,9 +104,11 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
VERSION = 1
async def async_step_user(self, user_input=None):
async def async_step_user(
self, user_input: dict[str, str] | None = None
) -> FlowResult:
"""Handle the initial step."""
errors = {}
errors: dict[str, str] = {}
if user_input is None:
return self.async_show_form(
step_id="user", data_schema=STEP_USER_DATA_SCHEMA, errors=errors
@ -114,11 +116,6 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
self._async_abort_entries_match({CONF_API_KEY: user_input[CONF_API_KEY]})
options = {}
if CONF_OPTIONS in user_input:
options = user_input.pop(CONF_OPTIONS)
try:
info = await validate_api(self.hass, user_input)
except CannotConnect:
@ -133,33 +130,11 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
_LOGGER.exception("Unexpected exception")
errors["base"] = "unknown"
else:
return self.async_create_entry(
title=info["title"], data=user_input, options=options
)
return self.async_create_entry(title=info["title"], data=user_input)
return self.async_show_form(
step_id="user", data_schema=STEP_USER_DATA_SCHEMA, errors=errors
)
async def async_step_import(self, config):
"""Handle import of Coinbase config from YAML."""
cleaned_data = {
CONF_API_KEY: config[CONF_API_KEY],
CONF_API_TOKEN: config[CONF_YAML_API_TOKEN],
}
cleaned_data[CONF_OPTIONS] = {
CONF_CURRENCIES: [],
CONF_EXCHANGE_RATES: [],
}
if CONF_CURRENCIES in config:
cleaned_data[CONF_OPTIONS][CONF_CURRENCIES] = config[CONF_CURRENCIES]
if CONF_EXCHANGE_RATES in config:
cleaned_data[CONF_OPTIONS][CONF_EXCHANGE_RATES] = config[
CONF_EXCHANGE_RATES
]
return await self.async_step_user(user_input=cleaned_data)
@staticmethod
@callback
def async_get_options_flow(
@ -176,7 +151,9 @@ class OptionsFlowHandler(config_entries.OptionsFlow):
"""Initialize options flow."""
self.config_entry = config_entry
async def async_step_init(self, user_input=None):
async def async_step_init(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
"""Manage the options."""
errors = {}

View File

@ -5,13 +5,9 @@ CONF_EXCHANGE_BASE = "exchange_base"
CONF_EXCHANGE_RATES = "exchange_rate_currencies"
CONF_EXCHANGE_PRECISION = "exchange_rate_precision"
CONF_EXCHANGE_PRECISION_DEFAULT = 2
CONF_OPTIONS = "options"
CONF_TITLE = "title"
DOMAIN = "coinbase"
# These are constants used by the previous YAML configuration
CONF_YAML_API_TOKEN = "api_secret"
# Constants for data returned by Coinbase API
API_ACCOUNT_AMOUNT = "amount"
API_ACCOUNT_BALANCE = "balance"

View File

@ -38,11 +38,5 @@
"currency_unavailable": "One or more of the requested currency balances is not provided by your Coinbase API.",
"exchange_rate_unavailable": "One or more of the requested exchange rates is not provided by Coinbase."
}
},
"issues": {
"removed_yaml": {
"title": "The Coinbase YAML configuration has been removed",
"description": "Configuring Coinbase using YAML has been removed.\n\nYour existing YAML configuration is not used by Home Assistant.\n\nRemove the YAML configuration from your configuration.yaml file and restart Home Assistant to fix this issue."
}
}
}

View File

@ -21,12 +21,6 @@
}
}
},
"issues": {
"removed_yaml": {
"description": "Configuring Coinbase using YAML has been removed.\n\nYour existing YAML configuration is not used by Home Assistant.\n\nRemove the YAML configuration from your configuration.yaml file and restart Home Assistant to fix this issue.",
"title": "The Coinbase YAML configuration has been removed"
}
},
"options": {
"error": {
"currency_unavailable": "One or more of the requested currency balances is not provided by your Coinbase API.",

View File

@ -10,7 +10,6 @@ from homeassistant.components.coinbase.const import (
CONF_CURRENCIES,
CONF_EXCHANGE_PRECISION,
CONF_EXCHANGE_RATES,
CONF_YAML_API_TOKEN,
DOMAIN,
)
from homeassistant.const import CONF_API_KEY, CONF_API_TOKEN
@ -23,8 +22,6 @@ from .common import (
)
from .const import BAD_CURRENCY, BAD_EXCHANGE_RATE, GOOD_CURRENCY, GOOD_EXCHANGE_RATE
from tests.common import MockConfigEntry
async def test_form(hass):
"""Test we get the form."""
@ -44,8 +41,6 @@ async def test_form(hass):
"coinbase.wallet.client.Client.get_exchange_rates",
return_value=mock_get_exchange_rates(),
), patch(
"homeassistant.components.coinbase.async_setup", return_value=True
) as mock_setup, patch(
"homeassistant.components.coinbase.async_setup_entry",
return_value=True,
) as mock_setup_entry:
@ -61,7 +56,6 @@ async def test_form(hass):
assert result2["type"] == "create_entry"
assert result2["title"] == "Test User"
assert result2["data"] == {CONF_API_KEY: "123456", CONF_API_TOKEN: "AbCDeF"}
assert len(mock_setup.mock_calls) == 1
assert len(mock_setup_entry.mock_calls) == 1
@ -303,62 +297,3 @@ async def test_option_catch_all_exception(hass):
assert result2["type"] == "form"
assert result2["errors"] == {"base": "unknown"}
async def test_yaml_import(hass):
"""Test YAML import works."""
conf = {
CONF_API_KEY: "123456",
CONF_YAML_API_TOKEN: "AbCDeF",
CONF_CURRENCIES: ["BTC", "USD"],
CONF_EXCHANGE_RATES: ["ATOM", "BTC"],
}
with patch(
"coinbase.wallet.client.Client.get_current_user",
return_value=mock_get_current_user(),
), patch(
"coinbase.wallet.client.Client.get_accounts", new=mocked_get_accounts
), patch(
"coinbase.wallet.client.Client.get_exchange_rates",
return_value=mock_get_exchange_rates(),
), patch(
"homeassistant.components.coinbase.async_setup", return_value=True
) as mock_setup, patch(
"homeassistant.components.coinbase.async_setup_entry",
return_value=True,
) as mock_setup_entry:
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_IMPORT}, data=conf
)
assert result["type"] == "create_entry"
assert result["title"] == "Test User"
assert result["data"] == {CONF_API_KEY: "123456", CONF_API_TOKEN: "AbCDeF"}
assert result["options"] == {
CONF_CURRENCIES: ["BTC", "USD"],
CONF_EXCHANGE_RATES: ["ATOM", "BTC"],
}
assert len(mock_setup.mock_calls) == 1
assert len(mock_setup_entry.mock_calls) == 1
async def test_yaml_existing(hass):
"""Test YAML ignored when already processed."""
MockConfigEntry(
domain=DOMAIN,
data={
CONF_API_KEY: "123456",
CONF_API_TOKEN: "AbCDeF",
},
).add_to_hass(hass)
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_IMPORT},
data={
CONF_API_KEY: "123456",
CONF_YAML_API_TOKEN: "AbCDeF",
},
)
assert result["type"] == "abort"
assert result["reason"] == "already_configured"

View File

@ -6,13 +6,10 @@ from homeassistant.components.coinbase.const import (
API_TYPE_VAULT,
CONF_CURRENCIES,
CONF_EXCHANGE_RATES,
CONF_YAML_API_TOKEN,
DOMAIN,
)
from homeassistant.const import CONF_API_KEY
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry
from homeassistant.setup import async_setup_component
from .common import (
init_mock_coinbase,
@ -28,37 +25,6 @@ from .const import (
)
async def test_setup(hass):
"""Test setting up from configuration.yaml."""
conf = {
DOMAIN: {
CONF_API_KEY: "123456",
CONF_YAML_API_TOKEN: "AbCDeF",
CONF_CURRENCIES: [GOOD_CURRENCY, GOOD_CURRENCY_2],
CONF_EXCHANGE_RATES: [GOOD_EXCHANGE_RATE, GOOD_EXCHANGE_RATE_2],
}
}
with patch(
"coinbase.wallet.client.Client.get_current_user",
return_value=mock_get_current_user(),
), patch(
"coinbase.wallet.client.Client.get_accounts",
new=mocked_get_accounts,
), patch(
"coinbase.wallet.client.Client.get_exchange_rates",
return_value=mock_get_exchange_rates(),
):
assert await async_setup_component(hass, DOMAIN, conf)
entries = hass.config_entries.async_entries(DOMAIN)
assert len(entries) == 1
assert entries[0].title == "Test User"
assert entries[0].source == config_entries.SOURCE_IMPORT
assert entries[0].options == {
CONF_CURRENCIES: [GOOD_CURRENCY, GOOD_CURRENCY_2],
CONF_EXCHANGE_RATES: [GOOD_EXCHANGE_RATE, GOOD_EXCHANGE_RATE_2],
}
async def test_unload_entry(hass):
"""Test successful unload of entry."""
with patch(