From 6f27d58188ce908ea6f04e0f9cd7887c1201b614 Mon Sep 17 00:00:00 2001 From: Fabian Affolter Date: Wed, 24 Aug 2016 03:58:59 +0200 Subject: [PATCH] Use voluptuous for Splunk (#2931) * Migrate to voluptuous * Update tests --- homeassistant/components/splunk.py | 50 ++++++++++++++---------------- tests/components/test_splunk.py | 13 ++++++-- 2 files changed, 34 insertions(+), 29 deletions(-) diff --git a/homeassistant/components/splunk.py b/homeassistant/components/splunk.py index 9a5f6e385eb..2ae2842bceb 100644 --- a/homeassistant/components/splunk.py +++ b/homeassistant/components/splunk.py @@ -1,8 +1,6 @@ """ 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 https://home-assistant.io/components/splunk/ """ @@ -10,47 +8,47 @@ import json import logging import requests +import voluptuous as vol -import homeassistant.util as util -from homeassistant.const import EVENT_STATE_CHANGED +from homeassistant.const import ( + CONF_HOST, CONF_PORT, CONF_SSL, CONF_TOKEN, EVENT_STATE_CHANGED) 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__) -DOMAIN = "splunk" -DEPENDENCIES = [] +DOMAIN = 'splunk' DEFAULT_HOST = 'localhost' -DEFAULT_PORT = '8088' +DEFAULT_PORT = 8088 DEFAULT_SSL = False -CONF_HOST = 'host' -CONF_PORT = 'port' -CONF_TOKEN = 'token' -CONF_SSL = 'SSL' +CONFIG_SCHEMA = vol.Schema({ + DOMAIN: vol.Schema({ + vol.Required(CONF_TOKEN): cv.string, + 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): """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] + 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: - uri_scheme = "https://" + uri_scheme = 'https://' else: - uri_scheme = "http://" - event_collector = uri_scheme + host + ":" + str(port) + \ - "/services/collector/event" - headers = {'Authorization': 'Splunk ' + token} + uri_scheme = 'http://' + + event_collector = '{}{}:{}/services/collector/event'.format( + uri_scheme, host, port) + headers = {'Authorization': 'Splunk {}'.format(token)} def splunk_event_listener(event): """Listen for new messages on the bus and sends them to Splunk.""" diff --git a/tests/components/test_splunk.py b/tests/components/test_splunk.py index e4e9cf96ee0..4b7dd5f0732 100644 --- a/tests/components/test_splunk.py +++ b/tests/components/test_splunk.py @@ -19,6 +19,7 @@ class TestSplunk(unittest.TestCase): 'use_ssl': 'False', } } + hass = mock.MagicMock() self.assertTrue(splunk.setup(hass, config)) self.assertTrue(hass.bus.listen.called) @@ -33,6 +34,7 @@ class TestSplunk(unittest.TestCase): 'token': 'secret', } } + hass = mock.MagicMock() self.assertTrue(splunk.setup(hass, config)) self.assertTrue(hass.bus.listen.called) @@ -48,8 +50,10 @@ class TestSplunk(unittest.TestCase): 'splunk': { 'host': 'host', 'token': 'secret', + 'port': 8088, } } + self.hass = mock.MagicMock() splunk.setup(self.hass, config) 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, STATE_ON: 1, STATE_OFF: 0, - 'foo': 'foo'} + 'foo': 'foo', + } + for in_, out in valid.items(): state = mock.MagicMock(state=in_, domain='fake', object_id='entity', attributes={}) - event = mock.MagicMock(data={'new_state': state}, - time_fired=12345) + event = mock.MagicMock(data={'new_state': state}, time_fired=12345) + body = [{ 'domain': 'fake', 'entity_id': 'entity', @@ -80,6 +86,7 @@ class TestSplunk(unittest.TestCase): 'time': '12345', 'value': out, }] + payload = {'host': 'http://host:8088/services/collector/event', 'event': body} self.handler_method(event)