Replace the usage of unit constants by enumerations in Tests [v-z] (#85938)

replace unit conts by enums v-z
This commit is contained in:
Michael 2023-01-15 19:14:02 +01:00 committed by GitHub
parent 03d9d30eca
commit 9828b2d13f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 189 additions and 166 deletions

View File

@ -9,7 +9,7 @@ from homeassistant.const import (
CONF_MONITORED_CONDITIONS,
CONF_NAME,
CONF_PLATFORM,
DATA_GIGABYTES,
UnitOfInformation,
)
from homeassistant.core import HomeAssistant
@ -58,7 +58,9 @@ def test_sensor(hass: HomeAssistant):
device.update()
if device.unit_of_measurement == DATA_GIGABYTES: # Test Bandwidth Used
if (
device.unit_of_measurement == UnitOfInformation.GIGABYTES
): # Test Bandwidth Used
if device.subscription == "576965":
assert device.name == "Vultr my new server Current Bandwidth Used"
assert device.icon == "mdi:chart-histogram"

View File

@ -1,5 +1,5 @@
"""Test Wallbox Switch component."""
from homeassistant.const import CONF_ICON, CONF_UNIT_OF_MEASUREMENT, POWER_KILO_WATT
from homeassistant.const import CONF_ICON, CONF_UNIT_OF_MEASUREMENT, UnitOfPower
from homeassistant.core import HomeAssistant
from . import entry, setup_integration
@ -16,7 +16,7 @@ async def test_wallbox_sensor_class(hass: HomeAssistant) -> None:
await setup_integration(hass)
state = hass.states.get(MOCK_SENSOR_CHARGING_POWER_ID)
assert state.attributes[CONF_UNIT_OF_MEASUREMENT] == POWER_KILO_WATT
assert state.attributes[CONF_UNIT_OF_MEASUREMENT] == UnitOfPower.KILO_WATT
assert state.name == "Mock Title Charging Power"
state = hass.states.get(MOCK_SENSOR_CHARGING_SPEED_ID)

View File

@ -31,21 +31,13 @@ from homeassistant.components.weather import (
)
from homeassistant.const import (
ATTR_FRIENDLY_NAME,
LENGTH_INCHES,
LENGTH_KILOMETERS,
LENGTH_MILES,
LENGTH_MILLIMETERS,
PRECISION_HALVES,
PRECISION_TENTHS,
PRECISION_WHOLE,
PRESSURE_HPA,
PRESSURE_INHG,
PRESSURE_PA,
SPEED_KILOMETERS_PER_HOUR,
SPEED_METERS_PER_SECOND,
SPEED_MILES_PER_HOUR,
TEMP_CELSIUS,
TEMP_FAHRENHEIT,
UnitOfLength,
UnitOfPressure,
UnitOfSpeed,
UnitOfTemperature,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
@ -68,15 +60,15 @@ class MockWeatherEntity(WeatherEntity):
"""Initiate Entity."""
super().__init__()
self._attr_condition = ATTR_CONDITION_SUNNY
self._attr_native_precipitation_unit = LENGTH_MILLIMETERS
self._attr_native_precipitation_unit = UnitOfLength.MILLIMETERS
self._attr_native_pressure = 10
self._attr_native_pressure_unit = PRESSURE_HPA
self._attr_native_pressure_unit = UnitOfPressure.HPA
self._attr_native_temperature = 20
self._attr_native_temperature_unit = TEMP_CELSIUS
self._attr_native_temperature_unit = UnitOfTemperature.CELSIUS
self._attr_native_visibility = 30
self._attr_native_visibility_unit = LENGTH_KILOMETERS
self._attr_native_visibility_unit = UnitOfLength.KILOMETERS
self._attr_native_wind_speed = 3
self._attr_native_wind_speed_unit = SPEED_METERS_PER_SECOND
self._attr_native_wind_speed_unit = UnitOfSpeed.METERS_PER_SECOND
self._attr_forecast = [
Forecast(
datetime=datetime(2022, 6, 20, 20, 00, 00),
@ -94,7 +86,7 @@ class MockWeatherEntityPrecision(WeatherEntity):
super().__init__()
self._attr_condition = ATTR_CONDITION_SUNNY
self._attr_native_temperature = 20.3
self._attr_native_temperature_unit = TEMP_CELSIUS
self._attr_native_temperature_unit = UnitOfTemperature.CELSIUS
self._attr_precision = PRECISION_HALVES
@ -105,15 +97,15 @@ class MockWeatherEntityCompat(WeatherEntity):
"""Initiate Entity."""
super().__init__()
self._attr_condition = ATTR_CONDITION_SUNNY
self._attr_precipitation_unit = LENGTH_MILLIMETERS
self._attr_precipitation_unit = UnitOfLength.MILLIMETERS
self._attr_pressure = 10
self._attr_pressure_unit = PRESSURE_HPA
self._attr_pressure_unit = UnitOfPressure.HPA
self._attr_temperature = 20
self._attr_temperature_unit = TEMP_CELSIUS
self._attr_temperature_unit = UnitOfTemperature.CELSIUS
self._attr_visibility = 30
self._attr_visibility_unit = LENGTH_KILOMETERS
self._attr_visibility_unit = UnitOfLength.KILOMETERS
self._attr_wind_speed = 3
self._attr_wind_speed_unit = SPEED_METERS_PER_SECOND
self._attr_wind_speed_unit = UnitOfSpeed.METERS_PER_SECOND
self._attr_forecast = [
Forecast(
datetime=datetime(2022, 6, 20, 20, 00, 00),
@ -142,10 +134,15 @@ async def create_entity(hass: HomeAssistant, **kwargs):
return entity0
@pytest.mark.parametrize("native_unit", (TEMP_FAHRENHEIT, TEMP_CELSIUS))
@pytest.mark.parametrize(
"native_unit", (UnitOfTemperature.FAHRENHEIT, UnitOfTemperature.CELSIUS)
)
@pytest.mark.parametrize(
"state_unit, unit_system",
((TEMP_CELSIUS, METRIC_SYSTEM), (TEMP_FAHRENHEIT, US_CUSTOMARY_SYSTEM)),
(
(UnitOfTemperature.CELSIUS, METRIC_SYSTEM),
(UnitOfTemperature.FAHRENHEIT, US_CUSTOMARY_SYSTEM),
),
)
async def test_temperature(
hass: HomeAssistant,
@ -178,7 +175,10 @@ async def test_temperature(
@pytest.mark.parametrize("native_unit", (None,))
@pytest.mark.parametrize(
"state_unit, unit_system",
((TEMP_CELSIUS, METRIC_SYSTEM), (TEMP_FAHRENHEIT, US_CUSTOMARY_SYSTEM)),
(
(UnitOfTemperature.CELSIUS, METRIC_SYSTEM),
(UnitOfTemperature.FAHRENHEIT, US_CUSTOMARY_SYSTEM),
),
)
async def test_temperature_no_unit(
hass: HomeAssistant,
@ -208,10 +208,10 @@ async def test_temperature_no_unit(
assert float(forecast[ATTR_FORECAST_TEMP_LOW]) == approx(expected, rel=0.1)
@pytest.mark.parametrize("native_unit", (PRESSURE_INHG, PRESSURE_INHG))
@pytest.mark.parametrize("native_unit", (UnitOfPressure.INHG, UnitOfPressure.INHG))
@pytest.mark.parametrize(
"state_unit, unit_system",
((PRESSURE_HPA, METRIC_SYSTEM), (PRESSURE_INHG, US_CUSTOMARY_SYSTEM)),
((UnitOfPressure.HPA, METRIC_SYSTEM), (UnitOfPressure.INHG, US_CUSTOMARY_SYSTEM)),
)
async def test_pressure(
hass: HomeAssistant,
@ -239,7 +239,7 @@ async def test_pressure(
@pytest.mark.parametrize("native_unit", (None,))
@pytest.mark.parametrize(
"state_unit, unit_system",
((PRESSURE_HPA, METRIC_SYSTEM), (PRESSURE_INHG, US_CUSTOMARY_SYSTEM)),
((UnitOfPressure.HPA, METRIC_SYSTEM), (UnitOfPressure.INHG, US_CUSTOMARY_SYSTEM)),
)
async def test_pressure_no_unit(
hass: HomeAssistant,
@ -266,13 +266,17 @@ async def test_pressure_no_unit(
@pytest.mark.parametrize(
"native_unit",
(SPEED_MILES_PER_HOUR, SPEED_KILOMETERS_PER_HOUR, SPEED_METERS_PER_SECOND),
(
UnitOfSpeed.MILES_PER_HOUR,
UnitOfSpeed.KILOMETERS_PER_HOUR,
UnitOfSpeed.METERS_PER_SECOND,
),
)
@pytest.mark.parametrize(
"state_unit, unit_system",
(
(SPEED_KILOMETERS_PER_HOUR, METRIC_SYSTEM),
(SPEED_MILES_PER_HOUR, US_CUSTOMARY_SYSTEM),
(UnitOfSpeed.KILOMETERS_PER_HOUR, METRIC_SYSTEM),
(UnitOfSpeed.MILES_PER_HOUR, US_CUSTOMARY_SYSTEM),
),
)
async def test_wind_speed(
@ -305,8 +309,8 @@ async def test_wind_speed(
@pytest.mark.parametrize(
"state_unit, unit_system",
(
(SPEED_KILOMETERS_PER_HOUR, METRIC_SYSTEM),
(SPEED_MILES_PER_HOUR, US_CUSTOMARY_SYSTEM),
(UnitOfSpeed.KILOMETERS_PER_HOUR, METRIC_SYSTEM),
(UnitOfSpeed.MILES_PER_HOUR, US_CUSTOMARY_SYSTEM),
),
)
async def test_wind_speed_no_unit(
@ -335,12 +339,12 @@ async def test_wind_speed_no_unit(
assert float(forecast[ATTR_FORECAST_WIND_SPEED]) == approx(expected, rel=1e-2)
@pytest.mark.parametrize("native_unit", (LENGTH_MILES, LENGTH_KILOMETERS))
@pytest.mark.parametrize("native_unit", (UnitOfLength.MILES, UnitOfLength.KILOMETERS))
@pytest.mark.parametrize(
"state_unit, unit_system",
(
(LENGTH_KILOMETERS, METRIC_SYSTEM),
(LENGTH_MILES, US_CUSTOMARY_SYSTEM),
(UnitOfLength.KILOMETERS, METRIC_SYSTEM),
(UnitOfLength.MILES, US_CUSTOMARY_SYSTEM),
),
)
async def test_visibility(
@ -370,8 +374,8 @@ async def test_visibility(
@pytest.mark.parametrize(
"state_unit, unit_system",
(
(LENGTH_KILOMETERS, METRIC_SYSTEM),
(LENGTH_MILES, US_CUSTOMARY_SYSTEM),
(UnitOfLength.KILOMETERS, METRIC_SYSTEM),
(UnitOfLength.MILES, US_CUSTOMARY_SYSTEM),
),
)
async def test_visibility_no_unit(
@ -397,12 +401,12 @@ async def test_visibility_no_unit(
)
@pytest.mark.parametrize("native_unit", (LENGTH_INCHES, LENGTH_MILLIMETERS))
@pytest.mark.parametrize("native_unit", (UnitOfLength.INCHES, UnitOfLength.MILLIMETERS))
@pytest.mark.parametrize(
"state_unit, unit_system",
(
(LENGTH_MILLIMETERS, METRIC_SYSTEM),
(LENGTH_INCHES, US_CUSTOMARY_SYSTEM),
(UnitOfLength.MILLIMETERS, METRIC_SYSTEM),
(UnitOfLength.INCHES, US_CUSTOMARY_SYSTEM),
),
)
async def test_precipitation(
@ -432,8 +436,8 @@ async def test_precipitation(
@pytest.mark.parametrize(
"state_unit, unit_system",
(
(LENGTH_MILLIMETERS, METRIC_SYSTEM),
(LENGTH_INCHES, US_CUSTOMARY_SYSTEM),
(UnitOfLength.MILLIMETERS, METRIC_SYSTEM),
(UnitOfLength.INCHES, US_CUSTOMARY_SYSTEM),
),
)
async def test_precipitation_no_unit(
@ -484,11 +488,11 @@ async def test_none_forecast(
entity0 = await create_entity(
hass,
native_pressure=None,
native_pressure_unit=PRESSURE_INHG,
native_pressure_unit=UnitOfPressure.INHG,
native_wind_speed=None,
native_wind_speed_unit=SPEED_METERS_PER_SECOND,
native_wind_speed_unit=UnitOfSpeed.METERS_PER_SECOND,
native_precipitation=None,
native_precipitation_unit=LENGTH_MILLIMETERS,
native_precipitation_unit=UnitOfLength.MILLIMETERS,
)
state = hass.states.get(entity0.entity_id)
@ -502,22 +506,22 @@ async def test_none_forecast(
async def test_custom_units(hass: HomeAssistant, enable_custom_integrations) -> None:
"""Test custom unit."""
wind_speed_value = 5
wind_speed_unit = SPEED_METERS_PER_SECOND
wind_speed_unit = UnitOfSpeed.METERS_PER_SECOND
pressure_value = 110
pressure_unit = PRESSURE_HPA
pressure_unit = UnitOfPressure.HPA
temperature_value = 20
temperature_unit = TEMP_CELSIUS
temperature_unit = UnitOfTemperature.CELSIUS
visibility_value = 11
visibility_unit = LENGTH_KILOMETERS
visibility_unit = UnitOfLength.KILOMETERS
precipitation_value = 1.1
precipitation_unit = LENGTH_MILLIMETERS
precipitation_unit = UnitOfLength.MILLIMETERS
set_options = {
"wind_speed_unit": SPEED_MILES_PER_HOUR,
"precipitation_unit": LENGTH_INCHES,
"pressure_unit": PRESSURE_INHG,
"temperature_unit": TEMP_FAHRENHEIT,
"visibility_unit": LENGTH_MILES,
"wind_speed_unit": UnitOfSpeed.MILES_PER_HOUR,
"precipitation_unit": UnitOfLength.INCHES,
"pressure_unit": UnitOfPressure.INHG,
"temperature_unit": UnitOfTemperature.FAHRENHEIT,
"visibility_unit": UnitOfLength.MILES,
}
entity_registry = er.async_get(hass)
@ -556,23 +560,27 @@ async def test_custom_units(hass: HomeAssistant, enable_custom_integrations) ->
forecast = state.attributes[ATTR_FORECAST][0]
expected_wind_speed = round(
SpeedConverter.convert(wind_speed_value, wind_speed_unit, SPEED_MILES_PER_HOUR),
SpeedConverter.convert(
wind_speed_value, wind_speed_unit, UnitOfSpeed.MILES_PER_HOUR
),
ROUNDING_PRECISION,
)
expected_temperature = TemperatureConverter.convert(
temperature_value, temperature_unit, TEMP_FAHRENHEIT
temperature_value, temperature_unit, UnitOfTemperature.FAHRENHEIT
)
expected_pressure = round(
PressureConverter.convert(pressure_value, pressure_unit, PRESSURE_INHG),
PressureConverter.convert(pressure_value, pressure_unit, UnitOfPressure.INHG),
ROUNDING_PRECISION,
)
expected_visibility = round(
DistanceConverter.convert(visibility_value, visibility_unit, LENGTH_MILES),
DistanceConverter.convert(
visibility_value, visibility_unit, UnitOfLength.MILES
),
ROUNDING_PRECISION,
)
expected_precipitation = round(
DistanceConverter.convert(
precipitation_value, precipitation_unit, LENGTH_INCHES
precipitation_value, precipitation_unit, UnitOfLength.INCHES
),
ROUNDING_PRECISION,
)
@ -613,15 +621,15 @@ async def test_backwards_compatibility(
) -> None:
"""Test backwards compatibility."""
wind_speed_value = 5
wind_speed_unit = SPEED_METERS_PER_SECOND
wind_speed_unit = UnitOfSpeed.METERS_PER_SECOND
pressure_value = 110000
pressure_unit = PRESSURE_PA
pressure_unit = UnitOfPressure.PA
temperature_value = 20
temperature_unit = TEMP_CELSIUS
temperature_unit = UnitOfTemperature.CELSIUS
visibility_value = 11
visibility_unit = LENGTH_KILOMETERS
visibility_unit = UnitOfLength.KILOMETERS
precipitation_value = 1
precipitation_unit = LENGTH_MILLIMETERS
precipitation_unit = UnitOfLength.MILLIMETERS
hass.config.units = METRIC_SYSTEM
@ -676,36 +684,44 @@ async def test_backwards_compatibility(
assert float(state.attributes[ATTR_WEATHER_WIND_SPEED]) == approx(
wind_speed_value * 3.6
)
assert state.attributes[ATTR_WEATHER_WIND_SPEED_UNIT] == SPEED_KILOMETERS_PER_HOUR
assert (
state.attributes[ATTR_WEATHER_WIND_SPEED_UNIT]
== UnitOfSpeed.KILOMETERS_PER_HOUR
)
assert float(state.attributes[ATTR_WEATHER_TEMPERATURE]) == approx(
temperature_value, rel=0.1
)
assert state.attributes[ATTR_WEATHER_TEMPERATURE_UNIT] == TEMP_CELSIUS
assert state.attributes[ATTR_WEATHER_TEMPERATURE_UNIT] == UnitOfTemperature.CELSIUS
assert float(state.attributes[ATTR_WEATHER_PRESSURE]) == approx(
pressure_value / 100
)
assert state.attributes[ATTR_WEATHER_PRESSURE_UNIT] == PRESSURE_HPA
assert state.attributes[ATTR_WEATHER_PRESSURE_UNIT] == UnitOfPressure.HPA
assert float(state.attributes[ATTR_WEATHER_VISIBILITY]) == approx(visibility_value)
assert state.attributes[ATTR_WEATHER_VISIBILITY_UNIT] == LENGTH_KILOMETERS
assert state.attributes[ATTR_WEATHER_VISIBILITY_UNIT] == UnitOfLength.KILOMETERS
assert float(forecast[ATTR_FORECAST_PRECIPITATION]) == approx(
precipitation_value, rel=1e-2
)
assert state.attributes[ATTR_WEATHER_PRECIPITATION_UNIT] == LENGTH_MILLIMETERS
assert state.attributes[ATTR_WEATHER_PRECIPITATION_UNIT] == UnitOfLength.MILLIMETERS
assert float(state1.attributes[ATTR_WEATHER_WIND_SPEED]) == approx(wind_speed_value)
assert state1.attributes[ATTR_WEATHER_WIND_SPEED_UNIT] == SPEED_KILOMETERS_PER_HOUR
assert (
state1.attributes[ATTR_WEATHER_WIND_SPEED_UNIT]
== UnitOfSpeed.KILOMETERS_PER_HOUR
)
assert float(state1.attributes[ATTR_WEATHER_TEMPERATURE]) == approx(
temperature_value, rel=0.1
)
assert state1.attributes[ATTR_WEATHER_TEMPERATURE_UNIT] == TEMP_CELSIUS
assert state1.attributes[ATTR_WEATHER_TEMPERATURE_UNIT] == UnitOfTemperature.CELSIUS
assert float(state1.attributes[ATTR_WEATHER_PRESSURE]) == approx(pressure_value)
assert state1.attributes[ATTR_WEATHER_PRESSURE_UNIT] == PRESSURE_HPA
assert state1.attributes[ATTR_WEATHER_PRESSURE_UNIT] == UnitOfPressure.HPA
assert float(state1.attributes[ATTR_WEATHER_VISIBILITY]) == approx(visibility_value)
assert state1.attributes[ATTR_WEATHER_VISIBILITY_UNIT] == LENGTH_KILOMETERS
assert state1.attributes[ATTR_WEATHER_VISIBILITY_UNIT] == UnitOfLength.KILOMETERS
assert float(forecast1[ATTR_FORECAST_PRECIPITATION]) == approx(
precipitation_value, rel=1e-2
)
assert state1.attributes[ATTR_WEATHER_PRECIPITATION_UNIT] == LENGTH_MILLIMETERS
assert (
state1.attributes[ATTR_WEATHER_PRECIPITATION_UNIT] == UnitOfLength.MILLIMETERS
)
async def test_backwards_compatibility_convert_values(
@ -713,15 +729,15 @@ async def test_backwards_compatibility_convert_values(
) -> None:
"""Test backward compatibility for converting values."""
wind_speed_value = 5
wind_speed_unit = SPEED_METERS_PER_SECOND
wind_speed_unit = UnitOfSpeed.METERS_PER_SECOND
pressure_value = 110000
pressure_unit = PRESSURE_PA
pressure_unit = UnitOfPressure.PA
temperature_value = 20
temperature_unit = TEMP_CELSIUS
temperature_unit = UnitOfTemperature.CELSIUS
visibility_value = 11
visibility_unit = LENGTH_KILOMETERS
visibility_unit = UnitOfLength.KILOMETERS
precipitation_value = 1
precipitation_unit = LENGTH_MILLIMETERS
precipitation_unit = UnitOfLength.MILLIMETERS
hass.config.units = US_CUSTOMARY_SYSTEM
@ -754,23 +770,27 @@ async def test_backwards_compatibility_convert_values(
state = hass.states.get(entity0.entity_id)
expected_wind_speed = round(
SpeedConverter.convert(wind_speed_value, wind_speed_unit, SPEED_MILES_PER_HOUR),
SpeedConverter.convert(
wind_speed_value, wind_speed_unit, UnitOfSpeed.MILES_PER_HOUR
),
ROUNDING_PRECISION,
)
expected_temperature = TemperatureConverter.convert(
temperature_value, temperature_unit, TEMP_FAHRENHEIT
temperature_value, temperature_unit, UnitOfTemperature.FAHRENHEIT
)
expected_pressure = round(
PressureConverter.convert(pressure_value, pressure_unit, PRESSURE_INHG),
PressureConverter.convert(pressure_value, pressure_unit, UnitOfPressure.INHG),
ROUNDING_PRECISION,
)
expected_visibility = round(
DistanceConverter.convert(visibility_value, visibility_unit, LENGTH_MILES),
DistanceConverter.convert(
visibility_value, visibility_unit, UnitOfLength.MILES
),
ROUNDING_PRECISION,
)
expected_precipitation = round(
DistanceConverter.convert(
precipitation_value, precipitation_unit, LENGTH_INCHES
precipitation_value, precipitation_unit, UnitOfLength.INCHES
),
ROUNDING_PRECISION,
)
@ -787,15 +807,15 @@ async def test_backwards_compatibility_convert_values(
}
],
ATTR_FRIENDLY_NAME: "Test",
ATTR_WEATHER_PRECIPITATION_UNIT: LENGTH_INCHES,
ATTR_WEATHER_PRECIPITATION_UNIT: UnitOfLength.INCHES,
ATTR_WEATHER_PRESSURE: approx(expected_pressure, rel=0.1),
ATTR_WEATHER_PRESSURE_UNIT: PRESSURE_INHG,
ATTR_WEATHER_PRESSURE_UNIT: UnitOfPressure.INHG,
ATTR_WEATHER_TEMPERATURE: approx(expected_temperature, rel=0.1),
ATTR_WEATHER_TEMPERATURE_UNIT: TEMP_FAHRENHEIT,
ATTR_WEATHER_TEMPERATURE_UNIT: UnitOfTemperature.FAHRENHEIT,
ATTR_WEATHER_VISIBILITY: approx(expected_visibility, rel=0.1),
ATTR_WEATHER_VISIBILITY_UNIT: LENGTH_MILES,
ATTR_WEATHER_VISIBILITY_UNIT: UnitOfLength.MILES,
ATTR_WEATHER_WIND_SPEED: approx(expected_wind_speed, rel=0.1),
ATTR_WEATHER_WIND_SPEED_UNIT: SPEED_MILES_PER_HOUR,
ATTR_WEATHER_WIND_SPEED_UNIT: UnitOfSpeed.MILES_PER_HOUR,
}
@ -815,20 +835,20 @@ async def test_attr(hass: HomeAssistant) -> None:
weather.hass = hass
assert weather.condition == ATTR_CONDITION_SUNNY
assert weather.native_precipitation_unit == LENGTH_MILLIMETERS
assert weather._precipitation_unit == LENGTH_MILLIMETERS
assert weather.native_precipitation_unit == UnitOfLength.MILLIMETERS
assert weather._precipitation_unit == UnitOfLength.MILLIMETERS
assert weather.native_pressure == 10
assert weather.native_pressure_unit == PRESSURE_HPA
assert weather._pressure_unit == PRESSURE_HPA
assert weather.native_pressure_unit == UnitOfPressure.HPA
assert weather._pressure_unit == UnitOfPressure.HPA
assert weather.native_temperature == 20
assert weather.native_temperature_unit == TEMP_CELSIUS
assert weather._temperature_unit == TEMP_CELSIUS
assert weather.native_temperature_unit == UnitOfTemperature.CELSIUS
assert weather._temperature_unit == UnitOfTemperature.CELSIUS
assert weather.native_visibility == 30
assert weather.native_visibility_unit == LENGTH_KILOMETERS
assert weather._visibility_unit == LENGTH_KILOMETERS
assert weather.native_visibility_unit == UnitOfLength.KILOMETERS
assert weather._visibility_unit == UnitOfLength.KILOMETERS
assert weather.native_wind_speed == 3
assert weather.native_wind_speed_unit == SPEED_METERS_PER_SECOND
assert weather._wind_speed_unit == SPEED_KILOMETERS_PER_HOUR
assert weather.native_wind_speed_unit == UnitOfSpeed.METERS_PER_SECOND
assert weather._wind_speed_unit == UnitOfSpeed.KILOMETERS_PER_HOUR
async def test_attr_compatibility(hass: HomeAssistant) -> None:
@ -838,15 +858,15 @@ async def test_attr_compatibility(hass: HomeAssistant) -> None:
weather.hass = hass
assert weather.condition == ATTR_CONDITION_SUNNY
assert weather._precipitation_unit == LENGTH_MILLIMETERS
assert weather._precipitation_unit == UnitOfLength.MILLIMETERS
assert weather.pressure == 10
assert weather._pressure_unit == PRESSURE_HPA
assert weather._pressure_unit == UnitOfPressure.HPA
assert weather.temperature == 20
assert weather._temperature_unit == TEMP_CELSIUS
assert weather._temperature_unit == UnitOfTemperature.CELSIUS
assert weather.visibility == 30
assert weather.visibility_unit == LENGTH_KILOMETERS
assert weather.visibility_unit == UnitOfLength.KILOMETERS
assert weather.wind_speed == 3
assert weather._wind_speed_unit == SPEED_KILOMETERS_PER_HOUR
assert weather._wind_speed_unit == UnitOfSpeed.KILOMETERS_PER_HOUR
forecast_entry = [
Forecast(
@ -861,14 +881,14 @@ async def test_attr_compatibility(hass: HomeAssistant) -> None:
assert weather.state_attributes == {
ATTR_FORECAST: forecast_entry,
ATTR_WEATHER_PRESSURE: 10.0,
ATTR_WEATHER_PRESSURE_UNIT: PRESSURE_HPA,
ATTR_WEATHER_PRESSURE_UNIT: UnitOfPressure.HPA,
ATTR_WEATHER_TEMPERATURE: 20.0,
ATTR_WEATHER_TEMPERATURE_UNIT: TEMP_CELSIUS,
ATTR_WEATHER_TEMPERATURE_UNIT: UnitOfTemperature.CELSIUS,
ATTR_WEATHER_VISIBILITY: 30.0,
ATTR_WEATHER_VISIBILITY_UNIT: LENGTH_KILOMETERS,
ATTR_WEATHER_VISIBILITY_UNIT: UnitOfLength.KILOMETERS,
ATTR_WEATHER_WIND_SPEED: 3.0 * 3.6,
ATTR_WEATHER_WIND_SPEED_UNIT: SPEED_KILOMETERS_PER_HOUR,
ATTR_WEATHER_PRECIPITATION_UNIT: LENGTH_MILLIMETERS,
ATTR_WEATHER_WIND_SPEED_UNIT: UnitOfSpeed.KILOMETERS_PER_HOUR,
ATTR_WEATHER_PRECIPITATION_UNIT: UnitOfLength.MILLIMETERS,
}
@ -880,7 +900,7 @@ async def test_precision_for_temperature(hass: HomeAssistant) -> None:
assert weather.condition == ATTR_CONDITION_SUNNY
assert weather.native_temperature == 20.3
assert weather._temperature_unit == TEMP_CELSIUS
assert weather._temperature_unit == UnitOfTemperature.CELSIUS
assert weather.precision == PRECISION_HALVES
assert weather.state_attributes[ATTR_WEATHER_TEMPERATURE] == 20.5

View File

@ -10,11 +10,11 @@ from homeassistant.const import (
ATTR_DEVICE_CLASS,
ATTR_ICON,
ATTR_UNIT_OF_MEASUREMENT,
DATA_BYTES,
ELECTRIC_CURRENT_MILLIAMPERE,
PERCENTAGE,
SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
STATE_UNKNOWN,
UnitOfElectricCurrent,
UnitOfInformation,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
@ -42,7 +42,8 @@ async def test_sensors(
state = hass.states.get("sensor.wled_rgb_light_estimated_current")
assert state
assert (
state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == ELECTRIC_CURRENT_MILLIAMPERE
state.attributes.get(ATTR_UNIT_OF_MEASUREMENT)
== UnitOfElectricCurrent.MILLIAMPERE
)
assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.CURRENT
assert state.state == "470"
@ -66,7 +67,7 @@ async def test_sensors(
state = hass.states.get("sensor.wled_rgb_light_free_memory")
assert state
assert state.attributes.get(ATTR_ICON) == "mdi:memory"
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == DATA_BYTES
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfInformation.BYTES
assert state.state == "14600"
assert entry.entity_category is EntityCategory.DIAGNOSTIC

View File

@ -18,21 +18,19 @@ from homeassistant.const import (
CONF_UNIT_SYSTEM,
CONF_UNIT_SYSTEM_IMPERIAL,
CONF_UNIT_SYSTEM_METRIC,
ELECTRIC_CURRENT_AMPERE,
ELECTRIC_POTENTIAL_VOLT,
ENERGY_KILO_WATT_HOUR,
LIGHT_LUX,
PERCENTAGE,
POWER_VOLT_AMPERE,
POWER_WATT,
PRESSURE_HPA,
STATE_UNAVAILABLE,
STATE_UNKNOWN,
TEMP_CELSIUS,
TEMP_FAHRENHEIT,
VOLUME_CUBIC_FEET,
VOLUME_CUBIC_METERS,
Platform,
UnitOfApparentPower,
UnitOfElectricCurrent,
UnitOfElectricPotential,
UnitOfEnergy,
UnitOfPower,
UnitOfPressure,
UnitOfTemperature,
UnitOfVolume,
)
from homeassistant.helpers import restore_state
from homeassistant.helpers.entity_component import async_update_entity
@ -114,16 +112,16 @@ async def async_test_humidity(hass, cluster, entity_id):
async def async_test_temperature(hass, cluster, entity_id):
"""Test temperature sensor."""
await send_attributes_report(hass, cluster, {1: 1, 0: 2900, 2: 100})
assert_state(hass, entity_id, "29.0", TEMP_CELSIUS)
assert_state(hass, entity_id, "29.0", UnitOfTemperature.CELSIUS)
async def async_test_pressure(hass, cluster, entity_id):
"""Test pressure sensor."""
await send_attributes_report(hass, cluster, {1: 1, 0: 1000, 2: 10000})
assert_state(hass, entity_id, "1000", PRESSURE_HPA)
assert_state(hass, entity_id, "1000", UnitOfPressure.HPA)
await send_attributes_report(hass, cluster, {0: 1000, 20: -1, 16: 10000})
assert_state(hass, entity_id, "1000", PRESSURE_HPA)
assert_state(hass, entity_id, "1000", UnitOfPressure.HPA)
async def async_test_illuminance(hass, cluster, entity_id):
@ -159,7 +157,7 @@ async def async_test_smart_energy_summation(hass, cluster, entity_id):
await send_attributes_report(
hass, cluster, {1025: 1, "current_summ_delivered": 12321, 1026: 100}
)
assert_state(hass, entity_id, "12.32", VOLUME_CUBIC_METERS)
assert_state(hass, entity_id, "12.32", UnitOfVolume.CUBIC_METERS)
assert hass.states.get(entity_id).attributes["status"] == "NO_ALARMS"
assert hass.states.get(entity_id).attributes["device_type"] == "Electric Metering"
assert (
@ -173,17 +171,17 @@ async def async_test_electrical_measurement(hass, cluster, entity_id):
# update divisor cached value
await send_attributes_report(hass, cluster, {"ac_power_divisor": 1})
await send_attributes_report(hass, cluster, {0: 1, 1291: 100, 10: 1000})
assert_state(hass, entity_id, "100", POWER_WATT)
assert_state(hass, entity_id, "100", UnitOfPower.WATT)
await send_attributes_report(hass, cluster, {0: 1, 1291: 99, 10: 1000})
assert_state(hass, entity_id, "99", POWER_WATT)
assert_state(hass, entity_id, "99", UnitOfPower.WATT)
await send_attributes_report(hass, cluster, {"ac_power_divisor": 10})
await send_attributes_report(hass, cluster, {0: 1, 1291: 1000, 10: 5000})
assert_state(hass, entity_id, "100", POWER_WATT)
assert_state(hass, entity_id, "100", UnitOfPower.WATT)
await send_attributes_report(hass, cluster, {0: 1, 1291: 99, 10: 5000})
assert_state(hass, entity_id, "9.9", POWER_WATT)
assert_state(hass, entity_id, "9.9", UnitOfPower.WATT)
assert "active_power_max" not in hass.states.get(entity_id).attributes
await send_attributes_report(hass, cluster, {0: 1, 0x050D: 88, 10: 5000})
@ -195,31 +193,31 @@ async def async_test_em_apparent_power(hass, cluster, entity_id):
# update divisor cached value
await send_attributes_report(hass, cluster, {"ac_power_divisor": 1})
await send_attributes_report(hass, cluster, {0: 1, 0x050F: 100, 10: 1000})
assert_state(hass, entity_id, "100", POWER_VOLT_AMPERE)
assert_state(hass, entity_id, "100", UnitOfApparentPower.VOLT_AMPERE)
await send_attributes_report(hass, cluster, {0: 1, 0x050F: 99, 10: 1000})
assert_state(hass, entity_id, "99", POWER_VOLT_AMPERE)
assert_state(hass, entity_id, "99", UnitOfApparentPower.VOLT_AMPERE)
await send_attributes_report(hass, cluster, {"ac_power_divisor": 10})
await send_attributes_report(hass, cluster, {0: 1, 0x050F: 1000, 10: 5000})
assert_state(hass, entity_id, "100", POWER_VOLT_AMPERE)
assert_state(hass, entity_id, "100", UnitOfApparentPower.VOLT_AMPERE)
await send_attributes_report(hass, cluster, {0: 1, 0x050F: 99, 10: 5000})
assert_state(hass, entity_id, "9.9", POWER_VOLT_AMPERE)
assert_state(hass, entity_id, "9.9", UnitOfApparentPower.VOLT_AMPERE)
async def async_test_em_rms_current(hass, cluster, entity_id):
"""Test electrical measurement RMS Current sensor."""
await send_attributes_report(hass, cluster, {0: 1, 0x0508: 1234, 10: 1000})
assert_state(hass, entity_id, "1.2", ELECTRIC_CURRENT_AMPERE)
assert_state(hass, entity_id, "1.2", UnitOfElectricCurrent.AMPERE)
await send_attributes_report(hass, cluster, {"ac_current_divisor": 10})
await send_attributes_report(hass, cluster, {0: 1, 0x0508: 236, 10: 1000})
assert_state(hass, entity_id, "23.6", ELECTRIC_CURRENT_AMPERE)
assert_state(hass, entity_id, "23.6", UnitOfElectricCurrent.AMPERE)
await send_attributes_report(hass, cluster, {0: 1, 0x0508: 1236, 10: 1000})
assert_state(hass, entity_id, "124", ELECTRIC_CURRENT_AMPERE)
assert_state(hass, entity_id, "124", UnitOfElectricCurrent.AMPERE)
assert "rms_current_max" not in hass.states.get(entity_id).attributes
await send_attributes_report(hass, cluster, {0: 1, 0x050A: 88, 10: 5000})
@ -230,14 +228,14 @@ async def async_test_em_rms_voltage(hass, cluster, entity_id):
"""Test electrical measurement RMS Voltage sensor."""
await send_attributes_report(hass, cluster, {0: 1, 0x0505: 1234, 10: 1000})
assert_state(hass, entity_id, "123", ELECTRIC_POTENTIAL_VOLT)
assert_state(hass, entity_id, "123", UnitOfElectricPotential.VOLT)
await send_attributes_report(hass, cluster, {0: 1, 0x0505: 234, 10: 1000})
assert_state(hass, entity_id, "23.4", ELECTRIC_POTENTIAL_VOLT)
assert_state(hass, entity_id, "23.4", UnitOfElectricPotential.VOLT)
await send_attributes_report(hass, cluster, {"ac_voltage_divisor": 100})
await send_attributes_report(hass, cluster, {0: 1, 0x0505: 2236, 10: 1000})
assert_state(hass, entity_id, "22.4", ELECTRIC_POTENTIAL_VOLT)
assert_state(hass, entity_id, "22.4", UnitOfElectricPotential.VOLT)
assert "rms_voltage_max" not in hass.states.get(entity_id).attributes
await send_attributes_report(hass, cluster, {0: 1, 0x0507: 888, 10: 5000})
@ -269,7 +267,7 @@ async def async_test_powerconfiguration2(hass, cluster, entity_id):
async def async_test_device_temperature(hass, cluster, entity_id):
"""Test temperature sensor."""
await send_attributes_report(hass, cluster, {0: 2900})
assert_state(hass, entity_id, "29.0", TEMP_CELSIUS)
assert_state(hass, entity_id, "29.0", UnitOfTemperature.CELSIUS)
@pytest.mark.parametrize(
@ -517,10 +515,10 @@ def core_rs(hass_storage):
@pytest.mark.parametrize(
"uom, raw_temp, expected, restore",
[
(TEMP_CELSIUS, 2900, 29, False),
(TEMP_CELSIUS, 2900, 29, True),
(TEMP_FAHRENHEIT, 2900, 84, False),
(TEMP_FAHRENHEIT, 2900, 84, True),
(UnitOfTemperature.CELSIUS, 2900, 29, False),
(UnitOfTemperature.CELSIUS, 2900, 29, True),
(UnitOfTemperature.FAHRENHEIT, 2900, 84, False),
(UnitOfTemperature.FAHRENHEIT, 2900, 84, True),
],
)
async def test_temp_uom(
@ -540,7 +538,9 @@ async def test_temp_uom(
core_rs(entity_id, uom, state=(expected - 2))
hass = await hass_ms(
CONF_UNIT_SYSTEM_METRIC if uom == TEMP_CELSIUS else CONF_UNIT_SYSTEM_IMPERIAL
CONF_UNIT_SYSTEM_METRIC
if uom == UnitOfTemperature.CELSIUS
else CONF_UNIT_SYSTEM_IMPERIAL
)
zigpy_device = zigpy_device_mock(
@ -753,25 +753,25 @@ async def test_unsupported_attributes_sensor(
1,
12320,
"1.23",
VOLUME_CUBIC_METERS,
UnitOfVolume.CUBIC_METERS,
),
(
1,
1232000,
"123.20",
VOLUME_CUBIC_METERS,
UnitOfVolume.CUBIC_METERS,
),
(
3,
2340,
"0.23",
f"100 {VOLUME_CUBIC_FEET}",
f"100 {UnitOfVolume.CUBIC_FEET}",
),
(
3,
2360,
"0.24",
f"100 {VOLUME_CUBIC_FEET}",
f"100 {UnitOfVolume.CUBIC_FEET}",
),
(
8,
@ -783,43 +783,43 @@ async def test_unsupported_attributes_sensor(
0,
9366,
"0.937",
ENERGY_KILO_WATT_HOUR,
UnitOfEnergy.KILO_WATT_HOUR,
),
(
0,
999,
"0.1",
ENERGY_KILO_WATT_HOUR,
UnitOfEnergy.KILO_WATT_HOUR,
),
(
0,
10091,
"1.009",
ENERGY_KILO_WATT_HOUR,
UnitOfEnergy.KILO_WATT_HOUR,
),
(
0,
10099,
"1.01",
ENERGY_KILO_WATT_HOUR,
UnitOfEnergy.KILO_WATT_HOUR,
),
(
0,
100999,
"10.1",
ENERGY_KILO_WATT_HOUR,
UnitOfEnergy.KILO_WATT_HOUR,
),
(
0,
100023,
"10.002",
ENERGY_KILO_WATT_HOUR,
UnitOfEnergy.KILO_WATT_HOUR,
),
(
0,
102456,
"10.246",
ENERGY_KILO_WATT_HOUR,
UnitOfEnergy.KILO_WATT_HOUR,
),
),
)