Thanks to visit codestin.com
Credit goes to github.com

Skip to content

A Home Assistant integration for Microsoft OneDrive for Business (M365). Connect and automate with your work/school cloud storage

License

Notifications You must be signed in to change notification settings

ghostinator/hass-onedrive-m365

Repository files navigation

Home Assistant - Microsoft 365 OneDrive Integration

hass-onedrive-m365logo hacs_badge

A comprehensive Home Assistant integration for Microsoft 365 OneDrive for Business. This component allows you to seamlessly use your OneDrive account as a native backup location and monitor your cloud storage.

Features

  • Native Backup Target: "Microsoft 365 OneDrive" appears directly in the Home Assistant backup UI, allowing you to create backups and send them to the cloud in one click.
  • Full Backup Management: Complete backup lifecycle support including upload, download, list, and delete operations.
  • Protected Backup Support: Backups are properly marked as encrypted/protected, maintaining security standards.
  • Automatic Cleanup: Configurable automatic deletion of old backups to manage storage space.
  • Automation-Friendly Service: Includes a service (hass-onedrive-m365.upload_latest_backup) to upload your most recent local backup, perfect for use in advanced automations.
  • Large File Support: Automatically uses resumable, chunked uploads for large backup files (>4MB).
  • Storage Monitoring: Includes a sensor entity to monitor your OneDrive storage usage.
  • Metadata Preservation: Maintains complete backup metadata including addons, folders, and Home Assistant version information.
  • Robust Error Handling: Comprehensive error handling and logging for troubleshooting.

Requirements

  • Home Assistant 2024.1.0 or newer (for backup agent support)
  • A Microsoft 365 account with OneDrive for Business
  • An Azure AD application with appropriate permissions

Installation

Via HACS (Recommended)

  1. Open HACS in your Home Assistant instance
  2. Click on "Integrations"
  3. Click the three dots menu in the top right and select "Custom repositories"
  4. Add this repository URL: https://github.com/ghostinator/hass-onedrive-m365
  5. Select "Integration" as the category
  6. Click "Add"
  7. Search for "Microsoft 365 OneDrive" and install it
  8. Restart Home Assistant

Manual Installation

  1. Download the latest release from GitHub
  2. Extract the hass-onedrive-m365 folder to your custom_components directory
  3. Restart Home Assistant

Configuration

Azure AD Application Setup

  1. Go to Azure Portal
  2. Navigate to "Azure Active Directory" → "App registrations"
  3. Click "New registration"
  4. Name your application (e.g., "Home Assistant OneDrive Backup")
  5. Select "Accounts in this organizational directory only"
  6. Set redirect URI to: https://login.microsoftonline.com/common/oauth2/nativeclient
  7. After creation, note down:
    • Application (client) ID
    • Directory (tenant) ID
  8. Go to "Certificates & secrets" → "New client secret"
  9. Create a secret and note down the value
  10. Go to "API permissions" → "Add a permission"
  11. Select "Microsoft Graph" → "Delegated permissions"
  12. Add these permissions:
    • Files.ReadWrite.All
    • User.Read.All
  13. Click "Grant admin consent" (may require admin privileges)

Home Assistant Configuration

  1. Go to Settings → Devices & Services
  2. Click "Add Integration"
  3. Search for "Microsoft 365 OneDrive"
  4. Enter the required information:
    • Client ID (from Azure AD app)
    • Client Secret (from Azure AD app)
    • Tenant ID (from Azure AD)
    • User Principal Name (your Microsoft 365 email)
  5. Click Submit

Usage

Creating Backups

  1. Go to Settings → System → Backups
  2. Click "Create backup"
  3. Select "Microsoft 365 OneDrive" as the location
  4. Configure your backup options
  5. Click "Create"

The integration will:

  • Stream the backup directly to OneDrive
  • Preserve all metadata (addons, folders, Home Assistant version)
  • Mark the backup as protected/encrypted
  • Automatically clean up old backups if configured

Restoring Backups

  1. Go to Settings → System → Backups
  2. Your OneDrive backups will appear in the list marked as "Protected"
  3. Click on a backup to view details including:
    • Backup size and date
    • Included addons and folders
    • Home Assistant version
  4. Click "Restore" to restore from that backup

Automation Service

Use the hass-onedrive-m365.upload_latest_backup service in your automations:

