Add I2c HTU21D temperature and humidity sensor for Raspberry Pi (#8049)
* Add new HTU21D temperature and humidity sensor * new sensor platform * requirements_all and .coveragerc update * fix lint * review changes: move sensor code to external module * remove debug log msg * add i2csense to COMMENT_REQUIREMENTS, require i2csense 0.0.3 * Add new HTU21D temperature and humidity sensor * new sensor platform * requirements_all and .coveragerc update * fix lint * review changes: move sensor code to external module * remove debug log msg * add i2csense to COMMENT_REQUIREMENTS, require i2csense 0.0.3 * change style for hass * fix requirements
This commit is contained in:
parent
8d0553d9e6
commit
435e5c8a91
3 changed files with 125 additions and 0 deletions
|
@ -419,6 +419,7 @@ omit =
|
|||
homeassistant/components/sensor/haveibeenpwned.py
|
||||
homeassistant/components/sensor/hddtemp.py
|
||||
homeassistant/components/sensor/hp_ilo.py
|
||||
homeassistant/components/sensor/htu21d.py
|
||||
homeassistant/components/sensor/hydroquebec.py
|
||||
homeassistant/components/sensor/imap.py
|
||||
homeassistant/components/sensor/imap_email_content.py
|
||||
|
|
122
homeassistant/components/sensor/htu21d.py
Normal file
122
homeassistant/components/sensor/htu21d.py
Normal file
|
@ -0,0 +1,122 @@
|
|||
"""
|
||||
Support for HTU21D temperature and humidity sensor.
|
||||
|
||||
For more details about this platform, please refer to the documentation at
|
||||
https://home-assistant.io/components/sensor.htu21d/
|
||||
"""
|
||||
import asyncio
|
||||
from datetime import timedelta
|
||||
from functools import partial
|
||||
import logging
|
||||
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.const import CONF_NAME, TEMP_FAHRENHEIT
|
||||
from homeassistant.helpers.entity import Entity
|
||||
from homeassistant.util import Throttle
|
||||
from homeassistant.util.temperature import celsius_to_fahrenheit
|
||||
|
||||
REQUIREMENTS = ['i2csense==0.0.3',
|
||||
'smbus-cffi==0.5.1']
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
CONF_I2C_BUS = 'i2c_bus'
|
||||
DEFAULT_I2C_BUS = 1
|
||||
|
||||
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=5)
|
||||
|
||||
DEFAULT_NAME = 'HTU21D Sensor'
|
||||
|
||||
SENSOR_TEMPERATURE = 'temperature'
|
||||
SENSOR_HUMIDITY = 'humidity'
|
||||
|
||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
||||
vol.Optional(CONF_I2C_BUS, default=DEFAULT_I2C_BUS): vol.Coerce(int),
|
||||
})
|
||||
|
||||
|
||||
# pylint: disable=import-error
|
||||
@asyncio.coroutine
|
||||
def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
|
||||
"""Set up the HTU21D sensor."""
|
||||
import smbus
|
||||
from i2csense.htu21d import HTU21D
|
||||
|
||||
name = config.get(CONF_NAME)
|
||||
bus_number = config.get(CONF_I2C_BUS)
|
||||
temp_unit = hass.config.units.temperature_unit
|
||||
|
||||
bus = smbus.SMBus(config.get(CONF_I2C_BUS))
|
||||
sensor = yield from hass.async_add_job(
|
||||
partial(HTU21D, bus, logger=_LOGGER)
|
||||
)
|
||||
if not sensor.sample_ok:
|
||||
_LOGGER.error("HTU21D sensor not detected in bus %s", bus_number)
|
||||
return False
|
||||
|
||||
sensor_handler = yield from hass.async_add_job(HTU21DHandler, sensor)
|
||||
|
||||
dev = [HTU21DSensor(sensor_handler, name, SENSOR_TEMPERATURE, temp_unit),
|
||||
HTU21DSensor(sensor_handler, name, SENSOR_HUMIDITY, '%')]
|
||||
|
||||
async_add_devices(dev)
|
||||
|
||||
|
||||
class HTU21DHandler:
|
||||
"""Implement HTU21D communication."""
|
||||
|
||||
def __init__(self, sensor):
|
||||
"""Initialize the sensor handler."""
|
||||
self.sensor = sensor
|
||||
self.sensor.update()
|
||||
|
||||
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
||||
def update(self):
|
||||
"""Read raw data and calculate temperature and humidity."""
|
||||
self.sensor.update()
|
||||
|
||||
|
||||
class HTU21DSensor(Entity):
|
||||
"""Implementation of the HTU21D sensor."""
|
||||
|
||||
def __init__(self, htu21d_client, name, variable, unit):
|
||||
"""Initialize the sensor."""
|
||||
self._name = '{}_{}'.format(name, variable)
|
||||
self._variable = variable
|
||||
self._unit_of_measurement = unit
|
||||
self._client = htu21d_client
|
||||
self._state = None
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
"""Return the name of the sensor."""
|
||||
return self._name
|
||||
|
||||
@property
|
||||
def state(self) -> int:
|
||||
"""Return the state of the sensor."""
|
||||
return self._state
|
||||
|
||||
@property
|
||||
def unit_of_measurement(self) -> str:
|
||||
"""Return the unit of measurement of the sensor."""
|
||||
return self._unit_of_measurement
|
||||
|
||||
@asyncio.coroutine
|
||||
def async_update(self):
|
||||
"""Get the latest data from the HTU21D sensor and update the state."""
|
||||
yield from self.hass.async_add_job(self._client.update)
|
||||
if self._client.sensor.sample_ok:
|
||||
if self._variable == SENSOR_TEMPERATURE:
|
||||
value = round(self._client.sensor.temperature, 1)
|
||||
if self.unit_of_measurement == TEMP_FAHRENHEIT:
|
||||
value = celsius_to_fahrenheit(value)
|
||||
else:
|
||||
value = round(self._client.sensor.humidity, 1)
|
||||
self._state = value
|
||||
else:
|
||||
_LOGGER.warning("Bad sample")
|
|
@ -316,6 +316,7 @@ https://github.com/wmalgadey/PyTado/archive/0.1.10.zip#PyTado==0.1.10
|
|||
https://github.com/wokar/pylgnetcast/archive/v0.2.0.zip#pylgnetcast==0.2.0
|
||||
|
||||
# homeassistant.components.sensor.bme280
|
||||
# homeassistant.components.sensor.htu21d
|
||||
# i2csense==0.0.3
|
||||
|
||||
# homeassistant.components.influxdb
|
||||
|
@ -823,6 +824,7 @@ sleepyq==0.6
|
|||
|
||||
# homeassistant.components.sensor.bme280
|
||||
# homeassistant.components.sensor.envirophat
|
||||
# homeassistant.components.sensor.htu21d
|
||||
# smbus-cffi==0.5.1
|
||||
|
||||
# homeassistant.components.media_player.snapcast
|
||||
|
|
Loading…
Add table
Reference in a new issue