示例#1
0
def test_convert_invalid_unit():
    """Test exception is thrown for invalid units."""
    with pytest.raises(ValueError):
        speed_util.convert(5, INVALID_SYMBOL, VALID_SYMBOL)

    with pytest.raises(ValueError):
        speed_util.convert(5, VALID_SYMBOL, INVALID_SYMBOL)
示例#2
0
    def wind_speed(self, wind_speed: float | None, from_unit: str) -> float:
        """Convert the given wind_speed to this unit system."""
        if not isinstance(wind_speed, Number):
            raise TypeError(f"{wind_speed!s} is not a numeric value.")

        # type ignore: https://github.com/python/mypy/issues/7207
        return speed_util.convert(wind_speed, from_unit, self.wind_speed_unit)  # type: ignore
示例#3
0
 def extra_state_attributes(self) -> Mapping[str, Any] | None:
     """Return additional attributes."""
     if self._forecasts:
         wind_gust = speed_util.convert(
             self._forecasts[0].wind_gust,
             SPEED_METERS_PER_SECOND,
             self._wind_speed_unit,
         )
         return {
             ATTR_SMHI_CLOUDINESS: self._forecasts[0].cloudiness,
             ATTR_SMHI_WIND_GUST_SPEED: round(wind_gust,
                                              ROUNDING_PRECISION),
             ATTR_SMHI_THUNDER_PROBABILITY: self._forecasts[0].thunder,
         }
     return None
示例#4
0
def test_convert_same_unit():
    """Test conversion from any unit to same unit."""
    assert speed_util.convert(2, SPEED_INCHES_PER_DAY, SPEED_INCHES_PER_DAY) == 2
    assert speed_util.convert(3, SPEED_INCHES_PER_HOUR, SPEED_INCHES_PER_HOUR) == 3
    assert (
        speed_util.convert(4, SPEED_KILOMETERS_PER_HOUR, SPEED_KILOMETERS_PER_HOUR) == 4
    )
    assert speed_util.convert(5, SPEED_METERS_PER_SECOND, SPEED_METERS_PER_SECOND) == 5
    assert speed_util.convert(6, SPEED_MILES_PER_HOUR, SPEED_MILES_PER_HOUR) == 6
    assert (
        speed_util.convert(7, SPEED_MILLIMETERS_PER_DAY, SPEED_MILLIMETERS_PER_DAY) == 7
    )
示例#5
0
def test_convert_different_units(from_value, from_unit, expected, to_unit):
    """Test conversion between units."""
    assert speed_util.convert(from_value, from_unit,
                              to_unit) == pytest.approx(expected, rel=1e-4)
示例#6
0
def test_convert_nonnumeric_value():
    """Test exception is thrown for nonnumeric type."""
    with pytest.raises(TypeError):
        speed_util.convert("a", SPEED_KILOMETERS_PER_HOUR,
                           SPEED_MILES_PER_HOUR)