Updated to new statsd library and added state change counters (#2429)
parent
ef74bd9892
commit
ffccca1f60
|
@ -20,7 +20,7 @@ DEFAULT_PORT = 8125
|
|||
DEFAULT_PREFIX = 'hass'
|
||||
DEFAULT_RATE = 1
|
||||
|
||||
REQUIREMENTS = ['python-statsd==1.7.2']
|
||||
REQUIREMENTS = ['statsd==3.2.1']
|
||||
|
||||
CONF_HOST = 'host'
|
||||
CONF_PORT = 'port'
|
||||
|
@ -30,7 +30,6 @@ CONF_RATE = 'rate'
|
|||
|
||||
def setup(hass, config):
|
||||
"""Setup the StatsD component."""
|
||||
from statsd.compat import NUM_TYPES
|
||||
import statsd
|
||||
|
||||
conf = config[DOMAIN]
|
||||
|
@ -40,15 +39,12 @@ def setup(hass, config):
|
|||
sample_rate = util.convert(conf.get(CONF_RATE), int, DEFAULT_RATE)
|
||||
prefix = util.convert(conf.get(CONF_PREFIX), str, DEFAULT_PREFIX)
|
||||
|
||||
statsd_connection = statsd.Connection(
|
||||
statsd_client = statsd.StatsClient(
|
||||
host=host,
|
||||
port=port,
|
||||
sample_rate=sample_rate,
|
||||
disabled=False
|
||||
prefix=prefix
|
||||
)
|
||||
|
||||
meter = statsd.Gauge(prefix, statsd_connection)
|
||||
|
||||
def statsd_event_listener(event):
|
||||
"""Listen for new messages on the bus and sends them to StatsD."""
|
||||
state = event.data.get('new_state')
|
||||
|
@ -61,11 +57,11 @@ def setup(hass, config):
|
|||
except ValueError:
|
||||
return
|
||||
|
||||
if not isinstance(_state, NUM_TYPES):
|
||||
return
|
||||
|
||||
_LOGGER.debug('Sending %s.%s', state.entity_id, _state)
|
||||
meter.send(state.entity_id, _state)
|
||||
statsd_client.gauge(state.entity_id, _state, sample_rate)
|
||||
|
||||
# Increment the count
|
||||
statsd_client.incr(state.entity_id, rate=sample_rate)
|
||||
|
||||
hass.bus.listen(EVENT_STATE_CHANGED, statsd_event_listener)
|
||||
|
||||
|
|
|
@ -319,9 +319,6 @@ python-nmap==0.6.0
|
|||
# homeassistant.components.notify.pushover
|
||||
python-pushover==0.2
|
||||
|
||||
# homeassistant.components.statsd
|
||||
python-statsd==1.7.2
|
||||
|
||||
# homeassistant.components.notify.telegram
|
||||
python-telegram-bot==4.3.1
|
||||
|
||||
|
@ -389,6 +386,9 @@ sqlalchemy==1.0.13
|
|||
# homeassistant.components.http
|
||||
static3==0.7.0
|
||||
|
||||
# homeassistant.components.statsd
|
||||
statsd==3.2.1
|
||||
|
||||
# homeassistant.components.sensor.steam_online
|
||||
steamodd==4.21
|
||||
|
||||
|
|
|
@ -10,9 +10,8 @@ from homeassistant.const import STATE_ON, STATE_OFF, EVENT_STATE_CHANGED
|
|||
class TestStatsd(unittest.TestCase):
|
||||
"""Test the StatsD component."""
|
||||
|
||||
@mock.patch('statsd.Connection')
|
||||
@mock.patch('statsd.Gauge')
|
||||
def test_statsd_setup_full(self, mock_gauge, mock_connection):
|
||||
@mock.patch('statsd.StatsClient')
|
||||
def test_statsd_setup_full(self, mock_connection):
|
||||
"""Test setup with all data."""
|
||||
config = {
|
||||
'statsd': {
|
||||
|
@ -24,18 +23,17 @@ class TestStatsd(unittest.TestCase):
|
|||
}
|
||||
hass = mock.MagicMock()
|
||||
self.assertTrue(statsd.setup(hass, config))
|
||||
mock_connection.assert_called_once_with(host='host', port=123,
|
||||
sample_rate=1,
|
||||
disabled=False)
|
||||
mock_gauge.assert_called_once_with('foo',
|
||||
mock_connection.return_value)
|
||||
mock_connection.assert_called_once_with(
|
||||
host='host',
|
||||
port=123,
|
||||
prefix='foo')
|
||||
|
||||
self.assertTrue(hass.bus.listen.called)
|
||||
self.assertEqual(EVENT_STATE_CHANGED,
|
||||
hass.bus.listen.call_args_list[0][0][0])
|
||||
|
||||
@mock.patch('statsd.Connection')
|
||||
@mock.patch('statsd.Gauge')
|
||||
def test_statsd_setup_defaults(self, mock_gauge, mock_connection):
|
||||
@mock.patch('statsd.StatsClient')
|
||||
def test_statsd_setup_defaults(self, mock_connection):
|
||||
"""Test setup with defaults."""
|
||||
config = {
|
||||
'statsd': {
|
||||
|
@ -47,15 +45,11 @@ class TestStatsd(unittest.TestCase):
|
|||
mock_connection.assert_called_once_with(
|
||||
host='host',
|
||||
port=statsd.DEFAULT_PORT,
|
||||
sample_rate=statsd.DEFAULT_RATE,
|
||||
disabled=False)
|
||||
mock_gauge.assert_called_once_with(statsd.DEFAULT_PREFIX,
|
||||
mock_connection.return_value)
|
||||
prefix=statsd.DEFAULT_PREFIX)
|
||||
self.assertTrue(hass.bus.listen.called)
|
||||
|
||||
@mock.patch('statsd.Connection')
|
||||
@mock.patch('statsd.Gauge')
|
||||
def test_event_listener(self, mock_gauge, mock_connection):
|
||||
@mock.patch('statsd.StatsClient')
|
||||
def test_event_listener(self, mock_client):
|
||||
"""Test event listener."""
|
||||
config = {
|
||||
'statsd': {
|
||||
|
@ -74,11 +68,16 @@ class TestStatsd(unittest.TestCase):
|
|||
for in_, out in valid.items():
|
||||
state = mock.MagicMock(state=in_)
|
||||
handler_method(mock.MagicMock(data={'new_state': state}))
|
||||
mock_gauge.return_value.send.assert_called_once_with(
|
||||
state.entity_id, out)
|
||||
mock_gauge.return_value.send.reset_mock()
|
||||
mock_client.return_value.gauge.assert_called_once_with(
|
||||
state.entity_id, out, statsd.DEFAULT_RATE)
|
||||
mock_client.return_value.gauge.reset_mock()
|
||||
|
||||
mock_client.return_value.incr.assert_called_once_with(
|
||||
state.entity_id, rate=statsd.DEFAULT_RATE)
|
||||
mock_client.return_value.incr.reset_mock()
|
||||
|
||||
for invalid in ('foo', '', object):
|
||||
handler_method(mock.MagicMock(data={
|
||||
'new_state': ha.State('domain.test', invalid, {})}))
|
||||
self.assertFalse(mock_gauge.return_value.send.called)
|
||||
self.assertFalse(mock_client.return_value.gauge.called)
|
||||
self.assertFalse(mock_client.return_value.incr.called)
|
||||
|
|
Loading…
Reference in New Issue