Skip to content

Migrate Hikvision integration to config flow#158279

Merged
joostlek merged 31 commits into
home-assistant:devfrom
ptarjan:claude/migrate-hikvision-integration-012bm7PWpikgSddSEixptrqe
Dec 16, 2025
Merged

Migrate Hikvision integration to config flow#158279
joostlek merged 31 commits into
home-assistant:devfrom
ptarjan:claude/migrate-hikvision-integration-012bm7PWpikgSddSEixptrqe

Conversation

@ptarjan
Copy link
Copy Markdown
Contributor

@ptarjan ptarjan commented Dec 8, 2025

Migrate the Hikvision integration from a legacy YAML-based integration to the modern config flow-based system. In the process of this migration I was able to fix the creation of my binary sensors. Now I have:

image

Breaking change

Users with existing YAML configuration will see a deprecation message.

Proposed change

  • Add config_flow.py for UI-based configuration
  • Add const.py with domain and platform constants
  • Modernize binary_sensor.py with:
    • Entity names using has_entity_name and translations
    • Device registry support with DeviceInfo
    • Proper callback registration in async_added_to_hass
    • PARALLEL_UPDATES set for push-based updates
  • Add strings.json for config flow and entity translations
  • Add quality_scale.yaml targeting Bronze tier
  • Update manifest.json with config_flow, integration_type, and quality_scale
  • Add comprehensive test coverage for:
    • Config flow (user setup, reauth, error handling)
    • Binary sensors (creation, attributes, device classes)
    • Integration setup/unload
  • Remove hikvision from INTEGRATIONS_WITHOUT_QUALITY_SCALE_FILE

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

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue:
  • Link to documentation pull request:
  • Link to developer documentation pull request:
  • Link to frontend pull request:

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:

@ptarjan ptarjan requested a review from a team as a code owner December 8, 2025 21:53
Copy link
Copy Markdown
Contributor

@home-assistant home-assistant Bot left a comment

Choose a reason for hiding this comment

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

Hi @claude

It seems you haven't yet signed a CLA. Please do so here.

Once you do that we will be able to review and accept this pull request.

Thanks!

@home-assistant home-assistant Bot added breaking-change cla-needed config-flow This integration migrates to the UI by adding a config flow has-tests integration: hikvision labels Dec 8, 2025
@home-assistant home-assistant Bot marked this pull request as draft December 8, 2025 21:53
@home-assistant
Copy link
Copy Markdown
Contributor

home-assistant Bot commented Dec 8, 2025

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.

@home-assistant
Copy link
Copy Markdown
Contributor

home-assistant Bot commented Dec 8, 2025

Hey there @mezz64, mind taking a look at this pull request as it has been labeled with an integration (hikvision) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of hikvision can trigger bot actions by commenting:

  • @home-assistant close Closes the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign hikvision Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component) on the pull request.

@ptarjan ptarjan force-pushed the claude/migrate-hikvision-integration-012bm7PWpikgSddSEixptrqe branch from 8a0a487 to 24dcd68 Compare December 8, 2025 21:54
@ptarjan ptarjan marked this pull request as ready for review December 8, 2025 21:54
@ptarjan ptarjan force-pushed the claude/migrate-hikvision-integration-012bm7PWpikgSddSEixptrqe branch from 64ac443 to c78d95d Compare December 8, 2025 22:12
Copy link
Copy Markdown
Contributor

@home-assistant home-assistant Bot left a comment

Choose a reason for hiding this comment

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

Hi @claude

It seems you haven't yet signed a CLA. Please do so here.

Once you do that we will be able to review and accept this pull request.

Thanks!

@home-assistant home-assistant Bot marked this pull request as draft December 8, 2025 22:29
@ptarjan ptarjan force-pushed the claude/migrate-hikvision-integration-012bm7PWpikgSddSEixptrqe branch from d3eb10a to a18bf82 Compare December 8, 2025 22:31
Copy link
Copy Markdown
Contributor

@home-assistant home-assistant Bot left a comment

Choose a reason for hiding this comment

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

Hi @claude

It seems you haven't yet signed a CLA. Please do so here.

Once you do that we will be able to review and accept this pull request.

Thanks!

@ptarjan ptarjan force-pushed the claude/migrate-hikvision-integration-012bm7PWpikgSddSEixptrqe branch from a18bf82 to 2c1a048 Compare December 8, 2025 22:32
@ptarjan ptarjan force-pushed the claude/migrate-hikvision-integration-012bm7PWpikgSddSEixptrqe branch from 2c1a048 to 4e409e3 Compare December 8, 2025 22:54
Comment thread homeassistant/components/hikvision/manifest.json Outdated
Comment thread homeassistant/components/hikvision/helpers.py Outdated
ptarjan pushed a commit to ptarjan/pyHik that referenced this pull request Dec 8, 2025
These functions extend the event detection capabilities to support NVRs
with non-standard notification methods (record, email, beep) in addition
to the existing 'center' and 'HTTP' methods.

- get_nvr_events: Fetches events from NVR with broader notification support
- inject_events_into_camera: Injects discovered events into HikCamera
- HikCamera.inject_events: New method to inject events into camera's state

Based on home-assistant/core#158279
@ptarjan ptarjan force-pushed the claude/migrate-hikvision-integration-012bm7PWpikgSddSEixptrqe branch from 4e409e3 to 23332a7 Compare December 8, 2025 23:24
The entry object is only used to access runtime_data, so we don't
need to store the entry itself.
Use requests.exceptions.RequestException instead of bare Exception
for more precise error handling when connecting to Hikvision devices.
Remove the reauthentication flow as requested by reviewer.
This can be added in a follow-up PR.
Preserve the user-configured name from YAML configuration when
importing to config entry. Falls back to device name or host.
Use HOMEASSISTANT_DOMAIN for the successful YAML import deprecation
issue, matching the pattern used by other integrations like
nederlandse_spoorwegen. This uses the shared translation strings.
- Remove reauth flow tests (reauth removed for follow-up PR)
- Use requests.exceptions.RequestException instead of Exception
- Update deprecated_yaml issue tests to use HOMEASSISTANT_DOMAIN
- Merge mock_hikcamera and mock_hikcamera_config_flow into single fixture
- Patch both HikCamera locations using combined context manager pattern
- Remove unnecessary MagicMock assignments (autospec handles methods)
- Fix fetch_attributes to use return_value assignment syntax
- Use combined mock_hikcamera fixture
- Reuse result variable instead of result2, result3
- Remove unnecessary async_block_till_done() calls
- Add unique_id assertions to verify entry uniqueness
- Merge error tests to include recovery (end in CREATE_ENTRY)
- Merge test_setup_entry and test_unload_entry into single test
- Remove runtime_data internal assertions
- Remove test_setup_entry_no_device_id (implementation detail)
- Remove test_setup_entry_default_name/type tests
- Use issue_registry fixture instead of getting from hass
- Use combined mock_hikcamera fixture from conftest
- Consolidate entity tests (unique_id, attributes covered in main test)
Use snapshot_platform helper to capture entity registry and state
snapshots for all binary sensor entities.
- Import CONF_DELAY from homeassistant.const (existing constant)
- Define CONF_IGNORED, DEFAULT_DELAY, DEFAULT_IGNORED locally in
  binary_sensor.py to match original code structure
- Add test for device_id None case in async_setup_entry
- Add test for import flow when device_id is None
- Add test for YAML import abort creating issue
- Add test for binary sensor update callback
- Remove unused timer cancellation code (dead code from delay feature)
@ptarjan ptarjan force-pushed the claude/migrate-hikvision-integration-012bm7PWpikgSddSEixptrqe branch from fabbe62 to c11fb3f Compare December 9, 2025 23:28
@ptarjan
Copy link
Copy Markdown
Contributor Author

ptarjan commented Dec 12, 2025

Anything you need from me for this PR?

Copy link
Copy Markdown
Member

@joostlek joostlek left a comment

Choose a reason for hiding this comment

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

Awesome!

Will you also look into becoming codeowner? And maybe send me a message on Discord

@joostlek joostlek merged commit 4df5a41 into home-assistant:dev Dec 16, 2025
67 checks passed
@joostlek
Copy link
Copy Markdown
Member

@ptarjan The documentation also needs to be updated

@ptarjan ptarjan deleted the claude/migrate-hikvision-integration-012bm7PWpikgSddSEixptrqe branch December 17, 2025 07:16
heindrichpaul pushed a commit to heindrichpaul/core that referenced this pull request Dec 17, 2025
Co-authored-by: Kamil Breguła <mik-laj@users.noreply.github.com>
mezz64 added a commit to mezz64/pyHik that referenced this pull request Dec 18, 2025
* Add get_nvr_events and inject_events_into_camera functions

These functions extend the event detection capabilities to support NVRs
with non-standard notification methods (record, email, beep) in addition
to the existing 'center' and 'HTTP' methods.

- get_nvr_events: Fetches events from NVR with broader notification support
- inject_events_into_camera: Injects discovered events into HikCamera
- HikCamera.inject_events: New method to inject events into camera's state

Based on home-assistant/core#158279

* Add tests for get_nvr_events and inject_events_into_camera

Tests cover:
- Parsing NVR events with various notification methods (record, email, beep, center)
- Skipping videoloss events (used for watchdog)
- Handling connection errors gracefully
- Handling non-200 responses
- Handling invalid XML
- Injecting events into camera event_states
- Preventing duplicate channel events

* Refactor: Add notification_methods parameter to get_event_triggers

Instead of a separate get_nvr_events function, extend the existing
get_event_triggers method to accept a notification_methods parameter.
This allows users to specify which notification methods to accept
(defaults to {'center', 'HTTP'} for backwards compatibility).

For NVRs, users can pass VALID_NOTIFICATION_METHODS which includes
'record', 'email', and 'beep' in addition to the defaults.

Changes:
- Add notification_methods parameter to get_event_triggers()
- Remove duplicate get_nvr_events function
- Export VALID_NOTIFICATION_METHODS constant for easy use
- Update tests to cover new parameter functionality

---------

Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: mezz64 <2854333+mezz64@users.noreply.github.com>
@github-actions github-actions Bot locked and limited conversation to collaborators Dec 18, 2025
@home-assistant home-assistant unlocked this conversation Dec 25, 2025
device_id = camera.get_id()
device_name = camera.get_name
except requests.exceptions.RequestException:
_LOGGER.exception(
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We only log unknown exceptions including the stack trace.

Copy link
Copy Markdown
Member

@MartinHjelmare MartinHjelmare Dec 25, 2025

Choose a reason for hiding this comment

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

We normally catch broad exception too. Haven't you used our scaffold script to generate the config flow? It has a template to follow.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

No i never used the script, I'll check what it does and see if I missed anything. Thanks.

Comment thread homeassistant/components/hikvision/config_flow.py
Comment thread tests/components/hikvision/conftest.py
mettolen pushed a commit to mettolen/core that referenced this pull request Dec 26, 2025
Co-authored-by: Kamil Breguła <mik-laj@users.noreply.github.com>
@github-actions github-actions Bot locked and limited conversation to collaborators Dec 26, 2025
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.

5 participants