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 2016-10-22 01:18:13 -05:00 committed by Paulus Schoutsen
parent 754d536974
commit 6e5a3c0a94
2 changed files with 13 additions and 10 deletions

View File

@ -61,18 +61,20 @@ def setup(hass, config):
try: try:
_state = state_helper.state_as_number(state) _state = state_helper.state_as_number(state)
except ValueError: except ValueError:
return # Set the state to none and continue for any numeric attributes.
_state = None
states = dict(state.attributes) 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: if show_attribute_flag is True:
statsd_client.gauge( if isinstance(_state, (float, int)):
"%s.state" % state.entity_id, statsd_client.gauge(
_state, "%s.state" % state.entity_id,
sample_rate _state,
) sample_rate
)
# Send attribute values # Send attribute values
for key, value in states.items(): for key, value in states.items():
@ -81,7 +83,8 @@ def setup(hass, config):
statsd_client.gauge(stat, value, sample_rate) statsd_client.gauge(stat, value, sample_rate)
else: 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 # Increment the count
statsd_client.incr(state.entity_id, rate=sample_rate) statsd_client.incr(state.entity_id, rate=sample_rate)

View File

@ -114,7 +114,7 @@ class TestStatsd(unittest.TestCase):
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_client.return_value.gauge.called) 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') @mock.patch('statsd.StatsClient')
def test_event_listener_attr_details(self, mock_client): def test_event_listener_attr_details(self, mock_client):
@ -162,4 +162,4 @@ class TestStatsd(unittest.TestCase):
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_client.return_value.gauge.called) self.assertFalse(mock_client.return_value.gauge.called)
self.assertFalse(mock_client.return_value.incr.called) self.assertTrue(mock_client.return_value.incr.called)