mirror of
https://github.com/home-assistant/core.git
synced 2025-07-22 04:37:06 +00:00
Convert DHT to use circuitpython library (#41525)
* Converts DHT to use circuitpython library * Adds dht requirement to requirements_all * Cleanup for isort,black and requirements * Adds validation to prefix with D for PIN * Sends name over to dhtclient * Adds exceptions for broad-except * Adds explict except for raised exception * Removes unused var * Moves setting dht var out of try * Bump to 3.5.8 * Moves non-exception steps to else * Bump library version * Bumps version
This commit is contained in:
parent
17e9e4fa31
commit
d6c01cc0e6
@ -2,7 +2,7 @@
|
||||
"domain": "dht",
|
||||
"name": "DHT Sensor",
|
||||
"documentation": "https://www.home-assistant.io/integrations/dht",
|
||||
"requirements": ["Adafruit-DHT==1.4.0"],
|
||||
"requirements": ["adafruit-circuitpython-dht==3.6.0"],
|
||||
"codeowners": [],
|
||||
"iot_class": "local_polling"
|
||||
}
|
||||
|
@ -3,7 +3,8 @@ from contextlib import suppress
|
||||
from datetime import timedelta
|
||||
import logging
|
||||
|
||||
import Adafruit_DHT # pylint: disable=import-error
|
||||
import adafruit_dht
|
||||
import board
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
|
||||
@ -36,10 +37,20 @@ SENSOR_TYPES = {
|
||||
SENSOR_HUMIDITY: ["Humidity", PERCENTAGE],
|
||||
}
|
||||
|
||||
|
||||
def validate_pin_input(value):
|
||||
"""Validate that the GPIO PIN is prefixed with a D."""
|
||||
try:
|
||||
int(value)
|
||||
return f"D{value}"
|
||||
except ValueError:
|
||||
return value.upper()
|
||||
|
||||
|
||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
||||
{
|
||||
vol.Required(CONF_SENSOR): cv.string,
|
||||
vol.Required(CONF_PIN): cv.string,
|
||||
vol.Required(CONF_PIN): vol.All(cv.string, validate_pin_input),
|
||||
vol.Optional(CONF_MONITORED_CONDITIONS, default=[]): vol.All(
|
||||
cv.ensure_list, [vol.In(SENSOR_TYPES)]
|
||||
),
|
||||
@ -58,22 +69,22 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||
"""Set up the DHT sensor."""
|
||||
SENSOR_TYPES[SENSOR_TEMPERATURE][1] = hass.config.units.temperature_unit
|
||||
available_sensors = {
|
||||
"AM2302": Adafruit_DHT.AM2302,
|
||||
"DHT11": Adafruit_DHT.DHT11,
|
||||
"DHT22": Adafruit_DHT.DHT22,
|
||||
"AM2302": adafruit_dht.DHT22,
|
||||
"DHT11": adafruit_dht.DHT11,
|
||||
"DHT22": adafruit_dht.DHT22,
|
||||
}
|
||||
sensor = available_sensors.get(config[CONF_SENSOR])
|
||||
pin = config[CONF_PIN]
|
||||
temperature_offset = config[CONF_TEMPERATURE_OFFSET]
|
||||
humidity_offset = config[CONF_HUMIDITY_OFFSET]
|
||||
name = config[CONF_NAME]
|
||||
|
||||
if not sensor:
|
||||
_LOGGER.error("DHT sensor type is not supported")
|
||||
return False
|
||||
|
||||
data = DHTClient(Adafruit_DHT, sensor, pin)
|
||||
data = DHTClient(sensor, pin, name)
|
||||
dev = []
|
||||
name = config[CONF_NAME]
|
||||
|
||||
with suppress(KeyError):
|
||||
for variable in config[CONF_MONITORED_CONDITIONS]:
|
||||
@ -157,18 +168,28 @@ class DHTSensor(SensorEntity):
|
||||
class DHTClient:
|
||||
"""Get the latest data from the DHT sensor."""
|
||||
|
||||
def __init__(self, adafruit_dht, sensor, pin):
|
||||
def __init__(self, sensor, pin, name):
|
||||
"""Initialize the sensor."""
|
||||
self.adafruit_dht = adafruit_dht
|
||||
self.sensor = sensor
|
||||
self.pin = pin
|
||||
self.pin = getattr(board, pin)
|
||||
self.data = {}
|
||||
self.name = name
|
||||
|
||||
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
||||
def update(self):
|
||||
"""Get the latest data the DHT sensor."""
|
||||
humidity, temperature = self.adafruit_dht.read_retry(self.sensor, self.pin)
|
||||
if temperature:
|
||||
self.data[SENSOR_TEMPERATURE] = temperature
|
||||
if humidity:
|
||||
self.data[SENSOR_HUMIDITY] = humidity
|
||||
dht = self.sensor(self.pin)
|
||||
try:
|
||||
temperature = dht.temperature
|
||||
humidity = dht.humidity
|
||||
except RuntimeError:
|
||||
_LOGGER.debug("Unexpected value from DHT sensor: %s", self.name)
|
||||
except Exception: # pylint: disable=broad-except
|
||||
_LOGGER.exception("Error updating DHT sensor: %s", self.name)
|
||||
else:
|
||||
if temperature:
|
||||
self.data[SENSOR_TEMPERATURE] = temperature
|
||||
if humidity:
|
||||
self.data[SENSOR_HUMIDITY] = humidity
|
||||
finally:
|
||||
dht.exit()
|
||||
|
@ -4,9 +4,6 @@
|
||||
# homeassistant.components.aemet
|
||||
AEMET-OpenData==0.1.8
|
||||
|
||||
# homeassistant.components.dht
|
||||
# Adafruit-DHT==1.4.0
|
||||
|
||||
# homeassistant.components.sht31
|
||||
Adafruit-GPIO==1.0.3
|
||||
|
||||
@ -101,6 +98,9 @@ accuweather==0.1.1
|
||||
# homeassistant.components.bmp280
|
||||
adafruit-circuitpython-bmp280==3.1.1
|
||||
|
||||
# homeassistant.components.dht
|
||||
adafruit-circuitpython-dht==3.6.0
|
||||
|
||||
# homeassistant.components.mcp23017
|
||||
adafruit-circuitpython-mcp230xx==2.2.2
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user