Use reported units for the Qbus integration#171588
Merged
Merged
Conversation
Contributor
|
Hey there @Qbus-iot, mind taking a look at this pull request as it has been labeled with an integration ( Code owner commandsCode owners of
|
Contributor
There was a problem hiding this comment.
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_measurementassignments from Qbus sensor descriptions. - Set
native_unit_of_measurementdynamically frommqtt_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_measurementfrom 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 omitscurrentValue.unit, and may affect statistics/long-term behavior that relies on units. Consider keeping the description defaults and only overriding_attr_native_unit_of_measurementwhen the payload unit is present and validated (e.g., allowlist/normalize units perdevice_class).
"""Support for Qbus sensor."""
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
approved these changes
May 26, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
Additional information
Checklist
ruff format homeassistant tests)If user exposed functionality or configuration variables are added/changed:
If the code communicates with devices, web services, or third-party tools:
Updated and included derived files by running:
python3 -m script.hassfest.requirements_all.txt.Updated by running
python3 -m script.gen_requirements_all.To help with the load of incoming pull requests: