mirror of
https://github.com/nikdoof/homeassistant-prometheus-query.git
synced 2025-12-17 12:39:22 +00:00
First upload to github
This commit is contained in:
36
README.md
Normal file
36
README.md
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
# homeassistant-prometheus-query
|
||||||
|
Inspired from homeassitant Command line Sensor this sensor take values from [Prometheus](https://prometheus.io/) metrics using [PromQL](https://prometheus.io/docs/prometheus/latest/querying/basics/) query . It allow to specify one or more query.
|
||||||
|
|
||||||
|
## Configuration
|
||||||
|
|
||||||
|
To enable it, add the following lines to your `configuration.yaml`:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
# Example configuration.yaml entry
|
||||||
|
sensor:
|
||||||
|
- platform: prometheus-query
|
||||||
|
name: Temperature Pisa
|
||||||
|
prometheus_url: http://localhost:9090
|
||||||
|
prometheus_query: temperature{location="Pisa",province="PI",region="Tuscany"}
|
||||||
|
unit_of_measurement: "°C"
|
||||||
|
```
|
||||||
|
|
||||||
|
### Configuration Variables
|
||||||
|
|
||||||
|
- name
|
||||||
|
|
||||||
|
(string)(Required) Name of the sensor..
|
||||||
|
|
||||||
|
- prometheus_url
|
||||||
|
|
||||||
|
(string)(Required) the url of your Prometheus server
|
||||||
|
|
||||||
|
- prometheus_query
|
||||||
|
|
||||||
|
(string)(Required) the PromQL query to retrieva sensor
|
||||||
|
|
||||||
|
- unit_of_measurement
|
||||||
|
|
||||||
|
(string)(Optional) Defines the unit of measurement of the sensor, if any.
|
||||||
|
|
||||||
|
It's a custom component so it must be downloaded under /custom_components folder.
|
||||||
1
prometheus-query/__init__.py
Normal file
1
prometheus-query/__init__.py
Normal file
@@ -0,0 +1 @@
|
|||||||
|
"""The prometheus query component."""
|
||||||
8
prometheus-query/manifest.json
Normal file
8
prometheus-query/manifest.json
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
{
|
||||||
|
"domain": "prometheus-query",
|
||||||
|
"name": "Prometheus query sensor",
|
||||||
|
"documentation": "",
|
||||||
|
"dependencies": [],
|
||||||
|
"codeowners": ["lfasci"],
|
||||||
|
"requirements": ["prometheus_client","requests"]
|
||||||
|
}
|
||||||
81
prometheus-query/sensor.py
Normal file
81
prometheus-query/sensor.py
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
import logging, time
|
||||||
|
import requests
|
||||||
|
import json
|
||||||
|
import voluptuous as vol
|
||||||
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
from datetime import timedelta
|
||||||
|
from homeassistant.helpers.config_validation import PLATFORM_SCHEMA
|
||||||
|
from homeassistant.helpers.entity import Entity
|
||||||
|
from homeassistant.util import Throttle
|
||||||
|
from homeassistant.const import (
|
||||||
|
CONF_NAME,
|
||||||
|
CONF_UNIT_OF_MEASUREMENT,
|
||||||
|
STATE_UNKNOWN,
|
||||||
|
)
|
||||||
|
|
||||||
|
from prometheus_client import Summary
|
||||||
|
|
||||||
|
DOMAIN = 'prometheus'
|
||||||
|
CONF_PROMETHEUS_URL = 'prometheus_url'
|
||||||
|
CONF_PROMETHEUS_QUERY = 'prometheus_query'
|
||||||
|
SCAN_INTERVAL = timedelta(seconds=600)
|
||||||
|
|
||||||
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||||
|
vol.Required(CONF_PROMETHEUS_URL): cv.string,
|
||||||
|
vol.Required(CONF_PROMETHEUS_QUERY): cv.string,
|
||||||
|
vol.Required(CONF_NAME): cv.string,
|
||||||
|
vol.Optional(CONF_UNIT_OF_MEASUREMENT): cv.string,
|
||||||
|
})
|
||||||
|
|
||||||
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||||
|
"""Set up the sensor platform."""
|
||||||
|
prom_data = {
|
||||||
|
'url': str(config.get(CONF_PROMETHEUS_URL)) + "/api/v1/query",
|
||||||
|
'query': str(config.get(CONF_PROMETHEUS_QUERY)),
|
||||||
|
'name': str(config.get(CONF_NAME)),
|
||||||
|
'unit': str(config.get(CONF_UNIT_OF_MEASUREMENT)),
|
||||||
|
}
|
||||||
|
add_entities([PrometheusQuery(prom_data)], True)
|
||||||
|
|
||||||
|
|
||||||
|
class PrometheusQuery(Entity):
|
||||||
|
"""Representation of a Sensor."""
|
||||||
|
def __init__(self, prom_data):
|
||||||
|
"""Initialize the sensor."""
|
||||||
|
self._url = prom_data["url"]
|
||||||
|
self._query = prom_data["query"]
|
||||||
|
self._name = prom_data["name"]
|
||||||
|
self._state = None
|
||||||
|
self._unit_of_measurement = prom_data["unit"]
|
||||||
|
|
||||||
|
@property
|
||||||
|
def name(self):
|
||||||
|
"""Return the name of the sensor."""
|
||||||
|
return self._name
|
||||||
|
|
||||||
|
@property
|
||||||
|
def state(self):
|
||||||
|
"""Return the state of the sensor."""
|
||||||
|
return self._state
|
||||||
|
|
||||||
|
@property
|
||||||
|
def unit_of_measurement(self):
|
||||||
|
"""Return the unit of measurement."""
|
||||||
|
return self._unit_of_measurement
|
||||||
|
|
||||||
|
def update(self):
|
||||||
|
"""Fetch new state data for the sensor.
|
||||||
|
This is the only method that should fetch new data for Home Assistant.
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
response = requests.get(self._url, params={'query': self._query})
|
||||||
|
if (response):
|
||||||
|
results = response.json()['data']['result']
|
||||||
|
self._state = results[0]['value'][1]
|
||||||
|
else:
|
||||||
|
self._state = STATE_UNKNOWN
|
||||||
|
except URLCallError:
|
||||||
|
_LOGGER.error("Error when retrieving update data")
|
||||||
Reference in New Issue
Block a user