Skip to content

Commit

Permalink
More logging, rework unit conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
rikroe committed Nov 16, 2021
1 parent 7f2de75 commit 9c61a1e
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 26 deletions.
1 change: 1 addition & 0 deletions custom_components/bmw_connected_drive/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ def __init__(

def update(self) -> None:
"""Read new state data from the library."""
_LOGGER.debug("Updating binary sensors of %s", self._vehicle.name)
vehicle_state = self._vehicle.status
result = self._attrs.copy()

Expand Down
1 change: 1 addition & 0 deletions custom_components/bmw_connected_drive/device_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ def source_type(self) -> Literal["gps"]:

def update(self) -> None:
"""Update state of the decvice tracker."""
_LOGGER.debug("Updating device tracker of %s", self._vehicle.name)
self._attr_extra_state_attributes = self._attrs
self._location = (
self._vehicle.status.gps_position
Expand Down
2 changes: 1 addition & 1 deletion custom_components/bmw_connected_drive/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def unlock(self, **kwargs: Any) -> None:

def update(self) -> None:
"""Update state of the lock."""
_LOGGER.debug("%s: updating data for %s", self._vehicle.name, self._attribute)
_LOGGER.debug("Updating lock data for '%s' of %s", self._attribute, self._vehicle.name)
vehicle_state = self._vehicle.status
if not self.door_lock_state_available:
self._attr_is_locked = None
Expand Down
2 changes: 1 addition & 1 deletion custom_components/bmw_connected_drive/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"domain": "bmw_connected_drive",
"name": "BMW Connected Drive",
"version": "20211115.2",
"version": "20211116.2",
"documentation": "https://www.home-assistant.io/integrations/bmw_connected_drive",
"requirements": ["bimmer_connected==0.8.0.0b7"],
"codeowners": ["@gerard33", "@rikroe"],
Expand Down
43 changes: 19 additions & 24 deletions custom_components/bmw_connected_drive/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,8 @@ async def async_setup_entry(
BMWConnectedDriveSensor(
account, vehicle, description, unit_system
)
for attribute_name in vehicle.drive_train_attributes
if attribute_name in vehicle.available_attributes
and (description := SENSOR_TYPES.get(attribute_name))
for attribute_name in vehicle.available_attributes
if (description := SENSOR_TYPES.get(attribute_name))
]
)

Expand Down Expand Up @@ -181,7 +180,7 @@ def __init__(

def update(self) -> None:
"""Read new state data from the library."""
_LOGGER.debug("Updating %s", self._vehicle.name)
_LOGGER.debug("Updating sensors of %s", self._vehicle.name)
vehicle_state = self._vehicle.status
sensor_key = self.entity_description.key
sensor_value = None
Expand All @@ -190,34 +189,29 @@ def update(self) -> None:
sensor_value = getattr(vehicle_state, sensor_key).value
elif self._service is None:
sensor_value = getattr(vehicle_state, sensor_key)
if sensor_key in ["remaining_range_electric"]:
_LOGGER.debug(
"sensor value for '%s' is '%s'. Source data: '%s'",
sensor_key,
sensor_value, vehicle_state.status["fuelIndicators"]
)

if sensor_key in ["charging_level_hv"]:
_LOGGER.debug(
"sensor value for '%s' is '%s'. Source data: '%s'",
sensor_key,
sensor_value,
vehicle_state.properties["electricRangeAndStatus"]["chargePercentage"]
)

if isinstance(sensor_value, tuple):
sensor_unit = UNIT_MAP.get(sensor_value[1], sensor_value[1])
if sensor_unit == self.unit_of_measurement:
sensor_value = sensor_value[0]
elif self.unit_of_measurement in [LENGTH_KILOMETERS, LENGTH_MILES]:
sensor_value = sensor_value[0]
sensor_value_org = sensor_value

if self.unit_of_measurement in [LENGTH_KILOMETERS, LENGTH_MILES]:
sensor_value = round(
self.hass.config.units.length(sensor_value[0], sensor_unit)
self.hass.config.units.length(sensor_value, sensor_unit)
)
elif self.unit_of_measurement in [VOLUME_LITERS, VOLUME_GALLONS]:
sensor_value = round(
self.hass.config.units.volume(sensor_value[0], sensor_unit)
self.hass.config.units.volume(sensor_value, sensor_unit)
)
self._attr_native_value = sensor_value

_LOGGER.debug(
"UoM Conversion for %s. HA: %s %s, Vehicle: %s %s.",
sensor_key,
sensor_value, self.unit_of_measurement,
sensor_value_org, sensor_unit
)

self._attr_native_value = sensor_value

if sensor_key == "charging_level_hv":
charging_state = self._vehicle.status.charging_status in {
Expand All @@ -226,3 +220,4 @@ def update(self) -> None:
self._attr_icon = icon_for_battery_level(
battery_level=vehicle_state.charging_level_hv, charging=charging_state
)

0 comments on commit 9c61a1e

Please sign in to comment.