Merge bmw_connected_drive metric and imperial sensor types (#56910)

This commit is contained in:
Marc Mueller 2021-10-15 19:02:12 +02:00 committed by GitHub
parent f8d0f76721
commit 19443b474c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,4 +1,6 @@
"""Support for reading vehicle status from BMW connected drive portal.""" """Support for reading vehicle status from BMW connected drive portal."""
from __future__ import annotations
import logging import logging
from bimmer_connected.const import SERVICE_ALL_TRIPS, SERVICE_LAST_TRIP, SERVICE_STATUS from bimmer_connected.const import SERVICE_ALL_TRIPS, SERVICE_LAST_TRIP, SERVICE_STATUS
@ -27,384 +29,343 @@ from .const import CONF_ACCOUNT, DATA_ENTRIES
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
ATTR_TO_HA_METRIC = { SENSOR_TYPES: dict[str, tuple[str | None, str | None, str | None, str | None, bool]] = {
# "<ID>": [<MDI_ICON>, <DEVICE_CLASS>, <UNIT_OF_MEASUREMENT>, <ENABLED_BY_DEFAULT>], # "<ID>": (<MDI_ICON>, <DEVICE_CLASS>, <UNIT_OF_MEASUREMENT (metric)>, <UNIT_OF_MEASUREMENT (imperial)>, <ENABLED_BY_DEFAULT>),
"mileage": ["mdi:speedometer", None, LENGTH_KILOMETERS, True], # --- Generic ---
"remaining_range_total": ["mdi:map-marker-distance", None, LENGTH_KILOMETERS, True], "charging_time_remaining": (
"remaining_range_electric": [ "mdi:update",
"mdi:map-marker-distance",
None, None,
LENGTH_KILOMETERS, TIME_HOURS,
TIME_HOURS,
True, True,
], ),
"remaining_range_fuel": ["mdi:map-marker-distance", None, LENGTH_KILOMETERS, True], "charging_status": (
"max_range_electric": ["mdi:map-marker-distance", None, LENGTH_KILOMETERS, True], "mdi:battery-charging",
"remaining_fuel": ["mdi:gas-station", None, VOLUME_LITERS, True], None,
# LastTrip attributes None,
"average_combined_consumption": [
"mdi:flash",
None, None,
f"{ENERGY_KILO_WATT_HOUR}/100{LENGTH_KILOMETERS}",
True, True,
], ),
"average_electric_consumption": [
"mdi:power-plug-outline",
None,
f"{ENERGY_KILO_WATT_HOUR}/100{LENGTH_KILOMETERS}",
True,
],
"average_recuperation": [
"mdi:recycle-variant",
None,
f"{ENERGY_KILO_WATT_HOUR}/100{LENGTH_KILOMETERS}",
True,
],
"electric_distance": ["mdi:map-marker-distance", None, LENGTH_KILOMETERS, True],
"saved_fuel": ["mdi:fuel", None, VOLUME_LITERS, False],
"total_distance": ["mdi:map-marker-distance", None, LENGTH_KILOMETERS, True],
# AllTrips attributes
"average_combined_consumption_community_average": [
"mdi:flash",
None,
f"{ENERGY_KILO_WATT_HOUR}/100{LENGTH_KILOMETERS}",
False,
],
"average_combined_consumption_community_high": [
"mdi:flash",
None,
f"{ENERGY_KILO_WATT_HOUR}/100{LENGTH_KILOMETERS}",
False,
],
"average_combined_consumption_community_low": [
"mdi:flash",
None,
f"{ENERGY_KILO_WATT_HOUR}/100{LENGTH_KILOMETERS}",
False,
],
"average_combined_consumption_user_average": [
"mdi:flash",
None,
f"{ENERGY_KILO_WATT_HOUR}/100{LENGTH_KILOMETERS}",
True,
],
"average_electric_consumption_community_average": [
"mdi:power-plug-outline",
None,
f"{ENERGY_KILO_WATT_HOUR}/100{LENGTH_KILOMETERS}",
False,
],
"average_electric_consumption_community_high": [
"mdi:power-plug-outline",
None,
f"{ENERGY_KILO_WATT_HOUR}/100{LENGTH_KILOMETERS}",
False,
],
"average_electric_consumption_community_low": [
"mdi:power-plug-outline",
None,
f"{ENERGY_KILO_WATT_HOUR}/100{LENGTH_KILOMETERS}",
False,
],
"average_electric_consumption_user_average": [
"mdi:power-plug-outline",
None,
f"{ENERGY_KILO_WATT_HOUR}/100{LENGTH_KILOMETERS}",
True,
],
"average_recuperation_community_average": [
"mdi:recycle-variant",
None,
f"{ENERGY_KILO_WATT_HOUR}/100{LENGTH_KILOMETERS}",
False,
],
"average_recuperation_community_high": [
"mdi:recycle-variant",
None,
f"{ENERGY_KILO_WATT_HOUR}/100{LENGTH_KILOMETERS}",
False,
],
"average_recuperation_community_low": [
"mdi:recycle-variant",
None,
f"{ENERGY_KILO_WATT_HOUR}/100{LENGTH_KILOMETERS}",
False,
],
"average_recuperation_user_average": [
"mdi:recycle-variant",
None,
f"{ENERGY_KILO_WATT_HOUR}/100{LENGTH_KILOMETERS}",
True,
],
"chargecycle_range_community_average": [
"mdi:map-marker-distance",
None,
LENGTH_KILOMETERS,
False,
],
"chargecycle_range_community_high": [
"mdi:map-marker-distance",
None,
LENGTH_KILOMETERS,
False,
],
"chargecycle_range_community_low": [
"mdi:map-marker-distance",
None,
LENGTH_KILOMETERS,
False,
],
"chargecycle_range_user_average": [
"mdi:map-marker-distance",
None,
LENGTH_KILOMETERS,
True,
],
"chargecycle_range_user_current_charge_cycle": [
"mdi:map-marker-distance",
None,
LENGTH_KILOMETERS,
True,
],
"chargecycle_range_user_high": [
"mdi:map-marker-distance",
None,
LENGTH_KILOMETERS,
True,
],
"total_electric_distance_community_average": [
"mdi:map-marker-distance",
None,
LENGTH_KILOMETERS,
False,
],
"total_electric_distance_community_high": [
"mdi:map-marker-distance",
None,
LENGTH_KILOMETERS,
False,
],
"total_electric_distance_community_low": [
"mdi:map-marker-distance",
None,
LENGTH_KILOMETERS,
False,
],
"total_electric_distance_user_average": [
"mdi:map-marker-distance",
None,
LENGTH_KILOMETERS,
False,
],
"total_electric_distance_user_total": [
"mdi:map-marker-distance",
None,
LENGTH_KILOMETERS,
False,
],
"total_saved_fuel": ["mdi:fuel", None, VOLUME_LITERS, False],
}
ATTR_TO_HA_IMPERIAL = {
# "<ID>": [<MDI_ICON>, <DEVICE_CLASS>, <UNIT_OF_MEASUREMENT>, <ENABLED_BY_DEFAULT>],
"mileage": ["mdi:speedometer", None, LENGTH_MILES, True],
"remaining_range_total": ["mdi:map-marker-distance", None, LENGTH_MILES, True],
"remaining_range_electric": ["mdi:map-marker-distance", None, LENGTH_MILES, True],
"remaining_range_fuel": ["mdi:map-marker-distance", None, LENGTH_MILES, True],
"max_range_electric": ["mdi:map-marker-distance", None, LENGTH_MILES, True],
"remaining_fuel": ["mdi:gas-station", None, VOLUME_GALLONS, True],
# LastTrip attributes
"average_combined_consumption": [
"mdi:flash",
None,
f"{ENERGY_KILO_WATT_HOUR}/100{LENGTH_MILES}",
True,
],
"average_electric_consumption": [
"mdi:power-plug-outline",
None,
f"{ENERGY_KILO_WATT_HOUR}/100{LENGTH_MILES}",
True,
],
"average_recuperation": [
"mdi:recycle-variant",
None,
f"{ENERGY_KILO_WATT_HOUR}/100{LENGTH_MILES}",
True,
],
"electric_distance": ["mdi:map-marker-distance", None, LENGTH_MILES, True],
"saved_fuel": ["mdi:fuel", None, VOLUME_GALLONS, False],
"total_distance": ["mdi:map-marker-distance", None, LENGTH_MILES, True],
# AllTrips attributes
"average_combined_consumption_community_average": [
"mdi:flash",
None,
f"{ENERGY_KILO_WATT_HOUR}/100{LENGTH_MILES}",
False,
],
"average_combined_consumption_community_high": [
"mdi:flash",
None,
f"{ENERGY_KILO_WATT_HOUR}/100{LENGTH_MILES}",
False,
],
"average_combined_consumption_community_low": [
"mdi:flash",
None,
f"{ENERGY_KILO_WATT_HOUR}/100{LENGTH_MILES}",
False,
],
"average_combined_consumption_user_average": [
"mdi:flash",
None,
f"{ENERGY_KILO_WATT_HOUR}/100{LENGTH_MILES}",
True,
],
"average_electric_consumption_community_average": [
"mdi:power-plug-outline",
None,
f"{ENERGY_KILO_WATT_HOUR}/100{LENGTH_MILES}",
False,
],
"average_electric_consumption_community_high": [
"mdi:power-plug-outline",
None,
f"{ENERGY_KILO_WATT_HOUR}/100{LENGTH_MILES}",
False,
],
"average_electric_consumption_community_low": [
"mdi:power-plug-outline",
None,
f"{ENERGY_KILO_WATT_HOUR}/100{LENGTH_MILES}",
False,
],
"average_electric_consumption_user_average": [
"mdi:power-plug-outline",
None,
f"{ENERGY_KILO_WATT_HOUR}/100{LENGTH_MILES}",
True,
],
"average_recuperation_community_average": [
"mdi:recycle-variant",
None,
f"{ENERGY_KILO_WATT_HOUR}/100{LENGTH_MILES}",
False,
],
"average_recuperation_community_high": [
"mdi:recycle-variant",
None,
f"{ENERGY_KILO_WATT_HOUR}/100{LENGTH_MILES}",
False,
],
"average_recuperation_community_low": [
"mdi:recycle-variant",
None,
f"{ENERGY_KILO_WATT_HOUR}/100{LENGTH_MILES}",
False,
],
"average_recuperation_user_average": [
"mdi:recycle-variant",
None,
f"{ENERGY_KILO_WATT_HOUR}/100{LENGTH_MILES}",
True,
],
"chargecycle_range_community_average": [
"mdi:map-marker-distance",
None,
LENGTH_MILES,
False,
],
"chargecycle_range_community_high": [
"mdi:map-marker-distance",
None,
LENGTH_MILES,
False,
],
"chargecycle_range_community_low": [
"mdi:map-marker-distance",
None,
LENGTH_MILES,
False,
],
"chargecycle_range_user_average": [
"mdi:map-marker-distance",
None,
LENGTH_MILES,
True,
],
"chargecycle_range_user_current_charge_cycle": [
"mdi:map-marker-distance",
None,
LENGTH_MILES,
True,
],
"chargecycle_range_user_high": [
"mdi:map-marker-distance",
None,
LENGTH_MILES,
True,
],
"total_electric_distance_community_average": [
"mdi:map-marker-distance",
None,
LENGTH_MILES,
False,
],
"total_electric_distance_community_high": [
"mdi:map-marker-distance",
None,
LENGTH_MILES,
False,
],
"total_electric_distance_community_low": [
"mdi:map-marker-distance",
None,
LENGTH_MILES,
False,
],
"total_electric_distance_user_average": [
"mdi:map-marker-distance",
None,
LENGTH_MILES,
False,
],
"total_electric_distance_user_total": [
"mdi:map-marker-distance",
None,
LENGTH_MILES,
False,
],
"total_saved_fuel": ["mdi:fuel", None, VOLUME_GALLONS, False],
}
ATTR_TO_HA_GENERIC = {
# "<ID>": [<MDI_ICON>, <DEVICE_CLASS>, <UNIT_OF_MEASUREMENT>, <ENABLED_BY_DEFAULT>],
"charging_time_remaining": ["mdi:update", None, TIME_HOURS, True],
"charging_status": ["mdi:battery-charging", None, None, True],
# No icon as this is dealt with directly as a special case in icon() # No icon as this is dealt with directly as a special case in icon()
"charging_level_hv": [None, None, PERCENTAGE, True], "charging_level_hv": (
None,
None,
PERCENTAGE,
PERCENTAGE,
True,
),
# LastTrip attributes # LastTrip attributes
"date_utc": [None, DEVICE_CLASS_TIMESTAMP, None, True], "date_utc": (
"duration": ["mdi:timer-outline", None, TIME_MINUTES, True], None,
"electric_distance_ratio": ["mdi:percent-outline", None, PERCENTAGE, False], DEVICE_CLASS_TIMESTAMP,
None,
None,
True,
),
"duration": (
"mdi:timer-outline",
None,
TIME_MINUTES,
TIME_MINUTES,
True,
),
"electric_distance_ratio": (
"mdi:percent-outline",
None,
PERCENTAGE,
PERCENTAGE,
False,
),
# AllTrips attributes # AllTrips attributes
"battery_size_max": ["mdi:battery-charging-high", None, ENERGY_WATT_HOUR, False], "battery_size_max": (
"reset_date_utc": [None, DEVICE_CLASS_TIMESTAMP, None, False], "mdi:battery-charging-high",
"saved_co2": ["mdi:tree-outline", None, MASS_KILOGRAMS, False], None,
"saved_co2_green_energy": ["mdi:tree-outline", None, MASS_KILOGRAMS, False], ENERGY_WATT_HOUR,
ENERGY_WATT_HOUR,
False,
),
"reset_date_utc": (
None,
DEVICE_CLASS_TIMESTAMP,
None,
None,
False,
),
"saved_co2": (
"mdi:tree-outline",
None,
MASS_KILOGRAMS,
MASS_KILOGRAMS,
False,
),
"saved_co2_green_energy": (
"mdi:tree-outline",
None,
MASS_KILOGRAMS,
MASS_KILOGRAMS,
False,
),
# --- Specific ---
"mileage": (
"mdi:speedometer",
None,
LENGTH_KILOMETERS,
LENGTH_MILES,
True,
),
"remaining_range_total": (
"mdi:map-marker-distance",
None,
LENGTH_KILOMETERS,
LENGTH_MILES,
True,
),
"remaining_range_electric": (
"mdi:map-marker-distance",
None,
LENGTH_KILOMETERS,
LENGTH_MILES,
True,
),
"remaining_range_fuel": (
"mdi:map-marker-distance",
None,
LENGTH_KILOMETERS,
LENGTH_MILES,
True,
),
"max_range_electric": (
"mdi:map-marker-distance",
None,
LENGTH_KILOMETERS,
LENGTH_MILES,
True,
),
"remaining_fuel": (
"mdi:gas-station",
None,
VOLUME_LITERS,
VOLUME_GALLONS,
True,
),
# LastTrip attributes
"average_combined_consumption": (
"mdi:flash",
None,
f"{ENERGY_KILO_WATT_HOUR}/100{LENGTH_KILOMETERS}",
f"{ENERGY_KILO_WATT_HOUR}/100{LENGTH_MILES}",
True,
),
"average_electric_consumption": (
"mdi:power-plug-outline",
None,
f"{ENERGY_KILO_WATT_HOUR}/100{LENGTH_KILOMETERS}",
f"{ENERGY_KILO_WATT_HOUR}/100{LENGTH_MILES}",
True,
),
"average_recuperation": (
"mdi:recycle-variant",
None,
f"{ENERGY_KILO_WATT_HOUR}/100{LENGTH_KILOMETERS}",
f"{ENERGY_KILO_WATT_HOUR}/100{LENGTH_MILES}",
True,
),
"electric_distance": (
"mdi:map-marker-distance",
None,
LENGTH_KILOMETERS,
LENGTH_MILES,
True,
),
"saved_fuel": (
"mdi:fuel",
None,
VOLUME_LITERS,
VOLUME_GALLONS,
False,
),
"total_distance": (
"mdi:map-marker-distance",
None,
LENGTH_KILOMETERS,
LENGTH_MILES,
True,
),
# AllTrips attributes
"average_combined_consumption_community_average": (
"mdi:flash",
None,
f"{ENERGY_KILO_WATT_HOUR}/100{LENGTH_KILOMETERS}",
f"{ENERGY_KILO_WATT_HOUR}/100{LENGTH_MILES}",
False,
),
"average_combined_consumption_community_high": (
"mdi:flash",
None,
f"{ENERGY_KILO_WATT_HOUR}/100{LENGTH_KILOMETERS}",
f"{ENERGY_KILO_WATT_HOUR}/100{LENGTH_MILES}",
False,
),
"average_combined_consumption_community_low": (
"mdi:flash",
None,
f"{ENERGY_KILO_WATT_HOUR}/100{LENGTH_KILOMETERS}",
f"{ENERGY_KILO_WATT_HOUR}/100{LENGTH_MILES}",
False,
),
"average_combined_consumption_user_average": (
"mdi:flash",
None,
f"{ENERGY_KILO_WATT_HOUR}/100{LENGTH_KILOMETERS}",
f"{ENERGY_KILO_WATT_HOUR}/100{LENGTH_MILES}",
True,
),
"average_electric_consumption_community_average": (
"mdi:power-plug-outline",
None,
f"{ENERGY_KILO_WATT_HOUR}/100{LENGTH_KILOMETERS}",
f"{ENERGY_KILO_WATT_HOUR}/100{LENGTH_MILES}",
False,
),
"average_electric_consumption_community_high": (
"mdi:power-plug-outline",
None,
f"{ENERGY_KILO_WATT_HOUR}/100{LENGTH_KILOMETERS}",
f"{ENERGY_KILO_WATT_HOUR}/100{LENGTH_MILES}",
False,
),
"average_electric_consumption_community_low": (
"mdi:power-plug-outline",
None,
f"{ENERGY_KILO_WATT_HOUR}/100{LENGTH_KILOMETERS}",
f"{ENERGY_KILO_WATT_HOUR}/100{LENGTH_MILES}",
False,
),
"average_electric_consumption_user_average": (
"mdi:power-plug-outline",
None,
f"{ENERGY_KILO_WATT_HOUR}/100{LENGTH_KILOMETERS}",
f"{ENERGY_KILO_WATT_HOUR}/100{LENGTH_MILES}",
True,
),
"average_recuperation_community_average": (
"mdi:recycle-variant",
None,
f"{ENERGY_KILO_WATT_HOUR}/100{LENGTH_KILOMETERS}",
f"{ENERGY_KILO_WATT_HOUR}/100{LENGTH_MILES}",
False,
),
"average_recuperation_community_high": (
"mdi:recycle-variant",
None,
f"{ENERGY_KILO_WATT_HOUR}/100{LENGTH_KILOMETERS}",
f"{ENERGY_KILO_WATT_HOUR}/100{LENGTH_MILES}",
False,
),
"average_recuperation_community_low": (
"mdi:recycle-variant",
None,
f"{ENERGY_KILO_WATT_HOUR}/100{LENGTH_KILOMETERS}",
f"{ENERGY_KILO_WATT_HOUR}/100{LENGTH_MILES}",
False,
),
"average_recuperation_user_average": (
"mdi:recycle-variant",
None,
f"{ENERGY_KILO_WATT_HOUR}/100{LENGTH_KILOMETERS}",
f"{ENERGY_KILO_WATT_HOUR}/100{LENGTH_MILES}",
True,
),
"chargecycle_range_community_average": (
"mdi:map-marker-distance",
None,
LENGTH_KILOMETERS,
LENGTH_MILES,
False,
),
"chargecycle_range_community_high": (
"mdi:map-marker-distance",
None,
LENGTH_KILOMETERS,
LENGTH_MILES,
False,
),
"chargecycle_range_community_low": (
"mdi:map-marker-distance",
None,
LENGTH_KILOMETERS,
LENGTH_MILES,
False,
),
"chargecycle_range_user_average": (
"mdi:map-marker-distance",
None,
LENGTH_KILOMETERS,
LENGTH_MILES,
True,
),
"chargecycle_range_user_current_charge_cycle": (
"mdi:map-marker-distance",
None,
LENGTH_KILOMETERS,
LENGTH_MILES,
True,
),
"chargecycle_range_user_high": (
"mdi:map-marker-distance",
None,
LENGTH_KILOMETERS,
LENGTH_MILES,
True,
),
"total_electric_distance_community_average": (
"mdi:map-marker-distance",
None,
LENGTH_KILOMETERS,
LENGTH_MILES,
False,
),
"total_electric_distance_community_high": (
"mdi:map-marker-distance",
None,
LENGTH_KILOMETERS,
LENGTH_MILES,
False,
),
"total_electric_distance_community_low": (
"mdi:map-marker-distance",
None,
LENGTH_KILOMETERS,
LENGTH_MILES,
False,
),
"total_electric_distance_user_average": (
"mdi:map-marker-distance",
None,
LENGTH_KILOMETERS,
LENGTH_MILES,
False,
),
"total_electric_distance_user_total": (
"mdi:map-marker-distance",
None,
LENGTH_KILOMETERS,
LENGTH_MILES,
False,
),
"total_saved_fuel": (
"mdi:fuel",
None,
VOLUME_LITERS,
VOLUME_GALLONS,
False,
),
} }
ATTR_TO_HA_METRIC.update(ATTR_TO_HA_GENERIC)
ATTR_TO_HA_IMPERIAL.update(ATTR_TO_HA_GENERIC)
async def async_setup_entry(hass, config_entry, async_add_entities): async def async_setup_entry(hass, config_entry, async_add_entities):
"""Set up the BMW ConnectedDrive sensors from config entry.""" """Set up the BMW ConnectedDrive sensors from config entry."""
# pylint: disable=too-many-nested-blocks # pylint: disable=too-many-nested-blocks
if hass.config.units.name == CONF_UNIT_SYSTEM_IMPERIAL:
attribute_info = ATTR_TO_HA_IMPERIAL
else:
attribute_info = ATTR_TO_HA_METRIC
account = hass.data[BMW_DOMAIN][DATA_ENTRIES][config_entry.entry_id][CONF_ACCOUNT] account = hass.data[BMW_DOMAIN][DATA_ENTRIES][config_entry.entry_id][CONF_ACCOUNT]
entities = [] entities = []
@ -414,33 +375,33 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
for attribute_name in vehicle.drive_train_attributes: for attribute_name in vehicle.drive_train_attributes:
if attribute_name in vehicle.available_attributes: if attribute_name in vehicle.available_attributes:
device = BMWConnectedDriveSensor( device = BMWConnectedDriveSensor(
account, vehicle, attribute_name, attribute_info hass, account, vehicle, attribute_name
) )
entities.append(device) entities.append(device)
if service == SERVICE_LAST_TRIP: if service == SERVICE_LAST_TRIP:
for attribute_name in vehicle.state.last_trip.available_attributes: for attribute_name in vehicle.state.last_trip.available_attributes:
if attribute_name == "date": if attribute_name == "date":
device = BMWConnectedDriveSensor( device = BMWConnectedDriveSensor(
hass,
account, account,
vehicle, vehicle,
"date_utc", "date_utc",
attribute_info,
service, service,
) )
entities.append(device) entities.append(device)
else: else:
device = BMWConnectedDriveSensor( device = BMWConnectedDriveSensor(
account, vehicle, attribute_name, attribute_info, service hass, account, vehicle, attribute_name, service
) )
entities.append(device) entities.append(device)
if service == SERVICE_ALL_TRIPS: if service == SERVICE_ALL_TRIPS:
for attribute_name in vehicle.state.all_trips.available_attributes: for attribute_name in vehicle.state.all_trips.available_attributes:
if attribute_name == "reset_date": if attribute_name == "reset_date":
device = BMWConnectedDriveSensor( device = BMWConnectedDriveSensor(
hass,
account, account,
vehicle, vehicle,
"reset_date_utc", "reset_date_utc",
attribute_info,
service, service,
) )
entities.append(device) entities.append(device)
@ -458,36 +419,36 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
"user_average", "user_average",
): ):
device = BMWConnectedDriveSensor( device = BMWConnectedDriveSensor(
hass,
account, account,
vehicle, vehicle,
f"{attribute_name}_{attr}", f"{attribute_name}_{attr}",
attribute_info,
service, service,
) )
entities.append(device) entities.append(device)
if attribute_name == "chargecycle_range": if attribute_name == "chargecycle_range":
for attr in ("user_current_charge_cycle", "user_high"): for attr in ("user_current_charge_cycle", "user_high"):
device = BMWConnectedDriveSensor( device = BMWConnectedDriveSensor(
hass,
account, account,
vehicle, vehicle,
f"{attribute_name}_{attr}", f"{attribute_name}_{attr}",
attribute_info,
service, service,
) )
entities.append(device) entities.append(device)
if attribute_name == "total_electric_distance": if attribute_name == "total_electric_distance":
for attr in ("user_total",): for attr in ("user_total",):
device = BMWConnectedDriveSensor( device = BMWConnectedDriveSensor(
hass,
account, account,
vehicle, vehicle,
f"{attribute_name}_{attr}", f"{attribute_name}_{attr}",
attribute_info,
service, service,
) )
entities.append(device) entities.append(device)
else: else:
device = BMWConnectedDriveSensor( device = BMWConnectedDriveSensor(
account, vehicle, attribute_name, attribute_info, service hass, account, vehicle, attribute_name, service
) )
entities.append(device) entities.append(device)
@ -497,7 +458,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
class BMWConnectedDriveSensor(BMWConnectedDriveBaseEntity, SensorEntity): class BMWConnectedDriveSensor(BMWConnectedDriveBaseEntity, SensorEntity):
"""Representation of a BMW vehicle sensor.""" """Representation of a BMW vehicle sensor."""
def __init__(self, account, vehicle, attribute: str, attribute_info, service=None): def __init__(self, hass, account, vehicle, attribute: str, service=None):
"""Initialize BMW vehicle sensor.""" """Initialize BMW vehicle sensor."""
super().__init__(account, vehicle) super().__init__(account, vehicle)
@ -509,19 +470,16 @@ class BMWConnectedDriveSensor(BMWConnectedDriveBaseEntity, SensorEntity):
else: else:
self._attr_name = f"{vehicle.name} {attribute}" self._attr_name = f"{vehicle.name} {attribute}"
self._attr_unique_id = f"{vehicle.vin}-{attribute}" self._attr_unique_id = f"{vehicle.vin}-{attribute}"
self._attribute_info = attribute_info self._attribute_info = SENSOR_TYPES.get(
self._attr_entity_registry_enabled_default = attribute_info.get( attribute, (None, None, None, None, True)
attribute, [None, None, None, True] )
)[3] self._attr_entity_registry_enabled_default = self._attribute_info[4]
self._attr_icon = self._attribute_info.get( self._attr_icon = self._attribute_info[0]
self._attribute, [None, None, None, None] self._attr_device_class = self._attribute_info[1]
)[0] if hass.config.units.name == CONF_UNIT_SYSTEM_IMPERIAL:
self._attr_device_class = attribute_info.get( self._attr_native_unit_of_measurement = self._attribute_info[3]
attribute, [None, None, None, None] else:
)[1] self._attr_native_unit_of_measurement = self._attribute_info[2]
self._attr_native_unit_of_measurement = attribute_info.get(
attribute, [None, None, None, None]
)[2]
def update(self) -> None: def update(self) -> None:
"""Read new state data from the library.""" """Read new state data from the library."""