Remove temperature conversion - temper (#55188)

This commit is contained in:
Marc Mueller 2021-08-25 11:19:28 +02:00 committed by GitHub
parent 49041b1469
commit bf1112bc10
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -10,7 +10,7 @@ from homeassistant.const import (
CONF_OFFSET, CONF_OFFSET,
DEVICE_CLASS_TEMPERATURE, DEVICE_CLASS_TEMPERATURE,
DEVICE_DEFAULT_NAME, DEVICE_DEFAULT_NAME,
TEMP_FAHRENHEIT, TEMP_CELSIUS,
) )
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -35,7 +35,6 @@ def get_temper_devices():
def setup_platform(hass, config, add_entities, discovery_info=None): def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the Temper sensors.""" """Set up the Temper sensors."""
temp_unit = hass.config.units.temperature_unit
name = config.get(CONF_NAME) name = config.get(CONF_NAME)
scaling = {"scale": config.get(CONF_SCALE), "offset": config.get(CONF_OFFSET)} scaling = {"scale": config.get(CONF_SCALE), "offset": config.get(CONF_OFFSET)}
temper_devices = get_temper_devices() temper_devices = get_temper_devices()
@ -43,7 +42,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
for idx, dev in enumerate(temper_devices): for idx, dev in enumerate(temper_devices):
if idx != 0: if idx != 0:
name = f"{name}_{idx!s}" name = f"{name}_{idx!s}"
TEMPER_SENSORS.append(TemperSensor(dev, temp_unit, name, scaling)) TEMPER_SENSORS.append(TemperSensor(dev, name, scaling))
add_entities(TEMPER_SENSORS) add_entities(TEMPER_SENSORS)
@ -61,30 +60,16 @@ def reset_devices():
class TemperSensor(SensorEntity): class TemperSensor(SensorEntity):
"""Representation of a Temper temperature sensor.""" """Representation of a Temper temperature sensor."""
def __init__(self, temper_device, temp_unit, name, scaling): _attr_device_class = DEVICE_CLASS_TEMPERATURE
_attr_native_unit_of_measurement = TEMP_CELSIUS
def __init__(self, temper_device, name, scaling):
"""Initialize the sensor.""" """Initialize the sensor."""
self.temp_unit = temp_unit
self.scale = scaling["scale"] self.scale = scaling["scale"]
self.offset = scaling["offset"] self.offset = scaling["offset"]
self.current_value = None
self._name = name
self.set_temper_device(temper_device) self.set_temper_device(temper_device)
self._attr_device_class = DEVICE_CLASS_TEMPERATURE
@property self._attr_name = name
def name(self):
"""Return the name of the temperature sensor."""
return self._name
@property
def native_value(self):
"""Return the state of the entity."""
return self.current_value
@property
def native_unit_of_measurement(self):
"""Return the unit of measurement of this entity, if any."""
return self.temp_unit
def set_temper_device(self, temper_device): def set_temper_device(self, temper_device):
"""Assign the underlying device for this sensor.""" """Assign the underlying device for this sensor."""
@ -96,11 +81,8 @@ class TemperSensor(SensorEntity):
def update(self): def update(self):
"""Retrieve latest state.""" """Retrieve latest state."""
try: try:
format_str = ( sensor_value = self.temper_device.get_temperature("celsius")
"fahrenheit" if self.temp_unit == TEMP_FAHRENHEIT else "celsius" self._attr_native_value = round(sensor_value, 1)
)
sensor_value = self.temper_device.get_temperature(format_str)
self.current_value = round(sensor_value, 1)
except OSError: except OSError:
_LOGGER.error( _LOGGER.error(
"Failed to get temperature. The device address may" "Failed to get temperature. The device address may"