Skip to content

Commit

Permalink
Allow files to be directly deleted in onedrive (#138908)
Browse files Browse the repository at this point in the history
* Allow files to be directly deleted in onedrive

* let options flow reload

* update description
  • Loading branch information
zweckj authored Feb 20, 2025
1 parent 2d09679 commit 9f7c464
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 9 deletions.
5 changes: 5 additions & 0 deletions homeassistant/components/onedrive/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ async def get_access_token() -> str:
_async_notify_backup_listeners_soon(hass)
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)

async def update_listener(hass: HomeAssistant, entry: OneDriveConfigEntry) -> None:
await hass.config_entries.async_reload(entry.entry_id)

entry.async_on_unload(entry.add_update_listener(update_listener))

return True


Expand Down
10 changes: 7 additions & 3 deletions homeassistant/components/onedrive/backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.aiohttp_client import async_get_clientsession

from .const import DATA_BACKUP_AGENT_LISTENERS, DOMAIN
from .const import CONF_DELETE_PERMANENTLY, DATA_BACKUP_AGENT_LISTENERS, DOMAIN
from .coordinator import OneDriveConfigEntry

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -205,8 +205,12 @@ async def async_delete_backup(

backup = backups[backup_id]

await self._client.delete_drive_item(backup.backup_file_id)
await self._client.delete_drive_item(backup.metadata_file_id)
delete_permanently = self._entry.options.get(CONF_DELETE_PERMANENTLY, False)

await self._client.delete_drive_item(backup.backup_file_id, delete_permanently)
await self._client.delete_drive_item(
backup.metadata_file_id, delete_permanently
)
self._cache_expiration = time()

@handle_backup_errors
Expand Down
44 changes: 42 additions & 2 deletions homeassistant/components/onedrive/config_flow.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
"""Config flow for OneDrive."""

from __future__ import annotations

from collections.abc import Mapping
import logging
from typing import Any, cast

from onedrive_personal_sdk.clients.client import OneDriveClient
from onedrive_personal_sdk.exceptions import OneDriveException
import voluptuous as vol

from homeassistant.config_entries import SOURCE_REAUTH, ConfigFlowResult
from homeassistant.config_entries import SOURCE_REAUTH, ConfigFlowResult, OptionsFlow
from homeassistant.const import CONF_ACCESS_TOKEN, CONF_TOKEN
from homeassistant.core import callback
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.config_entry_oauth2_flow import AbstractOAuth2FlowHandler

from .const import DOMAIN, OAUTH_SCOPES
from .const import CONF_DELETE_PERMANENTLY, DOMAIN, OAUTH_SCOPES
from .coordinator import OneDriveConfigEntry


class OneDriveConfigFlow(AbstractOAuth2FlowHandler, domain=DOMAIN):
Expand Down Expand Up @@ -86,3 +91,38 @@ async def async_step_reauth_confirm(
if user_input is None:
return self.async_show_form(step_id="reauth_confirm")
return await self.async_step_user()

@staticmethod
@callback
def async_get_options_flow(
config_entry: OneDriveConfigEntry,
) -> OneDriveOptionsFlowHandler:
"""Create the options flow."""
return OneDriveOptionsFlowHandler()


class OneDriveOptionsFlowHandler(OptionsFlow):
"""Handles options flow for the component."""

async def async_step_init(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Manage the options for OneDrive."""
if user_input:
return self.async_create_entry(title="", data=user_input)

options_schema = vol.Schema(
{
vol.Optional(
CONF_DELETE_PERMANENTLY,
default=self.config_entry.options.get(
CONF_DELETE_PERMANENTLY, False
),
): bool,
}
)

return self.async_show_form(
step_id="init",
data_schema=options_schema,
)
2 changes: 2 additions & 0 deletions homeassistant/components/onedrive/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

DOMAIN: Final = "onedrive"

CONF_DELETE_PERMANENTLY: Final = "delete_permanently"

# replace "consumers" with "common", when adding SharePoint or OneDrive for Business support
OAUTH2_AUTHORIZE: Final = (
"https://login.microsoftonline.com/consumers/oauth2/v2.0/authorize"
Expand Down
5 changes: 1 addition & 4 deletions homeassistant/components/onedrive/quality_scale.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@ rules:
# Silver
action-exceptions: done
config-entry-unloading: done
docs-configuration-parameters:
status: exempt
comment: |
No Options flow.
docs-configuration-parameters: done
docs-installation-parameters: done
entity-unavailable: done
integration-owner: done
Expand Down
13 changes: 13 additions & 0 deletions homeassistant/components/onedrive/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@
"default": "[%key:common::config_flow::create_entry::authenticated%]"
}
},
"options": {
"step": {
"init": {
"description": "By default, files are put into the Recycle Bin when deleted, where they remain available for another 30 days. If you enable this option, files will be deleted immediately when they are cleaned up by the backup system.",
"data": {
"delete_permanently": "Delete files permanently"
},
"data_description": {
"delete_permanently": "Delete files without moving them to the Recycle Bin"
}
}
}
},
"issues": {
"drive_full": {
"title": "OneDrive data cap exceeded",
Expand Down
27 changes: 27 additions & 0 deletions tests/components/onedrive/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from homeassistant import config_entries
from homeassistant.components.onedrive.const import (
CONF_DELETE_PERMANENTLY,
DOMAIN,
OAUTH2_AUTHORIZE,
OAUTH2_TOKEN,
Expand Down Expand Up @@ -223,3 +224,29 @@ async def test_reauth_flow_id_changed(

assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "wrong_drive"


async def test_options_flow(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test options flow."""
await setup_integration(hass, mock_config_entry)

result = await hass.config_entries.options.async_init(mock_config_entry.entry_id)

assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "init"

result2 = await hass.config_entries.options.async_configure(
result["flow_id"],
user_input={
CONF_DELETE_PERMANENTLY: True,
},
)
await hass.async_block_till_done()

assert result2["type"] is FlowResultType.CREATE_ENTRY
assert result2["data"] == {
CONF_DELETE_PERMANENTLY: True,
}

0 comments on commit 9f7c464

Please sign in to comment.