mirror of
https://github.com/home-assistant/core.git
synced 2025-09-06 05:11:35 +02:00
Use OptionsFlowWithReload in coinbase (#150587)
This commit is contained in:
@@ -12,7 +12,6 @@ from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_API_KEY, CONF_API_TOKEN, Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import ConfigEntryAuthFailed
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
from homeassistant.util import Throttle
|
||||
|
||||
from .const import (
|
||||
@@ -30,9 +29,7 @@ from .const import (
|
||||
API_RESOURCE_TYPE,
|
||||
API_V3_ACCOUNT_ID,
|
||||
API_V3_TYPE_VAULT,
|
||||
CONF_CURRENCIES,
|
||||
CONF_EXCHANGE_BASE,
|
||||
CONF_EXCHANGE_RATES,
|
||||
)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
@@ -47,9 +44,6 @@ async def async_setup_entry(hass: HomeAssistant, entry: CoinbaseConfigEntry) ->
|
||||
"""Set up Coinbase from a config entry."""
|
||||
|
||||
instance = await hass.async_add_executor_job(create_and_update_instance, entry)
|
||||
|
||||
entry.async_on_unload(entry.add_update_listener(update_listener))
|
||||
|
||||
entry.runtime_data = instance
|
||||
|
||||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||
@@ -83,29 +77,6 @@ def create_and_update_instance(entry: CoinbaseConfigEntry) -> CoinbaseData:
|
||||
return instance
|
||||
|
||||
|
||||
async def update_listener(
|
||||
hass: HomeAssistant, config_entry: CoinbaseConfigEntry
|
||||
) -> None:
|
||||
"""Handle options update."""
|
||||
|
||||
await hass.config_entries.async_reload(config_entry.entry_id)
|
||||
|
||||
registry = er.async_get(hass)
|
||||
entities = er.async_entries_for_config_entry(registry, config_entry.entry_id)
|
||||
|
||||
# Remove orphaned entities
|
||||
for entity in entities:
|
||||
currency = entity.unique_id.split("-")[-1]
|
||||
if (
|
||||
"xe" in entity.unique_id
|
||||
and currency not in config_entry.options.get(CONF_EXCHANGE_RATES, [])
|
||||
) or (
|
||||
"wallet" in entity.unique_id
|
||||
and currency not in config_entry.options.get(CONF_CURRENCIES, [])
|
||||
):
|
||||
registry.async_remove(entity.entity_id)
|
||||
|
||||
|
||||
def get_accounts(client):
|
||||
"""Handle paginated accounts."""
|
||||
response = client.get_accounts()
|
||||
|
@@ -10,7 +10,11 @@ from coinbase.rest import RESTClient
|
||||
from coinbase.rest.rest_base import HTTPError
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult, OptionsFlow
|
||||
from homeassistant.config_entries import (
|
||||
ConfigFlow,
|
||||
ConfigFlowResult,
|
||||
OptionsFlowWithReload,
|
||||
)
|
||||
from homeassistant.const import CONF_API_KEY, CONF_API_TOKEN
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
@@ -204,7 +208,7 @@ class CoinbaseConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
return OptionsFlowHandler()
|
||||
|
||||
|
||||
class OptionsFlowHandler(OptionsFlow):
|
||||
class OptionsFlowHandler(OptionsFlowWithReload):
|
||||
"""Handle a option flow for Coinbase."""
|
||||
|
||||
async def async_step_init(
|
||||
|
@@ -6,6 +6,7 @@ import logging
|
||||
|
||||
from homeassistant.components.sensor import SensorEntity, SensorStateClass
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
|
||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||
|
||||
@@ -68,6 +69,22 @@ async def async_setup_entry(
|
||||
CONF_EXCHANGE_PRECISION, CONF_EXCHANGE_PRECISION_DEFAULT
|
||||
)
|
||||
|
||||
# Remove orphaned entities
|
||||
registry = er.async_get(hass)
|
||||
existing_entities = er.async_entries_for_config_entry(
|
||||
registry, config_entry.entry_id
|
||||
)
|
||||
for entity in existing_entities:
|
||||
currency = entity.unique_id.split("-")[-1]
|
||||
if (
|
||||
"xe" in entity.unique_id
|
||||
and currency not in config_entry.options.get(CONF_EXCHANGE_RATES, [])
|
||||
) or (
|
||||
"wallet" in entity.unique_id
|
||||
and currency not in config_entry.options.get(CONF_CURRENCIES, [])
|
||||
):
|
||||
registry.async_remove(entity.entity_id)
|
||||
|
||||
for currency in desired_currencies:
|
||||
_LOGGER.debug(
|
||||
"Attempting to set up %s account sensor",
|
||||
|
@@ -186,9 +186,6 @@ async def test_option_form(hass: HomeAssistant) -> None:
|
||||
"coinbase.rest.RESTClient.get",
|
||||
return_value={"data": mock_get_exchange_rates()},
|
||||
),
|
||||
patch(
|
||||
"homeassistant.components.coinbase.update_listener"
|
||||
) as mock_update_listener,
|
||||
):
|
||||
config_entry = await init_mock_coinbase(hass)
|
||||
await hass.async_block_till_done()
|
||||
@@ -204,7 +201,6 @@ async def test_option_form(hass: HomeAssistant) -> None:
|
||||
)
|
||||
assert result2["type"] is FlowResultType.CREATE_ENTRY
|
||||
await hass.async_block_till_done()
|
||||
assert len(mock_update_listener.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_form_bad_account_currency(hass: HomeAssistant) -> None:
|
||||
|
Reference in New Issue
Block a user