jnxxx / homeassistant-connectedcars_io

Home Assistant integration for connectedcars.io (Min Volkswagen)
17 stars 2 forks source link

Update for `sensor.audi_a5_mileage_latest_month` fails (prerelease v1.1.0) #10

Closed mm98 closed 1 year ago

mm98 commented 1 year ago

Hi,

I'm using the latest prerelease v1.1.0. Every time I refuel my car, I get an error on sensor.audi_a5_mileage_latest_month. It almost looks like it can't read the value after the first few miles. It will start working the next day (sometimes) or if i travel like +50 miles. Currently traveled 24km since refuel.

At the same time sensor.audi_a5_mileage_since_refuel has become unavailable.

(Note: The strange thing is, that the Economy sensor is read/loading correctly)

This error originated from a custom integration.

Logger: homeassistant.helpers.entity
Source: custom_components/connectedcars_io/minvw/minvw.py:65
Integration: Connectedcars.io (Min Volkswagen) (documentation, issues)
First occurred: 10:01:31 (1 occurrences)
Last logged: 10:01:31

Update for sensor.audi_a5_mileage_latest_month fails
Traceback (most recent call last):
  File "/usr/local/lib/python3.11/site-packages/aiohttp/connector.py", line 1152, in _create_direct_connection
    hosts = await asyncio.shield(host_resolved)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/aiohttp/connector.py", line 874, in _resolve_host
    addrs = await self._resolver.resolve(host, port, family=self._family)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/aiohttp/resolver.py", line 33, in resolve
    infos = await self._loop.getaddrinfo(
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/asyncio/base_events.py", line 867, in getaddrinfo
    return await self.run_in_executor(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/socket.py", line 962, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
socket.gaierror: [Errno -3] Try again

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 559, in async_update_ha_state
    await self.async_device_update()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 784, in async_device_update
    await self.async_update()
  File "/config/custom_components/connectedcars_io/sensor.py", line 333, in async_update
    ) = await self._connectedcarsclient.get_latest_years_mileage(
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/connectedcars_io/minvw/minvw.py", line 96, in get_latest_years_mileage
    vehicle_data = await self.api_request(req_param)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/connectedcars_io/minvw/minvw.py", line 65, in api_request
    async with session.post(
  File "/usr/local/lib/python3.11/site-packages/aiohttp/client.py", line 1141, in __aenter__
    self._resp = await self._coro
                 ^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/aiohttp/client.py", line 536, in _request
    conn = await self._connector.connect(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/aiohttp/connector.py", line 540, in connect
    proto = await self._create_connection(req, traces, timeout)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/aiohttp/connector.py", line 901, in _create_connection
    _, proto = await self._create_direct_connection(req, traces, timeout)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/aiohttp/connector.py", line 1166, in _create_direct_connection
    raise ClientConnectorError(req.connection_key, exc) from exc
aiohttp.client_exceptions.ClientConnectorError: Cannot connect to host api.connectedcars.io:443 ssl:default [Try again]
jnxxx commented 1 year ago

I haven't been able to reproduce this. I can see why mileage_since_refuel can become unavailable, but I do not see why it would affect mileage_latest_month.

Anyways, I have added some more error handling and rewritten the code for mileage_since_refuel in an attempt to make it update faster. It previously used a feature in the API named totalTripStatistics. It is quite convenient, but to my surprise it seems to update overnight only. Instead I have made it find the odometer value at refuel time, and then subtract that from the current odometer value. However, the odometer can take a while to update also, but do update faster than totalTripStatistics.

I'll do some more testing tomorrow, and if satisfactory make an update of the preview version.

mm98 commented 1 year ago

These error could actually be timeouts by api.connectedcars.io graphql .. Not a big thing for me, I just wanted to report it. Since it is a new one this time 😊

This error originated from a custom integration.

Logger: homeassistant.helpers.entity
Source: custom_components/connectedcars_io/minvw/minvw.py:68
Integration: Connectedcars.io (Min Volkswagen) (documentation, issues)
First occurred: 11:38:24 (1 occurrences)
Last logged: 11:38:24

Update for sensor.audi_a5_mileage_since_refuel fails

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 559, in async_update_ha_state
    await self.async_device_update()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 784, in async_device_update
    await self.async_update()
  File "/config/custom_components/connectedcars_io/sensor.py", line 344, in async_update
    ) = await self._connectedcarsclient.get_mileage_since_refuel(
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/connectedcars_io/minvw/minvw.py", line 140, in get_mileage_since_refuel
    vehicle_data = await self.api_request(req_param)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/connectedcars_io/minvw/minvw.py", line 68, in api_request
    ret = await response.json()
          ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/aiohttp/client_reqrep.py", line 1104, in json
    raise ContentTypeError(
aiohttp.client_exceptions.ContentTypeError: 0, message='Attempt to decode JSON with unexpected mimetype: text/html', url=URL('https://api.connectedcars.io/graphql')
jnxxx commented 1 year ago

My test wasn't really conclusive as the API didn't detect the refuel event... maybe too little fuel was added. Apart from that it seems good and quicker than before, so I have published the latest changes now. If you want to update to it, redownload v1.1.0.

mm98 commented 1 year ago

If you want to update to it, redownload v1.1.0.

Thanks @jnxxx !! I'll give it a tryand report back, if I find something :)