Skip to content

Commit 42cbeca

Browse files
authored
Remove old roborock map storage (home-assistant#157379)
1 parent ad0a498 commit 42cbeca

File tree

3 files changed

+62
-10
lines changed

3 files changed

+62
-10
lines changed

homeassistant/components/roborock/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
RoborockWashingMachineUpdateCoordinator,
3434
RoborockWetDryVacUpdateCoordinator,
3535
)
36-
from .roborock_storage import CacheStore, async_remove_map_storage
36+
from .roborock_storage import CacheStore, async_cleanup_map_storage
3737

3838
SCAN_INTERVAL = timedelta(seconds=30)
3939

@@ -42,6 +42,7 @@
4242

4343
async def async_setup_entry(hass: HomeAssistant, entry: RoborockConfigEntry) -> bool:
4444
"""Set up roborock from a config entry."""
45+
await async_cleanup_map_storage(hass, entry.entry_id)
4546

4647
user_data = UserData.from_dict(entry.data[CONF_USER_DATA])
4748
user_params = UserParams(
@@ -245,6 +246,5 @@ async def async_unload_entry(hass: HomeAssistant, entry: RoborockConfigEntry) ->
245246

246247
async def async_remove_entry(hass: HomeAssistant, entry: RoborockConfigEntry) -> None:
247248
"""Handle removal of an entry."""
248-
await async_remove_map_storage(hass, entry.entry_id)
249249
store = CacheStore(hass, entry.entry_id)
250250
await store.async_remove()

homeassistant/components/roborock/roborock_storage.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,22 @@ def _storage_path_prefix(hass: HomeAssistant, entry_id: str) -> Path:
2525
return Path(hass.config.path(STORAGE_PATH)) / entry_id
2626

2727

28-
async def async_remove_map_storage(hass: HomeAssistant, entry_id: str) -> None:
29-
"""Remove all map storage associated with a config entry.
28+
async def async_cleanup_map_storage(hass: HomeAssistant, entry_id: str) -> None:
29+
"""Remove map storage in the old format, if any.
3030
3131
This removes all on-disk map files for the given config entry. This is the
3232
old format that was replaced by the `CacheStore` implementation.
3333
"""
3434

3535
def remove(path_prefix: Path) -> None:
3636
try:
37-
if path_prefix.exists():
37+
if path_prefix.exists() and path_prefix.is_dir():
38+
_LOGGER.debug("Removing maps from disk store: %s", path_prefix)
3839
shutil.rmtree(path_prefix, ignore_errors=True)
3940
except OSError as err:
4041
_LOGGER.error("Unable to remove map files in %s: %s", path_prefix, err)
4142

4243
path_prefix = _storage_path_prefix(hass, entry_id)
43-
_LOGGER.debug("Removing maps from disk store: %s", path_prefix)
4444
await hass.async_add_executor_job(remove, path_prefix)
4545

4646

tests/components/roborock/test_init.py

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,25 +52,77 @@ async def test_reauth_started(
5252

5353

5454
@pytest.mark.parametrize("platforms", [[Platform.IMAGE]])
55-
async def test_oserror_remove_image(
55+
@pytest.mark.parametrize(
56+
("exists", "is_dir", "rmtree_called"),
57+
[
58+
(True, True, True),
59+
(False, False, False),
60+
(True, False, False),
61+
],
62+
ids=[
63+
"old_storage_removed",
64+
"new_storage_ignored",
65+
"no_existing_storage",
66+
],
67+
)
68+
async def test_remove_old_storage_directory(
5669
hass: HomeAssistant,
57-
setup_entry: MockConfigEntry,
70+
mock_roborock_entry: MockConfigEntry,
5871
storage_path: pathlib.Path,
5972
hass_client: ClientSessionGenerator,
6073
caplog: pytest.LogCaptureFixture,
74+
exists: bool,
75+
is_dir: bool,
76+
rmtree_called: bool,
6177
) -> None:
62-
"""Test that we gracefully handle failing to remove old map storage."""
78+
"""Test cleanup of old old map storage."""
79+
with (
80+
patch(
81+
"homeassistant.components.roborock.roborock_storage.Path.exists",
82+
return_value=exists,
83+
),
84+
patch(
85+
"homeassistant.components.roborock.roborock_storage.Path.is_dir",
86+
return_value=is_dir,
87+
),
88+
patch(
89+
"homeassistant.components.roborock.roborock_storage.shutil.rmtree",
90+
) as mock_rmtree,
91+
):
92+
await hass.config_entries.async_setup(mock_roborock_entry.entry_id)
93+
await hass.async_block_till_done()
94+
assert mock_roborock_entry.state is ConfigEntryState.LOADED
6395

96+
assert mock_rmtree.called == rmtree_called
97+
98+
99+
@pytest.mark.parametrize("platforms", [[Platform.IMAGE]])
100+
async def test_oserror_remove_storage_directory(
101+
hass: HomeAssistant,
102+
mock_roborock_entry: MockConfigEntry,
103+
storage_path: pathlib.Path,
104+
hass_client: ClientSessionGenerator,
105+
caplog: pytest.LogCaptureFixture,
106+
) -> None:
107+
"""Test that we gracefully handle failing to remove old map storage."""
64108
with (
65109
patch(
66110
"homeassistant.components.roborock.roborock_storage.Path.exists",
111+
return_value=True,
112+
),
113+
patch(
114+
"homeassistant.components.roborock.roborock_storage.Path.is_dir",
115+
return_value=True,
67116
),
68117
patch(
69118
"homeassistant.components.roborock.roborock_storage.shutil.rmtree",
70119
side_effect=OSError,
71120
) as mock_rmtree,
72121
):
73-
await hass.config_entries.async_remove(setup_entry.entry_id)
122+
await hass.config_entries.async_setup(mock_roborock_entry.entry_id)
123+
await hass.async_block_till_done()
124+
assert mock_roborock_entry.state is ConfigEntryState.LOADED
125+
74126
assert mock_rmtree.called
75127
assert "Unable to remove map files" in caplog.text
76128

0 commit comments

Comments
 (0)