Beispiel #1
0
def convert(value: float, unit_1: str, unit_2: str) -> float:
    """Convert one unit of measurement to another."""
    if unit_1 not in VALID_UNITS:
        raise ValueError(
            UNIT_NOT_RECOGNIZED_TEMPLATE.format(unit_1, LENGTH))
    if unit_2 not in VALID_UNITS:
        raise ValueError(
            UNIT_NOT_RECOGNIZED_TEMPLATE.format(unit_2, LENGTH))

    if not isinstance(value, Number):
        raise TypeError('{} is not of numeric type'.format(value))

    if unit_1 == unit_2 or unit_1 not in VALID_UNITS:
        return value

    meters = value

    if unit_1 == LENGTH_MILES:
        meters = __miles_to_meters(value)
    elif unit_1 == LENGTH_FEET:
        meters = __feet_to_meters(value)
    elif unit_1 == LENGTH_KILOMETERS:
        meters = __kilometers_to_meters(value)

    result = meters

    if unit_2 == LENGTH_MILES:
        result = __meters_to_miles(meters)
    elif unit_2 == LENGTH_FEET:
        result = __meters_to_feet(meters)
    elif unit_2 == LENGTH_KILOMETERS:
        result = __meters_to_kilometers(meters)

    return result
Beispiel #2
0
def convert(
    temperature: float, from_unit: str, to_unit: str, interval: bool = False
) -> float:
    """Convert a temperature from one unit to another."""
    if from_unit not in (TEMP_CELSIUS, TEMP_FAHRENHEIT, TEMP_KELVIN):
        raise ValueError(UNIT_NOT_RECOGNIZED_TEMPLATE.format(from_unit, TEMPERATURE))
    if to_unit not in (TEMP_CELSIUS, TEMP_FAHRENHEIT, TEMP_KELVIN):
        raise ValueError(UNIT_NOT_RECOGNIZED_TEMPLATE.format(to_unit, TEMPERATURE))

    if from_unit == to_unit:
        return temperature

    if from_unit == TEMP_CELSIUS:
        if to_unit == TEMP_FAHRENHEIT:
            return celsius_to_fahrenheit(temperature, interval)
        # kelvin
        return celsius_to_kelvin(temperature, interval)

    if from_unit == TEMP_FAHRENHEIT:
        if to_unit == TEMP_CELSIUS:
            return fahrenheit_to_celsius(temperature, interval)
        # kelvin
        return celsius_to_kelvin(fahrenheit_to_celsius(temperature, interval), interval)

    # from_unit == kelvin
    if to_unit == TEMP_CELSIUS:
        return kelvin_to_celsius(temperature, interval)
    # fahrenheit
    return celsius_to_fahrenheit(kelvin_to_celsius(temperature, interval), interval)
Beispiel #3
0
def convert(volume: float, from_unit: str, to_unit: str) -> float:
    """Convert a temperature from one unit to another."""
    if from_unit not in VALID_UNITS:
        raise ValueError(UNIT_NOT_RECOGNIZED_TEMPLATE.format(
            from_unit, VOLUME))
    if to_unit not in VALID_UNITS:
        raise ValueError(UNIT_NOT_RECOGNIZED_TEMPLATE.format(to_unit, VOLUME))

    if not isinstance(volume, Number):
        raise TypeError(f"{volume} is not of numeric type")

    if from_unit == to_unit:
        return volume

    result: float = volume
    if from_unit == VOLUME_LITERS and to_unit == VOLUME_GALLONS:
        result = liter_to_gallon(volume)
    elif from_unit == VOLUME_GALLONS and to_unit == VOLUME_LITERS:
        result = gallon_to_liter(volume)
    elif from_unit == VOLUME_CUBIC_METERS and to_unit == VOLUME_CUBIC_FEET:
        result = cubic_meter_to_cubic_feet(volume)
    elif from_unit == VOLUME_CUBIC_FEET and to_unit == VOLUME_CUBIC_METERS:
        result = cubic_feet_to_cubic_meter(volume)

    return result
Beispiel #4
0
def convert(value: float, unit_1: str, unit_2: str) -> float:
    """Convert one unit of measurement to another."""
    if unit_1 not in VALID_UNITS:
        raise ValueError(UNIT_NOT_RECOGNIZED_TEMPLATE.format(unit_1, LENGTH))
    if unit_2 not in VALID_UNITS:
        raise ValueError(UNIT_NOT_RECOGNIZED_TEMPLATE.format(unit_2, LENGTH))

    if not isinstance(value, Number):
        raise TypeError('{} is not of numeric type'.format(value))

    if unit_1 == unit_2 or unit_1 not in VALID_UNITS:
        return value

    meters = value

    if unit_1 == LENGTH_MILES:
        meters = __miles_to_meters(value)
    elif unit_1 == LENGTH_FEET:
        meters = __feet_to_meters(value)
    elif unit_1 == LENGTH_KILOMETERS:
        meters = __kilometers_to_meters(value)

    result = meters

    if unit_2 == LENGTH_MILES:
        result = __meters_to_miles(meters)
    elif unit_2 == LENGTH_FEET:
        result = __meters_to_feet(meters)
    elif unit_2 == LENGTH_KILOMETERS:
        result = __meters_to_kilometers(meters)

    return result
def convert(temperature: float, from_unit: str, to_unit: str) -> float:
    """Convert a temperature from one unit to another."""
    if from_unit not in (TEMP_CELSIUS, TEMP_FAHRENHEIT):
        raise ValueError(
            UNIT_NOT_RECOGNIZED_TEMPLATE.format(from_unit, TEMPERATURE))
    if to_unit not in (TEMP_CELSIUS, TEMP_FAHRENHEIT):
        raise ValueError(
            UNIT_NOT_RECOGNIZED_TEMPLATE.format(to_unit, TEMPERATURE))

    if from_unit == to_unit:
        return temperature
    elif from_unit == TEMP_CELSIUS:
        return celsius_to_fahrenheit(temperature)
    return fahrenheit_to_celsius(temperature)
def convert(temperature: float, from_unit: str, to_unit: str) -> float:
    """Convert a temperature from one unit to another."""
    if from_unit not in (TEMP_CELSIUS, TEMP_FAHRENHEIT):
        raise ValueError(UNIT_NOT_RECOGNIZED_TEMPLATE.format(
            from_unit, TEMPERATURE))
    if to_unit not in (TEMP_CELSIUS, TEMP_FAHRENHEIT):
        raise ValueError(UNIT_NOT_RECOGNIZED_TEMPLATE.format(
            to_unit, TEMPERATURE))

    if from_unit == to_unit:
        return temperature
    elif from_unit == TEMP_CELSIUS:
        return celsius_to_fahrenheit(temperature)
    return fahrenheit_to_celsius(temperature)
def convert(value: float, unit_1: str, unit_2: str) -> float:
    """Convert one unit of measurement to another."""
    if unit_1 not in VALID_UNITS:
        raise ValueError(UNIT_NOT_RECOGNIZED_TEMPLATE.format(unit_1, PRESSURE))
    if unit_2 not in VALID_UNITS:
        raise ValueError(UNIT_NOT_RECOGNIZED_TEMPLATE.format(unit_2, PRESSURE))

    if not isinstance(value, Number):
        raise TypeError(f"{value} is not of numeric type")

    if unit_1 == unit_2 or unit_1 not in VALID_UNITS:
        return value

    pascals = value / UNIT_CONVERSION[unit_1]
    return pascals * UNIT_CONVERSION[unit_2]
Beispiel #8
0
    def __init__(
        self,
        name: str,
        temperature: str,
        length: str,
        wind_speed: str,
        volume: str,
        mass: str,
        pressure: str,
        accumulated_precipitation: str,
    ) -> None:
        """Initialize the unit system object."""
        errors: str = ", ".join(
            UNIT_NOT_RECOGNIZED_TEMPLATE.format(unit, unit_type)
            for unit, unit_type in (
                (accumulated_precipitation, ACCUMULATED_PRECIPITATION),
                (temperature, TEMPERATURE),
                (length, LENGTH),
                (wind_speed, WIND_SPEED),
                (volume, VOLUME),
                (mass, MASS),
                (pressure, PRESSURE),
            ) if not is_valid_unit(unit, unit_type))

        if errors:
            raise ValueError(errors)

        self.name = name
        self.accumulated_precipitation_unit = accumulated_precipitation
        self.temperature_unit = temperature
        self.length_unit = length
        self.mass_unit = mass
        self.pressure_unit = pressure
        self.volume_unit = volume
        self.wind_speed_unit = wind_speed
Beispiel #9
0
def convert(value: float, unit_1: str, unit_2: str) -> float:
    """Convert one unit of measurement to another."""
    if unit_1 not in VALID_UNITS:
        raise ValueError(UNIT_NOT_RECOGNIZED_TEMPLATE.format(unit_1, PRESSURE))
    if unit_2 not in VALID_UNITS:
        raise ValueError(UNIT_NOT_RECOGNIZED_TEMPLATE.format(unit_2, PRESSURE))

    if not isinstance(value, Number):
        raise TypeError("{} is not of numeric type".format(value))

    # type ignore: https://github.com/python/mypy/issues/7207
    if unit_1 == unit_2 or unit_1 not in VALID_UNITS:  # type: ignore
        return value

    pascals = value / UNIT_CONVERSION[unit_1]
    return pascals * UNIT_CONVERSION[unit_2]
Beispiel #10
0
    def __init__(
        self,
        name: str,
        temperature: str,
        length: str,
        volume: str,
        mass: str,
        pressure: str,
    ) -> None:
        """Initialize the unit system object."""
        errors: str = ", ".join(
            UNIT_NOT_RECOGNIZED_TEMPLATE.format(unit, unit_type)
            for unit, unit_type in [
                (temperature, TEMPERATURE),
                (length, LENGTH),
                (volume, VOLUME),
                (mass, MASS),
                (pressure, PRESSURE),
            ]
            if not is_valid_unit(unit, unit_type)
        )

        if errors:
            raise ValueError(errors)

        self.name = name
        self.temperature_unit = temperature
        self.length_unit = length
        self.mass_unit = mass
        self.pressure_unit = pressure
        self.volume_unit = volume
def convert(value: float, unit_1: str, unit_2: str) -> float:
    """Convert one unit of measurement to another."""
    if unit_1 not in VALID_UNITS:
        raise ValueError(UNIT_NOT_RECOGNIZED_TEMPLATE.format(unit_1, LENGTH))
    if unit_2 not in VALID_UNITS:
        raise ValueError(UNIT_NOT_RECOGNIZED_TEMPLATE.format(unit_2, LENGTH))

    if not isinstance(value, Number):
        raise TypeError(f"{value} is not of numeric type")

    if unit_1 == unit_2 or unit_1 not in VALID_UNITS:
        return value

    meters: float = TO_METERS[unit_1](value)

    return METERS_TO[unit_2](meters)
Beispiel #12
0
    def _get_temperature(self, entity) -> float:
        """Get temperature value from entity and convert it to
        Home Assistant common configured units."""
        if isinstance(entity, WeatherEntity):
            temperature = entity.temperature
            entity_unit = entity.temperature_unit
        elif isinstance(entity, (ClimateDevice, WaterHeaterDevice)):
            temperature = entity.current_temperature
            entity_unit = entity.temperature_unit
        else:
            temperature = entity.state
            entity_unit = entity.attributes.get(ATTR_UNIT_OF_MEASUREMENT)

        if self._has_state(temperature):
            if entity_unit not in (TEMP_CELSIUS, TEMP_FAHRENHEIT):
                raise ValueError(
                    UNIT_NOT_RECOGNIZED_TEMPLATE.format(entity_unit,
                                                        TEMPERATURE))

            temperature = float(temperature)
            ha_unit = self._hass.config.units.temperature_unit

            if entity_unit != ha_unit:
                temperature = convert_temperature(
                    temperature, entity_unit, ha_unit)

        return temperature
    def _temperature_index(self):
        """Transform indoor temperature values to IAQ points according
        to Indoor Air Quality UK: http://www.iaquk.org.uk/ """
        entity_id = self._sources.get(CONF_TEMPERATURE)

        if entity_id is None:
            return None

        entity = self._hass.states.get(entity_id)
        value = self._get_number_state(entity_id)
        _LOGGER.debug('[%s] temperature=%s', self._entity_id, value)
        if value is None:
            return None

        entity_unit = entity.attributes.get(ATTR_UNIT_OF_MEASUREMENT)
        if entity_unit not in (TEMP_CELSIUS, TEMP_FAHRENHEIT):
            raise ValueError(
                UNIT_NOT_RECOGNIZED_TEMPLATE.format(entity_unit, TEMPERATURE))

        if entity_unit != TEMP_CELSIUS:
            value = convert_temperature(value, entity_unit, TEMP_CELSIUS)

        index = 1
        if 18 <= value <= 21:  # °C
            index = 5
        elif value > 16 or value < 23:  # °C
            index = 4
        elif value > 15 or value < 24:  # °C
            index = 3
        elif value > 14 or value < 25:  # °C
            index = 2
        return index
Beispiel #14
0
    def _temperature_index(self) -> Optional[int]:
        """Transform indoor temperature values to IAQ points."""
        entity_id = self._sources.get(CONF_TEMPERATURE)
        if entity_id is None:
            return None

        value = self._get_number_state(entity_id, source_type=CONF_TEMPERATURE)
        if value is None:
            return None

        entity = self.hass.states.get(entity_id)
        entity_unit = entity.attributes.get(ATTR_UNIT_OF_MEASUREMENT)
        if entity_unit not in (TEMP_CELSIUS, TEMP_FAHRENHEIT):
            raise ValueError(
                UNIT_NOT_RECOGNIZED_TEMPLATE.format(entity_unit, TEMPERATURE))

        if entity_unit != TEMP_CELSIUS:
            value = convert_temperature(value, entity_unit, TEMP_CELSIUS)

        index = 1
        if 18 <= value <= 21:  # °C
            index = 5
        elif 16 < value < 23:  # °C
            index = 4
        elif 15 < value < 24:  # °C
            index = 3
        elif 14 < value < 25:  # °C
            index = 2
        return index
Beispiel #15
0
    def _get_temperature(self, entity) -> Optional[float]:
        """Get temperature value from entity."""
        if isinstance(entity, WeatherEntity):
            temperature = entity.temperature
            entity_unit = entity.temperature_unit
        elif isinstance(entity, (ClimateDevice, WaterHeaterDevice)):
            temperature = entity.current_temperature
            entity_unit = entity.temperature_unit
        else:
            temperature = entity.state
            entity_unit = entity.attributes.get(ATTR_UNIT_OF_MEASUREMENT)

        if not self._has_state(temperature):
            return None

        if entity_unit not in TEMPERATURE_UNITS:
            raise ValueError(
                UNIT_NOT_RECOGNIZED_TEMPLATE.format(entity_unit, TEMPERATURE))

        temperature = float(temperature)
        ha_unit = self._hass.config.units.temperature_unit

        if entity_unit != ha_unit:
            temperature = convert_temperature(temperature, entity_unit,
                                              ha_unit)

        return temperature
Beispiel #16
0
def convert(value: float, unit_1: str, unit_2: str) -> float:
    """Convert one unit of measurement to another."""
    if unit_1 not in VALID_UNITS:
        raise ValueError(
            UNIT_NOT_RECOGNIZED_TEMPLATE.format(unit_1, PRESSURE))
    if unit_2 not in VALID_UNITS:
        raise ValueError(
            UNIT_NOT_RECOGNIZED_TEMPLATE.format(unit_2, PRESSURE))

    if not isinstance(value, Number):
        raise TypeError('{} is not of numeric type'.format(value))

    if unit_1 == unit_2 or unit_1 not in VALID_UNITS:
        return value

    pascals = value / UNIT_CONVERSION[unit_1]
    return pascals * UNIT_CONVERSION[unit_2]
Beispiel #17
0
def convert(volume: float, from_unit: str, to_unit: str) -> float:
    """Convert a temperature from one unit to another."""
    if from_unit not in VALID_UNITS:
        raise ValueError(UNIT_NOT_RECOGNIZED_TEMPLATE.format(from_unit,
                                                             VOLUME))
    if to_unit not in VALID_UNITS:
        raise ValueError(UNIT_NOT_RECOGNIZED_TEMPLATE.format(to_unit, VOLUME))

    if not isinstance(volume, Number):
        raise TypeError('{} is not of numeric type'.format(volume))

    if from_unit == to_unit:
        return volume

    result = volume
    if from_unit == VOLUME_LITERS and to_unit == VOLUME_GALLONS:
        result = __liter_to_gallon(volume)
    elif from_unit == VOLUME_GALLONS and to_unit == VOLUME_LITERS:
        result = __gallon_to_liter(volume)

    return result
Beispiel #18
0
def convert(volume: float, from_unit: str, to_unit: str) -> float:
    """Convert a temperature from one unit to another."""
    if from_unit not in VALID_UNITS:
        raise ValueError(UNIT_NOT_RECOGNIZED_TEMPLATE.format(
            from_unit, VOLUME))
    if to_unit not in VALID_UNITS:
        raise ValueError(UNIT_NOT_RECOGNIZED_TEMPLATE.format(to_unit, VOLUME))

    if not isinstance(volume, Number):
        raise TypeError(f"{volume} is not of numeric type")

    # type ignore: https://github.com/python/mypy/issues/7207
    if from_unit == to_unit:  # type: ignore
        return volume

    result = volume
    if from_unit == VOLUME_LITERS and to_unit == VOLUME_GALLONS:
        result = __liter_to_gallon(volume)
    elif from_unit == VOLUME_GALLONS and to_unit == VOLUME_LITERS:
        result = __gallon_to_liter(volume)

    return result
Beispiel #19
0
    def __init__(self: object, name: str, temperature: str, length: str,
                 volume: str, mass: str) -> None:
        """Initialize the unit system object."""
        errors = \
            ', '.join(UNIT_NOT_RECOGNIZED_TEMPLATE.format(unit, unit_type)
                      for unit, unit_type in [
                          (temperature, TEMPERATURE),
                          (length, LENGTH),
                          (volume, VOLUME),
                          (mass, MASS), ]
                      if not is_valid_unit(unit, unit_type))  # type: str

        if errors:
            raise ValueError(errors)

        self.name = name
        self.temperature_unit = temperature
        self.length_unit = length
        self.mass_unit = mass
        self.volume_unit = volume