2024-03-28 12:07:55 +01:00
|
|
|
"""Common test utilities for sensor entity component tests."""
|
2024-03-08 16:36:11 +01:00
|
|
|
|
2021-12-17 05:10:06 -05:00
|
|
|
from homeassistant.components.sensor import (
|
2022-02-08 23:00:26 +01:00
|
|
|
RestoreSensor,
|
2021-12-17 05:10:06 -05:00
|
|
|
SensorDeviceClass,
|
|
|
|
|
SensorEntity,
|
|
|
|
|
)
|
2025-06-09 14:22:58 +01:00
|
|
|
from homeassistant.components.sensor.const import DEVICE_CLASS_STATE_CLASSES
|
2021-03-06 18:52:43 -05:00
|
|
|
from homeassistant.const import (
|
2021-08-16 22:33:28 +01:00
|
|
|
CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
|
2021-03-06 18:52:43 -05:00
|
|
|
CONCENTRATION_PARTS_PER_MILLION,
|
2025-06-09 14:22:58 +01:00
|
|
|
DEGREE,
|
2022-12-27 14:25:47 +01:00
|
|
|
LIGHT_LUX,
|
2021-03-06 18:52:43 -05:00
|
|
|
PERCENTAGE,
|
|
|
|
|
SIGNAL_STRENGTH_DECIBELS,
|
2023-12-27 09:04:25 +01:00
|
|
|
UnitOfApparentPower,
|
2025-06-09 14:22:58 +01:00
|
|
|
UnitOfArea,
|
|
|
|
|
UnitOfBloodGlucoseConcentration,
|
|
|
|
|
UnitOfConductivity,
|
|
|
|
|
UnitOfDataRate,
|
|
|
|
|
UnitOfElectricCurrent,
|
|
|
|
|
UnitOfElectricPotential,
|
|
|
|
|
UnitOfEnergy,
|
|
|
|
|
UnitOfEnergyDistance,
|
2023-12-27 09:04:25 +01:00
|
|
|
UnitOfFrequency,
|
2025-06-09 14:22:58 +01:00
|
|
|
UnitOfInformation,
|
|
|
|
|
UnitOfIrradiance,
|
|
|
|
|
UnitOfLength,
|
|
|
|
|
UnitOfMass,
|
|
|
|
|
UnitOfPower,
|
|
|
|
|
UnitOfPrecipitationDepth,
|
2023-01-27 12:13:27 +01:00
|
|
|
UnitOfPressure,
|
2025-05-15 13:05:46 +02:00
|
|
|
UnitOfReactiveEnergy,
|
2024-08-21 15:36:45 +02:00
|
|
|
UnitOfReactivePower,
|
2025-06-09 14:22:58 +01:00
|
|
|
UnitOfSoundPressure,
|
|
|
|
|
UnitOfSpeed,
|
|
|
|
|
UnitOfTemperature,
|
|
|
|
|
UnitOfTime,
|
2023-12-27 09:04:25 +01:00
|
|
|
UnitOfVolume,
|
2025-06-09 14:22:58 +01:00
|
|
|
UnitOfVolumeFlowRate,
|
|
|
|
|
UnitOfVolumetricFlux,
|
2021-03-06 18:52:43 -05:00
|
|
|
)
|
2019-10-03 06:14:35 +02:00
|
|
|
|
2019-12-09 16:52:24 +01:00
|
|
|
from tests.common import MockEntity
|
2019-10-03 06:14:35 +02:00
|
|
|
|
2019-10-03 22:30:59 +02:00
|
|
|
UNITS_OF_MEASUREMENT = {
|
2025-06-09 14:22:58 +01:00
|
|
|
SensorDeviceClass.APPARENT_POWER: UnitOfApparentPower.VOLT_AMPERE,
|
|
|
|
|
SensorDeviceClass.AQI: None,
|
|
|
|
|
SensorDeviceClass.AREA: UnitOfArea.SQUARE_METERS,
|
|
|
|
|
SensorDeviceClass.ATMOSPHERIC_PRESSURE: UnitOfPressure.HPA,
|
|
|
|
|
SensorDeviceClass.BATTERY: PERCENTAGE,
|
|
|
|
|
SensorDeviceClass.BLOOD_GLUCOSE_CONCENTRATION: UnitOfBloodGlucoseConcentration.MILLIGRAMS_PER_DECILITER,
|
|
|
|
|
SensorDeviceClass.CO2: CONCENTRATION_PARTS_PER_MILLION,
|
|
|
|
|
SensorDeviceClass.CO: CONCENTRATION_PARTS_PER_MILLION,
|
|
|
|
|
SensorDeviceClass.CONDUCTIVITY: UnitOfConductivity.SIEMENS_PER_CM,
|
|
|
|
|
SensorDeviceClass.CURRENT: UnitOfElectricCurrent.AMPERE,
|
|
|
|
|
SensorDeviceClass.DATA_RATE: UnitOfDataRate.BITS_PER_SECOND,
|
|
|
|
|
SensorDeviceClass.DATA_SIZE: UnitOfInformation.BYTES,
|
|
|
|
|
SensorDeviceClass.DATE: None,
|
|
|
|
|
SensorDeviceClass.DISTANCE: UnitOfLength.METERS,
|
|
|
|
|
SensorDeviceClass.DURATION: UnitOfTime.SECONDS,
|
|
|
|
|
SensorDeviceClass.ENERGY: UnitOfEnergy.KILO_WATT_HOUR,
|
|
|
|
|
SensorDeviceClass.ENERGY_DISTANCE: UnitOfEnergyDistance.KILO_WATT_HOUR_PER_100_KM,
|
|
|
|
|
SensorDeviceClass.ENERGY_STORAGE: UnitOfEnergy.KILO_WATT_HOUR,
|
|
|
|
|
SensorDeviceClass.ENUM: None,
|
|
|
|
|
SensorDeviceClass.FREQUENCY: UnitOfFrequency.GIGAHERTZ,
|
|
|
|
|
SensorDeviceClass.GAS: UnitOfVolume.CUBIC_METERS,
|
|
|
|
|
SensorDeviceClass.HUMIDITY: PERCENTAGE,
|
|
|
|
|
SensorDeviceClass.ILLUMINANCE: LIGHT_LUX,
|
|
|
|
|
SensorDeviceClass.IRRADIANCE: UnitOfIrradiance.WATTS_PER_SQUARE_METER,
|
|
|
|
|
SensorDeviceClass.MOISTURE: PERCENTAGE,
|
|
|
|
|
SensorDeviceClass.MONETARY: None,
|
|
|
|
|
SensorDeviceClass.NITROGEN_DIOXIDE: CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
|
|
|
|
|
SensorDeviceClass.NITROGEN_MONOXIDE: CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
|
|
|
|
|
SensorDeviceClass.NITROUS_OXIDE: CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
|
|
|
|
|
SensorDeviceClass.OZONE: CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
|
|
|
|
|
SensorDeviceClass.PH: None,
|
|
|
|
|
SensorDeviceClass.PM10: CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
|
|
|
|
|
SensorDeviceClass.PM1: CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
|
|
|
|
|
SensorDeviceClass.PM25: CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
|
|
|
|
|
SensorDeviceClass.POWER: UnitOfPower.KILO_WATT,
|
|
|
|
|
SensorDeviceClass.POWER_FACTOR: PERCENTAGE,
|
|
|
|
|
SensorDeviceClass.PRECIPITATION: UnitOfPrecipitationDepth.MILLIMETERS,
|
|
|
|
|
SensorDeviceClass.PRECIPITATION_INTENSITY: UnitOfVolumetricFlux.MILLIMETERS_PER_HOUR,
|
|
|
|
|
SensorDeviceClass.PRESSURE: UnitOfPressure.HPA,
|
|
|
|
|
SensorDeviceClass.REACTIVE_ENERGY: UnitOfReactiveEnergy.VOLT_AMPERE_REACTIVE_HOUR,
|
|
|
|
|
SensorDeviceClass.REACTIVE_POWER: UnitOfReactivePower.VOLT_AMPERE_REACTIVE,
|
|
|
|
|
SensorDeviceClass.SIGNAL_STRENGTH: SIGNAL_STRENGTH_DECIBELS,
|
|
|
|
|
SensorDeviceClass.SOUND_PRESSURE: UnitOfSoundPressure.DECIBEL,
|
|
|
|
|
SensorDeviceClass.SPEED: UnitOfSpeed.METERS_PER_SECOND,
|
|
|
|
|
SensorDeviceClass.SULPHUR_DIOXIDE: CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
|
|
|
|
|
SensorDeviceClass.TEMPERATURE: UnitOfTemperature.CELSIUS,
|
|
|
|
|
SensorDeviceClass.TIMESTAMP: None,
|
|
|
|
|
SensorDeviceClass.VOLATILE_ORGANIC_COMPOUNDS: CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
|
|
|
|
|
SensorDeviceClass.VOLATILE_ORGANIC_COMPOUNDS_PARTS: CONCENTRATION_PARTS_PER_MILLION,
|
|
|
|
|
SensorDeviceClass.VOLTAGE: UnitOfElectricPotential.VOLT,
|
|
|
|
|
SensorDeviceClass.VOLUME: UnitOfVolume.LITERS,
|
|
|
|
|
SensorDeviceClass.VOLUME_FLOW_RATE: UnitOfVolumeFlowRate.LITERS_PER_MINUTE,
|
|
|
|
|
SensorDeviceClass.VOLUME_STORAGE: UnitOfVolume.LITERS,
|
|
|
|
|
SensorDeviceClass.WATER: UnitOfVolume.LITERS,
|
|
|
|
|
SensorDeviceClass.WEIGHT: UnitOfMass.KILOGRAMS,
|
|
|
|
|
SensorDeviceClass.WIND_DIRECTION: DEGREE,
|
|
|
|
|
SensorDeviceClass.WIND_SPEED: UnitOfSpeed.METERS_PER_SECOND,
|
2019-10-03 22:30:59 +02:00
|
|
|
}
|
2025-06-09 14:22:58 +01:00
|
|
|
assert UNITS_OF_MEASUREMENT.keys() == {cls.value for cls in SensorDeviceClass}
|
2019-10-03 22:30:59 +02:00
|
|
|
|
2019-10-03 06:14:35 +02:00
|
|
|
|
2021-12-17 05:10:06 -05:00
|
|
|
class MockSensor(MockEntity, SensorEntity):
|
2019-10-03 06:14:35 +02:00
|
|
|
"""Mock Sensor class."""
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def device_class(self):
|
|
|
|
|
"""Return the class of this sensor."""
|
|
|
|
|
return self._handle("device_class")
|
2019-10-03 22:30:59 +02:00
|
|
|
|
2021-08-13 12:35:23 +02:00
|
|
|
@property
|
|
|
|
|
def last_reset(self):
|
|
|
|
|
"""Return the last_reset of this sensor."""
|
|
|
|
|
return self._handle("last_reset")
|
|
|
|
|
|
2023-01-25 08:55:46 +01:00
|
|
|
@property
|
2023-02-04 20:54:36 +01:00
|
|
|
def suggested_display_precision(self):
|
2023-01-25 08:55:46 +01:00
|
|
|
"""Return the number of digits after the decimal point."""
|
2023-02-04 20:54:36 +01:00
|
|
|
return self._handle("suggested_display_precision")
|
2023-01-25 08:55:46 +01:00
|
|
|
|
2019-10-03 22:30:59 +02:00
|
|
|
@property
|
2021-08-11 10:45:05 +02:00
|
|
|
def native_unit_of_measurement(self):
|
|
|
|
|
"""Return the native unit_of_measurement of this sensor."""
|
|
|
|
|
return self._handle("native_unit_of_measurement")
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def native_value(self):
|
|
|
|
|
"""Return the native value of this sensor."""
|
|
|
|
|
return self._handle("native_value")
|
2021-08-13 12:35:23 +02:00
|
|
|
|
2022-12-02 09:11:15 +01:00
|
|
|
@property
|
|
|
|
|
def options(self):
|
|
|
|
|
"""Return the options for this sensor."""
|
|
|
|
|
return self._handle("options")
|
|
|
|
|
|
2021-08-13 12:35:23 +02:00
|
|
|
@property
|
|
|
|
|
def state_class(self):
|
|
|
|
|
"""Return the state class of this sensor."""
|
|
|
|
|
return self._handle("state_class")
|
2022-02-08 23:00:26 +01:00
|
|
|
|
2022-10-24 16:08:02 +02:00
|
|
|
@property
|
|
|
|
|
def suggested_unit_of_measurement(self):
|
|
|
|
|
"""Return the state class of this sensor."""
|
|
|
|
|
return self._handle("suggested_unit_of_measurement")
|
|
|
|
|
|
2022-02-08 23:00:26 +01:00
|
|
|
|
|
|
|
|
class MockRestoreSensor(MockSensor, RestoreSensor):
|
|
|
|
|
"""Mock RestoreSensor class."""
|
|
|
|
|
|
|
|
|
|
async def async_added_to_hass(self) -> None:
|
|
|
|
|
"""Restore native_value and native_unit_of_measurement."""
|
|
|
|
|
await super().async_added_to_hass()
|
|
|
|
|
if (last_sensor_data := await self.async_get_last_sensor_data()) is None:
|
|
|
|
|
return
|
|
|
|
|
self._values["native_value"] = last_sensor_data.native_value
|
|
|
|
|
self._values["native_unit_of_measurement"] = (
|
|
|
|
|
last_sensor_data.native_unit_of_measurement
|
2024-03-26 00:02:16 +01:00
|
|
|
)
|
2024-03-28 12:07:55 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
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,
|
2025-06-09 14:22:58 +01:00
|
|
|
state_class=DEVICE_CLASS_STATE_CLASSES.get(device_class),
|
2024-03-28 12:07:55 +01:00
|
|
|
native_unit_of_measurement=UNITS_OF_MEASUREMENT.get(device_class),
|
|
|
|
|
)
|
|
|
|
|
for device_class in SensorDeviceClass
|
|
|
|
|
}
|