Use unit enums in energy and sensor (#84343)

This commit is contained in:
epenet 2022-12-21 10:49:51 +01:00 committed by GitHub
parent 255f35b979
commit 9a488cf250
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 15 deletions

View File

@ -11,11 +11,8 @@ from homeassistant.const import (
ATTR_DEVICE_CLASS,
STATE_UNAVAILABLE,
STATE_UNKNOWN,
VOLUME_CUBIC_FEET,
VOLUME_CUBIC_METERS,
VOLUME_GALLONS,
VOLUME_LITERS,
UnitOfEnergy,
UnitOfVolume,
)
from homeassistant.core import HomeAssistant, callback, valid_entity_id
@ -47,7 +44,7 @@ GAS_USAGE_UNITS = {
UnitOfEnergy.MEGA_WATT_HOUR,
UnitOfEnergy.GIGA_JOULE,
),
sensor.SensorDeviceClass.GAS: (VOLUME_CUBIC_METERS, VOLUME_CUBIC_FEET),
sensor.SensorDeviceClass.GAS: (UnitOfVolume.CUBIC_METERS, UnitOfVolume.CUBIC_FEET),
}
GAS_PRICE_UNITS = tuple(
f"/{unit}" for units in GAS_USAGE_UNITS.values() for unit in units
@ -57,10 +54,10 @@ GAS_PRICE_UNIT_ERROR = "entity_unexpected_unit_gas_price"
WATER_USAGE_DEVICE_CLASSES = (sensor.SensorDeviceClass.WATER,)
WATER_USAGE_UNITS = {
sensor.SensorDeviceClass.WATER: (
VOLUME_CUBIC_METERS,
VOLUME_CUBIC_FEET,
VOLUME_GALLONS,
VOLUME_LITERS,
UnitOfVolume.CUBIC_METERS,
UnitOfVolume.CUBIC_FEET,
UnitOfVolume.GALLONS,
UnitOfVolume.LITERS,
),
}
WATER_PRICE_UNITS = tuple(

View File

@ -45,9 +45,7 @@ from homeassistant.const import ( # noqa: F401, pylint: disable=[hass-deprecate
DEVICE_CLASS_TIMESTAMP,
DEVICE_CLASS_VOLATILE_ORGANIC_COMPOUNDS,
DEVICE_CLASS_VOLTAGE,
TEMP_CELSIUS,
TEMP_FAHRENHEIT,
TEMP_KELVIN,
UnitOfTemperature,
)
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import entity_registry as er
@ -737,7 +735,8 @@ class SensorEntity(Entity):
if (
self.device_class == DEVICE_CLASS_TEMPERATURE
and native_unit_of_measurement in (TEMP_CELSIUS, TEMP_FAHRENHEIT)
and native_unit_of_measurement
in {UnitOfTemperature.CELSIUS, UnitOfTemperature.FAHRENHEIT}
):
return self.hass.config.units.temperature_unit

View File

@ -6,7 +6,7 @@ from typing import Any
from homeassistant.const import (
ATTR_DEVICE_CLASS,
ATTR_UNIT_OF_MEASUREMENT,
TEMP_FAHRENHEIT,
UnitOfTemperature,
)
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.significant_change import (
@ -44,7 +44,7 @@ def async_check_significant_change(
absolute_change: float | None = None
percentage_change: float | None = None
if device_class == SensorDeviceClass.TEMPERATURE:
if new_attrs.get(ATTR_UNIT_OF_MEASUREMENT) == TEMP_FAHRENHEIT:
if new_attrs.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfTemperature.FAHRENHEIT:
absolute_change = 1.0
else:
absolute_change = 0.5