Populate measurement state field for HA states like home/not_home (#9833)

This commit is contained in:
PeteBa 2017-11-19 22:49:49 +00:00 committed by Martin Hjelmare
parent b548116f9b
commit fb32cc39e1
2 changed files with 71 additions and 92 deletions

View file

@ -145,11 +145,16 @@ def setup(hass, config):
(whitelist_d and state.domain not in whitelist_d):
return
_state = float(state_helper.state_as_number(state))
_state_key = "value"
_include_state = _include_value = False
_state_as_value = float(state.state)
_include_value = True
except ValueError:
_state = state.state
_state_key = "state"
try:
_state_as_value = float(state_helper.state_as_number(state))
_include_state = _include_value = True
except ValueError:
_include_state = True
include_uom = True
measurement = component_config.get(state.entity_id).get(
@ -176,10 +181,13 @@ def setup(hass, config):
},
'time': event.time_fired,
'fields': {
_state_key: _state,
}
}
]
if _include_state:
json_body[0]['fields']['state'] = state.state
if _include_value:
json_body[0]['fields']['value'] = _state_as_value
for key, value in state.attributes.items():
if key in tags_attributes:

View file

@ -7,7 +7,8 @@ import influxdb as influx_client
from homeassistant.setup import setup_component
import homeassistant.components.influxdb as influxdb
from homeassistant.const import EVENT_STATE_CHANGED, STATE_OFF, STATE_ON
from homeassistant.const import EVENT_STATE_CHANGED, STATE_OFF, STATE_ON, \
STATE_STANDBY
from tests.common import get_test_home_assistant
@ -110,12 +111,14 @@ class TestInfluxDB(unittest.TestCase):
"""Test the event listener."""
self._setup()
# map of HA State to valid influxdb [state, value] fields
valid = {
'1': 1,
'1.0': 1.0,
STATE_ON: 1,
STATE_OFF: 0,
'foo': 'foo'
'1': [None, 1],
'1.0': [None, 1.0],
STATE_ON: [STATE_ON, 1],
STATE_OFF: [STATE_OFF, 0],
STATE_STANDBY: [STATE_STANDBY, None],
'foo': ['foo', None]
}
for in_, out in valid.items():
attrs = {
@ -132,7 +135,6 @@ class TestInfluxDB(unittest.TestCase):
state=in_, domain='fake', entity_id='fake.entity-id',
object_id='entity', attributes=attrs)
event = mock.MagicMock(data={'new_state': state}, time_fired=12345)
if isinstance(out, str):
body = [{
'measurement': 'foobars',
'tags': {
@ -141,7 +143,6 @@ class TestInfluxDB(unittest.TestCase):
},
'time': 12345,
'fields': {
'state': out,
'longitude': 1.1,
'latitude': 2.2,
'battery_level_str': '99%',
@ -155,30 +156,11 @@ class TestInfluxDB(unittest.TestCase):
'multi_periods_str': '0.120.240.2023873'
},
}]
if out[0] is not None:
body[0]['fields']['state'] = out[0]
if out[1] is not None:
body[0]['fields']['value'] = out[1]
else:
body = [{
'measurement': 'foobars',
'tags': {
'domain': 'fake',
'entity_id': 'entity',
},
'time': 12345,
'fields': {
'value': out,
'longitude': 1.1,
'latitude': 2.2,
'battery_level_str': '99%',
'battery_level': 99.0,
'temperature_str': '20c',
'temperature': 20.0,
'last_seen_str': 'Last seen 23 minutes ago',
'last_seen': 23.0,
'updated_at_str': '2017-01-01 00:00:00',
'updated_at': 20170101000000,
'multi_periods_str': '0.120.240.2023873'
},
}]
self.handler_method(event)
self.assertEqual(
mock_client.return_value.write_points.call_count, 1
@ -428,12 +410,14 @@ class TestInfluxDB(unittest.TestCase):
"""Test the event listener when an attribute has an invalid type."""
self._setup()
# map of HA State to valid influxdb [state, value] fields
valid = {
'1': 1,
'1.0': 1.0,
STATE_ON: 1,
STATE_OFF: 0,
'foo': 'foo'
'1': [None, 1],
'1.0': [None, 1.0],
STATE_ON: [STATE_ON, 1],
STATE_OFF: [STATE_OFF, 0],
STATE_STANDBY: [STATE_STANDBY, None],
'foo': ['foo', None]
}
for in_, out in valid.items():
attrs = {
@ -446,7 +430,6 @@ class TestInfluxDB(unittest.TestCase):
state=in_, domain='fake', entity_id='fake.entity-id',
object_id='entity', attributes=attrs)
event = mock.MagicMock(data={'new_state': state}, time_fired=12345)
if isinstance(out, str):
body = [{
'measurement': 'foobars',
'tags': {
@ -455,28 +438,16 @@ class TestInfluxDB(unittest.TestCase):
},
'time': 12345,
'fields': {
'state': out,
'longitude': 1.1,
'latitude': 2.2,
'invalid_attribute_str': "['value1', 'value2']"
},
}]
if out[0] is not None:
body[0]['fields']['state'] = out[0]
if out[1] is not None:
body[0]['fields']['value'] = out[1]
else:
body = [{
'measurement': 'foobars',
'tags': {
'domain': 'fake',
'entity_id': 'entity',
},
'time': 12345,
'fields': {
'value': float(out),
'longitude': 1.1,
'latitude': 2.2,
'invalid_attribute_str': "['value1', 'value2']"
},
}]
self.handler_method(event)
self.assertEqual(
mock_client.return_value.write_points.call_count, 1