remove the need to have query feature support (#18942)

* remove the need to have query feature support

Some InfluxDB servers don't have /query support feature but are still valid servers for storing data.
Usually those servers are proxies to others timeseries databases.
The change proposes to still validate the configuration but with less requirements on the server side.

* `.query` call is replaced by `.write_points`

* no more query call in the influxdb component. remove test

* reset mock after the setup and before the test

* remove unused import

* reset mock stats after component setup
pull/19033/head
Pierre Gronlier 2018-12-04 09:59:03 +01:00 committed by Paulus Schoutsen
parent 75b855ef93
commit a6511fc0b9
2 changed files with 19 additions and 26 deletions

View File

@ -136,7 +136,7 @@ def setup(hass, config):
try:
influx = InfluxDBClient(**kwargs)
influx.query("SHOW SERIES LIMIT 1;", database=conf[CONF_DB_NAME])
influx.write_points([])
except (exceptions.InfluxDBClientError,
requests.exceptions.ConnectionError) as exc:
_LOGGER.error("Database host is not accessible due to '%s', please "

View File

@ -3,8 +3,6 @@ import datetime
import unittest
from unittest import mock
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, \
@ -48,7 +46,7 @@ class TestInfluxDB(unittest.TestCase):
assert self.hass.bus.listen.called
assert \
EVENT_STATE_CHANGED == self.hass.bus.listen.call_args_list[0][0][0]
assert mock_client.return_value.query.called
assert mock_client.return_value.write_points.call_count == 1
def test_setup_config_defaults(self, mock_client):
"""Test the setup with default configuration."""
@ -82,20 +80,7 @@ class TestInfluxDB(unittest.TestCase):
assert not setup_component(self.hass, influxdb.DOMAIN, config)
def test_setup_query_fail(self, mock_client):
"""Test the setup for query failures."""
config = {
'influxdb': {
'host': 'host',
'username': 'user',
'password': 'pass',
}
}
mock_client.return_value.query.side_effect = \
influx_client.exceptions.InfluxDBClientError('fake')
assert not setup_component(self.hass, influxdb.DOMAIN, config)
def _setup(self, **kwargs):
def _setup(self, mock_client, **kwargs):
"""Set up the client."""
config = {
'influxdb': {
@ -111,10 +96,11 @@ class TestInfluxDB(unittest.TestCase):
config['influxdb'].update(kwargs)
assert setup_component(self.hass, influxdb.DOMAIN, config)
self.handler_method = self.hass.bus.listen.call_args_list[0][0][1]
mock_client.return_value.write_points.reset_mock()
def test_event_listener(self, mock_client):
"""Test the event listener."""
self._setup()
self._setup(mock_client)
# map of HA State to valid influxdb [state, value] fields
valid = {
@ -176,7 +162,7 @@ class TestInfluxDB(unittest.TestCase):
def test_event_listener_no_units(self, mock_client):
"""Test the event listener for missing units."""
self._setup()
self._setup(mock_client)
for unit in (None, ''):
if unit:
@ -207,7 +193,7 @@ class TestInfluxDB(unittest.TestCase):
def test_event_listener_inf(self, mock_client):
"""Test the event listener for missing units."""
self._setup()
self._setup(mock_client)
attrs = {'bignumstring': '9' * 999, 'nonumstring': 'nan'}
state = mock.MagicMock(
@ -234,7 +220,7 @@ class TestInfluxDB(unittest.TestCase):
def test_event_listener_states(self, mock_client):
"""Test the event listener against ignored states."""
self._setup()
self._setup(mock_client)
for state_state in (1, 'unknown', '', 'unavailable'):
state = mock.MagicMock(
@ -264,7 +250,7 @@ class TestInfluxDB(unittest.TestCase):
def test_event_listener_blacklist(self, mock_client):
"""Test the event listener against a blacklist."""
self._setup()
self._setup(mock_client)
for entity_id in ('ok', 'blacklisted'):
state = mock.MagicMock(
@ -294,7 +280,7 @@ class TestInfluxDB(unittest.TestCase):
def test_event_listener_blacklist_domain(self, mock_client):
"""Test the event listener against a blacklist."""
self._setup()
self._setup(mock_client)
for domain in ('ok', 'another_fake'):
state = mock.MagicMock(
@ -337,6 +323,7 @@ class TestInfluxDB(unittest.TestCase):
}
assert setup_component(self.hass, influxdb.DOMAIN, config)
self.handler_method = self.hass.bus.listen.call_args_list[0][0][1]
mock_client.return_value.write_points.reset_mock()
for entity_id in ('included', 'default'):
state = mock.MagicMock(
@ -378,6 +365,7 @@ class TestInfluxDB(unittest.TestCase):
}
assert setup_component(self.hass, influxdb.DOMAIN, config)
self.handler_method = self.hass.bus.listen.call_args_list[0][0][1]
mock_client.return_value.write_points.reset_mock()
for domain in ('fake', 'another_fake'):
state = mock.MagicMock(
@ -408,7 +396,7 @@ class TestInfluxDB(unittest.TestCase):
def test_event_listener_invalid_type(self, mock_client):
"""Test the event listener when an attribute has an invalid type."""
self._setup()
self._setup(mock_client)
# map of HA State to valid influxdb [state, value] fields
valid = {
@ -470,6 +458,7 @@ class TestInfluxDB(unittest.TestCase):
}
assert setup_component(self.hass, influxdb.DOMAIN, config)
self.handler_method = self.hass.bus.listen.call_args_list[0][0][1]
mock_client.return_value.write_points.reset_mock()
for entity_id in ('ok', 'blacklisted'):
state = mock.MagicMock(
@ -509,6 +498,7 @@ class TestInfluxDB(unittest.TestCase):
}
assert setup_component(self.hass, influxdb.DOMAIN, config)
self.handler_method = self.hass.bus.listen.call_args_list[0][0][1]
mock_client.return_value.write_points.reset_mock()
attrs = {
'unit_of_measurement': 'foobars',
@ -548,6 +538,7 @@ class TestInfluxDB(unittest.TestCase):
}
assert setup_component(self.hass, influxdb.DOMAIN, config)
self.handler_method = self.hass.bus.listen.call_args_list[0][0][1]
mock_client.return_value.write_points.reset_mock()
attrs = {
'friendly_fake': 'tag_str',
@ -604,6 +595,7 @@ class TestInfluxDB(unittest.TestCase):
}
assert setup_component(self.hass, influxdb.DOMAIN, config)
self.handler_method = self.hass.bus.listen.call_args_list[0][0][1]
mock_client.return_value.write_points.reset_mock()
test_components = [
{'domain': 'sensor', 'id': 'fake_humidity', 'res': 'humidity'},
@ -647,6 +639,7 @@ class TestInfluxDB(unittest.TestCase):
}
assert setup_component(self.hass, influxdb.DOMAIN, config)
self.handler_method = self.hass.bus.listen.call_args_list[0][0][1]
mock_client.return_value.write_points.reset_mock()
state = mock.MagicMock(
state=1, domain='fake', entity_id='entity.id', object_id='entity',
@ -674,7 +667,7 @@ class TestInfluxDB(unittest.TestCase):
def test_queue_backlog_full(self, mock_client):
"""Test the event listener to drop old events."""
self._setup()
self._setup(mock_client)
state = mock.MagicMock(
state=1, domain='fake', entity_id='entity.id', object_id='entity',