mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 03:07:37 +00:00
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
This commit is contained in:
parent
5f508b6afa
commit
46dcfb3d70
@ -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
|
||||
|
85
homeassistant/components/sensor/hmz19.py
Normal file
85
homeassistant/components/sensor/hmz19.py
Normal file
@ -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
|
@ -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
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user