Skip to content

Commit

Permalink
Draft work at fixing KeyError: (<VictronSensor.EXTERNAL_DEVICE_LOAD: …
Browse files Browse the repository at this point in the history
…'external_device_load'>, <Units.ELECTRIC_CURRENT_AMPERE: 'A'>) keshavdv#123

Rather than fixing the problem, this aims to robustify the code by handling failed lookups - not perfectly, but well enough to move on with, rather than simply failing to record anything at all into HomeAssistant.

It's probably overkill - I was trying to add in debug logs, and in hindsight, they aren't useful. Just adding the default description under line 169 and changing line 178 to use that default is probably sufficient.
  • Loading branch information
jamezpolley committed Oct 28, 2024
1 parent a8eee7c commit b79c9fd
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions custom_components/victron_ble/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,29 @@
}


def log_and_get_description(description, default_description):
"""Log the description and get the value from SENSOR_DESCRIPTIONS."""
_LOGGER.debug("Dict: {}".format(description.__dict__))
_LOGGER.debug("Unit: {}".format(description.native_unit_of_measurement))
_LOGGER.debug(f"Processing description: {description}")
sensor_description = SENSOR_DESCRIPTIONS.get(
(description.device_key.key, description.native_unit_of_measurement),
default_description
)
_LOGGER.debug(f"Returning: {sensor_description}")


def sensor_update_to_bluetooth_data_update(
sensor_update: SensorUpdate,
) -> PassiveBluetoothDataUpdate:
"""Convert a sensor update to a bluetooth data update."""
default_description = SensorEntityDescription(
key="default",
device_class=None,
native_unit_of_measurement=None,
state_class=None,
)

data = PassiveBluetoothDataUpdate(
devices={
device_id: sensor_device_info_to_hass_device_info(device_info)
Expand All @@ -169,9 +188,7 @@ def sensor_update_to_bluetooth_data_update(
entity_descriptions={
PassiveBluetoothEntityKey(
device_key.key, device_key.device_id
): SENSOR_DESCRIPTIONS[
(description.device_key.key, description.native_unit_of_measurement)
]
): log_and_get_description(description, default_description)
for device_key, description in sensor_update.entity_descriptions.items()
if description.device_key
},
Expand Down

0 comments on commit b79c9fd

Please sign in to comment.