Allow easier customization of whole domain, entity lists, globs. (#5215)

This commit is contained in:
andrey-git authored and Johann Kellerman committed 2017-01-22 21:19:50 +02:00
1 parent ab19577322
commit addc2c4340
8 files changed
+285 -48

No files matched your search

+28
View File
@@ -165,6 +165,25 @@ def test_entity_ids():
]
def test_ensure_list_csv():
"""Test ensure_list_csv."""
schema = vol.Schema(cv.ensure_list_csv)
options = (
None,
12,
[],
['string'],
'string1,string2'
)
for value in options:
schema(value)
assert schema('string1, string2 ') == [
'string1', 'string2'
]
def test_event_schema():
"""Test event_schema validation."""
options = (
@@ -429,6 +448,15 @@ def test_has_at_least_one_key():
schema(value)
def test_ordered_dict_only_dict():
"""Test ordered_dict validator."""
schema = vol.Schema(cv.ordered_dict(cv.match_all, cv.match_all))
for value in (None, [], 100, 'hello'):
with pytest.raises(vol.MultipleInvalid):
schema(value)
def test_ordered_dict_order():
"""Test ordered_dict validator."""
schema = vol.Schema(cv.ordered_dict(int, cv.string))
+87
View File
@@ -0,0 +1,87 @@
"""Test the customize helper."""
import homeassistant.helpers.customize as customize
class MockHass(object):
"""Mock object for HassAssistant."""
data = {}
class TestHelpersCustomize(object):
"""Test homeassistant.helpers.customize module."""
def setup_method(self, method):
"""Setup things to be run when tests are started."""
self.entity_id = 'test.test'
self.hass = MockHass()
def _get_overrides(self, overrides):
customize.set_customize(self.hass, overrides)
return customize.get_overrides(self.hass, self.entity_id)
def test_override_single_value(self):
"""Test entity customization through configuration."""
result = self._get_overrides([
{'entity_id': [self.entity_id], 'key': 'value'}])
assert result == {'key': 'value'}
def test_override_multiple_values(self):
"""Test entity customization through configuration."""
result = self._get_overrides([
{'entity_id': [self.entity_id], 'key1': 'value1'},
{'entity_id': [self.entity_id], 'key2': 'value2'}])
assert result == {'key1': 'value1', 'key2': 'value2'}
def test_override_same_value(self):
"""Test entity customization through configuration."""
result = self._get_overrides([
{'entity_id': [self.entity_id], 'key': 'value1'},
{'entity_id': [self.entity_id], 'key': 'value2'}])
assert result == {'key': 'value2'}
def test_override_by_domain(self):
"""Test entity customization through configuration."""
result = self._get_overrides([
{'entity_id': ['test'], 'key': 'value'}])
assert result == {'key': 'value'}
def test_override_by_glob(self):
"""Test entity customization through configuration."""
result = self._get_overrides([
{'entity_id': ['test.?e*'], 'key': 'value'}])
assert result == {'key': 'value'}
def test_override_exact_over_glob_over_domain(self):
"""Test entity customization through configuration."""
result = self._get_overrides([
{'entity_id': ['test.test'], 'key1': 'valueExact'},
{'entity_id': ['test.tes?'],
'key1': 'valueGlob',
'key2': 'valueGlob'},
{'entity_id': ['test'],
'key1': 'valueDomain',
'key2': 'valueDomain',
'key3': 'valueDomain'}])
assert result == {
'key1': 'valueExact',
'key2': 'valueGlob',
'key3': 'valueDomain'}
def test_override_deep_dict(self):
"""Test we can overwrite hidden property to True."""
result = self._get_overrides(
[{'entity_id': [self.entity_id],
'test': {'key1': 'value1', 'key2': 'value2'}},
{'entity_id': [self.entity_id],
'test': {'key3': 'value3', 'key2': 'value22'}}])
assert result['test'] == {
'key1': 'value1',
'key2': 'value22',
'key3': 'value3'}
+4 -2
View File
@@ -6,6 +6,7 @@ from unittest.mock import MagicMock
import pytest
import homeassistant.helpers.entity as entity
from homeassistant.helpers.customize import set_customize
from homeassistant.const import ATTR_HIDDEN
from tests.common import get_test_home_assistant
@@ -78,7 +79,6 @@ class TestHelpersEntity(object):
def teardown_method(self, method):
"""Stop everything that was started."""
entity.set_customize({})
self.hass.stop()
def test_default_hidden_not_in_attributes(self):
@@ -88,7 +88,9 @@ class TestHelpersEntity(object):
def test_overwriting_hidden_property_to_true(self):
"""Test we can overwrite hidden property to True."""
entity.set_customize({self.entity.entity_id: {ATTR_HIDDEN: True}})
set_customize(
self.hass,
[{'entity_id': [self.entity.entity_id], ATTR_HIDDEN: True}])
self.entity.update_ha_state()
state = self.hass.states.get(self.entity.entity_id)
+52 -15
View File
@@ -170,16 +170,17 @@ class TestConfig(unittest.TestCase):
os.path.join(CONFIG_DIR, 'non_existing_dir/'), False))
self.assertTrue(mock_print.called)
# pylint: disable=no-self-use
def test_core_config_schema(self):
"""Test core config schema."""
for value in (
{CONF_UNIT_SYSTEM: 'K'},
{'time_zone': 'non-exist'},
{'latitude': '91'},
{'longitude': -181},
{'customize': 'bla'},
{'customize': {'invalid_entity_id': {}}},
{'customize': {'light.sensor': 100}},
{CONF_UNIT_SYSTEM: 'K'},
{'time_zone': 'non-exist'},
{'latitude': '91'},
{'longitude': -181},
{'customize': 'bla'},
{'customize': {'light.sensor': 100}},
{'customize': {'entity_id': []}},
):
with pytest.raises(MultipleInvalid):
config_util.CORE_CONFIG_SCHEMA(value)
@@ -196,13 +197,7 @@ class TestConfig(unittest.TestCase):
},
})
def test_entity_customization(self):
"""Test entity customization through configuration."""
config = {CONF_LATITUDE: 50,
CONF_LONGITUDE: 50,
CONF_NAME: 'Test',
CONF_CUSTOMIZE: {'test.test': {'hidden': True}}}
def _compute_state(self, config):
run_coroutine_threadsafe(
config_util.async_process_ha_core_config(self.hass, config),
self.hass.loop).result()
@@ -214,10 +209,50 @@ class TestConfig(unittest.TestCase):
self.hass.block_till_done()
state = self.hass.states.get('test.test')
return self.hass.states.get('test.test')
def test_entity_customization_false(self):
"""Test entity customization through configuration."""
config = {CONF_LATITUDE: 50,
CONF_LONGITUDE: 50,
CONF_NAME: 'Test',
CONF_CUSTOMIZE: {
'test.test': {'hidden': False}}}
state = self._compute_state(config)
assert 'hidden' not in state.attributes
def test_entity_customization(self):
"""Test entity customization through configuration."""
config = {CONF_LATITUDE: 50,
CONF_LONGITUDE: 50,
CONF_NAME: 'Test',
CONF_CUSTOMIZE: {'test.test': {'hidden': True}}}
state = self._compute_state(config)
assert state.attributes['hidden']
def test_entity_customization_comma_separated(self):
"""Test entity customization through configuration."""
config = {CONF_LATITUDE: 50,
CONF_LONGITUDE: 50,
CONF_NAME: 'Test',
CONF_CUSTOMIZE: [
{'entity_id': 'test.not_test,test,test.not_t*',
'key1': 'value1'},
{'entity_id': 'test.test,not_test,test.not_t*',
'key2': 'value2'},
{'entity_id': 'test.not_test,not_test,test.t*',
'key3': 'value3'}]}
state = self._compute_state(config)
assert state.attributes['key1'] == 'value1'
assert state.attributes['key2'] == 'value2'
assert state.attributes['key3'] == 'value3'
@mock.patch('homeassistant.config.shutil')
@mock.patch('homeassistant.config.os')
def test_remove_lib_on_upgrade(self, mock_os, mock_shutil):
@@ -229,6 +264,7 @@ class TestConfig(unittest.TestCase):
mock_open = mock.mock_open()
with mock.patch('homeassistant.config.open', mock_open, create=True):
opened_file = mock_open.return_value
# pylint: disable=no-member
opened_file.readline.return_value = ha_version
self.hass.config.path = mock.Mock()
@@ -258,6 +294,7 @@ class TestConfig(unittest.TestCase):
mock_open = mock.mock_open()
with mock.patch('homeassistant.config.open', mock_open, create=True):
opened_file = mock_open.return_value
# pylint: disable=no-member
opened_file.readline.return_value = ha_version
self.hass.config.path = mock.Mock()