parent
cf832499cd
commit
6f27d58188
|
@ -1,8 +1,6 @@
|
||||||
"""
|
"""
|
||||||
Support to send data to an Splunk instance.
|
Support to send data to an Splunk instance.
|
||||||
|
|
||||||
Uses the HTTP Event Collector.
|
|
||||||
|
|
||||||
For more details about this component, please refer to the documentation at
|
For more details about this component, please refer to the documentation at
|
||||||
https://home-assistant.io/components/splunk/
|
https://home-assistant.io/components/splunk/
|
||||||
"""
|
"""
|
||||||
|
@ -10,47 +8,47 @@ import json
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
import voluptuous as vol
|
||||||
|
|
||||||
import homeassistant.util as util
|
from homeassistant.const import (
|
||||||
from homeassistant.const import EVENT_STATE_CHANGED
|
CONF_HOST, CONF_PORT, CONF_SSL, CONF_TOKEN, EVENT_STATE_CHANGED)
|
||||||
from homeassistant.helpers import state as state_helper
|
from homeassistant.helpers import state as state_helper
|
||||||
from homeassistant.helpers import validate_config
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
DOMAIN = "splunk"
|
DOMAIN = 'splunk'
|
||||||
DEPENDENCIES = []
|
|
||||||
|
|
||||||
DEFAULT_HOST = 'localhost'
|
DEFAULT_HOST = 'localhost'
|
||||||
DEFAULT_PORT = '8088'
|
DEFAULT_PORT = 8088
|
||||||
DEFAULT_SSL = False
|
DEFAULT_SSL = False
|
||||||
|
|
||||||
CONF_HOST = 'host'
|
CONFIG_SCHEMA = vol.Schema({
|
||||||
CONF_PORT = 'port'
|
DOMAIN: vol.Schema({
|
||||||
CONF_TOKEN = 'token'
|
vol.Required(CONF_TOKEN): cv.string,
|
||||||
CONF_SSL = 'SSL'
|
vol.Optional(CONF_HOST, default=DEFAULT_HOST): cv.string,
|
||||||
|
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
|
||||||
|
vol.Optional(CONF_SSL, default=False): cv.boolean,
|
||||||
|
}),
|
||||||
|
}, extra=vol.ALLOW_EXTRA)
|
||||||
|
|
||||||
|
|
||||||
def setup(hass, config):
|
def setup(hass, config):
|
||||||
"""Setup the Splunk component."""
|
"""Setup the Splunk component."""
|
||||||
if not validate_config(config, {DOMAIN: ['token']}, _LOGGER):
|
|
||||||
_LOGGER.error("You must include the token for your HTTP "
|
|
||||||
"Event Collector input in Splunk.")
|
|
||||||
return False
|
|
||||||
|
|
||||||
conf = config[DOMAIN]
|
conf = config[DOMAIN]
|
||||||
|
host = conf.get(CONF_HOST)
|
||||||
|
port = conf.get(CONF_PORT)
|
||||||
|
token = conf.get(CONF_TOKEN)
|
||||||
|
use_ssl = conf.get(CONF_SSL)
|
||||||
|
|
||||||
host = conf[CONF_HOST]
|
|
||||||
port = util.convert(conf.get(CONF_PORT), int, DEFAULT_PORT)
|
|
||||||
token = util.convert(conf.get(CONF_TOKEN), str)
|
|
||||||
use_ssl = util.convert(conf.get(CONF_SSL), bool, DEFAULT_SSL)
|
|
||||||
if use_ssl:
|
if use_ssl:
|
||||||
uri_scheme = "https://"
|
uri_scheme = 'https://'
|
||||||
else:
|
else:
|
||||||
uri_scheme = "http://"
|
uri_scheme = 'http://'
|
||||||
event_collector = uri_scheme + host + ":" + str(port) + \
|
|
||||||
"/services/collector/event"
|
event_collector = '{}{}:{}/services/collector/event'.format(
|
||||||
headers = {'Authorization': 'Splunk ' + token}
|
uri_scheme, host, port)
|
||||||
|
headers = {'Authorization': 'Splunk {}'.format(token)}
|
||||||
|
|
||||||
def splunk_event_listener(event):
|
def splunk_event_listener(event):
|
||||||
"""Listen for new messages on the bus and sends them to Splunk."""
|
"""Listen for new messages on the bus and sends them to Splunk."""
|
||||||
|
|
|
@ -19,6 +19,7 @@ class TestSplunk(unittest.TestCase):
|
||||||
'use_ssl': 'False',
|
'use_ssl': 'False',
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
hass = mock.MagicMock()
|
hass = mock.MagicMock()
|
||||||
self.assertTrue(splunk.setup(hass, config))
|
self.assertTrue(splunk.setup(hass, config))
|
||||||
self.assertTrue(hass.bus.listen.called)
|
self.assertTrue(hass.bus.listen.called)
|
||||||
|
@ -33,6 +34,7 @@ class TestSplunk(unittest.TestCase):
|
||||||
'token': 'secret',
|
'token': 'secret',
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
hass = mock.MagicMock()
|
hass = mock.MagicMock()
|
||||||
self.assertTrue(splunk.setup(hass, config))
|
self.assertTrue(splunk.setup(hass, config))
|
||||||
self.assertTrue(hass.bus.listen.called)
|
self.assertTrue(hass.bus.listen.called)
|
||||||
|
@ -48,8 +50,10 @@ class TestSplunk(unittest.TestCase):
|
||||||
'splunk': {
|
'splunk': {
|
||||||
'host': 'host',
|
'host': 'host',
|
||||||
'token': 'secret',
|
'token': 'secret',
|
||||||
|
'port': 8088,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
self.hass = mock.MagicMock()
|
self.hass = mock.MagicMock()
|
||||||
splunk.setup(self.hass, config)
|
splunk.setup(self.hass, config)
|
||||||
self.handler_method = self.hass.bus.listen.call_args_list[0][0][1]
|
self.handler_method = self.hass.bus.listen.call_args_list[0][0][1]
|
||||||
|
@ -65,14 +69,16 @@ class TestSplunk(unittest.TestCase):
|
||||||
'1.0': 1.0,
|
'1.0': 1.0,
|
||||||
STATE_ON: 1,
|
STATE_ON: 1,
|
||||||
STATE_OFF: 0,
|
STATE_OFF: 0,
|
||||||
'foo': 'foo'}
|
'foo': 'foo',
|
||||||
|
}
|
||||||
|
|
||||||
for in_, out in valid.items():
|
for in_, out in valid.items():
|
||||||
state = mock.MagicMock(state=in_,
|
state = mock.MagicMock(state=in_,
|
||||||
domain='fake',
|
domain='fake',
|
||||||
object_id='entity',
|
object_id='entity',
|
||||||
attributes={})
|
attributes={})
|
||||||
event = mock.MagicMock(data={'new_state': state},
|
event = mock.MagicMock(data={'new_state': state}, time_fired=12345)
|
||||||
time_fired=12345)
|
|
||||||
body = [{
|
body = [{
|
||||||
'domain': 'fake',
|
'domain': 'fake',
|
||||||
'entity_id': 'entity',
|
'entity_id': 'entity',
|
||||||
|
@ -80,6 +86,7 @@ class TestSplunk(unittest.TestCase):
|
||||||
'time': '12345',
|
'time': '12345',
|
||||||
'value': out,
|
'value': out,
|
||||||
}]
|
}]
|
||||||
|
|
||||||
payload = {'host': 'http://host:8088/services/collector/event',
|
payload = {'host': 'http://host:8088/services/collector/event',
|
||||||
'event': body}
|
'event': body}
|
||||||
self.handler_method(event)
|
self.handler_method(event)
|
||||||
|
|
Loading…
Reference in New Issue