8000 splunk: Handle datetime objects in event payload by philk · Pull Request #9628 · home-assistant/core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

splunk: Handle datetime objects in event payload #9628

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion homeassistant/components/splunk.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
CONF_NAME, CONF_HOST, CONF_PORT, CONF_SSL, CONF_TOKEN, EVENT_STATE_CHANGED)
from homeassistant.helpers import state as state_helper
import homeassistant.helpers.config_validation as cv
from homeassistant.remote import JSONEncoder

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -81,7 +82,8 @@ def splunk_event_listener(event):
"host": event_collector,
"event": json_body,
}
requests.post(event_collector, data=json.dumps(payload),
requests.post(event_collector,
data=json.dumps(payload, cls=JSONEncoder),
headers=headers, timeout=10)
except requests.exceptions.RequestException as error:
_LOGGER.exception("Error saving event to Splunk: %s", error)
Expand Down
34 changes: 22 additions & 12 deletions tests/components/test_splunk.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
"""The tests for the Splunk component."""
import json
import unittest
from unittest import mock

from homeassistant.setup import setup_component
import homeassistant.components.splunk as splunk
from homeassistant.const import STATE_ON, STATE_OFF, EVENT_STATE_CHANGED
from homeassistant.helpers import state as state_helper
import homeassistant.util.dt as dt_util

from tests.common import get_test_home_assistant

Expand Down Expand Up @@ -71,30 +74,37 @@ def _setup(self, mock_requests):
self.handler_method = self.hass.bus.listen.call_args_list[0][0][1]

@mock.patch.object(splunk, 'requests')
@mock.patch('json.dumps')
def test_event_listener(self, mock_dump, mock_requests):
def test_event_listener(self, mock_requests):
"""Test event listener."""
mock_dump.side_effect = lambda x: x
self._setup(mock_requests)

valid = {'1': 1,
'1.0': 1.0,
STATE_ON: 1,
STATE_OFF: 0,
'foo': 'foo',
}
now = dt_util.now()
valid = {
'1': 1,
'1.0': 1.0,
STATE_ON: 1,
STATE_OFF: 0,
'foo': 'foo',
}

for in_, out in valid.items():
state = mock.MagicMock(state=in_,
domain='fake',
object_id='entity',
attributes={})
attributes={'datetime_attr': now})
event = mock.MagicMock(data={'new_state': state}, time_fired=12345)

try:
out = state_helper.state_as_number(state)
except ValueError:
out = state.state

body = [{
'domain': 'fake',
'entity_id': 'entity',
'attributes': {},
'attributes': {
'datetime_attr': now.isoformat()
},
'time': '12345',
'value': out,
'host': 'HASS',
Expand All @@ -107,7 +117,7 @@ def test_event_listener(self, mock_dump, mock_requests):
self.assertEqual(
self.mock_post.call_args,
mock.call(
payload['host'], data=payload,
payload['host'], data=json.dumps(payload),
headers={'Authorization': 'Splunk secret'},
timeout=10
)
Expand Down
0