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 class
This commit is contained in:
Jan-Philipp Benecke 2024-03-28 12:07:55 +01:00 committed by GitHub
parent 41bd3d0853
commit 22b14d83e8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 219 additions and 306 deletions

View File

@ -1461,7 +1461,10 @@ def mock_integration(
def mock_platform( 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: ) -> None:
"""Mock a platform. """Mock a platform.
@ -1472,7 +1475,7 @@ def mock_platform(
module_cache = hass.data[loader.DATA_COMPONENTS] module_cache = hass.data[loader.DATA_COMPONENTS]
if domain not in integration_cache: 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") integration_cache[domain]._top_level_files.add(f"{platform_name}.py")
_LOGGER.info("Adding mock integration platform: %s", platform_path) _LOGGER.info("Adding mock integration platform: %s", platform_path)
@ -1665,6 +1668,7 @@ def setup_test_component_platform(
domain: str, domain: str,
entities: Sequence[Entity], entities: Sequence[Entity],
from_config_entry: bool = False, from_config_entry: bool = False,
built_in: bool = True,
) -> MockPlatform: ) -> MockPlatform:
"""Mock a test component platform for tests.""" """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_entry = _async_setup_entry
platform.async_setup_platform = None platform.async_setup_platform = None
mock_platform( mock_platform(hass, f"test.{domain}", platform, built_in=built_in)
hass,
f"test.{domain}",
platform,
)
return platform return platform

View File

@ -10,6 +10,7 @@ from homeassistant.const import STATE_OFF, STATE_ON
if TYPE_CHECKING: if TYPE_CHECKING:
from tests.components.light.common import MockLight from tests.components.light.common import MockLight
from tests.components.sensor.common import MockSensor
@pytest.fixture(scope="session", autouse=True) @pytest.fixture(scope="session", autouse=True)
@ -118,3 +119,11 @@ def mock_light_entities() -> list["MockLight"]:
MockLight("Ceiling", STATE_OFF), MockLight("Ceiling", STATE_OFF),
MockLight(None, 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()

View File

@ -23,6 +23,7 @@ from homeassistant.components.mqtt.models import (
MqttValueTemplateException, MqttValueTemplateException,
ReceiveMessage, ReceiveMessage,
) )
from homeassistant.components.sensor import SensorDeviceClass
from homeassistant.config_entries import ConfigEntryDisabler, ConfigEntryState from homeassistant.config_entries import ConfigEntryDisabler, ConfigEntryState
from homeassistant.const import ( from homeassistant.const import (
ATTR_ASSUMED_STATE, ATTR_ASSUMED_STATE,
@ -52,10 +53,9 @@ from tests.common import (
async_fire_mqtt_message, async_fire_mqtt_message,
async_fire_time_changed, async_fire_time_changed,
mock_restore_cache, mock_restore_cache,
setup_test_component_platform,
) )
from tests.testing_config.custom_components.test.sensor import ( # type: ignore[attr-defined] from tests.components.sensor.common import MockSensor
DEVICE_CLASSES,
)
from tests.typing import ( from tests.typing import (
MqttMockHAClient, MqttMockHAClient,
MqttMockHAClientGenerator, MqttMockHAClientGenerator,
@ -3142,12 +3142,12 @@ async def test_debug_info_non_mqtt(
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
mqtt_mock_entry: MqttMockHAClientGenerator, mqtt_mock_entry: MqttMockHAClientGenerator,
mock_sensor_entities: dict[str, MockSensor],
) -> None: ) -> None:
"""Test we get empty debug_info for a device with non MQTT entities.""" """Test we get empty debug_info for a device with non MQTT entities."""
await mqtt_mock_entry() await mqtt_mock_entry()
domain = "sensor" domain = "sensor"
platform = getattr(hass.components, f"test.{domain}") setup_test_component_platform(hass, domain, mock_sensor_entities)
platform.init()
config_entry = MockConfigEntry(domain="test", data={}) config_entry = MockConfigEntry(domain="test", data={})
config_entry.add_to_hass(hass) config_entry.add_to_hass(hass)
@ -3155,11 +3155,11 @@ async def test_debug_info_non_mqtt(
config_entry_id=config_entry.entry_id, config_entry_id=config_entry.entry_id,
connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")}, 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( entity_registry.async_get_or_create(
domain, domain,
"test", "test",
platform.ENTITIES[device_class].unique_id, mock_sensor_entities[device_class].unique_id,
device_id=device_entry.id, device_id=device_entry.id,
) )

View File

@ -1,10 +1,6 @@
"""Provide a mock sensor platform. """Common test utilities for sensor entity component tests."""
Call init before using it in your tests to ensure clean test data.
"""
from homeassistant.components.sensor import ( from homeassistant.components.sensor import (
DEVICE_CLASSES,
RestoreSensor, RestoreSensor,
SensorDeviceClass, SensorDeviceClass,
SensorEntity, SensorEntity,
@ -24,8 +20,6 @@ from homeassistant.const import (
from tests.common import MockEntity from tests.common import MockEntity
DEVICE_CLASSES.append("none")
UNITS_OF_MEASUREMENT = { UNITS_OF_MEASUREMENT = {
SensorDeviceClass.APPARENT_POWER: UnitOfApparentPower.VOLT_AMPERE, # apparent power (VA) SensorDeviceClass.APPARENT_POWER: UnitOfApparentPower.VOLT_AMPERE, # apparent power (VA)
SensorDeviceClass.BATTERY: PERCENTAGE, # % of battery that is left SensorDeviceClass.BATTERY: PERCENTAGE, # % of battery that is left
@ -56,34 +50,6 @@ UNITS_OF_MEASUREMENT = {
SensorDeviceClass.GAS: UnitOfVolume.CUBIC_METERS, # gas (m³) 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): class MockSensor(MockEntity, SensorEntity):
"""Mock Sensor class.""" """Mock Sensor class."""
@ -141,3 +107,16 @@ class MockRestoreSensor(MockSensor, RestoreSensor):
self._values["native_unit_of_measurement"] = ( self._values["native_unit_of_measurement"] = (
last_sensor_data.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
}

View File

@ -26,8 +26,9 @@ from tests.common import (
async_get_device_automation_capabilities, async_get_device_automation_capabilities,
async_get_device_automations, async_get_device_automations,
async_mock_service, 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") @pytest.fixture(autouse=True, name="stub_blueprint_populate")
@ -85,11 +86,10 @@ async def test_get_conditions(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
enable_custom_integrations: None, mock_sensor_entities: dict[str, MockSensor],
) -> None: ) -> None:
"""Test we get the expected conditions from a sensor.""" """Test we get the expected conditions from a sensor."""
platform = getattr(hass.components, f"test.{DOMAIN}") setup_test_component_platform(hass, DOMAIN, mock_sensor_entities.values())
platform.init()
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}}) assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
await hass.async_block_till_done() await hass.async_block_till_done()
sensor_entries = {} sensor_entries = {}
@ -104,7 +104,7 @@ async def test_get_conditions(
sensor_entries[device_class] = entity_registry.async_get_or_create( sensor_entries[device_class] = entity_registry.async_get_or_create(
DOMAIN, DOMAIN,
"test", "test",
platform.ENTITIES[device_class].unique_id, mock_sensor_entities[device_class].unique_id,
device_id=device_entry.id, device_id=device_entry.id,
) )
@ -284,6 +284,7 @@ async def test_get_condition_capabilities(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
mock_sensor_entities: dict[str, MockSensor],
set_state, set_state,
device_class_reg, device_class_reg,
device_class_state, device_class_state,
@ -291,8 +292,7 @@ async def test_get_condition_capabilities(
unit_state, unit_state,
) -> None: ) -> None:
"""Test we get the expected capabilities from a sensor condition.""" """Test we get the expected capabilities from a sensor condition."""
platform = getattr(hass.components, f"test.{DOMAIN}") setup_test_component_platform(hass, DOMAIN, mock_sensor_entities.values())
platform.init()
config_entry = MockConfigEntry(domain="test", data={}) config_entry = MockConfigEntry(domain="test", data={})
config_entry.add_to_hass(hass) config_entry.add_to_hass(hass)
@ -303,7 +303,7 @@ async def test_get_condition_capabilities(
entity_id = entity_registry.async_get_or_create( entity_id = entity_registry.async_get_or_create(
DOMAIN, DOMAIN,
"test", "test",
platform.ENTITIES["battery"].unique_id, mock_sensor_entities["battery"].unique_id,
device_id=device_entry.id, device_id=device_entry.id,
original_device_class=device_class_reg, original_device_class=device_class_reg,
unit_of_measurement=unit_reg, unit_of_measurement=unit_reg,
@ -353,6 +353,7 @@ async def test_get_condition_capabilities_legacy(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
mock_sensor_entities: dict[str, MockSensor],
set_state, set_state,
device_class_reg, device_class_reg,
device_class_state, device_class_state,
@ -360,8 +361,7 @@ async def test_get_condition_capabilities_legacy(
unit_state, unit_state,
) -> None: ) -> None:
"""Test we get the expected capabilities from a sensor condition.""" """Test we get the expected capabilities from a sensor condition."""
platform = getattr(hass.components, f"test.{DOMAIN}") setup_test_component_platform(hass, DOMAIN, mock_sensor_entities.values())
platform.init()
config_entry = MockConfigEntry(domain="test", data={}) config_entry = MockConfigEntry(domain="test", data={})
config_entry.add_to_hass(hass) 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( entity_id = entity_registry.async_get_or_create(
DOMAIN, DOMAIN,
"test", "test",
platform.ENTITIES["battery"].unique_id, mock_sensor_entities["battery"].unique_id,
device_id=device_entry.id, device_id=device_entry.id,
original_device_class=device_class_reg, original_device_class=device_class_reg,
unit_of_measurement=unit_reg, unit_of_measurement=unit_reg,
@ -417,11 +417,13 @@ async def test_get_condition_capabilities_legacy(
async def test_get_condition_capabilities_none( async def test_get_condition_capabilities_none(
hass: HomeAssistant, hass: HomeAssistant,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
enable_custom_integrations: None,
) -> None: ) -> None:
"""Test we get the expected capabilities from a sensor condition.""" """Test we get the expected capabilities from a sensor condition."""
platform = getattr(hass.components, f"test.{DOMAIN}") entity = MockSensor(
platform.init() name="none sensor",
unique_id="unique_none",
)
setup_test_component_platform(hass, DOMAIN, [entity])
config_entry = MockConfigEntry(domain="test", data={}) config_entry = MockConfigEntry(domain="test", data={})
config_entry.add_to_hass(hass) 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( entry_none = entity_registry.async_get_or_create(
DOMAIN, DOMAIN,
"test", "test",
platform.ENTITIES["none"].unique_id, entity.unique_id,
) )
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}}) assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})

View File

@ -30,8 +30,9 @@ from tests.common import (
async_get_device_automation_capabilities, async_get_device_automation_capabilities,
async_get_device_automations, async_get_device_automations,
async_mock_service, 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") @pytest.fixture(autouse=True, name="stub_blueprint_populate")
@ -87,11 +88,10 @@ async def test_get_triggers(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
enable_custom_integrations: None, mock_sensor_entities: dict[str, MockSensor],
) -> None: ) -> None:
"""Test we get the expected triggers from a sensor.""" """Test we get the expected triggers from a sensor."""
platform = getattr(hass.components, f"test.{DOMAIN}") setup_test_component_platform(hass, DOMAIN, mock_sensor_entities.values())
platform.init()
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}}) assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
await hass.async_block_till_done() await hass.async_block_till_done()
sensor_entries: dict[SensorDeviceClass, er.RegistryEntry] = {} 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( sensor_entries[device_class] = entity_registry.async_get_or_create(
DOMAIN, DOMAIN,
"test", "test",
platform.ENTITIES[device_class].unique_id, mock_sensor_entities[device_class].unique_id,
device_id=device_entry.id, device_id=device_entry.id,
) )
@ -241,6 +241,7 @@ async def test_get_trigger_capabilities(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
mock_sensor_entities: dict[str, MockSensor],
set_state, set_state,
device_class_reg, device_class_reg,
device_class_state, device_class_state,
@ -248,8 +249,7 @@ async def test_get_trigger_capabilities(
unit_state, unit_state,
) -> None: ) -> None:
"""Test we get the expected capabilities from a sensor trigger.""" """Test we get the expected capabilities from a sensor trigger."""
platform = getattr(hass.components, f"test.{DOMAIN}") setup_test_component_platform(hass, DOMAIN, mock_sensor_entities)
platform.init()
config_entry = MockConfigEntry(domain="test", data={}) config_entry = MockConfigEntry(domain="test", data={})
config_entry.add_to_hass(hass) config_entry.add_to_hass(hass)
@ -260,7 +260,7 @@ async def test_get_trigger_capabilities(
entity_id = entity_registry.async_get_or_create( entity_id = entity_registry.async_get_or_create(
DOMAIN, DOMAIN,
"test", "test",
platform.ENTITIES["battery"].unique_id, mock_sensor_entities["battery"].unique_id,
device_id=device_entry.id, device_id=device_entry.id,
original_device_class=device_class_reg, original_device_class=device_class_reg,
unit_of_measurement=unit_reg, unit_of_measurement=unit_reg,
@ -311,6 +311,7 @@ async def test_get_trigger_capabilities_legacy(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
mock_sensor_entities: dict[str, MockSensor],
set_state, set_state,
device_class_reg, device_class_reg,
device_class_state, device_class_state,
@ -318,8 +319,7 @@ async def test_get_trigger_capabilities_legacy(
unit_state, unit_state,
) -> None: ) -> None:
"""Test we get the expected capabilities from a sensor trigger.""" """Test we get the expected capabilities from a sensor trigger."""
platform = getattr(hass.components, f"test.{DOMAIN}") setup_test_component_platform(hass, DOMAIN, mock_sensor_entities)
platform.init()
config_entry = MockConfigEntry(domain="test", data={}) config_entry = MockConfigEntry(domain="test", data={})
config_entry.add_to_hass(hass) 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( entity_id = entity_registry.async_get_or_create(
DOMAIN, DOMAIN,
"test", "test",
platform.ENTITIES["battery"].unique_id, mock_sensor_entities["battery"].unique_id,
device_id=device_entry.id, device_id=device_entry.id,
original_device_class=device_class_reg, original_device_class=device_class_reg,
unit_of_measurement=unit_reg, unit_of_measurement=unit_reg,
@ -374,11 +374,13 @@ async def test_get_trigger_capabilities_legacy(
async def test_get_trigger_capabilities_none( async def test_get_trigger_capabilities_none(
hass: HomeAssistant, hass: HomeAssistant,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
enable_custom_integrations: None,
) -> None: ) -> None:
"""Test we get the expected capabilities from a sensor trigger.""" """Test we get the expected capabilities from a sensor trigger."""
platform = getattr(hass.components, f"test.{DOMAIN}") entity = MockSensor(
platform.init() name="none sensor",
unique_id="unique_none",
)
setup_test_component_platform(hass, DOMAIN, [entity])
config_entry = MockConfigEntry(domain="test", data={}) config_entry = MockConfigEntry(domain="test", data={})
config_entry.add_to_hass(hass) 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( entry_none = entity_registry.async_get_or_create(
DOMAIN, DOMAIN,
"test", "test",
platform.ENTITIES["none"].unique_id, entity.unique_id,
) )
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}}) assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})

View File

@ -63,7 +63,9 @@ from tests.common import (
mock_integration, mock_integration,
mock_platform, mock_platform,
mock_restore_cache_with_extra_data, mock_restore_cache_with_extra_data,
setup_test_component_platform,
) )
from tests.components.sensor.common import MockRestoreSensor, MockSensor
TEST_DOMAIN = "test" TEST_DOMAIN = "test"
@ -103,7 +105,6 @@ TEST_DOMAIN = "test"
) )
async def test_temperature_conversion( async def test_temperature_conversion(
hass: HomeAssistant, hass: HomeAssistant,
enable_custom_integrations: None,
unit_system, unit_system,
native_unit, native_unit,
state_unit, state_unit,
@ -112,16 +113,14 @@ async def test_temperature_conversion(
) -> None: ) -> None:
"""Test temperature conversion.""" """Test temperature conversion."""
hass.config.units = unit_system hass.config.units = unit_system
platform = getattr(hass.components, "test.sensor") entity0 = MockSensor(
platform.init(empty=True)
platform.ENTITIES["0"] = platform.MockSensor(
name="Test", name="Test",
native_value=str(native_value), native_value=str(native_value),
native_unit_of_measurement=native_unit, native_unit_of_measurement=native_unit,
device_class=SensorDeviceClass.TEMPERATURE, device_class=SensorDeviceClass.TEMPERATURE,
) )
setup_test_component_platform(hass, sensor.DOMAIN, [entity0])
entity0 = platform.ENTITIES["0"]
assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}}) assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}})
await hass.async_block_till_done() await hass.async_block_till_done()
@ -132,19 +131,17 @@ async def test_temperature_conversion(
@pytest.mark.parametrize("device_class", [None, SensorDeviceClass.PRESSURE]) @pytest.mark.parametrize("device_class", [None, SensorDeviceClass.PRESSURE])
async def test_temperature_conversion_wrong_device_class( async def test_temperature_conversion_wrong_device_class(
hass: HomeAssistant, device_class, enable_custom_integrations: None hass: HomeAssistant, device_class
) -> None: ) -> None:
"""Test temperatures are not converted if the sensor has wrong device class.""" """Test temperatures are not converted if the sensor has wrong device class."""
platform = getattr(hass.components, "test.sensor") entity0 = MockSensor(
platform.init(empty=True)
platform.ENTITIES["0"] = platform.MockSensor(
name="Test", name="Test",
native_value="0.0", native_value="0.0",
native_unit_of_measurement=UnitOfTemperature.FAHRENHEIT, native_unit_of_measurement=UnitOfTemperature.FAHRENHEIT,
device_class=device_class, device_class=device_class,
) )
setup_test_component_platform(hass, sensor.DOMAIN, [entity0])
entity0 = platform.ENTITIES["0"]
assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}}) assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}})
await hass.async_block_till_done() await hass.async_block_till_done()
@ -158,21 +155,19 @@ async def test_temperature_conversion_wrong_device_class(
async def test_deprecated_last_reset( async def test_deprecated_last_reset(
hass: HomeAssistant, hass: HomeAssistant,
caplog: pytest.LogCaptureFixture, caplog: pytest.LogCaptureFixture,
enable_custom_integrations: None,
state_class, state_class,
) -> None: ) -> None:
"""Test warning on deprecated last reset.""" """Test warning on deprecated last reset."""
platform = getattr(hass.components, "test.sensor") entity0 = MockSensor(
platform.init(empty=True)
platform.ENTITIES["0"] = platform.MockSensor(
name="Test", state_class=state_class, last_reset=dt_util.utc_from_timestamp(0) name="Test", state_class=state_class, last_reset=dt_util.utc_from_timestamp(0)
) )
setup_test_component_platform(hass, sensor.DOMAIN, [entity0])
assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}}) assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}})
await hass.async_block_till_done() await hass.async_block_till_done()
assert ( assert (
"Entity sensor.test (<class 'custom_components.test.sensor.MockSensor'>) " "Entity sensor.test (<class 'tests.components.sensor.common.MockSensor'>) "
f"with state_class {state_class} has set last_reset. Setting last_reset for " f"with state_class {state_class} has set last_reset. Setting last_reset for "
"entities with state_class other than 'total' is not supported. Please update " "entities with state_class other than 'total' is not supported. Please update "
"your configuration if state_class is manually configured." "your configuration if state_class is manually configured."
@ -185,7 +180,6 @@ async def test_deprecated_last_reset(
async def test_datetime_conversion( async def test_datetime_conversion(
hass: HomeAssistant, hass: HomeAssistant,
caplog: pytest.LogCaptureFixture, caplog: pytest.LogCaptureFixture,
enable_custom_integrations: None,
) -> None: ) -> None:
"""Test conversion of datetime.""" """Test conversion of datetime."""
test_timestamp = datetime(2017, 12, 19, 18, 29, 42, tzinfo=UTC) test_timestamp = datetime(2017, 12, 19, 18, 29, 42, tzinfo=UTC)
@ -193,51 +187,49 @@ async def test_datetime_conversion(
dt_util.get_time_zone("Europe/Amsterdam") dt_util.get_time_zone("Europe/Amsterdam")
) )
test_date = date(2017, 12, 19) test_date = date(2017, 12, 19)
platform = getattr(hass.components, "test.sensor") entities = [
platform.init(empty=True) MockSensor(
platform.ENTITIES["0"] = platform.MockSensor(
name="Test", name="Test",
native_value=test_timestamp, native_value=test_timestamp,
device_class=SensorDeviceClass.TIMESTAMP, device_class=SensorDeviceClass.TIMESTAMP,
) ),
platform.ENTITIES["1"] = platform.MockSensor( MockSensor(
name="Test", native_value=test_date, device_class=SensorDeviceClass.DATE name="Test", native_value=test_date, device_class=SensorDeviceClass.DATE
) ),
platform.ENTITIES["2"] = platform.MockSensor( MockSensor(
name="Test", native_value=None, device_class=SensorDeviceClass.TIMESTAMP name="Test", native_value=None, device_class=SensorDeviceClass.TIMESTAMP
) ),
platform.ENTITIES["3"] = platform.MockSensor( MockSensor(name="Test", native_value=None, device_class=SensorDeviceClass.DATE),
name="Test", native_value=None, device_class=SensorDeviceClass.DATE MockSensor(
)
platform.ENTITIES["4"] = platform.MockSensor(
name="Test", name="Test",
native_value=test_local_timestamp, native_value=test_local_timestamp,
device_class=SensorDeviceClass.TIMESTAMP, device_class=SensorDeviceClass.TIMESTAMP,
) ),
]
setup_test_component_platform(hass, sensor.DOMAIN, entities)
assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}}) assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}})
await hass.async_block_till_done() await hass.async_block_till_done()
state = hass.states.get(platform.ENTITIES["0"].entity_id) state = hass.states.get(entities[0].entity_id)
assert state.state == test_timestamp.isoformat() assert state.state == test_timestamp.isoformat()
state = hass.states.get(platform.ENTITIES["1"].entity_id) state = hass.states.get(entities[1].entity_id)
assert state.state == test_date.isoformat() assert state.state == test_date.isoformat()
state = hass.states.get(platform.ENTITIES["2"].entity_id) state = hass.states.get(entities[2].entity_id)
assert state.state == STATE_UNKNOWN assert state.state == STATE_UNKNOWN
state = hass.states.get(platform.ENTITIES["3"].entity_id) state = hass.states.get(entities[3].entity_id)
assert state.state == STATE_UNKNOWN assert state.state == STATE_UNKNOWN
state = hass.states.get(platform.ENTITIES["4"].entity_id) state = hass.states.get(entities[4].entity_id)
assert state.state == test_timestamp.isoformat() assert state.state == test_timestamp.isoformat()
async def test_a_sensor_with_a_non_numeric_device_class( async def test_a_sensor_with_a_non_numeric_device_class(
hass: HomeAssistant, hass: HomeAssistant,
caplog: pytest.LogCaptureFixture, caplog: pytest.LogCaptureFixture,
enable_custom_integrations: None,
) -> None: ) -> None:
"""Test that a sensor with a non numeric device class will be non numeric. """Test that a sensor with a non numeric device class will be non numeric.
@ -249,29 +241,29 @@ async def test_a_sensor_with_a_non_numeric_device_class(
dt_util.get_time_zone("Europe/Amsterdam") dt_util.get_time_zone("Europe/Amsterdam")
) )
platform = getattr(hass.components, "test.sensor") entities = [
platform.init(empty=True) MockSensor(
platform.ENTITIES["0"] = platform.MockSensor(
name="Test", name="Test",
native_value=test_local_timestamp, native_value=test_local_timestamp,
native_unit_of_measurement="", native_unit_of_measurement="",
device_class=SensorDeviceClass.TIMESTAMP, device_class=SensorDeviceClass.TIMESTAMP,
) ),
MockSensor(
platform.ENTITIES["1"] = platform.MockSensor(
name="Test", name="Test",
native_value=test_local_timestamp, native_value=test_local_timestamp,
state_class="", state_class="",
device_class=SensorDeviceClass.TIMESTAMP, device_class=SensorDeviceClass.TIMESTAMP,
) ),
]
setup_test_component_platform(hass, sensor.DOMAIN, entities)
assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}}) assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}})
await hass.async_block_till_done() await hass.async_block_till_done()
state = hass.states.get(platform.ENTITIES["0"].entity_id) state = hass.states.get(entities[0].entity_id)
assert state.state == test_timestamp.isoformat() assert state.state == test_timestamp.isoformat()
state = hass.states.get(platform.ENTITIES["1"].entity_id) state = hass.states.get(entities[1].entity_id)
assert state.state == test_timestamp.isoformat() assert state.state == test_timestamp.isoformat()
@ -285,17 +277,15 @@ async def test_a_sensor_with_a_non_numeric_device_class(
async def test_deprecated_datetime_str( async def test_deprecated_datetime_str(
hass: HomeAssistant, hass: HomeAssistant,
caplog: pytest.LogCaptureFixture, caplog: pytest.LogCaptureFixture,
enable_custom_integrations: None,
device_class, device_class,
state_value, state_value,
provides, provides,
) -> None: ) -> None:
"""Test warning on deprecated str for a date(time) value.""" """Test warning on deprecated str for a date(time) value."""
platform = getattr(hass.components, "test.sensor") entity0 = MockSensor(
platform.init(empty=True)
platform.ENTITIES["0"] = platform.MockSensor(
name="Test", native_value=state_value, device_class=device_class name="Test", native_value=state_value, device_class=device_class
) )
setup_test_component_platform(hass, sensor.DOMAIN, [entity0])
assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}}) assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}})
await hass.async_block_till_done() await hass.async_block_till_done()
@ -309,17 +299,15 @@ async def test_deprecated_datetime_str(
async def test_reject_timezoneless_datetime_str( async def test_reject_timezoneless_datetime_str(
hass: HomeAssistant, hass: HomeAssistant,
caplog: pytest.LogCaptureFixture, caplog: pytest.LogCaptureFixture,
enable_custom_integrations: None,
) -> None: ) -> None:
"""Test rejection of timezone-less datetime objects as timestamp.""" """Test rejection of timezone-less datetime objects as timestamp."""
test_timestamp = datetime(2017, 12, 19, 18, 29, 42, tzinfo=None) test_timestamp = datetime(2017, 12, 19, 18, 29, 42, tzinfo=None)
platform = getattr(hass.components, "test.sensor") entity0 = MockSensor(
platform.init(empty=True)
platform.ENTITIES["0"] = platform.MockSensor(
name="Test", name="Test",
native_value=test_timestamp, native_value=test_timestamp,
device_class=SensorDeviceClass.TIMESTAMP, device_class=SensorDeviceClass.TIMESTAMP,
) )
setup_test_component_platform(hass, sensor.DOMAIN, [entity0])
assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}}) assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}})
await hass.async_block_till_done() await hass.async_block_till_done()
@ -403,7 +391,6 @@ RESTORE_DATA = {
) )
async def test_restore_sensor_save_state( async def test_restore_sensor_save_state(
hass: HomeAssistant, hass: HomeAssistant,
enable_custom_integrations: None,
hass_storage: dict[str, Any], hass_storage: dict[str, Any],
native_value, native_value,
native_value_type, native_value_type,
@ -412,16 +399,14 @@ async def test_restore_sensor_save_state(
uom, uom,
) -> None: ) -> None:
"""Test RestoreSensor.""" """Test RestoreSensor."""
platform = getattr(hass.components, "test.sensor") entity0 = MockRestoreSensor(
platform.init(empty=True)
platform.ENTITIES["0"] = platform.MockRestoreSensor(
name="Test", name="Test",
native_value=native_value, native_value=native_value,
native_unit_of_measurement=uom, native_unit_of_measurement=uom,
device_class=device_class, device_class=device_class,
) )
setup_test_component_platform(hass, sensor.DOMAIN, [entity0])
entity0 = platform.ENTITIES["0"]
assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}}) assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}})
await hass.async_block_till_done() await hass.async_block_till_done()
@ -472,7 +457,6 @@ async def test_restore_sensor_save_state(
) )
async def test_restore_sensor_restore_state( async def test_restore_sensor_restore_state(
hass: HomeAssistant, hass: HomeAssistant,
enable_custom_integrations: None,
hass_storage: dict[str, Any], hass_storage: dict[str, Any],
native_value, native_value,
native_value_type, native_value_type,
@ -483,14 +467,12 @@ async def test_restore_sensor_restore_state(
"""Test RestoreSensor.""" """Test RestoreSensor."""
mock_restore_cache_with_extra_data(hass, ((State("sensor.test", ""), extra_data),)) mock_restore_cache_with_extra_data(hass, ((State("sensor.test", ""), extra_data),))
platform = getattr(hass.components, "test.sensor") entity0 = MockRestoreSensor(
platform.init(empty=True)
platform.ENTITIES["0"] = platform.MockRestoreSensor(
name="Test", name="Test",
device_class=device_class, device_class=device_class,
) )
setup_test_component_platform(hass, sensor.DOMAIN, [entity0])
entity0 = platform.ENTITIES["0"]
assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}}) assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}})
await hass.async_block_till_done() await hass.async_block_till_done()
@ -621,7 +603,6 @@ async def test_restore_sensor_restore_state(
) )
async def test_custom_unit( async def test_custom_unit(
hass: HomeAssistant, hass: HomeAssistant,
enable_custom_integrations: None,
device_class, device_class,
native_unit, native_unit,
custom_unit, custom_unit,
@ -638,17 +619,15 @@ async def test_custom_unit(
) )
await hass.async_block_till_done() await hass.async_block_till_done()
platform = getattr(hass.components, "test.sensor") entity0 = MockSensor(
platform.init(empty=True)
platform.ENTITIES["0"] = platform.MockSensor(
name="Test", name="Test",
native_value=str(native_value), native_value=str(native_value),
native_unit_of_measurement=native_unit, native_unit_of_measurement=native_unit,
device_class=device_class, device_class=device_class,
unique_id="very_unique", unique_id="very_unique",
) )
setup_test_component_platform(hass, sensor.DOMAIN, [entity0])
entity0 = platform.ENTITIES["0"]
assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}}) assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}})
await hass.async_block_till_done() await hass.async_block_till_done()
@ -884,7 +863,6 @@ async def test_custom_unit(
) )
async def test_custom_unit_change( async def test_custom_unit_change(
hass: HomeAssistant, hass: HomeAssistant,
enable_custom_integrations: None,
native_unit, native_unit,
custom_unit, custom_unit,
state_unit, state_unit,
@ -895,17 +873,15 @@ async def test_custom_unit_change(
) -> None: ) -> None:
"""Test custom unit changes are picked up.""" """Test custom unit changes are picked up."""
entity_registry = er.async_get(hass) entity_registry = er.async_get(hass)
platform = getattr(hass.components, "test.sensor") entity0 = MockSensor(
platform.init(empty=True)
platform.ENTITIES["0"] = platform.MockSensor(
name="Test", name="Test",
native_value=str(native_value), native_value=str(native_value),
native_unit_of_measurement=native_unit, native_unit_of_measurement=native_unit,
device_class=device_class, device_class=device_class,
unique_id="very_unique", unique_id="very_unique",
) )
setup_test_component_platform(hass, sensor.DOMAIN, [entity0])
entity0 = platform.ENTITIES["0"]
assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}}) assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}})
await hass.async_block_till_done() await hass.async_block_till_done()
@ -972,7 +948,6 @@ async def test_custom_unit_change(
) )
async def test_unit_conversion_priority( async def test_unit_conversion_priority(
hass: HomeAssistant, hass: HomeAssistant,
enable_custom_integrations: None,
unit_system, unit_system,
native_unit, native_unit,
automatic_unit, automatic_unit,
@ -990,27 +965,21 @@ async def test_unit_conversion_priority(
hass.config.units = unit_system hass.config.units = unit_system
entity_registry = er.async_get(hass) entity_registry = er.async_get(hass)
platform = getattr(hass.components, "test.sensor")
platform.init(empty=True)
platform.ENTITIES["0"] = platform.MockSensor( entity0 = MockSensor(
name="Test", name="Test",
device_class=device_class, device_class=device_class,
native_unit_of_measurement=native_unit, native_unit_of_measurement=native_unit,
native_value=str(native_value), native_value=str(native_value),
unique_id="very_unique", unique_id="very_unique",
) )
entity0 = platform.ENTITIES["0"] entity1 = MockSensor(
platform.ENTITIES["1"] = platform.MockSensor(
name="Test", name="Test",
device_class=device_class, device_class=device_class,
native_unit_of_measurement=native_unit, native_unit_of_measurement=native_unit,
native_value=str(native_value), native_value=str(native_value),
) )
entity1 = platform.ENTITIES["1"] entity2 = MockSensor(
platform.ENTITIES["2"] = platform.MockSensor(
name="Test", name="Test",
device_class=device_class, device_class=device_class,
native_unit_of_measurement=native_unit, native_unit_of_measurement=native_unit,
@ -1018,16 +987,23 @@ async def test_unit_conversion_priority(
suggested_unit_of_measurement=suggested_unit, suggested_unit_of_measurement=suggested_unit,
unique_id="very_unique_2", unique_id="very_unique_2",
) )
entity2 = platform.ENTITIES["2"] entity3 = MockSensor(
platform.ENTITIES["3"] = platform.MockSensor(
name="Test", name="Test",
device_class=device_class, device_class=device_class,
native_unit_of_measurement=native_unit, native_unit_of_measurement=native_unit,
native_value=str(native_value), native_value=str(native_value),
suggested_unit_of_measurement=suggested_unit, suggested_unit_of_measurement=suggested_unit,
) )
entity3 = platform.ENTITIES["3"] setup_test_component_platform(
hass,
sensor.DOMAIN,
[
entity0,
entity1,
entity2,
entity3,
],
)
assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}}) assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}})
await hass.async_block_till_done() await hass.async_block_till_done()
@ -1119,7 +1095,6 @@ async def test_unit_conversion_priority(
) )
async def test_unit_conversion_priority_precision( async def test_unit_conversion_priority_precision(
hass: HomeAssistant, hass: HomeAssistant,
enable_custom_integrations: None,
unit_system, unit_system,
native_unit, native_unit,
automatic_unit, automatic_unit,
@ -1138,10 +1113,8 @@ async def test_unit_conversion_priority_precision(
hass.config.units = unit_system hass.config.units = unit_system
entity_registry = er.async_get(hass) entity_registry = er.async_get(hass)
platform = getattr(hass.components, "test.sensor")
platform.init(empty=True)
platform.ENTITIES["0"] = platform.MockSensor( entity0 = MockSensor(
name="Test", name="Test",
device_class=device_class, device_class=device_class,
native_unit_of_measurement=native_unit, native_unit_of_measurement=native_unit,
@ -1149,18 +1122,14 @@ async def test_unit_conversion_priority_precision(
suggested_display_precision=suggested_precision, suggested_display_precision=suggested_precision,
unique_id="very_unique", unique_id="very_unique",
) )
entity0 = platform.ENTITIES["0"] entity1 = MockSensor(
platform.ENTITIES["1"] = platform.MockSensor(
name="Test", name="Test",
device_class=device_class, device_class=device_class,
native_unit_of_measurement=native_unit, native_unit_of_measurement=native_unit,
native_value=str(native_value), native_value=str(native_value),
suggested_display_precision=suggested_precision, suggested_display_precision=suggested_precision,
) )
entity1 = platform.ENTITIES["1"] entity2 = MockSensor(
platform.ENTITIES["2"] = platform.MockSensor(
name="Test", name="Test",
device_class=device_class, device_class=device_class,
native_unit_of_measurement=native_unit, native_unit_of_measurement=native_unit,
@ -1169,9 +1138,7 @@ async def test_unit_conversion_priority_precision(
suggested_unit_of_measurement=suggested_unit, suggested_unit_of_measurement=suggested_unit,
unique_id="very_unique_2", unique_id="very_unique_2",
) )
entity2 = platform.ENTITIES["2"] entity3 = MockSensor(
platform.ENTITIES["3"] = platform.MockSensor(
name="Test", name="Test",
device_class=device_class, device_class=device_class,
native_unit_of_measurement=native_unit, native_unit_of_measurement=native_unit,
@ -1179,7 +1146,16 @@ async def test_unit_conversion_priority_precision(
suggested_display_precision=suggested_precision, suggested_display_precision=suggested_precision,
suggested_unit_of_measurement=suggested_unit, suggested_unit_of_measurement=suggested_unit,
) )
entity3 = platform.ENTITIES["3"] setup_test_component_platform(
hass,
sensor.DOMAIN,
[
entity0,
entity1,
entity2,
entity3,
],
)
assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}}) assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}})
await hass.async_block_till_done() await hass.async_block_till_done()
@ -1280,7 +1256,6 @@ async def test_unit_conversion_priority_precision(
) )
async def test_unit_conversion_priority_suggested_unit_change( async def test_unit_conversion_priority_suggested_unit_change(
hass: HomeAssistant, hass: HomeAssistant,
enable_custom_integrations: None,
unit_system, unit_system,
native_unit, native_unit,
original_unit, original_unit,
@ -1294,8 +1269,6 @@ async def test_unit_conversion_priority_suggested_unit_change(
hass.config.units = unit_system hass.config.units = unit_system
entity_registry = er.async_get(hass) entity_registry = er.async_get(hass)
platform = getattr(hass.components, "test.sensor")
platform.init(empty=True)
# Pre-register entities # Pre-register entities
entry = entity_registry.async_get_or_create( entry = entity_registry.async_get_or_create(
@ -1315,16 +1288,14 @@ async def test_unit_conversion_priority_suggested_unit_change(
{"suggested_unit_of_measurement": original_unit}, {"suggested_unit_of_measurement": original_unit},
) )
platform.ENTITIES["0"] = platform.MockSensor( entity0 = MockSensor(
name="Test", name="Test",
device_class=device_class, device_class=device_class,
native_unit_of_measurement=native_unit, native_unit_of_measurement=native_unit,
native_value=str(native_value), native_value=str(native_value),
unique_id="very_unique", unique_id="very_unique",
) )
entity0 = platform.ENTITIES["0"] entity1 = MockSensor(
platform.ENTITIES["1"] = platform.MockSensor(
name="Test", name="Test",
device_class=device_class, device_class=device_class,
native_unit_of_measurement=native_unit, native_unit_of_measurement=native_unit,
@ -1332,7 +1303,7 @@ async def test_unit_conversion_priority_suggested_unit_change(
suggested_unit_of_measurement=suggested_unit, suggested_unit_of_measurement=suggested_unit,
unique_id="very_unique_2", unique_id="very_unique_2",
) )
entity1 = platform.ENTITIES["1"] setup_test_component_platform(hass, sensor.DOMAIN, [entity0, entity1])
assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}}) assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}})
await hass.async_block_till_done() await hass.async_block_till_done()
@ -1392,7 +1363,6 @@ async def test_unit_conversion_priority_suggested_unit_change(
) )
async def test_unit_conversion_priority_suggested_unit_change_2( async def test_unit_conversion_priority_suggested_unit_change_2(
hass: HomeAssistant, hass: HomeAssistant,
enable_custom_integrations: None,
native_unit_1, native_unit_1,
native_unit_2, native_unit_2,
suggested_unit, suggested_unit,
@ -1405,8 +1375,6 @@ async def test_unit_conversion_priority_suggested_unit_change_2(
hass.config.units = METRIC_SYSTEM hass.config.units = METRIC_SYSTEM
entity_registry = er.async_get(hass) entity_registry = er.async_get(hass)
platform = getattr(hass.components, "test.sensor")
platform.init(empty=True)
# Pre-register entities # Pre-register entities
entity_registry.async_get_or_create( entity_registry.async_get_or_create(
@ -1416,16 +1384,14 @@ async def test_unit_conversion_priority_suggested_unit_change_2(
"sensor", "test", "very_unique_2", unit_of_measurement=native_unit_1 "sensor", "test", "very_unique_2", unit_of_measurement=native_unit_1
) )
platform.ENTITIES["0"] = platform.MockSensor( entity0 = MockSensor(
name="Test", name="Test",
device_class=device_class, device_class=device_class,
native_unit_of_measurement=native_unit_2, native_unit_of_measurement=native_unit_2,
native_value=str(native_value), native_value=str(native_value),
unique_id="very_unique", unique_id="very_unique",
) )
entity0 = platform.ENTITIES["0"] entity1 = MockSensor(
platform.ENTITIES["1"] = platform.MockSensor(
name="Test", name="Test",
device_class=device_class, device_class=device_class,
native_unit_of_measurement=native_unit_2, native_unit_of_measurement=native_unit_2,
@ -1433,7 +1399,7 @@ async def test_unit_conversion_priority_suggested_unit_change_2(
suggested_unit_of_measurement=suggested_unit, suggested_unit_of_measurement=suggested_unit,
unique_id="very_unique_2", unique_id="very_unique_2",
) )
entity1 = platform.ENTITIES["1"] setup_test_component_platform(hass, sensor.DOMAIN, [entity0, entity1])
assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}}) assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}})
await hass.async_block_till_done() await hass.async_block_till_done()
@ -1496,7 +1462,6 @@ async def test_unit_conversion_priority_suggested_unit_change_2(
) )
async def test_suggested_precision_option( async def test_suggested_precision_option(
hass: HomeAssistant, hass: HomeAssistant,
enable_custom_integrations: None,
unit_system, unit_system,
native_unit, native_unit,
integration_suggested_precision, integration_suggested_precision,
@ -1510,10 +1475,7 @@ async def test_suggested_precision_option(
hass.config.units = unit_system hass.config.units = unit_system
entity_registry = er.async_get(hass) entity_registry = er.async_get(hass)
platform = getattr(hass.components, "test.sensor") entity0 = MockSensor(
platform.init(empty=True)
platform.ENTITIES["0"] = platform.MockSensor(
name="Test", name="Test",
device_class=device_class, device_class=device_class,
native_unit_of_measurement=native_unit, native_unit_of_measurement=native_unit,
@ -1521,7 +1483,7 @@ async def test_suggested_precision_option(
suggested_display_precision=integration_suggested_precision, suggested_display_precision=integration_suggested_precision,
unique_id="very_unique", unique_id="very_unique",
) )
entity0 = platform.ENTITIES["0"] setup_test_component_platform(hass, sensor.DOMAIN, [entity0])
assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}}) assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}})
await hass.async_block_till_done() await hass.async_block_till_done()
@ -1574,7 +1536,6 @@ async def test_suggested_precision_option(
) )
async def test_suggested_precision_option_update( async def test_suggested_precision_option_update(
hass: HomeAssistant, hass: HomeAssistant,
enable_custom_integrations: None,
unit_system, unit_system,
native_unit, native_unit,
suggested_unit, suggested_unit,
@ -1590,8 +1551,6 @@ async def test_suggested_precision_option_update(
hass.config.units = unit_system hass.config.units = unit_system
entity_registry = er.async_get(hass) entity_registry = er.async_get(hass)
platform = getattr(hass.components, "test.sensor")
platform.init(empty=True)
# Pre-register entities # Pre-register entities
entry = entity_registry.async_get_or_create("sensor", "test", "very_unique") entry = entity_registry.async_get_or_create("sensor", "test", "very_unique")
@ -1610,7 +1569,7 @@ async def test_suggested_precision_option_update(
}, },
) )
platform.ENTITIES["0"] = platform.MockSensor( entity0 = MockSensor(
name="Test", name="Test",
device_class=device_class, device_class=device_class,
native_unit_of_measurement=native_unit, native_unit_of_measurement=native_unit,
@ -1618,7 +1577,7 @@ async def test_suggested_precision_option_update(
suggested_display_precision=new_precision, suggested_display_precision=new_precision,
unique_id="very_unique", unique_id="very_unique",
) )
entity0 = platform.ENTITIES["0"] setup_test_component_platform(hass, sensor.DOMAIN, [entity0])
assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}}) assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}})
await hass.async_block_till_done() await hass.async_block_till_done()
@ -1666,7 +1625,6 @@ async def test_suggested_precision_option_update(
) )
async def test_unit_conversion_priority_legacy_conversion_removed( async def test_unit_conversion_priority_legacy_conversion_removed(
hass: HomeAssistant, hass: HomeAssistant,
enable_custom_integrations: None,
unit_system, unit_system,
native_unit, native_unit,
original_unit, original_unit,
@ -1679,22 +1637,20 @@ async def test_unit_conversion_priority_legacy_conversion_removed(
hass.config.units = unit_system hass.config.units = unit_system
entity_registry = er.async_get(hass) entity_registry = er.async_get(hass)
platform = getattr(hass.components, "test.sensor")
platform.init(empty=True)
# Pre-register entities # Pre-register entities
entity_registry.async_get_or_create( entity_registry.async_get_or_create(
"sensor", "test", "very_unique", unit_of_measurement=original_unit "sensor", "test", "very_unique", unit_of_measurement=original_unit
) )
platform.ENTITIES["0"] = platform.MockSensor( entity0 = MockSensor(
name="Test", name="Test",
device_class=device_class, device_class=device_class,
native_unit_of_measurement=native_unit, native_unit_of_measurement=native_unit,
native_value=str(native_value), native_value=str(native_value),
unique_id="very_unique", unique_id="very_unique",
) )
entity0 = platform.ENTITIES["0"] setup_test_component_platform(hass, sensor.DOMAIN, [entity0])
assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}}) assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}})
await hass.async_block_till_done() await hass.async_block_till_done()
@ -1715,17 +1671,15 @@ def test_device_classes_aligned() -> None:
async def test_value_unknown_in_enumeration( async def test_value_unknown_in_enumeration(
hass: HomeAssistant, hass: HomeAssistant,
caplog: pytest.LogCaptureFixture, caplog: pytest.LogCaptureFixture,
enable_custom_integrations: None,
) -> None: ) -> None:
"""Test warning on invalid enum value.""" """Test warning on invalid enum value."""
platform = getattr(hass.components, "test.sensor") entity0 = MockSensor(
platform.init(empty=True)
platform.ENTITIES["0"] = platform.MockSensor(
name="Test", name="Test",
native_value="invalid_option", native_value="invalid_option",
device_class=SensorDeviceClass.ENUM, device_class=SensorDeviceClass.ENUM,
options=["option1", "option2"], options=["option1", "option2"],
) )
setup_test_component_platform(hass, sensor.DOMAIN, [entity0])
assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}}) assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}})
await hass.async_block_till_done() await hass.async_block_till_done()
@ -1739,17 +1693,15 @@ async def test_value_unknown_in_enumeration(
async def test_invalid_enumeration_entity_with_device_class( async def test_invalid_enumeration_entity_with_device_class(
hass: HomeAssistant, hass: HomeAssistant,
caplog: pytest.LogCaptureFixture, caplog: pytest.LogCaptureFixture,
enable_custom_integrations: None,
) -> None: ) -> None:
"""Test warning on entities that provide an enum with a device class.""" """Test warning on entities that provide an enum with a device class."""
platform = getattr(hass.components, "test.sensor") entity0 = MockSensor(
platform.init(empty=True)
platform.ENTITIES["0"] = platform.MockSensor(
name="Test", name="Test",
native_value=21, native_value=21,
device_class=SensorDeviceClass.POWER, device_class=SensorDeviceClass.POWER,
options=["option1", "option2"], options=["option1", "option2"],
) )
setup_test_component_platform(hass, sensor.DOMAIN, [entity0])
assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}}) assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}})
await hass.async_block_till_done() await hass.async_block_till_done()
@ -1763,16 +1715,14 @@ async def test_invalid_enumeration_entity_with_device_class(
async def test_invalid_enumeration_entity_without_device_class( async def test_invalid_enumeration_entity_without_device_class(
hass: HomeAssistant, hass: HomeAssistant,
caplog: pytest.LogCaptureFixture, caplog: pytest.LogCaptureFixture,
enable_custom_integrations: None,
) -> None: ) -> None:
"""Test warning on entities that provide an enum without a device class.""" """Test warning on entities that provide an enum without a device class."""
platform = getattr(hass.components, "test.sensor") entity0 = MockSensor(
platform.init(empty=True)
platform.ENTITIES["0"] = platform.MockSensor(
name="Test", name="Test",
native_value=21, native_value=21,
options=["option1", "option2"], options=["option1", "option2"],
) )
setup_test_component_platform(hass, sensor.DOMAIN, [entity0])
assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}}) assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}})
await hass.async_block_till_done() await hass.async_block_till_done()
@ -1794,19 +1744,17 @@ async def test_invalid_enumeration_entity_without_device_class(
async def test_non_numeric_device_class_with_unit_of_measurement( async def test_non_numeric_device_class_with_unit_of_measurement(
hass: HomeAssistant, hass: HomeAssistant,
caplog: pytest.LogCaptureFixture, caplog: pytest.LogCaptureFixture,
enable_custom_integrations: None,
device_class: SensorDeviceClass, device_class: SensorDeviceClass,
) -> None: ) -> None:
"""Test error on numeric entities that provide an unit of measurement.""" """Test error on numeric entities that provide an unit of measurement."""
platform = getattr(hass.components, "test.sensor") entity0 = MockSensor(
platform.init(empty=True)
platform.ENTITIES["0"] = platform.MockSensor(
name="Test", name="Test",
native_value=None, native_value=None,
device_class=device_class, device_class=device_class,
native_unit_of_measurement=UnitOfTemperature.CELSIUS, native_unit_of_measurement=UnitOfTemperature.CELSIUS,
options=["option1", "option2"], options=["option1", "option2"],
) )
setup_test_component_platform(hass, sensor.DOMAIN, [entity0])
assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}}) assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}})
await hass.async_block_till_done() await hass.async_block_till_done()
@ -1869,18 +1817,16 @@ async def test_non_numeric_device_class_with_unit_of_measurement(
async def test_device_classes_with_invalid_unit_of_measurement( async def test_device_classes_with_invalid_unit_of_measurement(
hass: HomeAssistant, hass: HomeAssistant,
caplog: pytest.LogCaptureFixture, caplog: pytest.LogCaptureFixture,
enable_custom_integrations: None,
device_class: SensorDeviceClass, device_class: SensorDeviceClass,
) -> None: ) -> None:
"""Test error when unit of measurement is not valid for used device class.""" """Test error when unit of measurement is not valid for used device class."""
platform = getattr(hass.components, "test.sensor") entity0 = MockSensor(
platform.init(empty=True)
platform.ENTITIES["0"] = platform.MockSensor(
name="Test", name="Test",
native_value="1.0", native_value="1.0",
device_class=device_class, device_class=device_class,
native_unit_of_measurement="INVALID!", native_unit_of_measurement="INVALID!",
) )
setup_test_component_platform(hass, sensor.DOMAIN, [entity0])
units = [ units = [
str(unit) if unit else "no unit of measurement" str(unit) if unit else "no unit of measurement"
for unit in DEVICE_CLASS_UNITS.get(device_class, set()) for unit in DEVICE_CLASS_UNITS.get(device_class, set())
@ -1920,7 +1866,6 @@ async def test_device_classes_with_invalid_unit_of_measurement(
async def test_non_numeric_validation_error( async def test_non_numeric_validation_error(
hass: HomeAssistant, hass: HomeAssistant,
caplog: pytest.LogCaptureFixture, caplog: pytest.LogCaptureFixture,
enable_custom_integrations: None,
native_value: Any, native_value: Any,
problem: str, problem: str,
device_class: SensorDeviceClass | None, device_class: SensorDeviceClass | None,
@ -1928,16 +1873,14 @@ async def test_non_numeric_validation_error(
unit: str | None, unit: str | None,
) -> None: ) -> None:
"""Test error on expected numeric entities.""" """Test error on expected numeric entities."""
platform = getattr(hass.components, "test.sensor") entity0 = MockSensor(
platform.init(empty=True)
platform.ENTITIES["0"] = platform.MockSensor(
name="Test", name="Test",
native_value=native_value, native_value=native_value,
device_class=device_class, device_class=device_class,
native_unit_of_measurement=unit, native_unit_of_measurement=unit,
state_class=state_class, state_class=state_class,
) )
entity0 = platform.ENTITIES["0"] setup_test_component_platform(hass, sensor.DOMAIN, [entity0])
assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}}) assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}})
await hass.async_block_till_done() await hass.async_block_till_done()
@ -1966,7 +1909,6 @@ async def test_non_numeric_validation_error(
async def test_non_numeric_validation_raise( async def test_non_numeric_validation_raise(
hass: HomeAssistant, hass: HomeAssistant,
caplog: pytest.LogCaptureFixture, caplog: pytest.LogCaptureFixture,
enable_custom_integrations: None,
native_value: Any, native_value: Any,
expected: str, expected: str,
device_class: SensorDeviceClass | None, device_class: SensorDeviceClass | None,
@ -1975,9 +1917,7 @@ async def test_non_numeric_validation_raise(
precision, precision,
) -> None: ) -> None:
"""Test error on expected numeric entities.""" """Test error on expected numeric entities."""
platform = getattr(hass.components, "test.sensor") entity0 = MockSensor(
platform.init(empty=True)
platform.ENTITIES["0"] = platform.MockSensor(
name="Test", name="Test",
device_class=device_class, device_class=device_class,
native_unit_of_measurement=unit, native_unit_of_measurement=unit,
@ -1985,7 +1925,7 @@ async def test_non_numeric_validation_raise(
state_class=state_class, state_class=state_class,
suggested_display_precision=precision, suggested_display_precision=precision,
) )
entity0 = platform.ENTITIES["0"] setup_test_component_platform(hass, sensor.DOMAIN, [entity0])
assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}}) assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}})
await hass.async_block_till_done() await hass.async_block_till_done()
@ -2018,7 +1958,6 @@ async def test_non_numeric_validation_raise(
async def test_numeric_validation( async def test_numeric_validation(
hass: HomeAssistant, hass: HomeAssistant,
caplog: pytest.LogCaptureFixture, caplog: pytest.LogCaptureFixture,
enable_custom_integrations: None,
native_value: Any, native_value: Any,
expected: str, expected: str,
device_class: SensorDeviceClass | None, device_class: SensorDeviceClass | None,
@ -2026,16 +1965,14 @@ async def test_numeric_validation(
unit: str | None, unit: str | None,
) -> None: ) -> None:
"""Test does not error on expected numeric entities.""" """Test does not error on expected numeric entities."""
platform = getattr(hass.components, "test.sensor") entity0 = MockSensor(
platform.init(empty=True)
platform.ENTITIES["0"] = platform.MockSensor(
name="Test", name="Test",
native_value=native_value, native_value=native_value,
device_class=device_class, device_class=device_class,
native_unit_of_measurement=unit, native_unit_of_measurement=unit,
state_class=state_class, state_class=state_class,
) )
entity0 = platform.ENTITIES["0"] setup_test_component_platform(hass, sensor.DOMAIN, [entity0])
assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}}) assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}})
await hass.async_block_till_done() await hass.async_block_till_done()
@ -2052,18 +1989,15 @@ async def test_numeric_validation(
async def test_numeric_validation_ignores_custom_device_class( async def test_numeric_validation_ignores_custom_device_class(
hass: HomeAssistant, hass: HomeAssistant,
caplog: pytest.LogCaptureFixture, caplog: pytest.LogCaptureFixture,
enable_custom_integrations: None,
) -> None: ) -> None:
"""Test does not error on expected numeric entities.""" """Test does not error on expected numeric entities."""
native_value = "Three elephants" native_value = "Three elephants"
platform = getattr(hass.components, "test.sensor") entity0 = MockSensor(
platform.init(empty=True)
platform.ENTITIES["0"] = platform.MockSensor(
name="Test", name="Test",
native_value=native_value, native_value=native_value,
device_class="custom__deviceclass", device_class="custom__deviceclass",
) )
entity0 = platform.ENTITIES["0"] setup_test_component_platform(hass, sensor.DOMAIN, [entity0])
assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}}) assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}})
await hass.async_block_till_done() await hass.async_block_till_done()
@ -2084,18 +2018,16 @@ async def test_numeric_validation_ignores_custom_device_class(
async def test_device_classes_with_invalid_state_class( async def test_device_classes_with_invalid_state_class(
hass: HomeAssistant, hass: HomeAssistant,
caplog: pytest.LogCaptureFixture, caplog: pytest.LogCaptureFixture,
enable_custom_integrations: None,
device_class: SensorDeviceClass, device_class: SensorDeviceClass,
) -> None: ) -> None:
"""Test error when unit of measurement is not valid for used device class.""" """Test error when unit of measurement is not valid for used device class."""
platform = getattr(hass.components, "test.sensor") entity0 = MockSensor(
platform.init(empty=True)
platform.ENTITIES["0"] = platform.MockSensor(
name="Test", name="Test",
native_value=None, native_value=None,
state_class="INVALID!", state_class="INVALID!",
device_class=device_class, device_class=device_class,
) )
setup_test_component_platform(hass, sensor.DOMAIN, [entity0])
assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}}) assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}})
await hass.async_block_till_done() await hass.async_block_till_done()
@ -2133,7 +2065,6 @@ async def test_device_classes_with_invalid_state_class(
async def test_numeric_state_expected_helper( async def test_numeric_state_expected_helper(
hass: HomeAssistant, hass: HomeAssistant,
caplog: pytest.LogCaptureFixture, caplog: pytest.LogCaptureFixture,
enable_custom_integrations: None,
device_class: SensorDeviceClass | None, device_class: SensorDeviceClass | None,
state_class: SensorStateClass | None, state_class: SensorStateClass | None,
native_unit_of_measurement: str | None, native_unit_of_measurement: str | None,
@ -2141,9 +2072,7 @@ async def test_numeric_state_expected_helper(
is_numeric: bool, is_numeric: bool,
) -> None: ) -> None:
"""Test numeric_state_expected helper.""" """Test numeric_state_expected helper."""
platform = getattr(hass.components, "test.sensor") entity0 = MockSensor(
platform.init(empty=True)
platform.ENTITIES["0"] = platform.MockSensor(
name="Test", name="Test",
native_value=None, native_value=None,
device_class=device_class, device_class=device_class,
@ -2151,11 +2080,11 @@ async def test_numeric_state_expected_helper(
native_unit_of_measurement=native_unit_of_measurement, native_unit_of_measurement=native_unit_of_measurement,
suggested_display_precision=suggested_precision, suggested_display_precision=suggested_precision,
) )
setup_test_component_platform(hass, sensor.DOMAIN, [entity0])
assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}}) assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}})
await hass.async_block_till_done() await hass.async_block_till_done()
entity0 = platform.ENTITIES["0"]
state = hass.states.get(entity0.entity_id) state = hass.states.get(entity0.entity_id)
assert state is not None assert state is not None
@ -2199,7 +2128,6 @@ async def test_numeric_state_expected_helper(
) )
async def test_unit_conversion_update( async def test_unit_conversion_update(
hass: HomeAssistant, hass: HomeAssistant,
enable_custom_integrations: None,
unit_system_1, unit_system_1,
unit_system_2, unit_system_2,
native_unit, native_unit,
@ -2219,9 +2147,8 @@ async def test_unit_conversion_update(
hass.config.units = unit_system_1 hass.config.units = unit_system_1
entity_registry = er.async_get(hass) entity_registry = er.async_get(hass)
platform = getattr(hass.components, "test.sensor")
entity0 = platform.MockSensor( entity0 = MockSensor(
name="Test 0", name="Test 0",
device_class=device_class, device_class=device_class,
native_unit_of_measurement=native_unit, native_unit_of_measurement=native_unit,
@ -2229,7 +2156,7 @@ async def test_unit_conversion_update(
unique_id="very_unique", unique_id="very_unique",
) )
entity1 = platform.MockSensor( entity1 = MockSensor(
name="Test 1", name="Test 1",
device_class=device_class, device_class=device_class,
native_unit_of_measurement=native_unit, native_unit_of_measurement=native_unit,
@ -2237,7 +2164,7 @@ async def test_unit_conversion_update(
unique_id="very_unique_1", unique_id="very_unique_1",
) )
entity2 = platform.MockSensor( entity2 = MockSensor(
name="Test 2", name="Test 2",
device_class=device_class, device_class=device_class,
native_unit_of_measurement=native_unit, native_unit_of_measurement=native_unit,
@ -2246,7 +2173,7 @@ async def test_unit_conversion_update(
unique_id="very_unique_2", unique_id="very_unique_2",
) )
entity3 = platform.MockSensor( entity3 = MockSensor(
name="Test 3", name="Test 3",
device_class=device_class, device_class=device_class,
native_unit_of_measurement=native_unit, native_unit_of_measurement=native_unit,
@ -2255,7 +2182,7 @@ async def test_unit_conversion_update(
unique_id="very_unique_3", unique_id="very_unique_3",
) )
entity4 = platform.MockSensor( entity4 = MockSensor(
name="Test 4", name="Test 4",
device_class=device_class, device_class=device_class,
native_unit_of_measurement=native_unit, native_unit_of_measurement=native_unit,
@ -2544,11 +2471,8 @@ async def test_entity_category_config_raises_error(
caplog: pytest.LogCaptureFixture, caplog: pytest.LogCaptureFixture,
) -> None: ) -> None:
"""Test error is raised when entity category is set to config.""" """Test error is raised when entity category is set to config."""
platform = getattr(hass.components, "test.sensor") entity0 = MockSensor(name="Test", entity_category=EntityCategory.CONFIG)
platform.init(empty=True) setup_test_component_platform(hass, sensor.DOMAIN, [entity0])
platform.ENTITIES["0"] = platform.MockSensor(
name="Test", entity_category=EntityCategory.CONFIG
)
assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}}) assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}})
await hass.async_block_till_done() await hass.async_block_till_done()
@ -2644,13 +2568,11 @@ async def test_suggested_unit_guard_invalid_unit(
An invalid suggested unit creates a log entry and the suggested unit will be ignored. An invalid suggested unit creates a log entry and the suggested unit will be ignored.
""" """
entity_registry = er.async_get(hass) entity_registry = er.async_get(hass)
platform = getattr(hass.components, "test.sensor")
platform.init(empty=True)
state_value = 10 state_value = 10
invalid_suggested_unit = "invalid_unit" invalid_suggested_unit = "invalid_unit"
entity = platform.ENTITIES["0"] = platform.MockSensor( entity = MockSensor(
name="Invalid", name="Invalid",
device_class=device_class, device_class=device_class,
native_unit_of_measurement=native_unit, native_unit_of_measurement=native_unit,
@ -2658,6 +2580,7 @@ async def test_suggested_unit_guard_invalid_unit(
native_value=str(state_value), native_value=str(state_value),
unique_id="invalid", unique_id="invalid",
) )
setup_test_component_platform(hass, sensor.DOMAIN, [entity])
assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}}) assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}})
await hass.async_block_till_done() await hass.async_block_till_done()
@ -2674,10 +2597,10 @@ async def test_suggested_unit_guard_invalid_unit(
"homeassistant.components.sensor", "homeassistant.components.sensor",
logging.WARNING, logging.WARNING,
( (
"<class 'custom_components.test.sensor.MockSensor'> sets an" "<class 'tests.components.sensor.common.MockSensor'> sets an"
" invalid suggested_unit_of_measurement. Please report it to the author" " invalid suggested_unit_of_measurement. Please create a bug report at "
" of the 'test' custom integration. This warning will become an error in" "https://github.com/home-assistant/core/issues?q=is%3Aopen+is%3Aissue+label%3A%22integration%3A+test%22."
" Home Assistant Core 2024.5" " This warning will become an error in Home Assistant Core 2024.5"
), ),
) in caplog.record_tuples ) in caplog.record_tuples
@ -2715,10 +2638,8 @@ async def test_suggested_unit_guard_valid_unit(
in the entity registry. in the entity registry.
""" """
entity_registry = er.async_get(hass) entity_registry = er.async_get(hass)
platform = getattr(hass.components, "test.sensor")
platform.init(empty=True)
entity = platform.ENTITIES["0"] = platform.MockSensor( entity = MockSensor(
name="Valid", name="Valid",
device_class=device_class, device_class=device_class,
native_unit_of_measurement=native_unit, native_unit_of_measurement=native_unit,
@ -2726,6 +2647,7 @@ async def test_suggested_unit_guard_valid_unit(
suggested_unit_of_measurement=suggested_unit, suggested_unit_of_measurement=suggested_unit,
unique_id="valid", unique_id="valid",
) )
setup_test_component_platform(hass, sensor.DOMAIN, [entity])
assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}}) assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}})
await hass.async_block_till_done() await hass.async_block_till_done()

View File

@ -33,13 +33,14 @@ from homeassistant.components.recorder.statistics import (
list_statistic_ids, list_statistic_ids,
) )
from homeassistant.components.recorder.util import get_instance, session_scope 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.const import ATTR_FRIENDLY_NAME, STATE_UNAVAILABLE
from homeassistant.core import HomeAssistant, State from homeassistant.core import HomeAssistant, State
from homeassistant.setup import async_setup_component, setup_component from homeassistant.setup import async_setup_component, setup_component
import homeassistant.util.dt as dt_util import homeassistant.util.dt as dt_util
from homeassistant.util.unit_system import METRIC_SYSTEM, US_CUSTOMARY_SYSTEM 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 ( from tests.components.recorder.common import (
assert_dict_of_states_equal_without_context_and_last_changed, assert_dict_of_states_equal_without_context_and_last_changed,
assert_multiple_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, statistics_during_period,
wait_recording_done, wait_recording_done,
) )
from tests.components.sensor.common import MockSensor
from tests.typing import WebSocketGenerator from tests.typing import WebSocketGenerator
BATTERY_SENSOR_ATTRIBUTES = { BATTERY_SENSOR_ATTRIBUTES = {
@ -1363,11 +1365,9 @@ def test_compile_hourly_sum_statistics_negative_state(
hass = hass_recorder() hass = hass_recorder()
hass.data.pop(loader.DATA_CUSTOM_COMPONENTS) hass.data.pop(loader.DATA_CUSTOM_COMPONENTS)
platform = getattr(hass.components, "test.sensor") mocksensor = MockSensor(name="custom_sensor")
platform.init(empty=True)
mocksensor = platform.MockSensor(name="custom_sensor")
mocksensor._attr_should_poll = False 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(hass, "homeassistant", {})
setup_component( setup_component(
@ -5178,9 +5178,7 @@ async def test_exclude_attributes(
recorder_mock: Recorder, hass: HomeAssistant, enable_custom_integrations: None recorder_mock: Recorder, hass: HomeAssistant, enable_custom_integrations: None
) -> None: ) -> None:
"""Test sensor attributes to be excluded.""" """Test sensor attributes to be excluded."""
platform = getattr(hass.components, "test.sensor") entity0 = MockSensor(
platform.init(empty=True)
platform.ENTITIES["0"] = platform.MockSensor(
has_entity_name=True, has_entity_name=True,
unique_id="test", unique_id="test",
name="Test", name="Test",
@ -5188,6 +5186,7 @@ async def test_exclude_attributes(
device_class=SensorDeviceClass.ENUM, device_class=SensorDeviceClass.ENUM,
options=["option1", "option2"], options=["option1", "option2"],
) )
setup_test_component_platform(hass, DOMAIN, [entity0])
assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}}) assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}})
await hass.async_block_till_done() await hass.async_block_till_done()
await async_wait_recording_done(hass) await async_wait_recording_done(hass)