andyb2000 / HACS-husqvarna_automower_ble

HACS custom component for husqvarna_automower_ble
14 stars 3 forks source link

Error decoding statistics array #4

Closed andyb2000 closed 4 months ago

andyb2000 commented 4 months ago

The array for statistics is causing difficult decoding it. Probably need to use a loop.

Current attempt:

            for entry in self.coordinator.data["statistics"]:
                if entry.split()[self.entity_description.key]:
                    self._attr_native_value = entry.split()[self.entity_description.key]
                    self._attr_available = self._attr_native_value is not None
                    _LOGGER.debug("Update sensor %s with value %s", self.entity_description.key, self._attr_native_value)
                    return self._attr_native_value

Fails:

2024-07-13 16:18:11.705 DEBUG (MainThread) [custom_components.husqvarna_automower_ble.sensor] state of sensor data structure: {'battery_level': 100, 'activity': <MowerActivity.PARKED: 5>, 'state': <MowerState.RESTRICTED: 7>, 'next_start_time': datetime.datetime(2024, 7, 14, 12, 0, tzinfo=datetime.timezone.utc), 'statistics': {'totalRunningTime': 2662242, 'totalCuttingTime': 2526858, 'totalChargingTime': 814977, 'totalSearchingTime': 121809, 'numberOfCollisions': 12774, 'numberOfChargingCycles': 315, 'cuttingBladeUsageTime': 2526858}, 'operatorstate': 0}
2024-07-13 16:18:11.705 DEBUG (MainThread) [custom_components.husqvarna_automower_ble.sensor] value of sensor data structure: 2024-07-14 12:00:00+00:00
2024-07-13 16:18:11.705 ERROR (MainThread) [custom_components.husqvarna_automower_ble.sensor] totalRunningTime not a valid attribute (in _update_attr)
2024-07-13 16:18:11.705 DEBUG (MainThread) [custom_components.husqvarna_automower_ble.sensor] Attempting deep search in array for key
2024-07-13 16:18:11.706 ERROR (MainThread) [homeassistant] Error doing job: Task exception was never retrieved (None)
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 255, in _handle_refresh_interval
    await self._async_refresh(log_failures=True, scheduled=True)
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 411, in _async_refresh
    self.async_update_listeners()
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 165, in async_update_listeners
    update_callback()
  File "/config/custom_components/husqvarna_automower_ble/sensor.py", line 199, in _handle_coordinator_update
    self._update_attr()
  File "/config/custom_components/husqvarna_automower_ble/sensor.py", line 223, in _update_attr
    if entry.split()[self.entity_description.key]:
       ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: list indices must be integers or slices, not str
andyb2000 commented 4 months ago

Resolved in push #6