Use setup_test_component_platform helper for binary_sensor entity component tests instead of hass.components (#114293)

This commit is contained in:
Jan-Philipp Benecke 2024-03-27 14:24:02 +01:00 committed by GitHub
parent c222cfd692
commit 6313571fbc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 74 additions and 90 deletions

View file

@ -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")

View file

@ -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
}

View file

@ -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:

View file

@ -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,
)

View file

@ -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"

View file

@ -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")