mirror of
https://github.com/home-assistant/core.git
synced 2025-07-22 20:57:21 +00:00
Migrate lastfm to use runtime_data (#147330)
This commit is contained in:
parent
10c573bbc3
commit
69d2cd0ac0
@ -2,19 +2,18 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from .const import DOMAIN, PLATFORMS
|
||||
from .coordinator import LastFMDataUpdateCoordinator
|
||||
from .const import PLATFORMS
|
||||
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."""
|
||||
|
||||
coordinator = LastFMDataUpdateCoordinator(hass, entry)
|
||||
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)
|
||||
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
|
||||
|
||||
|
||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
async def async_unload_entry(hass: HomeAssistant, entry: LastFMConfigEntry) -> bool:
|
||||
"""Unload lastfm config entry."""
|
||||
|
||||
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."""
|
||||
await hass.config_entries.async_reload(entry.entry_id)
|
||||
|
@ -8,12 +8,7 @@ from typing import Any
|
||||
from pylast import LastFMNetwork, PyLastError, User, WSError
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.config_entries import (
|
||||
ConfigEntry,
|
||||
ConfigFlow,
|
||||
ConfigFlowResult,
|
||||
OptionsFlow,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult, OptionsFlow
|
||||
from homeassistant.const import CONF_API_KEY
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.helpers.selector import (
|
||||
@ -23,6 +18,7 @@ from homeassistant.helpers.selector import (
|
||||
)
|
||||
|
||||
from .const import CONF_MAIN_USER, CONF_USERS, DOMAIN
|
||||
from .coordinator import LastFMConfigEntry
|
||||
|
||||
PLACEHOLDERS = {"api_account_url": "https://www.last.fm/api/account/create"}
|
||||
|
||||
@ -81,7 +77,7 @@ class LastFmConfigFlowHandler(ConfigFlow, domain=DOMAIN):
|
||||
@staticmethod
|
||||
@callback
|
||||
def async_get_options_flow(
|
||||
config_entry: ConfigEntry,
|
||||
config_entry: LastFMConfigEntry,
|
||||
) -> LastFmOptionsFlowHandler:
|
||||
"""Get the options flow for this handler."""
|
||||
return LastFmOptionsFlowHandler()
|
||||
@ -162,6 +158,8 @@ class LastFmConfigFlowHandler(ConfigFlow, domain=DOMAIN):
|
||||
class LastFmOptionsFlowHandler(OptionsFlow):
|
||||
"""LastFm Options flow handler."""
|
||||
|
||||
config_entry: LastFMConfigEntry
|
||||
|
||||
async def async_step_init(
|
||||
self, user_input: dict[str, Any] | None = None
|
||||
) -> ConfigFlowResult:
|
||||
|
@ -14,6 +14,8 @@ from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, Upda
|
||||
|
||||
from .const import CONF_USERS, DOMAIN, LOGGER
|
||||
|
||||
type LastFMConfigEntry = ConfigEntry[LastFMDataUpdateCoordinator]
|
||||
|
||||
|
||||
def format_track(track: Track | None) -> str | None:
|
||||
"""Format the track."""
|
||||
|
@ -6,7 +6,6 @@ import hashlib
|
||||
from typing import Any
|
||||
|
||||
from homeassistant.components.sensor import SensorEntity
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
|
||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||
@ -21,17 +20,17 @@ from .const import (
|
||||
DOMAIN,
|
||||
STATE_NOT_SCROBBLING,
|
||||
)
|
||||
from .coordinator import LastFMDataUpdateCoordinator, LastFMUserData
|
||||
from .coordinator import LastFMConfigEntry, LastFMDataUpdateCoordinator, LastFMUserData
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
entry: ConfigEntry,
|
||||
entry: LastFMConfigEntry,
|
||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||
) -> None:
|
||||
"""Initialize the entries."""
|
||||
|
||||
coordinator: LastFMDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
||||
coordinator = entry.runtime_data
|
||||
async_add_entities(
|
||||
(
|
||||
LastFmSensor(coordinator, username, entry.entry_id)
|
||||
|
Loading…
x
Reference in New Issue
Block a user