mirror of
https://github.com/home-assistant/core.git
synced 2026-12-02 19:57:03 -04:00
Store hub configuration file under .storage for Harmony (#192442)
This commit is contained in:
@@ -108,6 +108,25 @@ async def _update_listener(hass: HomeAssistant, entry: HarmonyConfigEntry) -> No
|
||||
)
|
||||
|
||||
|
||||
def _move_legacy_config_file(hass: HomeAssistant, unique_id: str | None) -> None:
|
||||
"""Move the hub configuration file from the config root into .storage."""
|
||||
old_file = Path(hass.config.path(f"harmony_{unique_id}.conf"))
|
||||
if not old_file.exists():
|
||||
return
|
||||
new_file = Path(config_file_path(hass, unique_id))
|
||||
new_file.parent.mkdir(parents=True, exist_ok=True)
|
||||
old_file.replace(new_file)
|
||||
|
||||
|
||||
async def async_migrate_entry(hass: HomeAssistant, entry: HarmonyConfigEntry) -> None:
|
||||
"""Migrate old entry."""
|
||||
if entry.version == 1 and entry.minor_version < 2:
|
||||
await hass.async_add_executor_job(
|
||||
_move_legacy_config_file, hass, entry.unique_id
|
||||
)
|
||||
hass.config_entries.async_update_entry(entry, minor_version=2)
|
||||
|
||||
|
||||
async def async_unload_entry(hass: HomeAssistant, entry: HarmonyConfigEntry) -> None:
|
||||
"""Unload a config entry."""
|
||||
try:
|
||||
|
||||
@@ -64,6 +64,7 @@ class HarmonyConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
"""Handle a config flow for Logitech Harmony Hub."""
|
||||
|
||||
VERSION = 1
|
||||
MINOR_VERSION = 2
|
||||
|
||||
def __init__(self) -> None:
|
||||
"""Initialize the Harmony config flow."""
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
from collections.abc import Iterable
|
||||
import json
|
||||
import logging
|
||||
from pathlib import Path
|
||||
from typing import Any, override
|
||||
|
||||
from homeassistant.components.remote import (
|
||||
@@ -259,6 +260,7 @@ class HarmonyRemote(HarmonyEntity, RemoteEntity, RestoreEntity):
|
||||
return
|
||||
|
||||
try:
|
||||
Path(self._config_path).parent.mkdir(parents=True, exist_ok=True)
|
||||
with open(self._config_path, "w+", encoding="utf-8") as file_out:
|
||||
json.dump(json_config, file_out, sort_keys=True, indent=4)
|
||||
except OSError as exc:
|
||||
|
||||
@@ -5,6 +5,9 @@ from aioharmony.harmonyapi import HarmonyAPI
|
||||
|
||||
from homeassistant.const import CONF_NAME
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.storage import STORAGE_DIR
|
||||
|
||||
from .const import DOMAIN
|
||||
|
||||
|
||||
def find_unique_id_for_remote(harmony: HarmonyAPI):
|
||||
@@ -18,7 +21,7 @@ def find_unique_id_for_remote(harmony: HarmonyAPI):
|
||||
|
||||
def config_file_path(hass: HomeAssistant, unique_id: str | None) -> str:
|
||||
"""Return the path the hub configuration is written to."""
|
||||
return hass.config.path(f"harmony_{unique_id}.conf")
|
||||
return hass.config.path(STORAGE_DIR, DOMAIN, f"harmony_{unique_id}.conf")
|
||||
|
||||
|
||||
def find_best_name_for_remote(data: dict, harmony: HarmonyAPI):
|
||||
|
||||
@@ -198,4 +198,5 @@ def mock_config_entry() -> MockConfigEntry:
|
||||
domain=DOMAIN,
|
||||
unique_id="123",
|
||||
data={CONF_HOST: "192.0.2.0", CONF_NAME: HUB_NAME},
|
||||
minor_version=2,
|
||||
)
|
||||
|
||||
@@ -19,6 +19,7 @@ from homeassistant.core import HomeAssistant
|
||||
from homeassistant.data_entry_flow import FlowResultType
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
from homeassistant.helpers.service_info.ssdp import SsdpServiceInfo
|
||||
from homeassistant.helpers.storage import STORAGE_DIR
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from .conftest import FakeHarmonyClient
|
||||
@@ -33,6 +34,9 @@ from .const import (
|
||||
WATCH_TV_ACTIVITY_ID,
|
||||
)
|
||||
|
||||
OLD_CONFIG_FILE = "harmony_123.conf"
|
||||
NEW_CONFIG_FILE = f"{STORAGE_DIR}/{DOMAIN}/harmony_123.conf"
|
||||
|
||||
|
||||
async def test_unique_id_migration(
|
||||
mock_hc, hass: HomeAssistant, mock_write_config
|
||||
@@ -195,7 +199,8 @@ async def test_remove_entry_removes_config_file(
|
||||
) -> None:
|
||||
"""Test removing the entry removes the hub configuration file."""
|
||||
hass.config.config_dir = str(tmp_path)
|
||||
config_file = tmp_path / "harmony_123.conf"
|
||||
config_file = tmp_path / NEW_CONFIG_FILE
|
||||
config_file.parent.mkdir(parents=True)
|
||||
config_file.write_text("{}")
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
|
||||
@@ -203,3 +208,41 @@ async def test_remove_entry_removes_config_file(
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert not config_file.exists()
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("mock_hc", "mock_write_config")
|
||||
async def test_migrate_config_file_to_storage(
|
||||
hass: HomeAssistant, mock_config_entry: MockConfigEntry, tmp_path: Path
|
||||
) -> None:
|
||||
"""Test the hub configuration file is moved from the config root into .storage."""
|
||||
hass.config.config_dir = str(tmp_path)
|
||||
(tmp_path / OLD_CONFIG_FILE).write_text("{}")
|
||||
config_entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
unique_id=mock_config_entry.unique_id,
|
||||
data=mock_config_entry.data,
|
||||
minor_version=1,
|
||||
)
|
||||
config_entry.add_to_hass(hass)
|
||||
|
||||
assert await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
|
||||
assert config_entry.state is ConfigEntryState.LOADED
|
||||
assert config_entry.minor_version == 2
|
||||
assert not (tmp_path / OLD_CONFIG_FILE).exists()
|
||||
assert (tmp_path / NEW_CONFIG_FILE).read_text() == "{}"
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("mock_hc")
|
||||
async def test_config_file_written_to_storage(
|
||||
hass: HomeAssistant, mock_config_entry: MockConfigEntry, tmp_path: Path
|
||||
) -> None:
|
||||
"""Test the hub configuration file is written into .storage."""
|
||||
hass.config.config_dir = str(tmp_path)
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
|
||||
assert await hass.config_entries.async_setup(mock_config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert (tmp_path / NEW_CONFIG_FILE).exists()
|
||||
assert not (tmp_path / OLD_CONFIG_FILE).exists()
|
||||
|
||||
Reference in New Issue
Block a user