|
13 | 13 | import aiohttp |
14 | 14 |
|
15 | 15 | from homeassistant.core import HomeAssistant |
| 16 | +from homeassistant.helpers.storage import Store |
16 | 17 | from homeassistant.util import slugify |
17 | 18 |
|
18 | 19 | _LOGGER = logging.getLogger(__name__) |
@@ -178,31 +179,28 @@ async def async_enrich_via_odesli( |
178 | 179 | } |
179 | 180 |
|
180 | 181 |
|
181 | | -async def async_save_credentials( |
182 | | - hass: HomeAssistant, client_id: str, client_secret: str |
183 | | -) -> None: |
184 | | - """Save Spotify credentials to disk.""" |
185 | | - creds_path = Path(hass.config.path("beatify/spotify_credentials.json")) |
| 182 | +_CREDS_STORE_KEY = "beatify.spotify_credentials" |
| 183 | +_CREDS_STORE_VERSION = 1 |
186 | 184 |
|
187 | | - def _write() -> None: |
188 | | - creds_path.parent.mkdir(parents=True, exist_ok=True) |
189 | | - creds_path.write_text( |
190 | | - json.dumps({"client_id": client_id, "client_secret": client_secret}, indent=2) |
191 | | - ) |
192 | 185 |
|
193 | | - await hass.async_add_executor_job(_write) |
| 186 | +def _get_store(hass: HomeAssistant) -> Store: |
| 187 | + """Get or create the HA Store for Spotify credentials.""" |
| 188 | + return Store(hass, _CREDS_STORE_VERSION, _CREDS_STORE_KEY) |
194 | 189 |
|
195 | 190 |
|
196 | | -async def async_load_credentials(hass: HomeAssistant) -> dict[str, str]: |
197 | | - """Load Spotify credentials from disk.""" |
198 | | - creds_path = Path(hass.config.path("beatify/spotify_credentials.json")) |
| 191 | +async def async_save_credentials( |
| 192 | + hass: HomeAssistant, client_id: str, client_secret: str |
| 193 | +) -> None: |
| 194 | + """Save Spotify credentials via HA storage.""" |
| 195 | + store = _get_store(hass) |
| 196 | + await store.async_save({"client_id": client_id, "client_secret": client_secret}) |
199 | 197 |
|
200 | | - def _read() -> dict[str, str]: |
201 | | - if not creds_path.exists(): |
202 | | - return {} |
203 | | - return json.loads(creds_path.read_text()) |
204 | 198 |
|
205 | | - return await hass.async_add_executor_job(_read) |
| 199 | +async def async_load_credentials(hass: HomeAssistant) -> dict[str, str]: |
| 200 | + """Load Spotify credentials from HA storage.""" |
| 201 | + store = _get_store(hass) |
| 202 | + data = await store.async_load() |
| 203 | + return data if data else {} |
206 | 204 |
|
207 | 205 |
|
208 | 206 | async def async_import_playlist( |
|
0 commit comments