Add unique ID to InfluxDB sensor (#65518)

pull/65926/head
Sander Huisman 2022-02-06 18:40:37 +01:00 committed by GitHub
parent b02a030336
commit e7dfc89452
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 2 deletions

View File

@ -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)

View File

@ -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",