Splunk component filter support (#25071)
* Added code to support entity and domain filters in the config for splunk component, and the code to enforce the filter. * * Moved code for posting splunk request to separate function, primarily to more easily write a test case where I can mock the point where the post would occur and validate that filtering is working correctly. * Test cases created for full config check and to test the filtering * Correcting static check errors/issues * Correcting flake8 static check issue (introduced when addressing prior static check issues) * Removing unused parameter to setup function - cleanup from reviewer request.
This commit is contained in:
1 parent
f7aa1b026f
commit
f705a1e62e
2 files changed
+85
-12
No files matched your search
@@ -11,10 +11,12 @@ from homeassistant.const import (
|
||||
CONF_PORT, CONF_TOKEN, EVENT_STATE_CHANGED)
|
||||
from homeassistant.helpers import state as state_helper
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.entityfilter import FILTER_SCHEMA
|
||||
from homeassistant.helpers.json import JSONEncoder
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
CONF_FILTER = 'filter'
|
||||
DOMAIN = 'splunk'
|
||||
|
||||
DEFAULT_HOST = 'localhost'
|
||||
@@ -30,10 +32,26 @@ CONFIG_SCHEMA = vol.Schema({
|
||||
vol.Optional(CONF_SSL, default=False): cv.boolean,
|
||||
vol.Optional(CONF_VERIFY_SSL, default=True): cv.boolean,
|
||||
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
||||
vol.Optional(CONF_FILTER, default={}): FILTER_SCHEMA,
|
||||
}),
|
||||
}, extra=vol.ALLOW_EXTRA)
|
||||
|
||||
|
||||
def post_request(event_collector, body, headers, verify_ssl):
|
||||
"""Post request to Splunk."""
|
||||
try:
|
||||
payload = {
|
||||
"host": event_collector,
|
||||
"event": body,
|
||||
}
|
||||
requests.post(event_collector,
|
||||
data=json.dumps(payload, cls=JSONEncoder),
|
||||
headers=headers, timeout=10, verify=verify_ssl)
|
||||
|
||||
except requests.exceptions.RequestException as error:
|
||||
_LOGGER.exception("Error saving event to Splunk: %s", error)
|
||||
|
||||
|
||||
def setup(hass, config):
|
||||
"""Set up the Splunk component."""
|
||||
conf = config[DOMAIN]
|
||||
@@ -43,6 +61,7 @@ def setup(hass, config):
|
||||
use_ssl = conf.get(CONF_SSL)
|
||||
verify_ssl = conf.get(CONF_VERIFY_SSL)
|
||||
name = conf.get(CONF_NAME)
|
||||
entity_filter = conf[CONF_FILTER]
|
||||
|
||||
if use_ssl:
|
||||
uri_scheme = 'https://'
|
||||
@@ -57,7 +76,7 @@ def setup(hass, config):
|
||||
"""Listen for new messages on the bus and sends them to Splunk."""
|
||||
state = event.data.get('new_state')
|
||||
|
||||
if state is None:
|
||||
if state is None or not entity_filter(state.entity_id):
|
||||
return
|
||||
|
||||
try:
|
||||
@@ -76,16 +95,7 @@ def setup(hass, config):
|
||||
}
|
||||
]
|
||||
|
||||
try:
|
||||
payload = {
|
||||
"host": event_collector,
|
||||
"event": json_body,
|
||||
}
|
||||
requests.post(event_collector,
|
||||
data=json.dumps(payload, cls=JSONEncoder),
|
||||
headers=headers, timeout=10, verify=verify_ssl)
|
||||
except requests.exceptions.RequestException as error:
|
||||
_LOGGER.exception("Error saving event to Splunk: %s", error)
|
||||
post_request(event_collector, json_body, headers, verify_ssl)
|
||||
|
||||
hass.bus.listen(EVENT_STATE_CHANGED, splunk_event_listener)
|
||||
|
||||
|
||||
Reference in new issue
Block a user