Exemple #1
0
def convert(temperature, unit, to_unit):
    """Convert temperature to correct unit."""
    if unit == to_unit or unit is None or to_unit is None:
        return temperature
    elif unit == TEMP_CELSIUS:
        return temp_util.celsius_to_fahrenheit(temperature)

    return temp_util.fahrenheit_to_celsius(temperature)
Exemple #2
0
 def update(self):
     """Read from sensor and update the state."""
     self._mhz_client.update()
     data = self._mhz_client.data
     self._temperature = data.get(SENSOR_TEMPERATURE)
     if self._temperature is not None and \
             self._temp_unit == TEMP_FAHRENHEIT:
         self._temperature = round(
             celsius_to_fahrenheit(self._temperature), 1)
     self._ppm = data.get(SENSOR_CO2)
Exemple #3
0
    def update(self):
        """Get the latest data from the DHT and updates the states."""
        self.dht_client.update()
        data = self.dht_client.data

        if self.type == 'temperature':
            self._state = round(data['temperature'], 1)
            if self.temp_unit == TEMP_FAHRENHEIT:
                self._state = round(celsius_to_fahrenheit(data['temperature']),
                                    1)
        elif self.type == 'humidity':
            self._state = round(data['humidity'], 1)
Exemple #4
0
 def async_update(self):
     """Get the latest data from the HTU21D sensor and update the state."""
     yield from self.hass.async_add_job(self._client.update)
     if self._client.sensor.sample_ok:
         if self._variable == SENSOR_TEMPERATURE:
             value = round(self._client.sensor.temperature, 1)
             if self.unit_of_measurement == TEMP_FAHRENHEIT:
                 value = celsius_to_fahrenheit(value)
         else:
             value = round(self._client.sensor.humidity, 1)
         self._state = value
     else:
         _LOGGER.warning("Bad sample")
Exemple #5
0
    def update(self):
        """Get the latest data from the DHT and updates the states."""
        self.dht_client.update()
        data = self.dht_client.data

        if self.type == SENSOR_TEMPERATURE:
            temperature = round(data[SENSOR_TEMPERATURE], 1)
            if (temperature >= -20) and (temperature < 80):
                self._state = temperature
                if self.temp_unit == TEMP_FAHRENHEIT:
                    self._state = round(celsius_to_fahrenheit(temperature), 1)
        elif self.type == SENSOR_HUMIDITY:
            humidity = round(data[SENSOR_HUMIDITY], 1)
            if (humidity >= 0) and (humidity <= 100):
                self._state = humidity
Exemple #6
0
 async def async_update(self):
     """Get the latest data from the BME280 and update the states."""
     await self.hass.async_add_job(self.bme280_client.update)
     if self.bme280_client.sensor.sample_ok:
         if self.type == SENSOR_TEMP:
             temperature = round(self.bme280_client.sensor.temperature, 1)
             if self.temp_unit == TEMP_FAHRENHEIT:
                 temperature = round(celsius_to_fahrenheit(temperature), 1)
             self._state = temperature
         elif self.type == SENSOR_HUMID:
             self._state = round(self.bme280_client.sensor.humidity, 1)
         elif self.type == SENSOR_PRESS:
             self._state = round(self.bme280_client.sensor.pressure, 1)
     else:
         _LOGGER.warning("Bad update of sensor.%s", self.name)
Exemple #7
0
 async def async_update(self):
     """Get the latest data from the BME680 and update the states."""
     await self.hass.async_add_job(self.bme680_client.update)
     if self.type == SENSOR_TEMP:
         temperature = round(self.bme680_client.sensor_data.temperature, 1)
         if self.temp_unit == TEMP_FAHRENHEIT:
             temperature = round(celsius_to_fahrenheit(temperature), 1)
         self._state = temperature
     elif self.type == SENSOR_HUMID:
         self._state = round(self.bme680_client.sensor_data.humidity, 1)
     elif self.type == SENSOR_PRESS:
         self._state = round(self.bme680_client.sensor_data.pressure, 1)
     elif self.type == SENSOR_GAS:
         self._state = int(
             round(self.bme680_client.sensor_data.gas_resistance, 0))
     elif self.type == SENSOR_AQ:
         aq_score = self.bme680_client.sensor_data.air_quality
         if aq_score is not None:
             self._state = round(aq_score, 1)
Exemple #8
0
    def update(self):
        """
        Update current conditions.

        This uses a rolling median over 3 values to filter out outliers.
        """
        try:
            _LOGGER.debug("Polling data for %s", self.name)
            data = self.poller.parameter_value(self.parameter)
        except (OSError, BluetoothBackendException) as err:
            _LOGGER.info("Polling error %s: %s", type(err).__name__, err)
            return

        if data is not None:
            _LOGGER.debug("%s = %s", self.name, data)
            if self._unit == TEMP_FAHRENHEIT:
                data = celsius_to_fahrenheit(data)
            self.data.append(data)
            self.last_successful_update = dt_util.utcnow()
        else:
            _LOGGER.info("Did not receive any data from Mi Flora sensor %s",
                         self.name)
            # Remove old data from median list or set sensor value to None
            # if no data is available anymore
            if self.data:
                self.data = self.data[1:]
            else:
                self._state = None
            return

        _LOGGER.debug("Data collected: %s", self.data)
        if len(self.data) > self.median_count:
            self.data = self.data[1:]

        if len(self.data) == self.median_count:
            median = sorted(self.data)[int((self.median_count - 1) / 2)]
            _LOGGER.debug("Median is: %s", median)
            self._state = median
        elif self._state is None:
            _LOGGER.debug("Set initial state")
            self._state = self.data[0]
        else:
            _LOGGER.debug("Not yet enough data for median calculation")
