* Use const

* Align quotes
This commit is contained in:
Fabian Affolter 2017-08-25 13:30:00 +02:00 committed by GitHub
parent 2ec0d25a38
commit fdeef2f707
2 changed files with 16 additions and 18 deletions

View File

@ -45,13 +45,12 @@ def setup(hass, config):
try: try:
sock.connect((host, port)) sock.connect((host, port))
sock.shutdown(2) sock.shutdown(2)
_LOGGER.debug('Connection to Graphite possible') _LOGGER.debug("Connection to Graphite possible")
except socket.error: except socket.error:
_LOGGER.error('Not able to connect to Graphite') _LOGGER.error("Not able to connect to Graphite")
return False return False
GraphiteFeeder(hass, host, port, prefix) GraphiteFeeder(hass, host, port, prefix)
return True return True
@ -143,15 +142,15 @@ class GraphiteFeeder(threading.Thread):
_LOGGER.debug("Processing STATE_CHANGED event for %s", _LOGGER.debug("Processing STATE_CHANGED event for %s",
event.data['entity_id']) event.data['entity_id'])
try: try:
self._report_attributes(event.data['entity_id'], self._report_attributes(
event.data['new_state']) event.data['entity_id'], event.data['new_state'])
# pylint: disable=broad-except # pylint: disable=broad-except
except Exception: except Exception:
# Catch this so we can avoid the thread dying and # Catch this so we can avoid the thread dying and
# make it visible. # make it visible.
_LOGGER.exception("Failed to process STATE_CHANGED event") _LOGGER.exception("Failed to process STATE_CHANGED event")
else: else:
_LOGGER.warning("Processing unexpected event type %s", _LOGGER.warning(
event.event_type) "Processing unexpected event type %s", event.event_type)
self._queue.task_done() self._queue.task_done()

View File

@ -12,16 +12,18 @@ from aiohttp import web
from homeassistant.components.http import HomeAssistantView from homeassistant.components.http import HomeAssistantView
from homeassistant.components import recorder from homeassistant.components import recorder
from homeassistant.const import (CONF_DOMAINS, CONF_ENTITIES, CONF_EXCLUDE, from homeassistant.const import (
CONF_INCLUDE, EVENT_STATE_CHANGED, CONF_DOMAINS, CONF_ENTITIES, CONF_EXCLUDE, CONF_INCLUDE, TEMP_CELSIUS,
TEMP_CELSIUS, TEMP_FAHRENHEIT) EVENT_STATE_CHANGED, TEMP_FAHRENHEIT, CONTENT_TYPE_TEXT_PLAIN)
from homeassistant import core as hacore from homeassistant import core as hacore
from homeassistant.helpers import state as state_helper from homeassistant.helpers import state as state_helper
from homeassistant.util.temperature import fahrenheit_to_celsius from homeassistant.util.temperature import fahrenheit_to_celsius
REQUIREMENTS = ['prometheus_client==0.0.19']
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
REQUIREMENTS = ['prometheus_client==0.0.19'] API_ENDPOINT = '/api/prometheus'
DOMAIN = 'prometheus' DOMAIN = 'prometheus'
DEPENDENCIES = ['http'] DEPENDENCIES = ['http']
@ -30,8 +32,6 @@ CONFIG_SCHEMA = vol.Schema({
DOMAIN: recorder.FILTER_SCHEMA, DOMAIN: recorder.FILTER_SCHEMA,
}, extra=vol.ALLOW_EXTRA) }, extra=vol.ALLOW_EXTRA)
API_ENDPOINT = '/api/prometheus'
def setup(hass, config): def setup(hass, config):
"""Activate Prometheus component.""" """Activate Prometheus component."""
@ -45,11 +45,10 @@ def setup(hass, config):
metrics = Metrics(prometheus_client, exclude, include) metrics = Metrics(prometheus_client, exclude, include)
hass.bus.listen(EVENT_STATE_CHANGED, metrics.handle_event) hass.bus.listen(EVENT_STATE_CHANGED, metrics.handle_event)
return True return True
class Metrics: class Metrics(object):
"""Model all of the metrics which should be exposed to Prometheus.""" """Model all of the metrics which should be exposed to Prometheus."""
def __init__(self, prometheus_client, exclude, include): def __init__(self, prometheus_client, exclude, include):
@ -81,7 +80,7 @@ class Metrics:
entity_id not in self.include_entities): entity_id not in self.include_entities):
return return
handler = '_handle_' + domain handler = '_handle_{}'.format(domain)
if hasattr(self, handler): if hasattr(self, handler):
getattr(self, handler)(state) getattr(self, handler)(state)
@ -233,8 +232,8 @@ class PrometheusView(HomeAssistantView):
@asyncio.coroutine @asyncio.coroutine
def get(self, request): 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_client.generate_latest(),
content_type="text/plain") content_type=CONTENT_TYPE_TEXT_PLAIN)