mirror of
https://github.com/home-assistant/core.git
synced 2025-04-27 10:47:51 +00:00
Use runtime_data in coinbase (#136381)
This commit is contained in:
parent
005ae3ace6
commit
a70a9d2f76
@ -37,7 +37,6 @@ from .const import (
|
|||||||
CONF_CURRENCIES,
|
CONF_CURRENCIES,
|
||||||
CONF_EXCHANGE_BASE,
|
CONF_EXCHANGE_BASE,
|
||||||
CONF_EXCHANGE_RATES,
|
CONF_EXCHANGE_RATES,
|
||||||
DOMAIN,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
@ -45,33 +44,29 @@ _LOGGER = logging.getLogger(__name__)
|
|||||||
PLATFORMS = [Platform.SENSOR]
|
PLATFORMS = [Platform.SENSOR]
|
||||||
MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=1)
|
MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=1)
|
||||||
|
|
||||||
|
type CoinbaseConfigEntry = ConfigEntry[CoinbaseData]
|
||||||
|
|
||||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|
||||||
|
async def async_setup_entry(hass: HomeAssistant, entry: CoinbaseConfigEntry) -> bool:
|
||||||
"""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.async_on_unload(entry.add_update_listener(update_listener))
|
||||||
|
|
||||||
hass.data.setdefault(DOMAIN, {})
|
entry.runtime_data = instance
|
||||||
|
|
||||||
hass.data[DOMAIN][entry.entry_id] = instance
|
|
||||||
|
|
||||||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_unload_entry(hass: HomeAssistant, entry: CoinbaseConfigEntry) -> bool:
|
||||||
"""Unload a config entry."""
|
"""Unload a config entry."""
|
||||||
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||||
if unload_ok:
|
|
||||||
hass.data[DOMAIN].pop(entry.entry_id)
|
|
||||||
|
|
||||||
return unload_ok
|
|
||||||
|
|
||||||
|
|
||||||
def create_and_update_instance(entry: ConfigEntry) -> CoinbaseData:
|
def create_and_update_instance(entry: CoinbaseConfigEntry) -> CoinbaseData:
|
||||||
"""Create and update a Coinbase Data instance."""
|
"""Create and update a Coinbase Data instance."""
|
||||||
if "organizations" not in entry.data[CONF_API_KEY]:
|
if "organizations" not in entry.data[CONF_API_KEY]:
|
||||||
client = LegacyClient(entry.data[CONF_API_KEY], entry.data[CONF_API_TOKEN])
|
client = LegacyClient(entry.data[CONF_API_KEY], entry.data[CONF_API_TOKEN])
|
||||||
@ -87,7 +82,9 @@ def create_and_update_instance(entry: ConfigEntry) -> CoinbaseData:
|
|||||||
return instance
|
return instance
|
||||||
|
|
||||||
|
|
||||||
async def update_listener(hass: HomeAssistant, config_entry: ConfigEntry) -> None:
|
async def update_listener(
|
||||||
|
hass: HomeAssistant, config_entry: CoinbaseConfigEntry
|
||||||
|
) -> None:
|
||||||
"""Handle options update."""
|
"""Handle options update."""
|
||||||
|
|
||||||
await hass.config_entries.async_reload(config_entry.entry_id)
|
await hass.config_entries.async_reload(config_entry.entry_id)
|
||||||
|
@ -11,18 +11,13 @@ from coinbase.wallet.client import Client as LegacyClient
|
|||||||
from coinbase.wallet.error import AuthenticationError
|
from coinbase.wallet.error import AuthenticationError
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.config_entries import (
|
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult, OptionsFlow
|
||||||
ConfigEntry,
|
|
||||||
ConfigFlow,
|
|
||||||
ConfigFlowResult,
|
|
||||||
OptionsFlow,
|
|
||||||
)
|
|
||||||
from homeassistant.const import CONF_API_KEY, CONF_API_TOKEN, CONF_API_VERSION
|
from homeassistant.const import CONF_API_KEY, CONF_API_TOKEN, CONF_API_VERSION
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.exceptions import HomeAssistantError
|
from homeassistant.exceptions import HomeAssistantError
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
|
||||||
from . import get_accounts
|
from . import CoinbaseConfigEntry, get_accounts
|
||||||
from .const import (
|
from .const import (
|
||||||
ACCOUNT_IS_VAULT,
|
ACCOUNT_IS_VAULT,
|
||||||
API_ACCOUNT_CURRENCY,
|
API_ACCOUNT_CURRENCY,
|
||||||
@ -83,10 +78,12 @@ async def validate_api(hass: HomeAssistant, data):
|
|||||||
return {"title": user, "api_version": api_version}
|
return {"title": user, "api_version": api_version}
|
||||||
|
|
||||||
|
|
||||||
async def validate_options(hass: HomeAssistant, config_entry: ConfigEntry, options):
|
async def validate_options(
|
||||||
|
hass: HomeAssistant, config_entry: CoinbaseConfigEntry, options
|
||||||
|
):
|
||||||
"""Validate the requested resources are provided by API."""
|
"""Validate the requested resources are provided by API."""
|
||||||
|
|
||||||
client = hass.data[DOMAIN][config_entry.entry_id].client
|
client = config_entry.runtime_data.client
|
||||||
|
|
||||||
accounts = await hass.async_add_executor_job(
|
accounts = await hass.async_add_executor_job(
|
||||||
get_accounts, client, config_entry.data.get("api_version", "v2")
|
get_accounts, client, config_entry.data.get("api_version", "v2")
|
||||||
@ -155,7 +152,7 @@ class CoinbaseConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||||||
@staticmethod
|
@staticmethod
|
||||||
@callback
|
@callback
|
||||||
def async_get_options_flow(
|
def async_get_options_flow(
|
||||||
config_entry: ConfigEntry,
|
config_entry: CoinbaseConfigEntry,
|
||||||
) -> OptionsFlowHandler:
|
) -> OptionsFlowHandler:
|
||||||
"""Get the options flow for this handler."""
|
"""Get the options flow for this handler."""
|
||||||
return OptionsFlowHandler()
|
return OptionsFlowHandler()
|
||||||
|
@ -3,12 +3,11 @@
|
|||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from homeassistant.components.diagnostics import async_redact_data
|
from homeassistant.components.diagnostics import async_redact_data
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.const import CONF_API_KEY, CONF_API_TOKEN, CONF_ID
|
from homeassistant.const import CONF_API_KEY, CONF_API_TOKEN, CONF_ID
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
|
||||||
from . import CoinbaseData
|
from . import CoinbaseConfigEntry
|
||||||
from .const import API_ACCOUNT_AMOUNT, API_RESOURCE_PATH, CONF_TITLE, DOMAIN
|
from .const import API_ACCOUNT_AMOUNT, API_RESOURCE_PATH, CONF_TITLE
|
||||||
|
|
||||||
TO_REDACT = {
|
TO_REDACT = {
|
||||||
API_ACCOUNT_AMOUNT,
|
API_ACCOUNT_AMOUNT,
|
||||||
@ -21,15 +20,13 @@ TO_REDACT = {
|
|||||||
|
|
||||||
|
|
||||||
async def async_get_config_entry_diagnostics(
|
async def async_get_config_entry_diagnostics(
|
||||||
hass: HomeAssistant, entry: ConfigEntry
|
hass: HomeAssistant, entry: CoinbaseConfigEntry
|
||||||
) -> dict[str, Any]:
|
) -> dict[str, Any]:
|
||||||
"""Return diagnostics for a config entry."""
|
"""Return diagnostics for a config entry."""
|
||||||
instance: CoinbaseData = hass.data[DOMAIN][entry.entry_id]
|
|
||||||
|
|
||||||
return async_redact_data(
|
return async_redact_data(
|
||||||
{
|
{
|
||||||
"entry": entry.as_dict(),
|
"entry": entry.as_dict(),
|
||||||
"accounts": instance.accounts,
|
"accounts": entry.runtime_data.accounts,
|
||||||
},
|
},
|
||||||
TO_REDACT,
|
TO_REDACT,
|
||||||
)
|
)
|
||||||
|
@ -5,12 +5,11 @@ from __future__ import annotations
|
|||||||
import logging
|
import logging
|
||||||
|
|
||||||
from homeassistant.components.sensor import SensorEntity, SensorStateClass
|
from homeassistant.components.sensor import SensorEntity, SensorStateClass
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
|
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
from . import CoinbaseData
|
from . import CoinbaseConfigEntry, CoinbaseData
|
||||||
from .const import (
|
from .const import (
|
||||||
ACCOUNT_IS_VAULT,
|
ACCOUNT_IS_VAULT,
|
||||||
API_ACCOUNT_AMOUNT,
|
API_ACCOUNT_AMOUNT,
|
||||||
@ -45,11 +44,11 @@ ATTRIBUTION = "Data provided by coinbase.com"
|
|||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
config_entry: ConfigEntry,
|
config_entry: CoinbaseConfigEntry,
|
||||||
async_add_entities: AddEntitiesCallback,
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up Coinbase sensor platform."""
|
"""Set up Coinbase sensor platform."""
|
||||||
instance: CoinbaseData = hass.data[DOMAIN][config_entry.entry_id]
|
instance = config_entry.runtime_data
|
||||||
|
|
||||||
entities: list[SensorEntity] = []
|
entities: list[SensorEntity] = []
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user