Use UnitOfVolume in integrations (#83646)

This commit is contained in:
epenet 2022-12-10 12:43:13 +01:00 committed by GitHub
parent 68efa37bff
commit 456c5515c8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 57 additions and 64 deletions

View file

@ -28,7 +28,7 @@ from homeassistant.const import (
CONF_HOST,
CONF_PORT,
EVENT_HOMEASSISTANT_STOP,
VOLUME_CUBIC_METERS,
UnitOfVolume,
)
from homeassistant.core import CoreState, Event, HomeAssistant, callback
from homeassistant.helpers.entity import DeviceInfo, EntityCategory
@ -55,7 +55,7 @@ from .const import (
LOGGER,
)
UNIT_CONVERSION = {"m3": VOLUME_CUBIC_METERS}
UNIT_CONVERSION = {"m3": UnitOfVolume.CUBIC_METERS}
@dataclass

View file

@ -7,12 +7,7 @@ from homeassistant.components.sensor import (
SensorStateClass,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
PERCENTAGE,
PRESSURE_PSI,
TEMP_FAHRENHEIT,
VOLUME_GALLONS,
)
from homeassistant.const import PERCENTAGE, PRESSURE_PSI, TEMP_FAHRENHEIT, UnitOfVolume
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
@ -68,7 +63,7 @@ class FloDailyUsageSensor(FloEntity, SensorEntity):
"""Monitors the daily water usage."""
_attr_icon = WATER_ICON
_attr_native_unit_of_measurement = VOLUME_GALLONS
_attr_native_unit_of_measurement = UnitOfVolume.GALLONS
_attr_state_class: SensorStateClass = SensorStateClass.TOTAL_INCREASING
_attr_device_class = SensorDeviceClass.WATER

View file

@ -10,7 +10,7 @@ from homeassistant.components.sensor import (
SensorStateClass,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import VOLUME_GALLONS
from homeassistant.const import UnitOfVolume
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
@ -35,45 +35,45 @@ FLUME_QUERIES_SENSOR: tuple[SensorEntityDescription, ...] = (
SensorEntityDescription(
key="current_interval",
name="Current",
native_unit_of_measurement=f"{VOLUME_GALLONS}/m",
native_unit_of_measurement=f"{UnitOfVolume.GALLONS}/m",
),
SensorEntityDescription(
key="month_to_date",
name="Current Month",
native_unit_of_measurement=VOLUME_GALLONS,
native_unit_of_measurement=UnitOfVolume.GALLONS,
device_class=SensorDeviceClass.WATER,
state_class=SensorStateClass.TOTAL_INCREASING,
),
SensorEntityDescription(
key="week_to_date",
name="Current Week",
native_unit_of_measurement=VOLUME_GALLONS,
native_unit_of_measurement=UnitOfVolume.GALLONS,
device_class=SensorDeviceClass.WATER,
state_class=SensorStateClass.TOTAL_INCREASING,
),
SensorEntityDescription(
key="today",
name="Current Day",
native_unit_of_measurement=VOLUME_GALLONS,
native_unit_of_measurement=UnitOfVolume.GALLONS,
device_class=SensorDeviceClass.WATER,
state_class=SensorStateClass.TOTAL_INCREASING,
),
SensorEntityDescription(
key="last_60_min",
name="60 Minutes",
native_unit_of_measurement=f"{VOLUME_GALLONS}/h",
native_unit_of_measurement=f"{UnitOfVolume.GALLONS}/h",
state_class=SensorStateClass.MEASUREMENT,
),
SensorEntityDescription(
key="last_24_hrs",
name="24 Hours",
native_unit_of_measurement=f"{VOLUME_GALLONS}/d",
native_unit_of_measurement=f"{UnitOfVolume.GALLONS}/d",
state_class=SensorStateClass.MEASUREMENT,
),
SensorEntityDescription(
key="last_30_days",
name="30 Days",
native_unit_of_measurement=f"{VOLUME_GALLONS}/mo",
native_unit_of_measurement=f"{UnitOfVolume.GALLONS}/mo",
state_class=SensorStateClass.MEASUREMENT,
),
)

View file

@ -24,10 +24,10 @@ from homeassistant.const import (
POWER_WATT,
PRESSURE_HPA,
TEMP_CELSIUS,
VOLUME_CUBIC_METERS,
UnitOfElectricCurrent,
UnitOfPrecipitationDepth,
UnitOfSpeed,
UnitOfVolume,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
@ -154,13 +154,13 @@ SENSOR_DESCRIPTIONS: dict[str, SensorEntityDescription] = {
),
"GAS_POWER": SensorEntityDescription(
key="GAS_POWER",
native_unit_of_measurement=VOLUME_CUBIC_METERS,
native_unit_of_measurement=UnitOfVolume.CUBIC_METERS,
device_class=SensorDeviceClass.GAS,
state_class=SensorStateClass.MEASUREMENT,
),
"GAS_ENERGY_COUNTER": SensorEntityDescription(
key="GAS_ENERGY_COUNTER",
native_unit_of_measurement=VOLUME_CUBIC_METERS,
native_unit_of_measurement=UnitOfVolume.CUBIC_METERS,
device_class=SensorDeviceClass.GAS,
state_class=SensorStateClass.TOTAL_INCREASING,
),

View file

@ -15,7 +15,7 @@ from homeassistant.const import (
ENERGY_KILO_WATT_HOUR,
PERCENTAGE,
POWER_WATT,
VOLUME_CUBIC_METERS,
UnitOfVolume,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import EntityCategory
@ -115,7 +115,7 @@ SENSORS: Final[tuple[SensorEntityDescription, ...]] = (
SensorEntityDescription(
key="total_gas_m3",
name="Total gas",
native_unit_of_measurement=VOLUME_CUBIC_METERS,
native_unit_of_measurement=UnitOfVolume.CUBIC_METERS,
device_class=SensorDeviceClass.GAS,
state_class=SensorStateClass.TOTAL_INCREASING,
),
@ -129,7 +129,7 @@ SENSORS: Final[tuple[SensorEntityDescription, ...]] = (
SensorEntityDescription(
key="total_liter_m3",
name="Total water usage",
native_unit_of_measurement=VOLUME_CUBIC_METERS,
native_unit_of_measurement=UnitOfVolume.CUBIC_METERS,
icon="mdi:gauge",
device_class=SensorDeviceClass.WATER,
state_class=SensorStateClass.TOTAL_INCREASING,

View file

@ -17,7 +17,7 @@ from homeassistant.const import (
PRESSURE_BAR,
TEMP_CELSIUS,
TIME_HOURS,
VOLUME_LITERS,
UnitOfVolume,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import EntityCategory
@ -102,7 +102,7 @@ SENSOR_TYPES = (
key="reservoir_content",
name="Reservoir content",
icon="mdi:car-coolant-level",
native_unit_of_measurement=VOLUME_LITERS,
native_unit_of_measurement=UnitOfVolume.LITERS,
device_class=SensorDeviceClass.VOLUME,
state_class=SensorStateClass.MEASUREMENT,
entity_category=EntityCategory.DIAGNOSTIC,
@ -112,7 +112,7 @@ SENSOR_TYPES = (
key="total_saved",
name="Total saved",
icon="mdi:water-opacity",
native_unit_of_measurement=VOLUME_LITERS,
native_unit_of_measurement=UnitOfVolume.LITERS,
device_class=SensorDeviceClass.VOLUME,
state_class=SensorStateClass.TOTAL_INCREASING,
entity_category=EntityCategory.DIAGNOSTIC,
@ -122,7 +122,7 @@ SENSOR_TYPES = (
key="total_replenished",
name="Total replenished",
icon="mdi:water",
native_unit_of_measurement=VOLUME_LITERS,
native_unit_of_measurement=UnitOfVolume.LITERS,
device_class=SensorDeviceClass.VOLUME,
state_class=SensorStateClass.TOTAL_INCREASING,
entity_category=EntityCategory.DIAGNOSTIC,
@ -140,7 +140,7 @@ SENSOR_TYPES = (
key="totver",
name="Total use",
icon="mdi:chart-donut",
native_unit_of_measurement=VOLUME_LITERS,
native_unit_of_measurement=UnitOfVolume.LITERS,
device_class=SensorDeviceClass.VOLUME,
state_class=SensorStateClass.TOTAL_INCREASING,
entity_category=EntityCategory.DIAGNOSTIC,
@ -150,7 +150,7 @@ SENSOR_TYPES = (
key="reservoir_content_max",
name="Max reservoir content",
icon="mdi:waves",
native_unit_of_measurement=VOLUME_LITERS,
native_unit_of_measurement=UnitOfVolume.LITERS,
device_class=SensorDeviceClass.VOLUME,
state_class=SensorStateClass.TOTAL,
entity_category=EntityCategory.DIAGNOSTIC,

View file

@ -22,7 +22,7 @@ from homeassistant.components.sensor import (
SensorEntityDescription,
SensorStateClass,
)
from homeassistant.const import SIGNAL_STRENGTH_DECIBELS_MILLIWATT, VOLUME_LITERS
from homeassistant.const import SIGNAL_STRENGTH_DECIBELS_MILLIWATT, UnitOfVolume
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback
@ -39,7 +39,7 @@ SENSOR_DESCRIPTIONS = {
KegtronSensorDeviceClass.KEG_SIZE: SensorEntityDescription(
key=KegtronSensorDeviceClass.KEG_SIZE,
icon="mdi:keg",
native_unit_of_measurement=VOLUME_LITERS,
native_unit_of_measurement=UnitOfVolume.LITERS,
device_class=SensorDeviceClass.VOLUME,
state_class=SensorStateClass.MEASUREMENT,
),
@ -50,14 +50,14 @@ SENSOR_DESCRIPTIONS = {
KegtronSensorDeviceClass.VOLUME_START: SensorEntityDescription(
key=KegtronSensorDeviceClass.VOLUME_START,
icon="mdi:keg",
native_unit_of_measurement=VOLUME_LITERS,
native_unit_of_measurement=UnitOfVolume.LITERS,
device_class=SensorDeviceClass.VOLUME,
state_class=SensorStateClass.MEASUREMENT,
),
KegtronSensorDeviceClass.VOLUME_DISPENSED: SensorEntityDescription(
key=KegtronSensorDeviceClass.VOLUME_DISPENSED,
icon="mdi:keg",
native_unit_of_measurement=VOLUME_LITERS,
native_unit_of_measurement=UnitOfVolume.LITERS,
device_class=SensorDeviceClass.VOLUME,
state_class=SensorStateClass.TOTAL,
),

View file

@ -6,7 +6,7 @@ import logging
import voluptuous as vol
from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
from homeassistant.const import CURRENCY_CENT, VOLUME_LITERS
from homeassistant.const import CURRENCY_CENT, UnitOfVolume
from homeassistant.core import HomeAssistant
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback
@ -127,7 +127,7 @@ class StationPriceSensor(
@property
def native_unit_of_measurement(self) -> str:
"""Return the units of measurement."""
return f"{CURRENCY_CENT}/{VOLUME_LITERS}"
return f"{CURRENCY_CENT}/{UnitOfVolume.LITERS}"
def _get_station_name(self):
default_name = f"station {self._station_id}"

View file

@ -10,7 +10,7 @@ from homeassistant.const import (
PERCENTAGE,
TEMP_CELSIUS,
TEMP_FAHRENHEIT,
VOLUME_LITERS,
UnitOfVolume,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
@ -174,7 +174,7 @@ class OmniLogicSaltLevelSensor(OmnilogicSensor):
if self._unit_type == "Metric":
salt_return = round(int(salt_return) / 1000, 2)
unit_of_measurement = f"{MASS_GRAMS}/{VOLUME_LITERS}"
unit_of_measurement = f"{MASS_GRAMS}/{UnitOfVolume.LITERS}"
self._unit = unit_of_measurement

View file

@ -17,9 +17,8 @@ from homeassistant.const import (
ELECTRIC_POTENTIAL_VOLT,
ENERGY_KILO_WATT_HOUR,
POWER_WATT,
VOLUME_CUBIC_METERS,
VOLUME_LITERS,
UnitOfElectricCurrent,
UnitOfVolume,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceEntryType
@ -42,7 +41,7 @@ SENSORS_SMARTMETER: tuple[SensorEntityDescription, ...] = (
key="gas_consumption",
name="Gas Consumption",
entity_registry_enabled_default=False,
native_unit_of_measurement=VOLUME_CUBIC_METERS,
native_unit_of_measurement=UnitOfVolume.CUBIC_METERS,
device_class=SensorDeviceClass.GAS,
state_class=SensorStateClass.TOTAL_INCREASING,
),
@ -188,7 +187,7 @@ SENSORS_SETTINGS: tuple[SensorEntityDescription, ...] = (
name="Gas Consumption Price",
entity_registry_enabled_default=False,
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=f"{CURRENCY_EURO}/{VOLUME_CUBIC_METERS}",
native_unit_of_measurement=f"{CURRENCY_EURO}/{UnitOfVolume.CUBIC_METERS}",
),
SensorEntityDescription(
key="energy_consumption_price_low",
@ -221,14 +220,14 @@ SENSORS_WATERMETER: tuple[SensorEntityDescription, ...] = (
key="consumption_day",
name="Consumption Day",
state_class=SensorStateClass.TOTAL_INCREASING,
native_unit_of_measurement=VOLUME_LITERS,
native_unit_of_measurement=UnitOfVolume.LITERS,
device_class=SensorDeviceClass.WATER,
),
SensorEntityDescription(
key="consumption_total",
name="Consumption Total",
state_class=SensorStateClass.TOTAL_INCREASING,
native_unit_of_measurement=VOLUME_CUBIC_METERS,
native_unit_of_measurement=UnitOfVolume.CUBIC_METERS,
device_class=SensorDeviceClass.WATER,
),
SensorEntityDescription(

View file

@ -15,7 +15,7 @@ from homeassistant.const import (
POWER_WATT,
PRESSURE_BAR,
TEMP_CELSIUS,
VOLUME_CUBIC_METERS,
UnitOfVolume,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import EntityCategory
@ -222,14 +222,14 @@ SENSORS: tuple[SensorEntityDescription, ...] = (
SensorEntityDescription(
key="gas_consumed_interval",
name="Gas consumed interval",
native_unit_of_measurement=VOLUME_CUBIC_METERS,
native_unit_of_measurement=UnitOfVolume.CUBIC_METERS,
device_class=SensorDeviceClass.GAS,
state_class=SensorStateClass.TOTAL,
),
SensorEntityDescription(
key="gas_consumed_cumulative",
name="Gas consumed cumulative",
native_unit_of_measurement=VOLUME_CUBIC_METERS,
native_unit_of_measurement=UnitOfVolume.CUBIC_METERS,
device_class=SensorDeviceClass.GAS,
state_class=SensorStateClass.TOTAL,
),

View file

@ -29,7 +29,7 @@ from homeassistant.const import (
POWER_KILO_WATT,
TEMP_CELSIUS,
TIME_MINUTES,
VOLUME_LITERS,
UnitOfVolume,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
@ -312,7 +312,7 @@ SENSOR_TYPES: tuple[RenaultSensorEntityDescription[Any], ...] = (
entity_class=RenaultSensor[KamereonVehicleCockpitData],
icon="mdi:fuel",
name="Fuel quantity",
native_unit_of_measurement=VOLUME_LITERS,
native_unit_of_measurement=UnitOfVolume.LITERS,
state_class=SensorStateClass.MEASUREMENT,
requires_fuel=True,
value_lambda=_get_rounded_value,

View file

@ -14,7 +14,7 @@ from homeassistant.const import (
LENGTH_KILOMETERS,
PERCENTAGE,
TEMP_CELSIUS,
VOLUME_LITERS,
UnitOfVolume,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
@ -160,7 +160,7 @@ class StarlineSensor(StarlineEntity, SensorEntity):
if type_value == "percents":
return PERCENTAGE
if type_value == "litres":
return VOLUME_LITERS
return UnitOfVolume.LITERS
return self.entity_description.native_unit_of_measurement
@property

View file

@ -18,8 +18,7 @@ from homeassistant.const import (
LENGTH_MILES,
PERCENTAGE,
PRESSURE_HPA,
VOLUME_GALLONS,
VOLUME_LITERS,
UnitOfVolume,
)
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import entity_registry as er
@ -55,7 +54,7 @@ _LOGGER = logging.getLogger(__name__)
FUEL_CONSUMPTION_LITERS_PER_HUNDRED_KILOMETERS = "L/100km"
FUEL_CONSUMPTION_MILES_PER_GALLON = "mi/gal"
L_PER_GAL = VolumeConverter.convert(1, VOLUME_GALLONS, VOLUME_LITERS)
L_PER_GAL = VolumeConverter.convert(1, UnitOfVolume.GALLONS, UnitOfVolume.LITERS)
KM_PER_MI = DistanceConverter.convert(1, LENGTH_MILES, LENGTH_KILOMETERS)
# Sensor available to "Subaru Safety Plus" subscribers with Gen1 or Gen2 vehicles

View file

@ -9,7 +9,7 @@ from surepy.enums import EntityType
from homeassistant.components.sensor import SensorDeviceClass, SensorEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_VOLTAGE, PERCENTAGE, VOLUME_MILLILITERS
from homeassistant.const import ATTR_VOLTAGE, PERCENTAGE, UnitOfVolume
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity import EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback
@ -88,7 +88,7 @@ class Felaqua(SurePetcareEntity, SensorEntity):
"""Sure Petcare Felaqua."""
_attr_device_class = SensorDeviceClass.VOLUME
_attr_native_unit_of_measurement = VOLUME_MILLILITERS
_attr_native_unit_of_measurement = UnitOfVolume.MILLILITERS
def __init__(
self, surepetcare_id: int, coordinator: SurePetcareDataCoordinator

View file

@ -15,7 +15,7 @@ from homeassistant.const import (
PERCENTAGE,
POWER_WATT,
TEMP_CELSIUS,
VOLUME_CUBIC_METERS,
UnitOfVolume,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
@ -164,7 +164,7 @@ SENSOR_ENTITIES: tuple[ToonSensorEntityDescription, ...] = (
section="gas_usage",
measurement="day_average",
device_class=SensorDeviceClass.GAS,
native_unit_of_measurement=VOLUME_CUBIC_METERS,
native_unit_of_measurement=UnitOfVolume.CUBIC_METERS,
entity_registry_enabled_default=False,
cls=ToonGasMeterDeviceSensor,
),
@ -174,7 +174,7 @@ SENSOR_ENTITIES: tuple[ToonSensorEntityDescription, ...] = (
section="gas_usage",
measurement="day_usage",
device_class=SensorDeviceClass.GAS,
native_unit_of_measurement=VOLUME_CUBIC_METERS,
native_unit_of_measurement=UnitOfVolume.CUBIC_METERS,
cls=ToonGasMeterDeviceSensor,
),
ToonSensorEntityDescription(
@ -193,7 +193,7 @@ SENSOR_ENTITIES: tuple[ToonSensorEntityDescription, ...] = (
name="Gas Meter",
section="gas_usage",
measurement="meter",
native_unit_of_measurement=VOLUME_CUBIC_METERS,
native_unit_of_measurement=UnitOfVolume.CUBIC_METERS,
state_class=SensorStateClass.TOTAL_INCREASING,
device_class=SensorDeviceClass.GAS,
cls=ToonGasMeterDeviceSensor,
@ -312,7 +312,7 @@ SENSOR_ENTITIES: tuple[ToonSensorEntityDescription, ...] = (
name="Average Daily Water Usage",
section="water_usage",
measurement="day_average",
native_unit_of_measurement=VOLUME_CUBIC_METERS,
native_unit_of_measurement=UnitOfVolume.CUBIC_METERS,
icon="mdi:water",
entity_registry_enabled_default=False,
cls=ToonWaterMeterDeviceSensor,
@ -323,7 +323,7 @@ SENSOR_ENTITIES: tuple[ToonSensorEntityDescription, ...] = (
name="Water Usage Today",
section="water_usage",
measurement="day_usage",
native_unit_of_measurement=VOLUME_CUBIC_METERS,
native_unit_of_measurement=UnitOfVolume.CUBIC_METERS,
icon="mdi:water",
entity_registry_enabled_default=False,
cls=ToonWaterMeterDeviceSensor,
@ -334,7 +334,7 @@ SENSOR_ENTITIES: tuple[ToonSensorEntityDescription, ...] = (
name="Water Meter",
section="water_usage",
measurement="meter",
native_unit_of_measurement=VOLUME_CUBIC_METERS,
native_unit_of_measurement=UnitOfVolume.CUBIC_METERS,
icon="mdi:water",
entity_registry_enabled_default=False,
state_class=SensorStateClass.TOTAL_INCREASING,

View file

@ -13,7 +13,7 @@ from homeassistant.const import (
CONF_DEVICE,
ENERGY_KILO_WATT_HOUR,
POWER_WATT,
VOLUME_CUBIC_METERS,
UnitOfVolume,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import DeviceInfo
@ -98,7 +98,7 @@ class YoulessBaseSensor(CoordinatorEntity, SensorEntity):
class GasSensor(YoulessBaseSensor):
"""The Youless gas sensor."""
_attr_native_unit_of_measurement = VOLUME_CUBIC_METERS
_attr_native_unit_of_measurement = UnitOfVolume.CUBIC_METERS
_attr_device_class = SensorDeviceClass.GAS
_attr_state_class = SensorStateClass.TOTAL_INCREASING