Skip to content

Use reported units for the Qbus integration#171588

Merged
joostlek merged 4 commits into
home-assistant:devfrom
thomasddn:qbus/fix/units
May 26, 2026
Merged

Use reported units for the Qbus integration#171588
joostlek merged 4 commits into
home-assistant:devfrom
thomasddn:qbus/fix/units

Conversation

@thomasddn
Copy link
Copy Markdown
Contributor

Breaking change

Sensor units now follow the unit reported by Qbus, which can change existing entities from values like Wh to kWh (or vice versa). As a result, automations, templates, dashboards, and long-term statistics that relied on the previous units may produce incorrect results or stop matching expected conditions until updated.

Proposed change

Qbus reports the unit configured by the user in the Qbus system. With this PR we're now using the reported unit for gauge sensors instead of using a fixed unit.

I'm assuming this is a breaking change. If not, feel free to change the PR type to "Bugfix".

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 diff between library versions and ideally a link to the changelog/release notes is added to the PR description.

To help with the load of incoming pull requests:

@home-assistant
Copy link
Copy Markdown
Contributor

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

Code owner commands

Code owners of qbus 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 qbus 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.

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.

Updates the Qbus sensor platform to derive sensor units from the device payload instead of Home Assistant unit enums, and refreshes test fixtures/snapshots accordingly.

Changes:

  • Remove static native_unit_of_measurement assignments from Qbus sensor descriptions.
  • Set native_unit_of_measurement dynamically from mqtt_output.properties["currentValue"]["unit"].
  • Update test snapshots and fixture payload units to match the new behavior.

Reviewed changes

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

File Description
homeassistant/components/qbus/sensor.py Switch unit assignment from static HA enums to unit strings provided by the Qbus payload.
tests/components/qbus/fixtures/payload_config.json Adjust fixture units (swap "kWh"/"A") to align with the updated unit source.
tests/components/qbus/snapshots/test_sensor.ambr Refresh snapshots to expect string units (e.g., 'kWh') instead of enum reprs.
Comments suppressed due to low confidence (1)

homeassistant/components/qbus/sensor.py:1

  • Removing native_unit_of_measurement from the entity descriptions means entities will have no unit unless it is present in the payload. This can lead to missing/incorrect units for common device classes (e.g., temperature, humidity, CO2) when the payload omits currentValue.unit, and may affect statistics/long-term behavior that relies on units. Consider keeping the description defaults and only overriding _attr_native_unit_of_measurement when the payload unit is present and validated (e.g., allowlist/normalize units per device_class).
"""Support for Qbus sensor."""

Comment thread homeassistant/components/qbus/sensor.py Outdated
Comment thread homeassistant/components/qbus/sensor.py Outdated
Comment thread homeassistant/components/qbus/sensor.py Outdated
Comment thread tests/components/qbus/fixtures/payload_config.json
Comment thread tests/components/qbus/fixtures/payload_config.json Outdated
@thomasddn thomasddn marked this pull request as draft May 20, 2026 14:33
@thomasddn thomasddn marked this pull request as ready for review May 20, 2026 15:33
Copilot AI review requested due to automatic review settings May 20, 2026 15:33
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 4 out of 4 changed files in this pull request and generated 4 comments.

Comment on lines +313 to +322
allowed_units = (
DEVICE_CLASS_UNITS.get(self.entity_description.device_class)
if self.entity_description.device_class
else None
)
value_properties: dict = mqtt_output.properties.get("currentValue", {})
unit = self._find_matching_unit(value_properties.get("unit"), allowed_units)

if allowed_units is not None and unit in allowed_units:
self._attr_native_unit_of_measurement = unit
Comment on lines +327 to +337
def _find_matching_unit(
self,
unit: str | None,
allowed_units: set[type[StrEnum] | str | None] | None,
) -> str | None:
"""Do a case-insensitive search in the allowed units. Returns the properly cased unit if found, else None."""
if unit is None or allowed_units is None:
return None

lookup = {str(u).casefold(): str(u) for u in allowed_units if u is not None}
return lookup.get(unit.casefold())
Comment on lines +35 to +37
entity = hass.states.get("sensor.garage_energie")
assert entity
assert entity.attributes[ATTR_UNIT_OF_MEASUREMENT] == "Wh"
Comment on lines +45 to +47
entity = hass.states.get("sensor.tuin_luchtkwaliteit")
assert entity
assert entity.attributes[ATTR_UNIT_OF_MEASUREMENT] == "ppm"
@joostlek joostlek merged commit 978171b into home-assistant:dev May 26, 2026
33 checks passed
@thomasddn thomasddn deleted the qbus/fix/units branch May 27, 2026 08:33
@github-actions github-actions Bot locked and limited conversation to collaborators May 28, 2026
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.

3 participants