Example #1
0
def test_empty():
    cmd = SensirionI2cCommand(None, None, None, 0.1, 0.2,
                              CrcCalculator(8, 0x31, 0xFF))
    assert cmd.tx_data is None
    assert cmd.rx_length is None
    assert cmd.read_delay == 0.1
    assert cmd.timeout == 0.2
Example #2
0
def test_command_and_tx_data():
    cmd = SensirionI2cCommand(0x1337, b"\xDE\xAD\xBE\xEF", 5, 0.1, 0.2,
                              CrcCalculator(8, 0x31, 0xFF))
    assert type(cmd.tx_data) is bytes
    assert cmd.tx_data == b"\x13\x37\xDE\xAD\x98\xBE\xEF\x92"
    assert cmd.rx_length is 5
    assert cmd.read_delay == 0.1
    assert cmd.timeout == 0.2
Example #3
0
def test_only_command():
    cmd = SensirionI2cCommand(0x1337, None, None, 0.1, 0.2,
                              CrcCalculator(8, 0x31, 0xFF))
    assert type(cmd.tx_data) is bytes
    assert cmd.tx_data == b"\x13\x37"
    assert cmd.rx_length is None
    assert cmd.read_delay == 0.1
    assert cmd.timeout == 0.2
Example #4
0
def test_only_tx_data_4_bytes():
    cmd = SensirionI2cCommand(None, b"\xDE\xAD\xBE\xEF", None, 0.1, 0.2,
                              CrcCalculator(8, 0x31, 0xFF))
    assert type(cmd.tx_data) is bytes
    assert cmd.tx_data == b"\xDE\xAD\x98\xBE\xEF\x92"
    assert cmd.rx_length is None
    assert cmd.read_delay == 0.1
    assert cmd.timeout == 0.2
Example #5
0
def test_single_byte_command():
    cmd = SensirionI2cCommand(0x42, b"\xDE\xAD\xBE\xEF", 5, 0.1, 0.2,
                              CrcCalculator(8, 0x31, 0xFF), 1)
    assert type(cmd.tx_data) is bytes
    assert cmd.tx_data == b"\x42\xDE\xAD\x98\xBE\xEF\x92"
    assert cmd.rx_length is 5
    assert cmd.read_delay == 0.1
    assert cmd.timeout == 0.2
Example #6
0
    def interpret_response(self, data):
        """
        Converts the raw response from the device to the proper data type.

        :param bytes data: Received raw bytes from the read operation.
        :return: The read humidity.
        :rtype: :py:class:`~sensirion_i2c_sht.sht2x.response_types.Sht2xHumidity`
        """  # noqa: E501
        checked_data = SensirionI2cCommand.interpret_response(self, data)
        return Sht2xHumidity(unpack(">H", checked_data)[0])
Example #7
0
    def interpret_response(self, data):
        """
        Converts the raw response from the device to the proper data type.

        :param bytes data: Received raw bytes from the read operation.
        :return: The read words from the metal ROM.
        :rtype: list(int)
        """
        checked_data = SensirionI2cCommand.interpret_response(self, data)
        n_words = int(len(checked_data) / 2)
        words = unpack(">{}H".format(n_words), checked_data)
        return words
Example #8
0
    def interpret_response(self, data):
        """
        Converts the raw response from the device to the proper data type.

        :param bytes data:
            Received raw bytes from the read operation.
        :return:
            The measured temperature and humidity.

            - temperature (:py:class:`~sensirion_i2c_sht.sht4x.response_types.Sht4xTemperature`) -
              Temperature response object.
            - humidity (:py:class:`~sensirion_i2c_sht.sht4x.response_types.Sht4xHumidity`) -
              Humidity response object.
        :rtype:
            tuple
        """  # noqa: E501
        checked_data = SensirionI2cCommand.interpret_response(self, data)
        temperature_ticks, humidity_ticks = unpack(">2H", checked_data)
        return Sht4xTemperature(temperature_ticks), \
            Sht4xHumidity(humidity_ticks)
Example #9
0
def test_interpret_response_empty():
    cmd = SensirionI2cCommand(None, None, 0, 0.1, 0.2,
                              CrcCalculator(8, 0x31, 0xFF))
    response = cmd.interpret_response(b"")
    assert response is None
Example #10
0
def test_post_processing_time_float():
    cmd = SensirionI2cCommand(0x1337, b"\xDE\xAD\xBE\xEF", 5, 0.1, 0.2,
                              CrcCalculator(8, 0x31, 0xFF), 2, 2.3)
    assert type(cmd.post_processing_time) is float
    assert cmd.post_processing_time == 2.3
Example #11
0
def test_interpret_response_crc_error():
    cmd = SensirionI2cCommand(None, None, 6, 0.1, 0.2,
                              CrcCalculator(8, 0x31, 0xFF))
    with pytest.raises(I2cChecksumError):
        cmd.interpret_response(b"\xDE\xAD\x98\xBE\xEF\x93")  # wrong crc
Example #12
0
def test_interpret_response_6_bytes():
    cmd = SensirionI2cCommand(None, None, 6, 0.1, 0.2,
                              CrcCalculator(8, 0x31, 0xFF))
    response = cmd.interpret_response(b"\xDE\xAD\x98\xBE\xEF\x92")
    assert type(response) is bytes
    assert response == b"\xDE\xAD\xBE\xEF"
Example #13
0
def test_interpret_response_1_byte():
    cmd = SensirionI2cCommand(None, None, 2, 0.1, 0.2,
                              CrcCalculator(8, 0x31, 0xFF))
    response = cmd.interpret_response(b"\xDE")
    assert type(response) is bytes
    assert response == b"\xDE"