Skip to content

Commit

Permalink
Use voluptuous for statsd (home-assistant#2928)
Browse files Browse the repository at this point in the history
* Migrate to voluptuous

* Update tests
  • Loading branch information
fabaff authored and balloob committed Sep 14, 2016
1 parent cb3ab1e commit ac5647a
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 25 deletions.
48 changes: 26 additions & 22 deletions homeassistant/components/statsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,50 @@
"""
import logging

import homeassistant.util as util
from homeassistant.const import EVENT_STATE_CHANGED
import voluptuous as vol

from homeassistant.const import (
CONF_HOST, CONF_PORT, CONF_PREFIX, EVENT_STATE_CHANGED)
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers import state as state_helper

REQUIREMENTS = ['statsd==3.2.1']

_LOGGER = logging.getLogger(__name__)

DOMAIN = "statsd"
DEPENDENCIES = []
CONF_ATTR = 'log_attributes'
CONF_RATE = 'rate'

DEFAULT_HOST = 'localhost'
DEFAULT_PORT = 8125
DEFAULT_PREFIX = 'hass'
DEFAULT_RATE = 1
DOMAIN = 'statsd'

REQUIREMENTS = ['statsd==3.2.1']

CONF_HOST = 'host'
CONF_PORT = 'port'
CONF_PREFIX = 'prefix'
CONF_RATE = 'rate'
CONF_ATTR = 'log_attributes'
CONFIG_SCHEMA = vol.Schema({
DOMAIN: vol.Schema({
vol.Required(CONF_HOST, default=DEFAULT_HOST): cv.string,
vol.Optional(CONF_ATTR, default=False): cv.boolean,
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
vol.Optional(CONF_PREFIX, default=DEFAULT_PREFIX): cv.string,
vol.Optional(CONF_RATE, default=DEFAULT_RATE):
vol.All(vol.Coerce(int), vol.Range(min=1)),
}),
}, extra=vol.ALLOW_EXTRA)


def setup(hass, config):
"""Setup the StatsD component."""
import statsd

conf = config[DOMAIN]
host = conf.get(CONF_HOST)
port = conf.get(CONF_PORT)
sample_rate = conf.get(CONF_RATE)
prefix = conf.get(CONF_PREFIX)
show_attribute_flag = conf.get(CONF_ATTR)

host = conf[CONF_HOST]
port = util.convert(conf.get(CONF_PORT), int, DEFAULT_PORT)
sample_rate = util.convert(conf.get(CONF_RATE), int, DEFAULT_RATE)
prefix = util.convert(conf.get(CONF_PREFIX), str, DEFAULT_PREFIX)
show_attribute_flag = conf.get(CONF_ATTR, False)

statsd_client = statsd.StatsClient(
host=host,
port=port,
prefix=prefix
)
statsd_client = statsd.StatsClient(host=host, port=port, prefix=prefix)

def statsd_event_listener(event):
"""Listen for new messages on the bus and sends them to StatsD."""
Expand Down
31 changes: 28 additions & 3 deletions tests/components/test_statsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,29 @@
import unittest
from unittest import mock

import voluptuous as vol

import homeassistant.core as ha
import homeassistant.components.statsd as statsd
from homeassistant.const import STATE_ON, STATE_OFF, EVENT_STATE_CHANGED
from homeassistant.const import (STATE_ON, STATE_OFF, EVENT_STATE_CHANGED)


class TestStatsd(unittest.TestCase):
"""Test the StatsD component."""

def test_invalid_config(self):
"""Test configuration with defaults."""
config = {
'statsd': {
'host1': 'host1',
}
}

with self.assertRaises(vol.Invalid):
statsd.CONFIG_SCHEMA(None)
with self.assertRaises(vol.Invalid):
statsd.CONFIG_SCHEMA(config)

@mock.patch('statsd.StatsClient')
def test_statsd_setup_full(self, mock_connection):
"""Test setup with all data."""
Expand Down Expand Up @@ -40,12 +55,16 @@ def test_statsd_setup_defaults(self, mock_connection):
'host': 'host',
}
}

config['statsd'][statsd.CONF_PORT] = statsd.DEFAULT_PORT
config['statsd'][statsd.CONF_PREFIX] = statsd.DEFAULT_PREFIX

hass = mock.MagicMock()
self.assertTrue(statsd.setup(hass, config))
mock_connection.assert_called_once_with(
host='host',
port=statsd.DEFAULT_PORT,
prefix=statsd.DEFAULT_PREFIX)
port=8125,
prefix='hass')
self.assertTrue(hass.bus.listen.called)

@mock.patch('statsd.StatsClient')
Expand All @@ -56,6 +75,9 @@ def test_event_listener_defaults(self, mock_client):
'host': 'host',
}
}

config['statsd'][statsd.CONF_RATE] = statsd.DEFAULT_RATE

hass = mock.MagicMock()
statsd.setup(hass, config)
self.assertTrue(hass.bus.listen.called)
Expand Down Expand Up @@ -94,6 +116,9 @@ def test_event_listener_attr_details(self, mock_client):
'log_attributes': True
}
}

config['statsd'][statsd.CONF_RATE] = statsd.DEFAULT_RATE

hass = mock.MagicMock()
statsd.setup(hass, config)
self.assertTrue(hass.bus.listen.called)
Expand Down

0 comments on commit ac5647a

Please sign in to comment.