Refactor unit of measurement handling in importer.py#340
Open
lanort wants to merge 1 commit into
Open
Conversation
pujux
approved these changes
Apr 7, 2026
Owner
|
Hi @lanort lanort, could you please rebease? |
Author
|
@DarwinsBuddy Should be good now. |
DarwinsBuddy
requested changes
May 2, 2026
DarwinsBuddy
left a comment
Owner
There was a problem hiding this comment.
I fear you didn't look at the PR at all, it seems that you commited something that didn't got unmerged.
I.g. please only commit what you have tested yourself.
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 join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fix KeyError: 'unitOfMeasurement' in importer.py
Problem
The component was experiencing a
KeyError: 'unitOfMeasurement'when the API response from WienerNetze did not include theunitOfMeasurementfield in the bewegungsdaten response. This occurred at line 162 incustom_components/wnsm/importer.pywhere the code directly accessedbewegungsdaten['unitOfMeasurement']without checking if the key exists.Error Traceback
Root Cause
The
translate_dict()utility function (inutils.py) only adds keys to the result dictionary if the source value is not None. When the WienerNetze API doesn't provide thedescriptor.einheitfield, theunitOfMeasurementkey is completely absent from thebewegungsdatendictionary, causing a KeyError when accessed directly.Solution
Implemented a robust fix that:
.get()method: Changed from direct dictionary access (bewegungsdaten['unitOfMeasurement']) to safe access using.get()methodunit_of_measurementconfigured in the sensorCode Changes
File:
custom_components/wnsm/importer.pyBefore (lines 162-167):
After (lines 163-186):
Testing
Importerclass already hasself.unit_of_measurementfrom the sensor configuration, making the fallback reliableBenefits
Additional Notes
The fix leverages the existing
self.unit_of_measurementattribute which is passed from the sensor during initialization (defaults toUnitOfEnergy.KILO_WATT_HOURfrom Home Assistant). This provides a reliable fallback that matches the sensor's configuration.