Exemplo n.º 1
0
def display_temp(opp: OpenPeerPower, temperature: Optional[float], unit: str,
                 precision: float) -> Optional[float]:
    """Convert temperature into preferred units/precision for display."""
    temperature_unit = unit
    op_unit = opp.config.units.temperature_unit

    if temperature is None:
        return temperature

    # If the temperature is not a number this can cause issues
    # with Polymer components, so bail early there.
    if not isinstance(temperature, Number):
        raise TypeError(f"Temperature is not a number: {temperature}")

    # type ignore: https://github.com/python/mypy/issues/7207
    if temperature_unit != op_unit:  # type: ignore
        temperature = convert_temperature(temperature, temperature_unit,
                                          op_unit)

    # Round in the units appropriate
    if precision == PRECISION_HALVES:
        temperature = round(temperature * 2) / 2.0
    elif precision == PRECISION_TENTHS:
        temperature = round(temperature, 1)
    # Integer as a fall back (PRECISION_WHOLE)
    else:
        temperature = round(temperature)

    return temperature
Exemplo n.º 2
0
 def temperature(self):
     """Return the current temperature."""
     temp_c = None
     if self.observation:
         temp_c = self.observation.get("temperature")
     if temp_c is not None:
         return convert_temperature(temp_c, TEMP_CELSIUS, TEMP_FAHRENHEIT)
     return None
Exemplo n.º 3
0
async def async_service_temperature_set(entity, service):
    """Handle set temperature service."""
    opp = entity.opp
    kwargs = {}

    for value, temp in service.data.items():
        if value in CONVERTIBLE_ATTRIBUTE:
            kwargs[value] = convert_temperature(
                temp, opp.config.units.temperature_unit, entity.temperature_unit
            )
        else:
            kwargs[value] = temp

    await entity.async_set_temperature(**kwargs)
Exemplo n.º 4
0
 def max_temp(self):
     """Return the maximum temperature."""
     max_temp = DEFAULT_MAX_TEMP
     return convert_temperature(max_temp, TEMP_CELSIUS, self.temperature_unit)
Exemplo n.º 5
0
 def min_temp(self):
     """Return the minimum temperature."""
     min_temp = DEFAULT_MIN_TEMP
     return convert_temperature(min_temp, TEMP_CELSIUS, self.temperature_unit)
Exemplo n.º 6
0
 def max_temp(self) -> float:
     """Return the maximum temperature."""
     return convert_temperature(DEFAULT_MAX_TEMP, TEMP_CELSIUS,
                                self.temperature_unit)
Exemplo n.º 7
0
 def current_temperature(self):
     """Return the current temperature."""
     # This field is not affected by temperatureUnit.
     # It is always in C
     return convert_temperature(self._measurements["temperature"],
                                TEMP_CELSIUS, self.temperature_unit)
Exemplo n.º 8
0
 def max_temp(self):
     """Return the maximum temperature."""
     return convert_temperature(
         DEFAULT_MAX_TEMP, TEMP_FAHRENHEIT, self.temperature_unit
     )
Exemplo n.º 9
0
SENSOR_EXPECTED_OBSERVATION_METRIC = {
    "dewpoint": "5",
    "temperature": "10",
    "windChill": "5",
    "heatIndex": "15",
    "relativeHumidity": "10",
    "windSpeed": "10",
    "windGust": "20",
    "windDirection": "180",
    "barometricPressure": "100000",
    "seaLevelPressure": "100000",
    "visibility": "10000",
}

SENSOR_EXPECTED_OBSERVATION_IMPERIAL = {
    "dewpoint": str(round(convert_temperature(5, TEMP_CELSIUS, TEMP_FAHRENHEIT))),
    "temperature": str(round(convert_temperature(10, TEMP_CELSIUS, TEMP_FAHRENHEIT))),
    "windChill": str(round(convert_temperature(5, TEMP_CELSIUS, TEMP_FAHRENHEIT))),
    "heatIndex": str(round(convert_temperature(15, TEMP_CELSIUS, TEMP_FAHRENHEIT))),
    "relativeHumidity": "10",
    "windSpeed": str(round(convert_distance(10, LENGTH_KILOMETERS, LENGTH_MILES))),
    "windGust": str(round(convert_distance(20, LENGTH_KILOMETERS, LENGTH_MILES))),
    "windDirection": "180",
    "barometricPressure": str(
        round(convert_pressure(100000, PRESSURE_PA, PRESSURE_INHG), 2)
    ),
    "seaLevelPressure": str(
        round(convert_pressure(100000, PRESSURE_PA, PRESSURE_INHG), 2)
    ),
    "visibility": str(round(convert_distance(10000, LENGTH_METERS, LENGTH_MILES))),
}