Move imports in prometheus component (#28086)

pull/28103/head
Diefferson Koderer Môro 2019-10-22 05:35:43 +00:00 committed by Paulus Schoutsen
parent 0193207b5c
commit 2d36e9c08e
1 changed files with 23 additions and 28 deletions

View File

@ -3,24 +3,25 @@ import logging
import string import string
from aiohttp import web from aiohttp import web
import prometheus_client
import voluptuous as vol import voluptuous as vol
from homeassistant import core as hacore from homeassistant import core as hacore
from homeassistant.components.climate.const import ATTR_CURRENT_TEMPERATURE from homeassistant.components.climate.const import ATTR_CURRENT_TEMPERATURE
from homeassistant.components.http import HomeAssistantView from homeassistant.components.http import HomeAssistantView
from homeassistant.const import ( from homeassistant.const import (
ATTR_DEVICE_CLASS,
ATTR_TEMPERATURE, ATTR_TEMPERATURE,
ATTR_UNIT_OF_MEASUREMENT, ATTR_UNIT_OF_MEASUREMENT,
ATTR_DEVICE_CLASS,
CONTENT_TYPE_TEXT_PLAIN, CONTENT_TYPE_TEXT_PLAIN,
EVENT_STATE_CHANGED, EVENT_STATE_CHANGED,
TEMP_FAHRENHEIT,
TEMP_CELSIUS, TEMP_CELSIUS,
TEMP_FAHRENHEIT,
) )
from homeassistant.helpers import entityfilter, state as state_helper from homeassistant.helpers import entityfilter, state as state_helper
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.util.temperature import fahrenheit_to_celsius
from homeassistant.helpers.entity_values import EntityValues from homeassistant.helpers.entity_values import EntityValues
from homeassistant.util.temperature import fahrenheit_to_celsius
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -64,8 +65,6 @@ CONFIG_SCHEMA = vol.Schema(
def setup(hass, config): def setup(hass, config):
"""Activate Prometheus component.""" """Activate Prometheus component."""
import prometheus_client
hass.http.register_view(PrometheusView(prometheus_client)) hass.http.register_view(PrometheusView(prometheus_client))
conf = config[DOMAIN] conf = config[DOMAIN]
@ -99,7 +98,7 @@ class PrometheusMetrics:
def __init__( def __init__(
self, self,
prometheus_client, prometheus_cli,
entity_filter, entity_filter,
namespace, namespace,
climate_units, climate_units,
@ -108,7 +107,7 @@ class PrometheusMetrics:
default_metric, default_metric,
): ):
"""Initialize Prometheus Metrics.""" """Initialize Prometheus Metrics."""
self.prometheus_client = prometheus_client self.prometheus_cli = prometheus_cli
self._component_config = component_config self._component_config = component_config
self._override_metric = override_metric self._override_metric = override_metric
self._default_metric = default_metric self._default_metric = default_metric
@ -147,9 +146,7 @@ class PrometheusMetrics:
getattr(self, handler)(state) getattr(self, handler)(state)
metric = self._metric( metric = self._metric(
"state_change", "state_change", self.prometheus_cli.Counter, "The number of state changes"
self.prometheus_client.Counter,
"The number of state changes",
) )
metric.labels(**self._labels(state)).inc() metric.labels(**self._labels(state)).inc()
@ -199,7 +196,7 @@ class PrometheusMetrics:
if "battery_level" in state.attributes: if "battery_level" in state.attributes:
metric = self._metric( metric = self._metric(
"battery_level_percent", "battery_level_percent",
self.prometheus_client.Gauge, self.prometheus_cli.Gauge,
"Battery level as a percentage of its capacity", "Battery level as a percentage of its capacity",
) )
try: try:
@ -211,7 +208,7 @@ class PrometheusMetrics:
def _handle_binary_sensor(self, state): def _handle_binary_sensor(self, state):
metric = self._metric( metric = self._metric(
"binary_sensor_state", "binary_sensor_state",
self.prometheus_client.Gauge, self.prometheus_cli.Gauge,
"State of the binary sensor (0/1)", "State of the binary sensor (0/1)",
) )
value = self.state_as_number(state) value = self.state_as_number(state)
@ -220,7 +217,7 @@ class PrometheusMetrics:
def _handle_input_boolean(self, state): def _handle_input_boolean(self, state):
metric = self._metric( metric = self._metric(
"input_boolean_state", "input_boolean_state",
self.prometheus_client.Gauge, self.prometheus_cli.Gauge,
"State of the input boolean (0/1)", "State of the input boolean (0/1)",
) )
value = self.state_as_number(state) value = self.state_as_number(state)
@ -229,7 +226,7 @@ class PrometheusMetrics:
def _handle_device_tracker(self, state): def _handle_device_tracker(self, state):
metric = self._metric( metric = self._metric(
"device_tracker_state", "device_tracker_state",
self.prometheus_client.Gauge, self.prometheus_cli.Gauge,
"State of the device tracker (0/1)", "State of the device tracker (0/1)",
) )
value = self.state_as_number(state) value = self.state_as_number(state)
@ -237,14 +234,14 @@ class PrometheusMetrics:
def _handle_person(self, state): def _handle_person(self, state):
metric = self._metric( metric = self._metric(
"person_state", self.prometheus_client.Gauge, "State of the person (0/1)" "person_state", self.prometheus_cli.Gauge, "State of the person (0/1)"
) )
value = self.state_as_number(state) value = self.state_as_number(state)
metric.labels(**self._labels(state)).set(value) metric.labels(**self._labels(state)).set(value)
def _handle_light(self, state): def _handle_light(self, state):
metric = self._metric( metric = self._metric(
"light_state", self.prometheus_client.Gauge, "Load level of a light (0..1)" "light_state", self.prometheus_cli.Gauge, "Load level of a light (0..1)"
) )
try: try:
@ -259,7 +256,7 @@ class PrometheusMetrics:
def _handle_lock(self, state): def _handle_lock(self, state):
metric = self._metric( metric = self._metric(
"lock_state", self.prometheus_client.Gauge, "State of the lock (0/1)" "lock_state", self.prometheus_cli.Gauge, "State of the lock (0/1)"
) )
value = self.state_as_number(state) value = self.state_as_number(state)
metric.labels(**self._labels(state)).set(value) metric.labels(**self._labels(state)).set(value)
@ -271,7 +268,7 @@ class PrometheusMetrics:
temp = fahrenheit_to_celsius(temp) temp = fahrenheit_to_celsius(temp)
metric = self._metric( metric = self._metric(
"temperature_c", "temperature_c",
self.prometheus_client.Gauge, self.prometheus_cli.Gauge,
"Temperature in degrees Celsius", "Temperature in degrees Celsius",
) )
metric.labels(**self._labels(state)).set(temp) metric.labels(**self._labels(state)).set(temp)
@ -282,15 +279,13 @@ class PrometheusMetrics:
current_temp = fahrenheit_to_celsius(current_temp) current_temp = fahrenheit_to_celsius(current_temp)
metric = self._metric( metric = self._metric(
"current_temperature_c", "current_temperature_c",
self.prometheus_client.Gauge, self.prometheus_cli.Gauge,
"Current Temperature in degrees Celsius", "Current Temperature in degrees Celsius",
) )
metric.labels(**self._labels(state)).set(current_temp) metric.labels(**self._labels(state)).set(current_temp)
metric = self._metric( metric = self._metric(
"climate_state", "climate_state", self.prometheus_cli.Gauge, "State of the thermostat (0/1)"
self.prometheus_client.Gauge,
"State of the thermostat (0/1)",
) )
try: try:
value = self.state_as_number(state) value = self.state_as_number(state)
@ -308,7 +303,7 @@ class PrometheusMetrics:
if metric is not None: if metric is not None:
_metric = self._metric( _metric = self._metric(
metric, self.prometheus_client.Gauge, f"Sensor data measured in {unit}" metric, self.prometheus_cli.Gauge, f"Sensor data measured in {unit}"
) )
try: try:
@ -368,7 +363,7 @@ class PrometheusMetrics:
def _handle_switch(self, state): def _handle_switch(self, state):
metric = self._metric( metric = self._metric(
"switch_state", self.prometheus_client.Gauge, "State of the switch (0/1)" "switch_state", self.prometheus_cli.Gauge, "State of the switch (0/1)"
) )
try: try:
@ -383,7 +378,7 @@ class PrometheusMetrics:
def _handle_automation(self, state): def _handle_automation(self, state):
metric = self._metric( metric = self._metric(
"automation_triggered_count", "automation_triggered_count",
self.prometheus_client.Counter, self.prometheus_cli.Counter,
"Count of times an automation has been triggered", "Count of times an automation has been triggered",
) )
@ -396,15 +391,15 @@ class PrometheusView(HomeAssistantView):
url = API_ENDPOINT url = API_ENDPOINT
name = "api:prometheus" name = "api:prometheus"
def __init__(self, prometheus_client): def __init__(self, prometheus_cli):
"""Initialize Prometheus view.""" """Initialize Prometheus view."""
self.prometheus_client = prometheus_client self.prometheus_cli = prometheus_cli
async def get(self, request): async def get(self, request):
"""Handle request for Prometheus metrics.""" """Handle request for Prometheus metrics."""
_LOGGER.debug("Received Prometheus metrics request") _LOGGER.debug("Received Prometheus metrics request")
return web.Response( return web.Response(
body=self.prometheus_client.generate_latest(), body=self.prometheus_cli.generate_latest(),
content_type=CONTENT_TYPE_TEXT_PLAIN, content_type=CONTENT_TYPE_TEXT_PLAIN,
) )