Exemple #9
0
    def state(self):
        """Return the state."""
        attr = getattr(self._api.storage, self.sensor_type)(self.monitored_device)
        if attr is None:
            return None

        # Data (disk space)
        if self._unit == DATA_TERABYTES:
            return round(attr / 1024.0 ** 4, 2)

        # Temperature
        if self._api.temp_unit == TEMP_CELSIUS:
            # Celsius
            return attr
        if self.sensor_type in TEMP_SENSORS_KEYS:
            # Fahrenheit
            return celsius_to_fahrenheit(attr)

        return attr
Exemple #10
0
 def mean_temperature(self) -> Optional[float]:
     """Mean temperature of values collected, or None if all samples were spikes."""
     try:
         avg = sts.mean(self._temperature_measurements)
     except (AssertionError, sts.StatisticsError):
         return None
     if self._report_fahrenheit:
         avg = celsius_to_fahrenheit(avg)
     calibrated_avg = avg + self._calibrate_temp
     if self._decimal_places is not None:
         calibrated_avg = float(round(calibrated_avg, self._decimal_places))
     _LOGGER.debug(
         "%s: reporting %s (%s + %s)",
         self.mac,
         calibrated_avg,
         avg,
         self._calibrate_temp,
     )
     return calibrated_avg
Exemple #11
0
 async def async_update(self):
     """Get the latest data from the BME680 and update the states."""
     await self.hass.async_add_job(self.bme680_client.update)
     if self.type == SENSOR_TEMP:
         temperature = round(self.bme680_client.sensor_data.temperature, 1)
         if self.temp_unit == TEMP_FAHRENHEIT:
             temperature = round(celsius_to_fahrenheit(temperature), 1)
         self._state = temperature
     elif self.type == SENSOR_HUMID:
         self._state = round(self.bme680_client.sensor_data.humidity, 1)
     elif self.type == SENSOR_PRESS:
         self._state = round(self.bme680_client.sensor_data.pressure, 1)
     elif self.type == SENSOR_GAS:
         self._state = int(
             round(self.bme680_client.sensor_data.gas_resistance, 0)
         )
     elif self.type == SENSOR_AQ:
         aq_score = self.bme680_client.sensor_data.air_quality
         if aq_score is not None:
             self._state = round(aq_score, 1)
Exemple #12
0
    def update(self):
        """Get the latest data from the DHT and updates the states."""
        self.dht_client.update()
        temperature_offset = self.temperature_offset
        humidity_offset = self.humidity_offset
        data = self.dht_client.data

        if self.type == SENSOR_TEMPERATURE and SENSOR_TEMPERATURE in data:
            temperature = data[SENSOR_TEMPERATURE]
            _LOGGER.debug("Temperature %.1f \u00b0C + offset %.1f",
                          temperature, temperature_offset)
            if -20 <= temperature < 80:
                self._state = round(temperature + temperature_offset, 1)
                if self.temp_unit == TEMP_FAHRENHEIT:
                    self._state = round(celsius_to_fahrenheit(temperature), 1)
        elif self.type == SENSOR_HUMIDITY and SENSOR_HUMIDITY in data:
            humidity = data[SENSOR_HUMIDITY]
            _LOGGER.debug("Humidity %.1f%% + offset %.1f",
                          humidity, humidity_offset)
            if 0 <= humidity <= 100:
                self._state = round(humidity + humidity_offset, 1)
Exemple #13
0
    def update(self):
        """Get the latest data from the DHT and updates the states."""
        self.dht_client.update()
        temperature_offset = self.temperature_offset
        humidity_offset = self.humidity_offset
        data = self.dht_client.data

        if self.type == SENSOR_TEMPERATURE and SENSOR_TEMPERATURE in data:
            temperature = data[SENSOR_TEMPERATURE]
            _LOGGER.debug("Temperature %.1f \u00b0C + offset %.1f",
                          temperature, temperature_offset)
            if -20 <= temperature < 80:
                self._state = round(temperature + temperature_offset, 1)
                if self.temp_unit == TEMP_FAHRENHEIT:
                    self._state = round(celsius_to_fahrenheit(temperature), 1)
        elif self.type == SENSOR_HUMIDITY and SENSOR_HUMIDITY in data:
            humidity = data[SENSOR_HUMIDITY]
            _LOGGER.debug("Humidity %.1f%% + offset %.1f", humidity,
                          humidity_offset)
            if 0 <= humidity <= 100:
                self._state = round(humidity + humidity_offset, 1)