From c58b30d8ce7a5162db48aa17caea8587b0f8d3c1 Mon Sep 17 00:00:00 2001 From: Allen Porter Date: Fri, 30 Jun 2023 04:34:33 +0000 Subject: [PATCH 1/2] Delete calendar store when removing the config entry --- .../components/local_calendar/__init__.py | 7 +++++++ .../components/local_calendar/store.py | 9 +++++++++ tests/components/local_calendar/test_init.py | 17 +++++++++++++++++ 3 files changed, 33 insertions(+) create mode 100644 tests/components/local_calendar/test_init.py diff --git a/homeassistant/components/local_calendar/__init__.py b/homeassistant/components/local_calendar/__init__.py index 33ad67cc81a6ef..b5668ef34c8dea 100644 --- a/homeassistant/components/local_calendar/__init__.py +++ b/homeassistant/components/local_calendar/__init__.py @@ -39,3 +39,10 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: hass.data[DOMAIN].pop(entry.entry_id) return unload_ok + + +async def async_remove_entry(hass: HomeAssistant, entry: ConfigEntry) -> None: + """Handle removal of an entry.""" + store = hass.data[DOMAIN].get(entry.entry_id) + if store: + await store.async_unlink() diff --git a/homeassistant/components/local_calendar/store.py b/homeassistant/components/local_calendar/store.py index 3955717a06616a..499841998e75f3 100644 --- a/homeassistant/components/local_calendar/store.py +++ b/homeassistant/components/local_calendar/store.py @@ -36,3 +36,12 @@ async def async_store(self, ics_content: str) -> None: def _store(self, ics_content: str) -> None: """Persist the calendar to storage.""" self._path.write_text(ics_content) + + async def async_unlink(self) -> None: + """Remove the calendar storage.""" + async with self._lock: + await self._hass.async_add_executor_job(self._unlink) + + def _unlink(self) -> None: + """Remove the calendar to storage.""" + self._path.unlink() diff --git a/tests/components/local_calendar/test_init.py b/tests/components/local_calendar/test_init.py new file mode 100644 index 00000000000000..f97ccb7249c5a4 --- /dev/null +++ b/tests/components/local_calendar/test_init.py @@ -0,0 +1,17 @@ +"""Tests for init platform of local calendar.""" + +from unittest.mock import patch + +from homeassistant.core import HomeAssistant + +from tests.common import MockConfigEntry + + +async def test_remove_config_entry( + hass: HomeAssistant, setup_integration: None, config_entry: MockConfigEntry +) -> None: + """Test removing a config entry.""" + + with patch("homeassistant.components.local_calendar.Path.unlink"): + assert await hass.config_entries.async_remove(config_entry.entry_id) + await hass.async_block_till_done() From b247cedc42f43457d7c6add8f18b90a506eb9891 Mon Sep 17 00:00:00 2001 From: Allen Porter Date: Fri, 30 Jun 2023 04:47:34 +0000 Subject: [PATCH 2/2] Unlink file on remove with tests --- homeassistant/components/local_calendar/__init__.py | 10 +++++++--- homeassistant/components/local_calendar/store.py | 9 --------- tests/components/local_calendar/test_init.py | 3 ++- 3 files changed, 9 insertions(+), 13 deletions(-) diff --git a/homeassistant/components/local_calendar/__init__.py b/homeassistant/components/local_calendar/__init__.py index b5668ef34c8dea..7c1d2f09b04b80 100644 --- a/homeassistant/components/local_calendar/__init__.py +++ b/homeassistant/components/local_calendar/__init__.py @@ -43,6 +43,10 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: async def async_remove_entry(hass: HomeAssistant, entry: ConfigEntry) -> None: """Handle removal of an entry.""" - store = hass.data[DOMAIN].get(entry.entry_id) - if store: - await store.async_unlink() + key = slugify(entry.data[CONF_CALENDAR_NAME]) + path = Path(hass.config.path(STORAGE_PATH.format(key=key))) + + def unlink(path: Path) -> None: + path.unlink(missing_ok=True) + + await hass.async_add_executor_job(unlink, path) diff --git a/homeassistant/components/local_calendar/store.py b/homeassistant/components/local_calendar/store.py index 499841998e75f3..3955717a06616a 100644 --- a/homeassistant/components/local_calendar/store.py +++ b/homeassistant/components/local_calendar/store.py @@ -36,12 +36,3 @@ async def async_store(self, ics_content: str) -> None: def _store(self, ics_content: str) -> None: """Persist the calendar to storage.""" self._path.write_text(ics_content) - - async def async_unlink(self) -> None: - """Remove the calendar storage.""" - async with self._lock: - await self._hass.async_add_executor_job(self._unlink) - - def _unlink(self) -> None: - """Remove the calendar to storage.""" - self._path.unlink() diff --git a/tests/components/local_calendar/test_init.py b/tests/components/local_calendar/test_init.py index f97ccb7249c5a4..e5ca209e8a667e 100644 --- a/tests/components/local_calendar/test_init.py +++ b/tests/components/local_calendar/test_init.py @@ -12,6 +12,7 @@ async def test_remove_config_entry( ) -> None: """Test removing a config entry.""" - with patch("homeassistant.components.local_calendar.Path.unlink"): + with patch("homeassistant.components.local_calendar.Path.unlink") as unlink_mock: assert await hass.config_entries.async_remove(config_entry.entry_id) await hass.async_block_till_done() + unlink_mock.assert_called_once()