4.5 KiB
4.5 KiB
Copilot / AI Agent Instructions for system2mqtt
Short, actionable guidance so an AI coding agent can be immediately productive.
- Purpose
- This repo collects host metrics and publishes them to Home Assistant over MQTT.
- Main entry point:
system2mqttconsole script (defined insetup.py).
- How to run (development)
- Create a venv and install deps:
python3 -m venv .venv source .venv/bin/activate pip install -r requirements.txt pip install -e . - Run the service with the provided entry point:
system2mqtt - Environment override: set
SYSTEM2MQTT_CONFIGto point to aconfig.yamlpath.
- Big-picture architecture
system2mqtt.main(console entry) loads configuration, connects to MQTT, discovers collector modules undersrc/system2mqtt/collectors/, and loops to:- call each collector's
collect_metrics() - publish Home Assistant discovery payloads and state/attributes topics
- publish availability to
system2mqtt/{HOSTNAME}/status
- call each collector's
- Collectors are pure Python modules (examples:
collectors/system_metrics.py,collectors/cpu_temperature.py,collectors/zfs_pools.py). The main program imports them dynamically viacollectors.<name>and expects acollect_metrics()function.
- Collector conventions (exact, follow these)
- Each collector should expose
collect_metrics() -> Dictwhich returns a dict with anentitieslist. - Each entity must include at least:
sensor_id,name,value,state_class,unit_of_measurement,device_class, andattributes. - Optional: module-level
DEFAULT_INTERVALcan be present (collectors include this today), but note: currentmaindoes not use per-collector intervals (see "Notable quirks"). - Example entity (from
system_metrics.py):{ "sensor_id": "cpu_usage", "name": "CPU Usage", "value": "12.3", "state_class": "measurement", "unit_of_measurement": "%", "device_class": "power_factor", "attributes": {"friendly_name": "CPU Usage"} }
- Configuration and secrets
- Default config path:
~/.config/system2mqtt/config.yaml(created fromconfig.yaml.exampleon first run). SYSTEM2MQTT_CONFIGenv var can override the config file location.- Required config keys under
mqtt:host,port,username,password,client_id,discovery_prefix.
- MQTT topics & discovery (concrete examples)
- Discovery topic format published by
main:{discovery_prefix}/sensor/system2mqtt_{HOSTNAME}_{sensor_id}/config - State topic format:
system2mqtt/{HOSTNAME}/{sensor_id}/state - Attributes topic:
system2mqtt/{HOSTNAME}/{sensor_id}/attributes(JSON) - Availability:
system2mqtt/{HOSTNAME}/statuswithonline/offlinepayloads (retained)
- Integration points & dependencies
- MQTT client:
paho-mqtt(callback handlers inmain.py). - System metrics:
psutilused bycollectors/system_metrics.py. - ZFS collectors call
zpoolbinaries viasubprocess(requires ZFS tools present on host).
- Notable quirks & places to be careful
- The README mentions per-collector update intervals, and collector modules define
DEFAULT_INTERVAL, butmain.pycurrently uses a fixedtime.sleep(60)and does not schedule collectors individually. If changing scheduling, updatemain.pyto read collectorDEFAULT_INTERVALor configcollectors.intervals. main.pylists collectors from the relativecollectorsdirectory usingos.listdir('collectors'). For correct imports:- Run from package-installed environment (
pip install -e .) and callsystem2mqtt(recommended), or - Ensure working directory /
PYTHONPATHis set socollectorsimport works.
- Run from package-installed environment (
- On first run the code copies
config.yaml.exampleto the user config dir and exits — tests or CI should populate a config before invokingsystem2mqtt.
- Suggested tasks for AI agents (concrete, small changes)
- Implement per-collector scheduling using
DEFAULT_INTERVALorconfig['collectors']['intervals']. - Add unit tests around collector
collect_metrics()return schema (validate required keys). - Improve error handling around dynamic imports (log which path was attempted).
- Where to look in the code
- Entry point & runtime:
src/system2mqtt/main.py - Collector examples:
src/system2mqtt/collectors/*.py(system_metrics.py,cpu_temperature.py,zfs_pools.py) - Example config:
config.yaml.example - Packaging / console script:
setup.py(entry pointsystem2mqtt)
If anything here is unclear or you want the instructions to emphasize other areas (tests, CI, packaging), tell me which part to expand or correct.