From 6e5a3c0a949894a407764e5543aceb467e205128 Mon Sep 17 00:00:00 2001 From: Brent Hughes Date: Sat, 22 Oct 2016 01:18:13 -0500 Subject: [PATCH] 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 --- homeassistant/components/statsd.py | 19 +++++++++++-------- tests/components/test_statsd.py | 4 ++-- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/homeassistant/components/statsd.py b/homeassistant/components/statsd.py index 3a99a65fe6a..d85bc1e030c 100644 --- a/homeassistant/components/statsd.py +++ b/homeassistant/components/statsd.py @@ -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) diff --git a/tests/components/test_statsd.py b/tests/components/test_statsd.py index 696617a92eb..ccc494fbc24 100644 --- a/tests/components/test_statsd.py +++ b/tests/components/test_statsd.py @@ -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)