parent
2eadae2039
commit
d611010a6e
1 changed files with 15 additions and 12 deletions
|
@ -18,9 +18,13 @@ REQUIREMENTS = ['http://github.com/adafruit/Adafruit_Python_DHT/archive/'
|
||||||
'#Adafruit_DHT==1.3.0']
|
'#Adafruit_DHT==1.3.0']
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
CONF_PIN = 'pin'
|
||||||
|
CONF_SENSOR = 'sensor'
|
||||||
|
SENSOR_TEMPERATURE = 'temperature'
|
||||||
|
SENSOR_HUMIDITY = 'humidity'
|
||||||
SENSOR_TYPES = {
|
SENSOR_TYPES = {
|
||||||
'temperature': ['Temperature', None],
|
SENSOR_TEMPERATURE: ['Temperature', None],
|
||||||
'humidity': ['Humidity', '%']
|
SENSOR_HUMIDITY: ['Humidity', '%']
|
||||||
}
|
}
|
||||||
DEFAULT_NAME = "DHT Sensor"
|
DEFAULT_NAME = "DHT Sensor"
|
||||||
# Return cached results if last scan was less then this time ago
|
# Return cached results if last scan was less then this time ago
|
||||||
|
@ -33,15 +37,14 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||||
# pylint: disable=import-error
|
# pylint: disable=import-error
|
||||||
import Adafruit_DHT
|
import Adafruit_DHT
|
||||||
|
|
||||||
SENSOR_TYPES['temperature'][1] = hass.config.units.temperature_unit
|
SENSOR_TYPES[SENSOR_TEMPERATURE][1] = hass.config.units.temperature_unit
|
||||||
available_sensors = {
|
available_sensors = {
|
||||||
"DHT11": Adafruit_DHT.DHT11,
|
"DHT11": Adafruit_DHT.DHT11,
|
||||||
"DHT22": Adafruit_DHT.DHT22,
|
"DHT22": Adafruit_DHT.DHT22,
|
||||||
"AM2302": Adafruit_DHT.AM2302
|
"AM2302": Adafruit_DHT.AM2302
|
||||||
}
|
}
|
||||||
sensor = available_sensors[config['sensor']]
|
sensor = available_sensors.get(config.get(CONF_SENSOR))
|
||||||
|
pin = config.get(CONF_PIN)
|
||||||
pin = config['pin']
|
|
||||||
|
|
||||||
if not sensor or not pin:
|
if not sensor or not pin:
|
||||||
_LOGGER.error(
|
_LOGGER.error(
|
||||||
|
@ -101,15 +104,15 @@ class DHTSensor(Entity):
|
||||||
self.dht_client.update()
|
self.dht_client.update()
|
||||||
data = self.dht_client.data
|
data = self.dht_client.data
|
||||||
|
|
||||||
if self.type == 'temperature':
|
if self.type == SENSOR_TEMPERATURE:
|
||||||
temperature = round(data['temperature'], 1)
|
temperature = round(data[SENSOR_TEMPERATURE], 1)
|
||||||
if (temperature >= -20) and (temperature < 80):
|
if (temperature >= -20) and (temperature < 80):
|
||||||
self._state = temperature
|
self._state = temperature
|
||||||
if self.temp_unit == TEMP_FAHRENHEIT:
|
if self.temp_unit == TEMP_FAHRENHEIT:
|
||||||
self._state = round(celsius_to_fahrenheit(temperature),
|
self._state = round(celsius_to_fahrenheit(temperature),
|
||||||
1)
|
1)
|
||||||
elif self.type == 'humidity':
|
elif self.type == SENSOR_HUMIDITY:
|
||||||
humidity = round(data['humidity'], 1)
|
humidity = round(data[SENSOR_HUMIDITY], 1)
|
||||||
if (humidity >= 0) and (humidity <= 100):
|
if (humidity >= 0) and (humidity <= 100):
|
||||||
self._state = humidity
|
self._state = humidity
|
||||||
|
|
||||||
|
@ -130,6 +133,6 @@ class DHTClient(object):
|
||||||
humidity, temperature = self.adafruit_dht.read_retry(self.sensor,
|
humidity, temperature = self.adafruit_dht.read_retry(self.sensor,
|
||||||
self.pin)
|
self.pin)
|
||||||
if temperature:
|
if temperature:
|
||||||
self.data['temperature'] = temperature
|
self.data[SENSOR_TEMPERATURE] = temperature
|
||||||
if humidity:
|
if humidity:
|
||||||
self.data['humidity'] = humidity
|
self.data[SENSOR_HUMIDITY] = humidity
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue