mirror of
https://github.com/home-assistant/core.git
synced 2025-07-22 20:57:21 +00:00
Fix field type conflict in influxdb
* Add STATE_UNAVAILABLE to states that are ignored when writing to the database. This will avoid a field type error for string if the field already contains a different type, eg integer. * Add test for ignored states for influxdb. * Clean up influxdb tests.
This commit is contained in:
parent
37a28c799f
commit
ca2d969198
@ -7,7 +7,8 @@ https://home-assistant.io/components/influxdb/
|
|||||||
import logging
|
import logging
|
||||||
|
|
||||||
import homeassistant.util as util
|
import homeassistant.util as util
|
||||||
from homeassistant.const import EVENT_STATE_CHANGED, STATE_UNKNOWN
|
from homeassistant.const import (EVENT_STATE_CHANGED, STATE_UNAVAILABLE,
|
||||||
|
STATE_UNKNOWN)
|
||||||
from homeassistant.helpers import state as state_helper
|
from homeassistant.helpers import state as state_helper
|
||||||
from homeassistant.helpers import validate_config
|
from homeassistant.helpers import validate_config
|
||||||
|
|
||||||
@ -70,8 +71,9 @@ def setup(hass, config):
|
|||||||
def influx_event_listener(event):
|
def influx_event_listener(event):
|
||||||
"""Listen for new messages on the bus and sends them to Influx."""
|
"""Listen for new messages on the bus and sends them to Influx."""
|
||||||
state = event.data.get('new_state')
|
state = event.data.get('new_state')
|
||||||
if state is None or state.state in (STATE_UNKNOWN, '') \
|
if state is None or state.state in (
|
||||||
or state.entity_id in blacklist:
|
STATE_UNKNOWN, '', STATE_UNAVAILABLE) or \
|
||||||
|
state.entity_id in blacklist:
|
||||||
return
|
return
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
@ -6,13 +6,18 @@ from unittest import mock
|
|||||||
import influxdb as influx_client
|
import influxdb as influx_client
|
||||||
|
|
||||||
import homeassistant.components.influxdb as influxdb
|
import homeassistant.components.influxdb as influxdb
|
||||||
from homeassistant.const import STATE_ON, STATE_OFF, EVENT_STATE_CHANGED
|
from homeassistant.const import EVENT_STATE_CHANGED, STATE_OFF, STATE_ON
|
||||||
|
|
||||||
|
|
||||||
|
@mock.patch('influxdb.InfluxDBClient')
|
||||||
class TestInfluxDB(unittest.TestCase):
|
class TestInfluxDB(unittest.TestCase):
|
||||||
"""Test the InfluxDB component."""
|
"""Test the InfluxDB component."""
|
||||||
|
|
||||||
@mock.patch('influxdb.InfluxDBClient')
|
def setUp(self):
|
||||||
|
"""Setup things to be run when tests are started."""
|
||||||
|
self.hass = mock.MagicMock()
|
||||||
|
self.handler_method = None
|
||||||
|
|
||||||
def test_setup_config_full(self, mock_client):
|
def test_setup_config_full(self, mock_client):
|
||||||
"""Test the setup with full configuration."""
|
"""Test the setup with full configuration."""
|
||||||
config = {
|
config = {
|
||||||
@ -26,14 +31,12 @@ class TestInfluxDB(unittest.TestCase):
|
|||||||
'verify_ssl': 'False',
|
'verify_ssl': 'False',
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
hass = mock.MagicMock()
|
self.assertTrue(influxdb.setup(self.hass, config))
|
||||||
self.assertTrue(influxdb.setup(hass, config))
|
self.assertTrue(self.hass.bus.listen.called)
|
||||||
self.assertTrue(hass.bus.listen.called)
|
|
||||||
self.assertEqual(EVENT_STATE_CHANGED,
|
self.assertEqual(EVENT_STATE_CHANGED,
|
||||||
hass.bus.listen.call_args_list[0][0][0])
|
self.hass.bus.listen.call_args_list[0][0][0])
|
||||||
self.assertTrue(mock_client.return_value.query.called)
|
self.assertTrue(mock_client.return_value.query.called)
|
||||||
|
|
||||||
@mock.patch('influxdb.InfluxDBClient')
|
|
||||||
def test_setup_config_defaults(self, mock_client):
|
def test_setup_config_defaults(self, mock_client):
|
||||||
"""Test the setup with default configuration."""
|
"""Test the setup with default configuration."""
|
||||||
config = {
|
config = {
|
||||||
@ -43,13 +46,11 @@ class TestInfluxDB(unittest.TestCase):
|
|||||||
'password': 'pass',
|
'password': 'pass',
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
hass = mock.MagicMock()
|
self.assertTrue(influxdb.setup(self.hass, config))
|
||||||
self.assertTrue(influxdb.setup(hass, config))
|
self.assertTrue(self.hass.bus.listen.called)
|
||||||
self.assertTrue(hass.bus.listen.called)
|
|
||||||
self.assertEqual(EVENT_STATE_CHANGED,
|
self.assertEqual(EVENT_STATE_CHANGED,
|
||||||
hass.bus.listen.call_args_list[0][0][0])
|
self.hass.bus.listen.call_args_list[0][0][0])
|
||||||
|
|
||||||
@mock.patch('influxdb.InfluxDBClient')
|
|
||||||
def test_setup_missing_keys(self, mock_client):
|
def test_setup_missing_keys(self, mock_client):
|
||||||
"""Test the setup with missing keys."""
|
"""Test the setup with missing keys."""
|
||||||
config = {
|
config = {
|
||||||
@ -59,13 +60,11 @@ class TestInfluxDB(unittest.TestCase):
|
|||||||
'password': 'pass',
|
'password': 'pass',
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
hass = mock.MagicMock()
|
|
||||||
for missing in config['influxdb'].keys():
|
for missing in config['influxdb'].keys():
|
||||||
config_copy = copy.deepcopy(config)
|
config_copy = copy.deepcopy(config)
|
||||||
del config_copy['influxdb'][missing]
|
del config_copy['influxdb'][missing]
|
||||||
self.assertFalse(influxdb.setup(hass, config_copy))
|
self.assertFalse(influxdb.setup(self.hass, config_copy))
|
||||||
|
|
||||||
@mock.patch('influxdb.InfluxDBClient')
|
|
||||||
def test_setup_query_fail(self, mock_client):
|
def test_setup_query_fail(self, mock_client):
|
||||||
"""Test the setup for query failures."""
|
"""Test the setup for query failures."""
|
||||||
config = {
|
config = {
|
||||||
@ -75,14 +74,12 @@ class TestInfluxDB(unittest.TestCase):
|
|||||||
'password': 'pass',
|
'password': 'pass',
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
hass = mock.MagicMock()
|
|
||||||
mock_client.return_value.query.side_effect = \
|
mock_client.return_value.query.side_effect = \
|
||||||
influx_client.exceptions.InfluxDBClientError('fake')
|
influx_client.exceptions.InfluxDBClientError('fake')
|
||||||
self.assertFalse(influxdb.setup(hass, config))
|
self.assertFalse(influxdb.setup(self.hass, config))
|
||||||
|
|
||||||
def _setup(self, mock_influx):
|
def _setup(self):
|
||||||
"""Setup the client."""
|
"""Setup the client."""
|
||||||
self.mock_client = mock_influx.return_value
|
|
||||||
config = {
|
config = {
|
||||||
'influxdb': {
|
'influxdb': {
|
||||||
'host': 'host',
|
'host': 'host',
|
||||||
@ -91,14 +88,12 @@ class TestInfluxDB(unittest.TestCase):
|
|||||||
'blacklist': ['fake.blacklisted']
|
'blacklist': ['fake.blacklisted']
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
self.hass = mock.MagicMock()
|
|
||||||
influxdb.setup(self.hass, config)
|
influxdb.setup(self.hass, config)
|
||||||
self.handler_method = self.hass.bus.listen.call_args_list[0][0][1]
|
self.handler_method = self.hass.bus.listen.call_args_list[0][0][1]
|
||||||
|
|
||||||
@mock.patch('influxdb.InfluxDBClient')
|
def test_event_listener(self, mock_client):
|
||||||
def test_event_listener(self, mock_influx):
|
|
||||||
"""Test the event listener."""
|
"""Test the event listener."""
|
||||||
self._setup(mock_influx)
|
self._setup()
|
||||||
|
|
||||||
valid = {'1': 1,
|
valid = {'1': 1,
|
||||||
'1.0': 1.0,
|
'1.0': 1.0,
|
||||||
@ -125,13 +120,12 @@ class TestInfluxDB(unittest.TestCase):
|
|||||||
},
|
},
|
||||||
}]
|
}]
|
||||||
self.handler_method(event)
|
self.handler_method(event)
|
||||||
self.mock_client.write_points.assert_called_once_with(body)
|
mock_client.return_value.write_points.assert_called_once_with(body)
|
||||||
self.mock_client.write_points.reset_mock()
|
mock_client.return_value.write_points.reset_mock()
|
||||||
|
|
||||||
@mock.patch('influxdb.InfluxDBClient')
|
def test_event_listener_no_units(self, mock_client):
|
||||||
def test_event_listener_no_units(self, mock_influx):
|
|
||||||
"""Test the event listener for missing units."""
|
"""Test the event listener for missing units."""
|
||||||
self._setup(mock_influx)
|
self._setup()
|
||||||
|
|
||||||
for unit in (None, ''):
|
for unit in (None, ''):
|
||||||
if unit:
|
if unit:
|
||||||
@ -157,13 +151,12 @@ class TestInfluxDB(unittest.TestCase):
|
|||||||
},
|
},
|
||||||
}]
|
}]
|
||||||
self.handler_method(event)
|
self.handler_method(event)
|
||||||
self.mock_client.write_points.assert_called_once_with(body)
|
mock_client.return_value.write_points.assert_called_once_with(body)
|
||||||
self.mock_client.write_points.reset_mock()
|
mock_client.return_value.write_points.reset_mock()
|
||||||
|
|
||||||
@mock.patch('influxdb.InfluxDBClient')
|
def test_event_listener_fail_write(self, mock_client):
|
||||||
def test_event_listener_fail_write(self, mock_influx):
|
|
||||||
"""Test the event listener for write failures."""
|
"""Test the event listener for write failures."""
|
||||||
self._setup(mock_influx)
|
self._setup()
|
||||||
|
|
||||||
state = mock.MagicMock(state=1,
|
state = mock.MagicMock(state=1,
|
||||||
domain='fake',
|
domain='fake',
|
||||||
@ -172,14 +165,44 @@ class TestInfluxDB(unittest.TestCase):
|
|||||||
attributes={})
|
attributes={})
|
||||||
event = mock.MagicMock(data={'new_state': state},
|
event = mock.MagicMock(data={'new_state': state},
|
||||||
time_fired=12345)
|
time_fired=12345)
|
||||||
self.mock_client.write_points.side_effect = \
|
mock_client.return_value.write_points.side_effect = \
|
||||||
influx_client.exceptions.InfluxDBClientError('foo')
|
influx_client.exceptions.InfluxDBClientError('foo')
|
||||||
self.handler_method(event)
|
self.handler_method(event)
|
||||||
|
|
||||||
@mock.patch('influxdb.InfluxDBClient')
|
def test_event_listener_states(self, mock_client):
|
||||||
def test_event_listener_blacklist(self, mock_influx):
|
"""Test the event listener against ignored states."""
|
||||||
|
self._setup()
|
||||||
|
|
||||||
|
for state_state in (1, 'unknown', '', 'unavailable'):
|
||||||
|
state = mock.MagicMock(state=state_state,
|
||||||
|
domain='fake',
|
||||||
|
entity_id='entity-id',
|
||||||
|
object_id='entity',
|
||||||
|
attributes={})
|
||||||
|
event = mock.MagicMock(data={'new_state': state},
|
||||||
|
time_fired=12345)
|
||||||
|
body = [{
|
||||||
|
'measurement': 'entity-id',
|
||||||
|
'tags': {
|
||||||
|
'domain': 'fake',
|
||||||
|
'entity_id': 'entity',
|
||||||
|
},
|
||||||
|
'time': 12345,
|
||||||
|
'fields': {
|
||||||
|
'value': 1,
|
||||||
|
},
|
||||||
|
}]
|
||||||
|
self.handler_method(event)
|
||||||
|
if state_state == 1:
|
||||||
|
mock_client.return_value.write_points.assert_called_once_with(
|
||||||
|
body)
|
||||||
|
else:
|
||||||
|
self.assertFalse(mock_client.return_value.write_points.called)
|
||||||
|
mock_client.return_value.write_points.reset_mock()
|
||||||
|
|
||||||
|
def test_event_listener_blacklist(self, mock_client):
|
||||||
"""Test the event listener against a blacklist."""
|
"""Test the event listener against a blacklist."""
|
||||||
self._setup(mock_influx)
|
self._setup()
|
||||||
|
|
||||||
for entity_id in ('ok', 'blacklisted'):
|
for entity_id in ('ok', 'blacklisted'):
|
||||||
state = mock.MagicMock(state=1,
|
state = mock.MagicMock(state=1,
|
||||||
@ -202,7 +225,8 @@ class TestInfluxDB(unittest.TestCase):
|
|||||||
}]
|
}]
|
||||||
self.handler_method(event)
|
self.handler_method(event)
|
||||||
if entity_id == 'ok':
|
if entity_id == 'ok':
|
||||||
self.mock_client.write_points.assert_called_once_with(body)
|
mock_client.return_value.write_points.assert_called_once_with(
|
||||||
|
body)
|
||||||
else:
|
else:
|
||||||
self.assertFalse(self.mock_client.write_points.called)
|
self.assertFalse(mock_client.return_value.write_points.called)
|
||||||
self.mock_client.write_points.reset_mock()
|
mock_client.return_value.write_points.reset_mock()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user