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:
@@ -19,16 +19,22 @@ class SnmpChecker(BaseChecker):
|
||||
port = self.config.get("port", 161)
|
||||
community = self.config.get("community", "public")
|
||||
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)
|
||||
|
||||
start = time.time()
|
||||
try:
|
||||
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(
|
||||
community,
|
||||
host,
|
||||
port,
|
||||
ObjectType(ObjectIdentity(oid)),
|
||||
*oids,
|
||||
timeout=timeout_val,
|
||||
retries=1
|
||||
)
|
||||
@@ -48,12 +54,23 @@ class SnmpChecker(BaseChecker):
|
||||
response_time=None
|
||||
)
|
||||
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(
|
||||
success=True,
|
||||
message="SNMP response OK",
|
||||
response_time=response_time,
|
||||
details=values
|
||||
details=details
|
||||
)
|
||||
except Exception as e:
|
||||
return CheckResult(
|
||||
|
||||
@@ -49,6 +49,7 @@ checks:
|
||||
host: "192.168.1.2"
|
||||
community: "public"
|
||||
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
|
||||
|
||||
# DNS checks
|
||||
|
||||
@@ -121,6 +121,29 @@ class LanChecker:
|
||||
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}")
|
||||
|
||||
def _setup_checks(self):
|
||||
@@ -155,6 +178,9 @@ class LanChecker:
|
||||
|
||||
if 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"
|
||||
self.mqtt_client.publish(topic, json.dumps(payload), retain=True)
|
||||
|
||||
Reference in New Issue
Block a user