2016-03-09 09:25:50 +00:00
|
|
|
"""The tests for the InfluxDB component."""
|
2016-02-12 05:24:49 +00:00
|
|
|
import unittest
|
2017-06-20 05:53:13 +00:00
|
|
|
import datetime
|
2016-02-12 05:24:49 +00:00
|
|
|
from unittest import mock
|
|
|
|
|
|
|
|
import influxdb as influx_client
|
|
|
|
|
2017-03-05 09:41:54 +00:00
|
|
|
from homeassistant.setup import setup_component
|
2016-02-12 05:24:49 +00:00
|
|
|
import homeassistant.components.influxdb as influxdb
|
2017-11-19 22:49:49 +00:00
|
|
|
from homeassistant.const import EVENT_STATE_CHANGED, STATE_OFF, STATE_ON, \
|
|
|
|
STATE_STANDBY
|
2016-02-12 05:24:49 +00:00
|
|
|
|
2016-10-27 07:16:23 +00:00
|
|
|
from tests.common import get_test_home_assistant
|
|
|
|
|
2016-02-12 05:24:49 +00:00
|
|
|
|
2016-04-16 14:05:05 +00:00
|
|
|
@mock.patch('influxdb.InfluxDBClient')
|
2016-02-12 05:24:49 +00:00
|
|
|
class TestInfluxDB(unittest.TestCase):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test the InfluxDB component."""
|
|
|
|
|
2016-04-16 14:05:05 +00:00
|
|
|
def setUp(self):
|
|
|
|
"""Setup things to be run when tests are started."""
|
2016-10-31 15:47:29 +00:00
|
|
|
self.hass = get_test_home_assistant()
|
2016-04-16 14:05:05 +00:00
|
|
|
self.handler_method = None
|
2016-10-27 07:16:23 +00:00
|
|
|
self.hass.bus.listen = mock.Mock()
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
"""Clear data."""
|
|
|
|
self.hass.stop()
|
2016-04-16 14:05:05 +00:00
|
|
|
|
2016-02-12 05:24:49 +00:00
|
|
|
def test_setup_config_full(self, mock_client):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test the setup with full configuration."""
|
2016-02-12 05:24:49 +00:00
|
|
|
config = {
|
|
|
|
'influxdb': {
|
|
|
|
'host': 'host',
|
|
|
|
'port': 123,
|
|
|
|
'database': 'db',
|
|
|
|
'username': 'user',
|
|
|
|
'password': 'password',
|
|
|
|
'ssl': 'False',
|
|
|
|
'verify_ssl': 'False',
|
|
|
|
}
|
|
|
|
}
|
2016-09-18 22:32:18 +00:00
|
|
|
assert setup_component(self.hass, influxdb.DOMAIN, config)
|
2016-04-16 14:05:05 +00:00
|
|
|
self.assertTrue(self.hass.bus.listen.called)
|
2016-10-20 17:10:12 +00:00
|
|
|
self.assertEqual(
|
|
|
|
EVENT_STATE_CHANGED, self.hass.bus.listen.call_args_list[0][0][0])
|
2016-02-12 05:24:49 +00:00
|
|
|
self.assertTrue(mock_client.return_value.query.called)
|
|
|
|
|
|
|
|
def test_setup_config_defaults(self, mock_client):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test the setup with default configuration."""
|
2016-02-12 05:24:49 +00:00
|
|
|
config = {
|
|
|
|
'influxdb': {
|
|
|
|
'host': 'host',
|
|
|
|
'username': 'user',
|
|
|
|
'password': 'pass',
|
|
|
|
}
|
|
|
|
}
|
2016-09-18 22:32:18 +00:00
|
|
|
assert setup_component(self.hass, influxdb.DOMAIN, config)
|
2016-04-16 14:05:05 +00:00
|
|
|
self.assertTrue(self.hass.bus.listen.called)
|
2016-10-20 17:10:12 +00:00
|
|
|
self.assertEqual(
|
|
|
|
EVENT_STATE_CHANGED, self.hass.bus.listen.call_args_list[0][0][0])
|
2016-02-12 05:24:49 +00:00
|
|
|
|
2016-10-15 04:10:04 +00:00
|
|
|
def test_setup_minimal_config(self, mock_client):
|
2016-10-20 17:10:12 +00:00
|
|
|
"""Test the setup with minimal configuration."""
|
2016-10-15 04:10:04 +00:00
|
|
|
config = {
|
|
|
|
'influxdb': {}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert setup_component(self.hass, influxdb.DOMAIN, config)
|
|
|
|
|
|
|
|
def test_setup_missing_password(self, mock_client):
|
|
|
|
"""Test the setup with existing username and missing password."""
|
2016-02-12 05:24:49 +00:00
|
|
|
config = {
|
|
|
|
'influxdb': {
|
2016-10-15 04:10:04 +00:00
|
|
|
'username': 'user'
|
2016-02-12 05:24:49 +00:00
|
|
|
}
|
|
|
|
}
|
2016-10-15 04:10:04 +00:00
|
|
|
|
|
|
|
assert not setup_component(self.hass, influxdb.DOMAIN, config)
|
2016-02-12 05:24:49 +00:00
|
|
|
|
|
|
|
def test_setup_query_fail(self, mock_client):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test the setup for query failures."""
|
2016-02-12 05:24:49 +00:00
|
|
|
config = {
|
|
|
|
'influxdb': {
|
|
|
|
'host': 'host',
|
|
|
|
'username': 'user',
|
|
|
|
'password': 'pass',
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mock_client.return_value.query.side_effect = \
|
|
|
|
influx_client.exceptions.InfluxDBClientError('fake')
|
2016-09-18 22:32:18 +00:00
|
|
|
assert not setup_component(self.hass, influxdb.DOMAIN, config)
|
2016-02-12 05:24:49 +00:00
|
|
|
|
2016-04-16 14:05:05 +00:00
|
|
|
def _setup(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Setup the client."""
|
2016-02-12 05:24:49 +00:00
|
|
|
config = {
|
|
|
|
'influxdb': {
|
|
|
|
'host': 'host',
|
|
|
|
'username': 'user',
|
|
|
|
'password': 'pass',
|
2017-04-26 19:14:52 +00:00
|
|
|
'exclude': {
|
|
|
|
'entities': ['fake.blacklisted'],
|
|
|
|
'domains': ['another_fake']
|
|
|
|
}
|
2016-02-12 05:24:49 +00:00
|
|
|
}
|
|
|
|
}
|
2016-09-18 22:32:18 +00:00
|
|
|
assert setup_component(self.hass, influxdb.DOMAIN, config)
|
2016-02-12 05:24:49 +00:00
|
|
|
self.handler_method = self.hass.bus.listen.call_args_list[0][0][1]
|
|
|
|
|
2016-04-16 14:05:05 +00:00
|
|
|
def test_event_listener(self, mock_client):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test the event listener."""
|
2016-04-16 14:05:05 +00:00
|
|
|
self._setup()
|
2016-02-12 05:24:49 +00:00
|
|
|
|
2017-11-19 22:49:49 +00:00
|
|
|
# map of HA State to valid influxdb [state, value] fields
|
2017-01-14 17:52:47 +00:00
|
|
|
valid = {
|
2017-11-19 22:49:49 +00:00
|
|
|
'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]
|
2017-01-14 17:52:47 +00:00
|
|
|
}
|
2016-02-12 05:24:49 +00:00
|
|
|
for in_, out in valid.items():
|
2017-01-14 17:52:47 +00:00
|
|
|
attrs = {
|
|
|
|
'unit_of_measurement': 'foobars',
|
|
|
|
'longitude': '1.1',
|
Make percentage string values as floats/ints in InfluxDB (#7879)
* Make percentage string values as floats in InfluxDB
Currently Z-wave and other compontents report an attributes battery
level as an integer, for example
```yaml
{
"is_awake": false,
"battery_level": 61,
}
```
However, some other components like Vera add the battery level as a
string
```yaml
{
"Vera Device Id": 25,
"device_armed": "False",
"battery_level": "63%",
"device_tripped": "False",
}
```
By removing any % signs in the field, this will send the value to
InfluxDB as an int, which can then be used to plot the data in graphs
correctly, like other percentage fields.
* Add tests and remove all trailing non digits
Adds tests and now removes all trailing non-numeric characters for
better use
* Update variable name for InfluxDB digit checks
Updates the variable used for the regex to remove trailing non digits
* Fix linting errors for InfluxDB component
Fixes a small linting error on the InfluxDB component
2017-06-13 22:42:55 +00:00
|
|
|
'latitude': '2.2',
|
|
|
|
'battery_level': '99%',
|
|
|
|
'temperature': '20c',
|
2017-06-20 05:53:13 +00:00
|
|
|
'last_seen': 'Last seen 23 minutes ago',
|
|
|
|
'updated_at': datetime.datetime(2017, 1, 1, 0, 0),
|
|
|
|
'multi_periods': '0.120.240.2023873'
|
2017-01-14 17:52:47 +00:00
|
|
|
}
|
|
|
|
state = mock.MagicMock(
|
2017-08-03 14:26:01 +00:00
|
|
|
state=in_, domain='fake', entity_id='fake.entity-id',
|
|
|
|
object_id='entity', attributes=attrs)
|
2016-10-20 17:10:12 +00:00
|
|
|
event = mock.MagicMock(data={'new_state': state}, time_fired=12345)
|
2017-11-19 22:49:49 +00:00
|
|
|
body = [{
|
|
|
|
'measurement': 'foobars',
|
|
|
|
'tags': {
|
|
|
|
'domain': 'fake',
|
|
|
|
'entity_id': 'entity',
|
|
|
|
},
|
|
|
|
'time': 12345,
|
|
|
|
'fields': {
|
|
|
|
'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'
|
|
|
|
},
|
|
|
|
}]
|
|
|
|
if out[0] is not None:
|
|
|
|
body[0]['fields']['state'] = out[0]
|
|
|
|
if out[1] is not None:
|
|
|
|
body[0]['fields']['value'] = out[1]
|
2016-02-12 05:24:49 +00:00
|
|
|
|
2017-01-14 17:52:47 +00:00
|
|
|
self.handler_method(event)
|
|
|
|
self.assertEqual(
|
|
|
|
mock_client.return_value.write_points.call_count, 1
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
mock_client.return_value.write_points.call_args,
|
|
|
|
mock.call(body)
|
|
|
|
)
|
|
|
|
mock_client.return_value.write_points.reset_mock()
|
2017-01-04 21:36:54 +00:00
|
|
|
|
2017-01-14 17:52:47 +00:00
|
|
|
def test_event_listener_no_units(self, mock_client):
|
|
|
|
"""Test the event listener for missing units."""
|
|
|
|
self._setup()
|
2016-02-12 05:24:49 +00:00
|
|
|
|
2017-01-14 17:52:47 +00:00
|
|
|
for unit in (None, ''):
|
|
|
|
if unit:
|
|
|
|
attrs = {'unit_of_measurement': unit}
|
|
|
|
else:
|
|
|
|
attrs = {}
|
|
|
|
state = mock.MagicMock(
|
2017-08-03 14:26:01 +00:00
|
|
|
state=1, domain='fake', entity_id='fake.entity-id',
|
2017-01-14 17:52:47 +00:00
|
|
|
object_id='entity', attributes=attrs)
|
|
|
|
event = mock.MagicMock(data={'new_state': state}, time_fired=12345)
|
|
|
|
body = [{
|
2017-08-03 14:26:01 +00:00
|
|
|
'measurement': 'fake.entity-id',
|
2017-01-14 17:52:47 +00:00
|
|
|
'tags': {
|
|
|
|
'domain': 'fake',
|
|
|
|
'entity_id': 'entity',
|
|
|
|
},
|
|
|
|
'time': 12345,
|
|
|
|
'fields': {
|
|
|
|
'value': 1,
|
|
|
|
},
|
|
|
|
}]
|
2016-02-12 05:24:49 +00:00
|
|
|
self.handler_method(event)
|
2016-10-16 23:13:27 +00:00
|
|
|
self.assertEqual(
|
|
|
|
mock_client.return_value.write_points.call_count, 1
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
mock_client.return_value.write_points.call_args,
|
|
|
|
mock.call(body)
|
|
|
|
)
|
2016-04-16 14:05:05 +00:00
|
|
|
mock_client.return_value.write_points.reset_mock()
|
2016-02-12 05:24:49 +00:00
|
|
|
|
2016-04-16 14:05:05 +00:00
|
|
|
def test_event_listener_fail_write(self, mock_client):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test the event listener for write failures."""
|
2016-04-16 14:05:05 +00:00
|
|
|
self._setup()
|
2016-02-12 05:24:49 +00:00
|
|
|
|
2016-10-20 17:10:12 +00:00
|
|
|
state = mock.MagicMock(
|
2017-08-03 14:26:01 +00:00
|
|
|
state=1, domain='fake', entity_id='fake.entity-id',
|
|
|
|
object_id='entity', attributes={})
|
2016-10-20 17:10:12 +00:00
|
|
|
event = mock.MagicMock(data={'new_state': state}, time_fired=12345)
|
2016-04-16 14:05:05 +00:00
|
|
|
mock_client.return_value.write_points.side_effect = \
|
2016-02-12 05:24:49 +00:00
|
|
|
influx_client.exceptions.InfluxDBClientError('foo')
|
|
|
|
self.handler_method(event)
|
2016-03-03 10:09:37 +00:00
|
|
|
|
2017-01-14 17:52:47 +00:00
|
|
|
def test_event_listener_states(self, mock_client):
|
|
|
|
"""Test the event listener against ignored states."""
|
|
|
|
self._setup()
|
|
|
|
|
|
|
|
for state_state in (1, 'unknown', '', 'unavailable'):
|
|
|
|
state = mock.MagicMock(
|
2017-08-03 14:26:01 +00:00
|
|
|
state=state_state, domain='fake', entity_id='fake.entity-id',
|
2017-01-14 17:52:47 +00:00
|
|
|
object_id='entity', attributes={})
|
|
|
|
event = mock.MagicMock(data={'new_state': state}, time_fired=12345)
|
|
|
|
body = [{
|
2017-08-03 14:26:01 +00:00
|
|
|
'measurement': 'fake.entity-id',
|
2017-01-14 17:52:47 +00:00
|
|
|
'tags': {
|
|
|
|
'domain': 'fake',
|
|
|
|
'entity_id': 'entity',
|
|
|
|
},
|
|
|
|
'time': 12345,
|
|
|
|
'fields': {
|
|
|
|
'value': 1,
|
|
|
|
},
|
|
|
|
}]
|
|
|
|
self.handler_method(event)
|
|
|
|
if state_state == 1:
|
|
|
|
self.assertEqual(
|
|
|
|
mock_client.return_value.write_points.call_count, 1
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
mock_client.return_value.write_points.call_args,
|
|
|
|
mock.call(body)
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
self.assertFalse(mock_client.return_value.write_points.called)
|
|
|
|
mock_client.return_value.write_points.reset_mock()
|
|
|
|
|
2016-04-16 14:05:05 +00:00
|
|
|
def test_event_listener_blacklist(self, mock_client):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test the event listener against a blacklist."""
|
2016-04-16 14:05:05 +00:00
|
|
|
self._setup()
|
2016-03-03 10:09:37 +00:00
|
|
|
|
|
|
|
for entity_id in ('ok', 'blacklisted'):
|
2016-10-20 17:10:12 +00:00
|
|
|
state = mock.MagicMock(
|
|
|
|
state=1, domain='fake', entity_id='fake.{}'.format(entity_id),
|
|
|
|
object_id=entity_id, attributes={})
|
|
|
|
event = mock.MagicMock(data={'new_state': state}, time_fired=12345)
|
2017-01-14 17:52:47 +00:00
|
|
|
body = [{
|
|
|
|
'measurement': 'fake.{}'.format(entity_id),
|
|
|
|
'tags': {
|
|
|
|
'domain': 'fake',
|
|
|
|
'entity_id': entity_id,
|
2016-03-03 10:09:37 +00:00
|
|
|
},
|
2017-01-14 17:52:47 +00:00
|
|
|
'time': 12345,
|
|
|
|
'fields': {
|
|
|
|
'value': 1,
|
|
|
|
},
|
|
|
|
}]
|
2016-03-03 10:09:37 +00:00
|
|
|
self.handler_method(event)
|
|
|
|
if entity_id == 'ok':
|
2016-10-16 23:13:27 +00:00
|
|
|
self.assertEqual(
|
|
|
|
mock_client.return_value.write_points.call_count, 1
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
mock_client.return_value.write_points.call_args,
|
|
|
|
mock.call(body)
|
|
|
|
)
|
2016-03-03 10:09:37 +00:00
|
|
|
else:
|
2016-04-16 14:05:05 +00:00
|
|
|
self.assertFalse(mock_client.return_value.write_points.called)
|
|
|
|
mock_client.return_value.write_points.reset_mock()
|
2016-12-02 06:13:55 +00:00
|
|
|
|
2017-04-26 19:14:52 +00:00
|
|
|
def test_event_listener_blacklist_domain(self, mock_client):
|
|
|
|
"""Test the event listener against a blacklist."""
|
|
|
|
self._setup()
|
|
|
|
|
|
|
|
for domain in ('ok', 'another_fake'):
|
|
|
|
state = mock.MagicMock(
|
|
|
|
state=1, domain=domain,
|
|
|
|
entity_id='{}.something'.format(domain),
|
|
|
|
object_id='something', attributes={})
|
|
|
|
event = mock.MagicMock(data={'new_state': state}, time_fired=12345)
|
|
|
|
body = [{
|
|
|
|
'measurement': '{}.something'.format(domain),
|
|
|
|
'tags': {
|
|
|
|
'domain': domain,
|
|
|
|
'entity_id': 'something',
|
|
|
|
},
|
|
|
|
'time': 12345,
|
|
|
|
'fields': {
|
|
|
|
'value': 1,
|
|
|
|
},
|
|
|
|
}]
|
|
|
|
self.handler_method(event)
|
|
|
|
if domain == 'ok':
|
|
|
|
self.assertEqual(
|
|
|
|
mock_client.return_value.write_points.call_count, 1
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
mock_client.return_value.write_points.call_args,
|
|
|
|
mock.call(body)
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
self.assertFalse(mock_client.return_value.write_points.called)
|
|
|
|
mock_client.return_value.write_points.reset_mock()
|
|
|
|
|
|
|
|
def test_event_listener_whitelist(self, mock_client):
|
|
|
|
"""Test the event listener against a whitelist."""
|
|
|
|
config = {
|
|
|
|
'influxdb': {
|
|
|
|
'host': 'host',
|
|
|
|
'username': 'user',
|
|
|
|
'password': 'pass',
|
|
|
|
'include': {
|
|
|
|
'entities': ['fake.included'],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert setup_component(self.hass, influxdb.DOMAIN, config)
|
|
|
|
self.handler_method = self.hass.bus.listen.call_args_list[0][0][1]
|
|
|
|
|
|
|
|
for entity_id in ('included', 'default'):
|
|
|
|
state = mock.MagicMock(
|
|
|
|
state=1, domain='fake', entity_id='fake.{}'.format(entity_id),
|
|
|
|
object_id=entity_id, attributes={})
|
|
|
|
event = mock.MagicMock(data={'new_state': state}, time_fired=12345)
|
|
|
|
body = [{
|
|
|
|
'measurement': 'fake.{}'.format(entity_id),
|
|
|
|
'tags': {
|
|
|
|
'domain': 'fake',
|
|
|
|
'entity_id': entity_id,
|
|
|
|
},
|
|
|
|
'time': 12345,
|
|
|
|
'fields': {
|
|
|
|
'value': 1,
|
|
|
|
},
|
|
|
|
}]
|
|
|
|
self.handler_method(event)
|
|
|
|
if entity_id == 'included':
|
|
|
|
self.assertEqual(
|
|
|
|
mock_client.return_value.write_points.call_count, 1
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
mock_client.return_value.write_points.call_args,
|
|
|
|
mock.call(body)
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
self.assertFalse(mock_client.return_value.write_points.called)
|
|
|
|
mock_client.return_value.write_points.reset_mock()
|
|
|
|
|
|
|
|
def test_event_listener_whitelist_domain(self, mock_client):
|
|
|
|
"""Test the event listener against a whitelist."""
|
|
|
|
config = {
|
|
|
|
'influxdb': {
|
|
|
|
'host': 'host',
|
|
|
|
'username': 'user',
|
|
|
|
'password': 'pass',
|
|
|
|
'include': {
|
|
|
|
'domains': ['fake'],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert setup_component(self.hass, influxdb.DOMAIN, config)
|
|
|
|
self.handler_method = self.hass.bus.listen.call_args_list[0][0][1]
|
|
|
|
|
|
|
|
for domain in ('fake', 'another_fake'):
|
|
|
|
state = mock.MagicMock(
|
|
|
|
state=1, domain=domain,
|
|
|
|
entity_id='{}.something'.format(domain),
|
|
|
|
object_id='something', attributes={})
|
|
|
|
event = mock.MagicMock(data={'new_state': state}, time_fired=12345)
|
|
|
|
body = [{
|
|
|
|
'measurement': '{}.something'.format(domain),
|
|
|
|
'tags': {
|
|
|
|
'domain': domain,
|
|
|
|
'entity_id': 'something',
|
|
|
|
},
|
|
|
|
'time': 12345,
|
|
|
|
'fields': {
|
|
|
|
'value': 1,
|
|
|
|
},
|
|
|
|
}]
|
|
|
|
self.handler_method(event)
|
|
|
|
if domain == 'fake':
|
|
|
|
self.assertEqual(
|
|
|
|
mock_client.return_value.write_points.call_count, 1
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
mock_client.return_value.write_points.call_args,
|
|
|
|
mock.call(body)
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
self.assertFalse(mock_client.return_value.write_points.called)
|
|
|
|
mock_client.return_value.write_points.reset_mock()
|
|
|
|
|
2016-12-02 07:02:58 +00:00
|
|
|
def test_event_listener_invalid_type(self, mock_client):
|
2017-08-03 14:26:01 +00:00
|
|
|
"""Test the event listener when an attribute has an invalid type."""
|
2016-12-02 07:02:58 +00:00
|
|
|
self._setup()
|
|
|
|
|
2017-11-19 22:49:49 +00:00
|
|
|
# map of HA State to valid influxdb [state, value] fields
|
2016-12-02 07:02:58 +00:00
|
|
|
valid = {
|
2017-11-19 22:49:49 +00:00
|
|
|
'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]
|
2016-12-02 07:02:58 +00:00
|
|
|
}
|
|
|
|
for in_, out in valid.items():
|
|
|
|
attrs = {
|
|
|
|
'unit_of_measurement': 'foobars',
|
2017-01-14 17:52:47 +00:00
|
|
|
'longitude': '1.1',
|
|
|
|
'latitude': '2.2',
|
2016-12-02 07:02:58 +00:00
|
|
|
'invalid_attribute': ['value1', 'value2']
|
|
|
|
}
|
|
|
|
state = mock.MagicMock(
|
2017-08-03 14:26:01 +00:00
|
|
|
state=in_, domain='fake', entity_id='fake.entity-id',
|
|
|
|
object_id='entity', attributes=attrs)
|
2016-12-02 07:02:58 +00:00
|
|
|
event = mock.MagicMock(data={'new_state': state}, time_fired=12345)
|
2017-11-19 22:49:49 +00:00
|
|
|
body = [{
|
|
|
|
'measurement': 'foobars',
|
|
|
|
'tags': {
|
|
|
|
'domain': 'fake',
|
|
|
|
'entity_id': 'entity',
|
|
|
|
},
|
|
|
|
'time': 12345,
|
|
|
|
'fields': {
|
|
|
|
'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]
|
2017-01-04 21:36:54 +00:00
|
|
|
|
2016-12-02 07:02:58 +00:00
|
|
|
self.handler_method(event)
|
|
|
|
self.assertEqual(
|
|
|
|
mock_client.return_value.write_points.call_count, 1
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
mock_client.return_value.write_points.call_args,
|
|
|
|
mock.call(body)
|
|
|
|
)
|
2017-01-14 17:52:47 +00:00
|
|
|
mock_client.return_value.write_points.reset_mock()
|
2016-12-02 06:13:55 +00:00
|
|
|
|
2017-01-14 17:52:47 +00:00
|
|
|
def test_event_listener_default_measurement(self, mock_client):
|
|
|
|
"""Test the event listener with a default measurement."""
|
|
|
|
config = {
|
|
|
|
'influxdb': {
|
|
|
|
'host': 'host',
|
|
|
|
'username': 'user',
|
|
|
|
'password': 'pass',
|
|
|
|
'default_measurement': 'state',
|
2017-04-26 19:14:52 +00:00
|
|
|
'exclude': {
|
|
|
|
'entities': ['fake.blacklisted']
|
|
|
|
}
|
2017-01-14 17:52:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
assert setup_component(self.hass, influxdb.DOMAIN, config)
|
|
|
|
self.handler_method = self.hass.bus.listen.call_args_list[0][0][1]
|
|
|
|
|
|
|
|
for entity_id in ('ok', 'blacklisted'):
|
|
|
|
state = mock.MagicMock(
|
|
|
|
state=1, domain='fake', entity_id='fake.{}'.format(entity_id),
|
|
|
|
object_id=entity_id, attributes={})
|
|
|
|
event = mock.MagicMock(data={'new_state': state}, time_fired=12345)
|
|
|
|
body = [{
|
|
|
|
'measurement': 'state',
|
|
|
|
'tags': {
|
|
|
|
'domain': 'fake',
|
|
|
|
'entity_id': entity_id,
|
|
|
|
},
|
|
|
|
'time': 12345,
|
|
|
|
'fields': {
|
|
|
|
'value': 1,
|
|
|
|
},
|
|
|
|
}]
|
|
|
|
self.handler_method(event)
|
|
|
|
if entity_id == 'ok':
|
|
|
|
self.assertEqual(
|
|
|
|
mock_client.return_value.write_points.call_count, 1
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
mock_client.return_value.write_points.call_args,
|
|
|
|
mock.call(body)
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
self.assertFalse(mock_client.return_value.write_points.called)
|
2016-12-02 06:13:55 +00:00
|
|
|
mock_client.return_value.write_points.reset_mock()
|
2017-08-03 14:26:01 +00:00
|
|
|
|
2017-11-19 20:30:47 +00:00
|
|
|
def test_event_listener_unit_of_measurement_field(self, mock_client):
|
|
|
|
"""Test the event listener for unit of measurement field."""
|
|
|
|
config = {
|
|
|
|
'influxdb': {
|
|
|
|
'host': 'host',
|
|
|
|
'username': 'user',
|
|
|
|
'password': 'pass',
|
|
|
|
'override_measurement': 'state',
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert setup_component(self.hass, influxdb.DOMAIN, config)
|
|
|
|
self.handler_method = self.hass.bus.listen.call_args_list[0][0][1]
|
|
|
|
|
|
|
|
attrs = {
|
|
|
|
'unit_of_measurement': 'foobars',
|
|
|
|
}
|
|
|
|
state = mock.MagicMock(
|
|
|
|
state='foo', domain='fake', entity_id='fake.entity-id',
|
|
|
|
object_id='entity', attributes=attrs)
|
|
|
|
event = mock.MagicMock(data={'new_state': state}, time_fired=12345)
|
|
|
|
body = [{
|
|
|
|
'measurement': 'state',
|
|
|
|
'tags': {
|
|
|
|
'domain': 'fake',
|
|
|
|
'entity_id': 'entity',
|
|
|
|
},
|
|
|
|
'time': 12345,
|
|
|
|
'fields': {
|
|
|
|
'state': 'foo',
|
|
|
|
'unit_of_measurement_str': 'foobars',
|
|
|
|
},
|
|
|
|
}]
|
|
|
|
self.handler_method(event)
|
|
|
|
self.assertEqual(
|
|
|
|
mock_client.return_value.write_points.call_count, 1
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
mock_client.return_value.write_points.call_args,
|
|
|
|
mock.call(body)
|
|
|
|
)
|
|
|
|
mock_client.return_value.write_points.reset_mock()
|
|
|
|
|
2017-08-03 14:26:01 +00:00
|
|
|
def test_event_listener_tags_attributes(self, mock_client):
|
|
|
|
"""Test the event listener when some attributes should be tags."""
|
|
|
|
config = {
|
|
|
|
'influxdb': {
|
|
|
|
'host': 'host',
|
|
|
|
'username': 'user',
|
|
|
|
'password': 'pass',
|
|
|
|
'tags_attributes': ['friendly_fake']
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert setup_component(self.hass, influxdb.DOMAIN, config)
|
|
|
|
self.handler_method = self.hass.bus.listen.call_args_list[0][0][1]
|
|
|
|
|
|
|
|
attrs = {
|
|
|
|
'friendly_fake': 'tag_str',
|
|
|
|
'field_fake': 'field_str',
|
|
|
|
}
|
|
|
|
state = mock.MagicMock(
|
|
|
|
state=1, domain='fake',
|
|
|
|
entity_id='fake.something',
|
|
|
|
object_id='something', attributes=attrs)
|
|
|
|
event = mock.MagicMock(data={'new_state': state}, time_fired=12345)
|
|
|
|
body = [{
|
|
|
|
'measurement': 'fake.something',
|
|
|
|
'tags': {
|
|
|
|
'domain': 'fake',
|
|
|
|
'entity_id': 'something',
|
|
|
|
'friendly_fake': 'tag_str'
|
|
|
|
},
|
|
|
|
'time': 12345,
|
|
|
|
'fields': {
|
|
|
|
'value': 1,
|
|
|
|
'field_fake_str': 'field_str'
|
|
|
|
},
|
|
|
|
}]
|
|
|
|
self.handler_method(event)
|
|
|
|
self.assertEqual(
|
|
|
|
mock_client.return_value.write_points.call_count, 1
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
mock_client.return_value.write_points.call_args,
|
|
|
|
mock.call(body)
|
|
|
|
)
|
|
|
|
mock_client.return_value.write_points.reset_mock()
|
|
|
|
|
|
|
|
def test_event_listener_component_override_measurement(self, mock_client):
|
2017-09-23 15:15:46 +00:00
|
|
|
"""Test the event listener with overridden measurements."""
|
2017-08-03 14:26:01 +00:00
|
|
|
config = {
|
|
|
|
'influxdb': {
|
|
|
|
'host': 'host',
|
|
|
|
'username': 'user',
|
|
|
|
'password': 'pass',
|
|
|
|
'component_config': {
|
|
|
|
'sensor.fake_humidity': {
|
|
|
|
'override_measurement': 'humidity'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
'component_config_glob': {
|
|
|
|
'binary_sensor.*motion': {
|
|
|
|
'override_measurement': 'motion'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
'component_config_domain': {
|
|
|
|
'climate': {
|
|
|
|
'override_measurement': 'hvac'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert setup_component(self.hass, influxdb.DOMAIN, config)
|
|
|
|
self.handler_method = self.hass.bus.listen.call_args_list[0][0][1]
|
|
|
|
|
|
|
|
test_components = [
|
|
|
|
{'domain': 'sensor', 'id': 'fake_humidity', 'res': 'humidity'},
|
|
|
|
{'domain': 'binary_sensor', 'id': 'fake_motion', 'res': 'motion'},
|
|
|
|
{'domain': 'climate', 'id': 'fake_thermostat', 'res': 'hvac'},
|
|
|
|
{'domain': 'other', 'id': 'just_fake', 'res': 'other.just_fake'},
|
|
|
|
]
|
|
|
|
for comp in test_components:
|
|
|
|
state = mock.MagicMock(
|
|
|
|
state=1, domain=comp['domain'],
|
|
|
|
entity_id=comp['domain'] + '.' + comp['id'],
|
|
|
|
object_id=comp['id'], attributes={})
|
|
|
|
event = mock.MagicMock(data={'new_state': state}, time_fired=12345)
|
|
|
|
body = [{
|
|
|
|
'measurement': comp['res'],
|
|
|
|
'tags': {
|
|
|
|
'domain': comp['domain'],
|
|
|
|
'entity_id': comp['id']
|
|
|
|
},
|
|
|
|
'time': 12345,
|
|
|
|
'fields': {
|
|
|
|
'value': 1,
|
|
|
|
},
|
|
|
|
}]
|
|
|
|
self.handler_method(event)
|
|
|
|
self.assertEqual(
|
|
|
|
mock_client.return_value.write_points.call_count, 1
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
mock_client.return_value.write_points.call_args,
|
|
|
|
mock.call(body)
|
|
|
|
)
|
|
|
|
mock_client.return_value.write_points.reset_mock()
|