Use `setup_test_component_platform` helper for sensor entity component tests instead of `hass.components` (#114316)
* Use `setup_test_component_platform` helper for sensor entity component tests instead of `hass.components` * Missing file * Fix import * Remove invalid device classpull/114305/head
parent
41bd3d0853
commit
22b14d83e8
|
@ -1461,7 +1461,10 @@ def mock_integration(
|
|||
|
||||
|
||||
def mock_platform(
|
||||
hass: HomeAssistant, platform_path: str, module: Mock | MockPlatform | None = None
|
||||
hass: HomeAssistant,
|
||||
platform_path: str,
|
||||
module: Mock | MockPlatform | None = None,
|
||||
built_in=True,
|
||||
) -> None:
|
||||
"""Mock a platform.
|
||||
|
||||
|
@ -1472,7 +1475,7 @@ def mock_platform(
|
|||
module_cache = hass.data[loader.DATA_COMPONENTS]
|
||||
|
||||
if domain not in integration_cache:
|
||||
mock_integration(hass, MockModule(domain))
|
||||
mock_integration(hass, MockModule(domain), built_in=built_in)
|
||||
|
||||
integration_cache[domain]._top_level_files.add(f"{platform_name}.py")
|
||||
_LOGGER.info("Adding mock integration platform: %s", platform_path)
|
||||
|
@ -1665,6 +1668,7 @@ def setup_test_component_platform(
|
|||
domain: str,
|
||||
entities: Sequence[Entity],
|
||||
from_config_entry: bool = False,
|
||||
built_in: bool = True,
|
||||
) -> MockPlatform:
|
||||
"""Mock a test component platform for tests."""
|
||||
|
||||
|
@ -1695,9 +1699,5 @@ def setup_test_component_platform(
|
|||
platform.async_setup_entry = _async_setup_entry
|
||||
platform.async_setup_platform = None
|
||||
|
||||
mock_platform(
|
||||
hass,
|
||||
f"test.{domain}",
|
||||
platform,
|
||||
)
|
||||
mock_platform(hass, f"test.{domain}", platform, built_in=built_in)
|
||||
return platform
|
||||
|
|
|
@ -10,6 +10,7 @@ from homeassistant.const import STATE_OFF, STATE_ON
|
|||
|
||||
if TYPE_CHECKING:
|
||||
from tests.components.light.common import MockLight
|
||||
from tests.components.sensor.common import MockSensor
|
||||
|
||||
|
||||
@pytest.fixture(scope="session", autouse=True)
|
||||
|
@ -118,3 +119,11 @@ def mock_light_entities() -> list["MockLight"]:
|
|||
MockLight("Ceiling", STATE_OFF),
|
||||
MockLight(None, STATE_OFF),
|
||||
]
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_sensor_entities() -> dict[str, "MockSensor"]:
|
||||
"""Return mocked sensor entities."""
|
||||
from tests.components.sensor.common import get_mock_sensor_entities
|
||||
|
||||
return get_mock_sensor_entities()
|
||||
|
|
|
@ -23,6 +23,7 @@ from homeassistant.components.mqtt.models import (
|
|||
MqttValueTemplateException,
|
||||
ReceiveMessage,
|
||||
)
|
||||
from homeassistant.components.sensor import SensorDeviceClass
|
||||
from homeassistant.config_entries import ConfigEntryDisabler, ConfigEntryState
|
||||
from homeassistant.const import (
|
||||
ATTR_ASSUMED_STATE,
|
||||
|
@ -52,10 +53,9 @@ from tests.common import (
|
|||
async_fire_mqtt_message,
|
||||
async_fire_time_changed,
|
||||
mock_restore_cache,
|
||||
setup_test_component_platform,
|
||||
)
|
||||
from tests.testing_config.custom_components.test.sensor import ( # type: ignore[attr-defined]
|
||||
DEVICE_CLASSES,
|
||||
)
|
||||
from tests.components.sensor.common import MockSensor
|
||||
from tests.typing import (
|
||||
MqttMockHAClient,
|
||||
MqttMockHAClientGenerator,
|
||||
|
@ -3142,12 +3142,12 @@ async def test_debug_info_non_mqtt(
|
|||
device_registry: dr.DeviceRegistry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
mqtt_mock_entry: MqttMockHAClientGenerator,
|
||||
mock_sensor_entities: dict[str, MockSensor],
|
||||
) -> None:
|
||||
"""Test we get empty debug_info for a device with non MQTT entities."""
|
||||
await mqtt_mock_entry()
|
||||
domain = "sensor"
|
||||
platform = getattr(hass.components, f"test.{domain}")
|
||||
platform.init()
|
||||
setup_test_component_platform(hass, domain, mock_sensor_entities)
|
||||
|
||||
config_entry = MockConfigEntry(domain="test", data={})
|
||||
config_entry.add_to_hass(hass)
|
||||
|
@ -3155,11 +3155,11 @@ async def test_debug_info_non_mqtt(
|
|||
config_entry_id=config_entry.entry_id,
|
||||
connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
|
||||
)
|
||||
for device_class in DEVICE_CLASSES:
|
||||
for device_class in SensorDeviceClass:
|
||||
entity_registry.async_get_or_create(
|
||||
domain,
|
||||
"test",
|
||||
platform.ENTITIES[device_class].unique_id,
|
||||
mock_sensor_entities[device_class].unique_id,
|
||||
device_id=device_entry.id,
|
||||
)
|
||||
|
||||
|
|
|
@ -1,10 +1,6 @@
|
|||
"""Provide a mock sensor platform.
|
||||
|
||||
Call init before using it in your tests to ensure clean test data.
|
||||
"""
|
||||
"""Common test utilities for sensor entity component tests."""
|
||||
|
||||
from homeassistant.components.sensor import (
|
||||
DEVICE_CLASSES,
|
||||
RestoreSensor,
|
||||
SensorDeviceClass,
|
||||
SensorEntity,
|
||||
|
@ -24,8 +20,6 @@ from homeassistant.const import (
|
|||
|
||||
from tests.common import MockEntity
|
||||
|
||||
DEVICE_CLASSES.append("none")
|
||||
|
||||
UNITS_OF_MEASUREMENT = {
|
||||
SensorDeviceClass.APPARENT_POWER: UnitOfApparentPower.VOLT_AMPERE, # apparent power (VA)
|
||||
SensorDeviceClass.BATTERY: PERCENTAGE, # % of battery that is left
|
||||
|
@ -56,34 +50,6 @@ UNITS_OF_MEASUREMENT = {
|
|||
SensorDeviceClass.GAS: UnitOfVolume.CUBIC_METERS, # gas (m³)
|
||||
}
|
||||
|
||||
ENTITIES = {}
|
||||
|
||||
|
||||
def init(empty=False):
|
||||
"""Initialize the platform with entities."""
|
||||
global ENTITIES
|
||||
|
||||
ENTITIES = (
|
||||
{}
|
||||
if empty
|
||||
else {
|
||||
device_class: MockSensor(
|
||||
name=f"{device_class} sensor",
|
||||
unique_id=f"unique_{device_class}",
|
||||
device_class=device_class,
|
||||
native_unit_of_measurement=UNITS_OF_MEASUREMENT.get(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 MockSensor(MockEntity, SensorEntity):
|
||||
"""Mock Sensor class."""
|
||||
|
@ -141,3 +107,16 @@ class MockRestoreSensor(MockSensor, RestoreSensor):
|
|||
self._values["native_unit_of_measurement"] = (
|
||||
last_sensor_data.native_unit_of_measurement
|
||||
)
|
||||
|
||||
|
||||
def get_mock_sensor_entities() -> dict[str, MockSensor]:
|
||||
"""Get mock sensor entities."""
|
||||
return {
|
||||
device_class: MockSensor(
|
||||
name=f"{device_class} sensor",
|
||||
unique_id=f"unique_{device_class}",
|
||||
device_class=device_class,
|
||||
native_unit_of_measurement=UNITS_OF_MEASUREMENT.get(device_class),
|
||||
)
|
||||
for device_class in SensorDeviceClass
|
||||
}
|
|
@ -26,8 +26,9 @@ from tests.common import (
|
|||
async_get_device_automation_capabilities,
|
||||
async_get_device_automations,
|
||||
async_mock_service,
|
||||
setup_test_component_platform,
|
||||
)
|
||||
from tests.testing_config.custom_components.test.sensor import UNITS_OF_MEASUREMENT
|
||||
from tests.components.sensor.common import UNITS_OF_MEASUREMENT, MockSensor
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True, name="stub_blueprint_populate")
|
||||
|
@ -85,11 +86,10 @@ async def test_get_conditions(
|
|||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
enable_custom_integrations: None,
|
||||
mock_sensor_entities: dict[str, MockSensor],
|
||||
) -> None:
|
||||
"""Test we get the expected conditions from a sensor."""
|
||||
platform = getattr(hass.components, f"test.{DOMAIN}")
|
||||
platform.init()
|
||||
setup_test_component_platform(hass, DOMAIN, mock_sensor_entities.values())
|
||||
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
|
||||
await hass.async_block_till_done()
|
||||
sensor_entries = {}
|
||||
|
@ -104,7 +104,7 @@ async def test_get_conditions(
|
|||
sensor_entries[device_class] = entity_registry.async_get_or_create(
|
||||
DOMAIN,
|
||||
"test",
|
||||
platform.ENTITIES[device_class].unique_id,
|
||||
mock_sensor_entities[device_class].unique_id,
|
||||
device_id=device_entry.id,
|
||||
)
|
||||
|
||||
|
@ -284,6 +284,7 @@ async def test_get_condition_capabilities(
|
|||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
mock_sensor_entities: dict[str, MockSensor],
|
||||
set_state,
|
||||
device_class_reg,
|
||||
device_class_state,
|
||||
|
@ -291,8 +292,7 @@ async def test_get_condition_capabilities(
|
|||
unit_state,
|
||||
) -> None:
|
||||
"""Test we get the expected capabilities from a sensor condition."""
|
||||
platform = getattr(hass.components, f"test.{DOMAIN}")
|
||||
platform.init()
|
||||
setup_test_component_platform(hass, DOMAIN, mock_sensor_entities.values())
|
||||
|
||||
config_entry = MockConfigEntry(domain="test", data={})
|
||||
config_entry.add_to_hass(hass)
|
||||
|
@ -303,7 +303,7 @@ async def test_get_condition_capabilities(
|
|||
entity_id = entity_registry.async_get_or_create(
|
||||
DOMAIN,
|
||||
"test",
|
||||
platform.ENTITIES["battery"].unique_id,
|
||||
mock_sensor_entities["battery"].unique_id,
|
||||
device_id=device_entry.id,
|
||||
original_device_class=device_class_reg,
|
||||
unit_of_measurement=unit_reg,
|
||||
|
@ -353,6 +353,7 @@ async def test_get_condition_capabilities_legacy(
|
|||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
mock_sensor_entities: dict[str, MockSensor],
|
||||
set_state,
|
||||
device_class_reg,
|
||||
device_class_state,
|
||||
|
@ -360,8 +361,7 @@ async def test_get_condition_capabilities_legacy(
|
|||
unit_state,
|
||||
) -> None:
|
||||
"""Test we get the expected capabilities from a sensor condition."""
|
||||
platform = getattr(hass.components, f"test.{DOMAIN}")
|
||||
platform.init()
|
||||
setup_test_component_platform(hass, DOMAIN, mock_sensor_entities.values())
|
||||
|
||||
config_entry = MockConfigEntry(domain="test", data={})
|
||||
config_entry.add_to_hass(hass)
|
||||
|
@ -372,7 +372,7 @@ async def test_get_condition_capabilities_legacy(
|
|||
entity_id = entity_registry.async_get_or_create(
|
||||
DOMAIN,
|
||||
"test",
|
||||
platform.ENTITIES["battery"].unique_id,
|
||||
mock_sensor_entities["battery"].unique_id,
|
||||
device_id=device_entry.id,
|
||||
original_device_class=device_class_reg,
|
||||
unit_of_measurement=unit_reg,
|
||||
|
@ -417,11 +417,13 @@ async def test_get_condition_capabilities_legacy(
|
|||
async def test_get_condition_capabilities_none(
|
||||
hass: HomeAssistant,
|
||||
entity_registry: er.EntityRegistry,
|
||||
enable_custom_integrations: None,
|
||||
) -> None:
|
||||
"""Test we get the expected capabilities from a sensor condition."""
|
||||
platform = getattr(hass.components, f"test.{DOMAIN}")
|
||||
platform.init()
|
||||
entity = MockSensor(
|
||||
name="none sensor",
|
||||
unique_id="unique_none",
|
||||
)
|
||||
setup_test_component_platform(hass, DOMAIN, [entity])
|
||||
|
||||
config_entry = MockConfigEntry(domain="test", data={})
|
||||
config_entry.add_to_hass(hass)
|
||||
|
@ -429,7 +431,7 @@ async def test_get_condition_capabilities_none(
|
|||
entry_none = entity_registry.async_get_or_create(
|
||||
DOMAIN,
|
||||
"test",
|
||||
platform.ENTITIES["none"].unique_id,
|
||||
entity.unique_id,
|
||||
)
|
||||
|
||||
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
|
||||
|
|
|
@ -30,8 +30,9 @@ from tests.common import (
|
|||
async_get_device_automation_capabilities,
|
||||
async_get_device_automations,
|
||||
async_mock_service,
|
||||
setup_test_component_platform,
|
||||
)
|
||||
from tests.testing_config.custom_components.test.sensor import UNITS_OF_MEASUREMENT
|
||||
from tests.components.sensor.common import UNITS_OF_MEASUREMENT, MockSensor
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True, name="stub_blueprint_populate")
|
||||
|
@ -87,11 +88,10 @@ async def test_get_triggers(
|
|||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
enable_custom_integrations: None,
|
||||
mock_sensor_entities: dict[str, MockSensor],
|
||||
) -> None:
|
||||
"""Test we get the expected triggers from a sensor."""
|
||||
platform = getattr(hass.components, f"test.{DOMAIN}")
|
||||
platform.init()
|
||||
setup_test_component_platform(hass, DOMAIN, mock_sensor_entities.values())
|
||||
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
|
||||
await hass.async_block_till_done()
|
||||
sensor_entries: dict[SensorDeviceClass, er.RegistryEntry] = {}
|
||||
|
@ -106,7 +106,7 @@ async def test_get_triggers(
|
|||
sensor_entries[device_class] = entity_registry.async_get_or_create(
|
||||
DOMAIN,
|
||||
"test",
|
||||
platform.ENTITIES[device_class].unique_id,
|
||||
mock_sensor_entities[device_class].unique_id,
|
||||
device_id=device_entry.id,
|
||||
)
|
||||
|
||||
|
@ -241,6 +241,7 @@ async def test_get_trigger_capabilities(
|
|||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
mock_sensor_entities: dict[str, MockSensor],
|
||||
set_state,
|
||||
device_class_reg,
|
||||
device_class_state,
|
||||
|
@ -248,8 +249,7 @@ async def test_get_trigger_capabilities(
|
|||
unit_state,
|
||||
) -> None:
|
||||
"""Test we get the expected capabilities from a sensor trigger."""
|
||||
platform = getattr(hass.components, f"test.{DOMAIN}")
|
||||
platform.init()
|
||||
setup_test_component_platform(hass, DOMAIN, mock_sensor_entities)
|
||||
|
||||
config_entry = MockConfigEntry(domain="test", data={})
|
||||
config_entry.add_to_hass(hass)
|
||||
|
@ -260,7 +260,7 @@ async def test_get_trigger_capabilities(
|
|||
entity_id = entity_registry.async_get_or_create(
|
||||
DOMAIN,
|
||||
"test",
|
||||
platform.ENTITIES["battery"].unique_id,
|
||||
mock_sensor_entities["battery"].unique_id,
|
||||
device_id=device_entry.id,
|
||||
original_device_class=device_class_reg,
|
||||
unit_of_measurement=unit_reg,
|
||||
|
@ -311,6 +311,7 @@ async def test_get_trigger_capabilities_legacy(
|
|||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
mock_sensor_entities: dict[str, MockSensor],
|
||||
set_state,
|
||||
device_class_reg,
|
||||
device_class_state,
|
||||
|
@ -318,8 +319,7 @@ async def test_get_trigger_capabilities_legacy(
|
|||
unit_state,
|
||||
) -> None:
|
||||
"""Test we get the expected capabilities from a sensor trigger."""
|
||||
platform = getattr(hass.components, f"test.{DOMAIN}")
|
||||
platform.init()
|
||||
setup_test_component_platform(hass, DOMAIN, mock_sensor_entities)
|
||||
|
||||
config_entry = MockConfigEntry(domain="test", data={})
|
||||
config_entry.add_to_hass(hass)
|
||||
|
@ -330,7 +330,7 @@ async def test_get_trigger_capabilities_legacy(
|
|||
entity_id = entity_registry.async_get_or_create(
|
||||
DOMAIN,
|
||||
"test",
|
||||
platform.ENTITIES["battery"].unique_id,
|
||||
mock_sensor_entities["battery"].unique_id,
|
||||
device_id=device_entry.id,
|
||||
original_device_class=device_class_reg,
|
||||
unit_of_measurement=unit_reg,
|
||||
|
@ -374,11 +374,13 @@ async def test_get_trigger_capabilities_legacy(
|
|||
async def test_get_trigger_capabilities_none(
|
||||
hass: HomeAssistant,
|
||||
entity_registry: er.EntityRegistry,
|
||||
enable_custom_integrations: None,
|
||||
) -> None:
|
||||
"""Test we get the expected capabilities from a sensor trigger."""
|
||||
platform = getattr(hass.components, f"test.{DOMAIN}")
|
||||
platform.init()
|
||||
entity = MockSensor(
|
||||
name="none sensor",
|
||||
unique_id="unique_none",
|
||||
)
|
||||
setup_test_component_platform(hass, DOMAIN, [entity])
|
||||
|
||||
config_entry = MockConfigEntry(domain="test", data={})
|
||||
config_entry.add_to_hass(hass)
|
||||
|
@ -386,7 +388,7 @@ async def test_get_trigger_capabilities_none(
|
|||
entry_none = entity_registry.async_get_or_create(
|
||||
DOMAIN,
|
||||
"test",
|
||||
platform.ENTITIES["none"].unique_id,
|
||||
entity.unique_id,
|
||||
)
|
||||
|
||||
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -33,13 +33,14 @@ from homeassistant.components.recorder.statistics import (
|
|||
list_statistic_ids,
|
||||
)
|
||||
from homeassistant.components.recorder.util import get_instance, session_scope
|
||||
from homeassistant.components.sensor import ATTR_OPTIONS, SensorDeviceClass
|
||||
from homeassistant.components.sensor import ATTR_OPTIONS, DOMAIN, SensorDeviceClass
|
||||
from homeassistant.const import ATTR_FRIENDLY_NAME, STATE_UNAVAILABLE
|
||||
from homeassistant.core import HomeAssistant, State
|
||||
from homeassistant.setup import async_setup_component, setup_component
|
||||
import homeassistant.util.dt as dt_util
|
||||
from homeassistant.util.unit_system import METRIC_SYSTEM, US_CUSTOMARY_SYSTEM
|
||||
|
||||
from tests.common import setup_test_component_platform
|
||||
from tests.components.recorder.common import (
|
||||
assert_dict_of_states_equal_without_context_and_last_changed,
|
||||
assert_multiple_states_equal_without_context_and_last_changed,
|
||||
|
@ -49,6 +50,7 @@ from tests.components.recorder.common import (
|
|||
statistics_during_period,
|
||||
wait_recording_done,
|
||||
)
|
||||
from tests.components.sensor.common import MockSensor
|
||||
from tests.typing import WebSocketGenerator
|
||||
|
||||
BATTERY_SENSOR_ATTRIBUTES = {
|
||||
|
@ -1363,11 +1365,9 @@ def test_compile_hourly_sum_statistics_negative_state(
|
|||
hass = hass_recorder()
|
||||
hass.data.pop(loader.DATA_CUSTOM_COMPONENTS)
|
||||
|
||||
platform = getattr(hass.components, "test.sensor")
|
||||
platform.init(empty=True)
|
||||
mocksensor = platform.MockSensor(name="custom_sensor")
|
||||
mocksensor = MockSensor(name="custom_sensor")
|
||||
mocksensor._attr_should_poll = False
|
||||
platform.ENTITIES["custom_sensor"] = mocksensor
|
||||
setup_test_component_platform(hass, DOMAIN, [mocksensor], built_in=False)
|
||||
|
||||
setup_component(hass, "homeassistant", {})
|
||||
setup_component(
|
||||
|
@ -5178,9 +5178,7 @@ async def test_exclude_attributes(
|
|||
recorder_mock: Recorder, hass: HomeAssistant, enable_custom_integrations: None
|
||||
) -> None:
|
||||
"""Test sensor attributes to be excluded."""
|
||||
platform = getattr(hass.components, "test.sensor")
|
||||
platform.init(empty=True)
|
||||
platform.ENTITIES["0"] = platform.MockSensor(
|
||||
entity0 = MockSensor(
|
||||
has_entity_name=True,
|
||||
unique_id="test",
|
||||
name="Test",
|
||||
|
@ -5188,6 +5186,7 @@ async def test_exclude_attributes(
|
|||
device_class=SensorDeviceClass.ENUM,
|
||||
options=["option1", "option2"],
|
||||
)
|
||||
setup_test_component_platform(hass, DOMAIN, [entity0])
|
||||
assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}})
|
||||
await hass.async_block_till_done()
|
||||
await async_wait_recording_done(hass)
|
||||
|
|
Loading…
Reference in New Issue