Migrate lastfm to use runtime_data (#147330)

This commit is contained in:
epenet 2025-06-23 10:40:48 +02:00 committed by GitHub
parent 10c573bbc3
commit 69d2cd0ac0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 16 additions and 19 deletions

View File

@ -2,19 +2,18 @@
from __future__ import annotations from __future__ import annotations
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from .const import DOMAIN, PLATFORMS from .const import PLATFORMS
from .coordinator import LastFMDataUpdateCoordinator from .coordinator import LastFMConfigEntry, LastFMDataUpdateCoordinator
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: async def async_setup_entry(hass: HomeAssistant, entry: LastFMConfigEntry) -> bool:
"""Set up lastfm from a config entry.""" """Set up lastfm from a config entry."""
coordinator = LastFMDataUpdateCoordinator(hass, entry) coordinator = LastFMDataUpdateCoordinator(hass, entry)
await coordinator.async_config_entry_first_refresh() await coordinator.async_config_entry_first_refresh()
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator entry.runtime_data = coordinator
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
entry.async_on_unload(entry.add_update_listener(update_listener)) entry.async_on_unload(entry.add_update_listener(update_listener))
@ -22,12 +21,11 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
return True return True
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: async def async_unload_entry(hass: HomeAssistant, entry: LastFMConfigEntry) -> bool:
"""Unload lastfm config entry.""" """Unload lastfm config entry."""
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS) return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
async def update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None: async def update_listener(hass: HomeAssistant, entry: LastFMConfigEntry) -> None:
"""Handle options update.""" """Handle options update."""
await hass.config_entries.async_reload(entry.entry_id) await hass.config_entries.async_reload(entry.entry_id)

View File

@ -8,12 +8,7 @@ from typing import Any
from pylast import LastFMNetwork, PyLastError, User, WSError from pylast import LastFMNetwork, PyLastError, User, WSError
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 from homeassistant.const import CONF_API_KEY
from homeassistant.core import callback from homeassistant.core import callback
from homeassistant.helpers.selector import ( from homeassistant.helpers.selector import (
@ -23,6 +18,7 @@ from homeassistant.helpers.selector import (
) )
from .const import CONF_MAIN_USER, CONF_USERS, DOMAIN from .const import CONF_MAIN_USER, CONF_USERS, DOMAIN
from .coordinator import LastFMConfigEntry
PLACEHOLDERS = {"api_account_url": "https://www.last.fm/api/account/create"} PLACEHOLDERS = {"api_account_url": "https://www.last.fm/api/account/create"}
@ -81,7 +77,7 @@ class LastFmConfigFlowHandler(ConfigFlow, domain=DOMAIN):
@staticmethod @staticmethod
@callback @callback
def async_get_options_flow( def async_get_options_flow(
config_entry: ConfigEntry, config_entry: LastFMConfigEntry,
) -> LastFmOptionsFlowHandler: ) -> LastFmOptionsFlowHandler:
"""Get the options flow for this handler.""" """Get the options flow for this handler."""
return LastFmOptionsFlowHandler() return LastFmOptionsFlowHandler()
@ -162,6 +158,8 @@ class LastFmConfigFlowHandler(ConfigFlow, domain=DOMAIN):
class LastFmOptionsFlowHandler(OptionsFlow): class LastFmOptionsFlowHandler(OptionsFlow):
"""LastFm Options flow handler.""" """LastFm Options flow handler."""
config_entry: LastFMConfigEntry
async def async_step_init( async def async_step_init(
self, user_input: dict[str, Any] | None = None self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult: ) -> ConfigFlowResult:

View File

@ -14,6 +14,8 @@ from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, Upda
from .const import CONF_USERS, DOMAIN, LOGGER from .const import CONF_USERS, DOMAIN, LOGGER
type LastFMConfigEntry = ConfigEntry[LastFMDataUpdateCoordinator]
def format_track(track: Track | None) -> str | None: def format_track(track: Track | None) -> str | None:
"""Format the track.""" """Format the track."""

View File

@ -6,7 +6,6 @@ import hashlib
from typing import Any from typing import Any
from homeassistant.components.sensor import SensorEntity from homeassistant.components.sensor import SensorEntity
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 AddConfigEntryEntitiesCallback from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
@ -21,17 +20,17 @@ from .const import (
DOMAIN, DOMAIN,
STATE_NOT_SCROBBLING, STATE_NOT_SCROBBLING,
) )
from .coordinator import LastFMDataUpdateCoordinator, LastFMUserData from .coordinator import LastFMConfigEntry, LastFMDataUpdateCoordinator, LastFMUserData
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, hass: HomeAssistant,
entry: ConfigEntry, entry: LastFMConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback, async_add_entities: AddConfigEntryEntitiesCallback,
) -> None: ) -> None:
"""Initialize the entries.""" """Initialize the entries."""
coordinator: LastFMDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id] coordinator = entry.runtime_data
async_add_entities( async_add_entities(
( (
LastFmSensor(coordinator, username, entry.entry_id) LastFmSensor(coordinator, username, entry.entry_id)