From 999980789121dc08b0e98d5674bd720f8dc78376 Mon Sep 17 00:00:00 2001 From: G Johansson Date: Wed, 13 Aug 2025 23:48:20 +0200 Subject: [PATCH] Use OptionsFlowWithReload in coinbase (#150587) --- homeassistant/components/coinbase/__init__.py | 29 ------------------- .../components/coinbase/config_flow.py | 8 +++-- homeassistant/components/coinbase/sensor.py | 17 +++++++++++ tests/components/coinbase/test_config_flow.py | 4 --- 4 files changed, 23 insertions(+), 35 deletions(-) diff --git a/homeassistant/components/coinbase/__init__.py b/homeassistant/components/coinbase/__init__.py index adb6dc48c9c..dca7f774331 100644 --- a/homeassistant/components/coinbase/__init__.py +++ b/homeassistant/components/coinbase/__init__.py @@ -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() diff --git a/homeassistant/components/coinbase/config_flow.py b/homeassistant/components/coinbase/config_flow.py index e1dad899d2b..6aad3a81d17 100644 --- a/homeassistant/components/coinbase/config_flow.py +++ b/homeassistant/components/coinbase/config_flow.py @@ -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( diff --git a/homeassistant/components/coinbase/sensor.py b/homeassistant/components/coinbase/sensor.py index f69aed8c386..4dfc744b7fa 100644 --- a/homeassistant/components/coinbase/sensor.py +++ b/homeassistant/components/coinbase/sensor.py @@ -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", diff --git a/tests/components/coinbase/test_config_flow.py b/tests/components/coinbase/test_config_flow.py index 0dc7fa95ffb..3858df83269 100644 --- a/tests/components/coinbase/test_config_flow.py +++ b/tests/components/coinbase/test_config_flow.py @@ -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: