From 8b2774e08296fdb14cbdc2de26d444ade1be11bf Mon Sep 17 00:00:00 2001 From: rikroe <42204099+rikroe@users.noreply.github.com> Date: Sun, 10 Apr 2022 18:40:03 +0200 Subject: [PATCH] Fix converting (value, unit) tuples if value is None (#69802) Co-authored-by: rikroe --- .../components/bmw_connected_drive/sensor.py | 31 ++++++++++--------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/homeassistant/components/bmw_connected_drive/sensor.py b/homeassistant/components/bmw_connected_drive/sensor.py index f21c1b851ca..8b21a10a39f 100644 --- a/homeassistant/components/bmw_connected_drive/sensor.py +++ b/homeassistant/components/bmw_connected_drive/sensor.py @@ -46,6 +46,17 @@ class BMWSensorEntityDescription(SensorEntityDescription): value: Callable = lambda x, y: x +def convert_and_round( + state: tuple, + converter: Callable[[float | None, str], float], + precision: int, +) -> float | None: + """Safely convert and round a value from a Tuple[value, unit].""" + if state[0] is None: + return None + return round(converter(state[0], UNIT_MAP.get(state[1], state[1])), precision) + + SENSOR_TYPES: dict[str, BMWSensorEntityDescription] = { # --- Generic --- "charging_start_time": BMWSensorEntityDescription( @@ -78,45 +89,35 @@ SENSOR_TYPES: dict[str, BMWSensorEntityDescription] = { icon="mdi:speedometer", unit_metric=LENGTH_KILOMETERS, unit_imperial=LENGTH_MILES, - value=lambda x, hass: round( - hass.config.units.length(x[0], UNIT_MAP.get(x[1], x[1])), 2 - ), + value=lambda x, hass: convert_and_round(x, hass.config.units.length, 2), ), "remaining_range_total": BMWSensorEntityDescription( key="remaining_range_total", icon="mdi:map-marker-distance", unit_metric=LENGTH_KILOMETERS, unit_imperial=LENGTH_MILES, - value=lambda x, hass: round( - hass.config.units.length(x[0], UNIT_MAP.get(x[1], x[1])), 2 - ), + value=lambda x, hass: convert_and_round(x, hass.config.units.length, 2), ), "remaining_range_electric": BMWSensorEntityDescription( key="remaining_range_electric", icon="mdi:map-marker-distance", unit_metric=LENGTH_KILOMETERS, unit_imperial=LENGTH_MILES, - value=lambda x, hass: round( - hass.config.units.length(x[0], UNIT_MAP.get(x[1], x[1])), 2 - ), + value=lambda x, hass: convert_and_round(x, hass.config.units.length, 2), ), "remaining_range_fuel": BMWSensorEntityDescription( key="remaining_range_fuel", icon="mdi:map-marker-distance", unit_metric=LENGTH_KILOMETERS, unit_imperial=LENGTH_MILES, - value=lambda x, hass: round( - hass.config.units.length(x[0], UNIT_MAP.get(x[1], x[1])), 2 - ), + value=lambda x, hass: convert_and_round(x, hass.config.units.length, 2), ), "remaining_fuel": BMWSensorEntityDescription( key="remaining_fuel", icon="mdi:gas-station", unit_metric=VOLUME_LITERS, unit_imperial=VOLUME_GALLONS, - value=lambda x, hass: round( - hass.config.units.volume(x[0], UNIT_MAP.get(x[1], x[1])), 2 - ), + value=lambda x, hass: convert_and_round(x, hass.config.units.volume, 2), ), "fuel_percent": BMWSensorEntityDescription( key="fuel_percent",