automation:
  - alias: "Daily Backup to OneDrive"
    trigger:
      - platform: time
        at: "03:00:00"
    action:
      - service: hass-onedrive-m365.upload_latest_backup

Debug Services

The integration includes several debug services for troubleshooting:

  • hass-onedrive-m365.debug_backup_agents: Test backup agent discovery
  • hass-onedrive-m365.debug_list_backups: Test backup listing functionality
  • hass-onedrive-m365.debug_test_backup_roundtrip: Test complete backup upload/download cycle

Configuration Options

After setup, you can configure these options:

  • Backup Folder: The OneDrive folder where backups are stored (default: "Home Assistant Backups")
  • Max Backups: Maximum number of backups to keep (default: 10, range: 1-100)
  • Auto Cleanup: Automatically delete old backups when the limit is reached (default: enabled)

Entities

Sensor

  • sensor.microsoft_365_onedrive_storage: Shows OneDrive storage usage
    • State: Used storage in GB
    • Attributes:
      • total_storage_gb: Total available storage
      • used_storage_gb: Used storage
      • free_storage_gb: Free storage
      • usage_percentage: Percentage of storage used
      • owner_name: Account owner's display name

Technical Details

File Handling

  • Small Files (<4MB): Direct upload to OneDrive
  • Large Files (≥4MB): Resumable upload sessions with chunked transfer
  • Download: Streaming download with 8KB chunks for memory efficiency
  • Validation: File size verification and integrity checks

Metadata Management

The integration maintains a local metadata cache to improve performance:

  • Stores backup metadata locally for faster listing
  • Syncs with remote backups to remove orphaned entries
  • Preserves complete Home Assistant backup structure

Security

  • Backups are marked as protected/encrypted by default
  • Uses Microsoft Graph API with proper OAuth2 authentication
  • Supports token refresh and error recovery
  • No sensitive data stored in plain text

Troubleshooting

"Home Assistant or database must be included in restore" Error

This error occurs when the backup metadata is missing required information. The integration now automatically ensures all backups include the necessary metadata for restoration. If you encounter this with older backups:

  1. Create a new backup using the updated integration
  2. Use the new backup for restoration
  3. Older backups may need to be deleted and recreated

Authentication Issues

If you encounter authentication errors:

  1. Verify your Azure AD app permissions are correctly configured
  2. Ensure admin consent has been granted for the permissions
  3. Check that your client secret hasn't expired
  4. Try removing and re-adding the integration

Upload Failures

For upload issues:

  1. Check your OneDrive storage quota
  2. Verify network connectivity
  3. Check Home Assistant logs for detailed error messages
  4. Ensure the backup file isn't corrupted
  5. For large files, ensure stable internet connection for resumable uploads

Download Issues

If backups fail to download or appear corrupted:

  1. Check the backup file size in OneDrive matches the expected size
  2. Verify network stability during download
  3. Check Home Assistant logs for streaming errors
  4. Try downloading again - the integration supports resumable downloads

Debug Logging

Enable debug logging for more detailed information:

logger:
  default: info
  logs:
    custom_components.hass-onedrive-m365: debug

This will provide detailed information about:

  • Authentication and token management
  • File upload/download progress
  • Metadata extraction and processing
  • Error conditions and recovery attempts

Known Limitations

  • Requires Home Assistant 2024.1.0+ for full backup agent functionality
  • OneDrive for Business accounts only (personal OneDrive not supported)
  • Maximum backup size limited to 2GB (configurable safety limit)
  • Requires stable internet connection for large file transfers

Support

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Support and Donations

If you find this integration helpful and would like to support further development or simply show your appreciation, you can use the following donation addresses:

PayPal: [email protected]
SOL: AUyf3DxRL9uKWf7ePEjY5GhSL3XJtAqQZ2w7kUMLi1xS
ETH, Monad, Base, Polygon: 0x521E4D25212180B5f8A9f0261D00835EA3bd7fa6
Sui: 0x89844c9e0c509115e8d2747b02d230ceb2bf96a1246707416177457d0e1bb593e
Bitcoin: bc1qkljmuw3mexksnk0wg06p35xvhpkakgnhyp63la

About

A Home Assistant integration for Microsoft OneDrive for Business (M365). Connect and automate with your work/school cloud storage

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages