Skip to content

Improve date handling in UniFi Protect media source#159491

Merged
joostlek merged 8 commits into
home-assistant:devfrom
RaHehl:improve-unifiprotect-date-handling
Dec 23, 2025
Merged

Improve date handling in UniFi Protect media source#159491
joostlek merged 8 commits into
home-assistant:devfrom
RaHehl:improve-unifiprotect-date-handling

Conversation

@RaHehl
Copy link
Copy Markdown
Contributor

@RaHehl RaHehl commented Dec 20, 2025

Breaking change

Proposed change

  • Replace manual month increment logic with calendar.monthrange()
  • Simplify _format_duration() using divmod pattern
  • Normalize _get_month_start_end() to use second=0
  • Fix month iteration to use >= instead of > for same-month handling
  • Add comprehensive tests for date calculations across all month types

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

Checklist

  • I understand the code I am submitting and can explain how it works.
  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • I have followed the perfect PR recommendations
  • The code has been formatted using Ruff (ruff format homeassistant tests)
  • Tests have been added to verify that the new code works.
  • Any generated code has been carefully reviewed for correctness and compliance with project standards.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.

To help with the load of incoming pull requests:

- Replace manual month increment logic with calendar.monthrange()
- Simplify _format_duration() using divmod pattern
- Normalize _get_month_start_end() to use second=0
- Fix month iteration to use >= instead of > for same-month handling
- Add comprehensive tests for date calculations across all month types
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves date handling in the UniFi Protect media source component by refactoring manual date arithmetic with cleaner, more maintainable approaches using Python's standard library functions.

Key Changes:

  • Replaces error-prone manual month increment logic with calendar.monthrange() for accurate month boundary calculations
  • Refactors _format_duration() to use the divmod pattern for cleaner time component extraction
  • Fixes month iteration logic to correctly handle same-month scenarios by changing comparison from > to >=

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
homeassistant/components/unifiprotect/media_source.py Implements date calculation improvements: adds monthrange import, simplifies _format_duration() with divmod, normalizes _get_month_start_end() to use second=0, fixes month end calculation in _build_month() and _build_days() using monthrange, and corrects same-month iteration logic
tests/components/unifiprotect/test_media_source.py Adds comprehensive parameterized test for date calculations covering various month types including leap years and December edge case, but contains a critical bug in the test implementation

Comment thread tests/components/unifiprotect/test_media_source.py Outdated
Comment thread tests/components/unifiprotect/test_media_source.py Outdated
@home-assistant home-assistant Bot marked this pull request as draft December 23, 2025 14:56
@home-assistant
Copy link
Copy Markdown
Contributor

Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍

Learn more about our pull request process.

@RaHehl RaHehl requested a review from joostlek December 23, 2025 15:47
@RaHehl RaHehl marked this pull request as ready for review December 23, 2025 15:48
@joostlek joostlek added this to the 2025.12.5 milestone Dec 23, 2025
@joostlek joostlek merged commit b3c78d4 into home-assistant:dev Dec 23, 2025
36 checks passed
@github-actions github-actions Bot locked and limited conversation to collaborators Dec 24, 2025
@RaHehl RaHehl deleted the improve-unifiprotect-date-handling branch January 22, 2026 21:14
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unify Protect - Media Folder December error

4 participants