Exemplo n.º 1
0
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")
Exemplo n.º 2
0
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"
Exemplo n.º 3
0
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")
Exemplo n.º 4
0
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']
Exemplo n.º 5
0
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)
Exemplo n.º 6
0
 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")
Exemplo n.º 7
0
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")
Exemplo n.º 8
0
    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)
Exemplo n.º 9
0
    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)
Exemplo n.º 10
0
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)
Exemplo n.º 11
0
    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)
Exemplo n.º 12
0
    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)
Exemplo n.º 13
0
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)
Exemplo n.º 14
0
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
Exemplo n.º 15
0
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)
Exemplo n.º 16
0
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')"
Exemplo n.º 17
0
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
Exemplo n.º 18
0
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
Exemplo n.º 19
0
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
Exemplo n.º 20
0
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
Exemplo n.º 21
0
 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
Exemplo n.º 22
0
def test_setting_invalid_resolution(sensors, resolution):
    """Test setting invalid resolution for sensor"""
    # given
    sensor = W1ThermSensor()
    expected_error_msg = "The given sensor resolution '{}' is out of range (9-12)".format(
        resolution)

    # when & then
    with pytest.raises(ValueError, message=expected_error_msg):
        sensor.set_resolution(resolution)
Exemplo n.º 23
0
def test_sensor_partially_disconnected(sensors):
    """Test handling the partially disconnected sensor with wrong value"""
    # given
    sensor = W1ThermSensor()
    expected_error_msg = "Sensor {} is not yet ready to read temperature".format(
        sensor.id)

    # when & then
    with pytest.raises(SensorNotReadyError, message=expected_error_msg):
        sensor.get_temperature()
Exemplo n.º 24
0
def test_sensor_all_temperature_units():
    # 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_temperatures([
        W1ThermSensor.DEGREES_C, W1ThermSensor.DEGREES_F, W1ThermSensor.KELVIN
    ]).should.be.equal([20, 68, 293.15])
Exemplo n.º 25
0
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")
Exemplo n.º 26
0
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
Exemplo n.º 27
0
def test_sensor_not_ready(sensors):
    """Test exception when sensor is not ready yet"""
    # given
    sensor = W1ThermSensor()
    expected_error_msg = "Sensor {} is not yet ready to read temperature".format(
        sensor.id)

    # when & then
    with pytest.raises(SensorNotReadyError, message=expected_error_msg):
        sensor.get_temperature()
Exemplo n.º 28
0
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'
Exemplo n.º 29
0
    def test_sensor_temperature_in_K(self):
        # 20 C = 293.15 K
        # 26.55 C = 299.7 K

        # 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.KELVIN).should.be.equal(293.15)

        # 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.KELVIN).should.be.equal(
            299.7, epsilon=FLOAT_EPSILON)
Exemplo n.º 30
0
def test_unsupported_unit_error(sensors):
    """Test exception when requested to read temperature in unsupported unit"""
    # given
    sensor = W1ThermSensor()
    # when
    with pytest.raises(UnsupportedUnitError) as exc:
        sensor.get_temperature(unit=0xFF)  # 0xFF is no valid unit id

    # then
    assert str(
        exc.value) == 'Only Degrees C, F and Kelvin are currently supported'