mirror of
https://github.com/nikdoof/homeassistant-prometheus-query.git
synced 2025-12-17 04:29:21 +00:00
Support unique_id
Support is a bit fragile since we don't really have a serial number to base on. However we have a reasonnable default (source prometheus + expression) and a way for user to specify id easily if they to change the prometheus query.
This commit is contained in:
@@ -22,6 +22,7 @@ from prometheus_client import Summary
|
||||
CONF_PROMETHEUS_URL = 'prometheus_url'
|
||||
CONF_PROMETHEUS_QUERY = 'prometheus_query'
|
||||
CONF_STATE_CLASS = 'state_class'
|
||||
CONF_UNIQUE_ID = 'unique_id'
|
||||
SCAN_INTERVAL = timedelta(seconds=600)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
@@ -33,6 +34,7 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||
vol.Required(CONF_NAME): cv.string,
|
||||
vol.Optional(CONF_UNIT_OF_MEASUREMENT): cv.string,
|
||||
vol.Optional(CONF_STATE_CLASS): STATE_CLASSES_SCHEMA,
|
||||
vol.Optional(CONF_UNIQUE_ID): cv.string,
|
||||
})
|
||||
|
||||
def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||
@@ -43,6 +45,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||
'name': str(config.get(CONF_NAME)),
|
||||
'unit': str(config.get(CONF_UNIT_OF_MEASUREMENT)),
|
||||
'state_class': str(config.get(CONF_STATE_CLASS)),
|
||||
'unique_id': str(config.get(CONF_UNIQUE_ID)),
|
||||
}
|
||||
add_entities([PrometheusQuery(prom_data)], True)
|
||||
|
||||
@@ -57,6 +60,9 @@ class PrometheusQuery(Entity):
|
||||
self._state = None
|
||||
self._unit_of_measurement = prom_data["unit"]
|
||||
self._state_class = prom_data["state_class"]
|
||||
self._attr_unique_id = f"${prom_data['url']}$${prom_data['query']}"
|
||||
if prom_data["unique_id"] is not None:
|
||||
self._attr_unique_id = prom_data["unique_id"]
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
|
||||
Reference in New Issue
Block a user