mirror of
https://github.com/home-assistant/core.git
synced 2025-04-23 00:37:53 +00:00
Convert statsd, influx, splunk, and graphite to use state_as_number()
Fixes #1205
This commit is contained in:
parent
3aa34deaa2
commit
4a2b956493
@ -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)
|
||||
|
@ -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, ''):
|
||||
|
@ -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 = [
|
||||
{
|
||||
|
@ -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
|
||||
|
@ -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',
|
||||
|
Loading…
x
Reference in New Issue
Block a user