def evaluate_temperature( raw_temperature_line: str, raw_temperature_to_degree_celsius_factor: float, target_temperature_unit: Unit, sensor_type: Sensor, sensor_id: str, sensor_offset: float, sensor_reset_value: float, ) -> float: factor = Unit.get_conversion_function(Unit.DEGREES_C, target_temperature_unit) if sensor_type.comply_12bit_standard(): value = float( convert_raw_temperature_to_sensor_count(raw_temperature_line)) # the int part is 8 bit wide, 4 bit are left on 12 bit # so divide with 2^4 = 16 to get the celsius fractions value /= 16.0 # check if the sensor value is the reset value if value == sensor_reset_value: raise ResetValueError(sensor_id) else: # Fallback to precalculated value for other sensor types value = get_raw_temperature(raw_temperature_line) value *= raw_temperature_to_degree_celsius_factor return factor(value + sensor_offset)
def get_offset(self, unit: Unit = Unit.DEGREES_C) -> float: """Get the offset set for this sensor. If no offset has been set, 0.0 is returned. :param: int unit: The unit to return the temperature offset in :returns: The offset set for this temperature sensor :rtype: float """ # We need to subtract `factor(0)` from the result, in order to # eliminate any offset temperatures used in the conversion formulas # (such as 32F, when converting from C to F). factor = Unit.get_conversion_function(Unit.DEGREES_C, unit) return factor(self.offset) - factor(0)
def get_temperatures(self, units: Iterable[Unit]) -> List[float]: """Returns the temperatures in the specified units :param list units: the units for the sensor temperature :returns: the sensor temperature in the given units. The order of the temperatures matches the order of the given units. :rtype: list :raises UnsupportedUnitError: if the unit is not supported :raises NoSensorFoundError: if the sensor could not be found :raises SensorNotReadyError: if the sensor is not ready yet """ sensor_value = self.get_temperature(Unit.DEGREES_C) return [ Unit.get_conversion_function(Unit.DEGREES_C, unit)(sensor_value) for unit in units ]
def set_offset(self, offset: float, unit: Unit = Unit.DEGREES_C) -> None: """Set an offset to be applied to each temperature reading. This is used to tune sensors which report values which are either too high or too low. The offset is converted as needed when getting temperatures in other units than Celcius. :param float offset: The value to add or subtract from the temperature measurement. Positive values increase themperature, negative values decrease temperature. :param: int unit: The unit in which offset is expressed. Default is Celcius. :rtype: None """ # We need to subtract `factor(0)` from the result, in order to # eliminate any offset temperatures used in the conversion formulas # (such as 32F, when converting from C to F). factor = Unit.get_conversion_function(unit, Unit.DEGREES_C) self.offset = factor(offset) - factor(0)