From 46dcfb3d704a1f2369d19978641e451e1f77448d Mon Sep 17 00:00:00 2001 From: Open Home Automation Date: Sun, 21 Aug 2016 01:35:10 +0200 Subject: [PATCH] Serial CO2 sensor support (#2885) * Added support for serial HM-Z19 CO2 sensor * Minor pylint bug fixes * Added new files to .coveragerc * Removed newline * Changes in requirements after change of pmsensor library * Change the implementation of default name * Check if serial interface is working before adding the sensor * Maximum sensor value is 5000ppm --- .coveragerc | 1 + homeassistant/components/sensor/hmz19.py | 85 ++++++++++++++++++++++++ requirements_all.txt | 1 + 3 files changed, 87 insertions(+) create mode 100644 homeassistant/components/sensor/hmz19.py diff --git a/.coveragerc b/.coveragerc index 06e35d9b0bc..5bff9790325 100644 --- a/.coveragerc +++ b/.coveragerc @@ -216,6 +216,7 @@ omit = homeassistant/components/sensor/google_travel_time.py homeassistant/components/sensor/gpsd.py homeassistant/components/sensor/gtfs.py + homeassistant/components/sensor/hmz19.py homeassistant/components/sensor/hp_ilo.py homeassistant/components/sensor/imap.py homeassistant/components/sensor/lastfm.py diff --git a/homeassistant/components/sensor/hmz19.py b/homeassistant/components/sensor/hmz19.py new file mode 100644 index 00000000000..9cf3e4e4dd2 --- /dev/null +++ b/homeassistant/components/sensor/hmz19.py @@ -0,0 +1,85 @@ +""" +Support for CO2 sensor connected to a serial port. + +For more details about this platform, please refer to the documentation at +https://home-assistant.io/components/sensor.hmz19/ +""" +import logging +import voluptuous as vol + +from homeassistant.const import CONF_NAME +from homeassistant.helpers.entity import Entity +import homeassistant.helpers.config_validation as cv +from homeassistant.components.sensor import PLATFORM_SCHEMA + +REQUIREMENTS = ['pmsensor==0.3'] + + +_LOGGER = logging.getLogger(__name__) + +CONF_SERIAL_DEVICE = "serial_device" +DEFAULT_NAME = 'CO2 Sensor' + +PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ + vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string, + vol.Required(CONF_SERIAL_DEVICE): cv.string, +}) + + +def setup_platform(hass, config, add_devices, discovery_info=None): + """Setup the available CO2 sensors.""" + from pmsensor import co2sensor + + try: + co2sensor.read_mh_z19(config.get(CONF_SERIAL_DEVICE)) + except OSError as err: + _LOGGER.error("Could not open serial connection to %s (%s)", + config.get(CONF_SERIAL_DEVICE), err) + return False + + dev = HMZ19Sensor(config.get(CONF_SERIAL_DEVICE), config.get(CONF_NAME)) + add_devices([dev]) + + +class HMZ19Sensor(Entity): + """Representation of an CO2 sensor.""" + + def __init__(self, serial_device, name): + """Initialize a new PM sensor.""" + self._name = name + self._state = None + self._serial = serial_device + + @property + def name(self): + """Return the name of the sensor.""" + return self._name + + @property + def state(self): + """Return the state of the sensor.""" + return self._state + + @property + def unit_of_measurement(self): + """Return the unit of measurement of this entity, if any.""" + return "ppm" + + def update(self): + """Read from sensor and update the state.""" + from pmsensor import co2sensor + + _LOGGER.debug("Reading data from CO2 sensor") + try: + ppm = co2sensor.read_mh_z19(self._serial) + # values from sensor can only between 0 and 5000 + if (ppm >= 0) & (ppm <= 5000): + self._state = ppm + except OSError as err: + _LOGGER.error("Could not open serial connection to %s (%s)", + self._serial, err) + return + + def should_poll(self): + """Sensor needs polling.""" + return True diff --git a/requirements_all.txt b/requirements_all.txt index 4ac074448a4..85322e1ccf4 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -254,6 +254,7 @@ pilight==0.0.2 # homeassistant.components.sensor.plex plexapi==2.0.2 +# homeassistant.components.sensor.hmz19 # homeassistant.components.sensor.serial_pm pmsensor==0.3