Reference¶
linux2mqtt package.
SensorType = Literal['sensor', 'binary_sensor']
module-attribute
¶
Sensor type for discovery
StatusType = Literal['online', 'offline']
module-attribute
¶
Metric status
Linux2MqttConfigException
¶
Bases: Linux2MqttException
Bad config exception occurred.
Linux2MqttConnectionException
¶
Bases: Linux2MqttException
Connection processing exception occurred.
Linux2MqttMetricsException
¶
Bases: Linux2MqttException
Metrics processing exception occurred.
Linux2Mqtt
¶
linux2mqtt class.
Attributes:
Name | Type | Description |
---|---|---|
version |
str
|
The version of linux2mqtt |
cfg |
Linux2MqttConfig
|
The config for linux2mqtt |
discovery_binary_sensor_topic |
str
|
Topic template for a binary sensor |
discovery_sensor_topic |
str
|
Topic template for a nary sensor |
status_topic |
str
|
Topic template for a status value |
version_topic |
str
|
Topic template for a version value |
state_topic |
str
|
Topic template for a state dict |
availability_topic |
str
|
Topic template for a availability value |
deferred_metrics_queue |
Queue[BaseMetric]
|
Queue with metrics to publish available through lazy gathering |
do_not_exit |
bool
|
Prevent exit from within linux2mqtt, when handled outside |
Initialize the linux2mqtt.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cfg
|
Linux2MqttConfig
|
The config for the linux2mqtt |
required |
do_not_exit
|
bool
|
Prevent exit from within linux2mqtt, when handled outside |
True
|
Raises:
Type | Description |
---|---|
Linux2MqttConfigException
|
Bad config |
Source code in linux2mqtt/linux2mqtt.py
connect
¶
Initialize the linux2mqtt.
Raises:
Type | Description |
---|---|
Linux2MqttConnectionException
|
If anything with the mqtt connection goes wrong |
Source code in linux2mqtt/linux2mqtt.py
__del__
¶
add_metric
¶
Add metric to linux2mqtt.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
metric
|
BaseMetric
|
The metric to add |
required |
loop_busy
¶
Monitor the metrics and handle the update interval for each metric.
When not connected, it waits for it until the process is exited or a connection is established.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
raise_known_exceptions
|
bool
|
Should any known processing exception be raised or ignored |
False
|
Raises:
Type | Description |
---|---|
Linux2MqttConnectionException
|
If anything with the mqtt connection goes wrong |
Source code in linux2mqtt/linux2mqtt.py
BaseMetric
¶
Base metric class.
Attributes:
Name | Type | Description |
---|---|---|
_name |
str
|
the name of the metric |
unit_of_measurement |
str | None
|
The unit of the metric |
device_class |
str | None
|
The device_class of the metric |
icon |
str | None
|
The icon of the metric |
state_field |
str
|
The field for the state in the data dict of .polled_result |
ha_sensor_typ |
The sensor type of the metric |
|
polled_result |
dict[str, str | int | float | list[str] | list[int] | None] | None
|
The dict with the polled result data for the state and attributes |
Initialize base class.
Source code in linux2mqtt/metrics.py
name_sanitized
property
¶
get_discovery
¶
Get the discovery topic config data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
state_topic
|
str
|
The state topic where to find the data for state and attributes |
required |
availability_topic
|
str
|
The availability topic for the entry |
required |
device_definition
|
LinuxDeviceEntry
|
The device entry fro the homeassistant config |
required |
Returns:
Type | Description |
---|---|
LinuxEntry
|
The homeassistant config entry |
Source code in linux2mqtt/metrics.py
poll
¶
Poll new data for the metric. Can happened instantly or lazily (separate thread for example).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
result_queue
|
Queue[Any]
|
The queue where to post new data if data is gathered lazily |
required |
Returns:
Type | Description |
---|---|
bool
|
False if data is readily available, True if data is gathered lazily |
Raises:
Type | Description |
---|---|
Linux2MqttException
|
General exception. |
Source code in linux2mqtt/metrics.py
BaseMetricThread
¶
Bases: Thread
Base metric thread.
Attributes:
Name | Type | Description |
---|---|---|
result_queue |
Queue[BaseMetric]
|
The queue to put the metric into once data is gathered |
metric |
BaseMetric
|
The metric to gather data for |
interval |
int
|
The interval to gather data over |
CPUMetrics
¶
Bases: BaseMetric
CPU metric.
Attributes:
Name | Type | Description |
---|---|---|
interval |
int
|
The interval to gather cpu data over |
Initialize the cpu metric.
Raises:
Type | Description |
---|---|
Linux2MqttConfigException
|
Bad config |
Source code in linux2mqtt/metrics.py
poll
¶
Poll new data for the cpu metric.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
result_queue
|
Queue[BaseMetric]
|
The queue where to post new data once gathered |
required |
Returns:
Type | Description |
---|---|
bool = False
|
True as the data is gathered lazily |
Raises:
Type | Description |
---|---|
Linux2MqttException
|
General exception |
Source code in linux2mqtt/metrics.py
CPUMetricThread
¶
Bases: BaseMetricThread
CPU metric thread.
Initialize the cpu thread.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
result_queue
|
Queue[BaseMetric]
|
The queue to put the metric into once the data is gathered |
required |
metric
|
BaseMetric
|
The cpu metric to gather data for |
required |
interval
|
int
|
The interval to gather data over |
required |
Source code in linux2mqtt/metrics.py
run
¶
Run the cpu thread. Once data is gathered, it is put into the queue and the thread exits.
Raises:
Type | Description |
---|---|
Linux2MqttMetricsException
|
cpu information could not be gathered or prepared for publishing |
Source code in linux2mqtt/metrics.py
DiskUsageMetrics
¶
Bases: BaseMetric
Disk usage metrics.
Attributes:
Name | Type | Description |
---|---|---|
_name_template |
The template to create the name using the mountpoint as value |
|
mountpoint |
str
|
The mountpoint to check the metric |
Initialize the disk usage metric.
Source code in linux2mqtt/metrics.py
poll
¶
Poll new data for the virtual memory metric.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
result_queue
|
Queue[Self]
|
(Unused) |
required |
Returns:
Type | Description |
---|---|
bool
|
True as the data is readily available |
Raises:
Type | Description |
---|---|
Linux2MqttMetricsException
|
virtual memory information could not be gathered or prepared for publishing |
Source code in linux2mqtt/metrics.py
FanSpeedMetrics
¶
Bases: BaseMetric
Fan speed metric.
Initialize the fan speed metric.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
device
|
str
|
The device |
required |
fan
|
str
|
The fan |
required |
Raises:
Type | Description |
---|---|
Linux2MqttConfigException
|
Bad config |
Source code in linux2mqtt/metrics.py
poll
¶
Poll new data for the thermal zone metric.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
result_queue
|
Queue[Self]
|
(Unused) |
required |
Returns:
Type | Description |
---|---|
bool
|
True as the data is readily available |
Raises:
Type | Description |
---|---|
Linux2MqttMetricsException
|
Fan speed information could not be gathered or prepared for publishing |
Source code in linux2mqtt/metrics.py
NetConnectionMetrics
¶
Bases: BaseMetric
Network connections metric.
Extract local IPs for evaluation during poll.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
interval
|
int
|
The interval to gather data over |
required |
Source code in linux2mqtt/metrics.py
poll
¶
Poll new data for the network connection metrics.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
result_queue
|
Queue[Self]
|
(Unused) |
required |
Returns:
Type | Description |
---|---|
bool
|
True as the data is readily available |
Raises:
Type | Description |
---|---|
Linux2MqttMetricsException
|
network connection information could not be gathered or prepared for publishing |
Source code in linux2mqtt/metrics.py
615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 |
|
NetworkMetrics
¶
Bases: BaseMetric
Network metric thread.
Attributes:
Name | Type | Description |
---|---|---|
_name_template |
The template to create the name using the nic as value |
|
interval |
int
|
The interval to gather cpu data over |
nic |
str
|
The network interface to gather data for. |
Initialize the network metric.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
nic
|
str
|
The network interface |
required |
interval
|
int
|
The interval to gather data over |
required |
Raises:
Type | Description |
---|---|
Linux2MqttConfigException
|
Bad config |
Source code in linux2mqtt/metrics.py
poll
¶
Poll new data for the network metric.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
result_queue
|
Queue[BaseMetric]
|
The queue where to post new data once gathered |
required |
Returns:
Type | Description |
---|---|
bool = False
|
True as the data is gathered lazily |
Raises:
Type | Description |
---|---|
Linux2MqttException
|
General exception |
Source code in linux2mqtt/metrics.py
NetworkMetricThread
¶
Bases: BaseMetricThread
Network metric thread.
Attributes:
Name | Type | Description |
---|---|---|
nic |
The network interface to gather data for. |
Initialize the cpu thread.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
result_queue
|
Queue[BaseMetric]
|
The queue to put the metric into once the data is gathered |
required |
metric
|
BaseMetric
|
The cpu metric to gather data for |
required |
interval
|
int
|
The interval to gather data over |
required |
nic
|
str
|
The network interface |
required |
Source code in linux2mqtt/metrics.py
run
¶
Run the cpu thread. Once data is gathered, it is put into the queue and the thread exits.
Raises:
Type | Description |
---|---|
Linux2MqttMetricsException
|
network information could not be gathered or prepared for publishing |
Source code in linux2mqtt/metrics.py
TempMetrics
¶
Bases: BaseMetric
Thermal zones metric.
Initialize the thermal zone metric.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
device
|
str
|
The device |
required |
thermal_zone
|
str
|
The thermal zone |
required |
Raises:
Type | Description |
---|---|
Linux2MqttConfigException
|
Bad config |
Source code in linux2mqtt/metrics.py
poll
¶
Poll new data for the thermal zone metric.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
result_queue
|
Queue[Self]
|
(Unused) |
required |
Returns:
Type | Description |
---|---|
bool
|
True as the data is readily available |
Raises:
Type | Description |
---|---|
Linux2MqttMetricsException
|
Thermal zone information could not be gathered or prepared for publishing |
Source code in linux2mqtt/metrics.py
VirtualMemoryMetrics
¶
Bases: BaseMetric
Virtual memory metric.
Source code in linux2mqtt/metrics.py
poll
¶
Poll new data for the virtual memory metric.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
result_queue
|
Queue[Self]
|
(Unused) |
required |
Returns:
Type | Description |
---|---|
bool
|
True as the data is readily available |
Raises:
Type | Description |
---|---|
Linux2MqttMetricsException
|
virtual memory information could not be gathered or prepared for publishing |
Source code in linux2mqtt/metrics.py
Linux2MqttConfig
¶
Bases: TypedDict
A config object.
Attributes:
Name | Type | Description |
---|---|---|
log_level |
str
|
Log verbosity |
homeassistant_prefix |
str
|
MQTT discovery topic prefix |
linux2mqtt_hostname |
str
|
A descriptive name for the system being monitored |
mqtt_client_id |
str
|
Client Id for MQTT broker client |
mqtt_user |
str
|
Username for MQTT broker authentication |
mqtt_password |
str
|
Password for MQTT broker authentication |
mqtt_host |
str
|
Hostname or IP address of the MQTT broker |
mqtt_port |
int
|
Port or IP address of the MQTT broker |
mqtt_timeout |
int
|
Timeout for MQTT messages |
mqtt_topic_prefix |
str
|
MQTT topic prefix |
mqtt_qos |
int
|
QOS for standard MQTT messages |
interval |
int
|
Publish metrics to MQTT broker every n seconds |
LinuxDeviceEntry
¶
LinuxEntry
¶
Bases: TypedDict
A linux entry object for discovery in home assistant.
Attributes:
Name | Type | Description |
---|---|---|
name |
str
|
The name of the sensor to display in home assistant |
unique_id |
str
|
The unique id of the sensor in home assistant |
icon |
str | None
|
The icon of the sensor to display |
availability_topic |
str
|
The topic to check the availability of the sensor |
payload_available |
str
|
The payload of availability_topic of the sensor when available |
payload_unavailable |
The payload of availability_topic of the sensor when unavailable |
|
state_topic |
str
|
The topic containing all information for the state of the sensor |
value_template |
str
|
The jinja2 template to extract the state value from the state_topic for the sensor |
unit_of_measurement |
str | None
|
The unit of measurement of the sensor |
payload_on |
str
|
When a binary sensor: The value of extracted state of the sensor to be considered 'on' |
payload_off |
str
|
When a binary sensor: The value of extracted state of the sensor to be considered 'off' |
device |
LinuxDeviceEntry
|
The device the sensor is attributed to |
device_class |
str | None
|
The device class of the sensor |
state_topic |
str
|
The topic containing all information for the attributes of the sensor |
qos |
int
|
The QOS of the discovery message |
clean_for_discovery
¶
Cleanup a typed dict for home assistant discovery, which is quite picky and does not like empty of None values.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
val
|
LinuxEntry
|
The TypedDict to cleanup |
required |
Returns:
Type | Description |
---|---|
dict
|
The cleaned dict |