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