Remove temperature conversion - mhz19 (#55164)

This commit is contained in:
Marc Mueller 2021-08-25 10:29:59 +02:00 committed by GitHub
parent ed95bda781
commit 4036ba82fe
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 20 deletions

View file

@ -13,11 +13,10 @@ from homeassistant.const import (
CONF_NAME, CONF_NAME,
DEVICE_CLASS_CO2, DEVICE_CLASS_CO2,
DEVICE_CLASS_TEMPERATURE, DEVICE_CLASS_TEMPERATURE,
TEMP_FAHRENHEIT, TEMP_CELSIUS,
) )
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.util import Throttle from homeassistant.util import Throttle
from homeassistant.util.temperature import celsius_to_fahrenheit
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -31,7 +30,7 @@ ATTR_CO2_CONCENTRATION = "co2_concentration"
SENSOR_TEMPERATURE = "temperature" SENSOR_TEMPERATURE = "temperature"
SENSOR_CO2 = "co2" SENSOR_CO2 = "co2"
SENSOR_TYPES = { SENSOR_TYPES = {
SENSOR_TEMPERATURE: ["Temperature", None, DEVICE_CLASS_TEMPERATURE], SENSOR_TEMPERATURE: ["Temperature", TEMP_CELSIUS, DEVICE_CLASS_TEMPERATURE],
SENSOR_CO2: ["CO2", CONCENTRATION_PARTS_PER_MILLION, DEVICE_CLASS_CO2], SENSOR_CO2: ["CO2", CONCENTRATION_PARTS_PER_MILLION, DEVICE_CLASS_CO2],
} }
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
@ -57,14 +56,13 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
err, err,
) )
return False return False
SENSOR_TYPES[SENSOR_TEMPERATURE][1] = hass.config.units.temperature_unit
data = MHZClient(co2sensor, config.get(CONF_SERIAL_DEVICE)) data = MHZClient(co2sensor, config.get(CONF_SERIAL_DEVICE))
dev = [] dev = []
name = config.get(CONF_NAME) name = config.get(CONF_NAME)
for variable in config[CONF_MONITORED_CONDITIONS]: for variable in config[CONF_MONITORED_CONDITIONS]:
dev.append(MHZ19Sensor(data, variable, SENSOR_TYPES[variable][1], name)) dev.append(MHZ19Sensor(data, variable, name))
add_entities(dev, True) add_entities(dev, True)
return True return True
@ -73,11 +71,10 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
class MHZ19Sensor(SensorEntity): class MHZ19Sensor(SensorEntity):
"""Representation of an CO2 sensor.""" """Representation of an CO2 sensor."""
def __init__(self, mhz_client, sensor_type, temp_unit, name): def __init__(self, mhz_client, sensor_type, name):
"""Initialize a new PM sensor.""" """Initialize a new PM sensor."""
self._mhz_client = mhz_client self._mhz_client = mhz_client
self._sensor_type = sensor_type self._sensor_type = sensor_type
self._temp_unit = temp_unit
self._name = name self._name = name
self._unit_of_measurement = SENSOR_TYPES[sensor_type][1] self._unit_of_measurement = SENSOR_TYPES[sensor_type][1]
self._ppm = None self._ppm = None
@ -104,8 +101,6 @@ class MHZ19Sensor(SensorEntity):
self._mhz_client.update() self._mhz_client.update()
data = self._mhz_client.data data = self._mhz_client.data
self._temperature = data.get(SENSOR_TEMPERATURE) 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) self._ppm = data.get(SENSOR_CO2)
@property @property

View file

@ -86,13 +86,13 @@ async def aiohttp_client_update_good_read(mock_function):
async def test_co2_sensor(mock_function, hass): async def test_co2_sensor(mock_function, hass):
"""Test CO2 sensor.""" """Test CO2 sensor."""
client = mhz19.MHZClient(co2sensor, "test.serial") client = mhz19.MHZClient(co2sensor, "test.serial")
sensor = mhz19.MHZ19Sensor(client, mhz19.SENSOR_CO2, None, "name") sensor = mhz19.MHZ19Sensor(client, mhz19.SENSOR_CO2, "name")
sensor.hass = hass sensor.hass = hass
sensor.update() sensor.update()
assert sensor.name == "name: CO2" assert sensor.name == "name: CO2"
assert sensor.state == 1000 assert sensor.state == 1000
assert sensor.unit_of_measurement == CONCENTRATION_PARTS_PER_MILLION assert sensor.native_unit_of_measurement == CONCENTRATION_PARTS_PER_MILLION
assert sensor.should_poll assert sensor.should_poll
assert sensor.extra_state_attributes == {"temperature": 24} assert sensor.extra_state_attributes == {"temperature": 24}
@ -101,13 +101,13 @@ async def test_co2_sensor(mock_function, hass):
async def test_temperature_sensor(mock_function, hass): async def test_temperature_sensor(mock_function, hass):
"""Test temperature sensor.""" """Test temperature sensor."""
client = mhz19.MHZClient(co2sensor, "test.serial") client = mhz19.MHZClient(co2sensor, "test.serial")
sensor = mhz19.MHZ19Sensor(client, mhz19.SENSOR_TEMPERATURE, None, "name") sensor = mhz19.MHZ19Sensor(client, mhz19.SENSOR_TEMPERATURE, "name")
sensor.hass = hass sensor.hass = hass
sensor.update() sensor.update()
assert sensor.name == "name: Temperature" assert sensor.name == "name: Temperature"
assert sensor.state == 24 assert sensor.state == 24
assert sensor.unit_of_measurement == TEMP_CELSIUS assert sensor.native_unit_of_measurement == TEMP_CELSIUS
assert sensor.should_poll assert sensor.should_poll
assert sensor.extra_state_attributes == {"co2_concentration": 1000} assert sensor.extra_state_attributes == {"co2_concentration": 1000}
@ -115,11 +115,10 @@ async def test_temperature_sensor(mock_function, hass):
@patch("pmsensor.co2sensor.read_mh_z19_with_temperature", return_value=(1000, 24)) @patch("pmsensor.co2sensor.read_mh_z19_with_temperature", return_value=(1000, 24))
async def test_temperature_sensor_f(mock_function, hass): async def test_temperature_sensor_f(mock_function, hass):
"""Test temperature sensor.""" """Test temperature sensor."""
client = mhz19.MHZClient(co2sensor, "test.serial") with patch.object(hass.config.units, "temperature_unit", TEMP_FAHRENHEIT):
sensor = mhz19.MHZ19Sensor( client = mhz19.MHZClient(co2sensor, "test.serial")
client, mhz19.SENSOR_TEMPERATURE, TEMP_FAHRENHEIT, "name" sensor = mhz19.MHZ19Sensor(client, mhz19.SENSOR_TEMPERATURE, "name")
) sensor.hass = hass
sensor.hass = hass sensor.update()
sensor.update()
assert sensor.state == 75.2 assert sensor.state == "75"