From 4036ba82fe4c018d66366f92a6837db1e2d48d60 Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Wed, 25 Aug 2021 10:29:59 +0200 Subject: [PATCH] Remove temperature conversion - mhz19 (#55164) --- homeassistant/components/mhz19/sensor.py | 13 ++++--------- tests/components/mhz19/test_sensor.py | 21 ++++++++++----------- 2 files changed, 14 insertions(+), 20 deletions(-) diff --git a/homeassistant/components/mhz19/sensor.py b/homeassistant/components/mhz19/sensor.py index 7d5d5eba183..ea90186e75a 100644 --- a/homeassistant/components/mhz19/sensor.py +++ b/homeassistant/components/mhz19/sensor.py @@ -13,11 +13,10 @@ from homeassistant.const import ( CONF_NAME, DEVICE_CLASS_CO2, DEVICE_CLASS_TEMPERATURE, - TEMP_FAHRENHEIT, + TEMP_CELSIUS, ) import homeassistant.helpers.config_validation as cv from homeassistant.util import Throttle -from homeassistant.util.temperature import celsius_to_fahrenheit _LOGGER = logging.getLogger(__name__) @@ -31,7 +30,7 @@ ATTR_CO2_CONCENTRATION = "co2_concentration" SENSOR_TEMPERATURE = "temperature" SENSOR_CO2 = "co2" 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], } PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( @@ -57,14 +56,13 @@ def setup_platform(hass, config, add_entities, discovery_info=None): err, ) return False - SENSOR_TYPES[SENSOR_TEMPERATURE][1] = hass.config.units.temperature_unit data = MHZClient(co2sensor, config.get(CONF_SERIAL_DEVICE)) dev = [] name = config.get(CONF_NAME) 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) return True @@ -73,11 +71,10 @@ def setup_platform(hass, config, add_entities, discovery_info=None): class MHZ19Sensor(SensorEntity): """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.""" self._mhz_client = mhz_client self._sensor_type = sensor_type - self._temp_unit = temp_unit self._name = name self._unit_of_measurement = SENSOR_TYPES[sensor_type][1] self._ppm = None @@ -104,8 +101,6 @@ class MHZ19Sensor(SensorEntity): 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) @property diff --git a/tests/components/mhz19/test_sensor.py b/tests/components/mhz19/test_sensor.py index 26e9441f9fc..5af7338abc4 100644 --- a/tests/components/mhz19/test_sensor.py +++ b/tests/components/mhz19/test_sensor.py @@ -86,13 +86,13 @@ async def aiohttp_client_update_good_read(mock_function): async def test_co2_sensor(mock_function, hass): """Test CO2 sensor.""" 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.update() assert sensor.name == "name: CO2" 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.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): """Test temperature sensor.""" 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.update() assert sensor.name == "name: Temperature" 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.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)) async def test_temperature_sensor_f(mock_function, hass): """Test temperature sensor.""" - client = mhz19.MHZClient(co2sensor, "test.serial") - sensor = mhz19.MHZ19Sensor( - client, mhz19.SENSOR_TEMPERATURE, TEMP_FAHRENHEIT, "name" - ) - sensor.hass = hass - sensor.update() + with patch.object(hass.config.units, "temperature_unit", TEMP_FAHRENHEIT): + client = mhz19.MHZClient(co2sensor, "test.serial") + sensor = mhz19.MHZ19Sensor(client, mhz19.SENSOR_TEMPERATURE, "name") + sensor.hass = hass + sensor.update() - assert sensor.state == 75.2 + assert sensor.state == "75"