This commit is contained in:
Paulus Schoutsen committed 2019-07-31 12:25:30 -07:00
1 parent da05dfe708
commit 4de97abc3a
2676 files changed
+163166 -140084

No files matched your search

+35 -35
View File
@@ -5,8 +5,7 @@ from unittest.mock import call, mock_open, patch
from homeassistant.setup import setup_component
import homeassistant.components.notify as notify
from homeassistant.components.notify import (
ATTR_TITLE_DEFAULT)
from homeassistant.components.notify import ATTR_TITLE_DEFAULT
import homeassistant.util.dt as dt_util
from tests.common import assert_setup_component, get_test_home_assistant
@@ -25,60 +24,61 @@ class TestNotifyFile(unittest.TestCase):
def test_bad_config(self):
"""Test set up the platform with bad/missing config."""
config = {
notify.DOMAIN: {
'name': 'test',
'platform': 'file',
},
}
config = {notify.DOMAIN: {"name": "test", "platform": "file"}}
with assert_setup_component(0) as handle_config:
assert setup_component(self.hass, notify.DOMAIN, config)
assert not handle_config[notify.DOMAIN]
def _test_notify_file(self, timestamp):
"""Test the notify file output."""
filename = 'mock_file'
message = 'one, two, testing, testing'
filename = "mock_file"
message = "one, two, testing, testing"
with assert_setup_component(1) as handle_config:
assert setup_component(self.hass, notify.DOMAIN, {
'notify': {
'name': 'test',
'platform': 'file',
'filename': filename,
'timestamp': timestamp,
}
})
assert setup_component(
self.hass,
notify.DOMAIN,
{
"notify": {
"name": "test",
"platform": "file",
"filename": filename,
"timestamp": timestamp,
}
},
)
assert handle_config[notify.DOMAIN]
m_open = mock_open()
with patch(
'homeassistant.components.file.notify.open',
m_open, create=True
), patch('homeassistant.components.file.notify.os.stat') as mock_st, \
patch('homeassistant.util.dt.utcnow',
return_value=dt_util.utcnow()):
"homeassistant.components.file.notify.open", m_open, create=True
), patch("homeassistant.components.file.notify.os.stat") as mock_st, patch(
"homeassistant.util.dt.utcnow", return_value=dt_util.utcnow()
):
mock_st.return_value.st_size = 0
title = '{} notifications (Log started: {})\n{}\n'.format(
ATTR_TITLE_DEFAULT,
dt_util.utcnow().isoformat(),
'-' * 80)
title = "{} notifications (Log started: {})\n{}\n".format(
ATTR_TITLE_DEFAULT, dt_util.utcnow().isoformat(), "-" * 80
)
self.hass.services.call('notify', 'test', {'message': message},
blocking=True)
self.hass.services.call(
"notify", "test", {"message": message}, blocking=True
)
full_filename = os.path.join(self.hass.config.path(), filename)
assert m_open.call_count == 1
assert m_open.call_args == call(full_filename, 'a')
assert m_open.call_args == call(full_filename, "a")
assert m_open.return_value.write.call_count == 2
if not timestamp:
assert m_open.return_value.write.call_args_list == \
[call(title), call('{}\n'.format(message))]
assert m_open.return_value.write.call_args_list == [
call(title),
call("{}\n".format(message)),
]
else:
assert m_open.return_value.write.call_args_list == \
[call(title), call('{} {}\n'.format(
dt_util.utcnow().isoformat(), message))]
assert m_open.return_value.write.call_args_list == [
call(title),
call("{} {}\n".format(dt_util.utcnow().isoformat(), message)),
]
def test_notify_file(self):
"""Test the notify file output without timestamp."""
+30 -39
View File
@@ -26,69 +26,60 @@ class TestFileSensor(unittest.TestCase):
"""Stop everything that was started."""
self.hass.stop()
@patch('os.path.isfile', Mock(return_value=True))
@patch('os.access', Mock(return_value=True))
@patch("os.path.isfile", Mock(return_value=True))
@patch("os.access", Mock(return_value=True))
def test_file_value(self):
"""Test the File sensor."""
config = {
'sensor': {
'platform': 'file',
'name': 'file1',
'file_path': 'mock.file1',
}
"sensor": {"platform": "file", "name": "file1", "file_path": "mock.file1"}
}
m_open = MockOpen(read_data='43\n45\n21')
with patch('homeassistant.components.file.sensor.open', m_open,
create=True):
assert setup_component(self.hass, 'sensor', config)
m_open = MockOpen(read_data="43\n45\n21")
with patch("homeassistant.components.file.sensor.open", m_open, create=True):
assert setup_component(self.hass, "sensor", config)
self.hass.block_till_done()
state = self.hass.states.get('sensor.file1')
assert state.state == '21'
state = self.hass.states.get("sensor.file1")
assert state.state == "21"
@patch('os.path.isfile', Mock(return_value=True))
@patch('os.access', Mock(return_value=True))
@patch("os.path.isfile", Mock(return_value=True))
@patch("os.access", Mock(return_value=True))
def test_file_value_template(self):
"""Test the File sensor with JSON entries."""
config = {
'sensor': {
'platform': 'file',
'name': 'file2',
'file_path': 'mock.file2',
'value_template': '{{ value_json.temperature }}',
"sensor": {
"platform": "file",
"name": "file2",
"file_path": "mock.file2",
"value_template": "{{ value_json.temperature }}",
}
}
data = '{"temperature": 29, "humidity": 31}\n' \
'{"temperature": 26, "humidity": 36}'
data = (
'{"temperature": 29, "humidity": 31}\n'
'{"temperature": 26, "humidity": 36}'
)
m_open = MockOpen(read_data=data)
with patch('homeassistant.components.file.sensor.open', m_open,
create=True):
assert setup_component(self.hass, 'sensor', config)
with patch("homeassistant.components.file.sensor.open", m_open, create=True):
assert setup_component(self.hass, "sensor", config)
self.hass.block_till_done()
state = self.hass.states.get('sensor.file2')
assert state.state == '26'
state = self.hass.states.get("sensor.file2")
assert state.state == "26"
@patch('os.path.isfile', Mock(return_value=True))
@patch('os.access', Mock(return_value=True))
@patch("os.path.isfile", Mock(return_value=True))
@patch("os.access", Mock(return_value=True))
def test_file_empty(self):
"""Test the File sensor with an empty file."""
config = {
'sensor': {
'platform': 'file',
'name': 'file3',
'file_path': 'mock.file',
}
"sensor": {"platform": "file", "name": "file3", "file_path": "mock.file"}
}
m_open = MockOpen(read_data='')
with patch('homeassistant.components.file.sensor.open', m_open,
create=True):
assert setup_component(self.hass, 'sensor', config)
m_open = MockOpen(read_data="")
with patch("homeassistant.components.file.sensor.open", m_open, create=True):
assert setup_component(self.hass, "sensor", config)
self.hass.block_till_done()
state = self.hass.states.get('sensor.file3')
state = self.hass.states.get("sensor.file3")
assert state.state == STATE_UNKNOWN