diff --git a/homeassistant/components/influxdb/sensor.py b/homeassistant/components/influxdb/sensor.py index 7404ceb4c7f..f9535292e69 100644 --- a/homeassistant/components/influxdb/sensor.py +++ b/homeassistant/components/influxdb/sensor.py @@ -14,6 +14,7 @@ from homeassistant.components.sensor import ( from homeassistant.const import ( CONF_API_VERSION, CONF_NAME, + CONF_UNIQUE_ID, CONF_UNIT_OF_MEASUREMENT, CONF_VALUE_TEMPLATE, EVENT_HOMEASSISTANT_STOP, @@ -109,6 +110,7 @@ def validate_query_format_for_version(conf: dict) -> dict: _QUERY_SENSOR_SCHEMA = vol.Schema( { vol.Required(CONF_NAME): cv.string, + vol.Optional(CONF_UNIQUE_ID): cv.string, vol.Optional(CONF_VALUE_TEMPLATE): cv.template, vol.Optional(CONF_UNIT_OF_MEASUREMENT): cv.string, } @@ -198,6 +200,7 @@ class InfluxSensor(SensorEntity): self._value_template = None self._state = None self._hass = hass + self._attr_unique_id = query.get(CONF_UNIQUE_ID) if query[CONF_LANGUAGE] == LANGUAGE_FLUX: query_clause = query.get(CONF_QUERY) diff --git a/tests/components/influxdb/test_sensor.py b/tests/components/influxdb/test_sensor.py index 86a32877a79..6bc2014d24e 100644 --- a/tests/components/influxdb/test_sensor.py +++ b/tests/components/influxdb/test_sensor.py @@ -44,13 +44,22 @@ BASE_V1_QUERY = { "queries": [ { "name": "test", + "unique_id": "unique_test_id", "measurement": "measurement", "where": "where", "field": "field", } ], } -BASE_V2_QUERY = {"queries_flux": [{"name": "test", "query": "query"}]} +BASE_V2_QUERY = { + "queries_flux": [ + { + "name": "test", + "unique_id": "unique_test_id", + "query": "query", + } + ] +} @dataclass @@ -232,6 +241,7 @@ async def test_minimal_config(hass, mock_client, config_ext, queries, set_query_ "queries": [ { "name": "test", + "unique_id": "unique_test_id", "unit_of_measurement": "unit", "measurement": "measurement", "where": "where", @@ -260,6 +270,7 @@ async def test_minimal_config(hass, mock_client, config_ext, queries, set_query_ "queries_flux": [ { "name": "test", + "unique_id": "unique_test_id", "unit_of_measurement": "unit", "range_start": "start", "range_stop": "end", @@ -452,6 +463,7 @@ async def test_error_querying_influx( "queries": [ { "name": "test", + "unique_id": "unique_test_id", "measurement": "measurement", "where": "{{ illegal.template }}", "field": "field", @@ -465,7 +477,15 @@ async def test_error_querying_influx( ( API_VERSION_2, BASE_V2_CONFIG, - {"queries_flux": [{"name": "test", "query": "{{ illegal.template }}"}]}, + { + "queries_flux": [ + { + "name": "test", + "unique_id": "unique_test_id", + "query": "{{ illegal.template }}", + } + ] + }, _set_query_mock_v2, _make_v2_resultset, "query",