mirror of
https://github.com/home-assistant/core.git
synced 2025-11-15 05:50:13 +00:00
Enable unit conversion for DATA_SIZE (#84699)
This commit is contained in:
@@ -5,6 +5,7 @@ from homeassistant.const import (
|
||||
UNIT_NOT_RECOGNIZED_TEMPLATE,
|
||||
UnitOfDataRate,
|
||||
UnitOfEnergy,
|
||||
UnitOfInformation,
|
||||
UnitOfLength,
|
||||
UnitOfMass,
|
||||
UnitOfPower,
|
||||
@@ -155,6 +156,38 @@ class EnergyConverter(BaseUnitConverter):
|
||||
}
|
||||
|
||||
|
||||
class InformationConverter(BaseUnitConverter):
|
||||
"""Utility to convert information values."""
|
||||
|
||||
UNIT_CLASS = "information"
|
||||
NORMALIZED_UNIT = UnitOfInformation.BITS
|
||||
# Units in terms of bits
|
||||
_UNIT_CONVERSION: dict[str, float] = {
|
||||
UnitOfInformation.BITS: 1,
|
||||
UnitOfInformation.KILOBITS: 1 / 1e3,
|
||||
UnitOfInformation.MEGABITS: 1 / 1e6,
|
||||
UnitOfInformation.GIGABITS: 1 / 1e9,
|
||||
UnitOfInformation.BYTES: 1 / 8,
|
||||
UnitOfInformation.KILOBYTES: 1 / 8e3,
|
||||
UnitOfInformation.MEGABYTES: 1 / 8e6,
|
||||
UnitOfInformation.GIGABYTES: 1 / 8e9,
|
||||
UnitOfInformation.TERABYTES: 1 / 8e12,
|
||||
UnitOfInformation.PETABYTES: 1 / 8e15,
|
||||
UnitOfInformation.EXABYTES: 1 / 8e18,
|
||||
UnitOfInformation.ZETTABYTES: 1 / 8e21,
|
||||
UnitOfInformation.YOTTABYTES: 1 / 8e24,
|
||||
UnitOfInformation.KIBIBYTES: 1 / 2**13,
|
||||
UnitOfInformation.MEBIBYTES: 1 / 2**23,
|
||||
UnitOfInformation.GIBIBYTES: 1 / 2**33,
|
||||
UnitOfInformation.TEBIBYTES: 1 / 2**43,
|
||||
UnitOfInformation.PEBIBYTES: 1 / 2**53,
|
||||
UnitOfInformation.EXBIBYTES: 1 / 2**63,
|
||||
UnitOfInformation.ZEBIBYTES: 1 / 2**73,
|
||||
UnitOfInformation.YOBIBYTES: 1 / 2**83,
|
||||
}
|
||||
VALID_UNITS = set(UnitOfInformation)
|
||||
|
||||
|
||||
class MassConverter(BaseUnitConverter):
|
||||
"""Utility to convert mass values."""
|
||||
|
||||
|
||||
Reference in New Issue
Block a user