Skip to content

Fix Proxmox VE sensor crashes due to missing API fields#168071

Open
irishpadres wants to merge 9 commits intohome-assistant:devfrom
irishpadres:fix-proxmox-sensors-robustness
Open

Fix Proxmox VE sensor crashes due to missing API fields#168071
irishpadres wants to merge 9 commits intohome-assistant:devfrom
irishpadres:fix-proxmox-sensors-robustness

Conversation

@irishpadres
Copy link
Copy Markdown
Contributor

@irishpadres irishpadres commented Apr 12, 2026

Proposed change

This PR improves robustness of the Proxmox VE integration by ensuring sensors handle missing or partial API fields gracefully rather than crashing or reporting incorrect state.

Architectural approach

All sensors now use `dict.get()` instead of direct key access. Rather than gating entity creation on the presence of API fields (which permanently prevents entity creation if the first coordinator refresh returns partial data), entities are always created and return `None` (reported as `STATE_UNKNOWN`) when data is absent.

Key changes:

  • All `value_fn` and `state_fn` accessors use `dict.get()` to safely handle missing fields
  • Node status binary sensor returns `None` (unknown) instead of `False` (offline) when `status` field is absent
  • Backup status binary sensor returns `False` (no problem) when there are no backups, and `None` (unknown) when a backup entry exists but its `status` field is missing
  • Storage binary sensors (active, enabled, shared) return `None` when their field is absent rather than `False`
  • Removed `exists_fn` from storage sensors — entities are always created and report `STATE_UNKNOWN` when fields are missing, avoiding permanent entity loss on partial first refresh
  • `ZeroDivisionError` guard added for memory percentage calculations
  • Test fixture updated with a real-world offline NFS storage entry (`active=0`, no `used_fraction`) matching actual Proxmox API responses
  • Comprehensive regression tests added covering missing-field scenarios

Note: snapshot tests will need regenerating in CI (`--snapshot-update`) due to the new storage fixture entry.

Type of change

  • Bugfix (non-breaking change which fixes an issue)
  • 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.
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • The code has been formatted using Ruff (`ruff format homeassistant tests`)
  • Tests have been added to verify that the new code works.

@irishpadres irishpadres requested a review from CoMPaTech as a code owner April 12, 2026 21:07
Copy link
Copy Markdown

@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.

Hello @irishpadres,

When attempting to inspect the commits of your pull request for CLA signature status among all authors we encountered commit(s) which were not linked to a GitHub account, thus not allowing us to determine their status(es).

The commits that are missing a linked GitHub account are the following:

Unfortunately, we are unable to accept this pull request until this situation is corrected.

Here are your options:

  1. If you had an email address set for the commit that simply wasn't linked to your GitHub account you can link that email now and it will retroactively apply to your commits. The simplest way to do this is to click the link to one of the above commits and look for a blue question mark in a blue circle in the top left. Hovering over that bubble will show you what email address you used. Clicking on that button will take you to your email address settings on GitHub. Just add the email address on that page and you're all set. GitHub has more information about this option in their help center.

  2. If you didn't use an email address at all, it was an invalid email, or it's one you can't link to your GitHub, you will need to change the authorship information of the commit and your global Git settings so this doesn't happen again going forward. GitHub provides some great instructions on how to change your authorship information in their help center.

    • If you only made a single commit you should be able to run
      git commit --amend --author="Author Name <email@address.com>"
      
      (substituting "Author Name" and "email@address.com" for your actual information) to set the authorship information.
    • If you made more than one commit and the commit with the missing authorship information is not the most recent one you have two options:
      1. You can re-create all commits missing authorship information. This is going to be the easiest solution for developers that aren't extremely confident in their Git and command line skills.
      2. You can use this script that GitHub provides to rewrite history. Please note: this should be used only if you are very confident in your abilities and understand its impacts.
    • Whichever method you choose, I will come by to re-check the pull request once you push the fixes to this branch.

We apologize for this inconvenience, especially since it usually bites new contributors to Home Assistant. We hope you understand the need for us to protect ourselves and the great community we all have built legally. The best thing to come out of this is that you only need to fix this once and it benefits the entire Home Assistant and GitHub community.

