<s> """<STR_LIT>""" <EOL> import logging <EOL> from datetime import timedelta <EOL> from homeassistant . const import TEMP_FAHRENHEIT <EOL> from homeassistant . helpers . entity import Entity <EOL> from homeassistant . util import Throttle <EOL> REQUIREMENTS = [ '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ] <EOL> _LOGGER = logging . getLogger ( __name__ ) <EOL> SENSOR_TYPES = { <EOL> '<STR_LIT>' : [ '<STR_LIT>' , None ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT:%>' ] <EOL> } <EOL> DEFAULT_NAME = "<STR_LIT>" <EOL> MIN_TIME_BETWEEN_UPDATES = timedelta ( seconds = <NUM_LIT:30> ) <EOL> def setup_platform ( hass , config , add_devices , discovery_info = None ) : <EOL> """<STR_LIT>""" <EOL> import Adafruit_DHT <EOL> SENSOR_TYPES [ '<STR_LIT>' ] [ <NUM_LIT:1> ] = hass . config . temperature_unit <EOL> unit = hass . config . temperature_unit <EOL> available_sensors = { <EOL> "<STR_LIT>" : Adafruit_DHT . DHT11 , <EOL> "<STR_LIT>" : Adafruit_DHT . DHT22 , <EOL> "<STR_LIT>" : Adafruit_DHT . AM2302 <EOL> } <EOL> sensor = available_sensors [ config [ '<STR_LIT>' ] ] <EOL> pin = config [ '<STR_LIT>' ] <EOL> if not sensor or not pin : <EOL> _LOGGER . error ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> return None <EOL> data = DHTClient ( Adafruit_DHT , sensor , pin ) <EOL> dev = [ ] <EOL> name = config . get ( '<STR_LIT:name>' , DEFAULT_NAME ) <EOL> try : <EOL> for variable in config [ '<STR_LIT>' ] : <EOL> if variable not in SENSOR_TYPES : <EOL> _LOGGER . error ( '<STR_LIT>' , variable ) <EOL> else : <EOL> dev . append ( DHTSensor ( data , variable , unit , name ) ) <EOL> except KeyError : <EOL> pass <EOL> add_devices ( dev ) <EOL> class DHTSensor ( Entity ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , dht_client , sensor_type , temp_unit , name ) : <EOL> """<STR_LIT>""" <EOL> self . client_name = name <EOL> self . _name = SENSOR_TYPES [ sensor_type ] [ <NUM_LIT:0> ] <EOL> self . dht_client = dht_client <EOL> self . temp_unit = temp_unit <EOL> self . type = sensor_type <EOL> self . _state = None <EOL> self . _unit_of_measurement = SENSOR_TYPES [ sensor_type ] [ <NUM_LIT:1> ] <EOL> self . update ( ) <EOL> @ property <EOL> def name ( self ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' . format ( self . client_name , self . _name ) <EOL> @ property <EOL> def state ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _state <EOL> @ property <EOL> def unit_of_measurement ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _unit_of_measurement <EOL> def update ( self ) : <EOL> """<STR_LIT>""" <EOL> self . dht_client . update ( ) <EOL> data = self . dht_client . data <EOL> if self . type == '<STR_LIT>' : <EOL> self . _state = round ( data [ '<STR_LIT>' ] , <NUM_LIT:1> ) <EOL> if self . temp_unit == TEMP_FAHRENHEIT : <EOL> self . _state = round ( data [ '<STR_LIT>' ] * <NUM_LIT> + <NUM_LIT:32> , <NUM_LIT:1> ) <EOL> elif self . type == '<STR_LIT>' : <EOL> self . _state = round ( data [ '<STR_LIT>' ] , <NUM_LIT:1> ) <EOL> class DHTClient ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , adafruit_dht , sensor , pin ) : <EOL> """<STR_LIT>""" <EOL> self . adafruit_dht = adafruit_dht <EOL> self . sensor = sensor <EOL> self . pin = pin <EOL> self . data = dict ( ) <EOL> @ Throttle ( MIN_TIME_BETWEEN_UPDATES ) <EOL> def update ( self ) : <EOL> """<STR_LIT>""" <EOL> humidity , temperature = self . adafruit_dht . read_retry ( self . sensor , <EOL> self . pin ) <EOL> if temperature : <EOL> self . data [ '<STR_LIT>' ] = temperature <EOL> if humidity : <EOL> self . data [ '<STR_LIT>' ] = humidity </s>
