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)
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
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
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 )
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)
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)