From 6313571fbc141326c7fa70a78b37d2f989423aa3 Mon Sep 17 00:00:00 2001 From: Jan-Philipp Benecke Date: Wed, 27 Mar 2024 14:24:02 +0100 Subject: [PATCH] Use `setup_test_component_platform` helper for binary_sensor entity component tests instead of `hass.components` (#114293) --- tests/components/binary_sensor/common.py | 19 +++++++ tests/components/binary_sensor/conftest.py | 21 ++++++++ .../binary_sensor/test_device_condition.py | 34 ++++++------- .../binary_sensor/test_device_trigger.py | 37 +++++++------- tests/components/binary_sensor/test_init.py | 3 +- .../custom_components/test/binary_sensor.py | 50 ------------------- 6 files changed, 74 insertions(+), 90 deletions(-) create mode 100644 tests/components/binary_sensor/common.py create mode 100644 tests/components/binary_sensor/conftest.py delete mode 100644 tests/testing_config/custom_components/test/binary_sensor.py diff --git a/tests/components/binary_sensor/common.py b/tests/components/binary_sensor/common.py new file mode 100644 index 00000000000..bfa9b8e2d52 --- /dev/null +++ b/tests/components/binary_sensor/common.py @@ -0,0 +1,19 @@ +"""Common test utilities for binary_sensor entity component tests.""" + +from homeassistant.components.binary_sensor import BinarySensorEntity + +from tests.common import MockEntity + + +class MockBinarySensor(MockEntity, BinarySensorEntity): + """Mock Binary Sensor class.""" + + @property + def is_on(self): + """Return true if the binary sensor is on.""" + return self._handle("is_on") + + @property + def device_class(self): + """Return the class of this sensor.""" + return self._handle("device_class") diff --git a/tests/components/binary_sensor/conftest.py b/tests/components/binary_sensor/conftest.py new file mode 100644 index 00000000000..33fcbc24089 --- /dev/null +++ b/tests/components/binary_sensor/conftest.py @@ -0,0 +1,21 @@ +"""Fixtures for binary_sensor entity component tests.""" + +import pytest + +from homeassistant.components.binary_sensor import BinarySensorDeviceClass + +from .common import MockBinarySensor + + +@pytest.fixture +def mock_binary_sensor_entities() -> dict[str, MockBinarySensor]: + """Return mock binary sensors.""" + return { + device_class: MockBinarySensor( + name=f"{device_class} sensor", + is_on=True, + unique_id=f"unique_{device_class}", + device_class=device_class, + ) + for device_class in BinarySensorDeviceClass + } diff --git a/tests/components/binary_sensor/test_device_condition.py b/tests/components/binary_sensor/test_device_condition.py index 904e62ef18d..83451313bad 100644 --- a/tests/components/binary_sensor/test_device_condition.py +++ b/tests/components/binary_sensor/test_device_condition.py @@ -16,11 +16,14 @@ from homeassistant.helpers import device_registry as dr, entity_registry as er from homeassistant.setup import async_setup_component import homeassistant.util.dt as dt_util +from .common import MockBinarySensor + from tests.common import ( MockConfigEntry, async_get_device_automation_capabilities, async_get_device_automations, async_mock_service, + setup_test_component_platform, ) @@ -39,11 +42,10 @@ async def test_get_conditions( hass: HomeAssistant, device_registry: dr.DeviceRegistry, entity_registry: er.EntityRegistry, - enable_custom_integrations: None, + mock_binary_sensor_entities: dict[str, MockBinarySensor], ) -> None: """Test we get the expected conditions from a binary_sensor.""" - platform = getattr(hass.components, f"test.{DOMAIN}") - platform.init() + setup_test_component_platform(hass, DOMAIN, mock_binary_sensor_entities.values()) assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}}) await hass.async_block_till_done() binary_sensor_entries = {} @@ -58,7 +60,7 @@ async def test_get_conditions( binary_sensor_entries[device_class] = entity_registry.async_get_or_create( DOMAIN, "test", - platform.ENTITIES[device_class].unique_id, + mock_binary_sensor_entities[device_class].unique_id, device_id=device_entry.id, ) @@ -238,12 +240,10 @@ async def test_if_state( device_registry: dr.DeviceRegistry, entity_registry: er.EntityRegistry, calls, - enable_custom_integrations: None, + mock_binary_sensor_entities: dict[str, MockBinarySensor], ) -> None: """Test for turn_on and turn_off conditions.""" - platform = getattr(hass.components, f"test.{DOMAIN}") - - platform.init() + setup_test_component_platform(hass, DOMAIN, mock_binary_sensor_entities.values()) assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}}) await hass.async_block_till_done() @@ -253,7 +253,7 @@ async def test_if_state( config_entry_id=config_entry.entry_id, connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")}, ) - entry = entity_registry.async_get(platform.ENTITIES["battery"].entity_id) + entry = entity_registry.async_get(mock_binary_sensor_entities["battery"].entity_id) entity_registry.async_update_entity(entry.entity_id, device_id=device_entry.id) assert await async_setup_component( @@ -324,12 +324,10 @@ async def test_if_state_legacy( device_registry: dr.DeviceRegistry, entity_registry: er.EntityRegistry, calls, - enable_custom_integrations: None, + mock_binary_sensor_entities: dict[str, MockBinarySensor], ) -> None: """Test for turn_on and turn_off conditions.""" - platform = getattr(hass.components, f"test.{DOMAIN}") - - platform.init() + setup_test_component_platform(hass, DOMAIN, mock_binary_sensor_entities.values()) assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}}) await hass.async_block_till_done() @@ -339,7 +337,7 @@ async def test_if_state_legacy( config_entry_id=config_entry.entry_id, connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")}, ) - entry = entity_registry.async_get(platform.ENTITIES["battery"].entity_id) + entry = entity_registry.async_get(mock_binary_sensor_entities["battery"].entity_id) entity_registry.async_update_entity(entry.entity_id, device_id=device_entry.id) assert await async_setup_component( @@ -384,16 +382,14 @@ async def test_if_fires_on_for_condition( device_registry: dr.DeviceRegistry, entity_registry: er.EntityRegistry, calls, - enable_custom_integrations: None, + mock_binary_sensor_entities: dict[str, MockBinarySensor], ) -> None: """Test for firing if condition is on with delay.""" point1 = dt_util.utcnow() point2 = point1 + timedelta(seconds=10) point3 = point2 + timedelta(seconds=10) - platform = getattr(hass.components, f"test.{DOMAIN}") - - platform.init() + setup_test_component_platform(hass, DOMAIN, mock_binary_sensor_entities.values()) assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}}) await hass.async_block_till_done() @@ -403,7 +399,7 @@ async def test_if_fires_on_for_condition( config_entry_id=config_entry.entry_id, connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")}, ) - entry = entity_registry.async_get(platform.ENTITIES["battery"].entity_id) + entry = entity_registry.async_get(mock_binary_sensor_entities["battery"].entity_id) entity_registry.async_update_entity(entry.entity_id, device_id=device_entry.id) with freeze_time(point1) as time_freeze: diff --git a/tests/components/binary_sensor/test_device_trigger.py b/tests/components/binary_sensor/test_device_trigger.py index ae78620b939..ad7bd9c3528 100644 --- a/tests/components/binary_sensor/test_device_trigger.py +++ b/tests/components/binary_sensor/test_device_trigger.py @@ -15,12 +15,15 @@ from homeassistant.helpers import device_registry as dr, entity_registry as er from homeassistant.setup import async_setup_component import homeassistant.util.dt as dt_util +from .common import MockBinarySensor + from tests.common import ( MockConfigEntry, async_fire_time_changed, async_get_device_automation_capabilities, async_get_device_automations, async_mock_service, + setup_test_component_platform, ) @@ -39,12 +42,11 @@ async def test_get_triggers( hass: HomeAssistant, device_registry: dr.DeviceRegistry, entity_registry: er.EntityRegistry, - enable_custom_integrations: None, + mock_binary_sensor_entities: dict[str, MockBinarySensor], ) -> None: """Test we get the expected triggers from a binary_sensor.""" registry_entries: dict[BinarySensorDeviceClass, er.RegistryEntry] = {} - platform = getattr(hass.components, f"test.{DOMAIN}") - platform.init() + setup_test_component_platform(hass, DOMAIN, mock_binary_sensor_entities.values()) assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}}) await hass.async_block_till_done() @@ -58,7 +60,7 @@ async def test_get_triggers( registry_entries[device_class] = entity_registry.async_get_or_create( DOMAIN, "test", - platform.ENTITIES[device_class].unique_id, + mock_binary_sensor_entities[device_class].unique_id, device_id=device_entry.id, ) @@ -132,11 +134,11 @@ async def test_get_triggers_no_state( hass: HomeAssistant, device_registry: dr.DeviceRegistry, entity_registry: er.EntityRegistry, + mock_binary_sensor_entities: dict[str, MockBinarySensor], ) -> None: """Test we get the expected triggers from a binary_sensor.""" registry_entries: dict[BinarySensorDeviceClass, er.RegistryEntry] = {} - platform = getattr(hass.components, f"test.{DOMAIN}") - platform.init() + setup_test_component_platform(hass, DOMAIN, mock_binary_sensor_entities.values()) config_entry = MockConfigEntry(domain="test", data={}) config_entry.add_to_hass(hass) @@ -239,11 +241,10 @@ async def test_if_fires_on_state_change( device_registry: dr.DeviceRegistry, entity_registry: er.EntityRegistry, calls, - enable_custom_integrations: None, + mock_binary_sensor_entities: dict[str, MockBinarySensor], ) -> None: """Test for on and off triggers firing.""" - platform = getattr(hass.components, f"test.{DOMAIN}") - platform.init() + setup_test_component_platform(hass, DOMAIN, mock_binary_sensor_entities.values()) assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}}) await hass.async_block_till_done() @@ -256,7 +257,7 @@ async def test_if_fires_on_state_change( entry = entity_registry.async_get_or_create( DOMAIN, "test", - platform.ENTITIES["battery"].unique_id, + mock_binary_sensor_entities["battery"].unique_id, device_id=device_entry.id, ) @@ -341,12 +342,10 @@ async def test_if_fires_on_state_change_with_for( device_registry: dr.DeviceRegistry, entity_registry: er.EntityRegistry, calls, - enable_custom_integrations: None, + mock_binary_sensor_entities: dict[str, MockBinarySensor], ) -> None: """Test for triggers firing with delay.""" - platform = getattr(hass.components, f"test.{DOMAIN}") - - platform.init() + setup_test_component_platform(hass, DOMAIN, mock_binary_sensor_entities.values()) assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}}) await hass.async_block_till_done() @@ -359,7 +358,7 @@ async def test_if_fires_on_state_change_with_for( entry = entity_registry.async_get_or_create( DOMAIN, "test", - platform.ENTITIES["battery"].unique_id, + mock_binary_sensor_entities["battery"].unique_id, device_id=device_entry.id, ) @@ -418,12 +417,10 @@ async def test_if_fires_on_state_change_legacy( device_registry: dr.DeviceRegistry, entity_registry: er.EntityRegistry, calls, - enable_custom_integrations: None, + mock_binary_sensor_entities: dict[str, MockBinarySensor], ) -> None: """Test for triggers firing.""" - platform = getattr(hass.components, f"test.{DOMAIN}") - - platform.init() + setup_test_component_platform(hass, DOMAIN, mock_binary_sensor_entities.values()) assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}}) await hass.async_block_till_done() @@ -436,7 +433,7 @@ async def test_if_fires_on_state_change_legacy( entry = entity_registry.async_get_or_create( DOMAIN, "test", - platform.ENTITIES["battery"].unique_id, + mock_binary_sensor_entities["battery"].unique_id, device_id=device_entry.id, ) diff --git a/tests/components/binary_sensor/test_init.py b/tests/components/binary_sensor/test_init.py index 059e387c4fd..335b9b40d50 100644 --- a/tests/components/binary_sensor/test_init.py +++ b/tests/components/binary_sensor/test_init.py @@ -11,6 +11,8 @@ from homeassistant.const import STATE_OFF, STATE_ON, EntityCategory from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback +from .common import MockBinarySensor + from tests.common import ( MockConfigEntry, MockModule, @@ -21,7 +23,6 @@ from tests.common import ( mock_integration, mock_platform, ) -from tests.testing_config.custom_components.test.binary_sensor import MockBinarySensor TEST_DOMAIN = "test" diff --git a/tests/testing_config/custom_components/test/binary_sensor.py b/tests/testing_config/custom_components/test/binary_sensor.py deleted file mode 100644 index e45e614a82c..00000000000 --- a/tests/testing_config/custom_components/test/binary_sensor.py +++ /dev/null @@ -1,50 +0,0 @@ -"""Provide a mock binary sensor platform. - -Call init before using it in your tests to ensure clean test data. -""" - -from homeassistant.components.binary_sensor import DEVICE_CLASSES, BinarySensorEntity - -from tests.common import MockEntity - -ENTITIES = {} - - -def init(empty=False): - """Initialize the platform with entities.""" - global ENTITIES - - ENTITIES = ( - {} - if empty - else { - device_class: MockBinarySensor( - name=f"{device_class} sensor", - is_on=True, - unique_id=f"unique_{device_class}", - device_class=device_class, - ) - for device_class in DEVICE_CLASSES - } - ) - - -async def async_setup_platform( - hass, config, async_add_entities_callback, discovery_info=None -): - """Return mock entities.""" - async_add_entities_callback(list(ENTITIES.values())) - - -class MockBinarySensor(MockEntity, BinarySensorEntity): - """Mock Binary Sensor class.""" - - @property - def is_on(self): - """Return true if the binary sensor is on.""" - return self._handle("is_on") - - @property - def device_class(self): - """Return the class of this sensor.""" - return self._handle("device_class")