daernsinstantfortress / cupra_we_connect

Cupra integration for Home Assistant
Apache License 2.0
59 stars 8 forks source link

Error with Vehicles with no Odometer set #77

Open neubi4 opened 3 months ago

neubi4 commented 3 months ago

After fixing Cupra Hybrids with daernsinstantfortress/WeConnect-Cupra-python/pull/13, the next error i get after configuring the extension is this:

  File "/home/nux/hass/core/config/custom_components/cupra_we_connect/sensor.py", line 209, in <lambda>
    value=lambda data: data["measurements"][
                       ~~~~^^^^^^^^^^^^^^^^

It seems that the API Lib can not get the Odometer Measurements from the API and so does not set the data here: WeConnect-Cupra-python/blob/main/weconnect_cupra/api/cupra/elements/vehicle.py#L268

Outout of the data object of self.data from cupra_we_connect/sensor.py#L268 of my hybrid:

VIN:               VSSZZZ...
Nickname:          CUPRA Leon
Role:              UNKNOWN
Enrollment Status: completed
Capabilities: 1 items
        [dealerAppointment] Status:  disabling: None (expires 2054-01-16T13:25:00+00:00)
Domains: 3 items
[charging] Elements: 4 items
        [chargingSettings] (last captured 2024-08-04T12:37:49+00:00) (last updated 2024-08-04T12:39:52+00:00)
                Maximum Charge Current AC: maximum
                Target SoC: 100.0 %
        [chargingStatus] (last captured 2024-08-04T12:37:51+00:00) (last updated 2024-08-04T12:39:52+00:00)
                State: charging
                Mode: manual
                Remaining Charging Time: 270 minutes
                Charge Type: unknown charge type
        [batteryStatus] (last captured 2024-08-04T12:37:51+00:00) (last updated 2024-08-04T12:39:52+00:00)
                Current SoC: 46%
                Range: 23km
        [plugStatus] (last captured 2024-08-04T12:37:51+00:00) (last updated 2024-08-04T12:39:52+00:00)
                Plug: connected, locked, External Power: ready
[climatisation] Elements: 3 items
        [climatisationStatus] (last captured 2024-08-04T11:50:20+00:00) (last updated 2024-08-04T12:39:52+00:00)
                State: off
        [windowHeatingStatus] (last captured 2024-08-04T11:50:21+00:00) (last updated 2024-08-04T12:39:52+00:00)
                Windows: 2 items
                        front: off
                        rear: off
        [climatisationSettings] (last captured 2024-07-23T13:53:22+00:00) (last updated 2024-08-04T12:39:52+00:00)
                Target Temperature in °C: 22.0 °C
                Target Temperature in °F: 72.0 °F
                Climatization without external Power: True
[status] Elements: 1 items
        [connectionStatus]
                Connection state: online

So i think we need to skip/disable the Odometer Sensors if data["measurements"] is not set.

I am not deep enough in home assistant extension development to see how i can do this.

Full Error Stack Trace:

2024-08-04 14:39:52.856 ERROR (MainThread) [homeassistant] Error doing job: Task exception was never retrieved (None)
Traceback (most recent call last):
  File "/home/nux/hass/core/homeassistant/helpers/update_coordinator.py", line 258, in _handle_refresh_interval
    await self._async_refresh(log_failures=True, scheduled=True)
  File "/home/nux/hass/core/homeassistant/helpers/update_coordinator.py", line 453, in _async_refresh
    self.async_update_listeners()
  File "/home/nux/hass/core/homeassistant/helpers/update_coordinator.py", line 168, in async_update_listeners
    update_callback()
  File "/home/nux/hass/core/homeassistant/helpers/update_coordinator.py", line 533, in _handle_coordinator_update
    self.async_write_ha_state()
  File "/home/nux/hass/core/homeassistant/helpers/entity.py", line 1005, in async_write_ha_state
    self._async_write_ha_state()
  File "/home/nux/hass/core/homeassistant/helpers/entity.py", line 1130, in _async_write_ha_state
    self.__async_calculate_state()
  File "/home/nux/hass/core/homeassistant/helpers/entity.py", line 1067, in __async_calculate_state
    state = self._stringify_state(available)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/nux/hass/core/homeassistant/helpers/entity.py", line 1011, in _stringify_state
    if (state := self.state) is None:
                 ^^^^^^^^^^
  File "/home/nux/hass/core/homeassistant/components/sensor/__init__.py", line 542, in state
    value = self.native_value
            ^^^^^^^^^^^^^^^^^
  File "/home/nux/hass/core/config/custom_components/cupra_we_connect/sensor.py", line 270, in native_value
    state = get_object_value(self.entity_description.value(self.data.domains))
                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/nux/hass/core/config/custom_components/cupra_we_connect/sensor.py", line 209, in <lambda>
    value=lambda data: data["measurements"][
                       ~~~~^^^^^^^^^^^^^^^^
KeyError: 'measurements'