alandtse / tesla

Tesla custom integration for Home Assistant. This requires a refresh token be generated by third-party apps to login.
Apache License 2.0
588 stars 100 forks source link

[Errno -5] Name has no usable address #1015

Open gjaegy opened 2 months ago

gjaegy commented 2 months ago

Is there an existing issue for this?

I have read about the Fleet API and understand I may need to use it

Version of the Tesla component

3.24

Version of the Tesla car software

2024.20.6.2

Model

Model Y

Current Behavior

I'm using an automation to adjust the charging amps depending on my solar production excess. I'm still using the legacy API (not the fleet API). The automation run in a loop but randomly stops after some time (can be 10 minutes or 5 hours) because of the following error when trying to issue a command: [Errno -5] Name has no usable address

Expected Behavior

The command should be issue with no error.

Debug logs

Enregistreur: homeassistant.components.automation.myteslacarcharger_flashg
Source: helpers/script.py:527
intégration: Automatisation (documentation, problèmes)
S'est produit pour la première fois: 07:52:22 (14 occurrences)
Dernier enregistrement: 15:03:22

MyTeslaCarCharger(flashg): Repeat at step 7: Error executing script. Unexpected error for choose at pos 3: [Errno -5] Name has no usable address
MyTeslaCarCharger(flashg): Error executing script. Unexpected error for repeat at pos 7: [Errno -5] Name has no usable address
MyTeslaCarCharger(flashg): Repeat at step 7: Choose at step 3: choice 4: Repeat at step 1: Error executing script. Unexpected error for call_service at pos 1: [Errno -5] Name has no usable address
MyTeslaCarCharger(flashg): Repeat at step 7: Choose at step 3: choice 4: Error executing script. Unexpected error for repeat at pos 1: [Errno -5] Name has no usable address
MyTeslaCarCharger(flashg): Repeat at step 7: Error executing script. Unexpected error for call_service at pos 4: [Errno -5] Name has no usable address
Traceback (most recent call last):
  File "/usr/local/lib/python3.12/site-packages/httpx/_transports/default.py", line 69, in map_httpcore_exceptions
    yield
  File "/usr/local/lib/python3.12/site-packages/httpx/_transports/default.py", line 373, in handle_async_request
    resp = await self._pool.handle_async_request(req)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/httpcore/_async/connection_pool.py", line 216, in handle_async_request
    raise exc from None
  File "/usr/local/lib/python3.12/site-packages/httpcore/_async/connection_pool.py", line 196, in handle_async_request
    response = await connection.handle_async_request(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/httpcore/_async/connection.py", line 99, in handle_async_request
    raise exc
  File "/usr/local/lib/python3.12/site-packages/httpcore/_async/connection.py", line 76, in handle_async_request
    stream = await self._connect(request)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/httpcore/_async/connection.py", line 154, in _connect
    stream = await stream.start_tls(**kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/httpcore/_backends/anyio.py", line 68, in start_tls
    with map_exceptions(exc_map):
  File "/usr/local/lib/python3.12/contextlib.py", line 158, in __exit__
    self.gen.throw(value)
  File "/usr/local/lib/python3.12/site-packages/httpcore/_exceptions.py", line 14, in map_exceptions
    raise to_exc(exc) from exc
httpcore.ConnectError

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

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 527, in _async_step
    await getattr(self, handler)()
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 764, in _async_call_service_step
    response_data = await self._async_run_long_action(
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 727, in _async_run_long_action
    return await long_task
           ^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/core.py", line 2731, in async_call
    response_data = await coro
                    ^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/core.py", line 2774, in _execute_service
    return await target(service_call)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 999, in entity_service_call
    single_response = await _handle_entity_call(
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 1071, in _handle_entity_call
    result = await task
             ^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/button/__init__.py", line 132, in _async_press_action
    await self.async_press()
  File "/config/custom_components/tesla_custom/button.py", line 85, in async_press
    await self.update_controller(wake_if_asleep=True, force=True)
  File "/config/custom_components/tesla_custom/base.py", line 98, in update_controller
    await self.coordinator.controller.update(
  File "/usr/local/lib/python3.12/site-packages/teslajsonpy/controller.py", line 752, in update
    self._product_list = await self.get_product_list()
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/teslajsonpy/controller.py", line 313, in get_product_list
    return (await self.api("PRODUCT_LIST"))["response"]
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/teslajsonpy/controller.py", line 1375, in api
    return await self.__post_with_retries_except_unavailable(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/tenacity/asyncio/__init__.py", line 189, in async_wrapped
    return await copy(fn, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/tenacity/asyncio/__init__.py", line 111, in __call__
    do = await self.iter(retry_state=retry_state)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/tenacity/asyncio/__init__.py", line 153, in iter
    result = await action(retry_state)
             ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/tenacity/_utils.py", line 99, in inner
    return call(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/tenacity/__init__.py", line 418, in exc_check
    raise retry_exc.reraise()
          ^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/tenacity/__init__.py", line 185, in reraise
    raise self.last_attempt.result()
          ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/concurrent/futures/_base.py", line 449, in result
    return self.__get_result()
           ^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/concurrent/futures/_base.py", line 401, in __get_result
    raise self._exception
  File "/usr/local/lib/python3.12/site-packages/tenacity/asyncio/__init__.py", line 114, in __call__
    result = await fn(*args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/teslajsonpy/controller.py", line 1407, in __post_with_retries_except_unavailable
    return await self.__connection.post(command, method=method, data=data, url=url)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/teslajsonpy/connection.py", line 166, in post
    return await self.__open(url, method=method, headers=self.head, data=data)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/teslajsonpy/connection.py", line 207, in __open
    resp: httpx.Response = await getattr(self.websession, method)(
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/httpx/_client.py", line 1801, in get
    return await self.request(
           ^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/httpx/_client.py", line 1574, in request
    return await self.send(request, auth=auth, follow_redirects=follow_redirects)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/httpx/_client.py", line 1661, in send
    response = await self._send_handling_auth(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/httpx/_client.py", line 1689, in _send_handling_auth
    response = await self._send_handling_redirects(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/httpx/_client.py", line 1726, in _send_handling_redirects
    response = await self._send_single_request(request)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/httpx/_client.py", line 1763, in _send_single_request
    response = await transport.handle_async_request(request)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/httpx/_transports/default.py", line 372, in handle_async_request
    with map_httpcore_exceptions():
  File "/usr/local/lib/python3.12/contextlib.py", line 158, in __exit__
    self.gen.throw(value)
  File "/usr/local/lib/python3.12/site-packages/httpx/_transports/default.py", line 86, in map_httpcore_exceptions
    raise mapped_exc(message) from exc
httpx.ConnectError

Anything else?

No response

gjaegy commented 1 month ago

Hi,

Actually this randomly raised error prevent me to use the API in an automation, as the automation simply stops when that exception is being thrown.

I tried to use the "continue_on_error" option, however, it doesn't work because the error is unhandled by the integration (cf. https://github.com/home-assistant/core/issues/123296 for further details).

Would it be possible to fix the integration so that the error gets handled, which would allow me to use it in the automation (I don't really care if the command fails, all I need is the script to continue to run, which would be the case if the error is being handled).

Thanks a lot !

alandtse commented 1 month ago

This error represents a network failure. Meaning we can't connect to tesla or the internet. There is no way for us to continue at this point.

gjaegy commented 1 month ago

Thanks for your answer.

Yes, I understand that.

However, while I'm happy with the fact the command can't be issued, my problem is that the error is not handled in the library (I guess this means the exception is not caught in the code, but I'm not sure about that, having never worked on HA code myself).

This leads to the fact that the automation basically exits, despite the "continue_on_error" options being used.

Would it be possible to fix that somehow, and have the error being "handled" (whatever that means) ?

alandtse commented 1 month ago

Find an example of what HA expects and I imagine someone may pick this up as an enhancement.

gjaegy commented 1 month ago

To be honest, I'm not sure what "handling the error" means in the HA context. Possibly just adding a try/catch block around the function call, I've asked on the HA Core forum (same issue as the one mentioned above), hopefully I'll get some further information about that.

gjaegy commented 1 month ago

OK I got some details (cf. https://github.com/home-assistant/core/issues/123296).

The extension should raise a "defined exception": https://developers.home-assistant.io/docs/dev_101_services/#response-data

and blog here : https://developers.home-assistant.io/blog/2023/11/30/service-exceptions-and-translations/?_highlight=exception

Do you think that's something doable (again, I'm afraid I have zero experience with HA development) ?

gjaegy commented 2 weeks ago

Hi, any update on this one ? I'm happy to help if I can !