Convert statsd, influx, splunk, and graphite to use state_as_number()

Fixes #1205
This commit is contained in:
Dan Smith 2016-02-11 17:13:57 +00:00
parent 3aa34deaa2
commit 4a2b956493
5 changed files with 30 additions and 55 deletions

View file

@ -23,8 +23,8 @@ import time
from homeassistant.const import ( from homeassistant.const import (
EVENT_STATE_CHANGED, EVENT_STATE_CHANGED,
EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP, EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP)
STATE_ON, STATE_OFF) from homeassistant.helpers import state
DOMAIN = "graphite" DOMAIN = "graphite"
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -92,13 +92,10 @@ class GraphiteFeeder(threading.Thread):
def _report_attributes(self, entity_id, new_state): def _report_attributes(self, entity_id, new_state):
now = time.time() now = time.time()
things = dict(new_state.attributes) things = dict(new_state.attributes)
state = new_state.state try:
if state in (STATE_ON, STATE_OFF): things['state'] = state.state_as_number(new_state)
state = float(state == STATE_ON) except ValueError:
else: pass
state = None
if state is not None:
things['state'] = state
lines = ['%s.%s.%s %f %i' % (self._prefix, lines = ['%s.%s.%s %f %i' % (self._prefix,
entity_id, key.replace(' ', '_'), entity_id, key.replace(' ', '_'),
value, now) value, now)

View file

@ -9,10 +9,8 @@ https://home-assistant.io/components/influxdb/
import logging import logging
import homeassistant.util as util import homeassistant.util as util
from homeassistant.helpers import validate_config from homeassistant.helpers import validate_config
from homeassistant.const import (EVENT_STATE_CHANGED, STATE_ON, STATE_OFF, from homeassistant.helpers import state as state_helper
STATE_UNLOCKED, STATE_LOCKED, STATE_UNKNOWN) from homeassistant.const import (EVENT_STATE_CHANGED, STATE_UNKNOWN)
from homeassistant.components.sun import (STATE_ABOVE_HORIZON,
STATE_BELOW_HORIZON)
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -73,15 +71,10 @@ def setup(hass, config):
if state is None or state.state in (STATE_UNKNOWN, ''): if state is None or state.state in (STATE_UNKNOWN, ''):
return return
if state.state in (STATE_ON, STATE_LOCKED, STATE_ABOVE_HORIZON): try:
_state = 1 _state = state_helper.state_as_number(state)
elif state.state in (STATE_OFF, STATE_UNLOCKED, STATE_BELOW_HORIZON): except ValueError:
_state = 0 _state = state.state
else:
try:
_state = float(state.state)
except ValueError:
_state = state.state
measurement = state.attributes.get('unit_of_measurement') measurement = state.attributes.get('unit_of_measurement')
if measurement in (None, ''): if measurement in (None, ''):

View file

@ -14,10 +14,8 @@ import requests
import homeassistant.util as util import homeassistant.util as util
from homeassistant.helpers import validate_config from homeassistant.helpers import validate_config
from homeassistant.const import (EVENT_STATE_CHANGED, STATE_ON, STATE_OFF, from homeassistant.helpers import state as state_helper
STATE_UNLOCKED, STATE_LOCKED, STATE_UNKNOWN) from homeassistant.const import EVENT_STATE_CHANGED
from homeassistant.components.sun import (STATE_ABOVE_HORIZON,
STATE_BELOW_HORIZON)
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -64,17 +62,10 @@ def setup(hass, config):
if state is None: if state is None:
return return
if state.state in (STATE_ON, STATE_LOCKED, STATE_ABOVE_HORIZON): try:
_state = 1 _state = state_helper.state_as_number(state)
elif state.state in (STATE_OFF, STATE_UNLOCKED, STATE_UNKNOWN, except ValueError:
STATE_BELOW_HORIZON):
_state = 0
else:
_state = state.state _state = state.state
try:
_state = float(_state)
except ValueError:
pass
json_body = [ json_body = [
{ {

View file

@ -8,10 +8,8 @@ https://home-assistant.io/components/statsd/
""" """
import logging import logging
import homeassistant.util as util import homeassistant.util as util
from homeassistant.const import (EVENT_STATE_CHANGED, STATE_ON, STATE_OFF, from homeassistant.const import EVENT_STATE_CHANGED
STATE_UNLOCKED, STATE_LOCKED, STATE_UNKNOWN) from homeassistant.helpers import state as state_helper
from homeassistant.components.sun import (STATE_ABOVE_HORIZON,
STATE_BELOW_HORIZON)
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -61,19 +59,10 @@ def setup(hass, config):
if state is None: if state is None:
return return
if state.state in (STATE_ON, STATE_LOCKED, STATE_ABOVE_HORIZON): try:
_state = 1 _state = state_helper.state_as_number(state)
elif state.state in (STATE_OFF, STATE_UNLOCKED, STATE_UNKNOWN, except ValueError:
STATE_BELOW_HORIZON): return
_state = 0
else:
_state = state.state
if _state == '':
return
try:
_state = float(_state)
except ValueError:
pass
if not isinstance(_state, NUM_TYPES): if not isinstance(_state, NUM_TYPES):
return return

View file

@ -102,15 +102,20 @@ class TestGraphite(unittest.TestCase):
@mock.patch('time.time') @mock.patch('time.time')
def test_report_with_string_state(self, mock_time): def test_report_with_string_state(self, mock_time):
mock_time.return_value = 12345 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}) state = mock.MagicMock(state='above_horizon', attributes={'foo': 1.0})
with mock.patch.object(self.gf, '_send_to_graphite') as mock_send: with mock.patch.object(self.gf, '_send_to_graphite') as mock_send:
self.gf._report_attributes('entity', state) 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') @mock.patch('time.time')
def test_report_with_binary_state(self, mock_time): def test_report_with_binary_state(self, mock_time):
mock_time.return_value = 12345 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: with mock.patch.object(self.gf, '_send_to_graphite') as mock_send:
self.gf._report_attributes('entity', state) self.gf._report_attributes('entity', state)
expected = ['ha.entity.foo 1.000000 12345', expected = ['ha.entity.foo 1.000000 12345',