2016-02-10 18:54:06 +00:00
|
|
|
"""
|
|
|
|
Component that records all events and state changes and feeds the data to
|
2016-02-26 22:52:54 +00:00
|
|
|
a Graphite installation.
|
2016-02-10 18:54:06 +00:00
|
|
|
|
2016-02-13 08:35:31 +00:00
|
|
|
For more details about this component, please refer to the documentation at
|
|
|
|
https://home-assistant.io/components/graphite/
|
2016-02-10 18:54:06 +00:00
|
|
|
"""
|
|
|
|
import logging
|
|
|
|
import queue
|
|
|
|
import socket
|
|
|
|
import threading
|
|
|
|
import time
|
|
|
|
|
|
|
|
from homeassistant.const import (
|
2016-02-19 05:27:50 +00:00
|
|
|
EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP, EVENT_STATE_CHANGED)
|
2016-02-11 17:13:57 +00:00
|
|
|
from homeassistant.helpers import state
|
2016-02-10 18:54:06 +00:00
|
|
|
|
|
|
|
DOMAIN = "graphite"
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
def setup(hass, config):
|
2016-02-26 22:52:54 +00:00
|
|
|
"""Setup the Graphite feeder."""
|
2016-02-10 18:54:06 +00:00
|
|
|
graphite_config = config.get('graphite', {})
|
|
|
|
host = graphite_config.get('host', 'localhost')
|
|
|
|
prefix = graphite_config.get('prefix', 'ha')
|
|
|
|
try:
|
|
|
|
port = int(graphite_config.get('port', 2003))
|
|
|
|
except ValueError:
|
|
|
|
_LOGGER.error('Invalid port specified')
|
|
|
|
return False
|
|
|
|
|
|
|
|
GraphiteFeeder(hass, host, port, prefix)
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
class GraphiteFeeder(threading.Thread):
|
2016-02-26 22:52:54 +00:00
|
|
|
"""Feeds data to Graphite."""
|
2016-02-10 18:54:06 +00:00
|
|
|
def __init__(self, hass, host, port, prefix):
|
|
|
|
super(GraphiteFeeder, self).__init__(daemon=True)
|
|
|
|
self._hass = hass
|
|
|
|
self._host = host
|
|
|
|
self._port = port
|
2016-02-26 22:52:54 +00:00
|
|
|
# rstrip any trailing dots in case they think they need it
|
2016-02-10 18:54:06 +00:00
|
|
|
self._prefix = prefix.rstrip('.')
|
|
|
|
self._queue = queue.Queue()
|
|
|
|
self._quit_object = object()
|
2016-02-17 15:45:00 +00:00
|
|
|
self._we_started = False
|
2016-02-10 18:54:06 +00:00
|
|
|
|
|
|
|
hass.bus.listen_once(EVENT_HOMEASSISTANT_START,
|
|
|
|
self.start_listen)
|
|
|
|
hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP,
|
|
|
|
self.shutdown)
|
|
|
|
hass.bus.listen(EVENT_STATE_CHANGED, self.event_listener)
|
2016-02-17 15:45:00 +00:00
|
|
|
_LOGGER.debug('Graphite feeding to %s:%i initialized',
|
|
|
|
self._host, self._port)
|
2016-02-10 18:54:06 +00:00
|
|
|
|
|
|
|
def start_listen(self, event):
|
2016-02-26 22:52:54 +00:00
|
|
|
"""Start event-processing thread."""
|
2016-02-17 15:45:00 +00:00
|
|
|
_LOGGER.debug('Event processing thread started')
|
|
|
|
self._we_started = True
|
2016-02-10 18:54:06 +00:00
|
|
|
self.start()
|
|
|
|
|
|
|
|
def shutdown(self, event):
|
2016-02-26 22:52:54 +00:00
|
|
|
"""Signal shutdown of processing event."""
|
2016-02-17 15:45:00 +00:00
|
|
|
_LOGGER.debug('Event processing signaled exit')
|
2016-02-10 18:54:06 +00:00
|
|
|
self._queue.put(self._quit_object)
|
|
|
|
|
|
|
|
def event_listener(self, event):
|
2016-02-26 22:52:54 +00:00
|
|
|
"""Queue an event for processing."""
|
2016-02-17 15:45:00 +00:00
|
|
|
if self.is_alive() or not self._we_started:
|
|
|
|
_LOGGER.debug('Received event')
|
|
|
|
self._queue.put(event)
|
|
|
|
else:
|
|
|
|
_LOGGER.error('Graphite feeder thread has died, not '
|
|
|
|
'queuing event!')
|
2016-02-10 18:54:06 +00:00
|
|
|
|
|
|
|
def _send_to_graphite(self, data):
|
2016-02-26 22:52:54 +00:00
|
|
|
"""Send data to Graphite."""
|
2016-02-10 18:54:06 +00:00
|
|
|
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
sock.settimeout(10)
|
|
|
|
sock.connect((self._host, self._port))
|
|
|
|
sock.sendall(data.encode('ascii'))
|
|
|
|
sock.send('\n'.encode('ascii'))
|
|
|
|
sock.close()
|
|
|
|
|
|
|
|
def _report_attributes(self, entity_id, new_state):
|
2016-02-26 22:52:54 +00:00
|
|
|
"""Report the attributes."""
|
2016-02-10 18:54:06 +00:00
|
|
|
now = time.time()
|
|
|
|
things = dict(new_state.attributes)
|
2016-02-11 17:13:57 +00:00
|
|
|
try:
|
|
|
|
things['state'] = state.state_as_number(new_state)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
2016-02-10 18:54:06 +00:00
|
|
|
lines = ['%s.%s.%s %f %i' % (self._prefix,
|
|
|
|
entity_id, key.replace(' ', '_'),
|
|
|
|
value, now)
|
|
|
|
for key, value in things.items()
|
|
|
|
if isinstance(value, (float, int))]
|
|
|
|
if not lines:
|
|
|
|
return
|
|
|
|
_LOGGER.debug('Sending to graphite: %s', lines)
|
|
|
|
try:
|
|
|
|
self._send_to_graphite('\n'.join(lines))
|
2016-02-14 23:39:24 +00:00
|
|
|
except socket.gaierror:
|
|
|
|
_LOGGER.error('Unable to connect to host %s', self._host)
|
2016-02-10 18:54:06 +00:00
|
|
|
except socket.error:
|
|
|
|
_LOGGER.exception('Failed to send data to graphite')
|
|
|
|
|
|
|
|
def run(self):
|
2016-02-26 22:52:54 +00:00
|
|
|
"""Run the process to export the data."""
|
2016-02-10 18:54:06 +00:00
|
|
|
while True:
|
|
|
|
event = self._queue.get()
|
|
|
|
if event == self._quit_object:
|
2016-02-17 15:45:00 +00:00
|
|
|
_LOGGER.debug('Event processing thread stopped')
|
2016-02-10 18:54:06 +00:00
|
|
|
self._queue.task_done()
|
|
|
|
return
|
|
|
|
elif (event.event_type == EVENT_STATE_CHANGED and
|
2016-02-14 23:57:03 +00:00
|
|
|
event.data.get('new_state')):
|
2016-02-17 15:45:00 +00:00
|
|
|
_LOGGER.debug('Processing STATE_CHANGED event for %s',
|
|
|
|
event.data['entity_id'])
|
|
|
|
try:
|
|
|
|
self._report_attributes(event.data['entity_id'],
|
|
|
|
event.data['new_state'])
|
|
|
|
# pylint: disable=broad-except
|
|
|
|
except Exception:
|
|
|
|
# Catch this so we can avoid the thread dying and
|
|
|
|
# make it visible.
|
|
|
|
_LOGGER.exception('Failed to process STATE_CHANGED event')
|
|
|
|
else:
|
|
|
|
_LOGGER.warning('Processing unexpected event type %s',
|
|
|
|
event.event_type)
|
|
|
|
|
2016-02-10 18:54:06 +00:00
|
|
|
self._queue.task_done()
|