diff --git a/homeassistant/components/influxdb/__init__.py b/homeassistant/components/influxdb/__init__.py index e496d3fa71f2..0daf6a542cb4 100644 --- a/homeassistant/components/influxdb/__init__.py +++ b/homeassistant/components/influxdb/__init__.py @@ -12,7 +12,7 @@ from typing import Any, override from influxdb import InfluxDBClient, exceptions from influxdb_client import InfluxDBClient as InfluxDBClientV2 -from influxdb_client.client.write_api import ASYNCHRONOUS, SYNCHRONOUS +from influxdb_client.client.write_api import SYNCHRONOUS from influxdb_client.rest import ApiException import probatio import requests.exceptions @@ -391,8 +391,8 @@ def get_influx_connection( # noqa: C901 bucket = conf.get(CONF_BUCKET) influx = InfluxDBClientV2(**kwargs) query_api = influx.query_api() - initial_write_mode = SYNCHRONOUS if test_write else ASYNCHRONOUS - write_api = influx.write_api(write_options=initial_write_mode) + # Asynchronous writes hide errors from the retry logic of InfluxThread + write_api = influx.write_api(write_options=SYNCHRONOUS) def write_v2(json): """Write data to V2 influx.""" @@ -431,7 +431,6 @@ def get_influx_connection( # noqa: C901 # Then invalid inputs is returned. Anything else is a broken config with suppress(ValueError): write_v2(b"") - write_api = influx.write_api(write_options=ASYNCHRONOUS) if test_read: tables = query_v2(TEST_QUERY_V2) diff --git a/tests/components/influxdb/test_init.py b/tests/components/influxdb/test_init.py index c9aaf3a97f63..be0ff5ebe837 100644 --- a/tests/components/influxdb/test_init.py +++ b/tests/components/influxdb/test_init.py @@ -11,6 +11,7 @@ import time from typing import Any from unittest.mock import ANY, MagicMock, Mock, call, patch +from influxdb_client.client.write_api import SYNCHRONOUS import probatio import pytest from pytest_homeassistant.common import MockConfigEntry @@ -1676,6 +1677,30 @@ async def test_event_listener_scheduled_write( assert write_api.call_count == 3 +@pytest.mark.parametrize( + ("hass_config", "mock_client"), + [({"influxdb": {}}, influxdb.API_VERSION_2)], + indirect=["mock_client"], +) +async def test_v2_write_api_is_synchronous( + hass: HomeAssistant, mock_client: MagicMock +) -> None: + """Test the V2 write API is synchronous so write errors reach the retry logic.""" + mock_entry = MockConfigEntry(domain=DOMAIN, data=BASE_V2_CONFIG) + mock_entry.add_to_hass(hass) + + await hass.config_entries.async_setup(mock_entry.entry_id) + await hass.async_block_till_done() + + hass.states.async_set("entity.entity_id", 1) + await hass.async_block_till_done() + await async_wait_for_queue_to_process(hass) + + mock_client.return_value.write_api.assert_called_once_with( + write_options=SYNCHRONOUS + ) + + @pytest.mark.parametrize( ("hass_config", "mock_client", "config_ext", "get_write_api", "get_mock_call"), [