#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# (c) Copyright 2026 Sensirion AG, Switzerland
#
# THIS FILE IS AUTOMATICALLY GENERATED!
#
# Generator: sensirion-driver-generator 1.6.2
# Product: sfa4x
# Model-Version: 2.1.0
#
"""
The class Sfa4xDeviceBase implements the low level interface of the sensor.
The class Sfa4xDevice extends the Sfa4xDeviceBase. It provides additional functions to ease the use of the
sensor.
"""
from sensirion_driver_adapters.transfer import execute_transfer
from sensirion_driver_support_types.mixin_access import MixinAccess
from sensirion_i2c_sfa4x.commands import (GetProductId, GetSerialNumber, ReadMeasurementDataRaw, ReadSelftestData,
StartContinuousMeasurement, StartSelftest, StopContinuousMeasurement)
from sensirion_i2c_sfa4x.result_types import (SignalHchoConcentration, SignalRelativeHumidity, SignalTemperature)
[docs]
class Sfa4xDeviceBase:
"""Low level API implementation of SFA4X"""
[docs]
def __init__(self, channel):
self._channel = channel
@property
def channel(self):
return self._channel
[docs]
def start_continuous_measurement(self):
"""Start a continuous measurement (interval 1 s)."""
transfer = StartContinuousMeasurement()
return execute_transfer(self._channel, transfer)
[docs]
def read_measurement_data_raw(self):
"""
Read HCHO, relative humidity, temperature, sensor info, sensor status, after calling *start_continous_measurement()*
:return hcho_concentration:
Calibrated HCHO concentration read from the sensor.
:return relative_humidity:
Relative humidity read from the sensor.
:return temperature:
Temperature read from the sensor.
:return status:
Information about the sensor status.
"""
transfer = ReadMeasurementDataRaw()
return execute_transfer(self._channel, transfer)
[docs]
def stop_continuous_measurement(self):
"""Stop continuous measurement to run other i2c commands or to save power."""
transfer = StopContinuousMeasurement()
return execute_transfer(self._channel, transfer)
[docs]
def get_serial_number(self):
"""
Read the sensor's unique serial number.
:return serial_number:
The 48-bit unique serial number of the sensor.
"""
transfer = GetSerialNumber()
return execute_transfer(self._channel, transfer)[0]
[docs]
def start_selftest(self):
"""
The self-test is a special autonomous test, able to detect certain malfunctions, e.g. due to bad soldering during
assembly. The test checks the integrity of electric connections between the user interface and the sensor cell.
It is recommended to run the self-test once, after the sensor has been soldered to the PCB.
After powering on the device, it is in idle state. The start_selftest command initiates a special measurement
mode. Refer to the read_selftest_data command for instructions on how to read out the sensor data after starting the self-test.
The self-test brings the sensor into special measurement mode for 5 – 6 minutes.
"""
transfer = StartSelftest()
return execute_transfer(self._channel, transfer)
[docs]
def read_selftest_data(self):
"""
After starting the self-test, the read_selftest_data command is used to obtain the selftest status and result.
It is recommended to trigger a readout e.g. every 10 seconds, until the output is different from 65535 (hex: FFFF).
Interpretation of the result:
* 65535 (hex: FFFF): Test is still running. Wait longer.
* 0: Test successful.
* other: Test failed.
:return selftest_result:
Self-test result.
"""
transfer = ReadSelftestData()
return execute_transfer(self._channel, transfer)[0]
[docs]
def get_product_id(self):
"""
Read the sensor's product id.
:return product_id:
4-byte product Id. Bytes 1-3 product code, byte 4 revision
"""
transfer = GetProductId()
return execute_transfer(self._channel, transfer)[0]
[docs]
class Sfa4xDevice(Sfa4xDeviceBase):
"""Driver class implementation of SFA4X"""
#: Access to base class
sfa4x = MixinAccess()
[docs]
def __init__(self, channel):
super().__init__(channel)
[docs]
def read_measurement_data(self):
"""
reads measurement data
:return hcho_concentration:
Measured HCHO concentration
:return relative_humidity:
Measured relative humidity
:return temperature:
Measured relative temperature
:return status:
Measurement status
"""
(raw_hcho_concentration, raw_relative_humidity, raw_temperature, raw_status
) = self.sfa4x.read_measurement_data_raw()
return (SignalHchoConcentration(raw_hcho_concentration), SignalRelativeHumidity(raw_relative_humidity),
SignalTemperature(raw_temperature), raw_status)