mirror of
https://github.com/home-assistant/core.git
synced 2026-04-26 03:39:31 +01:00
Add support for glob matching to entity filters (#36913)
* Added GLOB capability to entityfilter and every place that uses it. All existing tests are passing * added tests for components affected by glob change * fixed flake8 error * mocking the correct listener * mocking correct bus method in azure test * tests passing in 3.7 and 3.8 * fixed formatting issue from rebase/conflict * Checking against glob patterns in more performant way * perf improvments and reverted unnecessarily adjusted tests * added new benchmark test around filters * no longer using get with default in entityfilter * changed filter name and removed logbook from filter benchmark * simplified benchmark tests from feedback * fixed apache tests and returned include exclude schemas to normal * fixed azure event hub tests to properly go through component logic * fixed azure test and clean up for other tests * renaming test files to match standard * merged mqtt statestream test changes with base * removed dependency on recorder filter schema from history * fixed recorder tests after merge and a bunch of lint errors
This commit is contained in:
181
tests/components/apache_kafka/test_init.py
Normal file
181
tests/components/apache_kafka/test_init.py
Normal file
@@ -0,0 +1,181 @@
|
||||
"""The tests for the Apache Kafka component."""
|
||||
from collections import namedtuple
|
||||
|
||||
import pytest
|
||||
|
||||
import homeassistant.components.apache_kafka as apache_kafka
|
||||
from homeassistant.const import STATE_ON
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from tests.async_mock import patch
|
||||
|
||||
APACHE_KAFKA_PATH = "homeassistant.components.apache_kafka"
|
||||
PRODUCER_PATH = f"{APACHE_KAFKA_PATH}.AIOKafkaProducer"
|
||||
MIN_CONFIG = {
|
||||
"ip_address": "localhost",
|
||||
"port": 8080,
|
||||
"topic": "topic",
|
||||
}
|
||||
FilterTest = namedtuple("FilterTest", "id should_pass")
|
||||
MockKafkaClient = namedtuple("MockKafkaClient", "init start send_and_wait")
|
||||
|
||||
|
||||
@pytest.fixture(name="mock_client")
|
||||
def mock_client_fixture():
|
||||
"""Mock the apache kafka client."""
|
||||
with patch(f"{PRODUCER_PATH}.start") as start, patch(
|
||||
f"{PRODUCER_PATH}.send_and_wait"
|
||||
) as send_and_wait, patch(f"{PRODUCER_PATH}.__init__", return_value=None) as init:
|
||||
yield MockKafkaClient(init, start, send_and_wait)
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True, scope="module")
|
||||
def mock_client_stop():
|
||||
"""Mock client stop at module scope for teardown."""
|
||||
with patch(f"{PRODUCER_PATH}.stop") as stop:
|
||||
yield stop
|
||||
|
||||
|
||||
async def test_minimal_config(hass, mock_client):
|
||||
"""Test the minimal config and defaults of component."""
|
||||
config = {apache_kafka.DOMAIN: MIN_CONFIG}
|
||||
assert await async_setup_component(hass, apache_kafka.DOMAIN, config)
|
||||
await hass.async_block_till_done()
|
||||
assert mock_client.start.called_once
|
||||
|
||||
|
||||
async def test_full_config(hass, mock_client):
|
||||
"""Test the full config of component."""
|
||||
config = {
|
||||
apache_kafka.DOMAIN: {
|
||||
"filter": {
|
||||
"include_domains": ["light"],
|
||||
"include_entity_globs": ["sensor.included_*"],
|
||||
"include_entities": ["binary_sensor.included"],
|
||||
"exclude_domains": ["light"],
|
||||
"exclude_entity_globs": ["sensor.excluded_*"],
|
||||
"exclude_entities": ["binary_sensor.excluded"],
|
||||
},
|
||||
}
|
||||
}
|
||||
config[apache_kafka.DOMAIN].update(MIN_CONFIG)
|
||||
|
||||
assert await async_setup_component(hass, apache_kafka.DOMAIN, config)
|
||||
await hass.async_block_till_done()
|
||||
assert mock_client.start.called_once
|
||||
|
||||
|
||||
async def _setup(hass, filter_config):
|
||||
"""Shared set up for filtering tests."""
|
||||
config = {apache_kafka.DOMAIN: {"filter": filter_config}}
|
||||
config[apache_kafka.DOMAIN].update(MIN_CONFIG)
|
||||
|
||||
assert await async_setup_component(hass, apache_kafka.DOMAIN, config)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
|
||||
async def _run_filter_tests(hass, tests, mock_client):
|
||||
"""Run a series of filter tests on apache kafka."""
|
||||
for test in tests:
|
||||
hass.states.async_set(test.id, STATE_ON)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
if test.should_pass:
|
||||
mock_client.send_and_wait.assert_called_once()
|
||||
mock_client.send_and_wait.reset_mock()
|
||||
else:
|
||||
mock_client.send_and_wait.assert_not_called()
|
||||
|
||||
|
||||
async def test_allowlist(hass, mock_client):
|
||||
"""Test an allowlist only config."""
|
||||
await _setup(
|
||||
hass,
|
||||
{
|
||||
"include_domains": ["light"],
|
||||
"include_entity_globs": ["sensor.included_*"],
|
||||
"include_entities": ["binary_sensor.included"],
|
||||
},
|
||||
)
|
||||
|
||||
tests = [
|
||||
FilterTest("climate.excluded", False),
|
||||
FilterTest("light.included", True),
|
||||
FilterTest("sensor.excluded_test", False),
|
||||
FilterTest("sensor.included_test", True),
|
||||
FilterTest("binary_sensor.included", True),
|
||||
FilterTest("binary_sensor.excluded", False),
|
||||
]
|
||||
|
||||
await _run_filter_tests(hass, tests, mock_client)
|
||||
|
||||
|
||||
async def test_denylist(hass, mock_client):
|
||||
"""Test a denylist only config."""
|
||||
await _setup(
|
||||
hass,
|
||||
{
|
||||
"exclude_domains": ["climate"],
|
||||
"exclude_entity_globs": ["sensor.excluded_*"],
|
||||
"exclude_entities": ["binary_sensor.excluded"],
|
||||
},
|
||||
)
|
||||
|
||||
tests = [
|
||||
FilterTest("climate.excluded", False),
|
||||
FilterTest("light.included", True),
|
||||
FilterTest("sensor.excluded_test", False),
|
||||
FilterTest("sensor.included_test", True),
|
||||
FilterTest("binary_sensor.included", True),
|
||||
FilterTest("binary_sensor.excluded", False),
|
||||
]
|
||||
|
||||
await _run_filter_tests(hass, tests, mock_client)
|
||||
|
||||
|
||||
async def test_filtered_allowlist(hass, mock_client):
|
||||
"""Test an allowlist config with a filtering denylist."""
|
||||
await _setup(
|
||||
hass,
|
||||
{
|
||||
"include_domains": ["light"],
|
||||
"include_entity_globs": ["*.included_*"],
|
||||
"exclude_domains": ["climate"],
|
||||
"exclude_entity_globs": ["*.excluded_*"],
|
||||
"exclude_entities": ["light.excluded"],
|
||||
},
|
||||
)
|
||||
|
||||
tests = [
|
||||
FilterTest("light.included", True),
|
||||
FilterTest("light.excluded_test", False),
|
||||
FilterTest("light.excluded", False),
|
||||
FilterTest("sensor.included_test", True),
|
||||
FilterTest("climate.included_test", False),
|
||||
]
|
||||
|
||||
await _run_filter_tests(hass, tests, mock_client)
|
||||
|
||||
|
||||
async def test_filtered_denylist(hass, mock_client):
|
||||
"""Test a denylist config with a filtering allowlist."""
|
||||
await _setup(
|
||||
hass,
|
||||
{
|
||||
"include_entities": ["climate.included", "sensor.excluded_test"],
|
||||
"exclude_domains": ["climate"],
|
||||
"exclude_entity_globs": ["*.excluded_*"],
|
||||
"exclude_entities": ["light.excluded"],
|
||||
},
|
||||
)
|
||||
|
||||
tests = [
|
||||
FilterTest("climate.excluded", False),
|
||||
FilterTest("climate.included", True),
|
||||
FilterTest("switch.excluded_test", False),
|
||||
FilterTest("sensor.excluded_test", True),
|
||||
FilterTest("light.excluded", False),
|
||||
FilterTest("light.included", True),
|
||||
]
|
||||
|
||||
await _run_filter_tests(hass, tests, mock_client)
|
||||
Reference in New Issue
Block a user