Utils

class nirahmq.utils.BaseModel[source]

Bases: BaseModel

A customized pydantic.BaseModel class for use by the rest of the library.

nirahmq.utils.clamp(val: T, min_: T, max_: T) T[source]

Clamp a value in the range \([min\_, max\_]\).

Parameters:
  • val (T) – The value to be clamped

  • min_ (T) – The minimum allowed value (inclusive)

  • max_ (T) – The maximum allowed value (inclusive)

Returns:

The clamped value

Return type:

T

nirahmq.utils.sanitize_string(string: str) str[source]

Sanitizes a string for use as an MQTT topic.

Note

The sanitization is not an MQTT restriction but a Home Assistant one.

Parameters:

string (str) – The string to be sanitized

Returns:

The sanitized string

Return type:

str

nirahmq.utils.Unset = _Unset.TOKEN

A custom singleton type used by to indicate a value is unset.

utils.Optional = Optional

A custom type alias that marks a variable as of types T or Unset.

utils.Required = Required

A custom type used by nirahmq.components.base.ComponentBase to mark a class field as required during serialization.