Merge pull request #1209 from kk7ds/abstract-numeric-state
Abstract numeric state
This commit is contained in:
commit
7241762bcc
9 files changed
+280
-59
No files matched your search
@@ -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',
|
||||
|
||||
@@ -0,0 +1,93 @@
|
||||
"""
|
||||
tests.components.test_splunk
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
Tests splunk component.
|
||||
"""
|
||||
import unittest
|
||||
from unittest import mock
|
||||
|
||||
import homeassistant.components.splunk as splunk
|
||||
from homeassistant.const import STATE_ON, STATE_OFF, EVENT_STATE_CHANGED
|
||||
|
||||
|
||||
class TestSplunk(unittest.TestCase):
|
||||
def test_setup_config_full(self):
|
||||
config = {
|
||||
'splunk': {
|
||||
'host': 'host',
|
||||
'port': 123,
|
||||
'token': 'secret',
|
||||
'use_ssl': 'False',
|
||||
}
|
||||
}
|
||||
hass = mock.MagicMock()
|
||||
self.assertTrue(splunk.setup(hass, config))
|
||||
self.assertTrue(hass.bus.listen.called)
|
||||
self.assertEqual(EVENT_STATE_CHANGED,
|
||||
hass.bus.listen.call_args_list[0][0][0])
|
||||
|
||||
def test_setup_config_defaults(self):
|
||||
config = {
|
||||
'splunk': {
|
||||
'host': 'host',
|
||||
'token': 'secret',
|
||||
}
|
||||
}
|
||||
hass = mock.MagicMock()
|
||||
self.assertTrue(splunk.setup(hass, config))
|
||||
self.assertTrue(hass.bus.listen.called)
|
||||
self.assertEqual(EVENT_STATE_CHANGED,
|
||||
hass.bus.listen.call_args_list[0][0][0])
|
||||
|
||||
|
||||
def _setup(self, mock_requests):
|
||||
self.mock_post = mock_requests.post
|
||||
self.mock_request_exception = Exception
|
||||
mock_requests.exceptions.RequestException = self.mock_request_exception
|
||||
config = {
|
||||
'splunk': {
|
||||
'host': 'host',
|
||||
'token': 'secret',
|
||||
}
|
||||
}
|
||||
self.hass = mock.MagicMock()
|
||||
splunk.setup(self.hass, config)
|
||||
self.handler_method = self.hass.bus.listen.call_args_list[0][0][1]
|
||||
|
||||
@mock.patch.object(splunk, 'requests')
|
||||
@mock.patch('json.dumps')
|
||||
def test_event_listener(self, mock_dump, mock_requests):
|
||||
mock_dump.side_effect = lambda x: x
|
||||
self._setup(mock_requests)
|
||||
|
||||
valid = {'1': 1,
|
||||
'1.0': 1.0,
|
||||
STATE_ON: 1,
|
||||
STATE_OFF: 0}
|
||||
for in_, out in valid.items():
|
||||
state = mock.MagicMock(state=in_,
|
||||
domain='fake',
|
||||
object_id='entity',
|
||||
attributes={})
|
||||
event = mock.MagicMock(data={'new_state': state},
|
||||
time_fired=12345)
|
||||
body = [{
|
||||
'domain': 'fake',
|
||||
'entity_id': 'entity',
|
||||
'attributes': {},
|
||||
'time': '12345',
|
||||
'value': out,
|
||||
}]
|
||||
payload = {'host': 'http://host:8088/services/collector/event',
|
||||
'event': body}
|
||||
self.handler_method(event)
|
||||
self.mock_post.assert_called_once_with(
|
||||
payload['host'], data=payload,
|
||||
headers={'Authorization': 'Splunk secret'})
|
||||
self.mock_post.reset_mock()
|
||||
|
||||
for invalid in ('foo', '', object):
|
||||
state = mock.MagicMock(state=invalid)
|
||||
self.handler_method(mock.MagicMock(data={'new_state': state}))
|
||||
self.assertFalse(self.mock_post.called)
|
||||
@@ -0,0 +1,83 @@
|
||||
"""
|
||||
tests.components.test_statsd
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
Tests statsd feeder.
|
||||
"""
|
||||
import unittest
|
||||
from unittest import mock
|
||||
|
||||
import homeassistant.components.statsd as statsd
|
||||
from homeassistant.const import STATE_ON, STATE_OFF, EVENT_STATE_CHANGED
|
||||
|
||||
|
||||
class TestStatsd(unittest.TestCase):
|
||||
@mock.patch('statsd.Connection')
|
||||
@mock.patch('statsd.Gauge')
|
||||
def test_statsd_setup_full(self, mock_gauge, mock_connection):
|
||||
config = {
|
||||
'statsd': {
|
||||
'host': 'host',
|
||||
'port': 123,
|
||||
'sample_rate': 1,
|
||||
'prefix': 'foo',
|
||||
}
|
||||
}
|
||||
hass = mock.MagicMock()
|
||||
self.assertTrue(statsd.setup(hass, config))
|
||||
mock_connection.assert_called_once_with(host='host', port=123,
|
||||
sample_rate=1,
|
||||
disabled=False)
|
||||
mock_gauge.assert_called_once_with('foo',
|
||||
mock_connection.return_value)
|
||||
self.assertTrue(hass.bus.listen.called)
|
||||
self.assertEqual(EVENT_STATE_CHANGED,
|
||||
hass.bus.listen.call_args_list[0][0][0])
|
||||
|
||||
@mock.patch('statsd.Connection')
|
||||
@mock.patch('statsd.Gauge')
|
||||
def test_statsd_setup_defaults(self, mock_gauge, mock_connection):
|
||||
config = {
|
||||
'statsd': {
|
||||
'host': 'host',
|
||||
}
|
||||
}
|
||||
hass = mock.MagicMock()
|
||||
self.assertTrue(statsd.setup(hass, config))
|
||||
mock_connection.assert_called_once_with(
|
||||
host='host',
|
||||
port=statsd.DEFAULT_PORT,
|
||||
sample_rate=statsd.DEFAULT_RATE,
|
||||
disabled=False)
|
||||
mock_gauge.assert_called_once_with(statsd.DEFAULT_PREFIX,
|
||||
mock_connection.return_value)
|
||||
self.assertTrue(hass.bus.listen.called)
|
||||
|
||||
@mock.patch('statsd.Connection')
|
||||
@mock.patch('statsd.Gauge')
|
||||
def test_event_listener(self, mock_gauge, mock_connection):
|
||||
config = {
|
||||
'statsd': {
|
||||
'host': 'host',
|
||||
}
|
||||
}
|
||||
hass = mock.MagicMock()
|
||||
statsd.setup(hass, config)
|
||||
self.assertTrue(hass.bus.listen.called)
|
||||
handler_method = hass.bus.listen.call_args_list[0][0][1]
|
||||
|
||||
valid = {'1': 1,
|
||||
'1.0': 1.0,
|
||||
STATE_ON: 1,
|
||||
STATE_OFF: 0}
|
||||
for in_, out in valid.items():
|
||||
state = mock.MagicMock(state=in_)
|
||||
handler_method(mock.MagicMock(data={'new_state': state}))
|
||||
mock_gauge.return_value.send.assert_called_once_with(
|
||||
state.entity_id, out)
|
||||
mock_gauge.return_value.send.reset_mock()
|
||||
|
||||
for invalid in ('foo', '', object):
|
||||
state = mock.MagicMock(state=invalid)
|
||||
handler_method(mock.MagicMock(data={'new_state': state}))
|
||||
self.assertFalse(mock_gauge.return_value.send.called)
|
||||
Reference in new issue
Block a user