def test_return_no_sensors_for_invalid_type(sensors): # given not_supported_sensor_name = "NOT-SUPPORTED" # then with pytest.raises(UnsupportedSensorError): # when W1ThermSensor.get_available_sensors([not_supported_sensor_name])
def test_no_sensor_found(sensors, monkeypatch): """Test exception when no sensor was found""" monkeypatch.setattr(time, "sleep", lambda x: True) with pytest.raises(NoSensorFoundError, match="Could not find any sensor"): W1ThermSensor() with pytest.raises( NoSensorFoundError, match="Could not find sensor of type DS1822 with id 1" ): W1ThermSensor(Sensor.DS1822, "1")
def test_no_sensor_found(sensors): """Test exception when no sensor was found""" with pytest.raises( NoSensorFoundError, message="No Unknown temperature sensor with id '' found" ): W1ThermSensor() with pytest.raises( NoSensorFoundError, message="No DS1822 temperature sensor with id '1' found" ): W1ThermSensor(W1ThermSensor.THERM_SENSOR_DS1822, "1")
def test_no_sensor_found(sensors): """Test exception when no sensor was found""" with pytest.raises(NoSensorFoundError) as exc: W1ThermSensor() assert str(exc.value) == "No Unknown temperature sensor with id '' found" with pytest.raises(NoSensorFoundError) as exc: W1ThermSensor(W1ThermSensor.THERM_SENSOR_DS1822, '1') assert str(exc.value) == "No DS1822 temperature sensor with id '1' found"
def test_get_available_ds18s20_sensors(self): # create 3 DS18S20 sensors create_w1_therm_sensor(W1ThermSensor.THERM_SENSOR_DS18S20) create_w1_therm_sensor(W1ThermSensor.THERM_SENSOR_DS18S20) create_w1_therm_sensor(W1ThermSensor.THERM_SENSOR_DS18S20) sensors = W1ThermSensor.get_available_sensors([W1ThermSensor.THERM_SENSOR_DS18S20]) sensors.should.have.length_of(3) # create 2 DS18B20 sensors create_w1_therm_sensor(W1ThermSensor.THERM_SENSOR_DS18B20) create_w1_therm_sensor(W1ThermSensor.THERM_SENSOR_DS18B20) sensors = W1ThermSensor.get_available_sensors([W1ThermSensor.THERM_SENSOR_DS18S20]) sensors.should.have.length_of(3)
def test_get_available_sensors(self): create_w1_therm_sensor(W1ThermSensor.THERM_SENSOR_DS18B20) sensors = W1ThermSensor.get_available_sensors() sensors.should.have.length_of(1) sensors[0].type.should.be.equal(W1ThermSensor.THERM_SENSOR_DS18B20) create_w1_therm_sensor(W1ThermSensor.THERM_SENSOR_DS1822) create_w1_therm_sensor(W1ThermSensor.THERM_SENSOR_DS18S20) sensors = W1ThermSensor.get_available_sensors() sensors.should.have.length_of(3) W1ThermSensor.THERM_SENSOR_DS1822.should.be.within(s.type for s in sensors) W1ThermSensor.THERM_SENSOR_DS18S20.should.be.within(s.type for s in sensors) W1ThermSensor.THERM_SENSOR_DS18B20.should.be.within(s.type for s in sensors)
def test_sensor_not_ready_error(): sensor_id = create_w1_therm_sensor(W1ThermSensor.THERM_SENSOR_DS1822, w1_file=W1_FILE_NOT_READY) sensor = W1ThermSensor(W1ThermSensor.THERM_SENSOR_DS1822, sensor_id) sensor.get_temperature.when.called_with( W1ThermSensor.DEGREES_C).should.throw( SensorNotReadyError, "Sensor is not yet ready to read temperature")
def test_init_first_sensor_of_type_if_not_existent(sensors): # then with pytest.raises( NoSensorFoundError, match="Could not find any sensor of type DS18B20" ): # when W1ThermSensor(Sensor.DS18B20)
def test_get_available_sensors_of_type(sensors, sensor_types): """Test getting available sensors of specific type""" # given & when available_sensors = W1ThermSensor.get_available_sensors(sensor_types) # then expected_sensor_amount = len([s for s in sensors if s["type"] in sensor_types]) assert len(available_sensors) == expected_sensor_amount
def all(types, unit, resolution, as_json): # pylint: disable=redefined-builtin """Get temperatures of all available sensors""" sensors = W1ThermSensor.get_available_sensors(types) temperatures = [] for sensor in sensors: if resolution: sensor.set_resolution(resolution, persist=False) temperatures.append(sensor.get_temperature(unit)) if as_json: data = [{ "id": i, "hwid": s.id, "type": s.name, "temperature": t, "unit": unit } for i, s, t in zip(count(start=1), sensors, temperatures)] click.echo(json.dumps(data, indent=4, sort_keys=True)) else: click.echo("Got temperatures of {0} sensors:".format( click.style(str(len(sensors)), bold=True))) for i, sensor, temperature in zip(count(start=1), sensors, temperatures): click.echo( " Sensor {0} ({1}) measured temperature: {2} {3}".format( click.style(str(i), bold=True), click.style(sensor.id, bold=True), click.style(str(round(temperature, 2)), bold=True), click.style(unit, bold=True), ))
def test_unsupported_unit_error(self): unsupported_unit = 0xFF sensor_id = create_w1_therm_sensor(W1ThermSensor.THERM_SENSOR_DS1822) sensor = W1ThermSensor(W1ThermSensor.THERM_SENSOR_DS1822, sensor_id) sensor.get_temperature.when.called_with(unsupported_unit).should.throw( UnsupportedUnitError, "Only Degress C, F and Kelvin are currently supported")
def test_get_available_sensors(sensors): """Test getting available sensors""" # given & when available_sensors = W1ThermSensor.get_available_sensors() # then assert len(available_sensors) == len(sensors) assert {t.type for t in available_sensors} == {s['type'] for s in sensors}
def test_repr(self): sensor_id = create_w1_therm_sensor(W1ThermSensor.THERM_SENSOR_DS18B20) s1 = W1ThermSensor(W1ThermSensor.THERM_SENSOR_DS18B20, sensor_id) s2 = eval(repr(s1)) s1.id.should.be.equal(s2.id) s1.type.should.be.equal(s2.type)
def test_str(self): sensor_id = create_w1_therm_sensor(W1ThermSensor.THERM_SENSOR_DS18B20) s1 = W1ThermSensor(W1ThermSensor.THERM_SENSOR_DS18B20, sensor_id) str(s1).should.be.equal( "W1ThermSensor(name='DS18B20', type=40(0x28), id='%s')" % sensor_id)
def test_get_available_sensors(sensors): """Test getting available sensors""" # given & when available_sensors = W1ThermSensor.get_available_sensors() # then assert len(available_sensors) == len(sensors) assert {t.type for t in available_sensors} == {s["type"] for s in sensors}
def test_init_sensor_by_type_and_id(sensors, sensor_specs): """Test initialize sensor by given type and id""" # when sensor = W1ThermSensor(**sensor_specs) # then assert sensor.id == sensor_specs['sensor_id'] assert sensor.type == sensor_specs['sensor_type']
def test_get_precision(sensors, expected_precision): """Test getting the sensor precison""" # given sensor = W1ThermSensor() # when precision = sensor.get_precision() # then assert precision == pytest.approx(expected_precision)
def test_get_temperature_in_multiple_units(sensors, units, expected_temperatures): """Test getting a sensor temperature in multiple units""" # given sensor = W1ThermSensor() # when temperatures = sensor.get_temperatures(units) # then assert temperatures == pytest.approx(expected_temperatures)
def test_sensor_type_name(sensors, expected_sensor_name): """Test getting the sensor type name""" # given sensor = W1ThermSensor() # when sensor_name = sensor.type_name # then assert sensor_name == expected_sensor_name
def test_get_available_ds18s20_sensors(self): # create 3 DS18S20 sensors create_w1_therm_sensor(W1ThermSensor.THERM_SENSOR_DS18S20) create_w1_therm_sensor(W1ThermSensor.THERM_SENSOR_DS18S20) create_w1_therm_sensor(W1ThermSensor.THERM_SENSOR_DS18S20) sensors = W1ThermSensor.get_available_sensors( [W1ThermSensor.THERM_SENSOR_DS18S20]) sensors.should.have.length_of(3) # create 2 DS18B20 sensors create_w1_therm_sensor(W1ThermSensor.THERM_SENSOR_DS18B20) create_w1_therm_sensor(W1ThermSensor.THERM_SENSOR_DS18B20) sensors = W1ThermSensor.get_available_sensors( [W1ThermSensor.THERM_SENSOR_DS18S20]) sensors.should.have.length_of(3)
def test_get_available_sensors(): create_w1_therm_sensor(W1ThermSensor.THERM_SENSOR_DS18B20) sensors = W1ThermSensor.get_available_sensors() sensors.should.have.length_of(1) sensors[0].type.should.be.equal(W1ThermSensor.THERM_SENSOR_DS18B20) create_w1_therm_sensor(W1ThermSensor.THERM_SENSOR_DS1822) create_w1_therm_sensor(W1ThermSensor.THERM_SENSOR_DS18S20) sensors = W1ThermSensor.get_available_sensors() sensors.should.have.length_of(3) W1ThermSensor.THERM_SENSOR_DS1822.should.be.within(s.type for s in sensors) W1ThermSensor.THERM_SENSOR_DS18S20.should.be.within(s.type for s in sensors) W1ThermSensor.THERM_SENSOR_DS18B20.should.be.within(s.type for s in sensors)
def test_sensor_temperature_in_C(self): # create DS18B20 sensor with 20 C degrees sensor_id = create_w1_therm_sensor(W1ThermSensor.THERM_SENSOR_DS18B20, temperature=20) sensor = W1ThermSensor(W1ThermSensor.THERM_SENSOR_DS18B20, sensor_id) sensor.id.should.be.equal(sensor_id) sensor.get_temperature(W1ThermSensor.DEGREES_C).should.be.equal(20.0) # create DS18B20 sensor with 26.55 C degrees sensor_id = create_w1_therm_sensor(W1ThermSensor.THERM_SENSOR_DS18B20, temperature=26.55) sensor = W1ThermSensor(W1ThermSensor.THERM_SENSOR_DS18B20, sensor_id) sensor.id.should.be.equal(sensor_id) sensor.get_temperature(W1ThermSensor.DEGREES_C).should.be.equal( 26.55, epsilon=FLOAT_EPSILON)
def test_get_temperature_for_different_units(sensors, unit, expected_temperature): """Test getting a sensor temperature for different units""" # given sensor = W1ThermSensor() # when temperature = sensor.get_temperature(unit) # then assert temperature == pytest.approx(expected_temperature)
def test_init_specific_sensor(self): # create DS18B20 sensor sensor_id = create_w1_therm_sensor(W1ThermSensor.THERM_SENSOR_DS18B20) sensor = W1ThermSensor(W1ThermSensor.THERM_SENSOR_DS18B20, sensor_id) sensor.should.be.a(W1ThermSensor) sensor.type.should.be.equal(W1ThermSensor.THERM_SENSOR_DS18B20) sensor.id.should.be.equal(sensor_id)
def test_str_protocol(sensors): """Test __str__ protocol of a sensor instance""" # given sensor = W1ThermSensor() # when stringyfied = str(sensor) # then assert stringyfied == "W1ThermSensor(name='DS18B20', type=40(0x28), id='1')"
def test_unsupported_unit_error(sensors): """Test exception when requested to read temperature in unsupported unit""" # given sensor = W1ThermSensor() expected_error_msg = "Only Degrees C, F and Kelvin are currently supported" # when & then with pytest.raises(UnsupportedUnitError, message=expected_error_msg): sensor.get_temperature(unit=0xFF) # 0xFF is no valid unit id
def available(self): try: wts = W1ThermSensor(sensor_type=self.w1_type, sensor_id=self.w1_id) except NoSensorFoundError: return False except KernelModuleLoadError: return False else: return True
def test_repr_protocol(sensors): """Test the __repr__ protocol of a sensor instance""" # given sensor = W1ThermSensor() # when sensor_copy = eval(repr(sensor)) # then assert sensor.id == sensor_copy.id assert sensor.type == sensor_copy.type
def test_init_first_sensor_of_type(sensors, sensor_type): """Test that first found sensor of specific type is initialized if not sensor specs given""" # given sensor_id = sensors[0]['id'] # when sensor = W1ThermSensor(sensor_type) # then assert sensor.id == sensor_id assert sensor.type == sensor_type
def test_init_first_sensor_by_id(sensors, sensor_id): """Test that sensor can be initialized by id""" # given sensor_type = sensors[0]['type'] # when sensor = W1ThermSensor(sensor_id=sensor_id) # then assert sensor.id == sensor_id assert sensor.type == sensor_type
def test_sensor_type_name(): # create sensors of all types ds18s20_sensor_id = create_w1_therm_sensor( W1ThermSensor.THERM_SENSOR_DS18S20) ds1822_sensor_id = create_w1_therm_sensor( W1ThermSensor.THERM_SENSOR_DS1822) ds18b20_sensor_id = create_w1_therm_sensor( W1ThermSensor.THERM_SENSOR_DS18B20) sensor = W1ThermSensor(W1ThermSensor.THERM_SENSOR_DS18S20, ds18s20_sensor_id) sensor.type_name.should.be.equal("DS18S20") sensor = W1ThermSensor(W1ThermSensor.THERM_SENSOR_DS1822, ds1822_sensor_id) sensor.type_name.should.be.equal("DS1822") sensor = W1ThermSensor(W1ThermSensor.THERM_SENSOR_DS18B20, ds18b20_sensor_id) sensor.type_name.should.be.equal("DS18B20")
def test_init_first_sensor(sensors): """Test that first found sensor is initialized if no sensor specs given""" # given sensor_id = sensors[0]['id'] sensor_type = sensors[0]['type'] # when sensor = W1ThermSensor() # then assert sensor.id == sensor_id assert sensor.type == sensor_type
def test_sensor_not_ready(sensors): """Test exception when sensor is not ready yet""" # given sensor = W1ThermSensor() # when with pytest.raises(SensorNotReadyError) as exc: sensor.get_temperature() # then assert str(exc.value) == 'Sensor is not yet ready to read temperature'
def test_get_available_sensors_no_sensors(self): sensors = W1ThermSensor.get_available_sensors() sensors.should.be.empty