mirror of
https://github.com/home-assistant/core.git
synced 2025-07-22 20:57:21 +00:00
Remove temperature conversion - bme280 (#55162)
This commit is contained in:
parent
81a6bec818
commit
cb556fe98e
@ -6,6 +6,7 @@ from homeassistant.const import (
|
|||||||
DEVICE_CLASS_PRESSURE,
|
DEVICE_CLASS_PRESSURE,
|
||||||
DEVICE_CLASS_TEMPERATURE,
|
DEVICE_CLASS_TEMPERATURE,
|
||||||
PERCENTAGE,
|
PERCENTAGE,
|
||||||
|
TEMP_CELSIUS,
|
||||||
)
|
)
|
||||||
|
|
||||||
# Common
|
# Common
|
||||||
@ -26,7 +27,7 @@ SENSOR_TEMP = "temperature"
|
|||||||
SENSOR_HUMID = "humidity"
|
SENSOR_HUMID = "humidity"
|
||||||
SENSOR_PRESS = "pressure"
|
SENSOR_PRESS = "pressure"
|
||||||
SENSOR_TYPES = {
|
SENSOR_TYPES = {
|
||||||
SENSOR_TEMP: ["Temperature", None, DEVICE_CLASS_TEMPERATURE],
|
SENSOR_TEMP: ["Temperature", TEMP_CELSIUS, DEVICE_CLASS_TEMPERATURE],
|
||||||
SENSOR_HUMID: ["Humidity", PERCENTAGE, DEVICE_CLASS_HUMIDITY],
|
SENSOR_HUMID: ["Humidity", PERCENTAGE, DEVICE_CLASS_HUMIDITY],
|
||||||
SENSOR_PRESS: ["Pressure", "mb", DEVICE_CLASS_PRESSURE],
|
SENSOR_PRESS: ["Pressure", "mb", DEVICE_CLASS_PRESSURE],
|
||||||
}
|
}
|
||||||
|
@ -7,18 +7,12 @@ from i2csense.bme280 import BME280 as BME280_i2c # pylint: disable=import-error
|
|||||||
import smbus
|
import smbus
|
||||||
|
|
||||||
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN, SensorEntity
|
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN, SensorEntity
|
||||||
from homeassistant.const import (
|
from homeassistant.const import CONF_MONITORED_CONDITIONS, CONF_NAME, CONF_SCAN_INTERVAL
|
||||||
CONF_MONITORED_CONDITIONS,
|
|
||||||
CONF_NAME,
|
|
||||||
CONF_SCAN_INTERVAL,
|
|
||||||
TEMP_FAHRENHEIT,
|
|
||||||
)
|
|
||||||
from homeassistant.helpers.update_coordinator import (
|
from homeassistant.helpers.update_coordinator import (
|
||||||
CoordinatorEntity,
|
CoordinatorEntity,
|
||||||
DataUpdateCoordinator,
|
DataUpdateCoordinator,
|
||||||
UpdateFailed,
|
UpdateFailed,
|
||||||
)
|
)
|
||||||
from homeassistant.util.temperature import celsius_to_fahrenheit
|
|
||||||
|
|
||||||
from .const import (
|
from .const import (
|
||||||
CONF_DELTA_TEMP,
|
CONF_DELTA_TEMP,
|
||||||
@ -47,7 +41,6 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
|
|||||||
"""Set up the BME280 sensor."""
|
"""Set up the BME280 sensor."""
|
||||||
if discovery_info is None:
|
if discovery_info is None:
|
||||||
return
|
return
|
||||||
SENSOR_TYPES[SENSOR_TEMP][1] = hass.config.units.temperature_unit
|
|
||||||
sensor_conf = discovery_info[SENSOR_DOMAIN]
|
sensor_conf = discovery_info[SENSOR_DOMAIN]
|
||||||
name = sensor_conf[CONF_NAME]
|
name = sensor_conf[CONF_NAME]
|
||||||
scan_interval = max(sensor_conf[CONF_SCAN_INTERVAL], MIN_TIME_BETWEEN_UPDATES)
|
scan_interval = max(sensor_conf[CONF_SCAN_INTERVAL], MIN_TIME_BETWEEN_UPDATES)
|
||||||
@ -110,7 +103,6 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
|
|||||||
entities.append(
|
entities.append(
|
||||||
BME280Sensor(
|
BME280Sensor(
|
||||||
condition,
|
condition,
|
||||||
SENSOR_TYPES[condition][1],
|
|
||||||
name,
|
name,
|
||||||
coordinator,
|
coordinator,
|
||||||
)
|
)
|
||||||
@ -121,11 +113,10 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
|
|||||||
class BME280Sensor(CoordinatorEntity, SensorEntity):
|
class BME280Sensor(CoordinatorEntity, SensorEntity):
|
||||||
"""Implementation of the BME280 sensor."""
|
"""Implementation of the BME280 sensor."""
|
||||||
|
|
||||||
def __init__(self, sensor_type, temp_unit, name, coordinator):
|
def __init__(self, sensor_type, name, coordinator):
|
||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
super().__init__(coordinator)
|
super().__init__(coordinator)
|
||||||
self._attr_name = f"{name} {SENSOR_TYPES[sensor_type][0]}"
|
self._attr_name = f"{name} {SENSOR_TYPES[sensor_type][0]}"
|
||||||
self.temp_unit = temp_unit
|
|
||||||
self.type = sensor_type
|
self.type = sensor_type
|
||||||
self._attr_native_unit_of_measurement = SENSOR_TYPES[sensor_type][1]
|
self._attr_native_unit_of_measurement = SENSOR_TYPES[sensor_type][1]
|
||||||
self._attr_device_class = SENSOR_TYPES[sensor_type][2]
|
self._attr_device_class = SENSOR_TYPES[sensor_type][2]
|
||||||
@ -135,8 +126,6 @@ class BME280Sensor(CoordinatorEntity, SensorEntity):
|
|||||||
"""Return the state of the sensor."""
|
"""Return the state of the sensor."""
|
||||||
if self.type == SENSOR_TEMP:
|
if self.type == SENSOR_TEMP:
|
||||||
temperature = round(self.coordinator.data.temperature, 1)
|
temperature = round(self.coordinator.data.temperature, 1)
|
||||||
if self.temp_unit == TEMP_FAHRENHEIT:
|
|
||||||
temperature = round(celsius_to_fahrenheit(temperature), 1)
|
|
||||||
state = temperature
|
state = temperature
|
||||||
elif self.type == SENSOR_HUMID:
|
elif self.type == SENSOR_HUMID:
|
||||||
state = round(self.coordinator.data.humidity, 1)
|
state = round(self.coordinator.data.humidity, 1)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user