diff --git a/homeassistant/components/graphite.py b/homeassistant/components/graphite.py index e6675df2f80..5b3e4869307 100644 --- a/homeassistant/components/graphite.py +++ b/homeassistant/components/graphite.py @@ -23,8 +23,8 @@ import time from homeassistant.const import ( EVENT_STATE_CHANGED, - EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP, - STATE_ON, STATE_OFF) + EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP) +from homeassistant.helpers import state DOMAIN = "graphite" _LOGGER = logging.getLogger(__name__) @@ -92,13 +92,10 @@ class GraphiteFeeder(threading.Thread): def _report_attributes(self, entity_id, new_state): now = time.time() things = dict(new_state.attributes) - state = new_state.state - if state in (STATE_ON, STATE_OFF): - state = float(state == STATE_ON) - else: - state = None - if state is not None: - things['state'] = state + try: + things['state'] = state.state_as_number(new_state) + except ValueError: + pass lines = ['%s.%s.%s %f %i' % (self._prefix, entity_id, key.replace(' ', '_'), value, now) diff --git a/homeassistant/components/influxdb.py b/homeassistant/components/influxdb.py index 4211b85e7d1..b23e1210063 100644 --- a/homeassistant/components/influxdb.py +++ b/homeassistant/components/influxdb.py @@ -9,10 +9,8 @@ https://home-assistant.io/components/influxdb/ import logging import homeassistant.util as util from homeassistant.helpers import validate_config -from homeassistant.const import (EVENT_STATE_CHANGED, STATE_ON, STATE_OFF, - STATE_UNLOCKED, STATE_LOCKED, STATE_UNKNOWN) -from homeassistant.components.sun import (STATE_ABOVE_HORIZON, - STATE_BELOW_HORIZON) +from homeassistant.helpers import state as state_helper +from homeassistant.const import (EVENT_STATE_CHANGED, STATE_UNKNOWN) _LOGGER = logging.getLogger(__name__) @@ -73,15 +71,10 @@ def setup(hass, config): if state is None or state.state in (STATE_UNKNOWN, ''): return - if state.state in (STATE_ON, STATE_LOCKED, STATE_ABOVE_HORIZON): - _state = 1 - elif state.state in (STATE_OFF, STATE_UNLOCKED, STATE_BELOW_HORIZON): - _state = 0 - else: - try: - _state = float(state.state) - except ValueError: - _state = state.state + try: + _state = state_helper.state_as_number(state) + except ValueError: + _state = state.state measurement = state.attributes.get('unit_of_measurement') if measurement in (None, ''): diff --git a/homeassistant/components/splunk.py b/homeassistant/components/splunk.py index f936326b734..da773cbe0f7 100644 --- a/homeassistant/components/splunk.py +++ b/homeassistant/components/splunk.py @@ -14,10 +14,8 @@ import requests import homeassistant.util as util from homeassistant.helpers import validate_config -from homeassistant.const import (EVENT_STATE_CHANGED, STATE_ON, STATE_OFF, - STATE_UNLOCKED, STATE_LOCKED, STATE_UNKNOWN) -from homeassistant.components.sun import (STATE_ABOVE_HORIZON, - STATE_BELOW_HORIZON) +from homeassistant.helpers import state as state_helper +from homeassistant.const import EVENT_STATE_CHANGED _LOGGER = logging.getLogger(__name__) @@ -64,17 +62,10 @@ def setup(hass, config): if state is None: return - if state.state in (STATE_ON, STATE_LOCKED, STATE_ABOVE_HORIZON): - _state = 1 - elif state.state in (STATE_OFF, STATE_UNLOCKED, STATE_UNKNOWN, - STATE_BELOW_HORIZON): - _state = 0 - else: + try: + _state = state_helper.state_as_number(state) + except ValueError: _state = state.state - try: - _state = float(_state) - except ValueError: - pass json_body = [ { diff --git a/homeassistant/components/statsd.py b/homeassistant/components/statsd.py index caf102ba529..640b703d5d9 100644 --- a/homeassistant/components/statsd.py +++ b/homeassistant/components/statsd.py @@ -8,10 +8,8 @@ https://home-assistant.io/components/statsd/ """ import logging import homeassistant.util as util -from homeassistant.const import (EVENT_STATE_CHANGED, STATE_ON, STATE_OFF, - STATE_UNLOCKED, STATE_LOCKED, STATE_UNKNOWN) -from homeassistant.components.sun import (STATE_ABOVE_HORIZON, - STATE_BELOW_HORIZON) +from homeassistant.const import EVENT_STATE_CHANGED +from homeassistant.helpers import state as state_helper _LOGGER = logging.getLogger(__name__) @@ -61,19 +59,10 @@ def setup(hass, config): if state is None: return - if state.state in (STATE_ON, STATE_LOCKED, STATE_ABOVE_HORIZON): - _state = 1 - elif state.state in (STATE_OFF, STATE_UNLOCKED, STATE_UNKNOWN, - STATE_BELOW_HORIZON): - _state = 0 - else: - _state = state.state - if _state == '': - return - try: - _state = float(_state) - except ValueError: - pass + try: + _state = state_helper.state_as_number(state) + except ValueError: + return if not isinstance(_state, NUM_TYPES): return diff --git a/tests/components/test_graphite.py b/tests/components/test_graphite.py index 2a3c8750d40..720da54930f 100644 --- a/tests/components/test_graphite.py +++ b/tests/components/test_graphite.py @@ -102,15 +102,20 @@ class TestGraphite(unittest.TestCase): @mock.patch('time.time') def test_report_with_string_state(self, mock_time): mock_time.return_value = 12345 + expected = [ + 'ha.entity.foo 1.000000 12345', + 'ha.entity.state 1.000000 12345', + ] state = mock.MagicMock(state='above_horizon', attributes={'foo': 1.0}) with mock.patch.object(self.gf, '_send_to_graphite') as mock_send: self.gf._report_attributes('entity', state) - mock_send.assert_called_once_with('ha.entity.foo 1.000000 12345') + actual = mock_send.call_args_list[0][0][0].split('\n') + self.assertEqual(sorted(expected), sorted(actual)) @mock.patch('time.time') def test_report_with_binary_state(self, mock_time): mock_time.return_value = 12345 - state = mock.MagicMock(state=STATE_ON, attributes={'foo': 1.0}) + state = ha.State('domain.entity', STATE_ON, {'foo': 1.0}) with mock.patch.object(self.gf, '_send_to_graphite') as mock_send: self.gf._report_attributes('entity', state) expected = ['ha.entity.foo 1.000000 12345',