From 92f89213a36238763e6dabc57302c28fdbbb54de Mon Sep 17 00:00:00 2001 From: Rob Bierbooms Date: Fri, 16 Oct 2020 16:44:50 +0200 Subject: [PATCH] Fix InfluxDB v2 API with write precision None (#41937) --- homeassistant/components/influxdb/__init__.py | 7 ++++++- tests/components/influxdb/test_init.py | 13 ++++++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/influxdb/__init__.py b/homeassistant/components/influxdb/__init__.py index 057d0657685..5696424b402 100644 --- a/homeassistant/components/influxdb/__init__.py +++ b/homeassistant/components/influxdb/__init__.py @@ -342,8 +342,13 @@ def get_influx_connection(conf, test_write=False, test_read=False): def write_v2(json): """Write data to V2 influx.""" + data = {"bucket": bucket, "record": json} + + if precision is not None: + data["write_precision"] = precision + try: - write_api.write(bucket=bucket, record=json, write_precision=precision) + write_api.write(**data) except (urllib3.exceptions.HTTPError, OSError) as exc: raise ConnectionError(CONNECTION_ERROR % exc) from exc except ApiException as exc: diff --git a/tests/components/influxdb/test_init.py b/tests/components/influxdb/test_init.py index 06ec3725195..da405e3975b 100644 --- a/tests/components/influxdb/test_init.py +++ b/tests/components/influxdb/test_init.py @@ -61,10 +61,17 @@ def mock_client_fixture(request): @pytest.fixture(name="get_mock_call") def get_mock_call_fixture(request): """Get version specific lambda to make write API call mock.""" + + def v2_call(body, precision): + data = {"bucket": DEFAULT_BUCKET, "record": body} + + if precision is not None: + data["write_precision"] = precision + + return call(**data) + if request.param == influxdb.API_VERSION_2: - return lambda body, precision=None: call( - bucket=DEFAULT_BUCKET, record=body, write_precision=precision - ) + return lambda body, precision=None: v2_call(body, precision) # pylint: disable=unnecessary-lambda return lambda body, precision=None: call(body, time_precision=precision)