core/tests/helpers/test_entity_values.py

68 lines
1.9 KiB
Python
Raw Normal View History

"""Test the entity values helper."""
from collections import OrderedDict
from homeassistant.helpers.entity_values import EntityValues as EV
2019-07-31 19:25:30 +00:00
ent = "test.test"
def test_override_single_value():
"""Test values with exact match."""
2019-07-31 19:25:30 +00:00
store = EV({ent: {"key": "value"}})
assert store.get(ent) == {"key": "value"}
assert len(store._cache) == 1
2019-07-31 19:25:30 +00:00
assert store.get(ent) == {"key": "value"}
assert len(store._cache) == 1
def test_override_by_domain():
"""Test values with domain match."""
2019-07-31 19:25:30 +00:00
store = EV(domain={"test": {"key": "value"}})
assert store.get(ent) == {"key": "value"}
def test_override_by_glob():
"""Test values with glob match."""
2019-07-31 19:25:30 +00:00
store = EV(glob={"test.?e*": {"key": "value"}})
assert store.get(ent) == {"key": "value"}
def test_glob_overrules_domain():
"""Test domain overrules glob match."""
2019-07-31 19:25:30 +00:00
store = EV(domain={"test": {"key": "domain"}}, glob={"test.?e*": {"key": "glob"}})
assert store.get(ent) == {"key": "glob"}
def test_exact_overrules_domain():
"""Test exact overrules domain match."""
store = EV(
2019-07-31 19:25:30 +00:00
exact={"test.test": {"key": "exact"}},
domain={"test": {"key": "domain"}},
glob={"test.?e*": {"key": "glob"}},
)
assert store.get(ent) == {"key": "exact"}
def test_merging_values():
"""Test merging glob, domain and exact configs."""
store = EV(
2019-07-31 19:25:30 +00:00
exact={"test.test": {"exact_key": "exact"}},
domain={"test": {"domain_key": "domain"}},
glob={"test.?e*": {"glob_key": "glob"}},
)
assert store.get(ent) == {
2019-07-31 19:25:30 +00:00
"exact_key": "exact",
"domain_key": "domain",
"glob_key": "glob",
}
def test_glob_order():
"""Test merging glob, domain and exact configs."""
glob = OrderedDict()
2019-07-31 19:25:30 +00:00
glob["test.*est"] = {"value": "first"}
glob["test.*"] = {"value": "second"}
store = EV(glob=glob)
2019-07-31 19:25:30 +00:00
assert store.get(ent) == {"value": "second"}