Skip to content

Commit

Permalink
Remove temperature conversion - mhz19 (home-assistant#55164)
Browse files Browse the repository at this point in the history
  • Loading branch information
cdce8p authored Aug 25, 2021
1 parent ed95bda commit 4036ba8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 20 deletions.
13 changes: 4 additions & 9 deletions homeassistant/components/mhz19/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@
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__)

Expand All @@ -31,7 +30,7 @@
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(
Expand All @@ -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
Expand All @@ -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
Expand All @@ -104,8 +101,6 @@ def update(self):
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
Expand Down
21 changes: 10 additions & 11 deletions tests/components/mhz19/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand All @@ -101,25 +101,24 @@ 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}


@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"

0 comments on commit 4036ba8

Please sign in to comment.