Fixed statsd stopping if state is not numeric or only attributes changed (#3981)

* Fixed statsd stopping if attribute changed by not the state

* Fixed tests which exposed a new bug.

* Fixed another issue. whoops
This commit is contained in:
Brent Hughes authored and Paulus Schoutsen committed 2016-10-21 23:18:13 -07:00
1 parent 754d536974
commit 6e5a3c0a94
2 files changed
+13 -10

No files matched your search

+11 -8
View File
@@ -61,18 +61,20 @@ def setup(hass, config):
try:
_state = state_helper.state_as_number(state)
except ValueError:
return
# Set the state to none and continue for any numeric attributes.
_state = None
states = dict(state.attributes)
_LOGGER.debug('Sending %s.%s', state.entity_id, _state)
_LOGGER.debug('Sending %s', state.entity_id)
if show_attribute_flag is True:
statsd_client.gauge(
"%s.state" % state.entity_id,
_state,
sample_rate
)
if isinstance(_state, (float, int)):
statsd_client.gauge(
"%s.state" % state.entity_id,
_state,
sample_rate
)
# Send attribute values
for key, value in states.items():
@@ -81,7 +83,8 @@ def setup(hass, config):
statsd_client.gauge(stat, value, sample_rate)
else:
statsd_client.gauge(state.entity_id, _state, sample_rate)
if isinstance(_state, (float, int)):
statsd_client.gauge(state.entity_id, _state, sample_rate)
# Increment the count
statsd_client.incr(state.entity_id, rate=sample_rate)
+2 -2
View File
@@ -114,7 +114,7 @@ class TestStatsd(unittest.TestCase):
handler_method(mock.MagicMock(data={
'new_state': ha.State('domain.test', invalid, {})}))
self.assertFalse(mock_client.return_value.gauge.called)
self.assertFalse(mock_client.return_value.incr.called)
self.assertTrue(mock_client.return_value.incr.called)
@mock.patch('statsd.StatsClient')
def test_event_listener_attr_details(self, mock_client):
@@ -162,4 +162,4 @@ class TestStatsd(unittest.TestCase):
handler_method(mock.MagicMock(data={
'new_state': ha.State('domain.test', invalid, {})}))
self.assertFalse(mock_client.return_value.gauge.called)
self.assertFalse(mock_client.return_value.incr.called)
self.assertTrue(mock_client.return_value.incr.called)