Add optional temperature support to SNMP checker

- New temperature_oid config parameter
- Temperature published as integer via MQTT
- Auto-creates Home Assistant temperature sensor via MQTT Discovery

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-01-26 17:47:01 +01:00
parent c72117e6e1
commit 10fad0d9f3
3 changed files with 47 additions and 3 deletions

View File

@@ -19,16 +19,22 @@ class SnmpChecker(BaseChecker):
port = self.config.get("port", 161) port = self.config.get("port", 161)
community = self.config.get("community", "public") community = self.config.get("community", "public")
oid = self.config.get("oid", "1.3.6.1.2.1.1.1.0") # sysDescr oid = self.config.get("oid", "1.3.6.1.2.1.1.1.0") # sysDescr
temperature_oid = self.config.get("temperature_oid")
timeout_val = self.config.get("timeout", 5) timeout_val = self.config.get("timeout", 5)
start = time.time() start = time.time()
try: try:
with Slim() as slim: with Slim() as slim:
# Build list of OIDs to query
oids = [ObjectType(ObjectIdentity(oid))]
if temperature_oid:
oids.append(ObjectType(ObjectIdentity(temperature_oid)))
error_indication, error_status, error_index, var_binds = await slim.get( error_indication, error_status, error_index, var_binds = await slim.get(
community, community,
host, host,
port, port,
ObjectType(ObjectIdentity(oid)), *oids,
timeout=timeout_val, timeout=timeout_val,
retries=1 retries=1
) )
@@ -48,12 +54,23 @@ class SnmpChecker(BaseChecker):
response_time=None response_time=None
) )
else: else:
values = {str(oid): str(val) for oid, val in var_binds} details = {str(oid): str(val) for oid, val in var_binds}
# Extract temperature if configured
if temperature_oid:
for oid_key, val in var_binds:
if str(oid_key) == temperature_oid:
try:
details["temperature"] = int(val)
except (ValueError, TypeError):
pass # Ignore if not a valid integer
break
return CheckResult( return CheckResult(
success=True, success=True,
message="SNMP response OK", message="SNMP response OK",
response_time=response_time, response_time=response_time,
details=values details=details
) )
except Exception as e: except Exception as e:
return CheckResult( return CheckResult(

View File

@@ -49,6 +49,7 @@ checks:
host: "192.168.1.2" host: "192.168.1.2"
community: "public" community: "public"
oid: "1.3.6.1.2.1.1.1.0" # sysDescr oid: "1.3.6.1.2.1.1.1.0" # sysDescr
temperature_oid: "1.3.6.1.4.1.14988.1.1.3.10.0" # Optional, OID varies by vendor
interval: 120 interval: 120
# DNS checks # DNS checks

View File

@@ -121,6 +121,29 @@ class LanChecker:
retain=True retain=True
) )
# Sensor for temperature (SNMP only, if temperature_oid configured)
if check.get("type") == "snmp" and check.get("temperature_oid"):
temp_config = {
"name": f"{device_name} Temperature",
"unique_id": f"lan_checker_{device_id}_temperature",
"state_topic": f"lan_checker/{device_id}/state",
"value_template": "{{ value_json.temperature | default('unavailable') }}",
"unit_of_measurement": "°C",
"device_class": "temperature",
"state_class": "measurement",
"device": {
"identifiers": [f"lan_checker_{device_id}"],
"name": device_name,
"manufacturer": "LAN Checker",
},
}
self.mqtt_client.publish(
f"homeassistant/sensor/lan_checker_{device_id}_temperature/config",
json.dumps(temp_config),
retain=True
)
logger.info(f"Published discovery for: {device_name}") logger.info(f"Published discovery for: {device_name}")
def _setup_checks(self): def _setup_checks(self):
@@ -155,6 +178,9 @@ class LanChecker:
if result.details: if result.details:
payload["details"] = result.details payload["details"] = result.details
# Extract temperature for SNMP checks
if "temperature" in result.details:
payload["temperature"] = result.details["temperature"]
topic = f"lan_checker/{check['id']}/state" topic = f"lan_checker/{check['id']}/state"
self.mqtt_client.publish(topic, json.dumps(payload), retain=True) self.mqtt_client.publish(topic, json.dumps(payload), retain=True)