Scale

class Scale(name: str, address: str, min_threshold: float, max_threshold: float, calibration_key: str = 'SCALE_CALIBRATION_VALUE', weight_to_calibrate_key: str = 'SCALE_WEIGHT_TO_CALIBRATE')[source]

Bases: NullScale

Interface for an I2C scale sensor.

Parameters:
  • name (str) – Which scale it is (“corridor” or “box”), shown in the log.

  • address (str) – The I2C address of the scale as a hex string (e.g., “0x2A”).

  • min_threshold (float) – The minimum weight threshold for valid measurements.

  • max_threshold (float) – The maximum weight threshold for valid measurements.

  • calibration_key (str) – The setting key holding this scale’s calibration factor. Each physical scale needs its own key so calibrating one does not overwrite another’s calibration.

  • weight_to_calibrate_key (str) – The setting key holding the known weight last used to calibrate this scale.

I2C_ADDR

The I2C address of the scale.

Type:

int

REG_DATA_GET_RAM_DATA

Register address for getting raw data.

Type:

int

bus

The I2C bus number.

Type:

int

calibration

Calibration factor.

Type:

float

calibration_key

The setting key this scale’s calibration is stored under.

Type:

str

weight_to_calibrate_key

The setting key this scale’s last calibration weight is stored under.

Type:

str

offset

Tare offset.

Type:

float

i2cbus

The I2C bus connection.

Type:

smbus2.SMBus

error

Error message if any.

Type:

str

error_message_timer

Timer to limit error logging frequency.

Type:

time_utils.Timer

alarm_timer

Timer for non-responsive alarms.

Type:

time_utils.Timer

min

Minimum weight threshold.

Type:

float

max

Maximum weight threshold.

Type:

float

trigger

Receives every reading made by the reader thread (see start()).

Type:

ScaleTriggerBase | None

last_weight

The last weight read by the reader thread.

Type:

float

__init__(name: str, address: str, min_threshold: float, max_threshold: float, calibration_key: str = 'SCALE_CALIBRATION_VALUE', weight_to_calibrate_key: str = 'SCALE_WEIGHT_TO_CALIBRATE') → None[source]

Initializes the Scale.

Parameters:
  • name (str) – Which scale it is (“corridor” or “box”).

  • address (str) – The I2C address as a hex string (e.g., “0x2A”).

  • min_threshold (float) – The minimum weight threshold.

  • max_threshold (float) – The maximum weight threshold.

  • calibration_key (str) – The setting key holding this scale’s calibration factor.

  • weight_to_calibrate_key (str) – The setting key holding the known weight last used to calibrate this scale.

Methods

Attributes

start() → None[source]

Starts reading the scale in its own thread every trigger.period seconds, calling trigger.on_weight with every reading.

stop() → None[source]

Stops the reader thread and waits for it to finish.

tare() → None[source]

Tares the scale (sets the current weight as zero).

calibrate(weight: float) → None[source]

Calibrates the scale with a known weight.

Parameters:

weight (float) – The known weight in grams used for calibration.

get_value() → int[source]

Reads one raw value from the scale (no averaging). A failed I2C read is retried up to 3 attempts in total, since the bus is shared with other devices and a single read can occasionally fail.

Returns:

The raw value (0 if the scale did not respond).

Return type:

int

get_weight() → float[source]

Gets the current weight in grams.

Returns:

The weight in grams.

Return type:

float

real_weight_inference() → tuple[bool, float][source]

Determines if a sequence of weight measurements represents a stable weight.

Conditions to call it a real weight: - standard deviation of the last 5 measurements is

smaller than 10% of the minimum threshold

Returns:

(True, median_weight) if stable, else (False, 0.0).

Return type:

tuple[bool, float]