Use OptionsFlowWithReload in coinbase (#150587)

This commit is contained in:
G Johansson
2025-08-13 23:48:20 +02:00
committed by GitHub
parent b5db0e98b4
commit 9999807891
4 changed files with 23 additions and 35 deletions

View File

@@ -12,7 +12,6 @@ from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_API_KEY, CONF_API_TOKEN, Platform from homeassistant.const import CONF_API_KEY, CONF_API_TOKEN, Platform
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryAuthFailed from homeassistant.exceptions import ConfigEntryAuthFailed
from homeassistant.helpers import entity_registry as er
from homeassistant.util import Throttle from homeassistant.util import Throttle
from .const import ( from .const import (
@@ -30,9 +29,7 @@ from .const import (
API_RESOURCE_TYPE, API_RESOURCE_TYPE,
API_V3_ACCOUNT_ID, API_V3_ACCOUNT_ID,
API_V3_TYPE_VAULT, API_V3_TYPE_VAULT,
CONF_CURRENCIES,
CONF_EXCHANGE_BASE, CONF_EXCHANGE_BASE,
CONF_EXCHANGE_RATES,
) )
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@@ -47,9 +44,6 @@ async def async_setup_entry(hass: HomeAssistant, entry: CoinbaseConfigEntry) ->
"""Set up Coinbase from a config entry.""" """Set up Coinbase from a config entry."""
instance = await hass.async_add_executor_job(create_and_update_instance, 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 entry.runtime_data = instance
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
@@ -83,29 +77,6 @@ def create_and_update_instance(entry: CoinbaseConfigEntry) -> CoinbaseData:
return instance 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): def get_accounts(client):
"""Handle paginated accounts.""" """Handle paginated accounts."""
response = client.get_accounts() response = client.get_accounts()

View File

@@ -10,7 +10,11 @@ from coinbase.rest import RESTClient
from coinbase.rest.rest_base import HTTPError from coinbase.rest.rest_base import HTTPError
import voluptuous as vol 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.const import CONF_API_KEY, CONF_API_TOKEN
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
from homeassistant.exceptions import HomeAssistantError from homeassistant.exceptions import HomeAssistantError
@@ -204,7 +208,7 @@ class CoinbaseConfigFlow(ConfigFlow, domain=DOMAIN):
return OptionsFlowHandler() return OptionsFlowHandler()
class OptionsFlowHandler(OptionsFlow): class OptionsFlowHandler(OptionsFlowWithReload):
"""Handle a option flow for Coinbase.""" """Handle a option flow for Coinbase."""
async def async_step_init( async def async_step_init(

View File

@@ -6,6 +6,7 @@ import logging
from homeassistant.components.sensor import SensorEntity, SensorStateClass from homeassistant.components.sensor import SensorEntity, SensorStateClass
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
@@ -68,6 +69,22 @@ async def async_setup_entry(
CONF_EXCHANGE_PRECISION, CONF_EXCHANGE_PRECISION_DEFAULT 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: for currency in desired_currencies:
_LOGGER.debug( _LOGGER.debug(
"Attempting to set up %s account sensor", "Attempting to set up %s account sensor",

View File

@@ -186,9 +186,6 @@ async def test_option_form(hass: HomeAssistant) -> None:
"coinbase.rest.RESTClient.get", "coinbase.rest.RESTClient.get",
return_value={"data": mock_get_exchange_rates()}, return_value={"data": mock_get_exchange_rates()},
), ),
patch(
"homeassistant.components.coinbase.update_listener"
) as mock_update_listener,
): ):
config_entry = await init_mock_coinbase(hass) config_entry = await init_mock_coinbase(hass)
await hass.async_block_till_done() 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 assert result2["type"] is FlowResultType.CREATE_ENTRY
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(mock_update_listener.mock_calls) == 1
async def test_form_bad_account_currency(hass: HomeAssistant) -> None: async def test_form_bad_account_currency(hass: HomeAssistant) -> None: