def test_dht22_special_method_repr(): """ expect a __repr__ format """ with mock.patch('mycodo.inputs.dht22.DHT22Sensor.get_measurement') as mock_measure: mock_measure.side_effect = [(0, 0, 0)] dht22 = DHT22Sensor(None, None, testing=True) dht22.read() assert "<DHT22Sensor(dewpoint=0.00)(humidity=0.00)(temperature=0.00)>" in repr(dht22)
def test_dht22_next_returns_dict(): """ next returns dict(altitude=float,pressure=int,temperature=float) """ with mock.patch('mycodo.inputs.dht22.DHT22Sensor.get_measurement') as mock_measure: mock_measure.side_effect = [(23, 50, 3000)] dht22 = DHT22Sensor(None, None, testing=True) assert dht22.next() == dict(dewpoint=23.0, humidity=50.0, temperature=3000.0)
def test_dht22_read_logs_unknown_errors(): """ verify that IOErrors are logged """ with LogCapture() as log_cap: with mock.patch('mycodo.inputs.dht22.DHT22Sensor.get_measurement', side_effect=Exception('msg')): DHT22Sensor(None, None, testing=True).read() expected_logs = ('mycodo.inputs.dht22', 'ERROR', 'DHT22Sensor raised an exception when taking a reading: msg') assert expected_logs in log_cap.actual()
def test_dht22_special_method_str(): """ expect a __str__ format """ with mock.patch('mycodo.inputs.dht22.DHT22Sensor.get_measurement') as mock_measure: mock_measure.side_effect = [(0, 0, 0)] dht22 = DHT22Sensor(None, None, testing=True) dht22.read() assert "Dew Point: 0.00" in str(dht22) assert "Humidity: 0.00" in str(dht22) assert "Temperature: 0.00" in str(dht22)
def test_dht22__iter__returns_iterator(): """ The iter methods must return an iterator in order to work properly """ with mock.patch('mycodo.inputs.dht22.DHT22Sensor.get_measurement') as mock_measure: mock_measure.side_effect = [(23, 50, 3000), (25, 55, 3200), (27, 60, 3400), (30, 65, 3300)] dht22 = DHT22Sensor(None, None, testing=True) assert isinstance(dht22.__iter__(), Iterator)
def test_dht22_iterates_using_in(): """ Verify that a DHT22Sensor object can use the 'in' operator """ with mock.patch('mycodo.inputs.dht22.DHT22Sensor.get_measurement') as mock_measure: mock_measure.side_effect = [(23, 50, 3000), (25, 55, 3200), (27, 60, 3400), (30, 65, 3300)] dht22 = DHT22Sensor(None, None, testing=True) expected_result_list = [dict(dewpoint=23.0, humidity=50.0, temperature=3000.0), dict(dewpoint=25.0, humidity=55.0, temperature=3200.0), dict(dewpoint=27.0, humidity=60.0, temperature=3400.0), dict(dewpoint=30.0, humidity=65.0, temperature=3300.0)] assert expected_result_list == [temp for temp in dht22]
def test_dht22_read_updates_temp(): """ Verify that DHT22Sensor(None, None, testing=True).read() gets the average temp """ with mock.patch('mycodo.inputs.dht22.DHT22Sensor.get_measurement') as mock_measure: mock_measure.side_effect = [(23, 50, 3000), (25, 55, 3200)] dht22 = DHT22Sensor(None, None, testing=True) assert dht22._dew_point is None assert dht22._humidity is None assert dht22._temperature is None assert not dht22.read() assert dht22._dew_point == 23.0 assert dht22._humidity == 50.0 assert dht22._temperature == 3000.0 assert not dht22.read() assert dht22._dew_point == 25.0 assert dht22._humidity == 55.0 assert dht22._temperature == 3200.0
def test_dht22_condition_properties(): """ verify temperature property """ with mock.patch('mycodo.inputs.dht22.DHT22Sensor.get_measurement') as mock_measure: mock_measure.side_effect = [(23, 50, 3000), (25, 55, 3200)] dht22 = DHT22Sensor(None, None, testing=True) assert dht22._dew_point is None # initial values assert dht22._humidity is None assert dht22._temperature is None assert dht22.dew_point == 23.0 assert dht22.dew_point == 23.0 assert dht22.humidity == 50.0 assert dht22.humidity == 50.0 assert dht22.temperature == 3000.0 assert dht22.temperature == 3000.0 assert not dht22.read() assert dht22.dew_point == 25.0 assert dht22.humidity == 55.0 assert dht22.temperature == 3200.0
from mycodo.inputs.tmp006 import TMP006Sensor from mycodo.inputs.tsl2561 import TSL2561Sensor from mycodo.inputs.tsl2591_sensor import TSL2591Sensor input_classes = [ AM2315Sensor(None, None, testing=True), AtlaspHSensor(None, testing=True), AtlasPT1000Sensor(None, testing=True), BH1750Sensor(None, None, None, None, testing=True), BME280Sensor(None, None, testing=True), BMP180Sensor(None, testing=True), BMP280Sensor(None, None, testing=True), ChirpSensor(None, None, testing=True), DHT11Sensor(None, None, testing=True), DHT22Sensor(None, None, testing=True), DS18B20Sensor(None, None, testing=True), HTU21DSensor(None, testing=True), K30Sensor(None, testing=True), LinuxCommand(None, 'measurement', testing=True), MHZ16Sensor(None, testing=True), MHZ19Sensor(None, testing=True), MycodoRam(testing=True), RaspberryPiCPUTemp(), RaspberryPiGPUTemp(), RaspberryPiCPULoad(), RaspberryPiFreeSpace(None, testing=True), SHT1x7xSensor(None, None, None, testing=True), SHT2xSensor(None, None, testing=True), SignalPWMInput(None, None, None, testing=True), SignalRPMInput(None, None, None, None, testing=True),
def __init__(self, ready, input_id): threading.Thread.__init__(self) self.logger = logging.getLogger( "mycodo.input_{id}".format(id=input_id.split('-')[0])) self.stop_iteration_counter = 0 self.thread_startup_timer = timeit.default_timer() self.thread_shutdown_timer = 0 self.ready = ready self.lock = {} self.measurement = None self.measurement_success = False self.control = DaemonControl() self.pause_loop = False self.verify_pause_loop = True self.sample_rate = db_retrieve_table_daemon( Misc, entry='first').sample_rate_controller_input self.input_id = input_id input_dev = db_retrieve_table_daemon( Input, unique_id=self.input_id) self.input_dev = input_dev self.input_name = input_dev.name self.unique_id = input_dev.unique_id self.i2c_bus = input_dev.i2c_bus self.location = input_dev.location self.power_output_id = input_dev.power_output_id self.measurements = input_dev.measurements self.convert_to_unit = input_dev.convert_to_unit self.device = input_dev.device self.interface = input_dev.interface self.device_loc = input_dev.device_loc self.baud_rate = input_dev.baud_rate self.period = input_dev.period self.resolution = input_dev.resolution self.sensitivity = input_dev.sensitivity self.cmd_command = input_dev.cmd_command self.cmd_measurement = input_dev.cmd_measurement self.cmd_measurement_units = input_dev.cmd_measurement_units self.thermocouple_type = input_dev.thermocouple_type self.ref_ohm = input_dev.ref_ohm # Serial self.pin_clock = input_dev.pin_clock self.pin_cs = input_dev.pin_cs self.pin_mosi = input_dev.pin_mosi self.pin_miso = input_dev.pin_miso # ADC self.adc_measure = input_dev.adc_measure self.adc_volts_min = input_dev.adc_volts_min self.adc_volts_max = input_dev.adc_volts_max self.adc_units_min = input_dev.adc_units_min self.adc_units_max = input_dev.adc_units_max self.adc_inverse_unit_scale = input_dev.adc_inverse_unit_scale # Edge detection self.switch_edge = input_dev.switch_edge self.switch_bouncetime = input_dev.switch_bouncetime self.switch_reset_period = input_dev.switch_reset_period # PWM and RPM options self.weighting = input_dev.weighting self.rpm_pulses_per_rev = input_dev.rpm_pulses_per_rev self.sample_time = input_dev.sample_time # Server options self.port = input_dev.port self.times_check = input_dev.times_check self.deadline = input_dev.deadline # Output that will activate prior to input read self.pre_output_id = input_dev.pre_output_id self.pre_output_duration = input_dev.pre_output_duration self.pre_output_during_measure = input_dev.pre_output_during_measure self.pre_output_setup = False self.next_measurement = time.time() self.get_new_measurement = False self.trigger_cond = False self.measurement_acquired = False self.pre_output_activated = False self.pre_output_locked = False self.pre_output_timer = time.time() # Check if Pre-Output ID actually exists output = db_retrieve_table_daemon(Output, entry='all') for each_output in output: if each_output.unique_id == self.pre_output_id and self.pre_output_duration: self.pre_output_setup = True smtp = db_retrieve_table_daemon(SMTP, entry='first') self.smtp_max_count = smtp.hourly_max self.email_count = 0 self.allowed_to_send_notice = True # Set up input lock self.input_lock = None self.lock_file = '/var/lock/input_pre_output_{id}'.format(id=self.pre_output_id) # Convert string I2C address to base-16 int if self.device in LIST_DEVICES_I2C: self.i2c_address = int(str(self.location), 16) # Set up edge detection of a GPIO pin if self.device == 'EDGE': if self.switch_edge == 'rising': self.switch_edge_gpio = GPIO.RISING elif self.switch_edge == 'falling': self.switch_edge_gpio = GPIO.FALLING else: self.switch_edge_gpio = GPIO.BOTH # Set up analog-to-digital converter if self.device in LIST_DEVICES_ADC: if self.device in ['ADS1x15', 'MCP342x']: self.adc_lock_file = "/var/lock/mycodo_adc_bus{bus}_0x{i2c:02X}.pid".format( bus=self.i2c_bus, i2c=self.i2c_address) elif self.device == 'MCP3008': self.adc_lock_file = "/var/lock/mycodo_adc_uart-{clock}-{cs}-{miso}-{mosi}".format( clock=self.pin_clock, cs=self.pin_cs, miso=self.pin_miso, mosi=self.pin_mosi) if self.device == 'ADS1x15' and self.location: from mycodo.devices.ads1x15 import ADS1x15Read self.adc = ADS1x15Read(self.input_dev) elif self.device == 'MCP3008': from mycodo.devices.mcp3008 import MCP3008Read self.adc = MCP3008Read(self.input_dev) elif self.device == 'MCP342x' and self.location: from mycodo.devices.mcp342x import MCP342xRead self.adc = MCP342xRead(self.input_dev) else: self.adc = None self.device_recognized = True # Set up inputs or devices if self.device in ['EDGE'] + LIST_DEVICES_ADC: self.measure_input = None elif self.device == 'MYCODO_RAM': from mycodo.inputs.mycodo_ram import MycodoRam self.measure_input = MycodoRam(self.input_dev) elif self.device == 'RPiCPULoad': from mycodo.inputs.raspi_cpuload import RaspberryPiCPULoad self.measure_input = RaspberryPiCPULoad(self.input_dev) elif self.device == 'RPi': from mycodo.inputs.raspi import RaspberryPiCPUTemp self.measure_input = RaspberryPiCPUTemp(self.input_dev) elif self.device == 'RPiFreeSpace': from mycodo.inputs.raspi_freespace import RaspberryPiFreeSpace self.measure_input = RaspberryPiFreeSpace(self.input_dev) elif self.device == 'AM2315': from mycodo.inputs.am2315 import AM2315Sensor self.measure_input = AM2315Sensor(self.input_dev) elif self.device == 'ATLAS_EC_I2C': from mycodo.inputs.atlas_ec import AtlasElectricalConductivitySensor self.measure_input = AtlasElectricalConductivitySensor(self.input_dev) elif self.device == 'ATLAS_EC_UART': from mycodo.inputs.atlas_ec import AtlasElectricalConductivitySensor self.measure_input = AtlasElectricalConductivitySensor(self.input_dev) elif self.device == 'ATLAS_PH_I2C': from mycodo.inputs.atlas_ph import AtlaspHSensor self.measure_input = AtlaspHSensor(self.input_dev) elif self.device == 'ATLAS_PH_UART': from mycodo.inputs.atlas_ph import AtlaspHSensor self.measure_input = AtlaspHSensor(self.input_dev) elif self.device == 'ATLAS_PT1000_I2C': from mycodo.inputs.atlas_pt1000 import AtlasPT1000Sensor self.measure_input = AtlasPT1000Sensor(self.input_dev) elif self.device == 'ATLAS_PT1000_UART': from mycodo.inputs.atlas_pt1000 import AtlasPT1000Sensor self.measure_input = AtlasPT1000Sensor(self.input_dev) elif self.device == 'BH1750': from mycodo.inputs.bh1750 import BH1750Sensor self.measure_input = BH1750Sensor(self.input_dev) elif self.device == 'BME280': from mycodo.inputs.bme280 import BME280Sensor self.measure_input = BME280Sensor(self.input_dev) elif self.device == 'BMP180': from mycodo.inputs.bmp180 import BMP180Sensor self.measure_input = BMP180Sensor(self.input_dev) elif self.device == 'BMP280': from mycodo.inputs.bmp280 import BMP280Sensor self.measure_input = BMP280Sensor(self.input_dev) elif self.device == 'CCS811': from mycodo.inputs.ccs811 import CCS811Sensor self.measure_input = CCS811Sensor(self.input_dev) elif self.device == 'CHIRP': from mycodo.inputs.chirp import ChirpSensor self.measure_input = ChirpSensor(self.input_dev) elif self.device == 'COZIR_CO2': from mycodo.inputs.cozir_co2 import COZIRSensor self.measure_input = COZIRSensor(self.input_dev) elif self.device == 'DHT11': from mycodo.inputs.dht11 import DHT11Sensor self.measure_input = DHT11Sensor(self.input_dev) elif self.device in ['DHT22', 'AM2302']: from mycodo.inputs.dht22 import DHT22Sensor self.measure_input = DHT22Sensor(self.input_dev) elif self.device == 'DS18B20': from mycodo.inputs.ds18b20 import DS18B20Sensor self.measure_input = DS18B20Sensor(self.input_dev) elif self.device == 'DS18S20': from mycodo.inputs.ds18s20 import DS18S20Sensor self.measure_input = DS18S20Sensor(self.input_dev) elif self.device == 'DS1822': from mycodo.inputs.ds1822 import DS1822Sensor self.measure_input = DS1822Sensor(self.input_dev) elif self.device == 'DS28EA00': from mycodo.inputs.ds28ea00 import DS28EA00Sensor self.measure_input = DS28EA00Sensor(self.input_dev) elif self.device == 'DS1825': from mycodo.inputs.ds1825 import DS1825Sensor self.measure_input = DS1825Sensor(self.input_dev) elif self.device == 'GPIO_STATE': from mycodo.inputs.gpio_state import GPIOState self.measure_input = GPIOState(self.input_dev) elif self.device == 'HDC1000': from mycodo.inputs.hdc1000 import HDC1000Sensor self.measure_input = HDC1000Sensor(self.input_dev) elif self.device == 'HTU21D': from mycodo.inputs.htu21d import HTU21DSensor self.measure_input = HTU21DSensor(self.input_dev) elif self.device == 'K30_UART': from mycodo.inputs.k30 import K30Sensor self.measure_input = K30Sensor(self.input_dev) elif self.device == 'LinuxCommand': from mycodo.inputs.linux_command import LinuxCommand self.measure_input = LinuxCommand(self.input_dev) elif self.device == 'MAX31850K': from mycodo.inputs.max31850k import MAX31850KSensor self.measure_input = MAX31850KSensor(self.input_dev) elif self.device == 'MAX31855': from mycodo.inputs.max31855 import MAX31855Sensor self.measure_input = MAX31855Sensor(self.input_dev) elif self.device == 'MAX31856': from mycodo.inputs.max31856 import MAX31856Sensor self.measure_input = MAX31856Sensor(self.input_dev) elif self.device == 'MAX31865': from mycodo.inputs.max31865 import MAX31865Sensor self.measure_input = MAX31865Sensor(self.input_dev) elif self.device == 'MH_Z16_I2C': from mycodo.inputs.mh_z16 import MHZ16Sensor self.measure_input = MHZ16Sensor(self.input_dev) elif self.device == 'MH_Z16_UART': from mycodo.inputs.mh_z16 import MHZ16Sensor self.measure_input = MHZ16Sensor(self.input_dev) elif self.device == 'MH_Z19_UART': from mycodo.inputs.mh_z19 import MHZ19Sensor self.measure_input = MHZ19Sensor(self.input_dev) elif self.device == 'MIFLORA': from mycodo.inputs.miflora_sensor import MifloraSensor self.measure_input = MifloraSensor(self.input_dev) elif self.device == 'SERVER_PING': from mycodo.inputs.server_ping import ServerPing self.measure_input = ServerPing(self.input_dev) elif self.device == 'SERVER_PORT_OPEN': from mycodo.inputs.server_port_open import ServerPortOpen self.measure_input = ServerPortOpen(self.input_dev) elif self.device == 'SHT1x_7x': from mycodo.inputs.sht1x_7x import SHT1x7xSensor self.measure_input = SHT1x7xSensor(self.input_dev) elif self.device == 'SHT2x': from mycodo.inputs.sht2x import SHT2xSensor self.measure_input = SHT2xSensor(self.input_dev) elif self.device == 'SIGNAL_PWM': from mycodo.inputs.signal_pwm import SignalPWMInput self.measure_input = SignalPWMInput(self.input_dev) elif self.device == 'SIGNAL_RPM': from mycodo.inputs.signal_rpm import SignalRPMInput self.measure_input = SignalRPMInput(self.input_dev) elif self.device == 'TMP006': from mycodo.inputs.tmp006 import TMP006Sensor self.measure_input = TMP006Sensor(self.input_dev) elif self.device == 'TSL2561': from mycodo.inputs.tsl2561 import TSL2561Sensor self.measure_input = TSL2561Sensor(self.input_dev) elif self.device == 'TSL2591': from mycodo.inputs.tsl2591_sensor import TSL2591Sensor self.measure_input = TSL2591Sensor(self.input_dev) else: self.device_recognized = False self.logger.debug("Device '{device}' not recognized".format( device=self.device)) raise Exception("'{device}' is not a valid device type.".format( device=self.device)) self.edge_reset_timer = time.time() self.input_timer = time.time() self.running = False self.lastUpdate = None
def __init__(self, ready, input_id): threading.Thread.__init__(self) self.logger = logging.getLogger( "mycodo.input_{id}".format(id=input_id)) self.stop_iteration_counter = 0 self.thread_startup_timer = timeit.default_timer() self.thread_shutdown_timer = 0 self.ready = ready self.lock = {} self.measurement = None self.updateSuccess = False self.input_id = input_id self.control = DaemonControl() self.pause_loop = False self.verify_pause_loop = True input_dev = db_retrieve_table_daemon(Input, device_id=self.input_id) self.input_sel = input_dev self.id = input_dev.id self.unique_id = input_dev.unique_id self.i2c_bus = input_dev.i2c_bus self.location = input_dev.location self.power_output_id = input_dev.power_relay_id self.measurements = input_dev.measurements self.device = input_dev.device self.interface = input_dev.interface self.device_loc = input_dev.device_loc self.baud_rate = input_dev.baud_rate self.period = input_dev.period self.resolution = input_dev.resolution self.sensitivity = input_dev.sensitivity self.cmd_command = input_dev.cmd_command self.cmd_measurement = input_dev.cmd_measurement self.cmd_measurement_units = input_dev.cmd_measurement_units self.mux_address_raw = input_dev.multiplexer_address self.mux_bus = input_dev.multiplexer_bus self.mux_chan = input_dev.multiplexer_channel self.adc_chan = input_dev.adc_channel self.adc_gain = input_dev.adc_gain self.adc_resolution = input_dev.adc_resolution self.adc_measure = input_dev.adc_measure self.adc_measure_units = input_dev.adc_measure_units self.adc_volts_min = input_dev.adc_volts_min self.adc_volts_max = input_dev.adc_volts_max self.adc_units_min = input_dev.adc_units_min self.adc_units_max = input_dev.adc_units_max self.adc_inverse_unit_scale = input_dev.adc_inverse_unit_scale self.sht_clock_pin = input_dev.sht_clock_pin self.sht_voltage = input_dev.sht_voltage # Edge detection self.switch_edge = input_dev.switch_edge self.switch_bouncetime = input_dev.switch_bouncetime self.switch_reset_period = input_dev.switch_reset_period # PWM and RPM options self.weighting = input_dev.weighting self.rpm_pulses_per_rev = input_dev.rpm_pulses_per_rev self.sample_time = input_dev.sample_time # Server options self.port = input_dev.port self.times_check = input_dev.times_check self.deadline = input_dev.deadline # Output that will activate prior to input read self.pre_output_id = input_dev.pre_relay_id self.pre_output_duration = input_dev.pre_relay_duration self.pre_output_setup = False self.next_measurement = time.time() self.get_new_measurement = False self.trigger_cond = False self.measurement_acquired = False self.pre_output_activated = False self.pre_output_timer = time.time() output = db_retrieve_table_daemon(Output, entry='all') for each_output in output: # Check if output ID actually exists if each_output.id == self.pre_output_id and self.pre_output_duration: self.pre_output_setup = True smtp = db_retrieve_table_daemon(SMTP, entry='first') self.smtp_max_count = smtp.hourly_max self.email_count = 0 self.allowed_to_send_notice = True # Convert string I2C address to base-16 int if self.device in LIST_DEVICES_I2C: self.i2c_address = int(str(self.location), 16) # Set up multiplexer if enabled if self.device in LIST_DEVICES_I2C and self.mux_address_raw: self.mux_address_string = self.mux_address_raw self.mux_address = int(str(self.mux_address_raw), 16) self.mux_lock = "/var/lock/mycodo_multiplexer_0x{i2c:02X}.pid".format( i2c=self.mux_address) self.mux_lock = fasteners.InterProcessLock(self.mux_lock) self.mux_lock_acquired = False self.multiplexer = TCA9548A(self.mux_bus, self.mux_address) else: self.multiplexer = None # Set up edge detection of a GPIO pin if self.device == 'EDGE': if self.switch_edge == 'rising': self.switch_edge_gpio = GPIO.RISING elif self.switch_edge == 'falling': self.switch_edge_gpio = GPIO.FALLING else: self.switch_edge_gpio = GPIO.BOTH # Lock multiplexer, if it's enabled if self.multiplexer: self.lock_multiplexer() # Set up analog-to-digital converter if self.device in ['ADS1x15', 'MCP342x'] and self.location: self.adc_lock_file = "/var/lock/mycodo_adc_bus{bus}_0x{i2c:02X}.pid".format( bus=self.i2c_bus, i2c=self.i2c_address) if self.device == 'ADS1x15': self.adc = ADS1x15Read(self.i2c_address, self.i2c_bus, self.adc_chan, self.adc_gain) elif self.device == 'MCP342x': self.adc = MCP342xRead(self.i2c_address, self.i2c_bus, self.adc_chan, self.adc_gain, self.adc_resolution) else: self.adc = None self.device_recognized = True # Set up inputs or devices if self.device in ['EDGE', 'ADS1x15', 'MCP342x']: self.measure_input = None elif self.device == 'MYCODO_RAM': self.measure_input = MycodoRam() elif self.device == 'RPiCPULoad': self.measure_input = RaspberryPiCPULoad() elif self.device == 'RPi': self.measure_input = RaspberryPiCPUTemp() elif self.device == 'RPiFreeSpace': self.measure_input = RaspberryPiFreeSpace(self.location) elif self.device == 'AM2302': self.measure_input = DHT22Sensor(int(self.location)) elif self.device == 'AM2315': self.measure_input = AM2315Sensor(self.i2c_bus, power=self.power_output_id) elif self.device == 'ATLAS_PH_I2C': self.measure_input = AtlaspHSensor(self.interface, i2c_address=self.i2c_address, i2c_bus=self.i2c_bus, sensor_sel=self.input_sel) elif self.device == 'ATLAS_PH_UART': self.measure_input = AtlaspHSensor(self.interface, device_loc=self.device_loc, baud_rate=self.baud_rate, sensor_sel=self.input_sel) elif self.device == 'ATLAS_PT1000_I2C': self.measure_input = AtlasPT1000Sensor( self.interface, i2c_address=self.i2c_address, i2c_bus=self.i2c_bus) elif self.device == 'ATLAS_PT1000_UART': self.measure_input = AtlasPT1000Sensor(self.interface, device_loc=self.device_loc, baud_rate=self.baud_rate) elif self.device == 'BH1750': self.measure_input = BH1750Sensor(self.i2c_address, self.i2c_bus, self.resolution, self.sensitivity) elif self.device == 'BME280': self.measure_input = BME280Sensor(self.i2c_address, self.i2c_bus) elif self.device == 'BMP180': self.measure_input = BMP180Sensor(self.i2c_bus) elif self.device == 'BMP280': self.measure_input = BMP280Sensor(self.i2c_address, self.i2c_bus) elif self.device == 'CHIRP': self.measure_input = ChirpSensor(self.i2c_address, self.i2c_bus) elif self.device == 'DS18B20': self.measure_input = DS18B20Sensor(self.location) elif self.device == 'DHT11': self.measure_input = DHT11Sensor(self.input_id, int(self.location), power=self.power_output_id) elif self.device == 'DHT22': self.measure_input = DHT22Sensor(int(self.location), power=self.power_output_id) elif self.device == 'GPIO_STATE': self.measure_input = GPIOState(int(self.location)) elif self.device == 'HTU21D': self.measure_input = HTU21DSensor(self.i2c_bus) elif self.device == 'K30_UART': self.measure_input = K30Sensor(self.device_loc, baud_rate=self.baud_rate) elif self.device == 'MH_Z16_I2C': self.measure_input = MHZ16Sensor(self.interface, i2c_address=self.i2c_address, i2c_bus=self.i2c_bus) elif self.device == 'MH_Z16_UART': self.measure_input = MHZ16Sensor(self.interface, device_loc=self.device_loc, baud_rate=self.baud_rate) elif self.device == 'MH_Z19_UART': self.measure_input = MHZ19Sensor(self.device_loc, baud_rate=self.baud_rate) elif self.device == 'SHT1x_7x': self.measure_input = SHT1x7xSensor(int(self.location), self.sht_clock_pin, self.sht_voltage) elif self.device == 'SHT2x': self.measure_input = SHT2xSensor(self.i2c_address, self.i2c_bus) elif self.device == 'SIGNAL_PWM': self.measure_input = SignalPWMInput(int(self.location), self.weighting, self.sample_time) elif self.device == 'SIGNAL_RPM': self.measure_input = SignalRPMInput(int(self.location), self.weighting, self.rpm_pulses_per_rev, self.sample_time) elif self.device == 'TMP006': self.measure_input = TMP006Sensor(self.i2c_address, self.i2c_bus) elif self.device == 'TSL2561': self.measure_input = TSL2561Sensor(self.i2c_address, self.i2c_bus) elif self.device == 'TSL2591': self.measure_input = TSL2591Sensor(self.i2c_address, self.i2c_bus) elif self.device == 'LinuxCommand': self.measure_input = LinuxCommand(self.cmd_command, self.cmd_measurement) elif self.device == 'SERVER_PING': self.measure_input = ServerPing(self.location, self.times_check, self.deadline) elif self.device == 'SERVER_PORT_OPEN': self.measure_input = ServerPortOpen(self.location, self.port) else: self.device_recognized = False self.logger.debug( "Device '{device}' not recognized".format(device=self.device)) raise Exception("'{device}' is not a valid device type.".format( device=self.device)) if self.multiplexer: self.unlock_multiplexer() self.edge_reset_timer = time.time() self.input_timer = time.time() self.running = False self.lastUpdate = None
def test_dht22_read_returns_1_on_exception(): """ Verify the read() method returns true on error """ with mock.patch('mycodo.inputs.dht22.DHT22Sensor.get_measurement', side_effect=Exception): assert DHT22Sensor(None, None, testing=True).read()
def test_dht22_raises_exception(): """ stops iteration on read() error """ with mock.patch('mycodo.inputs.dht22.DHT22Sensor.get_measurement', side_effect=IOError): with pytest.raises(StopIteration): DHT22Sensor(None, None, testing=True).next()