Thanks, I look forward to checking this PR again soon! ❤️

@home-assistant
Copy link
Copy Markdown

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

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

Code owner commands

Code owners of proxmoxve can trigger bot actions by commenting:

  • @home-assistant close Closes the pull request.
  • @home-assistant mark-draft Mark the pull request as draft.
  • @home-assistant ready-for-review Remove the draft status from the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign proxmoxve Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant update-branch Update the pull request branch with the base branch.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component, problem in config, problem in device, feature-request) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component, problem in config, problem in device, feature-request) on the pull request.

@irishpadres irishpadres marked this pull request as ready for review April 12, 2026 21:09
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

Note

Copilot was unable to run its full agentic suite in this review.

Fixes Proxmox VE sensor crashes when API responses omit expected fields by making sensor value extraction resilient and adding regression tests for incomplete/zero-value payloads.

Changes:

  • Updated Proxmox node/VM/container/storage sensor value_fn accessors to use dict.get() and guard missing keys.
  • Added safer handling for missing backup timestamps (and duration calculation).
  • Added new test coverage for incomplete API responses and a zero-capacity storage pool.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.

File Description
homeassistant/components/proxmoxve/sensor.py Makes sensor value functions tolerant of missing fields to prevent KeyError crashes.
tests/components/proxmoxve/test_sensor_missing_data.py Adds regression tests to ensure missing/zero fields don’t crash and yield unknown/safe values.

Comment thread tests/components/proxmoxve/test_sensor_missing_data.py Outdated
Comment thread tests/components/proxmoxve/test_sensor_missing_data.py Outdated
Comment thread tests/components/proxmoxve/test_sensor_missing_data.py Outdated
Comment thread tests/components/proxmoxve/test_sensor_missing_data.py Outdated
Comment thread homeassistant/components/proxmoxve/sensor.py
@irishpadres irishpadres force-pushed the fix-proxmox-sensors-robustness branch from b581592 to a37623e Compare April 12, 2026 21:16
@irishpadres
Copy link
Copy Markdown
Contributor Author

I have addressed the feedback from the automated review:

  • Fixed commit authorship email to match my GitHub account (addresses CLA error).
  • Added explicit checks for maxmem > 0 to prevent potential ZeroDivisionError.
  • Improved test robustness by using float() comparisons for numeric states and safer fixture access.
  • Fixed linting and formatting issues in the new test file.

Everything is now verified passing with local tests.

Copilot AI review requested due to automatic review settings April 12, 2026 21:22
@irishpadres irishpadres force-pushed the fix-proxmox-sensors-robustness branch from a37623e to 9525a74 Compare April 12, 2026 21:22
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

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

Comment thread homeassistant/components/proxmoxve/sensor.py
Comment thread tests/components/proxmoxve/test_sensor_missing_data.py Outdated
@irishpadres irishpadres force-pushed the fix-proxmox-sensors-robustness branch from 9525a74 to 751dca8 Compare April 12, 2026 21:32
Copilot AI review requested due to automatic review settings April 12, 2026 21:47
@irishpadres irishpadres force-pushed the fix-proxmox-sensors-robustness branch from 751dca8 to 6d331c5 Compare April 12, 2026 21:47
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

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

Comment thread tests/components/proxmoxve/test_sensor_missing_data.py Outdated
Comment thread tests/components/proxmoxve/test_sensor_missing_data.py Outdated
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

Copilot reviewed 3 out of 3 changed files in this pull request and generated 6 comments.

Comment thread tests/components/proxmoxve/test_sensor.py Outdated
Comment thread homeassistant/components/proxmoxve/sensor.py
Comment thread homeassistant/components/proxmoxve/sensor.py Outdated
Comment thread homeassistant/components/proxmoxve/binary_sensor.py
Comment thread homeassistant/components/proxmoxve/binary_sensor.py
Comment thread tests/components/proxmoxve/test_sensor.py Outdated
Update all sensor definitions (Node, VM, Container, and Storage) to
safely handle missing keys in the Proxmox API response by using
dict.get(). This prevents KeyError crashes for entities where
certain fields (like used_fraction, cpu, or memory) may be omitted.

