2019-04-03 15:40:03 +00:00
|
|
|
"""Support for HTU21D temperature and humidity sensor."""
|
2017-06-22 05:05:58 +00:00
|
|
|
from datetime import timedelta
|
|
|
|
from functools import partial
|
|
|
|
import logging
|
|
|
|
|
2019-10-19 03:57:36 +00:00
|
|
|
from i2csense.htu21d import HTU21D # pylint: disable=import-error
|
|
|
|
import smbus # pylint: disable=import-error
|
2017-06-22 05:05:58 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
|
|
|
from homeassistant.const import CONF_NAME, TEMP_FAHRENHEIT
|
2019-10-19 03:57:36 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2017-06-22 05:05:58 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
|
|
|
from homeassistant.util import Throttle
|
|
|
|
from homeassistant.util.temperature import celsius_to_fahrenheit
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_I2C_BUS = "i2c_bus"
|
2017-06-22 05:05:58 +00:00
|
|
|
DEFAULT_I2C_BUS = 1
|
|
|
|
|
|
|
|
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=5)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DEFAULT_NAME = "HTU21D Sensor"
|
2017-06-22 05:05:58 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
SENSOR_TEMPERATURE = "temperature"
|
|
|
|
SENSOR_HUMIDITY = "humidity"
|
2017-06-22 05:05:58 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
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),
|
|
|
|
}
|
|
|
|
)
|
2017-06-22 05:05:58 +00:00
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
2017-06-22 05:05:58 +00:00
|
|
|
"""Set up the HTU21D sensor."""
|
|
|
|
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))
|
2019-07-31 19:25:30 +00:00
|
|
|
sensor = await hass.async_add_job(partial(HTU21D, bus, logger=_LOGGER))
|
2017-06-22 05:05:58 +00:00
|
|
|
if not sensor.sample_ok:
|
|
|
|
_LOGGER.error("HTU21D sensor not detected in bus %s", bus_number)
|
|
|
|
return False
|
|
|
|
|
2018-10-01 06:55:43 +00:00
|
|
|
sensor_handler = await hass.async_add_job(HTU21DHandler, sensor)
|
2017-06-22 05:05:58 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
dev = [
|
|
|
|
HTU21DSensor(sensor_handler, name, SENSOR_TEMPERATURE, temp_unit),
|
|
|
|
HTU21DSensor(sensor_handler, name, SENSOR_HUMIDITY, "%"),
|
|
|
|
]
|
2017-06-22 05:05:58 +00:00
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
async_add_entities(dev)
|
2017-06-22 05:05:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
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."""
|
2019-09-03 15:27:14 +00:00
|
|
|
self._name = f"{name}_{variable}"
|
2017-06-22 05:05:58 +00:00
|
|
|
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
|
|
|
|
|
2018-10-01 06:55:43 +00:00
|
|
|
async def async_update(self):
|
2017-06-22 05:05:58 +00:00
|
|
|
"""Get the latest data from the HTU21D sensor and update the state."""
|
2018-10-01 06:55:43 +00:00
|
|
|
await self.hass.async_add_job(self._client.update)
|
2017-06-22 05:05:58 +00:00
|
|
|
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")
|