Black
This commit is contained in:
1 parent
da05dfe708
commit
4de97abc3a
2676 files changed
+163166
-140084
No files matched your search
@@ -9,24 +9,34 @@ import voluptuous as vol
|
||||
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.const import (
|
||||
CONF_HOST, CONF_PORT, CONF_PREFIX, EVENT_HOMEASSISTANT_START,
|
||||
EVENT_HOMEASSISTANT_STOP, EVENT_STATE_CHANGED)
|
||||
CONF_HOST,
|
||||
CONF_PORT,
|
||||
CONF_PREFIX,
|
||||
EVENT_HOMEASSISTANT_START,
|
||||
EVENT_HOMEASSISTANT_STOP,
|
||||
EVENT_STATE_CHANGED,
|
||||
)
|
||||
from homeassistant.helpers import state
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
DEFAULT_HOST = 'localhost'
|
||||
DEFAULT_HOST = "localhost"
|
||||
DEFAULT_PORT = 2003
|
||||
DEFAULT_PREFIX = 'ha'
|
||||
DOMAIN = 'graphite'
|
||||
DEFAULT_PREFIX = "ha"
|
||||
DOMAIN = "graphite"
|
||||
|
||||
CONFIG_SCHEMA = vol.Schema({
|
||||
DOMAIN: vol.Schema({
|
||||
vol.Optional(CONF_HOST, default=DEFAULT_HOST): cv.string,
|
||||
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
|
||||
vol.Optional(CONF_PREFIX, default=DEFAULT_PREFIX): cv.string,
|
||||
}),
|
||||
}, extra=vol.ALLOW_EXTRA)
|
||||
CONFIG_SCHEMA = vol.Schema(
|
||||
{
|
||||
DOMAIN: vol.Schema(
|
||||
{
|
||||
vol.Optional(CONF_HOST, default=DEFAULT_HOST): cv.string,
|
||||
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
|
||||
vol.Optional(CONF_PREFIX, default=DEFAULT_PREFIX): cv.string,
|
||||
}
|
||||
)
|
||||
},
|
||||
extra=vol.ALLOW_EXTRA,
|
||||
)
|
||||
|
||||
|
||||
def setup(hass, config):
|
||||
@@ -59,7 +69,7 @@ class GraphiteFeeder(threading.Thread):
|
||||
self._host = host
|
||||
self._port = port
|
||||
# rstrip any trailing dots in case they think they need it
|
||||
self._prefix = prefix.rstrip('.')
|
||||
self._prefix = prefix.rstrip(".")
|
||||
self._queue = queue.Queue()
|
||||
self._quit_object = object()
|
||||
self._we_started = False
|
||||
@@ -67,8 +77,7 @@ class GraphiteFeeder(threading.Thread):
|
||||
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)
|
||||
_LOGGER.debug("Graphite feeding to %s:%i initialized",
|
||||
self._host, self._port)
|
||||
_LOGGER.debug("Graphite feeding to %s:%i initialized", self._host, self._port)
|
||||
|
||||
def start_listen(self, event):
|
||||
"""Start event-processing thread."""
|
||||
@@ -87,16 +96,15 @@ class GraphiteFeeder(threading.Thread):
|
||||
_LOGGER.debug("Received event")
|
||||
self._queue.put(event)
|
||||
else:
|
||||
_LOGGER.error(
|
||||
"Graphite feeder thread has died, not queuing event")
|
||||
_LOGGER.error("Graphite feeder thread has died, not queuing event")
|
||||
|
||||
def _send_to_graphite(self, data):
|
||||
"""Send data to Graphite."""
|
||||
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.sendall(data.encode("ascii"))
|
||||
sock.send("\n".encode("ascii"))
|
||||
sock.close()
|
||||
|
||||
def _report_attributes(self, entity_id, new_state):
|
||||
@@ -104,19 +112,20 @@ class GraphiteFeeder(threading.Thread):
|
||||
now = time.time()
|
||||
things = dict(new_state.attributes)
|
||||
try:
|
||||
things['state'] = state.state_as_number(new_state)
|
||||
things["state"] = state.state_as_number(new_state)
|
||||
except ValueError:
|
||||
pass
|
||||
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))]
|
||||
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))
|
||||
self._send_to_graphite("\n".join(lines))
|
||||
except socket.gaierror:
|
||||
_LOGGER.error("Unable to connect to host %s", self._host)
|
||||
except socket.error:
|
||||
@@ -130,19 +139,19 @@ class GraphiteFeeder(threading.Thread):
|
||||
_LOGGER.debug("Event processing thread stopped")
|
||||
self._queue.task_done()
|
||||
return
|
||||
if event.event_type == EVENT_STATE_CHANGED and \
|
||||
event.data.get('new_state'):
|
||||
_LOGGER.debug("Processing STATE_CHANGED event for %s",
|
||||
event.data['entity_id'])
|
||||
if event.event_type == EVENT_STATE_CHANGED and event.data.get("new_state"):
|
||||
_LOGGER.debug(
|
||||
"Processing STATE_CHANGED event for %s", event.data["entity_id"]
|
||||
)
|
||||
try:
|
||||
self._report_attributes(
|
||||
event.data['entity_id'], event.data['new_state'])
|
||||
event.data["entity_id"], event.data["new_state"]
|
||||
)
|
||||
except Exception: # pylint: disable=broad-except
|
||||
# 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)
|
||||
_LOGGER.warning("Processing unexpected event type %s", event.event_type)
|
||||
|
||||
self._queue.task_done()
|
||||
Reference in new issue
Block a user