Added tests/components/proxmoxve/test_sensor_missing_data.py to
verify robust handling of incomplete API data.
@irishpadres irishpadres force-pushed the fix-proxmox-sensors-robustness branch from f9b2330 to 2433faf Compare April 13, 2026 23:26
Copilot AI review requested due to automatic review settings April 18, 2026 21:50
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

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

Comment thread homeassistant/components/proxmoxve/binary_sensor.py Outdated
Comment thread homeassistant/components/proxmoxve/binary_sensor.py Outdated
irishpadres and others added 2 commits April 19, 2026 09:25
- Return None (unknown) from node status binary sensor when status
  field is missing, instead of False (offline)
- Return False from backup status binary sensor when no backups exist
  (not a problem), and None only when backup entry has no status field
- Remove exists_fn from storage sensors; always create entities and
  return None when fields are transiently missing to avoid permanent
  loss of entities on partial first refresh
- Update tests to assert entities exist and report STATE_UNKNOWN
  instead of asserting entity_id is None
- Add assert state is not None guards throughout missing-data tests
Copilot AI review requested due to automatic review settings April 19, 2026 16:47
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

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

Comment thread tests/components/proxmoxve/test_sensor.py
Comment thread homeassistant/components/proxmoxve/sensor.py
- Return None (unknown) from node status binary sensor when status
  field is missing, instead of False (offline)
- Return False from backup status binary sensor when no backups exist
  (not a problem), and None only when backup entry has no status field
- Remove exists_fn from storage sensors; always create entities and
  return None when fields are transiently missing to avoid permanent
  loss of entities on partial first refresh
- Fix storage binary sensors (active, enabled, shared) to return None
  when their field is absent, instead of False
- Update tests to assert entities exist and report STATE_UNKNOWN
  instead of asserting entity_id is None
- Add assert state/entity_id is not None guards in missing-data tests
- Add photo NFS storage fixture entry (active=0, no used_fraction)
  reflecting real Proxmox API response for an offline NFS mount
- Reference photo storage in missing-data test instead of synthetic
  zero_pool entry

Fixes home-assistant#168554
Copilot AI review requested due to automatic review settings April 19, 2026 17:19
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

Copilot reviewed 7 out of 7 changed files in this pull request and generated 3 comments.

key="status",
translation_key="status",
state_fn=lambda data: data["status"] == VM_CONTAINER_RUNNING,
state_fn=lambda data: data.get("status") == VM_CONTAINER_RUNNING,
Copy link

Copilot AI Apr 19, 2026

Choose a reason for hiding this comment

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

Return None (unknown) when the container status field is missing instead of treating it as False, so a partial Proxmox API response doesn’t incorrectly mark the container as stopped.

Suggested change
state_fn=lambda data: data.get("status") == VM_CONTAINER_RUNNING,
state_fn=lambda data: (
status == VM_CONTAINER_RUNNING
if (status := data.get("status")) is not None
else None
),

Copilot uses AI. Check for mistakes.
Comment thread homeassistant/components/proxmoxve/binary_sensor.py
Comment thread homeassistant/components/proxmoxve/binary_sensor.py
Copy link
Copy Markdown
Member

@erwindouna erwindouna left a comment

Choose a reason for hiding this comment

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

Tests are failing, can you please look into them? :)

@home-assistant home-assistant bot marked this pull request as draft April 19, 2026 20:33
irishpadres and others added 2 commits April 19, 2026 16:14
The test strips 'used' from all storage entries to test missing-data
robustness, then incorrectly expected float(0.0) for the photo storage
used sensor. Since the field is stripped, the state is 'unknown'.
Copilot AI review requested due to automatic review settings April 19, 2026 23:19
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

Copilot reviewed 7 out of 7 changed files in this pull request and generated no new comments.

@irishpadres irishpadres marked this pull request as ready for review April 19, 2026 23:24
@home-assistant home-assistant bot requested a review from erwindouna April 19, 2026 23:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Proxmox VE sensors may report wrong state when API fields are absent

6 participants