Exemplo n.º 1
0
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])
Exemplo n.º 2
0
    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)
Exemplo n.º 3
0
    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)
Exemplo n.º 4
0
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}
Exemplo n.º 5
0
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
Exemplo n.º 6
0
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}
Exemplo n.º 7
0
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),
                ))
Exemplo n.º 8
0
    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)
Exemplo n.º 9
0
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)
Exemplo n.º 10
0
def get(id_, hwid, type_, unit, resolution, as_json, offset):
    """Get temperature of a specific sensor"""
    if id_ and (hwid or type_):
        raise click.BadArgumentUsage(
            "If --id is given --hwid and --type are not allowed.")

    if id_:
        try:
            sensor = W1ThermSensor.get_available_sensors()[id_ - 1]
        except IndexError:
            error_msg = ("No sensor with id {0} available. ".format(id_) +
                         "Use the ls command to show all available sensors.")
            if CLICK_MAJOR_VERSION >= 7:  # pragma: no cover
                raise click.BadOptionUsage("--id", error_msg)
            else:  # pragma: no cover
                raise click.BadOptionUsage(error_msg)
    else:
        sensor = W1ThermSensor(type_, hwid)

    if resolution:
        sensor.set_resolution(resolution, persist=False)

    if offset:
        sensor.set_offset(offset, unit)

    temperature = sensor.get_temperature(unit)

    if as_json:
        data = {
            "hwid": sensor.id,
            "offset": offset,
            "type": sensor.name,
            "temperature": temperature,
            "unit": unit,
        }
        click.echo(json.dumps(data, indent=4, sort_keys=True))
    else:
        click.echo("Sensor {0} measured temperature: {1} {2}".format(
            click.style(sensor.id, bold=True),
            click.style(str(temperature), bold=True),
            click.style(unit, bold=True),
        ))
Exemplo n.º 11
0
def resolution(resolution, id_, hwid, type_):
    """Change the resolution for the sensor and persist it in the sensor's EEPROM"""
    if id_ and (hwid or type_):
        raise click.BadArgumentUsage(
            "If --id is given --hwid and --type are not allowed.")

    if id_:
        try:
            sensor = W1ThermSensor.get_available_sensors()[id_ - 1]
        except IndexError:
            error_msg = ("No sensor with id {0} available. ".format(id_) +
                         "Use the ls command to show all available sensors.")
            if CLICK_MAJOR_VERSION >= 7:  # pragma: no cover
                raise click.BadOptionUsage("--id", error_msg)
            else:  # pragma: no cover
                raise click.BadOptionUsage(error_msg)
    else:
        sensor = W1ThermSensor(type_, hwid)

    sensor.set_resolution(resolution, persist=True)
Exemplo n.º 12
0
def ls(types, as_json, resolution):  # pylint: disable=invalid-name
    """List all available sensors"""
    sensors = W1ThermSensor.get_available_sensors(types)

    if as_json:
        if resolution:
            data = [{
                "id": i,
                "hwid": s.id,
                "type": s.name,
                "resolution": s.get_resolution(),
            } for i, s in enumerate(sensors, 1)]
        else:
            data = [{
                "id": i,
                "hwid": s.id,
                "type": s.name
            } for i, s in enumerate(sensors, 1)]
        click.echo(json.dumps(data, indent=4, sort_keys=True))
    else:
        click.echo("Found {0} sensors:".format(
            click.style(str(len(sensors)), bold=True)))
        for i, sensor in enumerate(sensors, 1):
            if resolution:
                click.echo("  {0}. HWID: {1} Type: {2} Resolution: {3}".format(
                    click.style(str(i), bold=True),
                    click.style(sensor.id, bold=True),
                    click.style(sensor.name, bold=True),
                    click.style(str(sensor.get_resolution()), bold=True),
                ))
            else:
                click.echo("  {0}. HWID: {1} Type: {2}".format(
                    click.style(str(i), bold=True),
                    click.style(sensor.id, bold=True),
                    click.style(sensor.name, bold=True),
                ))
Exemplo n.º 13
0
 def test_get_available_sensors_no_sensors(self):
     sensors = W1ThermSensor.get_available_sensors()
     sensors.should.be.empty
Exemplo n.º 14
0
def test_get_available_sensor_of_type_by_string_name(sensors):
    # given & when
    available_sensors = W1ThermSensor.get_available_sensors(["DS18B20"])
    # then
    assert len(available_sensors) == 1
Exemplo n.º 15
0
 def test_get_available_sensors_no_sensors(self):
     sensors = W1ThermSensor.get_available_sensors()
     sensors.should.be.empty