Exemplo n.º 1
0
def test_convert_invalid_unit():
    """Test exception is thrown for invalid units."""
    with pytest.raises(ValueError):
        volume_util.convert(5, INVALID_SYMBOL, VALID_SYMBOL)

    with pytest.raises(ValueError):
        volume_util.convert(5, VALID_SYMBOL, INVALID_SYMBOL)
Exemplo n.º 2
0
    def test_convert_invalid_unit(self):
        """Test exception is thrown for invalid units."""
        with pytest.raises(ValueError):
            volume_util.convert(5, INVALID_SYMBOL, VALID_SYMBOL)

        with pytest.raises(ValueError):
            volume_util.convert(5, VALID_SYMBOL, INVALID_SYMBOL)
Exemplo n.º 3
0
 def test_convert_same_unit(self):
     """Test conversion from any unit to same unit."""
     assert 2 == volume_util.convert(2, VOLUME_LITERS, VOLUME_LITERS)
     assert 3 == volume_util.convert(3, VOLUME_MILLILITERS,
                                     VOLUME_MILLILITERS)
     assert 4 == volume_util.convert(4, VOLUME_GALLONS, VOLUME_GALLONS)
     assert 5 == volume_util.convert(5, VOLUME_FLUID_OUNCE,
                                     VOLUME_FLUID_OUNCE)
Exemplo n.º 4
0
 def test_convert_same_unit(self):
     """Test conversion from any unit to same unit."""
     assert 2 == volume_util.convert(2, VOLUME_LITERS, VOLUME_LITERS)
     assert 3 == volume_util.convert(3, VOLUME_MILLILITERS,
                                     VOLUME_MILLILITERS)
     assert 4 == volume_util.convert(4, VOLUME_GALLONS,
                                     VOLUME_GALLONS)
     assert 5 == volume_util.convert(5, VOLUME_FLUID_OUNCE,
                                     VOLUME_FLUID_OUNCE)
Exemplo n.º 5
0
 def test_convert_same_unit(self):
     """Test conversion from any unit to same unit."""
     self.assertEqual(2, volume_util.convert(2, VOLUME_LITERS,
                                             VOLUME_LITERS))
     self.assertEqual(
         3, volume_util.convert(3, VOLUME_MILLILITERS, VOLUME_MILLILITERS))
     self.assertEqual(
         4, volume_util.convert(4, VOLUME_GALLONS, VOLUME_GALLONS))
     self.assertEqual(
         5, volume_util.convert(5, VOLUME_FLUID_OUNCE, VOLUME_FLUID_OUNCE))
Exemplo n.º 6
0
    def volume(self, volume: Optional[float], from_unit: str) -> float:
        """Convert the given volume to this unit system."""
        if not isinstance(volume, Number):
            raise TypeError(f"{volume!s} is not a numeric value.")

        # type ignore: https://github.com/python/mypy/issues/7207
        return volume_util.convert(volume, from_unit, self.volume_unit)  # type: ignore
Exemplo n.º 7
0
    def update(self):
        """Update current conditions."""
        self.flume.update_usage()
        gallons = round(self.flume.usage.get(self.sensor_type), 2)

        if self.config.get(CONF_UNIT_OF_MEASUREMENT) == VOLUME_GALLONS:
            self._state = gallons
        elif self.config.get(CONF_UNIT_OF_MEASUREMENT) == VOLUME_LITERS:
            self._state = round(convert(gallons, VOLUME_GALLONS, VOLUME_LITERS), 1)
Exemplo n.º 8
0
def test_convert_same_unit():
    """Test conversion from any unit to same unit."""
    assert volume_util.convert(2, VOLUME_LITERS, VOLUME_LITERS) == 2
    assert volume_util.convert(3, VOLUME_MILLILITERS, VOLUME_MILLILITERS) == 3
    assert volume_util.convert(4, VOLUME_GALLONS, VOLUME_GALLONS) == 4
    assert volume_util.convert(5, VOLUME_FLUID_OUNCE, VOLUME_FLUID_OUNCE) == 5
