Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot write to closing transport - when pressing preclimate buttons #156

Closed
TheGroundZero opened this issue Dec 11, 2023 · 12 comments
Closed
Labels
bug Something isn't working

Comments

@TheGroundZero
Copy link

Pressing the Preclimate start/stop buttons, I get an error popping up Cannot write to closing transport.
The service call does seem to work (at least the 2nd time I tried it).

HA logs show:

Logger: homeassistant.components.websocket_api.http.connection
Source: components/websocket_api/commands.py:238
Integration: Home Assistant WebSocket API (documentation, issues)
First occurred: 15:22:49 (2 occurrences)
Last logged: 15:22:57

[140541921440448] Cannot write to closing transport
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/websocket_api/commands.py", line 238, in handle_call_service
    response = await hass.services.async_call(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/core.py", line 2067, in async_call
    response_data = await coro
                    ^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/core.py", line 2104, in _execute_service
    return await target(service_call)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/entity_component.py", line 272, in handle_service
    return await service.entity_service_call(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 878, in entity_service_call
    single_response = await _handle_entity_call(
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 948, in _handle_entity_call
    result = await task
             ^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/button/__init__.py", line 124, in _async_press_action
    await self.async_press()
  File "/config/custom_components/mbapi2020/button.py", line 56, in async_press
    await service(self._vin)
  File "/config/custom_components/mbapi2020/client.py", line 796, in preheat_start
    await self.websocket.call(message.SerializeToString())
  File "/config/custom_components/mbapi2020/websocket.py", line 213, in call
    await self._connection.send_bytes(message)
  File "/usr/local/lib/python3.11/site-packages/aiohttp/client_ws.py", line 170, in send_bytes
    await self._writer.send(data, binary=True, compress=compress)
  File "/usr/local/lib/python3.11/site-packages/aiohttp/http_websocket.py", line 727, in send
    await self._send_frame(message, WSMsgType.BINARY, compress)
  File "/usr/local/lib/python3.11/site-packages/aiohttp/http_websocket.py", line 675, in _send_frame
    self._write(header + mask + message)
  File "/usr/local/lib/python3.11/site-packages/aiohttp/http_websocket.py", line 702, in _write
    raise ConnectionResetError("Cannot write to closing transport")
ConnectionResetError: Cannot write to closing transport

PS: any plans on turning this into a switch instead of 2 buttons?
Would help me get rid of these templates :)

template:
  - binary_sensor:
    - name: Car Preconditioning
      unique_id: car_preconditioning
      device_class: running
      state: >-
          {{ state_attr('sensor.licenseplate_range_electric', 'precondActive') 
          or state_attr('sensor.licenseplate_range_electric', 'precondNow') }}
      attributes:
        precondActive: "{{ state_attr('sensor.licenseplate_range_electric', 'precondActive') }}"
        precondNow: "{{ state_attr('sensor.licenseplate_range_electric', 'precondNow') }}"

switch:
  - platform: template
    switches:
      car_preconditioning_active:
        friendly_name: Preconditioning active
        unique_id: car_preconditioning_active
        value_template: "{{ is_state('binary_sensor.car_preconditioning', 'on') }}"
        turn_on:
          service: button.press
          target:
            entity_id: button.licenseplate_preclimate_start
        turn_off:
          service: button.press
          target:
            entity_id: button.licenseplate_preclimate_stop
        # turn_on:
        #   service: mbapi2020.preheat_start
        #   data:
        #     type: "0"
        #     vin: !secret car_vin
        # turn_off:
        #   service: mbapi2020.preheat_stop
        #   data:
        #     vin: !secret car_vin
        icon_template: >-
          {%- if this.state == "on" %}
            mdi:air-conditioner
          {%- else %}
            mdi:car-off
          {% endif %}
@ReneNulschDE
Copy link
Owner

Hi @TheGroundZero,

interesting... The button calls the service function in my code. Does this happen everytime? Could you enable the debug log, this would help to check if the connection is closed already. (you can send it to mb.dev.issue156@nulsch.de )

@TheGroundZero
Copy link
Author

So far I only saw it occur once, with both buttons.

So I pressed "start" -> error. Then pressed "stop" -> error.
Then I fiddled around a little and tried again, and I don't think I got an error that time round.

@ReneNulschDE
Copy link
Owner

I propose that we monitor this. I got this multiple times in my dev environments in the context of short network interruptions. Therefore the component has some fallback code to recreate the websocket connection already.

@ReneNulschDE ReneNulschDE added the bug Something isn't working label Dec 14, 2023
@ReneNulschDE
Copy link
Owner

Looks like I have found the problem. A solution will be part of the release v0.10... in the next few days/weeks...

@TheGroundZero
Copy link
Author

Got the same error today again.

I noticed car_charging_active and max_state_of_charge (and probably all the rest as well) didn't update.
I wanted to force a refresh by changing the max_state_of_charge via the battery_max_soc_configure service.

I have an input_number with a range 50-100 and moving it, triggers and automation that calls the service.
After doing so, I got the following error in the logs:

Logger: homeassistant.components.automation.car_battery_target
Source: components/automation/__init__.py:666
Integration: Automation ([documentation](https://www.home-assistant.io/integrations/automation), [issues](https://github.com/home-assistant/core/issues?q=is%3Aissue+is%3Aopen+label%3A%22integration%3A+automation%22))
First occurred: 15:05:47 (1 occurrences)
Last logged: 15:05:47
While executing automation automation.car_battery_target

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/automation/__init__.py", line 666, in async_trigger
    return await self.action_script.async_run(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 1600, in async_run
    return await asyncio.shield(run.async_run())
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 435, in async_run
    await self._async_step(log_exceptions=False)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 487, in _async_step
    self._handle_exception(
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 512, in _handle_exception
    raise exception
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 485, in _async_step
    await getattr(self, handler)()
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 943, in _async_choose_step
    await self._async_run_script(script)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 1100, in _async_run_script
    result = await self._async_run_long_action(
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 685, in _async_run_long_action
    return long_task.result()
           ^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 1600, in async_run
    return await asyncio.shield(run.async_run())
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 435, in async_run
    await self._async_step(log_exceptions=False)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 487, in _async_step
    self._handle_exception(
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 512, in _handle_exception
    raise exception
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 485, in _async_step
    await getattr(self, handler)()
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 723, in _async_call_service_step
    response_data = await self._async_run_long_action(
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 685, in _async_run_long_action
    return long_task.result()
           ^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/core.py", line 2279, in async_call
    response_data = await coro
                    ^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/core.py", line 2316, in _execute_service
    return await target(service_call)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/mbapi2020/services.py", line 146, in battery_max_soc_configure
    await domain[_get_config_entryid(call.data.get(CONF_VIN))].client.battery_max_soc_configure(
  File "/config/custom_components/mbapi2020/client.py", line 716, in battery_max_soc_configure
    await self.websocket.call(message.SerializeToString())
  File "/config/custom_components/mbapi2020/websocket.py", line 223, in call
    await self._connection.send_bytes(message)
  File "/usr/local/lib/python3.12/site-packages/aiohttp/client_ws.py", line 170, in send_bytes
    await self._writer.send(data, binary=True, compress=compress)
  File "/usr/local/lib/python3.12/site-packages/aiohttp/http_websocket.py", line 727, in send
    await self._send_frame(message, WSMsgType.BINARY, compress)
  File "/usr/local/lib/python3.12/site-packages/aiohttp/http_websocket.py", line 675, in _send_frame
    self._write(header + mask + message)
  File "/usr/local/lib/python3.12/site-packages/aiohttp/http_websocket.py", line 702, in _write
    raise ConnectionResetError("Cannot write to closing transport")
ConnectionResetError: Cannot write to closing transport

@TheGroundZero
Copy link
Author

My logs also shows a few of these:

Logger: homeassistant
Source: custom_components/mbapi2020/client.py:374
Integration: MercedesME 2020 ([documentation](https://github.com/ReneNulschDE/mbapi2020), [issues](https://github.com/ReneNulschDE/mbapi2020/issues))
First occurred: 4 March 2024 at 14:04:14 (3 occurrences)
Last logged: 14:25:12
Error doing job: Task exception was never retrieved

Traceback (most recent call last):
  File "/config/custom_components/mbapi2020/websocket.py", line 212, in _recv
    ack_message = self._on_data_received(message)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/mbapi2020/client.py", line 128, in on_data
    self._process_vep_updates(data)
  File "/config/custom_components/mbapi2020/client.py", line 436, in _process_vep_updates
    self._build_car(current_car, update_mode=True)
  File "/config/custom_components/mbapi2020/client.py", line 272, in _build_car
    car.electric = self._get_car_values(
                   ^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/mbapi2020/client.py", line 374, in _get_car_values
    display_value=curr_display_value,
                  ^^^^^^^^^^^^^^^^^^
UnboundLocalError: cannot access local variable 'curr_display_value' where it is not associated with a value

@ReneNulschDE ReneNulschDE reopened this Mar 5, 2024
@ReneNulschDE
Copy link
Owner

ReneNulschDE commented Mar 5, 2024

cannot access local variable 'curr_display_value' --> this one I had this morning too and it is fixed in the master branch.

Let me check the other one again. On what system (HW, SW) is your HA running? And what is your current version of the MBAPI component?

@TheGroundZero
Copy link
Author

On what system (HW, SW) is your HA running? And what is your current version of the MBAPI component?

Fedora CoreOS with HA Container managed via Podman.
Core 2024.2.5
v0.11.1 of MBAPI

@ReneNulschDE
Copy link
Owner

ReneNulschDE commented Mar 5, 2024

Is this a fast machine? I'm currently analyzing a different topic but the result could be the same. Looks like that sometimes the MB servers sending so many messages and my code is not fast enough to handle all this incoming traffic... Just setting up a slow PI3 to test this...

@TheGroundZero
Copy link
Author

It's a Beelink Mini S (Intel N5095)
I do sporadically have some network issues, but afaik it only interferes with my WiFi.

@ReneNulschDE
Copy link
Owner

I have reworked the connection logic to the MB servers. The data receiver and data processor are decoupled now and this should prevent "shadow"/"phantom" connections. Is part of v0.11.2-beta.4

@ReneNulschDE
Copy link
Owner

Also part of v0.12.0 - Please recheck. I'll close it for now. feel free to reopen it again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants