2019-04-03 15:40:03 +00:00
|
|
|
"""Support for CO2 sensor connected to a serial port."""
|
2017-02-27 19:19:11 +00:00
|
|
|
from datetime import timedelta
|
2019-12-05 05:14:03 +00:00
|
|
|
import logging
|
2016-10-30 08:58:34 +00:00
|
|
|
|
2019-12-05 05:14:03 +00:00
|
|
|
from pmsensor import co2sensor
|
2016-08-20 23:35:10 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2019-12-05 05:14:03 +00:00
|
|
|
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
2017-02-27 19:19:11 +00:00
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_TEMPERATURE,
|
2020-02-25 01:52:14 +00:00
|
|
|
CONCENTRATION_PARTS_PER_MILLION,
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_MONITORED_CONDITIONS,
|
2019-12-05 05:14:03 +00:00
|
|
|
CONF_NAME,
|
2019-07-31 19:25:30 +00:00
|
|
|
TEMP_FAHRENHEIT,
|
|
|
|
)
|
2016-08-20 23:35:10 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2019-12-05 05:14:03 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
2017-02-27 19:19:11 +00:00
|
|
|
from homeassistant.util import Throttle
|
2019-12-05 05:14:03 +00:00
|
|
|
from homeassistant.util.temperature import celsius_to_fahrenheit
|
2016-08-20 23:35:10 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_SERIAL_DEVICE = "serial_device"
|
2017-02-27 19:19:11 +00:00
|
|
|
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=10)
|
2016-10-30 08:58:34 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DEFAULT_NAME = "CO2 Sensor"
|
2016-08-20 23:35:10 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_CO2_CONCENTRATION = "co2_concentration"
|
2017-02-27 19:19:11 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
SENSOR_TEMPERATURE = "temperature"
|
|
|
|
SENSOR_CO2 = "co2"
|
2020-02-25 01:52:14 +00:00
|
|
|
SENSOR_TYPES = {
|
|
|
|
SENSOR_TEMPERATURE: ["Temperature", None],
|
|
|
|
SENSOR_CO2: ["CO2", CONCENTRATION_PARTS_PER_MILLION],
|
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
|
|
|
vol.Required(CONF_SERIAL_DEVICE): cv.string,
|
|
|
|
vol.Optional(CONF_MONITORED_CONDITIONS, default=[SENSOR_CO2]): vol.All(
|
|
|
|
cv.ensure_list, [vol.In(SENSOR_TYPES)]
|
|
|
|
),
|
|
|
|
}
|
|
|
|
)
|
2016-08-20 23:35:10 +00:00
|
|
|
|
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Set up the available CO2 sensors."""
|
2016-08-20 23:35:10 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
co2sensor.read_mh_z19(config.get(CONF_SERIAL_DEVICE))
|
|
|
|
except OSError as err:
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.error(
|
|
|
|
"Could not open serial connection to %s (%s)",
|
|
|
|
config.get(CONF_SERIAL_DEVICE),
|
|
|
|
err,
|
|
|
|
)
|
2016-08-20 23:35:10 +00:00
|
|
|
return False
|
2017-02-27 19:19:11 +00:00
|
|
|
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]:
|
2019-07-31 19:25:30 +00:00
|
|
|
dev.append(MHZ19Sensor(data, variable, SENSOR_TYPES[variable][1], name))
|
2016-08-20 23:35:10 +00:00
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities(dev, True)
|
2017-02-27 19:19:11 +00:00
|
|
|
return True
|
2016-08-20 23:35:10 +00:00
|
|
|
|
|
|
|
|
2016-08-24 16:54:34 +00:00
|
|
|
class MHZ19Sensor(Entity):
|
2016-08-20 23:35:10 +00:00
|
|
|
"""Representation of an CO2 sensor."""
|
|
|
|
|
2017-02-27 19:19:11 +00:00
|
|
|
def __init__(self, mhz_client, sensor_type, temp_unit, name):
|
2016-08-20 23:35:10 +00:00
|
|
|
"""Initialize a new PM sensor."""
|
2017-02-27 19:19:11 +00:00
|
|
|
self._mhz_client = mhz_client
|
|
|
|
self._sensor_type = sensor_type
|
|
|
|
self._temp_unit = temp_unit
|
2016-08-20 23:35:10 +00:00
|
|
|
self._name = name
|
2017-02-27 19:19:11 +00:00
|
|
|
self._unit_of_measurement = SENSOR_TYPES[sensor_type][1]
|
|
|
|
self._ppm = None
|
|
|
|
self._temperature = None
|
2016-08-20 23:35:10 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the sensor."""
|
2020-03-10 22:34:54 +00:00
|
|
|
return f"{self._name}: {SENSOR_TYPES[self._sensor_type][0]}"
|
2016-08-20 23:35:10 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the state of the sensor."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return self._ppm if self._sensor_type == SENSOR_CO2 else self._temperature
|
2016-08-20 23:35:10 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
|
|
|
"""Return the unit of measurement of this entity, if any."""
|
2017-02-27 19:19:11 +00:00
|
|
|
return self._unit_of_measurement
|
2016-08-20 23:35:10 +00:00
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Read from sensor and update the state."""
|
2017-02-27 19:19:11 +00:00
|
|
|
self._mhz_client.update()
|
|
|
|
data = self._mhz_client.data
|
|
|
|
self._temperature = data.get(SENSOR_TEMPERATURE)
|
2019-07-31 19:25:30 +00:00
|
|
|
if self._temperature is not None and self._temp_unit == TEMP_FAHRENHEIT:
|
|
|
|
self._temperature = round(celsius_to_fahrenheit(self._temperature), 1)
|
2017-02-27 19:19:11 +00:00
|
|
|
self._ppm = data.get(SENSOR_CO2)
|
2016-08-20 23:35:10 +00:00
|
|
|
|
2017-02-27 19:19:11 +00:00
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return the state attributes."""
|
|
|
|
result = {}
|
|
|
|
if self._sensor_type == SENSOR_TEMPERATURE and self._ppm is not None:
|
|
|
|
result[ATTR_CO2_CONCENTRATION] = self._ppm
|
|
|
|
if self._sensor_type == SENSOR_CO2 and self._temperature is not None:
|
|
|
|
result[ATTR_TEMPERATURE] = self._temperature
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
2018-07-20 08:45:20 +00:00
|
|
|
class MHZClient:
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Get the latest data from the MH-Z sensor."""
|
2017-02-27 19:19:11 +00:00
|
|
|
|
2019-12-05 06:47:40 +00:00
|
|
|
def __init__(self, co2sens, serial):
|
2017-02-27 19:19:11 +00:00
|
|
|
"""Initialize the sensor."""
|
2019-12-05 06:47:40 +00:00
|
|
|
self.co2sensor = co2sens
|
2017-02-27 19:19:11 +00:00
|
|
|
self._serial = serial
|
2020-04-04 20:31:56 +00:00
|
|
|
self.data = {}
|
2017-02-27 19:19:11 +00:00
|
|
|
|
|
|
|
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
|
|
|
def update(self):
|
|
|
|
"""Get the latest data the MH-Z19 sensor."""
|
|
|
|
self.data = {}
|
2016-08-20 23:35:10 +00:00
|
|
|
try:
|
2017-02-27 19:19:11 +00:00
|
|
|
result = self.co2sensor.read_mh_z19_with_temperature(self._serial)
|
|
|
|
if result is None:
|
|
|
|
return
|
|
|
|
co2, temperature = result
|
|
|
|
|
2016-08-20 23:35:10 +00:00
|
|
|
except OSError as err:
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.error(
|
|
|
|
"Could not open serial connection to %s (%s)", self._serial, err
|
|
|
|
)
|
2016-08-20 23:35:10 +00:00
|
|
|
return
|
|
|
|
|
2017-02-27 19:19:11 +00:00
|
|
|
if temperature is not None:
|
|
|
|
self.data[SENSOR_TEMPERATURE] = temperature
|
|
|
|
if co2 is not None and 0 < co2 <= 5000:
|
|
|
|
self.data[SENSOR_CO2] = co2
|