Exemplo n.º 9
0
STATISTICS_BAKERY = "recorder_statistics_bakery"
STATISTICS_META_BAKERY = "recorder_statistics_meta_bakery"
STATISTICS_SHORT_TERM_BAKERY = "recorder_statistics_short_term_bakery"

# Convert pressure and temperature statistics from the native unit used for statistics
# to the units configured by the user
UNIT_CONVERSIONS = {
    PRESSURE_PA:
    lambda x, units: pressure_util.convert(x, PRESSURE_PA, units.pressure_unit)
    if x is not None else None,
    TEMP_CELSIUS:
    lambda x, units: temperature_util.convert(
        x, TEMP_CELSIUS, units.temperature_unit) if x is not None else None,
    VOLUME_CUBIC_METERS:
    lambda x, units: volume_util.convert(
        x, VOLUME_CUBIC_METERS, _configured_unit(VOLUME_CUBIC_METERS, units))
    if x is not None else None,
}

_LOGGER = logging.getLogger(__name__)


def split_statistic_id(entity_id: str) -> list[str]:
    """Split a state entity ID into domain and object ID."""
    return entity_id.split(":", 1)


VALID_STATISTIC_ID = re.compile(
    r"^(?!.+__)(?!_)[\da-z_]+(?<!_):(?!_)[\da-z_]+(?<!_)$")

Exemplo n.º 10
0
 def test_convert_from_gallons(self):
     """Test conversion from gallons to other units."""
     gallons = 5
     assert volume_util.convert(gallons, VOLUME_GALLONS,
                                VOLUME_LITERS) == 18.925
Exemplo n.º 11
0
def test_convert_from_liters():
    """Test conversion from liters to other units."""
    liters = 5
    assert volume_util.convert(liters, VOLUME_LITERS, VOLUME_GALLONS) == 1.321
Exemplo n.º 12
0
    def volume(self, volume: Optional[float], from_unit: str) -> float:
        """Convert the given volume to this unit system."""
        if not isinstance(volume, Number):
            raise TypeError('{} is not a numeric value.'.format(str(volume)))

        return volume_util.convert(volume, from_unit, self.volume_unit)
Exemplo n.º 13
0
def test_convert_from_cubic_feet():
    """Test conversion from cubic feet to cubic meters to other units."""
    cubic_feets = 500
    assert (volume_util.convert(cubic_feets, VOLUME_CUBIC_FEET,
                                VOLUME_CUBIC_METERS) == 14.1584233)
Exemplo n.º 14
0
def test_convert_from_cubic_meters():
    """Test conversion from cubic meter to other units."""
    cubic_meters = 5
    assert (volume_util.convert(cubic_meters, VOLUME_CUBIC_METERS,
                                VOLUME_CUBIC_FEET) == 176.5733335)
Exemplo n.º 15
0
 def test_convert_from_gallons(self):
     """Test conversion from gallons to other units."""
     gallons = 5
     self.assertEqual(
         volume_util.convert(gallons, VOLUME_GALLONS, VOLUME_LITERS),
         18.925)
Exemplo n.º 16
0
 def test_convert_from_liters(self):
     """Test conversion from liters to other units."""
     liters = 5
     self.assertEqual(
         volume_util.convert(liters, VOLUME_LITERS, VOLUME_GALLONS), 1.321)
Exemplo n.º 17
0
def test_convert_nonnumeric_value():
    """Test exception is thrown for nonnumeric type."""
    with pytest.raises(TypeError):
        volume_util.convert('a', VOLUME_GALLONS, VOLUME_LITERS)
Exemplo n.º 18
0
 def test_convert_nonnumeric_value(self):
     """Test exception is thrown for nonnumeric type."""
     with pytest.raises(TypeError):
         volume_util.convert('a', VOLUME_GALLONS, VOLUME_LITERS)
Exemplo n.º 19
0
def test_convert_from_gallons():
    """Test conversion from gallons to other units."""
    gallons = 5
    assert volume_util.convert(gallons, VOLUME_GALLONS,
                               VOLUME_LITERS) == 18.925
Exemplo n.º 20
0
 def test_convert_from_liters(self):
     """Test conversion from liters to other units."""
     liters = 5
     assert volume_util.convert(liters, VOLUME_LITERS,
                                VOLUME_GALLONS) == 1.321